Skip to content

Commit f3438a1

Browse files
committed
Add machine readable serial formating
1 parent 8c65a5f commit f3438a1

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed

src/common/base_classes/FOCMotor.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,28 @@ void FOCMotor::monitor() {
9494
bool printed = 0;
9595

9696
if(monitor_variables & _MON_TARGET){
97+
if (monitor_prepend_id && !printed) {
98+
monitor_port->print(monitor_prepend_id);
99+
monitor_port->print("M");
100+
}
97101
monitor_port->print(target,4);
98102
monitor_port->print("\t");
99103
printed= true;
100104
}
101105
if(monitor_variables & _MON_VOLT_Q) {
106+
if (monitor_prepend_id && !printed) {
107+
monitor_port->print(monitor_prepend_id);
108+
monitor_port->print("M");
109+
}
102110
monitor_port->print(voltage.q,4);
103111
monitor_port->print("\t");
104112
printed= true;
105113
}
106114
if(monitor_variables & _MON_VOLT_D) {
115+
if (monitor_prepend_id && !printed) {
116+
monitor_port->print(monitor_prepend_id);
117+
monitor_port->print("M");
118+
}
107119
monitor_port->print(voltage.d,4);
108120
monitor_port->print("\t");
109121
printed= true;
@@ -117,23 +129,39 @@ void FOCMotor::monitor() {
117129
c.d = LPF_current_d(c.d);
118130
}
119131
if(monitor_variables & _MON_CURR_Q) {
132+
if (monitor_prepend_id && !printed) {
133+
monitor_port->print(monitor_prepend_id);
134+
monitor_port->print("M");
135+
}
120136
monitor_port->print(c.q*1000, 2); // mAmps
121137
monitor_port->print("\t");
122138
printed= true;
123139
}
124140
if(monitor_variables & _MON_CURR_D) {
141+
if (monitor_prepend_id && !printed) {
142+
monitor_port->print(monitor_prepend_id);
143+
monitor_port->print("M");
144+
}
125145
monitor_port->print(c.d*1000, 2); // mAmps
126146
monitor_port->print("\t");
127147
printed= true;
128148
}
129149
}
130150

131151
if(monitor_variables & _MON_VEL) {
152+
if (monitor_prepend_id && !printed) {
153+
monitor_port->print(monitor_prepend_id);
154+
monitor_port->print("M");
155+
}
132156
monitor_port->print(shaft_velocity,4);
133157
monitor_port->print("\t");
134158
printed= true;
135159
}
136160
if(monitor_variables & _MON_ANGLE) {
161+
if (monitor_prepend_id && !printed) {
162+
monitor_port->print(monitor_prepend_id);
163+
monitor_port->print("M");
164+
}
137165
monitor_port->print(shaft_angle,4);
138166
printed= true;
139167
}

src/communication/Commander.cpp

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void Commander::run(char* user_input){
6262
switch(id){
6363
case CMD_SCAN:
6464
for(int i=0; i < call_count; i++){
65+
printMachineReadable(F("?"));
6566
print(call_ids[i]);
6667
print(":");
6768
if(call_label[i]) println(call_label[i]);
@@ -79,16 +80,21 @@ void Commander::run(char* user_input){
7980
case VerboseMode::user_friendly:
8081
println(F("on!"));
8182
break;
83+
case VerboseMode::machine_readable:
84+
printlnMachineReadable(F("@3"));
85+
break;
8286
}
8387
break;
8488
case CMD_DECIMAL:
8589
if(!isSentinel(user_input[1])) decimal_places = atoi(&user_input[1]);
8690
printVerbose(F("Decimal:"));
91+
printMachineReadable(F("#"));
8792
println(decimal_places);
8893
break;
8994
default:
9095
for(int i=0; i < call_count; i++){
9196
if(id == call_ids[i]){
97+
printMachineReadable(user_input[0]);
9298
call_list[i](&user_input[1]);
9399
break;
94100
}
@@ -100,7 +106,7 @@ void Commander::run(char* user_input){
100106
void Commander::motor(FOCMotor* motor, char* user_command) {
101107

102108
// if target setting
103-
if(isDigit(user_command[0]) || user_command[0] == '-' || user_command[0] == '+'){
109+
if(isDigit(user_command[0]) || user_command[0] == '-' || user_command[0] == '+' || isSentinel(user_command[0])){
104110
target(motor, user_command);
105111
return;
106112
}
@@ -114,7 +120,10 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
114120
bool GET = isSentinel(user_command[value_index]);
115121
// parse command values
116122
float value = atof(&user_command[value_index]);
117-
123+
printMachineReadable(cmd);
124+
if (sub_cmd >= 'A' && sub_cmd <= 'Z') {
125+
printMachineReadable(sub_cmd);
126+
}
118127

119128
// a bit of optimisation of variable memory for Arduino UNO (atmega328)
120129
switch(cmd){
@@ -313,9 +322,9 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
313322
break;
314323
case SCMD_SET:
315324
if(!GET) motor->monitor_variables = (uint8_t) 0;
316-
for(int i = 0; i < 7; i++){
325+
for(int i = 0; i < 8; i++){
317326
if(isSentinel(user_command[value_index+i])) break;
318-
if(!GET) motor->monitor_variables |= (user_command[value_index+i] - '0') << (6-i);
327+
if(!GET) motor->monitor_variables |= (user_command[value_index+i] - '0') << (7-i);
319328
print( (user_command[value_index+i] - '0') );
320329
}
321330
println("");
@@ -468,8 +477,11 @@ void Commander::scalar(float* value, char* user_cmd){
468477

469478
void Commander::target(FOCMotor* motor, char* user_cmd, char* separator){
470479
// if no values sent
471-
if(isSentinel(user_cmd[0])) return;
472-
480+
if(isSentinel(user_cmd[0])) {
481+
printlnMachineReadable(motor->target);
482+
return;
483+
};
484+
473485
float pos, vel, torque;
474486
char* next_value;
475487
switch(motor->controller){
@@ -614,6 +626,39 @@ void Commander::printVerbose(const char* message){
614626
void Commander::printVerbose(const __FlashStringHelper *message){
615627
if(verbose == VerboseMode::user_friendly) print(message);
616628
}
629+
630+
void Commander::printMachineReadable(const int number){
631+
if(verbose == VerboseMode::machine_readable) print(number);
632+
}
633+
void Commander::printMachineReadable(const float number){
634+
if(verbose == VerboseMode::machine_readable) print(number);
635+
}
636+
void Commander::printMachineReadable(const char* message){
637+
if(verbose == VerboseMode::machine_readable) print(message);
638+
}
639+
void Commander::printMachineReadable(const __FlashStringHelper *message){
640+
if(verbose == VerboseMode::machine_readable) print(message);
641+
}
642+
void Commander::printMachineReadable(const char message){
643+
if(verbose == VerboseMode::machine_readable) print(message);
644+
}
645+
646+
void Commander::printlnMachineReadable(const int number){
647+
if(verbose == VerboseMode::machine_readable) println(number);
648+
}
649+
void Commander::printlnMachineReadable(const float number){
650+
if(verbose == VerboseMode::machine_readable) println(number);
651+
}
652+
void Commander::printlnMachineReadable(const char* message){
653+
if(verbose == VerboseMode::machine_readable) println(message);
654+
}
655+
void Commander::printlnMachineReadable(const __FlashStringHelper *message){
656+
if(verbose == VerboseMode::machine_readable) println(message);
657+
}
658+
void Commander::printlnMachineReadable(const char message){
659+
if(verbose == VerboseMode::machine_readable) println(message);
660+
}
661+
617662
void Commander::printError(){
618663
println(F("err"));
619664
}

src/communication/Commander.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
enum VerboseMode : uint8_t {
1616
nothing = 0x00, // display nothing - good for monitoring
1717
on_request = 0x01, // display only on user request
18-
user_friendly = 0x02 // display textual messages to the user
18+
user_friendly = 0x02, // display textual messages to the user
19+
machine_readable = 0x03 // display machine readable commands, matching commands to set each settings
1920
};
2021

2122

@@ -279,6 +280,19 @@ class Commander
279280
void println(const __FlashStringHelper *message);
280281
void println(const char message);
281282

283+
void printMachineReadable(const float number);
284+
void printMachineReadable(const int number);
285+
void printMachineReadable(const char* message);
286+
void printMachineReadable(const __FlashStringHelper *message);
287+
void printMachineReadable(const char message);
288+
289+
void printlnMachineReadable(const float number);
290+
void printlnMachineReadable(const int number);
291+
void printlnMachineReadable(const char* message);
292+
void printlnMachineReadable(const __FlashStringHelper *message);
293+
void printlnMachineReadable(const char message);
294+
295+
282296
void printError();
283297
bool isSentinel(char ch);
284298
};

0 commit comments

Comments
 (0)