-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDomeControlFirmware.ino
3423 lines (3206 loc) · 105 KB
/
DomeControlFirmware.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* --------------------------------------------------------------------
* DomeControlFirmware (https://github.com/reeltwo/DomeControlFirmware)
* --------------------------------------------------------------------
* Written by Mimir Reynisson (skelmir)
*
* Many Thanks to:
* Malcolm MacKenzie
* Greg Hulette
* Neil Hutchison
* Alec Muir
* Tim Hebel
* Stef
* Philip Wise
*
* DomeControlFirmware is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* DomeControlFirmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with DomeControlFirmware; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
///////////////////////////////////
#if __has_include("build_version.h")
#include "build_version.h"
#else
#define BUILD_VERSION "custom"
#endif
#if __has_include("reeltwo_build_version.h")
#include "reeltwo_build_version.h"
#endif
///////////////////////////////////
#ifdef ESP32
#if !defined(ROAM_A_DOME_FULLSIZE_PCB) && \
!defined(ROAM_A_DOME_COMPACT_PCB) && \
!defined(LILYGO_MINI32) && \
!defined(ROAM_A_DOME_DISPLAY)
#error Make sure you select the correct PCB and remove this error
//#define ROAM_A_DOME_FULLSIZE_PCB
#define ROAM_A_DOME_COMPACT_PCB
//#define LILYGO_MINI32
#endif
#elif defined(__AVR_ATmega2560__)
#define ROAM_A_DOME_MEGA_PCB
#elif defined(ARDUINO_ARCH_LINUX)
// Simulator no pins
#else
#error Unsupported platform
#endif
///////////////////////////////////
///////////////////////////////////
// CONFIGURABLE OPTIONS
///////////////////////////////////
#define USE_DEBUG // Define to enable debug diagnostic
#if defined(ROAM_A_DOME_FULLSIZE_PCB) || defined(ROAM_A_DOME_MEGA_PCB)
#define USE_LCD_SCREEN // Define if using LCD and Rotary encoder
#endif
#define USE_SERVOS // Define is enabling servo output on digital out pins
// #define USE_SERVO_DEBUG
// #define USE_VERBOSE_SERVO_DEBUG
#define USE_DOME_DEBUG // Define for dome drive mode debug
#if !defined(__AVR__)
#define USE_VERBOSE_DOME_DEBUG // Define for dome motor specific debug
#endif
#undef USE_DOME_SENSOR_SERIAL_DEBUG // Define for dome sensor ring specific debug
#undef USE_SCREEN_DEBUG
#ifdef ESP32
#define USE_DROID_REMOTE // Define for droid remote support
#define USE_WIFI
#if defined(ROAM_A_DOME_DISPLAY)
#define USE_SPIFFS
#endif
#define USE_PREFERENCES
#endif
#if defined(USE_LCD_SCREEN) || defined(USE_DROID_REMOTE)
#define USE_MENUS // Define if using menu system
#endif
#ifdef USE_WIFI
#define USE_MDNS
#define USE_OTA
#define USE_WIFI_WEB
#define USE_WIFI_MARCDUINO
#endif
///////////////////////////////////
#ifdef USE_DROID_REMOTE
#define REMOTE_ENABLED true
#define SMQ_HOSTNAME "RoamADome"
#define SMQ_SECRET "Astromech"
#endif
#ifdef USE_WIFI
#define WIFI_ENABLED true
// Set these to your desired credentials.
#define WIFI_AP_NAME "RoamADome"
#define WIFI_AP_PASSPHRASE "Astromech"
#define WIFI_ACCESS_POINT true /* true if access point: false if joining existing wifi */
#define MARC_SERIAL_BAUD_RATE 9600
#define MARC_WIFI_ENABLED true
#define MARC_WIFI_SERIAL_PASS true
#endif
#define SETUP_MAX_ANGULAR_VELOCITY 100 /* cm/s */
#define SETUP_VELOCITY_START 40
#define SETUP_VELOCITY_INCREMENT 10
#define DEFAULT_HOME_POSITION 0
#define DEFAULT_SYREN_BAUD 9600
#define DEFAULT_SERIAL_BAUD 9600
#define DEFAULT_PACKET_SERIAL_INPUT true
#define DEFAULT_PACKET_SERIAL_OUTPUT true
#define DEFAULT_PWM_INPUT false
#define DEFAULT_PWM_OUTPUT false
#define DEFAULT_INPUT_SPEED 100
#define DEFAULT_MAX_SPEED 50
#define DEFAULT_RANDOM_MODE false
#define DEFAULT_HOME_MODE false
#define DEFAULT_SPEED_SCALING false
// If true no automatic movement will happen until the dome has been moved by joystick first.
// If false automatic movement can happen on startup.
#define DEFAULT_AUTO_SAFETY true
#define DEFAULT_AUTO_RESTART true
#define DEFAULT_INVERTED false
#define DEFAULT_PWM_MIN_PULSE 1000
#define DEFAULT_PWM_MAX_PULSE 2000
#define DEFAULT_PWM_NEUTRAL_PULSE 1500
#define DEFAULT_PWM_DEADBAND 5
#define DEFAULT_ACCELERATION_SCALE 20
#define DEFAULT_DECELERATION_SCALE 50
#define DEFAULT_DOME_HOME_MIN_DELAY 6
#define DEFAULT_DOME_HOME_MAX_DELAY 8
#define DEFAULT_DOME_AUTO_MIN_DELAY 6
#define DEFAULT_DOME_AUTO_MAX_DELAY 8
#define DEFAULT_DOME_TARGET_MIN_DELAY 0
#define DEFAULT_DOME_TARGET_MAX_DELAY 1
#define DEFAULT_DOME_AUTO_LEFT 80
#define DEFAULT_DOME_AUTO_RIGHT 80
#define DEFAULT_DOME_FUDGE 5
#define DEFAULT_DOME_SPEED_HOME 40
#define DEFAULT_DOME_SPEED_AUTO 30
#define DEFAULT_DOME_SPEED_TARGET 100
#define DEFAULT_DOME_SPEED_MIN 15
#define DEFAULT_DIGITAL_PINS 0
#define DEFAULT_TIMEOUT 5
#define MAX_SPEED 100
#define MAX_SPEED_F float(MAX_SPEED)
#define MAX_FUDGE_FACTOR 20
#define MAX_AUTO_LEFT 180
#define MAX_AUTO_RIGHT 180
#define MAX_AUTO_DELAY 255
#define MAX_ACC_SCALE 255
#define MAX_DEC_SCALE 255
#define MAX_TIMEOUT 30
#define MOVEMODE_MAX_INTERVAL 5 // default interval between random commands
#define MAX_COMMANDS 100
#define KEY_REPEAT_RATE_MS 500 // Key repeat rate in milliseconds
#define SYREN_ADDRESS_INPUT 129
#define SYREN_ADDRESS_OUTPUT 129
#define DOME_SENSOR_SERIAL Serial1
#if defined(__AVR__)
#define CONSOLE_BUFFER_SIZE 300
#define COMMAND_BUFFER_SIZE 256
#else
#define CONSOLE_BUFFER_SIZE 1024
#define COMMAND_BUFFER_SIZE 1024
#endif
#ifdef ESP32
// Default dome sensor baud rate is 115200 for ESP32
#define DOME_SENSOR_SERIAL_BAUD 115200
#else
// Default dome sensor baud rate is 57600 for Mega
#define DOME_SENSOR_SERIAL_BAUD 57600
#endif
#define PACKET_SERIAL_TIMEOUT 1500
///////////////////////////////////
#include "pin-map.h"
#if defined(USE_SERVOS) && !defined(PWM_OUTPUT_PIN)
// Disable servo output if PWM_OUTPUT_PIN is not supported on PCB
#undef USE_SERVOS
#endif
///////////////////////////////////
#ifndef DOME_DRIVE_SERIAL_WRITE
#define DOME_DRIVE_SERIAL Serial2
#define DOME_DRIVE_SERIAL_WRITE Serial2
#define DOME_DRIVE_SERIAL_READ Serial2
#endif
///////////////////////////////////
#ifdef USE_DROID_REMOTE
#include "ReelTwoSMQ32.h"
#else
#include "ReelTwo.h"
#endif
#ifdef ARDUINO_ARCH_LINUX
#define USE_SIMULATOR
#undef DOME_SENSOR_SERIAL
#undef DOME_DRIVE_SERIAL
#endif
#if defined(ESP32) || defined(ARDUINO_ARCH_LINUX)
#define EEPROM_SIZE 4096
#endif
///////////////////////////////////
#include "core/AnimatedEvent.h"
#include "core/StringUtils.h"
#include "core/EEPROMSettings.h"
#include "core/PinManager.h"
#include "drive/DomePosition.h"
#include "drive/SerialConsoleController.h"
#include "drive/DomeDrive.h"
#ifndef USE_SIMULATOR
#include "drive/DomeSensorRingSerialListener.h"
#include "drive/DomeDriveSabertooth.h"
#endif
#ifdef PWM_INPUT_PIN
#ifdef ESP32
#include "encoder/PWMDecoder.h"
#else
#include "encoder/ServoDecoder.h"
#endif
#endif
#ifdef USE_SERVOS
#include "ServoDispatchDirect.h"
#include "ServoEasing.h"
#endif
#ifdef USE_PREFERENCES
#include <Preferences.h>
#define PREFERENCE_REMOTE_ENABLED "remote"
#define PREFERENCE_REMOTE_HOSTNAME "rhost"
#define PREFERENCE_REMOTE_SECRET "rsecret"
#define PREFERENCE_REMOTE_PAIRED "rpaired"
#define PREFERENCE_REMOTE_LMK "rlmk"
#define PREFERENCE_WIFI_ENABLED "wifi"
#define PREFERENCE_WIFI_SSID "ssid"
#define PREFERENCE_WIFI_PASS "pass"
#define PREFERENCE_WIFI_AP "ap"
#define PREFERENCE_MARCWIFI_ENABLED "mwifi"
#define PREFERENCE_MARCWIFI_SERIAL_PASS "mwifipass"
#endif
#ifdef USE_OTA
#include <ArduinoOTA.h>
#endif
#ifdef USE_SPIFFS
#include "SPIFFS.h"
#define USE_FS SPIFFS
#elif defined(USE_FATFS)
#include "FFat.h"
#define USE_FS FFat
#elif defined(USE_LITTLEFS)
#include "LITTLEFS.h"
#define USE_FS LITTLEFS
#endif
#ifdef ESP32
#include "FS.h"
#endif
///////////////////////////////////
#ifdef USE_LVGL_DISPLAY
#include "core/PushButton.h"
#include "TFT_eSPI.h"
#include "lvgl.h"
#define SCREEN_WIDTH 320 // OLED display width, in pixels
#define SCREEN_HEIGHT 170 // OLED display height, in pixels
#define SCREEN_BUFFER_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT)
#define SCREEN_SLEEP_TIMER 30*1000 // Turn off display if idle for 30 seconds
#define LONGPRESS_DELAY 2000
#define SPLASH_SCREEN_DURATION 2000
#define STATUS_SCREEN_DURATION 1000
#define LV_DELAY(x) { \
uint32_t start = millis(); \
do { \
lv_timer_handler(); \
delay(1); \
} while (millis() < start + (x)); \
}
TFT_eSPI tft = TFT_eSPI();
static lv_disp_drv_t disp_drv; // contains callback functions
static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
static lv_color_t *lv_disp_buf;
PushButton button1(PIN_BUTTON_1, true);
PushButton button2(PIN_BUTTON_2, true);
////////////////////////////////
LV_FONT_DECLARE(lv_font_Astromech);
LV_FONT_DECLARE(lv_font_montserrat_32);
LV_FONT_DECLARE(lv_font_montserrat_48);
////////////////////////////////
static const lv_font_t* font_large = &lv_font_Astromech;
static const lv_font_t* font_medium = &lv_font_montserrat_48;
static const lv_font_t* font_normal = &lv_font_montserrat_32;
#endif
///////////////////////////////////
// STATUSLED cannot support PWM INPUT until Adafruit_NeoPixel is fixed or replaced
#undef STATUSLED_PIN
#ifdef STATUSLED_PIN
#include "core/SingleStatusLED.h"
enum {
kNormalMode = 0,
kWifiMode = 1,
kRemoteMode = 2,
kNormalMovingMode = 3,
kWifiMovingMode = 4,
kRemoteMovingMode = 5
};
unsigned sCurrentMode = kNormalMode;
static constexpr uint8_t kStatusColors[][4][3] = {
{ { 0, 2, 0} , { 0, 2, 0} , { 0, 2, 0} , { 0, 2, 0} }, // normal (all green)
{ { 0, 0, 2} , { 0, 0, 2} , { 0, 0, 2} , { 0, 0, 2} }, // wifi enabled (all blue)
{ { 0, 2, 2} , { 0, 2, 2} , { 0, 2, 2} , { 0, 2, 2} }, // remote enabled (all yellow)
{ { 0, 10, 0} , { 10, 0, 0} , { 0, 10, 0} , { 10, 0, 0} }, // green,red,green,red
{ { 0, 0, 10} , { 10, 0, 0} , { 0, 0, 10} , { 10, 0, 0} }, // blue,red,blue,red
{ { 0, 10, 10} , { 10, 0, 0} , { 0, 10, 10} , { 10, 0, 0} } // yellow,red,yellow,red
};
typedef SingleStatusLED<STATUSLED_PIN> StatusLED;
StatusLED statusLED(kStatusColors, SizeOfArray(kStatusColors));
#endif
///////////////////////////////////
#ifdef USE_WIFI
#include "wifi/WifiAccess.h"
#endif
#ifdef USE_MDNS
#include <ESPmDNS.h>
#endif
#ifdef USE_WIFI_WEB
#include "wifi/WifiWebServer.h"
#include "web-images.h"
#endif
#ifdef USE_WIFI_MARCDUINO
#include "wifi/WifiMarcduinoReceiver.h"
#endif
///////////////////////////////////
#ifdef USE_I2C_GPIO_EXPANDER
#include "PCF8574.h"
#ifndef GPIO_EXPANDER_ADDRESS
#define GPIO_EXPANDER_ADDRESS 0x20
#endif
class CustomPinManager : public PinManager
{
public:
CustomPinManager(byte i2cAddress = GPIO_EXPANDER_ADDRESS) :
fGPIOExpander(i2cAddress)
{}
virtual void begin() override
{
fGPIOExpander.begin();
}
virtual bool digitalRead(uint8_t pin) override
{
if (pin >= GPIO_PIN_BASE)
{
return fGPIOExpander.digitalRead(pin-GPIO_PIN_BASE, true);
}
return PinManager::digitalRead(pin);
}
virtual void digitalWrite(uint8_t pin, uint8_t val) override
{
if (pin >= GPIO_PIN_BASE)
{
fGPIOExpander.digitalWrite(pin-GPIO_PIN_BASE, val);
}
else
{
PinManager::digitalWrite(pin, val);
}
}
virtual void pinMode(uint8_t pin, uint8_t mode) override
{
if (pin >= GPIO_PIN_BASE)
{
fGPIOExpander.pinMode(pin-GPIO_PIN_BASE, mode);
}
else
{
PinManager::pinMode(pin, mode);
}
}
protected:
PCF8574 fGPIOExpander;
};
CustomPinManager sPinManager;
#else
PinManager sPinManager;
#endif
///////////////////////////////////
#ifdef USE_WIFI
WifiAccess wifiAccess;
bool wifiEnabled;
bool wifiActive;
#endif
#ifdef USE_DROID_REMOTE
bool remoteEnabled;
bool remoteActive;
#endif
#ifdef USE_WIFI_MARCDUINO
WifiMarcduinoReceiver wifiMarcduinoReceiver(wifiAccess);
#endif
#ifdef USE_PREFERENCES
Preferences preferences;
#endif
#ifdef USE_WIFI
TaskHandle_t eventTask;
#endif
#ifdef USE_OTA
bool otaInProgress;
#endif
bool sDomeHasMovedManually = false;
///////////////////////////////////////////////////////////////////////////////
#ifdef USE_WIFI
String getHostName()
{
String mac = wifiAccess.getMacAddress();
String hostName = mac.substring(mac.length()-5, mac.length());
hostName.remove(2, 1);
hostName = WIFI_AP_NAME+String("-")+hostName;
return hostName;
}
#endif
///////////////////////////////////
bool mountReadOnlyFileSystem()
{
#ifdef USE_SPIFFS
return (SPIFFS.begin(true));
#endif
return false;
}
void unmountFileSystems()
{
#ifdef USE_SPIFFS
SPIFFS.end();
#endif
}
///////////////////////////////////
#ifdef USE_MENUS
#include "Screens.h"
#include "menus/CommandScreen.h"
#ifdef USE_LCD_SCREEN
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "menus/CommandScreenDisplay.h"
class DisplaySSD1306 : public Adafruit_SSD1306
{
public:
DisplaySSD1306() :
Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1)
{
}
void wakeDevice()
{
}
void sleepDevice()
{
}
};
DisplaySSD1306 sLCD;
CommandScreenDisplay<DisplaySSD1306> sDisplay(sLCD, sPinManager, []() {
return sLCD.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
});
#else
#include "menus/CommandScreenHandlerSMQ.h"
CommandScreenHandlerSMQ sDisplay;
#endif
#endif
byte sDigitalPin[] = {
#ifdef DOUT1_PIN
DOUT1_PIN
#endif
#ifdef DOUT2_PIN
,DOUT2_PIN
#endif
#ifdef DOUT3_PIN
,DOUT3_PIN
#endif
#ifdef DOUT4_PIN
,DOUT4_PIN
#endif
#ifdef DOUT5_PIN
,DOUT5_PIN
#endif
#ifdef DOUT6_PIN
,DOUT6_PIN
#endif
#ifdef DOUT7_PIN
,DOUT7_PIN
#endif
#ifdef DOUT8_PIN
,DOUT8_PIN
#endif
};
////////////////////////////////
static unsigned sSerialBaudRates[] = {
2400,
9600,
19200,
38400
};
#ifdef USE_WIFI_WEB
static String sSerialBaudRatesStr[] = {
"2400",
"9600",
"19200",
"38400"
};
#endif
////////////////////////////////
#ifdef USE_SERVOS
// Group ID is used by the ServoSequencer and some ServoDispatch functions to
// identify a group of servos.
//
// Pin Group ID, Min, Max
const ServoSettings sServoSettings[] PROGMEM = {
{ PWM_OUTPUT_PIN, 1000, 2000, 0 }
#ifdef DOUT1_PIN
,{ DOUT1_PIN, 1000, 2000, 0 }
#endif
#ifdef DOUT2_PIN
,{ DOUT2_PIN, 1000, 2000, 0 }
#endif
#ifdef DOUT3_PIN
,{ DOUT3_PIN, 1000, 2000, 0 }
#endif
#ifdef DOUT4_PIN
,{ DOUT4_PIN, 1000, 2000, 0 }
#endif
#ifdef DOUT5_PIN
,{ DOUT5_PIN, 1000, 2000, 0 }
#endif
#ifdef DOUT6_PIN
,{ DOUT6_PIN, 1000, 2000, 0 }
#endif
#ifdef DOUT7_PIN
,{ DOUT7_PIN, 1000, 2000, 0 }
#endif
#ifdef DOUT8_PIN
,{ DOUT8_PIN, 1000, 2000, 0 }
#endif
};
ServoDispatchDirect<SizeOfArray(sServoSettings)> sServoDispatch(sServoSettings);
#endif
////////////////////////////////
#ifdef USE_SIMULATOR
class DomeSensorRingEmulator: public DomePositionProvider
{
public:
DomeSensorRingEmulator()
{
}
inline unsigned getErrorCount()
{
return 0;
}
virtual bool ready() override
{
return true;
}
int normalize(int degrees)
{
degrees = fmod(degrees, 360);
if (degrees < 0)
degrees += 360;
return degrees;
}
virtual int getAngle() override
{
return normalize(fDomePositionDegrees);
}
float fDomePositionDegrees = 0;
private:
bool fReady = false;
};
DomeSensorRingEmulator sDomeRing;
#else
DomeSensorRingSerialListener sDomeRing(DOME_SENSOR_SERIAL);
#endif
DomePosition sDomePosition(sDomeRing);
static void restoreDomeSettings();
class SerialDomeController : public SerialConsoleController, public AnimatedEvent
{
public:
SerialDomeController(Stream& serial) :
SerialConsoleController(serial)
{}
virtual void notify() override
{
if (isEmulationActive())
{
sDomeHasMovedManually = true;
restoreDomeSettings();
}
}
virtual void animate() override
{
switch (read())
{
case kHome:
DEBUG_PRINTLN(F("HOME"));
sDomePosition.setDomeDefaultMode(DomePosition::kHome);
break;
case kEnd:
DEBUG_PRINTLN(F("OFF"));
sDomePosition.setDomeDefaultMode(DomePosition::kOff);
disconnect();
break;
case kPageUp:
increaseSpeed();
DEBUG_PRINT(F("SPEED: ")); DEBUG_PRINTLN(floor(getSpeed()*MAX_SPEED));
break;
case kPageDown:
decreaseSpeed();
DEBUG_PRINT(F("SPEED: ")); DEBUG_PRINTLN(floor(getSpeed()*MAX_SPEED));
break;
case 'R':
case 'r':
DEBUG_PRINTLN(F("RANDOM"));
sDomePosition.setDomeDefaultMode(DomePosition::kRandom);
break;
}
}
};
SerialDomeController sDomeStick(Serial);
#if defined(DOME_DRIVE_SOFT_SERIAL) || defined(COMMAND_SOFT_SERIAL)
#include "SoftwareSerial.h"
#endif
///////////////////////////////////////////////////////////////////////////////
#ifdef USE_SERVOS
class DomeDriveSabertoothPWM : public DomeDriveSabertooth
{
public:
DomeDriveSabertoothPWM(
int id,
Stream& serial,
ServoDispatch& dispatch,
uint8_t pwmNum,
JoystickController& domeStick) :
DomeDriveSabertooth(id, serial, domeStick),
fDispatch(dispatch),
fPWM(pwmNum)
{
}
inline void setOutput(bool pulseOutput, bool packetOutput)
{
if ((fPulseOutput && !pulseOutput) ||
(fPacketOutput && !packetOutput))
{
// Disabling either pulse or packet output. Motor might be
// moving so lets make sure it is stopped.
stop();
}
fPulseOutput = pulseOutput;
fPacketOutput = packetOutput;
}
virtual void stop() override
{
if (fPacketOutput)
{
DomeDriveSabertooth::stop();
}
if (fPulseOutput)
{
fDispatch.moveTo(fPWM, 0.5);
}
}
protected:
ServoDispatch& fDispatch;
uint8_t fPWM;
bool fPacketOutput = false;
bool fPulseOutput = false;
static float map(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
virtual void motor(float m) override
{
if (fPacketOutput)
{
DomeDriveSabertooth::motor(m);
}
if (fPulseOutput)
{
// printf("PWMOUT: %d\n", int(map(m, -1.0f, 1.0f, 0.0f, 1.0f) * 100));
fDispatch.moveTo(fPWM, map(m, -1.0f, 1.0f, 0.0f, 1.0f));
}
}
};
#endif
///////////////////////////////////////////////////////////////////////////////
#ifdef USE_SIMULATOR
class DomeDriveEmulator : public DomeDrive
{
public:
/** \brief Constructor
*
* Only a single instance of WifiSerialBridge should be created per sketch.
*
* \param port the port number of this service
*/
DomeDriveEmulator(DomeSensorRingEmulator& domeRing, JoystickController& domeStick) :
DomeDrive(domeStick),
fDomeRing(domeRing)
{
}
virtual void setup() override
{
}
virtual void stop() override
{
DomeDrive::stop();
}
void setBaudRate(unsigned)
{
}
void setAddress(uint8_t)
{
}
protected:
DomeSensorRingEmulator& fDomeRing;
virtual void motor(float m) override
{
static float sLastMotor;
static int sLastPos;
float pos = fDomeRing.fDomePositionDegrees;
if (sLastMotor != m || sLastPos != int(pos))
{
if (m == 0)
{
printf(" \r");
}
else
{
printf("MOTOR %0.2f POS=%d [%d] \r", m, normalize(pos), int(pos));
}
fflush(stdout);
// printf("MOTOR %0.2f POS=%d [%d]\n", m, normalize(pos), int(pos));
sLastMotor = m;
sLastPos = int(pos);
}
fDomeRing.fDomePositionDegrees -= m;
}
};
DomeDriveEmulator sDomeDrive(sDomeRing, sDomeStick);
#else
#ifdef DOME_DRIVE_SOFT_SERIAL
SoftwareSerial DOME_DRIVE_SERIAL;
#endif
#ifdef USE_SERVOS
DomeDriveSabertoothPWM sDomeDrive(SYREN_ADDRESS_OUTPUT, DOME_DRIVE_SERIAL, sServoDispatch, 0, sDomeStick);
#else
DomeDriveSabertooth sDomeDrive(SYREN_ADDRESS_OUTPUT, DOME_DRIVE_SERIAL, sDomeStick);
#endif
#endif
#ifdef COMMAND_SOFT_SERIAL
SoftwareSerial COMMAND_SERIAL;
#endif
#ifdef USE_SERVOS
struct Channel
{
uint8_t fEasing;
uint16_t fStartPulse;
uint16_t fEndPulse;
uint32_t fGroup;
};
#endif
struct DomeControllerSettings
{
uint8_t fSyrenAddressInput = SYREN_ADDRESS_INPUT;
uint8_t fSyrenAddressOutput = SYREN_ADDRESS_OUTPUT;
uint16_t fHomePosition = DEFAULT_HOME_POSITION;
uint32_t fSensorBaudRate = DOME_SENSOR_SERIAL_BAUD;
uint32_t fSyrenBaudRate = DEFAULT_SYREN_BAUD;
uint32_t fSerialBaudRate = DEFAULT_SERIAL_BAUD;
bool fPacketSerialInput = DEFAULT_PACKET_SERIAL_INPUT;
bool fPacketSerialOutput = DEFAULT_PACKET_SERIAL_OUTPUT;
bool fPWMInput = DEFAULT_PWM_INPUT;
bool fPWMOutput = DEFAULT_PWM_OUTPUT;
uint8_t fMaxSpeed = DEFAULT_MAX_SPEED;
uint8_t fInputSpeed = DEFAULT_INPUT_SPEED;
uint8_t fTimeout = DEFAULT_TIMEOUT;
bool fRandomMode = DEFAULT_RANDOM_MODE;
bool fHomeMode = DEFAULT_HOME_MODE;
bool fSpeedScaling = DEFAULT_SPEED_SCALING;
bool fInverted = DEFAULT_INVERTED;
bool fAutoSafety = DEFAULT_AUTO_SAFETY;
bool fAutoRestart = DEFAULT_AUTO_RESTART;
uint16_t fSetupAngularVelocity = SETUP_MAX_ANGULAR_VELOCITY;
uint16_t fPWMMinPulse = DEFAULT_PWM_MIN_PULSE;
uint16_t fPWMMaxPulse = DEFAULT_PWM_MAX_PULSE;
uint16_t fPWMNeutralPulse = DEFAULT_PWM_NEUTRAL_PULSE;
uint8_t fPWMDeadbandPercent = DEFAULT_PWM_DEADBAND;
uint8_t fAccScale = DEFAULT_ACCELERATION_SCALE;
uint8_t fDecScale = DEFAULT_DECELERATION_SCALE;
uint8_t fDomeAutoMinDelay = DEFAULT_DOME_AUTO_MIN_DELAY;
uint8_t fDomeAutoMaxDelay = DEFAULT_DOME_AUTO_MAX_DELAY;
uint8_t fDomeHomeMinDelay = DEFAULT_DOME_HOME_MIN_DELAY;
uint8_t fDomeHomeMaxDelay = DEFAULT_DOME_HOME_MAX_DELAY;
uint8_t fDomeTargetMinDelay = DEFAULT_DOME_TARGET_MIN_DELAY;
uint8_t fDomeTargetMaxDelay = DEFAULT_DOME_TARGET_MAX_DELAY;
uint8_t fDomeAutoLeft = DEFAULT_DOME_AUTO_LEFT;
uint8_t fDomeAutoRight = DEFAULT_DOME_AUTO_RIGHT;
uint8_t fDomeFudge = DEFAULT_DOME_FUDGE;
uint8_t fDomeSpeedHome = DEFAULT_DOME_SPEED_HOME;
uint8_t fDomeSpeedAuto = DEFAULT_DOME_SPEED_AUTO;
uint8_t fDomeSpeedTarget = DEFAULT_DOME_SPEED_TARGET;
uint8_t fDomeSpeedMin = DEFAULT_DOME_SPEED_MIN;
uint8_t fDigitalPins = DEFAULT_DIGITAL_PINS;
#ifdef USE_SERVOS
Channel fServos[9] = {
{ 0, 1000, 2000, 0 },
{ 0, 1000, 2000, 0 },
{ 0, 1000, 2000, 0 },
{ 0, 1000, 2000, 0 },
{ 0, 1000, 2000, 0 },
{ 0, 1000, 2000, 0 },
{ 0, 1000, 2000, 0 },
{ 0, 1000, 2000, 0 },
{ 0, 1000, 2000, 0 }
};
#endif
};
EEPROMSettings<DomeControllerSettings> sSettings;
///////////////////////////////////////////////////////////////////////////////
#ifdef PWM_INPUT_PIN
#define PWM_MIN_PULSE 800
#define PWM_MAX_PULSE 2200
ServoDecoder pulseInput([](int pin, uint16_t pulse) {
float drive = 0;
long min_pulse = sSettings.fPWMMinPulse;
long neutral_pulse = sSettings.fPWMNeutralPulse;
long max_pulse = sSettings.fPWMMaxPulse;
uint8_t deadband = sSettings.fPWMDeadbandPercent;
if (pulse > PWM_MIN_PULSE && pulse < PWM_MAX_PULSE)
{
if (pulse < min_pulse)
pulse = min_pulse;
else if (pulse > max_pulse)
pulse = max_pulse;
if (pulse < neutral_pulse)
{
drive = -float(neutral_pulse - pulse) / (neutral_pulse - min_pulse);
}
else
{
drive = float(pulse - neutral_pulse) / (max_pulse - neutral_pulse);
}
if (float(deadband)/100 >= abs(drive))
{
drive = 0;
}
else if (!sDomeHasMovedManually)
{
sDomeHasMovedManually = true;
restoreDomeSettings();
}
if (sVerboseDomeDebug)
printf("PWM: %d (pulse: %d [%d:%d:%d])\n", int(drive * 100), pulse, int(min_pulse), int(max_pulse), int(neutral_pulse));
if (sSettings.fPWMInput)
{
abortSerialCommand();
sDomeDrive.driveDome(drive);
}
}
else
{
DEBUG_PRINTLN("BAD PULSE");
}
}, PWM_INPUT_PIN);
#endif
///////////////////////////////////////////////////////////////////////////////
static uint8_t getByteBit(uint8_t &byte, uint8_t bit)
{
bit &= 7;
return (byte >> bit) & 1;
}