-
Notifications
You must be signed in to change notification settings - Fork 178
/
commands.cpp
1594 lines (1294 loc) · 37.2 KB
/
commands.cpp
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
/**********************************************************************
Copyright (C) 2018 MisfitTech LLC, All rights reserved.
MisfitTech uses a dual license model that allows the software to be used under
a standard GPL open source license, or a commercial license. The standard GPL
license requires that all software statically linked with MisfitTec Code is
also distributed under the same GPL V2 license terms. Details of both license
options follow:
- Open source licensing -
MisfitTech is a free download and may be used, modified, evaluated and
distributed without charge provided the user adheres to version two of the GNU
General Public License (GPL) and does not remove the copyright notice or this
text. The GPL V2 text is available on the gnu.org web site
- Commercial licensing -
Businesses and individuals that for commercial or other reasons cannot comply
with the terms of the GPL V2 license must obtain a low cost commercial license
before incorporating MisfitTech code into proprietary software for distribution in
any form. Commercial licenses can be purchased from www.misfittech.net
and do not require any source files to be changed.
This code is distributed in the hope that it will be useful. You cannot
use MisfitTech's code unless you agree that you use the software 'as is'.
MisfitTech's code is provided WITHOUT ANY WARRANTY; without even the implied
warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. MisfitTech LLC disclaims all conditions and terms, be they
implied, expressed, or statutory.
Written by Trampas Stern for MisfitTech.
Misfit Tech invests time and resources providing this open source code,
please support MisfitTech and open-source hardware by purchasing
products from MisfitTech, www.misifittech.net!
*********************************************************************/
#include "commands.h"
#include "command.h"
#include "calibration.h"
#include "stepper_controller.h"
#include <stdlib.h>
#include "nonvolatile.h"
#include "Reset.h"
#include "nzs.h"
#include "ftoa.h"
#include "board.h"
#include "eeprom.h"
#include "steppin.h"
extern int32_t dataEnabled;
#define COMMANDS_PROMPT (":>")
sCmdUart UsbUart;
sCmdUart SerialUart;
sCmdUart HostUart; //uart on the step/dir pins
static int isPowerOfTwo (unsigned int x)
{
while (((x % 2) == 0) && x > 1) /* While x is even and > 1 */
x /= 2;
return (x == 1);
}
CMD_STR(help,"Displays this message");
CMD_STR(getcal,"Prints the calibration table");
CMD_STR(calibrate,"Calbirates the encoder, should be done with motor disconnected from machine");
CMD_STR(testcal,"tests the calibaration of the encoder");
CMD_STR(microsteps,"gets/sets the microstep size, example 'microsteps 16'");
CMD_STR(step, "Steps motor one step, optionally direction can be set is 'step 1' for reverse");
CMD_STR(feedback, "enable or disable feedback controller, 'feedback 0' - disables, 'feedback 1' - enables");
CMD_STR(readpos, "reads the current angle as 16bit number, applies calibration if valid");
CMD_STR(encoderdiag, "Prints encoder diagnostic")
CMD_STR(spid, "with no arguments prints SIMPLE PID parameters, with arguments sets PID 'sPID Kp Ki Kd' "
"Where Kp,Ki,Kd are floating point numbers");
CMD_STR(vpid, "with no arguments prints VELOCITY PID parameters, with arguments sets PID 'sPID Kp Ki Kd' "
"Where Kp,Ki,Kd are floating point numbers");
CMD_STR(ppid, "with no arguments prints POSITIONAL PID parameters, with arguments sets PID 'sPID Kp Ki Kd' "
"Where Kp,Ki,Kd are floating point numbers");
//CMD_STR(testringing ,"Steps motor at various currents and measures encoder");
//CMD_STR(microsteperror ,"test error on microstepping")
CMD_STR(dirpin, "with no arguments read dirpin setting, with argument sets direction pin rotation");
#ifndef PIN_ENABLE
CMD_STR(errorpinmode,"gets/sets the functionality of the error/enable pin");
#else
CMD_STR(enablepinmode,"gets/sets the functionality of the enable pin");
#endif
CMD_STR(errorlimit, "gets/set the error limit which will assert error pin (when error pin is set for error output)");
CMD_STR(ctrlmode, "gets/set the feedback controller mode of operation");
CMD_STR(maxcurrent, "gets/set the maximum motor current allowed in milliAmps");
CMD_STR(holdcurrent, "gets/set the motor holding current in milliAmps, only used in the simple positional PID mode");
CMD_STR(homecurrent, "gets/set the motor moving and holding currents that will be used when pin A3 is low");
CMD_STR(motorwiring, "gets/set the motor wiring direction, should only be used by experts");
CMD_STR(stepsperrotation, "gets/set the motor steps per rotation, should only be used by experts");
//CMD_STR(sysparams, "with no arguments read parameters, will set with arguments");
//CMD_STR(motorparams, "with no arguments read parameters, will set with arguments");
CMD_STR(boot, "Enters the bootloader");
CMD_STR(move, "moves encoder to absolute angle in degrees 'move 400.1'");
//CMD_STR(printdata, "prints last n error terms");
CMD_STR(velocity, "gets/set velocity in RPMs");
CMD_STR(factoryreset, "resets board to factory defaults");
CMD_STR(stop, "stops the motion planner");
CMD_STR(setzero, "set the reference angle to zero");
CMD_STR(data, "enables/disables binary data output");
CMD_STR(looptime, "returns the control loop processing time");
CMD_STR(eepromerror, "returns error in degreees from eeprom at power up realtive to current encoder");
CMD_STR(eepromloc, "returns location in degreees eeprom on power up");
CMD_STR(eepromwrite, "forces write of location to eeprom");
CMD_STR(eepromsetloc, "sets the device angle based on EEPROM last reading, compenstates for error")
CMD_STR(setpos, "sets the current angle in degrees");
CMD_STR(reboot, "reboots the unit");
CMD_STR(homepin, "sets the pin used to drop to homing current");
CMD_STR(homeangledelay, "sets the angle delay in dropping to homing current");
#ifdef PIN_ENABLE
CMD_STR(home, "moves the motor until home switch (enable pin) is pulled low. example 'home 360 0.5' move up to 360 degrees at 0.5 RPM ")
#endif
CMD_STR(pinread, "reads pins as binary (bit 0-step, bit 1 - Dir, bit 2 - Enable, bit 3 - Error, bit 4 - A3, bit 5- TX, bit 6 - RX")
CMD_STR(errorpin, "Sets the logic level of error pin")
CMD_STR(geterror, "gets current error")
CMD_STR(getsteps, "returns number of steps seen")
CMD_STR(debug, "enables debug commands out USB")
//List of supported commands
sCommand Cmds[] =
{
COMMAND(help),
COMMAND(calibrate),
COMMAND(getcal),
COMMAND(testcal),
COMMAND(microsteps),
COMMAND(step),
COMMAND(feedback),
COMMAND(readpos),
COMMAND(encoderdiag),
COMMAND(spid),
COMMAND(vpid),
COMMAND(ppid),
//COMMAND(testringing),
//COMMAND(microsteperror),
COMMAND(dirpin),
#ifndef PIN_ENABLE
COMMAND(errorpinmode),
#else
COMMAND(enablepinmode),
#endif
COMMAND(errorlimit),
COMMAND(ctrlmode),
COMMAND(maxcurrent),
COMMAND(holdcurrent),
COMMAND(homecurrent),
COMMAND(motorwiring),
COMMAND(stepsperrotation),
//COMMAND(sysparams),
//COMMAND(motorparams),
COMMAND(boot),
COMMAND(move),
//COMMAND(printdata),
COMMAND(velocity),
COMMAND(factoryreset),
COMMAND(stop),
COMMAND(setzero),
COMMAND(data),
COMMAND(looptime),
COMMAND(eepromerror),
COMMAND(eepromloc),
COMMAND(eepromwrite),
COMMAND(setpos),
COMMAND(reboot),
COMMAND(eepromsetloc),
COMMAND(homepin),
COMMAND(homeangledelay),
#ifdef PIN_ENABLE
COMMAND(home),
#endif
COMMAND(pinread),
COMMAND(errorpin),
COMMAND(geterror),
COMMAND(getsteps),
COMMAND(debug),
{"",0,""}, //End of list signal
};
static int debug_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
uint32_t i;
if (argc>=1)
{
i=atol(argv[0]);
SysLogDebug(i);
}
}
static int getsteps_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
int32_t s;
s=(int32_t)getSteps();
// s=(int32_t)stepperCtrl.getSteps();
CommandPrintf(ptrUart,"steps %" PRIi32 "\n\r",s);
return 0;
}
static int geterror_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
float f;
char str[30];
f=ANGLE_T0_DEGREES(stepperCtrl.getLoopError());
ftoa(f,str,2,'f');
CommandPrintf(ptrUart,"error %s deg",str);
return 0;
}
static int errorpin_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
if (argc==1)
{
SystemParams_t params;
memcpy(¶ms,&NVM->SystemParams, sizeof(SystemParams_t) );
params.errorLogic=atol(argv[0]);
nvmWriteSystemParms(params);
stepperCtrl.updateParamsFromNVM();
}
CommandPrintf(ptrUart,"error pin assert level is %d\n\r",NVM->SystemParams.errorLogic);
return 0;
}
static int pinread_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
uint8_t ret=0;
if (digitalRead(PIN_STEP_INPUT))
{
ret |= 0x01;
}
if (digitalRead(PIN_DIR_INPUT))
{
ret |= 0x02;
}
#ifdef PIN_ENABLE
if (digitalRead(PIN_ENABLE))
{
ret |= 0x04;
}
#endif
if (digitalRead(PIN_ERROR))
{
ret |= 0x08;
}
if (digitalRead(PIN_A3))
{
ret |= 0x10;
}
if (digitalRead(30))
{
ret |= 0x20;
}
if (digitalRead(31))
{
ret |= 0x40;
}
CommandPrintf(ptrUart,"0x%02X\n\r",ret);
return 0;
}
#ifdef PIN_ENABLE
static void errorPinISR(void)
{
SmartPlanner.stop(); //stop the planner
}
static int home_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
float rpm=1;
float startDegrees=ANGLE_T0_DEGREES(stepperCtrl.getCurrentAngle());
float finalDegrees=startDegrees+360.0;
char str[20];
float deg;
if (argc>=1)
{
finalDegrees=startDegrees+atof(argv[0]);
}
if (argc>=2)
{
rpm=atof(argv[1]);
}
//setup a interrupt for the enable pin
attachInterrupt(digitalPinToInterrupt(PIN_ENABLE), errorPinISR, FALLING);
SmartPlanner.moveConstantVelocity(finalDegrees,rpm);
while(!SmartPlanner.done())
{
//do nothing
}
detachInterrupt(digitalPinToInterrupt(PIN_ENABLE));
deg=ANGLE_T0_DEGREES(stepperCtrl.getCurrentAngle());
ftoa(deg,str,2,'f');
CommandPrintf(ptrUart,"home is %s deg\n\r",str);
stepperCtrl.setZero();
return 0;
}
#endif
static int reboot_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
NVIC_SystemReset();
return 0;
}
static int setpos_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
if (argc>=1)
{
int64_t a;
float x;
x=fabs(atof(argv[0]));
a=ANGLE_FROM_DEGREES(x);
stepperCtrl.setAngle(a);
return 0;
}
return 1;
}
static int eepromwrite_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
eepromFlush();
return 0;
}
static int eepromerror_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
Angle a;
uint16_t error;
float deg;
char str[20];
a=(Angle)PowerupEEPROM.encoderAngle;
LOG("EEPROM encoder %d",(uint16_t)a);
LOG("start encoder %d",(uint16_t)stepperCtrl.getStartupEncoder());
LOG("current encoder %d",(uint16_t)stepperCtrl.getEncoderAngle());
a=(a-(Angle)stepperCtrl.getStartupEncoder());
deg=ANGLE_T0_DEGREES((uint16_t)a) ;
if (deg>360.0)
{
deg=deg-360.0;
}
ftoa(deg,str,2,'f');
CommandPrintf(ptrUart,"startup error(+/-) %s deg\n\r",str);
a=(Angle)PowerupEEPROM.encoderAngle;
a=(a-(Angle)stepperCtrl.getEncoderAngle());
deg=ANGLE_T0_DEGREES((uint16_t)a);
if (deg>360.0)
{
deg=deg-360.0;
}
ftoa(deg,str,2,'f');
CommandPrintf(ptrUart,"current error(+/-) %s deg\n\r",str);
return 0;
}
static int eepromsetloc_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
Angle a;
int64_t deg;
int32_t x;
x=(uint32_t)PowerupEEPROM.encoderAngle-(uint32_t)stepperCtrl.getEncoderAngle();
deg=PowerupEEPROM.angle-x;
stepperCtrl.setAngle(deg);
return 0;
}
static int eepromloc_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
Angle a;
int64_t deg;
int32_t x,y;
deg=PowerupEEPROM.angle;
deg=(deg*360*100)/(int32_t)ANGLE_STEPS;
x=(deg)/100;
y=abs(deg-(x*100));
CommandPrintf(ptrUart,"%d.%0.2d deg\n\r",x,y);
return 0;
}
static int looptime_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
CommandPrintf(ptrUart,"%dus",stepperCtrl.getLoopTime());
return 0;
}
static int setzero_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
stepperCtrl.setZero();
return 0;
}
static int stop_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
SmartPlanner.stop();
return 0;
}
static int data_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
if (argc == 1)
{
uint32_t x;
x=atol(argv[0]);
dataEnabled=x;
return 0;
}
return 1;
}
static int stepsperrotation_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
if (argc == 0)
{
uint32_t x;
x=NVM->motorParams.fullStepsPerRotation;
CommandPrintf(ptrUart,"full steps per rotation %u\n\r",x);
return 0;
}
if (argc == 1)
{
uint32_t x;
x=atol(argv[0]);
if (x==200 || x==400)
{
MotorParams_t motorParams;
memcpy(&motorParams,&NVM->motorParams, sizeof(motorParams) );
motorParams.fullStepsPerRotation=x;
nvmWriteMotorParms(motorParams);
stepperCtrl.updateParamsFromNVM();
x=NVM->motorParams.fullStepsPerRotation;
CommandPrintf(ptrUart,"full steps per rotation %u\n\r",x);
CommandPrintf(ptrUart,"please power cycle board\n\r");
return 0;
}
}
CommandPrintf(ptrUart,"usage 'stepsperrotation 200' or 'stepsperrotation 400'\n\r");
return 1;
}
static int motorwiring_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
if (argc == 0)
{
uint32_t x;
x=NVM->motorParams.motorWiring;
CommandPrintf(ptrUart,"motor wiring %u\n\r",x);
return 0;
}
if (argc == 1)
{
uint32_t x;
x=atol(argv[0]);
if (x<=1)
{
MotorParams_t motorParams;
memcpy(&motorParams,&NVM->motorParams, sizeof(motorParams) );
motorParams.motorWiring=x;
nvmWriteMotorParms(motorParams);
stepperCtrl.updateParamsFromNVM();
x=NVM->motorParams.motorWiring;
CommandPrintf(ptrUart,"motor wiring %u\n\r",x);
CommandPrintf(ptrUart,"please power cycle board\n\r");
return 0;
}
}
CommandPrintf(ptrUart,"usage 'motorwiring 0' or 'motorwiring 1'\n\r");
return 1;
}
static int homeangledelay_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
float f;
char str[30];
if (argc == 1)
{
f=atof(argv[0]);
SystemParams_t params;
memcpy(¶ms,&NVM->SystemParams, sizeof(SystemParams_t) );
params.homeAngleDelay=ANGLE_FROM_DEGREES(f);
nvmWriteSystemParms(params);
stepperCtrl.updateParamsFromNVM();
}
f=ANGLE_T0_DEGREES(NVM->SystemParams.homeAngleDelay);
ftoa(f,str,2,'f');
CommandPrintf(ptrUart,"home angle delay %s\n\r",str);
return 0;
}
static int homepin_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
int32_t x;
if (argc == 0)
{
x=NVM->SystemParams.homePin;
CommandPrintf(ptrUart,"home pin %d\n\r",x);
return 0;
}
if (argc == 1)
{
x=atol(argv[0]);
SystemParams_t params;
memcpy(¶ms,&NVM->SystemParams, sizeof(SystemParams_t) );
params.homePin=x;
nvmWriteSystemParms(params);
stepperCtrl.updateParamsFromNVM();
x=NVM->SystemParams.homePin;
CommandPrintf(ptrUart,"home pin %d\n\r",x);
return 0;
}
CommandPrintf(ptrUart, "use 'sethomepin 17' to set maximum home pin to A3");
return 1;
}
static int homecurrent_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
uint32_t x,y;
if (argc == 0)
{
x=NVM->motorParams.homeMa;
y=NVM->motorParams.homeHoldMa;
CommandPrintf(ptrUart,"current %umA, %umA\n\r",x,y);
return 0;
}
if (argc == 1)
{
x=atol(argv[0]);
MotorParams_t motorParams;
memcpy(&motorParams,&NVM->motorParams, sizeof(motorParams) );
motorParams.homeMa=x;
nvmWriteMotorParms(motorParams);
stepperCtrl.updateParamsFromNVM();
x=NVM->motorParams.homeMa;
y=NVM->motorParams.homeHoldMa;
CommandPrintf(ptrUart,"current %umA, %umA\n\r",x,y);
return 0;
}
if (argc == 2)
{
x=atol(argv[0]);
y=atol(argv[1]);
MotorParams_t motorParams;
memcpy(&motorParams,&NVM->motorParams, sizeof(motorParams) );
motorParams.homeMa=x;
motorParams.homeHoldMa=y;
nvmWriteMotorParms(motorParams);
stepperCtrl.updateParamsFromNVM();
x=NVM->motorParams.homeMa;
y=NVM->motorParams.homeHoldMa;
CommandPrintf(ptrUart,"current %umA, %umA\n\r",x,y);
return 0;
}
CommandPrintf(ptrUart, "use 'homecurrent 1000 500' to set maximum home current to 1.0A and hold to 500ma");
return 1;
}
static int holdcurrent_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
if (argc == 0)
{
uint32_t x;
x=NVM->motorParams.currentHoldMa;
CommandPrintf(ptrUart,"hold current %u mA\n\r",x);
return 0;
}
if (argc == 1)
{
uint32_t x;
x=atol(argv[0]);
MotorParams_t motorParams;
memcpy(&motorParams,&NVM->motorParams, sizeof(motorParams) );
motorParams.currentHoldMa=x;
nvmWriteMotorParms(motorParams);
stepperCtrl.updateParamsFromNVM();
x=NVM->motorParams.currentHoldMa;
CommandPrintf(ptrUart,"hold current %u mA\n\r",x);
return 0;
}
CommandPrintf(ptrUart, "use 'holdcurrent 1000' to set maximum current to 1.0A");
return 1;
}
static int maxcurrent_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
if (argc == 0)
{
uint32_t x;
x=NVM->motorParams.currentMa;
CommandPrintf(ptrUart,"max current %u mA\n\r",x);
return 0;
}
if (argc == 1)
{
uint32_t x;
x=atol(argv[0]);
MotorParams_t motorParams;
memcpy(&motorParams,&NVM->motorParams, sizeof(motorParams) );
motorParams.currentMa=x;
nvmWriteMotorParms(motorParams);
stepperCtrl.updateParamsFromNVM();
x=NVM->motorParams.currentMa;
CommandPrintf(ptrUart,"max current %u mA\n\r",x);
return 0;
}
CommandPrintf(ptrUart, "use 'maxcurrent 2000' to set maximum current to 2.0A");
return 1;
}
static int ctrlmode_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
bool ret;
if (argc == 0)
{
switch(NVM->SystemParams.controllerMode)
{
case CTRL_OFF:
CommandPrintf(ptrUart,"controller Off(0)");
return 0;
case CTRL_OPEN:
CommandPrintf(ptrUart,"controller Open-loop(1)");
return 0;
case CTRL_SIMPLE:
CommandPrintf(ptrUart,"controller Simple-Position-PID(2)");
return 0;
case CTRL_POS_PID:
CommandPrintf(ptrUart,"controller Current-Position-PID(3)");
return 0;
case CTRL_POS_VELOCITY_PID:
CommandPrintf(ptrUart,"controller Velocity-PID(4)");
return 0;
}
return 1;
}
if (argc == 1)
{
uint32_t x;
x=atol(argv[0]);
if (x<=4)
{
SystemParams_t systemParams;
memcpy(&systemParams,&NVM->SystemParams, sizeof(systemParams) );
systemParams.controllerMode=(feedbackCtrl_t)(x);
nvmWriteSystemParms(systemParams);
stepperCtrl.updateParamsFromNVM();
switch(NVM->SystemParams.controllerMode)
{
case CTRL_OFF:
CommandPrintf(ptrUart,"controller Off(0)");
return 0;
case CTRL_OPEN:
CommandPrintf(ptrUart,"controller Open-loop(1)");
return 0;
case CTRL_SIMPLE:
CommandPrintf(ptrUart,"controller Simple-Position-PID(2)");
return 0;
case CTRL_POS_PID:
CommandPrintf(ptrUart,"controller Current-Position-PID(3)");
return 0;
case CTRL_POS_VELOCITY_PID:
CommandPrintf(ptrUart,"controller Velocity-PID(4)");
return 0;
}
return 1;
}
}
CommandPrintf(ptrUart, "use 'ctrlmode [0 .. 4]' to set control mode");
return 1;
}
static int errorlimit_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
bool ret;
char str[20];
if (argc == 0)
{
float x;
x=ANGLE_T0_DEGREES(NVM->SystemParams.errorLimit);
ftoa(x,str,2,'f');
CommandPrintf(ptrUart,"errorLimit %s deg\n\r",str);
return 0;
}
if (argc == 1)
{
float x;
x=fabs(atof(argv[0]));
SystemParams_t systemParams;
memcpy(&systemParams,&NVM->SystemParams, sizeof(systemParams) );
systemParams.errorLimit=ANGLE_FROM_DEGREES(x);
nvmWriteSystemParms(systemParams);
stepperCtrl.updateParamsFromNVM();
x=ANGLE_T0_DEGREES(NVM->SystemParams.errorLimit);
ftoa(x,str,2,'f');
CommandPrintf(ptrUart,"errorLimit %s deg\n\r",str);
return 0;
}
CommandPrintf(ptrUart, "use 'errorlimit 1.8' to set error limit to 1.8 degrees");
return 1;
}
static int dirpin_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
bool ret;
if (argc == 0)
{
if (CW_ROTATION == NVM->SystemParams.dirPinRotation)
{
CommandPrintf(ptrUart,"dirpin CW(%d)\n\r",(uint32_t)NVM->SystemParams.dirPinRotation);
}else
{
CommandPrintf(ptrUart,"dirpin CCW(%d)\n\r",(uint32_t)NVM->SystemParams.dirPinRotation);
}
return 0;
}
if (argc == 1)
{
uint32_t x;
x=abs(atol(argv[0]));
if (x<=1)
{
SystemParams_t systemParams;
memcpy(&systemParams,&NVM->SystemParams, sizeof(systemParams) );
systemParams.dirPinRotation=(RotationDir_t)x;
nvmWriteSystemParms(systemParams);
stepperCtrl.updateParamsFromNVM();
if (CW_ROTATION == NVM->SystemParams.dirPinRotation)
{
CommandPrintf(ptrUart,"dirpin CW(%d)\n\r",(uint32_t)NVM->SystemParams.dirPinRotation);
}else
{
CommandPrintf(ptrUart,"dirpin CCW(%d)\n\r",(uint32_t)NVM->SystemParams.dirPinRotation);
}
return 0;
}
}
CommandPrintf(ptrUart, "used 'dirpin 0' for CW rotation and 'dirpin 1' for CCW");
return 1;
}
#ifndef PIN_ENABLE
static int errorpinmode_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
bool ret;
if (argc == 0)
{
if (ERROR_PIN_MODE_ENABLE == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Error pin - Enable Active High(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}else if (ERROR_PIN_MODE_ACTIVE_LOW_ENABLE == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Error pin - Enable active low(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}else if (ERROR_PIN_MODE_ERROR == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Error pin - Error pin(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
} else if (ERROR_PIN_MODE_BIDIR == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Error pin - Bidi error(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}
return 0;
}
if (argc == 1)
{
uint32_t x;
x=abs(atol(argv[0]));
if (x<=3)
{
SystemParams_t systemParams;
memcpy(&systemParams,&NVM->SystemParams, sizeof(systemParams) );
systemParams.errorPinMode=(ErrorPinMode_t)x;
nvmWriteSystemParms(systemParams);
stepperCtrl.updateParamsFromNVM();
if (ERROR_PIN_MODE_ENABLE == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Error pin - Enable Active High(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}else if (ERROR_PIN_MODE_ACTIVE_LOW_ENABLE == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Error pin - Enable active low(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}else if (ERROR_PIN_MODE_ERROR == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Error pin - Error pin(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
} else if (ERROR_PIN_MODE_BIDIR == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Error pin - Bidi error(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}
return 0;
}
}
CommandPrintf(ptrUart, "use 'errorpinmode 0' for enable active high, 'errorpinmode 1' for enable active low and 'errorpinmode 2' for error output" );
return 1;
}
#else
static int enablepinmode_cmd(sCmdUart *ptrUart,int argc, char * argv[])
{
bool ret;
if (argc == 0)
{
if (ERROR_PIN_MODE_ENABLE == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Enable pin - Enable Active High(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}else if (ERROR_PIN_MODE_ACTIVE_LOW_ENABLE == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Enable pin - Enable active low(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
} else if (ERROR_PIN_MODE_BIDIR == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Enable pin - Bidi error(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
} else
{
CommandPrintf(ptrUart,"UNDEFINED Pin Mode error(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}
return 0;
}
if (argc == 1)
{
uint32_t x;
x=abs(atol(argv[0]));
if (x<=1)
{
SystemParams_t systemParams;
memcpy(&systemParams,&NVM->SystemParams, sizeof(systemParams) );
systemParams.errorPinMode=(ErrorPinMode_t)x;
nvmWriteSystemParms(systemParams);
stepperCtrl.updateParamsFromNVM();
if (ERROR_PIN_MODE_ENABLE == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Enable pin - Enable Active High(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}else if (ERROR_PIN_MODE_ACTIVE_LOW_ENABLE == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Enable pin - Enable active low(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}else if (ERROR_PIN_MODE_BIDIR == NVM->SystemParams.errorPinMode)
{
CommandPrintf(ptrUart,"Enable pin - Bidi error(%d)\n\r",(uint32_t)NVM->SystemParams.errorPinMode);
}
return 0;
}
}
CommandPrintf(ptrUart, "use 'enablepinmode 0' for enable active high, 'enablepinmode 1' for enable active low " );
return 1;