forked from jhaupt/NearZero1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialPrompt.ino
1236 lines (1207 loc) · 45.5 KB
/
SerialPrompt.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
void PROMPT_maxI1Entry(){
SerialReceiveLoop();
maxIset1 = atoi(rxchars); //Convert rxchars to an integer
fholder = maxIset1/1000; //Convert mA to A, ONLY used for displaying value in Amps via terminal
if (maxIset1 > 1000){
CheckConfig();
fholder = maxIset1/1000; //Convert mA to A
Serial.print(F("Maximum PWM current is 1A. CURRENT LEFT AT "));
Serial.print(fholder);
Serial.println(F("A"));
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return; //Yes that's a goto. FRAK modern programming sensibilities.
}
else if (maxIset1 > 0 && maxIset1 <= 1000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
fholder = maxIset1/1000; //Convert mA to A
Serial.print(F("MAX PWM CURRENT LEFT AT "));
Serial.print(fholder);
Serial.println(F("A"));
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
//Integers stored in the EEPROM must be <=255, so we split our numbers in two, storing 2 digits in one EEPROM register and 2 in another.
if (maxIset1 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = maxIset1; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(maxIset1/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = maxIset1 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_maxIset1l, lhlf); //write the left two digits of Iset1 to the address location given by addr_.
EEPROM.write(addr_maxIset1r, rhlf); //etc.
}
void PROMPT_maxI2Entry(){
SerialReceiveLoop();
maxIset2 = atoi(rxchars); //Convert rxchars to an integer
fholder = maxIset2/1000; //Convert mA to A, ONLY used for displaying value in Amps via terminal
if (maxIset2 > 1000){
CheckConfig();
fholder = maxIset2/1000; //Convert mA to A
Serial.print(F("Maximum PWM current is 1A. CURRENT LEFT AT "));
Serial.print(fholder);
Serial.println(F("A"));
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return; //Yes that's a goto. FRAK modern programming sensibilities.
}
else if (maxIset2 > 0 && maxIset2 <= 1000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
fholder = maxIset2/1000; //Convert mA to A
Serial.print(F("MAX PWM CURRENT LEFT AT "));
Serial.print(fholder);
Serial.println(F("A"));
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
//Integers stored in the EEPROM must be <=255, so we split our numbers in two, storing 2 digits in one EEPROM register and 2 in another.
if (maxIset2 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = maxIset2; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(maxIset2/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = maxIset2 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_maxIset2l, lhlf); //write the left two digits of Iset1 to the address location given by addr_.
EEPROM.write(addr_maxIset2r, rhlf); //etc.
}
void PROMPT_minI1Entry(){
SerialReceiveLoop();
minIset1 = atoi(rxchars); //Convert rxchars to an integer
fholder = minIset1/1000; //Convert mA to A, ONLY used for displaying value in Amps via terminal
if (minIset1 > 1000){
CheckConfig();
fholder = minIset1/1000; //Convert mA to A
Serial.print(F("Maximum PWM current is 1A. CURRENT LEFT AT "));
Serial.print(fholder);
Serial.println(F("A"));
Serial.println(F("\n\n"));
exitflag = true;
return;
}
else if (minIset1 == 0) {minIset1 = 1;} //If it was zero enter 1. We can't have zero, but 1 is almost zero so just as good.
else if (minIset1 > 0 && minIset1 <= 1000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
minIset1 = maxIset1; //The default is to make the resting current the same as the non-resting current.
Serial.print(F("THE SAME CURRENT WILL BE USED FOR RESTING AND NON-RESTING PWM OPERATION"));
Serial.println(F("\n\n"));
exitflag = true;
return;
}
//Integers stored in the EEPROM must be <=255, so we split our numbers in two, storing 2 digits in one EEPROM register and 2 in another.
if (minIset1 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = minIset1; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(minIset1/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = minIset1 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_minIset1l, lhlf); //write the left two digits of Iset1 to the address location given by addr_.
EEPROM.write(addr_minIset1r, rhlf); //etc.
}
void PROMPT_minI2Entry(){
SerialReceiveLoop();
minIset2 = atoi(rxchars); //Convert rxchars to an integer
fholder = minIset2/1000; //Convert mA to A, ONLY used for displaying value in Amps via terminal
if (minIset2 > 1000){
CheckConfig();
fholder = minIset2/1000; //Convert mA to A
Serial.print(F("Maximum current is 1A. CURRENT LEFT AT "));
Serial.print(fholder);
Serial.println(F("A"));
Serial.println(F("\n\n"));
exitflag = true;
return;
}
else if (minIset2 == 0) {minIset2 = 1;} //If it was zero enter 1. We can't have zero, but 1 is almost zero so just as good.
else if (minIset2 > 0 && minIset2 <= 1000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
minIset2 = maxIset2; //The default is to make the resting current the same as the non-resting current.
Serial.print(F("THE SAME CURRENT WILL BE USED FOR RESTING AND NON-RESTING PWM OPERATION"));
Serial.println(F("\n\n"));
exitflag = true;
return;
}
//Integers stored in the EEPROM must be <=255, so we split our numbers in two, storing 2 digits in one EEPROM register and 2 in another.
if (minIset2 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = minIset2; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(minIset2/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = minIset2 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_minIset2l, lhlf); //write the left two digits of Iset1 to the address location given by addr_.
EEPROM.write(addr_minIset2r, rhlf); //etc.
}
void PROMPT_gain1Entry(){
SerialReceiveLoop();
gain1 = atoi(rxchars); //Convert rxchars to an integer
if (gain1 > 1000){
CheckConfig();
Serial.print(F("Maximum gain is 1000. PWM GAIN LEFT AT "));
Serial.println(gain1);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (gain1 > 0 && gain1 <= 1000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
Serial.print(F("PWM GAIN LEFT AT "));
Serial.println(gain1);
Serial.println(F("\n\n"));
exitflag = true;
return;
}
if (gain1 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = gain1; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(gain1/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = gain1 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_gain1l, lhlf);
EEPROM.write(addr_gain1r, rhlf);
}
void PROMPT_gain2Entry(){
SerialReceiveLoop();
gain2 = atoi(rxchars); //Convert rxchars to an integer
if (gain2 > 1000){
CheckConfig();
Serial.print(F("Maximum gain is 1000. PWM GAIN LEFT AT "));
Serial.println(gain2);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (gain2 > 0 && gain2 <= 1000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
Serial.print(F("PWM GAIN LEFT AT "));
Serial.println(gain2);
Serial.println(F("\n\n"));
exitflag = true;
return;
}
if (gain2 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = gain2; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(gain2/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = gain2 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_gain2l, lhlf);
EEPROM.write(addr_gain2r, rhlf);
}
void PROMPT_pwmoffset1Entry(){
SerialReceiveLoop();
pwmoffset1 = atoi(rxchars); //Convert rxchars to an integer
if (pwmoffset1 > 10000){
CheckConfig();
Serial.print(F("Maximum center offset is 10000. PWM CENTER LEFT AT "));
Serial.println(pwmoffset1);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (pwmoffset1 >= 0 && pwmoffset1 <= 10000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.}
else {
CheckConfig();
Serial.print(F("PWM CENTER OFFSET LEFT AT "));
Serial.println(pwmoffset1);
Serial.println(F("\n\n"));
exitflag = true;
return;
}
if (pwmoffset1 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = pwmoffset1; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(pwmoffset1/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = pwmoffset1 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_pwmoffset1l, lhlf);
EEPROM.write(addr_pwmoffset1r, rhlf);
}
void PROMPT_pwmoffset2Entry(){
SerialReceiveLoop();
pwmoffset2 = atoi(rxchars); //Convert rxchars to an integer
if (pwmoffset2 > 10000){
CheckConfig();
Serial.print(F("Maximum center offset is 10000. PWM CENTER LEFT AT "));
Serial.println(pwmoffset2);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (pwmoffset2 >= 0 && pwmoffset2 <= 10000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
Serial.print(F("PWM CENTER OFFSET LEFT AT "));
Serial.println(pwmoffset2);
Serial.println(F("\n\n"));
exitflag = true;
return;
}
if (pwmoffset2 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = pwmoffset2; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(pwmoffset2/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = pwmoffset2 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_pwmoffset2l, lhlf);
EEPROM.write(addr_pwmoffset2r, rhlf);
}
void PROMPT_tsphase1Entry(){
SerialReceiveLoop();
tsphase1 = atoi(rxchars); //Convert rxchars to an integer
if (tsphase1 > 6283){
CheckConfig();
Serial.print(F("Maximum phase is 2pi (6283). PHASE LEFT AT "));
Serial.println(tsphase1);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (tsphase1 > 0 && tsphase1 <= 6283){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
Serial.println(F("PHASE SET TO 0\n"));
Serial.println(F("\n"));
exitflag = true;
return;
}
if (tsphase1 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = tsphase1; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(tsphase1/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = tsphase1 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_tsphase1l, lhlf);
EEPROM.write(addr_tsphase1r, rhlf);
}
void PROMPT_tsphase2Entry(){
SerialReceiveLoop();
tsphase2 = atoi(rxchars); //Convert rxchars to an integer
if (tsphase2 > 6283){
CheckConfig();
Serial.print(F("Maximum phase is 2pi (6283). PHASE LEFT AT "));
Serial.println(tsphase2);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (tsphase2 > 0 && tsphase2 <= 6283){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
Serial.println(F("PHASE SET TO 0\n"));
Serial.println(F("\n"));
exitflag = true;
return;
}
if (tsphase2 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = tsphase2; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(tsphase2/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = tsphase2 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_tsphase2l, lhlf);
EEPROM.write(addr_tsphase2r, rhlf);
}
void PROMPT_tscoeff1Entry(){
SerialReceiveLoop();
tscoeff1 = atoi(rxchars); //Convert rxchars to an integer
if (tscoeff1 > 254){
CheckConfig();
Serial.print(F("Maximum value is 254. AGGRESSIVENESS LEFT AT "));
Serial.println(tscoeff1);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (tscoeff1 > 0 && tscoeff1 <255){ //If it's a valid evtry
EEPROM.write(addr_tscoeff1, tscoeff1);
}
else { //Then it's invalid in some other way
CheckConfig();
Serial.print(F("AGGRESSIVENESS LEFT AT "));
Serial.println(tscoeff1);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
}
void PROMPT_tscoeff2Entry(){
SerialReceiveLoop();
tscoeff2 = atoi(rxchars); //Convert rxchars to an integer
if (tscoeff2 > 254){
CheckConfig();
Serial.print(F("Maximum value is 254. AGGRESSIVENESS LEFT AT "));
Serial.println(tscoeff2);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (tscoeff2 > 0 && tscoeff2 <255){ //If it's a valid evtry
EEPROM.write(addr_tscoeff2, tscoeff2);
}
else { //Then it's invalid in some other way
CheckConfig();
Serial.print(F("AGGRESSIVENESS LEFT AT "));
Serial.println(tscoeff2);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
}
void PROMPT_accel1Entry(){
SerialReceiveLoop();
accel1 = atoi(rxchars); //Convert rxchars to an integer
if (accel1 > 10000){
CheckConfig();
Serial.print(F("Maximum acceleration is 10,000. ACCELERATION LEFT AT "));
Serial.println(accel1);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (accel1 > 0 && accel1 <= 10000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
Serial.print(F("ACCELERATION LEFT AT "));
Serial.println(accel1);
Serial.println(F("\n\n"));
exitflag = true;
return;
}
if (accel1 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = accel1; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(accel1/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = accel1 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_accel1l, lhlf);
EEPROM.write(addr_accel1r, rhlf);
}
void PROMPT_accel2Entry(){
SerialReceiveLoop();
accel2 = atoi(rxchars); //Convert rxchars to an integer
if (accel2 > 10000){
CheckConfig();
Serial.print(F("Maximum acceleration is 10,000. ACCELERATION LEFT AT "));
Serial.println(accel2);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (accel2 > 0 && accel2 <= 10000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
Serial.print(F("ACCELERATION LEFT AT "));
Serial.println(accel2);
Serial.println(F("\n\n"));
exitflag = true;
return;
}
if (accel2 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = accel2; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(accel2/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = accel2 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_accel2l, lhlf);
EEPROM.write(addr_accel2r, rhlf);
}
void PROMPT_maxslewvel1Entry(){
SerialReceiveLoop();
maxslewvel1 = atoi(rxchars); //Convert rxchars to an integer
if (maxslewvel1 > 1000){
CheckConfig();
Serial.print(F("Maximum MAX SLEW RATE is 1,000. MAX SLEW RATE LEFT AT "));
Serial.println(maxslewvel1);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (maxslewvel1 > 0 && maxslewvel1 <= 1000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
Serial.print(F("MAX SLEW RATE LEFT AT "));
Serial.println(maxslewvel1);
Serial.println(F("\n\n"));
exitflag = true;
return;
}
if (maxslewvel1 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = maxslewvel1; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(maxslewvel1/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = maxslewvel1 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_maxslewvel1l, lhlf);
EEPROM.write(addr_maxslewvel1r, rhlf);
}
void PROMPT_maxslewvel2Entry(){
SerialReceiveLoop();
maxslewvel2 = atoi(rxchars); //Convert rxchars to an integer
if (maxslewvel2 > 1000){
CheckConfig();
Serial.print(F("Maximum MAX SLEW RATE is 1,000. MAX SLEW RATE LEFT AT "));
Serial.println(maxslewvel2);
Serial.println(F("\n\n"));
exitflag = true;
delay(2000);
return;
}
else if (maxslewvel2 > 0 && maxslewvel2 <= 1000){} //Don't do anything. This is just here so we can have the below "else" to detect an invalid entry.
else {
CheckConfig();
Serial.print(F("MAX SLEW RATE LEFT AT "));
Serial.println(maxslewvel2);
Serial.println(F("\n\n"));
exitflag = true;
return;
}
if (maxslewvel2 < 100){ //If it's a 1 or 2 digit number
lhlf = 0; //The left half of the four digit number is 0
rhlf = maxslewvel2; //And the right half is just the number
}
else { //If it's a 3 or 4 digit number
lhlf = floor(maxslewvel2/100); //The left half is the leading digit of a 3 digit number or leading 2 digits of a 4 digit number.
rhlf = maxslewvel2 - (lhlf*100); //The right half is the trailing two digits of the number.
}
EEPROM.write(addr_maxslewvel2l, lhlf);
EEPROM.write(addr_maxslewvel2r, rhlf);
}
void SerialReceiveLoop(){
int ndx = 0;
char endMarker = '\n';
char rc;
newdata = false;
while (newdata == false){
while (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
rxchars[ndx] = rc;
ndx++;
if (ndx >= nchars) {
ndx = nchars - 1;
}
}
else {
rxchars[ndx] = '\0'; // terminate the string
ndx = 0;
newdata = true;
}
}
}
}
int SerialPrompt(){
//MAIN MENU
mainmenu:
exitflag = false;
Serial.println(F("\n\n\n"));
Serial.println(F("~~~~Welcome to the NearZero Configuration Terminal~~~~"));
Serial.println(F(" Firmware version 2.0 -- by J. Haupt"));
Serial.println(F(" CHANGES TAKE EFFECT AFTER CYCLING POWER"));
Serial.println(F(" or RESETTING\n"));
Serial.println(F("1: Channel 1 configuration"));
Serial.println(F("2: Channel 2 configuration"));
Serial.println(F("3: Set I2C Address"));
Serial.println(F("4: List current settings"));
Serial.println(F("5: Reset factory defaults"));
Serial.println(F("q: Quit/restart (use after changing INPUT SELECT jumper as desired)"));
Serial.println(F("\n\n>>\n"));
SerialReceiveLoop();
//CHANNEL 1 CONFIGURATION
if (rxchars[0] == '1'){
ch1menu:
exitflag = false;
Serial.println(F("Channel 1 General Settings:"));
Serial.println(F("1: Set SENSOR type (ENCODER or HALL)"));
Serial.println(F("2: Set maximum position/servo SLEW RATE"));
Serial.println(F("3: Set position/servo ACCELERATION"));
Serial.println(F("4: Set directionality (NORMAL or REVERSE)"));
Serial.println(F("5: Set waveform TORQUE SMOOTHING (experimental)"));
Serial.println(F(" "));
Serial.println(F("Channel 1 PWM Settings:"));
Serial.println(F("6: Set PWM running/resting CURRENT"));
Serial.println(F("7: Set PWM command type (VELOCITY, POSITION, or SERVO)"));
Serial.println(F("8: Set PWM command GAIN"));
Serial.println(F("9: Set PWM center OFFSET or to center AUTO"));
Serial.println(F(" "));
Serial.println(F("t: Test/run channel 1 motor"));
Serial.println(F("m: Return to main menu"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
exitflag =false;
//**Set SENSOR TYPE
if (rxchars[0] == '1'){
Serial.println(F("Set channel 1 sensor type:"));
Serial.println(F(" e: ENCODER -- reports quadrature ticks (signed integers counted since powerup"));
Serial.println(F(" over I2C for external closed-loop control -- also needed for SERVO command mode"));
Serial.println(F(" h: HALL -- reports hall ticks (signed integers counted since powerup)"));
Serial.println(F(" over I2C for external closed-loop control"));
Serial.println(F("\n (Or strike 'Enter' to leave as is)"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
if (rxchars[0] == 'e' || rxchars[0] == 'E'){
EEPROM.write(addr_sensortype1, 0); //0 = none
Serial.println(F("CHANNEL 1 SENSOR SET TO 'ENCODER'"));
}
else if (rxchars[0] == 'h' || rxchars[0] == 'H'){
exitflag = false;
EEPROM.write(addr_sensortype1, 1); //1 = encoder
Serial.print(F("CHANNEL 1 SENSOR SET TO 'HALL'"));
}
else{
Serial.print(F("SENSOR TYPE NOT CHANGED"));
}
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu;
}
//**Set MAX SLEW RATE
else if (rxchars[0] == '2'){
Serial.println(F("Set channel 1 position/servo maximum slew rate."));
Serial.println(F("\n This is an integer in arbitrary units between 1 and 1000. Must be low enough to both"));
Serial.println(F(" prevent the motor from stalling and to allow accurate tracking of an encoder:"));
Serial.println(F(" Strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_maxslewvel1Entry();
if (exitflag == true){
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
else {
Serial.print(F("CHANNEL 1 MAXIMUM SLEW RATE SET TO "));
Serial.print(maxslewvel1);
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu;
}
}
//**Set ACCELERATION
else if (rxchars[0] == '3'){
Serial.println(F("Set channel 1 position/servo acceleration."));
Serial.println(F("\n This is an integer in arbitrary units between 1 and 9999."));
Serial.println(F(" Enter 10000 to turn off acceleration dynamics entirely."));
Serial.println(F(" Strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_accel1Entry();
if (exitflag == true){
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
if (accel1 == 10000){
Serial.println(F("CHANNEL 1 ACCELERATION DYNAMICS TURNED OFF"));
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
else {
Serial.print(F("CHANNEL 1 ACCELERATION SET TO "));
Serial.print(accel1);
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
}
//**Set directionality to NORMAL or REVERSE
else if (rxchars[0] == '4'){
Serial.println(F("Set channel 1 directionality:"));
Serial.println(F(" n: NORMAL (default)"));
Serial.println(F(" r: REVERSE "));
Serial.println(F("\n (Or strike 'Enter' to leave as is)"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
if (rxchars[0] == 'n' || rxchars[0] == 'N'){
EEPROM.write(addr_dir1, 0); //0 = NORMAL
Serial.print(F("CHANNEL 1 DIRECTIONALITY SET TO 'NORMAL'"));
}
else if (rxchars[0] == 'r' || rxchars[0] == 'R'){
EEPROM.write(addr_dir1, 1); //1 = REVERSE
Serial.print(F("CHANNEL 1 DIRECTIONALITY SET TO 'REVERSE'"));
}
else {
Serial.println(F("DIRECTIONALITY NOT CHANGED"));
}
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu;
}
//**Set waveform TORQUE SMOOTHING
else if (rxchars[0] == '5'){
Serial.println(F("Set channel 1 commutation waveform for torque smoothing:"));
Serial.println(F(" Torque smoothing may improve motion smoothness with some motors."));
Serial.println(F(" If so inclined, the aggressiveness and phase may be tweaked by trial and error.\n"));
Serial.println(F(" 0: OFF. Pure sine commutation"));
Serial.println(F(" 1: ON. A correction function (two-term fourier series) is added to the sine waveform."));
Serial.println(F(" 2: Set aggressiveness of correction function (when ON)"));
Serial.println(F(" 3: Set phase of correction function (when ON)"));
Serial.println(F("\n (Or strike 'Enter' to leave as is)"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
if (rxchars[0] == '0'){
EEPROM.write(addr_torqueprofile1, 0); //0 = sine
Serial.print(F("CHANNEL 1 TORQUE SMOOTHING IS OFF (PURE SINE COMMUTATION)"));
}
else if (rxchars[0] == '1'){
EEPROM.write(addr_torqueprofile1, 1);
Serial.print(F("CHANNEL 1 TORQUE SMOOTHING IS ON"));
}
else if (rxchars[0] == '2'){
Serial.println(F("Enter aggressiveness parameter. This is an integer between 1 and 254 (arbitarary units)"));
Serial.println(F(" or strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_tscoeff1Entry();
if (exitflag == true){
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
Serial.print(F("CHANNEL 1 TORQUE SMOOTHING AGGRESSIVENESS SET TO "));
Serial.println(tscoeff1);
if (torqueprofile1 == 0){
Serial.println(F("(only applies when torque smoothing is ON)"));
}
}
else if (rxchars[0] == '3'){
Serial.println(F("Enter phase/offset of correction function. This is a number between 0 and 2pi (6.283) in thousands"));
Serial.println(F("(i.e. enter a number between 0 and 6283) or strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_tsphase1Entry();
if (exitflag == true){
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
Serial.print(F("CHANNEL 1 TORQUE SMOOTHING PHASE SET TO "));
fholder = tsphase1/1000; //Convert to decimal form
Serial.println(fholder);
if (torqueprofile1 == 0){
Serial.println(F("(only applies when torque smoothing is ON)"));
}
}
else {
Serial.println(F("TORQUE SMOOTHING NOT CHANGED"));
}
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu;
}
//**Set Power
else if (rxchars[0] == '6'){
Serial.println(F("Enter channel 1 PWM running current in mA (e.g. enter \"250\" for .25A)."));
Serial.println(F("\n This sets the current for when the motion is in motion."));
Serial.println(F(" Or strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_maxI1Entry();
if (exitflag == true){
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
Serial.print(F("CHANNEL 1 RUNNING CURRENT SET TO "));
Serial.print(fholder);
Serial.println(F("A"));
Serial.println(F("\nEnter channel 1 PWM resting current in mA (e.g. enter \"100\" for .1A)"));
Serial.println(F(" Or strike 'Enter' to not reduce current while at rest:"));
Serial.println(F("\n\n>>"));
PROMPT_minI1Entry();
if (exitflag == true){
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
Serial.print(F("CHANNEL 1 PWM RESTING CURRENT SET TO "));
Serial.print(fholder);
Serial.println(F("A"));
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu;
}
//**Set PWM command type to VELOCITY or POSITION
else if (rxchars[0] == '7'){
Serial.println(F("Set channel 1 to interpret PWM input as either velocity, position, or servo commands:"));
Serial.println(F(" v: VELOCITY"));
Serial.println(F(" p: POSITION"));
Serial.println(F(" s: SERVO (same as POSITION but with closed-loop control) -- requires encoder"));
Serial.println(F("\n (Or strike 'Enter' to leave as is)"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
if (rxchars[0] == 'v' || rxchars[0] == 'V'){
EEPROM.write(addr_commandmode1, 0); //0 = velocity
Serial.print(F("CHANNEL 1 PWM INPUT SET TO 'VELOCITY'"));
}
else if (rxchars[0] == 'p' || rxchars[0] == 'P'){
EEPROM.write(addr_commandmode1, 1); //1 = position
Serial.print(F("CHANNEL 1 PWM INPUT SET TO 'POSITION'"));
}
else if (rxchars[0] == 's' || rxchars[0] == 'S'){
EEPROM.write(addr_commandmode1, 2); //2 = servo
Serial.print(F("CHANNEL 1 PWM INPUT SET TO 'SERVO'"));
}
else {
Serial.println(F("COMMAND TYPE NOT CHANGED"));
}
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu;
}
//**Set velocity/position GAIN
else if (rxchars[0] == '8'){
Serial.println(F("Set channel 1 PWM command gain."));
Serial.println(F(" This is the scaling coefficient for velocity or position inputs when in PWM-command mode."));
Serial.println(F("\n Enter an integer between 1 and 1000 (arbitrary units)"));
Serial.println(F(" Strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_gain1Entry();
if (exitflag == true){
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
Serial.print(F("\nCHANNEL 1 PWM GAIN SET TO "));
Serial.print(gain1);
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu;
}
//**Set PWM CENTER
else if (rxchars[0] == '9'){
Serial.println(F("Set channel 1 PWM center offset."));
Serial.println(F(" This defines the pulse width which sets the command-neutral position."));
Serial.println(F("\n Enter an integer between 1 and 9000."));
Serial.println(F(" Enter 0 to automatically use the first detected PWM value as the command-neutral position:"));
Serial.println(F(" Strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_pwmoffset1Entry();
if (exitflag == true){
delay(2000);
goto ch1menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
if (pwmoffset1 == 0) {
Serial.print(F("\nTHE INITIAL PWM INPUT WILL BE TAKEN AS COMMAND-NEUTRAL"));
}
else {
Serial.print(F("\nCHANNEL 1 PWM CENTER SET TO "));
Serial.print(pwmoffset1);
}
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch1menu;
}
//Test Motor
else if (rxchars[0] == 't' || rxchars[0] == 'T'){
Serial.println(F("\nRunning motor on channel 1 for about 10s..."));
TorqueCal1();
goto ch1menu;
}
//**Return to main menu
else if (rxchars[0] == 'm'){
goto mainmenu;
}
}
//CHANNEL 2 CONFIGURATION
if (rxchars[0] == '2'){
ch2menu:
exitflag = false;
Serial.println(F("Channel 2 General Settings:"));
Serial.println(F("1: Set SENSOR type (ENCODER or HALL)"));
Serial.println(F("2: Set maximum position/servo SLEW RATE"));
Serial.println(F("3: Set position/servo ACCELERATION"));
Serial.println(F("4: Set directionality (NORMAL or REVERSE)"));
Serial.println(F("5: Set waveform TORQUE SMOOTHING (experimental)"));
Serial.println(F(" "));
Serial.println(F("Channel 2 PWM Settings:"));
Serial.println(F("6: Set PWM running/resting CURRENT"));
Serial.println(F("7: Set PWM command type (VELOCITY, POSITION, or SERVO)"));
Serial.println(F("8: Set PWM command GAIN"));
Serial.println(F("9: Set PWM center OFFSET or to center AUTO"));
Serial.println(F(" "));
Serial.println(F("t: Test/run channel 2 motor"));
Serial.println(F("m: Return to main menu"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
exitflag =false;
//**Set SENSOR TYPE
if (rxchars[0] == '1'){
Serial.println(F("Set channel 2 sensor type:"));
Serial.println(F(" e: ENCODER -- reports quadrature ticks (signed integers counted since powerup)"));
Serial.println(F(" over I2C for external closed-loop control -- also needed for SERVO command mode"));
Serial.println(F(" h: HALL -- reports hall ticks (signed integers counted since powerup) over I2C"));
Serial.println(F(" over I2C for external closed-loop control"));
Serial.println(F("\n (Or strike 'Enter' to leave as is)"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
if (rxchars[0] == 'e' || rxchars[0] == 'E'){
EEPROM.write(addr_sensortype2, 0);
Serial.println(F("CHANNEL 2 SENSOR SET TO 'ENCODER'"));
}
else if (rxchars[0] == 'h' || rxchars[0] == 'H'){
exitflag = false;
EEPROM.write(addr_sensortype2, 1);
Serial.print(F("CHANNEL 2 SENSOR SET TO 'HALL'"));
}
else{
Serial.print(F("SENSOR TYPE NOT CHANGED"));
}
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch2menu;
}
//**Set MAX SLEW RATE
else if (rxchars[0] == '2'){
Serial.println(F("Set channel 2 position/servo maximum slew rate."));
Serial.println(F("\n This is an integer in arbitrary units between 1 and 1000. Must be low enough to both"));
Serial.println(F(" prevent the motor from stalling and to allow accurate tracking of an encoder:"));
Serial.println(F(" Strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_maxslewvel2Entry();
if (exitflag == true){
delay(2000);
goto ch2menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
else {
Serial.print(F("CHANNEL 1 MAXIMUM SLEW RATE SET TO "));
Serial.print(maxslewvel1);
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch2menu;
}
}
//**Set ACCELERATION
else if (rxchars[0] == '3'){
Serial.println(F("Set channel 2 position/servo acceleration."));
Serial.println(F("\n This is an integer in arbitrary units between 1 and 9999."));
Serial.println(F(" Enter 10000 to turn off acceleration dynamics entirely."));
Serial.println(F(" Strike 'Enter' to leave as is:"));
Serial.println(F("\n\n>>"));
PROMPT_accel2Entry();
if (exitflag == true){
delay(2000);
goto ch2menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
if (accel2 == 10000){
Serial.println(F("CHANNEL 2 ACCELERATION DYNAMICS TURNED OFF"));
delay(2000);
goto ch2menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
else {
Serial.print(F("CHANNEL 2 ACCELERATION SET TO "));
Serial.print(accel2);
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch2menu; //Yes that's a goto. FRACK modern programming sensibilities.
}
}
//**Set directionality to NORMAL or REVERSE
else if (rxchars[0] == '4'){
Serial.println(F("Set channel 2 directionality:"));
Serial.println(F(" n: NORMAL (default)"));
Serial.println(F(" r: REVERSE "));
Serial.println(F("\n (Or strike 'Enter' to leave as is)"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
if (rxchars[0] == 'n' || rxchars[0] == 'N'){
EEPROM.write(addr_dir2, 0); //0 = NORMAL
Serial.print(F("CHANNEL 2 DIRECTIONALITY SET TO 'NORMAL'"));
}
else if (rxchars[0] == 'r' || rxchars[0] == 'R'){
EEPROM.write(addr_dir2, 1); //1 = REVERSE
Serial.print(F("CHANNEL 2 DIRECTIONALITY SET TO 'REVERSE'"));
}
else {
Serial.println(F("DIRECTIONALITY NOT CHANGED"));
}
Serial.println(F("\n\n\n\n"));
delay(2000);
goto ch2menu;
}
//**Set waveform TORQUE SMOOTHING
else if (rxchars[0] == '5'){
Serial.println(F("Set channel 2 commutation waveform for torque smoothing:"));
Serial.println(F(" Torque smoothing may improve motion smoothness with some motors."));
Serial.println(F(" If so inclined, the aggressiveness and phase may be tweaked by trial and error.\n"));
Serial.println(F(" 0: OFF. Pure sine commutation"));
Serial.println(F(" 1: ON. A correction function (two-term fourier series) is added to the sine waveform."));
Serial.println(F(" 2: Set aggressiveness of correction function (when ON)"));
Serial.println(F(" 3: Set phase of correction function (when ON)"));
Serial.println(F("\n (Or strike 'Enter' to leave as is)"));
Serial.println(F("\n\n>>"));
SerialReceiveLoop();
if (rxchars[0] == '0'){
EEPROM.write(addr_torqueprofile2, 0); //0 = sine
Serial.print(F("CHANNEL 2 TORQUE SMOOTHING IS OFF (PURE SINE COMMUTATION)"));
}
else if (rxchars[0] == '1'){
EEPROM.write(addr_torqueprofile2, 1);
Serial.print(F("CHANNEL 2 TORQUE SMOOTHING IS ON"));
}
else if (rxchars[0] == '2'){
Serial.println(F("Enter aggressiveness parameter. This is an integer between 1 and 254 (arbitarary units)"));