Skip to content

Commit f185099

Browse files
committed
a good state of the calibraiton code
1 parent 189a54c commit f185099

File tree

6 files changed

+208
-460
lines changed

6 files changed

+208
-460
lines changed

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,88 @@
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[200] = {0};
12+
13+
// magnetic sensor instance - SPI
14+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 14);
15+
// Stepper motor & driver instance
16+
StepperMotor motor = StepperMotor(50);
17+
StepperDriver4PWM driver = StepperDriver4PWM(10, 9, 5, 6,8);
18+
// instantiate the calibrated sensor object
19+
// instantiate the calibrated sensor object
20+
// argument 1 - sensor object
21+
// argument 2 - number of samples in the LUT (default 200)
22+
// argument 3 - pointer to the LUT array (defualt nullptr)
23+
CalibratedSensor sensor_calibrated = CalibratedSensor(sensor, 200, calibrationLut);
24+
25+
// voltage set point variable
26+
float target_voltage = 2;
27+
28+
// instantiate the commander
29+
Commander command = Commander(Serial);
30+
31+
void doTarget(char* cmd) { command.scalar(&target_voltage, cmd); }
32+
33+
void setup() {
34+
35+
sensor.init();
36+
// Link motor to sensor
37+
motor.linkSensor(&sensor);
38+
// power supply voltage
39+
driver.voltage_power_supply = 20;
40+
driver.init();
41+
motor.linkDriver(&driver);
42+
// aligning voltage
43+
motor.voltage_sensor_align = 8;
44+
motor.voltage_limit = 20;
45+
// set motion control loop to be used
46+
motor.controller = MotionControlType::torque;
47+
48+
// use monitoring with serial
49+
Serial.begin(115200);
50+
// comment out if not needed
51+
motor.useMonitoring(Serial);
52+
motor.monitor_variables = _MON_VEL;
53+
motor.monitor_downsample = 10; // default 10
54+
55+
// initialize motor
56+
motor.init();
57+
58+
// Running calibration
59+
// as the Lookup table (LUT) has been provided as an argument this function will not do anything
60+
sensor_calibrated.calibrate(motor);
61+
62+
// Linking sensor to motor object
63+
motor.linkSensor(&sensor_calibrated);
64+
65+
// write the sensor direction and zero electrical angle outputed by the calibration
66+
motor.sensor_direction = Direction::CW; // replace with the value outputed by the calibration
67+
motor.zero_electric_angle = 0.0; // replace with the value outputed by the calibration
68+
69+
// calibrated init FOC
70+
motor.initFOC();
71+
72+
// add target command T
73+
command.add('T', doTarget, "target voltage");
74+
75+
Serial.println(F("Motor ready."));
76+
77+
Serial.println(F("Set the target voltage using serial terminal:"));
78+
_delay(1000);
79+
}
80+
81+
void loop() {
82+
83+
motor.loopFOC();
84+
motor.move(target_voltage);
85+
command.run();
86+
motor.monitor();
87+
88+
}

0 commit comments

Comments
 (0)