Skip to content

Commit 9b47aa9

Browse files
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 0323dbb + 58e82d4 commit 9b47aa9

File tree

23 files changed

+558
-274
lines changed

23 files changed

+558
-274
lines changed

.github/workflows/ccpp.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ jobs:
2626
- arduino:mbed_rp2040:pico # rpi pico
2727
include:
2828
- arduino-boards-fqbn: arduino:avr:nano
29-
sketches-exclude: calibrated mt6816_spi smoothing simplefocnano_torque_voltage
29+
sketches-exclude: calibrated calibration_save mt6816_spi smoothing simplefocnano_torque_voltage
3030
required-libraries: Simple FOC
3131
- arduino-boards-fqbn: arduino:sam:arduino_due_x
3232
required-libraries: Simple FOC
33-
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega
33+
sketches-exclude: calibrated calibration_save smoothing simplefocnano_torque_voltage simplefocnano_atmega
3434
- arduino-boards-fqbn: arduino:samd:nano_33_iot
3535
required-libraries: Simple FOC
36-
sketches-exclude: calibrated smoothing
36+
sketches-exclude: calibrated calibration_save smoothing
3737
- arduino-boards-fqbn: arduino:mbed_rp2040:pico
3838
required-libraries: Simple FOC
3939
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega

examples/encoders/calibrated/calibrated.ino renamed to examples/encoders/calibrated_sensor/calibrated/calibrated.ino

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
/**
2-
* Torque control example using voltage control loop.
3-
*
4-
* Most of the low-end BLDC driver boards doesn't have current measurement therefore SimpleFOC offers
5-
* you a way to control motor torque by setting the voltage to the motor instead hte current.
6-
*
7-
* This makes the BLDC motor effectively a DC motor, and you can use it in a same way.
2+
* The example demonstrates the usage of the calibrated sensor object.
83
*/
94
#include <SimpleFOC.h>
105
#include <SimpleFOCDrivers.h>
@@ -16,6 +11,8 @@ MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, PB6);
1611
BLDCMotor motor = BLDCMotor(11);
1712
BLDCDriver3PWM driver = BLDCDriver3PWM(PB4,PC7,PB10,PA9);
1813
// instantiate the calibrated sensor object
14+
// argument 1 - sensor object
15+
// argument 2 - number of samples in the LUT (default 200)
1916
CalibratedSensor sensor_calibrated = CalibratedSensor(sensor);
2017

2118
// voltage set point variable
@@ -58,6 +55,7 @@ void setup() {
5855
// set voltage to run calibration
5956
sensor_calibrated.voltage_calibration = 6;
6057
// Running calibration
58+
// it will ouptut the LUT and the zero electrical angle to the serial monitor !!!!
6159
sensor_calibrated.calibrate(motor);
6260

6361
//Serial.println("Calibrating Sensor Done.");
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* The example demonstrates the calibration of the magnetic sensor with the calibration procedure and saving the calibration data.
3+
* So that the calibration procedure does not have to be run every time the motor is powered up.
4+
*/
5+
6+
#include <SimpleFOC.h>
7+
#include <SimpleFOCDrivers.h>
8+
#include "encoders/calibrated/CalibratedSensor.h"
9+
10+
// fill this array with the calibration values outputed by the calibration procedure
11+
float calibrationLut[50] = {0};
12+
float zero_electric_angle = 0;
13+
Direction sensor_direction = Direction::CW;
14+
15+
// magnetic sensor instance - SPI
16+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 14);
17+
// Stepper motor & driver instance
18+
StepperMotor motor = StepperMotor(50);
19+
StepperDriver4PWM driver = StepperDriver4PWM(10, 9, 5, 6,8);
20+
// instantiate the calibrated sensor object
21+
// instantiate the calibrated sensor object
22+
// argument 1 - sensor object
23+
// argument 2 - number of samples in the LUT (default 200)
24+
// argument 3 - pointer to the LUT array (defualt nullptr)
25+
CalibratedSensor sensor_calibrated = CalibratedSensor(sensor, 50);
26+
27+
// voltage set point variable
28+
float target_voltage = 2;
29+
30+
// instantiate the commander
31+
Commander command = Commander(Serial);
32+
33+
void doTarget(char* cmd) { command.scalar(&target_voltage, cmd); }
34+
35+
void setup() {
36+
37+
sensor.init();
38+
// Link motor to sensor
39+
motor.linkSensor(&sensor);
40+
// power supply voltage
41+
driver.voltage_power_supply = 20;
42+
driver.init();
43+
motor.linkDriver(&driver);
44+
// aligning voltage
45+
motor.voltage_sensor_align = 8;
46+
motor.voltage_limit = 20;
47+
// set motion control loop to be used
48+
motor.controller = MotionControlType::torque;
49+
50+
// use monitoring with serial
51+
Serial.begin(115200);
52+
// comment out if not needed
53+
motor.useMonitoring(Serial);
54+
motor.monitor_variables = _MON_VEL;
55+
motor.monitor_downsample = 10; // default 10
56+
57+
// initialize motor
58+
motor.init();
59+
60+
// Running calibration
61+
// the function will setup everything for the provided calibration LUT
62+
sensor_calibrated.calibrate(motor, calibrationLut, zero_electric_angle, sensor_direction);
63+
64+
// Linking sensor to motor object
65+
motor.linkSensor(&sensor_calibrated);
66+
67+
// calibrated init FOC
68+
motor.initFOC();
69+
70+
// add target command T
71+
command.add('T', doTarget, "target voltage");
72+
73+
Serial.println(F("Motor ready."));
74+
75+
Serial.println(F("Set the target voltage using serial terminal:"));
76+
_delay(1000);
77+
}
78+
79+
void loop() {
80+
81+
motor.loopFOC();
82+
motor.move(target_voltage);
83+
command.run();
84+
motor.monitor();
85+
86+
}

