Skip to content

Commit 59fc94e

Browse files
Merge pull request #54 from simplefoc/feat_stepper_calib
Extension of the CalibratedSensor
2 parents 6d96569 + b4a3262 commit 59fc94e

File tree

6 files changed

+421
-235
lines changed

6 files changed

+421
-235
lines changed

.github/workflows/ccpp.yml

+3-3
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

+4-6
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.");
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+
}

0 commit comments

Comments
 (0)