Skip to content

Commit 94fd755

Browse files
committed
first part of the updates for the v2.2.1
1 parent c6bac8b commit 94fd755

File tree

18 files changed

+1061
-341
lines changed

18 files changed

+1061
-341
lines changed

_sass/overrides.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ div.language-sh pre, div.language-sh code ,div.language-shell pre, div.languag
164164
.img100{
165165
height: 100px;
166166
}
167+
.img150{
168+
height: 150px;
169+
}
167170
.img200{
168171
height: 200px;
169172
}
@@ -201,6 +204,10 @@ div.language-sh pre, div.language-sh code ,div.language-shell pre, div.languag
201204
height:inherit;
202205
width: 100%;
203206
}
207+
.img150{
208+
height:inherit;
209+
width: 100%;
210+
}
204211
.img100{
205212
height:inherit;
206213
width: 100%;

docs/simplefoc_library/code/communication/commander/index.md

Lines changed: 7 additions & 337 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ permalink: /commander_interface
66
parent: Communication
77
grand_parent: Writing the Code
88
grand_grand_parent: Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span>
9+
has_children: true
910
---
1011

1112
# Commander interface
@@ -16,7 +17,7 @@ Commander is a simple and flexible interface monitoring, supervision, configurat
1617
<img src="extras/Images/cmd_motor_get.gif" class="img100">
1718

1819
This g-code like interface provides callback to configure and tune any:
19-
- BLDC or Stepper motor
20+
- [BLDC or Stepper motor](commander_motor)
2021
- PID controllers
2122
- Low pass filters
2223
- Motion control
@@ -26,9 +27,11 @@ This g-code like interface provides callback to configure and tune any:
2627
- sensor offsets
2728
- phase resistance
2829
- ...
29-
- PID controllers
30-
- Low pass filters
31-
- Or float variables
30+
- [PID controllers](commander_pid)
31+
- [Low pass filters](commander_lpf)
32+
- [Scalar variables](commander_scalar)
33+
- [Motion control target](commander_target) <b><i>NEW</i>📢</b>
34+
- Setting target values and limits at once (ex. angle velocity torque)
3235

3336
Furthermore commander enables you to easily create your own commands and extend this interface in any way you might need for your particular application.
3437

@@ -228,339 +231,6 @@ P: some pid
228231
R: some other motor
229232
```
230233

231-
### PID commands
232-
When using a standard callback for `PIDController` class:`commander.pid(&pid,cmd)` the user will have available set of possible commands:
233-
- **P**: PID controller P gain
234-
- **I**: PID controller I gain
235-
- **D**: PID controller D gain
236-
- **R**: PID controller output ramp
237-
- **L**: PID controller output limit
238-
239-
For example if you have a PID controller added to the `commander`:
240-
```cpp
241-
PIDController pid = ....
242-
Commander commander = ...
243-
244-
void onPid(char* cmd){ commander.pid(&pid,cmd); }
245-
void setup(){
246-
...
247-
commander.add('C',onPid,"my pid");
248-
...
249-
}
250-
void loop(){
251-
...
252-
commander.run();
253-
}
254-
```
255-
You will be able to configure (set and get) its parameters from serial monitor:
256-
```sh
257-
$ CP # get P gain
258-
P: 1.0
259-
$ CD0.05 # set D gain
260-
D: 0.05
261-
$ CO # unknown command
262-
err
263-
$ CL3.25 # set output limit
264-
limit: 3.25
265-
```
266-
267-
### Low pass filter commands
268-
When using a standard callback for `LowPassFilter` class:`commander.lpf(&lpf,cmd)` the user will have available a command:
269-
- **F**: Low pass filter time constant
270-
271-
For example if you have a low pass filter added to the `commander`:
272-
```cpp
273-
LowPassFilter filter = ....
274-
Commander commander = ...
275-
276-
void onLpf(char* cmd){ commander.lpf(&filter,cmd); }
277-
void setup(){
278-
...
279-
commander.add('A',onLpf,"my lpf");
280-
...
281-
}
282-
void loop(){
283-
...
284-
commander.run();
285-
}
286-
```
287-
You will be able to configure (set and get) its parameters from serial monitor:
288-
```sh
289-
$ AF # get time constant
290-
Tf: 1.0
291-
$ AF0.05 # set time constant
292-
Tf: 0.05
293-
$ AW # unknown command
294-
err
295-
```
296-
### Motor commands
297-
When using a standard callback for `BLDCMotor` and `StepperMotor` classes:`commander.motor(&motor,cmd)` the user will have available set of possible commands:
298-
299-
- **Q** - Q current PID controller & LPF (see [pid](#pid-commands) and [lpf](#low-pass-filter-commands) for commands)
300-
- **D** - D current PID controller & LPF (see [pid](#pid-commands) and [lpf](#low-pass-filter-commands) for commands)
301-
- **V** - Velocity PID controller & LPF (see [pid](#pid-commands) and [lpf](#low-pass-filter-commands) for commands)
302-
- **A** - Angle PID controller & LPF- (see [pid](#pid-commands) and [lpf](#low-pass-filter-commands) for commands)
303-
- **L** - Limits
304-
- **C** - Current
305-
- **U** - Voltage
306-
- **V** - Velocity
307-
- **C** - Motion control type config
308-
- **D** - downsample motion loop
309-
- `0` - torque
310-
- `1` - velocity
311-
- `2` - angle
312-
- `3` - velocity_openloop
313-
- `4` - angle_openloop
314-
- **T** - Torque control type
315-
- `0` - voltage
316-
- `1` - dc_current
317-
- `2` - foc_current
318-
- **E** - Motor status (enable/disable)
319-
- `0` - enable
320-
- `1` - disable
321-
- **R** - Motor phase resistance
322-
- **S** - Sensor offsets
323-
- **M** - sensor offset
324-
- **E** - sensor electrical zero
325-
- **W** - PWM settings
326-
- **T** - pwm modulation type
327-
- **C** - pwm waveform centering boolean
328-
- **M** - Monitoring control
329-
- **D** - downsample monitoring
330-
- **C** - clear monitor
331-
- **S** - set monitoring variables
332-
- **G** - get variable value
333-
- '' - Target get/set
334-
335-
<img src="extras/Images/motor_cmd.png" class="img100">
336-
337-
For example if you have a BLDC motor added to the `commander`:
338-
```cpp
339-
BLDCMotor motor = ....
340-
Commander commander = ...
341-
342-
void onMotor(char* cmd){ commander.motor(&motor,cmd); }
343-
void setup(){
344-
...
345-
commander.add('M',onMotor,"my motor");
346-
...
347-
}
348-
void loop(){
349-
...
350-
commander.run();
351-
}
352-
```
353-
354-
You will be able to configure (set and get) its parameters from serial monitor:
355-
```sh
356-
$ MVP # get PID velocity P gain
357-
PID vel| P: 0.20
358-
$ MVP1.2 # set PID velocity P gain
359-
PID vel| P: 1.20
360-
$ MAI # get PID angle I gain
361-
PID angle| I: 0.00
362-
$ MAF # get LPF angle time constant
363-
LPF angle| Tf: 0.00
364-
$ MLV50.4 # set velocity limit
365-
Limits| vel: 50.4
366-
$ MLC # get current limit
367-
Limits| curr: 0.5
368-
$ MT # get torque control mode
369-
Torque: volt
370-
$ MT1 # set torque control mode
371-
Torque: dc curr
372-
$ MT2 # set torque control mode
373-
Torque: foc curr
374-
$ ME # get motor status enabled/disabled
375-
Status: 1
376-
$ MSM # get sensor offset
377-
Sensor| offset: 0.0
378-
$ MSM1.2 # set sensor offset
379-
Sensor| offset: 1.2
380-
$ MC # get motion control mode
381-
Motion: torque
382-
$ MC3 # set motion control mode
383-
Motion: vel open
384-
$ MC2 # set motion control mode
385-
Motion: angle
386-
$ MCD100 # get motion control downsampling
387-
Motion: downsample: 100
388-
$ MMG0 # get variable - target
389-
Monitor | target: 0.0
390-
$ MMG1 # get variable - voltage q
391-
Monitor | Vq: 1.4
392-
$ MMG6 # get variable - angle
393-
Monitor | angle: 23.5
394-
$ MMG6 # get variable - angle
395-
Monitor | angle: 24.6
396-
$ MMG6 # get variable - angle
397-
Monitor | angle: 25.5
398-
$ M0 # set target
399-
Target: 0.0
400-
$ M0.4 # set target
401-
Target: 0.4
402-
$ @1 # set verbose mode: on_request
403-
Verb | on!
404-
$ MMG6 # get variable - angle
405-
26.5
406-
$ MMG5 # get variable - velocity
407-
2.57
408-
$ #6 # set 6 decimal places
409-
Decimal: 6
410-
$ MMG6 # get variable - angle
411-
27.732821
412-
$ @0 # set verbose mode: nothing
413-
Verb: off!
414-
$ MMG6 # get variable - angle
415-
$ MMG6 # get variable - angle
416-
$ @2 # set verbose mode: user_friendly
417-
Verb: on!
418-
$ MMG6 # get variable - angle
419-
Monitor | angle: 25.532131
420-
```
421-
422-
#### Motor monitoring control commands
423-
Commander interface enables the user to control the output of the [monitoring](monitoring) functionality. The combination of the two enables user a full control of the motor configuration and tuning as well as full control of variables that are outputted to the user. In order to use his functionality the user needs to enable monitoring for the motor which is really straight-forward:
424-
```cpp
425-
BLDCMotor motor = ....
426-
Commander commander = ...
427-
428-
void onMotor(char* cmd){ commander.motor(&motor,cmd); }
429-
void setup(){
430-
...
431-
motor.useMonitoring(Serial);
432-
commander.add('M',onMotor,"my motor");
433-
...
434-
}
435-
void loop(){
436-
...
437-
motor.monitor();
438-
commander.run();
439-
}
440-
```
441-
Finally once the motor is added to the commander interface the use will be able to configure the monitoring with commands:
442-
- **M** - Monitoring control
443-
- **D** - downsample monitoring
444-
- **C** - clear monitor
445-
- **S** - set monitoring variables
446-
447-
Using these commands you can change the downsampling rate (`motor.monitor_downsampling`) of the `monitor()` function that will determine your output sampling frequency. For example if your `loop` time is around 1ms, then with downsampling of monitor function with the rate of 100, it will output the motor variables each 100ms.
448-
If monitor dowsampling is set to 0 the `monitor()` function is disabled. The same is true if the `motor.monitor_variables` bitmap is empty (equal to `0`). Therefore the command **C** effectively does:
449-
```cpp
450-
// when command MC is called
451-
motor.monitor_variables = 0;
452-
```
453-
Finally the command **MS** is used to get/set the `motor.monitor_variables` bitmap.
454-
455-
Therefore te communication could look something like this:
456-
```sh
457-
$ MMD # get monitor downsampling rate
458-
Monitor | downsample: 10
459-
$ MMD1000 # set monitor downsampling rate
460-
Monitor | downsample: 1000
461-
$ MMS # get monitor variables
462-
Monitor | 0000000
463-
$ MMS1000001 # set monitor variables (target and angle)
464-
Monitor | 1000001
465-
1.000 0.999
466-
1.000 0.985
467-
1.000 1.064
468-
.....
469-
1.000 1.040
470-
$ MMS0100000 # set monitor variables (voltage q)
471-
Monitor | 0100000
472-
1.234
473-
-0.345
474-
...
475-
0.772
476-
$ MMC # clear monitoring variables
477-
Monitor | clear
478-
$ MMS # get monitoring variables
479-
Monitor | 0000000
480-
```
481-
482-
<blockquote class="info"><p class="heading">📈 Good practice for visualization</p>
483-
When using monitoring to tune the motion control parameters or just to visualize the different variables it makes sense to disable the commander outputs so that in the serial monitor you only have monitor output. To do that use the mode <code class="highlighter-rouge">VerboseMode::nothing</code> of the commander by sending the command <code class="highlighter-rouge">@0</code>. See all <a href="#commander-commands">commander commands</a>.
484-
</blockquote>
485-
486-
487-
## Example code using the motor commands
488-
This is one simple example of using motor commands with monitoring in the code. For more examples browse through the library examples, especially through the `examples/utils/communication_tes/commander` folder.
489-
```cpp
490-
#include <SimpleFOC.h>
491-
492-
// BLDC motor & driver instance
493-
BLDCMotor motor = BLDCMotor(11);
494-
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
495-
496-
// encoder instance
497-
Encoder encoder = Encoder(2, 3, 500);
498-
// channel A and B callbacks
499-
void doA(){encoder.handleA();}
500-
void doB(){encoder.handleB();}
501-
502-
503-
// commander interface
504-
Commander command = Commander(Serial);
505-
void onMotor(char* cmd){ command.motor(&motor, cmd); }
506-
507-
void setup() {
508-
509-
// initialize encoder sensor hardware
510-
encoder.init();
511-
encoder.enableInterrupts(doA, doB);
512-
// link the motor to the sensor
513-
motor.linkSensor(&encoder);
514-
515-
// driver config
516-
// power supply voltage [V]
517-
driver.voltage_power_supply = 12;
518-
driver.init();
519-
// link driver
520-
motor.linkDriver(&driver);
521-
522-
// set control loop type to be used
523-
motor.controller = MotionControlType::torque;
524-
525-
// use monitoring with serial for motor init
526-
// monitoring port
527-
Serial.begin(115200);
528-
// comment out if not needed
529-
motor.useMonitoring(Serial);
530-
motor.monitor_downsample = 0; // initially disable real-time monitoring
531-
532-
// initialise motor
533-
motor.init();
534-
// align encoder and start FOC
535-
motor.initFOC();
536-
537-
// set the inital target value
538-
motor.target = 2;
539-
540-
// define the motor id
541-
command.add('A', onMotor, "motor");
542-
543-
// Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
544-
Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 2V."));
545-
546-
_delay(1000);
547-
}
548-
549-
550-
void loop() {
551-
// iterative setting FOC phase voltage
552-
motor.loopFOC();
553-
554-
// iterative function setting the outter loop target
555-
motor.move();
556-
557-
// monitoring
558-
motor.monitor();
559-
// user communication
560-
command.run();
561-
}
562-
```
563-
564234
## *Simple**FOC**Studio* by [@JorgeMaker](https://github.com/JorgeMaker)
565235

566236
SimpleFOCStudio is an awesome application built by [@JorgeMaker](https://github.com/JorgeMaker) which we will try to keep up to date with out library. It is a python application that uses commander interface for tunning and configuring the motor.

0 commit comments

Comments
 (0)