4
4
Commander::Commander (HardwareSerial& serial){
5
5
com_port = &serial;
6
6
}
7
+ Commander::Commander (){
8
+ // do nothing
9
+ }
10
+
7
11
8
12
void Commander::add (char id, CommandCallback onCommand){
9
13
call_list[call_count] = onCommand;
10
14
call_ids[call_count] = id;
11
15
call_count++;
12
16
}
13
17
18
+
14
19
void Commander::run (){
20
+ if (!com_port) return ;
15
21
// a string to hold incoming data
16
22
while (com_port->available ()) {
17
23
// get the new byte:
@@ -28,39 +34,45 @@ void Commander::run(){
28
34
}
29
35
}
30
36
37
+ void Commander::run (HardwareSerial &serial){
38
+ // a string to hold incoming data
39
+ while (serial.available ()) {
40
+ // get the new byte:
41
+ received_chars[rec_cnt] = (char )serial.read ();
42
+ // end of user input
43
+ if (received_chars[rec_cnt++] == ' \n ' ) {
44
+ // execute the user command
45
+ run (received_chars);
46
+
47
+ // reset the command buffer
48
+ received_chars[0 ] = 0 ;
49
+ rec_cnt=0 ;
50
+ }
51
+ }
52
+ }
53
+
31
54
void Commander::run (char * user_input){
32
55
// execute the user command
33
- char id = received_chars [0 ];
56
+ char id = user_input [0 ];
34
57
if (id == CMD_SCAN)
35
58
for (int i=0 ; i < call_count; i++){
36
- com_port-> print (call_ids[i]);
37
- com_port-> print (" :" );
59
+ print (call_ids[i], 0 );
60
+ print (" :" , 0 );
38
61
call_list[i](cmd_scan_msg);
39
62
}
40
63
else
41
64
for (int i=0 ; i < call_count; i++){
42
65
if (id == call_ids[i]){
43
- call_list[i](&received_chars [1 ]);
66
+ call_list[i](&user_input [1 ]);
44
67
break ;
45
68
}
46
69
}
47
70
}
48
71
49
- void Commander::verbosePrint (const char * message){
50
- if (verbose) com_port->print (message);
51
- }
52
- void Commander::verbosePrint (const __FlashStringHelper *message){
53
- if (verbose) com_port->print (message);
54
- }
55
- void Commander::printNumber (const float number, const bool newline){
56
- if (newline) com_port->println (number,decimal_places);
57
- else com_port->print (number,decimal_places);
58
- }
59
-
60
72
void Commander::motor (FOCMotor* motor, char * user_command) {
61
73
// if empty string
62
74
if ( user_command[0 ] == CMD_SCAN ){
63
- com_port-> println (F (" mot" ));
75
+ print (F (" mot" ), 1 );
64
76
return ;
65
77
}
66
78
@@ -107,7 +119,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
107
119
// change velocity pid limit if in voltage mode and no phase resistance set
108
120
if ( !_isset (motor->phase_resistance ) && motor->torque_controller ==TorqueControlType::voltage) motor->PID_velocity .limit = value;
109
121
}
110
- printNumber (motor->voltage_limit ,1 );
122
+ print (motor->voltage_limit ,1 );
111
123
break ;
112
124
case SCMD_LIM_CURR: // current limit
113
125
verbosePrint (F (" curr: " ));
@@ -118,18 +130,18 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
118
130
// if phase resistance specified or the current control is on set the current limit to the velocity PID
119
131
if (_isset (motor->phase_resistance ) || motor->torque_controller != TorqueControlType::voltage ) motor->PID_velocity .limit = value;
120
132
}
121
- printNumber (motor->current_limit ,1 );
133
+ print (motor->current_limit ,1 );
122
134
break ;
123
135
case SCMD_LIM_VEL: // velocity limit
124
136
verbosePrint (F (" vel: " ));
125
137
if (!GET){
126
138
motor->velocity_limit = value;
127
139
motor->P_angle .limit = value;
128
140
}
129
- printNumber (motor->velocity_limit ,1 );
141
+ print (motor->velocity_limit ,1 );
130
142
break ;
131
143
default :
132
- com_port-> println ( F ( " err " ) );
144
+ printError ( );
133
145
break ;
134
146
}
135
147
break ;
@@ -139,27 +151,27 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
139
151
case SCMD_DOWNSAMPLE:
140
152
verbosePrint (F (" downsample: " ));
141
153
if (!GET) motor->motion_downsample = value;
142
- printNumber ( motor->motion_downsample ,1 );
154
+ print (( int ) motor->motion_downsample , 1 );
143
155
break ;
144
156
default :
145
157
// change control type
146
158
if (!GET && value >= 0 && (int )value < 5 )// if set command
147
159
motor->controller = (MotionControlType)value;
148
160
switch (motor->controller ){
149
161
case MotionControlType::torque:
150
- com_port-> println (F (" torque" ));
162
+ print (F (" torque" ), 1 );
151
163
break ;
152
164
case MotionControlType::velocity:
153
- com_port-> println (F (" vel" ));
165
+ print (F (" vel" ), 1 );
154
166
break ;
155
167
case MotionControlType::angle:
156
- com_port-> println (F (" angle" ));
168
+ print (F (" angle" ), 1 );
157
169
break ;
158
170
case MotionControlType::velocity_openloop:
159
- com_port-> println (F (" vel open" ));
171
+ print (F (" vel open" ), 1 );
160
172
break ;
161
173
case MotionControlType::angle_openloop:
162
- com_port-> println (F (" angle open" ));
174
+ print (F (" angle open" ), 1 );
163
175
break ;
164
176
}
165
177
break ;
@@ -172,21 +184,21 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
172
184
motor->torque_controller = (TorqueControlType)value;
173
185
switch (motor->torque_controller ){
174
186
case TorqueControlType::voltage:
175
- com_port-> println (F (" volt" ));
187
+ print (F (" volt" ), 1 );
176
188
break ;
177
189
case TorqueControlType::current:
178
- com_port-> println (F (" curr" ));
190
+ print (F (" curr" ), 1 );
179
191
break ;
180
192
case TorqueControlType::foc_current:
181
- com_port-> println (F (" foc" ));
193
+ print (F (" foc" ), 1 );
182
194
break ;
183
195
}
184
196
break ;
185
197
case CMD_STATUS:
186
198
// enable/disable
187
199
verbosePrint (F (" Status: " ));
188
200
if (!GET) (bool )value ? motor->enable () : motor->disable ();
189
- com_port-> println (motor->enabled );
201
+ print (motor->enabled , 1 );
190
202
break ;
191
203
case CMD_RESIST:
192
204
// enable/disable
@@ -198,8 +210,8 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
198
210
motor->PID_velocity .limit = motor->current_limit ;
199
211
}
200
212
}
201
- if (_isset (motor->phase_resistance )) printNumber (motor->phase_resistance ,1 );
202
- else com_port-> println ( 0 );
213
+ if (_isset (motor->phase_resistance )) print (motor->phase_resistance ,1 );
214
+ else print ( 0 , 0 );
203
215
break ;
204
216
case CMD_SENSOR:
205
217
// Sensor zero offset
@@ -208,15 +220,15 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
208
220
case SCMD_SENS_MECH_OFFSET: // zero offset
209
221
verbosePrint (F (" offset: " ));
210
222
if (!GET) motor->sensor_offset = value;
211
- printNumber (motor->sensor_offset ,1 );
223
+ print (motor->sensor_offset ,1 );
212
224
break ;
213
225
case SCMD_SENS_ELEC_OFFSET: // electrical zero offset - not suggested to touch
214
226
verbosePrint (F (" el. offset: " ));
215
227
if (!GET) motor->zero_electric_angle = value;
216
- printNumber (motor->zero_electric_angle ,1 );
228
+ print (motor->zero_electric_angle ,1 );
217
229
break ;
218
230
default :
219
- com_port-> println ( F ( " err " ) );
231
+ printError ( );
220
232
break ;
221
233
}
222
234
break ;
@@ -227,68 +239,68 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
227
239
switch ((uint8_t )value){
228
240
case 0 : // get target
229
241
verbosePrint (F (" target: " ));
230
- printNumber (motor->target ,1 );
242
+ print (motor->target ,1 );
231
243
break ;
232
244
case 1 : // get voltage q
233
245
verbosePrint (F (" Vq: " ));
234
- printNumber (motor->voltage .q ,1 );
246
+ print (motor->voltage .q ,1 );
235
247
break ;
236
248
case 2 : // get voltage d
237
249
verbosePrint (F (" Vd: " ));
238
- printNumber (motor->voltage .q ,1 );
250
+ print (motor->voltage .q ,1 );
239
251
break ;
240
252
case 3 : // get current q
241
253
verbosePrint (F (" Cq: " ));
242
- printNumber (motor->voltage .q ,1 );
254
+ print (motor->voltage .q ,1 );
243
255
break ;
244
256
case 4 : // get current d
245
257
verbosePrint (F (" Cd: " ));
246
- printNumber (motor->voltage .q ,1 );
258
+ print (motor->voltage .q ,1 );
247
259
break ;
248
260
case 5 : // get velocity
249
261
verbosePrint (F (" vel: " ));
250
- printNumber (motor->shaft_velocity ,1 );
262
+ print (motor->shaft_velocity ,1 );
251
263
break ;
252
264
case 6 : // get angle
253
265
verbosePrint (F (" Angle: " ));
254
- printNumber (motor->shaft_angle ,1 );
266
+ print (motor->shaft_angle ,1 );
255
267
break ;
256
268
default :
257
- com_port-> println ( F ( " err " ) );
269
+ printError ( );
258
270
break ;
259
271
}
260
272
break ;
261
273
case SCMD_DOWNSAMPLE:
262
274
verbosePrint (F (" downsample: " ));
263
275
if (!GET) motor->monitor_downsample = value;
264
- printNumber ( motor->monitor_downsample ,1 );
276
+ print (( int ) motor->monitor_downsample ,1 );
265
277
break ;
266
278
case SCMD_CLEAR:
267
279
for (int i=0 ; i<7 ; i++) motor->monitor_variables [i] = 0 ;
268
- com_port-> println (F (" clear" ));
280
+ print (F (" clear" ), 1 );
269
281
break ;
270
282
case SCMD_SET:
271
283
for (int i=0 ; i<7 ; i++){
272
284
motor->monitor_variables [i] = user_command[value_index+i] - ' 0' ;
273
- com_port-> print (motor->monitor_variables [i]);
285
+ print (motor->monitor_variables [i], 0 );
274
286
}
275
- com_port-> println ( );
287
+ print ( " " , 1 );
276
288
break ;
277
289
default :
278
- com_port-> println ( F ( " err " ) );
290
+ printError ( );
279
291
break ;
280
292
}
281
293
break ;
282
294
default : // target change
283
295
verbosePrint (F (" Target: " ));
284
296
motor->target = atof (user_command);
285
- printNumber (motor->target ,1 );
297
+ print (motor->target ,1 );
286
298
}
287
299
}
288
300
289
301
void Commander::pid (PIDController* pid, char * user_cmd){
290
302
if ( user_cmd[0 ] == CMD_SCAN ){
291
- com_port-> println (F (" pid" ));
303
+ print (F (" pid" ), 1 );
292
304
return ;
293
305
}
294
306
char cmd = user_cmd[0 ];
@@ -299,37 +311,37 @@ void Commander::pid(PIDController* pid, char* user_cmd){
299
311
case SCMD_PID_P: // P gain change
300
312
verbosePrint (" P: " );
301
313
if (!GET) pid->P = value;
302
- printNumber (pid->P ,1 );
314
+ print (pid->P ,1 );
303
315
break ;
304
316
case SCMD_PID_I: // I gain change
305
317
verbosePrint (" I: " );
306
318
if (!GET) pid->I = value;
307
- printNumber (pid->I ,1 );
319
+ print (pid->I ,1 );
308
320
break ;
309
321
case SCMD_PID_D: // D gain change
310
322
verbosePrint (" D: " );
311
323
if (!GET) pid->D = value;
312
- printNumber (pid->D ,1 );
324
+ print (pid->D ,1 );
313
325
break ;
314
326
case SCMD_PID_RAMP: // ramp change
315
327
verbosePrint (" ramp: " );
316
328
if (!GET) pid->output_ramp = value;
317
- printNumber (pid->output_ramp ,1 );
329
+ print (pid->output_ramp ,1 );
318
330
break ;
319
331
case SCMD_PID_LIM: // limit change
320
332
verbosePrint (" limit: " );
321
333
if (!GET) pid->limit = value;
322
- printNumber (pid->limit ,1 );
334
+ print (pid->limit ,1 );
323
335
break ;
324
336
default :
325
- com_port-> println ( F ( " err " ) );
337
+ printError ( );
326
338
break ;
327
339
}
328
340
}
329
341
330
342
void Commander::lpf (LowPassFilter* lpf, char * user_cmd){
331
343
if ( user_cmd[0 ] == CMD_SCAN ){
332
- com_port-> println (F (" lpf" ));
344
+ print (F (" lpf" ), 1 );
333
345
return ;
334
346
}
335
347
char cmd = user_cmd[0 ];
@@ -338,22 +350,60 @@ void Commander::lpf(LowPassFilter* lpf, char* user_cmd){
338
350
339
351
switch (cmd){
340
352
case SCMD_LPF_TF: // Tf value change
341
- verbosePrint (" Tf: " );
353
+ verbosePrint (F ( " Tf: " ) );
342
354
if (!GET) lpf->Tf = value;
343
- printNumber (lpf->Tf ,1 );
355
+ print (lpf->Tf ,1 );
344
356
break ;
345
357
default :
346
- com_port-> println ( F ( " err " ) );
358
+ printError ( );
347
359
break ;
348
360
}
349
361
}
350
362
351
363
void Commander::variable (float * value, char * user_cmd){
352
364
if ( user_cmd[0 ] == CMD_SCAN ){
353
- com_port-> println (F (" var" ));
365
+ print (F (" var" ), 1 );
354
366
return ;
355
367
}
356
368
bool GET = user_cmd[0 ] == ' \n ' ;
357
369
if (!GET) *value = atof (user_cmd);
358
- printNumber (*value,1 );
370
+ print (*value,1 );
371
+ }
372
+
373
+
374
+ void Commander::print (const int number, const bool newline){
375
+ if (!com_port) return ;
376
+ if (newline) com_port->println (number);
377
+ else com_port->print (number);
378
+ }
379
+ void Commander::print (const float number, const bool newline){
380
+ if (!com_port) return ;
381
+ if (newline) com_port->println (number,decimal_places);
382
+ else com_port->print (number,decimal_places);
383
+ }
384
+ void Commander::print (const char * message, const bool newline){
385
+ if (!com_port) return ;
386
+ if (newline) com_port->println (message);
387
+ else com_port->print (message);
388
+ }
389
+ void Commander::print (const __FlashStringHelper *message, const bool newline){
390
+ if (!com_port) return ;
391
+ if (newline) com_port->println (message);
392
+ else com_port->print (message);
393
+ }
394
+ void Commander::print (const char message, const bool newline){
395
+ if (!com_port) return ;
396
+ if (newline) com_port->println (message);
397
+ else com_port->print (message);
398
+ }
399
+
400
+
401
+ void Commander::verbosePrint (const char * message){
402
+ if (verbose) print (message,0 );
403
+ }
404
+ void Commander::verbosePrint (const __FlashStringHelper *message){
405
+ if (verbose) print (message,0 );
406
+ }
407
+ void Commander::printError (){
408
+ print (F (" err" ), 1 );
359
409
}
0 commit comments