examples/encoders/smoothing/smoothing.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/**
22
*
3-
* Hall sensor velocity motion control example, modified to demonstrate usage of SmoothingSensor
3+
* Hall sensor velocity motion control example, modified to demonstrate usage of SmoothingSensor.
4+
* The only changes are the declaration of the SmoothingSensor, passing it to motor.linkSensor
5+
* instead of the HallSensor instance, and the added Commander code to switch between the two.
6+
*
47
* Steps:
58
* 1) Configure the motor and sensor
69
* 2) Run the code
@@ -63,8 +66,6 @@ void setup() {
6366
sensor.enableInterrupts(doA, doB); //, doC);
6467
// software interrupts
6568
PciManager.registerListener(&listenerIndex);
66-
// set SmoothingSensor phase correction for hall sensors
67-
smooth.phase_correction = -_PI_6;
6869
// link the SmoothingSensor to the motor
6970
motor.linkSensor(&smooth);
7071

src/comms/stm32speeddir/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ The direction input is optional - if not provided, you can control the direction
2121

2222
The velocity values returned are in the range `min_speed` to `max_speed`, while the input PWM duty cycle should lie within the range `min_pwm` to `max_pwm`. Actual input values smaller than `min_pwm` will be treated as `min_pwm`, values larger than `max_pwm` will be treated as `max_pwm`. The behaviour for 100% or 0% duty cycles is undefined, and they should be avoided.
2323

24+
> **IMPORTANT**<br>
25+
> If the PWM frequency of the speed input is not known, provide its value in Hz to the constructor. If not provided, it will default to 1kHz (very common value). The frequency is used to make sure that the PWM period stays within one timer counter period. If this is not the case the timer counter can overflow and the input will not work correctly.
26+
27+
2428
## Usage
2529

2630
Use it like this:
@@ -31,8 +35,9 @@ Use it like this:
3135
// some example pins - the speed pin has to be on channel 1 or 2 of a timer
3236
#define PIN_SPEED PC6
3337
#define PIN_DIRECTION PB8
38+
#define PWM_FREQUENCY 1000 // 1kHz (defualt)
3439

35-
STM32SpeedDirInput speed_dir = STM32SpeedDirInput(PIN_SPEED, PIN_DIRECTION);
40+
STM32SpeedDirInput speed_dir = STM32SpeedDirInput(PIN_SPEED, PIN_DIRECTION, PWM_FREQUENCY);
3641

3742
float target = 0.0f;
3843

src/comms/stm32speeddir/STM32SpeedDirInput.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#if defined(_STM32_DEF_)
55

6-
STM32SpeedDirInput::STM32SpeedDirInput(int pin_speed, int pin_dir) : STM32PWMInput(pin_speed) {
6+
STM32SpeedDirInput::STM32SpeedDirInput(int pin_speed, int pin_dir, uint32_t pwm_freq) : STM32PWMInput(pin_speed, _pwm_freq) {
77
_pin_speed = pin_speed;
88
_pin_dir = pin_dir;
99
};

src/comms/stm32speeddir/STM32SpeedDirInput.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@
1010

1111
class STM32SpeedDirInput : public STM32PWMInput {
1212
public:
13-
STM32SpeedDirInput(int pin_speed, int pin_dir = NOT_SET);
13+
14+
/**
15+
* STM32SpeedDirInput constructor
16+
*
17+
* @param pin_speed - the pin number to read the speed PWM signal from
18+
* @param pin_dir - the pin number to read the direction signal from (default is NOT_SET)
19+
* @param pwm_freq - the frequency of the PWM signal (default is 1kHz)
20+
*
21+
* This class is used to read speed and direction signals from a pin using the STM32 HAL library.
22+
* IMPORTANT!
23+
* This class can only be used with the pins that are associated with some timer,
24+
* and only if they are associated to the channels 1 or 2. The timer can not be
25+
* used for other purposes, like motor control.
26+
*/
27+
STM32SpeedDirInput(int pin_speed, int pin_dir = NOT_SET, uint32_t pwm_freq = 1000);
1428
~STM32SpeedDirInput();
1529

1630
int init();

0 commit comments

Comments
 (0)