@@ -19,20 +19,7 @@ void Commander::run(){
19
19
// end of user input
20
20
if (received_chars[rec_cnt++] == ' \n ' ) {
21
21
// execute the user command
22
- char id = received_chars[0 ];
23
- if (id == CMD_SCAN)
24
- for (int i=0 ; i < call_count; i++){
25
- com_port->print (call_ids[i]);
26
- com_port->print (" :" );
27
- call_list[i](cmd_scan_msg);
28
- }
29
- else
30
- for (int i=0 ; i < call_count; i++){
31
- if (id == call_ids[i]){
32
- call_list[i](&received_chars[1 ]);
33
- break ;
34
- }
35
- }
22
+ run (received_chars);
36
23
37
24
// reset the command buffer
38
25
received_chars[0 ] = 0 ;
@@ -41,6 +28,35 @@ void Commander::run(){
41
28
}
42
29
}
43
30
31
+ void Commander::run (char * user_input){
32
+ // execute the user command
33
+ char id = received_chars[0 ];
34
+ if (id == CMD_SCAN)
35
+ for (int i=0 ; i < call_count; i++){
36
+ com_port->print (call_ids[i]);
37
+ com_port->print (" :" );
38
+ call_list[i](cmd_scan_msg);
39
+ }
40
+ else
41
+ for (int i=0 ; i < call_count; i++){
42
+ if (id == call_ids[i]){
43
+ call_list[i](&received_chars[1 ]);
44
+ break ;
45
+ }
46
+ }
47
+ }
48
+
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
+
44
60
void Commander::motor (FOCMotor* motor, char * user_command) {
45
61
// if empty string
46
62
if ( user_command[0 ] == CMD_SCAN ){
@@ -60,70 +76,70 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
60
76
// a bit of optimisation of variable memory for Arduino UNO (atmega328)
61
77
switch (cmd){
62
78
case CMD_C_Q_PID: //
63
- com_port-> print (F (" PID curr q| " ));
79
+ verbosePrint (F (" PID curr q| " ));
64
80
if (sub_cmd == SCMD_LPF_TF) lpf (&motor->LPF_current_q , &user_command[1 ]);
65
81
else pid (&motor->PID_current_q ,&user_command[1 ]);
66
82
break ;
67
83
case CMD_C_D_PID: //
68
- com_port-> print (F (" PID curr d| " ));
84
+ verbosePrint (F (" PID curr d| " ));
69
85
if (sub_cmd == SCMD_LPF_TF) lpf (&motor->LPF_current_d , &user_command[1 ]);
70
86
else pid (&motor->PID_current_d , &user_command[1 ]);
71
87
break ;
72
88
case CMD_V_PID: //
73
- com_port-> print (F (" PID vel| " ));
89
+ verbosePrint (F (" PID vel| " ));
74
90
if (sub_cmd == SCMD_LPF_TF) lpf (&motor->LPF_velocity , &user_command[1 ]);
75
91
else pid (&motor->PID_velocity , &user_command[1 ]);
76
92
break ;
77
93
case CMD_A_PID: //
78
- com_port-> print (F (" PID angle| " ));
94
+ verbosePrint (F (" PID angle| " ));
79
95
if (sub_cmd == SCMD_LPF_TF) lpf (&motor->LPF_angle , &user_command[1 ]);
80
96
else pid (&motor->P_angle , &user_command[1 ]);
81
97
break ;
82
98
case CMD_LIMITS: //
83
- com_port-> print (F (" Limits| " ));
99
+ verbosePrint (F (" Limits| " ));
84
100
switch (sub_cmd){
85
101
case SCMD_LIM_VOLT: // voltage limit change
86
- com_port-> print (F (" volt: " ));
102
+ verbosePrint (F (" volt: " ));
87
103
if (!GET) {
88
104
motor->voltage_limit = value;
89
105
motor->PID_current_d .limit = value;
90
106
motor->PID_current_q .limit = value;
91
107
// change velocity pid limit if in voltage mode and no phase resistance set
92
108
if ( !_isset (motor->phase_resistance ) && motor->torque_controller ==TorqueControlType::voltage) motor->PID_velocity .limit = value;
93
109
}
94
- com_port-> println (motor->voltage_limit );
110
+ printNumber (motor->voltage_limit , 1 );
95
111
break ;
96
112
case SCMD_LIM_CURR: // current limit
97
- com_port-> print (F (" curr: " ));
113
+ verbosePrint (F (" curr: " ));
98
114
if (!GET){
99
115
motor->current_limit = value;
100
116
// if phase resistance is set, change the voltage limit as well.
101
117
if (_isset (motor->phase_resistance )) motor->voltage_limit = value*motor->phase_resistance ;
102
118
// if phase resistance specified or the current control is on set the current limit to the velocity PID
103
119
if (_isset (motor->phase_resistance ) || motor->torque_controller != TorqueControlType::voltage ) motor->PID_velocity .limit = value;
104
120
}
105
- com_port-> println (motor->current_limit );
121
+ printNumber (motor->current_limit , 1 );
106
122
break ;
107
123
case SCMD_LIM_VEL: // velocity limit
108
- com_port-> print (F (" vel: " ));
124
+ verbosePrint (F (" vel: " ));
109
125
if (!GET){
110
126
motor->velocity_limit = value;
111
127
motor->P_angle .limit = value;
112
128
}
113
- com_port-> println (motor->velocity_limit );
129
+ printNumber (motor->velocity_limit , 1 );
114
130
break ;
115
131
default :
116
132
com_port->println (F (" err" ));
117
133
break ;
118
134
}
119
135
break ;
120
136
case CMD_MOTION_TYPE:
121
- com_port-> print (F (" Motion: " ));
137
+ verbosePrint (F (" Motion: " ));
122
138
switch (sub_cmd){
123
139
case SCMD_DOWNSAMPLE:
124
- com_port-> print (F (" downsample: " ));
140
+ verbosePrint (F (" downsample: " ));
125
141
if (!GET) motor->motion_downsample = value;
126
- com_port-> println (motor->motion_downsample );
142
+ printNumber (motor->motion_downsample , 1 );
127
143
break ;
128
144
default :
129
145
// change control type
@@ -151,7 +167,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
151
167
break ;
152
168
case CMD_TORQUE_TYPE:
153
169
// change control type
154
- com_port-> print (F (" Torque: " ));
170
+ verbosePrint (F (" Torque: " ));
155
171
if (!GET && (int8_t )value >= 0 && (int8_t )value < 3 )// if set command
156
172
motor->torque_controller = (TorqueControlType)value;
157
173
switch (motor->torque_controller ){
@@ -168,83 +184,84 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
168
184
break ;
169
185
case CMD_STATUS:
170
186
// enable/disable
171
- com_port-> print (F (" Status: " ));
187
+ verbosePrint (F (" Status: " ));
172
188
if (!GET) (bool )value ? motor->enable () : motor->disable ();
173
189
com_port->println (motor->enabled );
174
190
break ;
175
191
case CMD_RESIST:
176
192
// enable/disable
177
- com_port-> print (F (" R phase: " ));
193
+ verbosePrint (F (" R phase: " ));
178
194
if (!GET){
179
195
motor->phase_resistance = value;
180
196
if (motor->torque_controller ==TorqueControlType::voltage){
181
197
motor->voltage_limit = motor->current_limit *value;
182
198
motor->PID_velocity .limit = motor->current_limit ;
183
199
}
184
200
}
185
- com_port->println (_isset (motor->phase_resistance ) ? motor->phase_resistance : 0 );
201
+ if (_isset (motor->phase_resistance )) printNumber (motor->phase_resistance ,1 );
202
+ else com_port->println (0 );
186
203
break ;
187
204
case CMD_SENSOR:
188
205
// Sensor zero offset
189
- com_port-> print (F (" Sensor | " ));
206
+ verbosePrint (F (" Sensor | " ));
190
207
switch (sub_cmd){
191
208
case SCMD_SENS_MECH_OFFSET: // zero offset
192
- com_port-> print (F (" offset: " ));
209
+ verbosePrint (F (" offset: " ));
193
210
if (!GET) motor->sensor_offset = value;
194
- com_port-> println (motor->sensor_offset );
211
+ printNumber (motor->sensor_offset , 1 );
195
212
break ;
196
213
case SCMD_SENS_ELEC_OFFSET: // electrical zero offset - not suggested to touch
197
- com_port-> print (F (" el. offset: " ));
214
+ verbosePrint (F (" el. offset: " ));
198
215
if (!GET) motor->zero_electric_angle = value;
199
- com_port-> println (motor->zero_electric_angle );
216
+ printNumber (motor->zero_electric_angle , 1 );
200
217
break ;
201
218
default :
202
219
com_port->println (F (" err" ));
203
220
break ;
204
221
}
205
222
break ;
206
223
case CMD_MONITOR: // get current values of the state variables
207
- com_port-> print (F (" Monitor | " ));
224
+ verbosePrint (F (" Monitor | " ));
208
225
switch (sub_cmd){
209
226
case SCMD_GET: // get command
210
227
switch ((uint8_t )value){
211
228
case 0 : // get target
212
- com_port-> print (F (" target: " ));
213
- com_port-> println (motor->target );
229
+ verbosePrint (F (" target: " ));
230
+ printNumber (motor->target , 1 );
214
231
break ;
215
232
case 1 : // get voltage q
216
- com_port-> print (F (" Vq: " ));
217
- com_port-> println (motor->voltage .q );
233
+ verbosePrint (F (" Vq: " ));
234
+ printNumber (motor->voltage .q , 1 );
218
235
break ;
219
236
case 2 : // get voltage d
220
- com_port-> print (F (" Vd: " ));
221
- com_port-> println (motor->voltage .q );
237
+ verbosePrint (F (" Vd: " ));
238
+ printNumber (motor->voltage .q , 1 );
222
239
break ;
223
240
case 3 : // get current q
224
- com_port-> print (F (" Cq: " ));
225
- com_port-> println (motor->voltage .q );
241
+ verbosePrint (F (" Cq: " ));
242
+ printNumber (motor->voltage .q , 1 );
226
243
break ;
227
244
case 4 : // get current d
228
- com_port-> print (F (" Cd: " ));
229
- com_port-> println (motor->voltage .q );
245
+ verbosePrint (F (" Cd: " ));
246
+ printNumber (motor->voltage .q , 1 );
230
247
break ;
231
248
case 5 : // get velocity
232
- com_port-> print (F (" vel: " ));
233
- com_port-> println (motor->shaft_velocity );
249
+ verbosePrint (F (" vel: " ));
250
+ printNumber (motor->shaft_velocity , 1 );
234
251
break ;
235
252
case 6 : // get angle
236
- com_port-> print (F (" Angle: " ));
237
- com_port-> println (motor->shaft_angle );
253
+ verbosePrint (F (" Angle: " ));
254
+ printNumber (motor->shaft_angle , 1 );
238
255
break ;
239
256
default :
240
257
com_port->println (F (" err" ));
241
258
break ;
242
259
}
243
260
break ;
244
261
case SCMD_DOWNSAMPLE:
245
- com_port-> print (F (" downsample: " ));
262
+ verbosePrint (F (" downsample: " ));
246
263
if (!GET) motor->monitor_downsample = value;
247
- com_port-> println (motor->monitor_downsample );
264
+ printNumber (motor->monitor_downsample , 1 );
248
265
break ;
249
266
case SCMD_CLEAR:
250
267
for (int i=0 ; i<7 ; i++) motor->monitor_variables [i] = 0 ;
@@ -261,12 +278,11 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
261
278
com_port->println (F (" err" ));
262
279
break ;
263
280
}
264
-
265
281
break ;
266
282
default : // target change
267
- com_port-> print (F (" Target: " ));
283
+ verbosePrint (F (" Target: " ));
268
284
motor->target = atof (user_command);
269
- com_port-> println (motor->target );
285
+ printNumber (motor->target , 1 );
270
286
}
271
287
}
272
288
@@ -281,29 +297,29 @@ void Commander::pid(PIDController* pid, char* user_cmd){
281
297
282
298
switch (cmd){
283
299
case SCMD_PID_P: // P gain change
284
- com_port-> print (" P: " );
300
+ verbosePrint (" P: " );
285
301
if (!GET) pid->P = value;
286
- com_port-> println (pid->P );
302
+ printNumber (pid->P , 1 );
287
303
break ;
288
304
case SCMD_PID_I: // I gain change
289
- com_port-> print (" I: " );
305
+ verbosePrint (" I: " );
290
306
if (!GET) pid->I = value;
291
- com_port-> println (pid->I );
307
+ printNumber (pid->I , 1 );
292
308
break ;
293
309
case SCMD_PID_D: // D gain change
294
- com_port-> print (" D: " );
310
+ verbosePrint (" D: " );
295
311
if (!GET) pid->D = value;
296
- com_port-> println (pid->D );
312
+ printNumber (pid->D , 1 );
297
313
break ;
298
314
case SCMD_PID_RAMP: // ramp change
299
- com_port-> print (" ramp: " );
315
+ verbosePrint (" ramp: " );
300
316
if (!GET) pid->output_ramp = value;
301
- com_port-> println (pid->output_ramp );
317
+ printNumber (pid->output_ramp , 1 );
302
318
break ;
303
319
case SCMD_PID_LIM: // limit change
304
- com_port-> print (" limit: " );
320
+ verbosePrint (" limit: " );
305
321
if (!GET) pid->limit = value;
306
- com_port-> println (pid->limit );
322
+ printNumber (pid->limit , 1 );
307
323
break ;
308
324
default :
309
325
com_port->println (F (" err" ));
@@ -322,9 +338,9 @@ void Commander::lpf(LowPassFilter* lpf, char* user_cmd){
322
338
323
339
switch (cmd){
324
340
case SCMD_LPF_TF: // Tf value change
325
- com_port-> print (" Tf: " );
341
+ verbosePrint (" Tf: " );
326
342
if (!GET) lpf->Tf = value;
327
- com_port-> println (lpf->Tf );
343
+ printNumber (lpf->Tf , 1 );
328
344
break ;
329
345
default :
330
346
com_port->println (F (" err" ));
@@ -339,5 +355,5 @@ void Commander::variable(float* value, char* user_cmd){
339
355
}
340
356
bool GET = user_cmd[0 ] == ' \n ' ;
341
357
if (!GET) *value = atof (user_cmd);
342
- com_port-> println (*value);
358
+ printNumber (*value, 1 );
343
359
}
0 commit comments