SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
Commander.h
Go to the documentation of this file.
1#ifndef COMMANDS_H
2#define COMMANDS_H
3
4#include "Arduino.h"
5#include "../common/base_classes/FOCMotor.h"
6#include "../common/pid.h"
7#include "../common/lowpass_filter.h"
8#include "commands.h"
9
10
11#define MAX_COMMAND_LENGTH 20
12
13
14// Commander verbose display to the user type
15enum VerboseMode : uint8_t {
16 nothing = 0x00, // display nothing - good for monitoring
17 on_request = 0x01, // display only on user request
18 user_friendly = 0x02, // display textual messages to the user
19 machine_readable = 0x03 // display machine readable commands, matching commands to set each settings
20};
21
22
23// callback function pointer definiton
24typedef void (* CommandCallback)(char*); //!< command callback function pointer
25
26/**
27 * Commander class implementing string communication protocol based on IDvalue (ex AB5.321 - command id `A`, sub-command id `B`,value `5.321`)
28 *
29 * - This class can be used in combination with HardwareSerial instance which it would read and write
30 * or it can be used to parse strings that have been received from the user outside this library
31 * - Commander class implements command protocol for few standard components of the SimpleFOC library
32 * - FOCMotor
33 * - PIDController
34 * - LowPassFilter
35 * - Commander also provides a very simple command > callback interface that enables user to
36 * attach a callback function to certain command id - see function add()
37 */
39{
40 public:
41 /**
42 * Default constructor receiving a serial interface that it uses to output the values to
43 * Also if the function run() is used it uses this serial instance to read the serial for user commands
44 *
45 * @param serial - Serial com port instance
46 * @param eol - the end of line sentinel character
47 * @param echo - echo last typed character (for command line feedback)
48 */
49 Commander(Stream &serial, char eol = '\n', bool echo = false);
50 Commander(char eol = '\n', bool echo = false);
51
52 /**
53 * Function reading the serial port and firing callbacks that have been added to the commander
54 * once the user has requested them - when he sends the command
55 *
56 * - It has default commands (the letters can be changed in the commands.h file)
57 * '@' - Verbose mode
58 * '#' - Number of decimal places
59 * '?' - Scan command - displays all the labels of attached nodes
60 */
61 void run();
62 /**
63 * Function reading the string of user input and firing callbacks that have been added to the commander
64 * once the user has requested them - when he sends the command
65 *
66 * - It has default commands (the letters can be changed in the commands.h file)
67 * '@' - Verbose mode
68 * '#' - Number of decimal places
69 * '?' - Scan command - displays all the labels of attached nodes
70 *
71 * @param reader - temporary stream to read user input
72 * @param eol - temporary end of line sentinel
73 */
74 void run(Stream &reader, char eol = '\n');
75 /**
76 * Function reading the string of user input and firing callbacks that have been added to the commander
77 * once the user has requested them - when he sends the command
78 *
79 * - It has default commands (the letters can be changed in the commands.h file)
80 * '@' - Verbose mode
81 * '#' - Number of decimal places
82 * '?' - Scan command - displays all the labels of attached nodes
83 *
84 * @param user_input - string of user inputs
85 */
86 void run(char* user_input);
87
88 /**
89 * Function adding a callback to the commander with the command id
90 * @param id - char command letter
91 * @param onCommand - function pointer void function(char*)
92 * @param label - string label to be displayed when scan command sent
93 */
94 void add(char id , CommandCallback onCommand, const char* label = nullptr);
95
96 // printing variables
97 VerboseMode verbose = VerboseMode::user_friendly; //!< flag signaling that the commands should output user understanable text
98 uint8_t decimal_places = 3; //!< number of decimal places to be used when displaying numbers
99
100 // monitoring functions
101 Stream* com_port = nullptr; //!< Serial terminal variable if provided
102 char eol = '\n'; //!< end of line sentinel character
103 bool echo = false; //!< echo last typed character (for command line feedback)
104
105 /**
106 *
107 * FOC motor (StepperMotor and BLDCMotor) command interface
108 * @param motor - FOCMotor (BLDCMotor or StepperMotor) instance
109 * @param user_cmd - the string command
110 *
111 * - It has several paramters (the letters can be changed in the commands.h file)
112 * 'Q' - Q current PID controller & LPF (see function pid and lpf for commands)
113 * 'D' - D current PID controller & LPF (see function pid and lpf for commands)
114 * 'V' - Velocity PID controller & LPF (see function pid and lpf for commands)
115 * 'A' - Angle PID controller & LPF (see function pid and lpf for commands)
116 * 'L' - Limits
117 * sub-commands:
118 * 'C' - Current
119 * 'U' - Voltage
120 * 'V' - Velocity
121 * 'C' - Motion control type config
122 * sub-commands:
123 * 'D' - downsample motiron loop
124 * '0' - torque
125 * '1' - velocity
126 * '2' - angle
127 * 'T' - Torque control type
128 * sub-commands:
129 * '0' - voltage
130 * '1' - current
131 * '2' - foc_current
132 * 'E' - Motor status (enable/disable)
133 * sub-commands:
134 * '0' - enable
135 * '1' - disable
136 * 'R' - Motor resistance
137 * 'S' - Sensor offsets
138 * sub-commands:
139 * 'M' - sensor offset
140 * 'E' - sensor electrical zero
141 * 'M' - Monitoring control
142 * sub-commands:
143 * 'D' - downsample monitoring
144 * 'C' - clear monitor
145 * 'S' - set monitoring variables
146 * 'G' - get variable value
147 * '' - Target setting interface
148 * Depends of the motion control mode:
149 * - torque : torque (ex. M2.5)
150 * - velocity (open and closed loop) : velocity torque (ex.M10 2.5 or M10 to only chanage the target witout limits)
151 * - angle (open and closed loop) : angle velocity torque (ex.M3.5 10 2.5 or M3.5 to only chanage the target witout limits)
152 *
153 * - Each of them can be get by sening the command letter -(ex. 'R' - to get the phase resistance)
154 * - Each of them can be set by sending 'IdSubidValue' - (ex. SM1.5 for setting sensor zero offset to 1.5f)
155 *
156 */
157 void motor(FOCMotor* motor, char* user_cmd);
158
159 /**
160 * Low pass fileter command interface
161 * @param lpf - LowPassFilter instance
162 * @param user_cmd - the string command
163 *
164 * - It only has one property - filtering time constant Tf
165 * - It can be get by sending 'F'
166 * - It can be set by sending 'Fvalue' - (ex. F0.01 for settin Tf=0.01)
167 */
168 void lpf(LowPassFilter* lpf, char* user_cmd);
169 /**
170 * PID controller command interface
171 * @param pid - PIDController instance
172 * @param user_cmd - the string command
173 *
174 * - It has several paramters (the letters can be changed in the commands.h file)
175 * - P gain - 'P'
176 * - I gain - 'I'
177 * - D gain - 'D'
178 * - output ramp - 'R'
179 * - output limit - 'L'
180 * - Each of them can be get by sening the command letter -(ex. 'D' - to get the D gain)
181 * - Each of them can be set by sending 'IDvalue' - (ex. I1.5 for setting I=1.5)
182 */
183 void pid(PIDController* pid, char* user_cmd);
184 /**
185 * Float variable scalar command interface
186 * @param value - float variable pointer
187 * @param user_cmd - the string command
188 *
189 * - It only has one property - one float value
190 * - It can be get by sending an empty string '\n'
191 * - It can be set by sending 'value' - (ex. 0.01f for settin *value=0.01)
192 */
193 void scalar(float* value, char* user_cmd);
194 /**
195 * Target setting interface, enables setting the target and limiting variables at once.
196 * The values are sent separated by a separator specified as the third argument. The default separator is the space.
197 *
198 * @param motor - FOCMotor (BLDCMotor or StepperMotor) instance
199 * @param user_cmd - the string command
200 * @param separator - the string separator in between target and limit values, default is space - " "
201 *
202 * Example: P2.34 70 2
203 * `P` is the user defined command, `2.34` is the target angle `70` is the target
204 * velocity and `2` is the desired max current.
205 *
206 * It depends of the motion control mode:
207 * - torque : torque (ex. P2.5)
208 * - velocity : velocity torque (ex.P10 2.5 or P10 to only chanage the target witout limits)
209 * - angle : angle velocity torque (ex.P3.5 10 2.5 or P3.5 to only chanage the target witout limits)
210 */
211 void target(FOCMotor* motor, char* user_cmd, char* separator = (char *)" ");
212
213 /**
214 * FOC motor (StepperMotor and BLDCMotor) motion control interfaces
215 * @param motor - FOCMotor (BLDCMotor or StepperMotor) instance
216 * @param user_cmd - the string command
217 * @param separator - the string separator in between target and limit values, default is space - " "
218 *
219 * Commands:
220 * 'C' - Motion control type config
221 * sub-commands:
222 * 'D' - downsample motiron loop
223 * '0' - torque
224 * '1' - velocity
225 * '2' - angle
226 * 'T' - Torque control type
227 * sub-commands:
228 * '0' - voltage
229 * '1' - current
230 * '2' - foc_current
231 * 'E' - Motor status (enable/disable)
232 * sub-commands:
233 * '0' - enable
234 * '1' - disable
235 * '' - Target setting interface
236 * Depends of the motion control mode:
237 * - torque : torque (ex. M2.5)
238 * - velocity (open and closed loop) : velocity torque (ex.M10 2.5 or M10 to only chanage the target witout limits)
239 * - angle (open and closed loop) : angle velocity torque (ex.M3.5 10 2.5 or M3.5 to only chanage the target witout limits)
240 */
241 void motion(FOCMotor* motor, char* user_cmd, char* separator = (char *)" ");
242
243 bool isSentinel(char ch);
244 private:
245 // Subscribed command callback variables
246 CommandCallback call_list[20];//!< array of command callback pointers - 20 is an arbitrary number
247 char call_ids[20]; //!< added callback commands
248 char* call_label[20]; //!< added callback labels
249 int call_count = 0;//!< number callbacks that are subscribed
250
251 // helping variable for serial communication reading
252 char received_chars[MAX_COMMAND_LENGTH] = {0}; //!< so far received user message - waiting for newline
253 int rec_cnt = 0; //!< number of characters receives
254
255 // serial printing functions
256 /**
257 * print the string message only if verbose mode on
258 * @param message - message to be printed
259 */
260 void printVerbose(const char* message);
261 /**
262 * Print the string message only if verbose mode on
263 * - Function handling the case for strings defined by F macro
264 * @param message - message to be printed
265 */
266 void printVerbose(const __FlashStringHelper *message);
267 /**
268 * print the numbers to the serial with desired decimal point number
269 * @param message - number to be printed
270 * @param newline - if needs lewline (1) otherwise (0)
271 */
272
273 void print(const float number);
274 void print(const int number);
275 void print(const char* message);
276 void print(const __FlashStringHelper *message);
277 void print(const char message);
278 void println(const float number);
279 void println(const int number);
280 void println(const char* message);
281 void println(const __FlashStringHelper *message);
282 void println(const char message);
283
284 void printMachineReadable(const float number);
285 void printMachineReadable(const int number);
286 void printMachineReadable(const char* message);
287 void printMachineReadable(const __FlashStringHelper *message);
288 void printMachineReadable(const char message);
289
290 void printlnMachineReadable(const float number);
291 void printlnMachineReadable(const int number);
292 void printlnMachineReadable(const char* message);
293 void printlnMachineReadable(const __FlashStringHelper *message);
294 void printlnMachineReadable(const char message);
295
296
297 void printError();
298};
299
300
301#endif
VerboseMode
Definition Commander.h:15
@ machine_readable
Definition Commander.h:19
@ on_request
Definition Commander.h:17
@ user_friendly
Definition Commander.h:18
@ nothing
Definition Commander.h:16
void(* CommandCallback)(char *)
command callback function pointer
Definition Commander.h:24
#define MAX_COMMAND_LENGTH
Definition Commander.h:11
void target(FOCMotor *motor, char *user_cmd, char *separator=(char *)" ")
Stream * com_port
Serial terminal variable if provided.
Definition Commander.h:101
bool isSentinel(char ch)
char eol
end of line sentinel character
Definition Commander.h:102
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
Definition Commander.h:97
uint8_t decimal_places
number of decimal places to be used when displaying numbers
Definition Commander.h:98
bool echo
echo last typed character (for command line feedback)
Definition Commander.h:103
void scalar(float *value, char *user_cmd)
void lpf(LowPassFilter *lpf, char *user_cmd)
void run()
Definition Commander.cpp:22
void pid(PIDController *pid, char *user_cmd)
void add(char id, CommandCallback onCommand, const char *label=nullptr)
Definition Commander.cpp:14