Skip to content

Commit 39cf92e

Browse files
committed
FEAY added support: verbose commands + decimal places + string input
1 parent 6874508 commit 39cf92e

File tree

4 files changed

+98
-115
lines changed

4 files changed

+98
-115
lines changed

examples/osc_control_examples/osc_esp32_3pwm/osc_esp32_3pwm.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ void cmdControl(OSCMessage &msg){
127127
char cmdStr[16];
128128
if (msg.isString(0)) {
129129
msg.getString(0,cmdStr,16);
130-
String it(cmdStr);
131-
command.motor(&motor,it);
130+
command.motor(&motor,cmdStr);
132131
}
133132
}
134133

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ skip_align KEYWORD2
107107
sensor_direction KEYWORD2
108108
sensor_offset KEYWORD2
109109
zero_electric_angle KEYWORD2
110+
verbose KEYWORD2
111+
decimal_places KEYWORD2
112+
110113

111114

112115
voltage KEYWORD2

src/communication/Commander.cpp

Lines changed: 86 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,7 @@ void Commander::run(){
1919
// end of user input
2020
if (received_chars[rec_cnt++] == '\n') {
2121
// 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);
3623

3724
// reset the command buffer
3825
received_chars[0] = 0;
@@ -41,6 +28,35 @@ void Commander::run(){
4128
}
4229
}
4330

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+
4460
void Commander::motor(FOCMotor* motor, char* user_command) {
4561
// if empty string
4662
if( user_command[0] == CMD_SCAN ){
@@ -60,70 +76,70 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
6076
// a bit of optimisation of variable memory for Arduino UNO (atmega328)
6177
switch(cmd){
6278
case CMD_C_Q_PID: //
63-
com_port->print(F("PID curr q| "));
79+
verbosePrint(F("PID curr q| "));
6480
if(sub_cmd == SCMD_LPF_TF) lpf(&motor->LPF_current_q, &user_command[1]);
6581
else pid(&motor->PID_current_q,&user_command[1]);
6682
break;
6783
case CMD_C_D_PID: //
68-
com_port->print(F("PID curr d| "));
84+
verbosePrint(F("PID curr d| "));
6985
if(sub_cmd == SCMD_LPF_TF) lpf(&motor->LPF_current_d, &user_command[1]);
7086
else pid(&motor->PID_current_d, &user_command[1]);
7187
break;
7288
case CMD_V_PID: //
73-
com_port->print(F("PID vel| "));
89+
verbosePrint(F("PID vel| "));
7490
if(sub_cmd == SCMD_LPF_TF) lpf(&motor->LPF_velocity, &user_command[1]);
7591
else pid(&motor->PID_velocity, &user_command[1]);
7692
break;
7793
case CMD_A_PID: //
78-
com_port->print(F("PID angle| "));
94+
verbosePrint(F("PID angle| "));
7995
if(sub_cmd == SCMD_LPF_TF) lpf(&motor->LPF_angle, &user_command[1]);
8096
else pid(&motor->P_angle, &user_command[1]);
8197
break;
8298
case CMD_LIMITS: //
83-
com_port->print(F("Limits| "));
99+
verbosePrint(F("Limits| "));
84100
switch (sub_cmd){
85101
case SCMD_LIM_VOLT: // voltage limit change
86-
com_port->print(F("volt: "));
102+
verbosePrint(F("volt: "));
87103
if(!GET) {
88104
motor->voltage_limit = value;
89105
motor->PID_current_d.limit = value;
90106
motor->PID_current_q.limit = value;
91107
// change velocity pid limit if in voltage mode and no phase resistance set
92108
if( !_isset(motor->phase_resistance) && motor->torque_controller==TorqueControlType::voltage) motor->PID_velocity.limit = value;
93109
}
94-
com_port->println(motor->voltage_limit);
110+
printNumber(motor->voltage_limit,1);
95111
break;
96112
case SCMD_LIM_CURR: // current limit
97-
com_port->print(F("curr: "));
113+
verbosePrint(F("curr: "));
98114
if(!GET){
99115
motor->current_limit = value;
100116
// if phase resistance is set, change the voltage limit as well.
101117
if(_isset(motor->phase_resistance)) motor->voltage_limit = value*motor->phase_resistance;
102118
// if phase resistance specified or the current control is on set the current limit to the velocity PID
103119
if(_isset(motor->phase_resistance) || motor->torque_controller != TorqueControlType::voltage ) motor->PID_velocity.limit = value;
104120
}
105-
com_port->println(motor->current_limit);
121+
printNumber(motor->current_limit,1);
106122
break;
107123
case SCMD_LIM_VEL: // velocity limit
108-
com_port->print(F("vel: "));
124+
verbosePrint(F("vel: "));
109125
if(!GET){
110126
motor->velocity_limit = value;
111127
motor->P_angle.limit = value;
112128
}
113-
com_port->println(motor->velocity_limit);
129+
printNumber(motor->velocity_limit,1);
114130
break;
115131
default:
116132
com_port->println(F("err"));
117133
break;
118134
}
119135
break;
120136
case CMD_MOTION_TYPE:
121-
com_port->print(F("Motion: "));
137+
verbosePrint(F("Motion: "));
122138
switch(sub_cmd){
123139
case SCMD_DOWNSAMPLE:
124-
com_port->print(F("downsample: "));
140+
verbosePrint(F("downsample: "));
125141
if(!GET) motor->motion_downsample = value;
126-
com_port->println(motor->motion_downsample);
142+
printNumber(motor->motion_downsample,1);
127143
break;
128144
default:
129145
// change control type
@@ -151,7 +167,7 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
151167
break;
152168
case CMD_TORQUE_TYPE:
153169
// change control type
154-
com_port->print(F("Torque: "));
170+
verbosePrint(F("Torque: "));
155171
if(!GET && (int8_t)value >= 0 && (int8_t)value < 3)// if set command
156172
motor->torque_controller = (TorqueControlType)value;
157173
switch(motor->torque_controller){
@@ -168,83 +184,84 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
168184
break;
169185
case CMD_STATUS:
170186
// enable/disable
171-
com_port->print(F("Status: "));
187+
verbosePrint(F("Status: "));
172188
if(!GET) (bool)value ? motor->enable() : motor->disable();
173189
com_port->println(motor->enabled);
174190
break;
175191
case CMD_RESIST:
176192
// enable/disable
177-
com_port->print(F("R phase: "));
193+
verbosePrint(F("R phase: "));
178194
if(!GET){
179195
motor->phase_resistance = value;
180196
if(motor->torque_controller==TorqueControlType::voltage){
181197
motor->voltage_limit = motor->current_limit*value;
182198
motor->PID_velocity.limit= motor->current_limit;
183199
}
184200
}
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);
186203
break;
187204
case CMD_SENSOR:
188205
// Sensor zero offset
189-
com_port->print(F("Sensor | "));
206+
verbosePrint(F("Sensor | "));
190207
switch (sub_cmd){
191208
case SCMD_SENS_MECH_OFFSET: // zero offset
192-
com_port->print(F("offset: "));
209+
verbosePrint(F("offset: "));
193210
if(!GET) motor->sensor_offset = value;
194-
com_port->println(motor->sensor_offset);
211+
printNumber(motor->sensor_offset,1);
195212
break;
196213
case SCMD_SENS_ELEC_OFFSET: // electrical zero offset - not suggested to touch
197-
com_port->print(F("el. offset: "));
214+
verbosePrint(F("el. offset: "));
198215
if(!GET) motor->zero_electric_angle = value;
199-
com_port->println(motor->zero_electric_angle);
216+
printNumber(motor->zero_electric_angle,1);
200217
break;
201218
default:
202219
com_port->println(F("err"));
203220
break;
204221
}
205222
break;
206223
case CMD_MONITOR: // get current values of the state variables
207-
com_port->print(F("Monitor | "));
224+
verbosePrint(F("Monitor | "));
208225
switch (sub_cmd){
209226
case SCMD_GET: // get command
210227
switch((uint8_t)value){
211228
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);
214231
break;
215232
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);
218235
break;
219236
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);
222239
break;
223240
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);
226243
break;
227244
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);
230247
break;
231248
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);
234251
break;
235252
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);
238255
break;
239256
default:
240257
com_port->println(F("err"));
241258
break;
242259
}
243260
break;
244261
case SCMD_DOWNSAMPLE:
245-
com_port->print(F("downsample: "));
262+
verbosePrint(F("downsample: "));
246263
if(!GET) motor->monitor_downsample = value;
247-
com_port->println(motor->monitor_downsample);
264+
printNumber(motor->monitor_downsample,1);
248265
break;
249266
case SCMD_CLEAR:
250267
for(int i=0; i<7; i++) motor->monitor_variables[i] = 0;
@@ -261,12 +278,11 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
261278
com_port->println(F("err"));
262279
break;
263280
}
264-
265281
break;
266282
default: // target change
267-
com_port->print(F("Target: "));
283+
verbosePrint(F("Target: "));
268284
motor->target = atof(user_command);
269-
com_port->println(motor->target);
285+
printNumber(motor->target,1);
270286
}
271287
}
272288

