15 call_list[call_count] = onCommand;
16 call_ids[call_count] = id;
17 call_label[call_count] = (
char*)label;
29 char eol_tmp = this->
eol;
34 while (serial.available()) {
36 int ch = serial.read();
37 received_chars[rec_cnt++] = (char)ch;
61 char id = user_input[0];
64 for(
int i=0; i < call_count; i++){
68 if(call_label[i]) println(call_label[i]);
74 printVerbose(F(
"Verb:"));
85 printlnMachineReadable(F(
"machine"));
91 printVerbose(F(
"Decimal:"));
96 for(
int i=0; i < call_count; i++){
97 if(
id == call_ids[i]){
98 printMachineReadable(user_input[0]);
99 call_list[i](&user_input[1]);
110 if(isDigit(user_command[0]) || user_command[0] ==
'-' || user_command[0] ==
'+' ||
isSentinel(user_command[0])){
116 char cmd = user_command[0];
117 char sub_cmd = user_command[1];
119 int value_index = (sub_cmd >=
'A' && sub_cmd <=
'Z') || (sub_cmd ==
'#') ? 2 : 1;
121 bool GET =
isSentinel(user_command[value_index]);
123 float value = atof(&user_command[value_index]);
124 printMachineReadable(cmd);
125 if (sub_cmd >=
'A' && sub_cmd <=
'Z') {
126 printMachineReadable(sub_cmd);
132 printVerbose(F(
"PID curr q| "));
134 else pid(&
motor->PID_current_q,&user_command[1]);
137 printVerbose(F(
"PID curr d| "));
139 else pid(&
motor->PID_current_d, &user_command[1]);
142 printVerbose(F(
"PID vel| "));
144 else pid(&
motor->PID_velocity, &user_command[1]);
147 printVerbose(F(
"PID angle| "));
149 else pid(&
motor->P_angle, &user_command[1]);
152 printVerbose(F(
"Limits| "));
155 printVerbose(F(
"volt: "));
157 motor->updateVoltageLimit(value);
159 println(
motor->voltage_limit);
162 printVerbose(F(
"curr: "));
164 motor->updateCurrentLimit(value);
166 println(
motor->current_limit);
169 printVerbose(F(
"vel: "));
171 motor->updateVelocityLimit(value);
173 println(
motor->velocity_limit);
187 printVerbose(F(
"PWM Mod | "));
190 printVerbose(F(
"type: "));
192 switch(
motor->foc_modulation){
194 println(F(
"SinePWM"));
200 println(F(
"Trap 120"));
203 println(F(
"Trap 150"));
208 printVerbose(F(
"center: "));
209 if(!GET)
motor->modulation_centered = value;
210 println(
motor->modulation_centered);
218 printVerbose(F(
"R phase: "));
220 motor->phase_resistance = value;
226 printVerbose(F(
"L phase: "));
229 printVerbose(F(
"d: "));
231 motor->axis_inductance.d = value;
232 motor->phase_inductance = value;
238 printVerbose(F(
"q: "));
240 motor->axis_inductance.q = value;
247 motor->phase_inductance = value;
248 motor->axis_inductance.d = value;
249 motor->axis_inductance.q = value;
257 printVerbose(F(
"Motor KV: "));
259 motor->KV_rating = value;
266 printVerbose(F(
"Sensor | "));
269 printVerbose(F(
"offset: "));
270 if(!GET)
motor->sensor_offset = value;
271 println(
motor->sensor_offset);
274 printVerbose(F(
"el. offset: "));
275 if(!GET)
motor->zero_electric_angle = value;
276 println(
motor->zero_electric_angle);
284 printVerbose(F(
"MOT | "));
287 printVerbose(F(
"Reinit!"));
295 printVerbose(F(
"Meas motor params!"));
297 int res =
motor->characteriseMotor(value, 1.5f);
301 printVerbose(F(
"failed, err code: "));
307 printVerbose(F(
"PI tune curr.| "));
309 int res =
motor->tuneCurrentController(value);
313 printVerbose(F(
"failed, err code: "));
324 printVerbose(F(
"Monitor | "));
327 switch((uint8_t)value){
329 printVerbose(F(
"target: "));
330 println(
motor->target);
333 printVerbose(F(
"Vq: "));
334 println(
motor->voltage.q);
337 printVerbose(F(
"Vd: "));
338 println(
motor->voltage.d);
341 printVerbose(F(
"Cq: "));
342 println(
motor->current.q);
345 printVerbose(F(
"Cd: "));
346 println(
motor->current.d);
349 printVerbose(F(
"vel: "));
350 println(
motor->shaft_velocity);
353 printVerbose(F(
"angle: "));
354 println(
motor->shaft_angle);
357 printVerbose(F(
"all: "));
358 print(
motor->target);
360 print(
motor->voltage.q);
362 print(
motor->voltage.d);
364 print(
motor->current.q);
366 print(
motor->current.d);
368 print(
motor->shaft_velocity);
370 println(
motor->shaft_angle);
378 printVerbose(F(
"downsample: "));
379 if(!GET)
motor->monitor_downsample = value;
380 println((
int)
motor->monitor_downsample);
383 motor->monitor_variables = (uint8_t) 0;
387 printVerbose(F(
"decimal: "));
388 motor->monitor_decimals = value;
389 println((
int)
motor->monitor_decimals);
394 motor->monitor_variables = (uint8_t) 0;
395 for(
int i = 0; i < 7; i++){
396 if(
isSentinel(user_command[value_index+i]))
break;
397 motor->monitor_variables |= (user_command[value_index+i] -
'0') << (6-i);
401 for(
int i = 0; i < 7; i++){
402 print( (
motor->monitor_variables & (1 << (6-i))) >> (6-i));
412 printVerbose(F(
"unknown cmd "));
418 char cmd = user_cmd[0];
419 char sub_cmd = user_cmd[1];
422 float value = atof(&user_cmd[(sub_cmd >=
'A' && sub_cmd <=
'Z') ? 2 : 1]);
426 printVerbose(F(
"Motion:"));
429 printVerbose(F(
" time: "));
430 println((
int)
motor->move_time_us);
433 printVerbose(F(
" downsample: "));
434 if(!GET)
motor->motion_downsample = value;
435 println((
int)
motor->motion_downsample);
439 if(!GET && value >= 0 && (
int)value < 7)
441 switch(
motor->controller){
443 println(F(
"torque"));
452 println(F(
"vel open"));
455 println(F(
"angle open"));
458 println(F(
"angle nocascade"));
461 println(F(
"custom"));
469 printVerbose(F(
"Torque: "));
472 printVerbose(F(
" time: "));
473 println((
int)
motor->move_time_us);
476 if(!GET && (int8_t)value >= 0 && (int8_t)value < 4)
478 switch(
motor->torque_controller){
483 println(F(
"dc curr"));
486 println(F(
"foc curr"));
489 println(F(
"est. curr"));
499 printVerbose(F(
"Status: "));
500 if(!GET) (bool)value ?
motor->enable() :
motor->disable();
501 println(
motor->enabled);
510 char cmd = user_cmd[0];
512 float value = atof(&user_cmd[1]);
517 if(!GET)
pid->P = value;
522 if(!GET)
pid->I = value;
527 if(!GET)
pid->D = value;
531 printVerbose(
"ramp: ");
532 if(!GET)
pid->output_ramp = value;
533 println(
pid->output_ramp);
536 printVerbose(
"limit: ");
537 if(!GET)
pid->limit = value;
547 char cmd = user_cmd[0];
549 float value = atof(&user_cmd[1]);
553 printVerbose(F(
"Tf: "));
554 if(!GET)
lpf->Tf = value;
565 if(!GET) *value = atof(user_cmd);
573 printlnMachineReadable(
motor->target);
579 switch(
motor->controller){
582 torque = atof(strtok (user_cmd, separator));
587 vel= atof(strtok (user_cmd, separator));
591 next_value = strtok (NULL, separator);
593 torque = atof(next_value);
601 pos= atof(strtok (user_cmd, separator));
605 next_value = strtok (NULL, separator);
607 vel = atof(next_value);
608 motor->updateVelocityLimit(vel);
611 next_value = strtok (NULL, separator);
622 vel= atof(strtok (user_cmd, separator));
625 next_value = strtok (NULL, separator);
627 torque = atof(next_value);
635 pos= atof(strtok (user_cmd, separator));
639 next_value = strtok (NULL, separator);
641 vel = atof(next_value);
642 motor->updateVelocityLimit(vel);
644 next_value = strtok (NULL, separator);
646 torque = atof(next_value);
654 printVerbose(F(
"Target: "));
655 println(
motor->target);
665 printVerbose(F(
"Warn: \\r detected! \n"));
671void Commander::print(
const int number){
675void Commander::print(
const float number){
679void Commander::print(
const char* message){
683void Commander::print(
const __FlashStringHelper *message){
687void Commander::print(
const char message){
692void Commander::println(
const int number){
696void Commander::println(
const float number){
700void Commander::println(
const char* message){
704void Commander::println(
const __FlashStringHelper *message){
708void Commander::println(
const char message){
714void Commander::printVerbose(
const char* message){
717void Commander::printVerbose(
const __FlashStringHelper *message){
721void Commander::printMachineReadable(
const int number){
724void Commander::printMachineReadable(
const float number){
727void Commander::printMachineReadable(
const char* message){
730void Commander::printMachineReadable(
const __FlashStringHelper *message){
733void Commander::printMachineReadable(
const char message){
737void Commander::printlnMachineReadable(
const int number){
740void Commander::printlnMachineReadable(
const float number){
743void Commander::printlnMachineReadable(
const char* message){
746void Commander::printlnMachineReadable(
const __FlashStringHelper *message){
749void Commander::printlnMachineReadable(
const char message){
753void Commander::printError(){
void(* CommandCallback)(char *)
command callback function pointer
#define MAX_COMMAND_LENGTH
@ velocity
Velocity motion control.
@ custom
Custom control method - control method added by user.
@ angle_nocascade
Position/angle motion control without velocity cascade.
@ angle
Position/angle motion control.
@ SpaceVectorPWM
Space vector modulation method.
@ SinePWM
Sinusoidal PWM modulation.
@ estimated_current
torque control using estimated current (provided motor parameters)
@ voltage
Torque control using voltage.
@ dc_current
Torque control using DC current (one current magnitude)
@ foc_current
torque control using dq currents
void target(FOCMotor *motor, char *user_cmd, char *separator=(char *)" ")
Stream * com_port
Serial terminal variable if provided.
char eol
end of line sentinel character
void motor(FOCMotor *motor, char *user_cmd)
void motion(FOCMotor *motor, char *user_cmd, char *separator=(char *)" ")
VerboseMode verbose
flag signaling that the commands should output user understanable text
uint8_t decimal_places
number of decimal places to be used when displaying numbers
bool echo
echo last typed character (for command line feedback)
void scalar(float *value, char *user_cmd)
void lpf(LowPassFilter *lpf, char *user_cmd)
Commander(Stream &serial, char eol='\n', bool echo=false)
void pid(PIDController *pid, char *user_cmd)
void add(char id, CommandCallback onCommand, const char *label=nullptr)
#define SCMD_PID_RAMP
PID ramp.
#define CMD_LIMITS
limits current/voltage/velocity
#define SCMD_LIM_VOLT
Limit voltage.
#define CMD_V_PID
velocity PID & LPF
#define SCMD_GET
Get variable only one value.
#define CMD_SENSOR
sensor offsets
#define SCMD_PID_LIM
PID limit.
#define CMD_KV_RATING
motor kv rating
#define CMD_MOTION_TYPE
motion control type
#define CMD_MONITOR
monitoring
#define SCMD_TIME
Time between executions (filtered)
#define CMD_STATUS
motor status enable/disable
#define SCMD_PID_I
PID gain I.
#define CMD_C_D_PID
current d PID & LPF
#define CMD_FOC_PARAMS
time parameters
#define SCMD_PWMMOD_TYPE
< Pwm modulation type
#define CMD_INDUCTANCE
motor phase inductance
#define CMD_RESIST
motor phase resistance
#define SCMD_INDUCT_Q
inductance q axis
#define SCMD_SENS_MECH_OFFSET
Sensor offset.
#define SCMD_DOWNSAMPLE
Monitoring downsample value.
#define CMD_SCAN
command scaning the network - only for commander
#define CMD_TORQUE_TYPE
torque control type
#define CMD_VERBOSE
command setting output mode - only for commander
#define SCMD_LIM_CURR
Limit current.
#define SCMD_TUNE_CURR
tune current controller
#define SCMD_INDUCT_D
inductance d axis
#define SCMD_SET
Set variables to be monitored.
#define SCMD_CLEAR
Clear all monitored variables.
#define SCMD_LIM_VEL
Limit velocity.
#define SCMD_REINIT_FOC
reinitialize FOC
#define CMD_A_PID
angle PID & LPF
#define SCMD_MEAS_PARAMS
measure motor parameters (resistance and inductance)
#define SCMD_PID_D
PID gain D.
#define CMD_PWMMOD
pwm modulation
#define SCMD_PID_P
PID gain P.
#define CMD_DECIMAL
command setting decimal places - only for commander
#define SCMD_SENS_ELEC_OFFSET
Sensor electrical zero offset.
#define SCMD_LPF_TF
LPF time constant.
#define CMD_C_Q_PID
current q PID & LPF
#define SCMD_PWMMOD_CENTER
< Pwm modulation center flag