Skip to content

Extension of the CalibratedSensor #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ jobs:
- arduino:mbed_rp2040:pico # rpi pico
include:
- arduino-boards-fqbn: arduino:avr:nano
sketches-exclude: calibrated mt6816_spi smoothing simplefocnano_torque_voltage
sketches-exclude: calibrated calibration_save mt6816_spi smoothing simplefocnano_torque_voltage
required-libraries: Simple FOC
- arduino-boards-fqbn: arduino:sam:arduino_due_x
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega
sketches-exclude: calibrated calibration_save smoothing simplefocnano_torque_voltage simplefocnano_atmega
- arduino-boards-fqbn: arduino:samd:nano_33_iot
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing
sketches-exclude: calibrated calibration_save smoothing
- arduino-boards-fqbn: arduino:mbed_rp2040:pico
required-libraries: Simple FOC
sketches-exclude: calibrated smoothing simplefocnano_torque_voltage simplefocnano_atmega
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/**
* Torque control example using voltage control loop.
*
* Most of the low-end BLDC driver boards doesn't have current measurement therefore SimpleFOC offers
* you a way to control motor torque by setting the voltage to the motor instead hte current.
*
* This makes the BLDC motor effectively a DC motor, and you can use it in a same way.
* The example demonstrates the usage of the calibrated sensor object.
*/
#include <SimpleFOC.h>
#include <SimpleFOCDrivers.h>
Expand All @@ -16,6 +11,8 @@ MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, PB6);
BLDCMotor motor = BLDCMotor(11);
BLDCDriver3PWM driver = BLDCDriver3PWM(PB4,PC7,PB10,PA9);
// instantiate the calibrated sensor object
// argument 1 - sensor object
// argument 2 - number of samples in the LUT (default 200)
CalibratedSensor sensor_calibrated = CalibratedSensor(sensor);

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

//Serial.println("Calibrating Sensor Done.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* The example demonstrates the calibration of the magnetic sensor with the calibration procedure and saving the calibration data.
* So that the calibration procedure does not have to be run every time the motor is powered up.
*/

#include <SimpleFOC.h>
#include <SimpleFOCDrivers.h>
#include "encoders/calibrated/CalibratedSensor.h"

// fill this array with the calibration values outputed by the calibration procedure
float calibrationLut[50] = {0};
float zero_electric_angle = 0;
Direction sensor_direction = Direction::CW;

// magnetic sensor instance - SPI
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 14);
// Stepper motor & driver instance
StepperMotor motor = StepperMotor(50);
StepperDriver4PWM driver = StepperDriver4PWM(10, 9, 5, 6,8);
// instantiate the calibrated sensor object
// instantiate the calibrated sensor object
// argument 1 - sensor object
// argument 2 - number of samples in the LUT (default 200)
// argument 3 - pointer to the LUT array (defualt nullptr)
CalibratedSensor sensor_calibrated = CalibratedSensor(sensor, 50);

// voltage set point variable
float target_voltage = 2;

// instantiate the commander
Commander command = Commander(Serial);

void doTarget(char* cmd) { command.scalar(&target_voltage, cmd); }

void setup() {

sensor.init();
// Link motor to sensor
motor.linkSensor(&sensor);
// power supply voltage
driver.voltage_power_supply = 20;
driver.init();
motor.linkDriver(&driver);
// aligning voltage
motor.voltage_sensor_align = 8;
motor.voltage_limit = 20;
// set motion control loop to be used
motor.controller = MotionControlType::torque;

// use monitoring with serial
Serial.begin(115200);
// comment out if not needed
motor.useMonitoring(Serial);
motor.monitor_variables = _MON_VEL;
motor.monitor_downsample = 10; // default 10

// initialize motor
motor.init();

// Running calibration
// the function will setup everything for the provided calibration LUT
sensor_calibrated.calibrate(motor, calibrationLut, zero_electric_angle, sensor_direction);

// Linking sensor to motor object
motor.linkSensor(&sensor_calibrated);

// calibrated init FOC
motor.initFOC();

// add target command T
command.add('T', doTarget, "target voltage");

Serial.println(F("Motor ready."));

Serial.println(F("Set the target voltage using serial terminal:"));
_delay(1000);
}

void loop() {

motor.loopFOC();
motor.move(target_voltage);
command.run();
motor.monitor();

}
Loading