@@ -281,29 +297,29 @@ void Commander::pid(PIDController* pid, char* user_cmd){
281297

282298
switch (cmd){
283299
case SCMD_PID_P: // P gain change
284-
com_port->print("P: ");
300+
verbosePrint("P: ");
285301
if(!GET) pid->P = value;
286-
com_port->println(pid->P);
302+
printNumber(pid->P,1);
287303
break;
288304
case SCMD_PID_I: // I gain change
289-
com_port->print("I: ");
305+
verbosePrint("I: ");
290306
if(!GET) pid->I = value;
291-
com_port->println(pid->I);
307+
printNumber(pid->I,1);
292308
break;
293309
case SCMD_PID_D: // D gain change
294-
com_port->print("D: ");
310+
verbosePrint("D: ");
295311
if(!GET) pid->D = value;
296-
com_port->println(pid->D);
312+
printNumber(pid->D,1);
297313
break;
298314
case SCMD_PID_RAMP: // ramp change
299-
com_port->print("ramp: ");
315+
verbosePrint("ramp: ");
300316
if(!GET) pid->output_ramp = value;
301-
com_port->println(pid->output_ramp);
317+
printNumber(pid->output_ramp,1);
302318
break;
303319
case SCMD_PID_LIM: // limit change
304-
com_port->print("limit: ");
320+
verbosePrint("limit: ");
305321
if(!GET) pid->limit = value;
306-
com_port->println(pid->limit);
322+
printNumber(pid->limit,1);
307323
break;
308324
default:
309325
com_port->println(F("err"));
@@ -322,9 +338,9 @@ void Commander::lpf(LowPassFilter* lpf, char* user_cmd){
322338

323339
switch (cmd){
324340
case SCMD_LPF_TF: // Tf value change
325-
com_port->print("Tf: ");
341+
verbosePrint("Tf: ");
326342
if(!GET) lpf->Tf = value;
327-
com_port->println(lpf->Tf);
343+
printNumber(lpf->Tf,1);
328344
break;
329345
default:
330346
com_port->println(F("err"));
@@ -339,5 +355,5 @@ void Commander::variable(float* value, char* user_cmd){
339355
}
340356
bool GET = user_cmd[0] == '\n';
341357
if(!GET) *value = atof(user_cmd);
342-
com_port->println(*value);
358+
printNumber(*value,1);
343359
}

0 commit comments

Comments
 (0)