@runger thank you for your trapezoidal code.
EDIT: I found the culprit - I had forgotten to link the motor to the planner:
trapezoidal.linkMotor(motor);
Read on if you want to see the rest of the code. I think I definitely have enough of a proof of concept to order the driver boards.
I tried it today but have had some issues. I would be very grateful if you could help me work out what is going on.
First, a little bit about my setup. I’m using what I have on-hand, so the setup isn’t ideal. I’ve got the 5010 motor hooked up with an AS5600 connected to a SimpleFOC Mini driver board, all controlled with an ESP32. I do have to carefully set the power supply voltage to the SimpleFOC as I realise the phase resistance on that motor is probably too low and it does trip if I set it a bit high.
I have used example velocity mode successfully from the SimpleFOC library. But I’m having trouble when I add the trapezoid parts. I know the code example in the repo is incomplete so I have done my best to merge / construct a full application:
/**
ESP32 position motion control with magnetic sensor and trapezoidal profile
*/
#include <SimpleFOC.h>
#include <SimpleFOCDrivers.h>
#include <utilities\trapezoids\TrapezoidalPlanner.h>
// I2C Magnetic sensor instance (AS5600)
// make sure to use the pull-ups and connect DIR pin to GND or VCC!!
// SDA 21
// SCL 22
// magnetic sensor instance - I2C
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
// instantiate the commander
Commander command = Commander(Serial);
// Motor instance with 12N 14P(7 pairs)
BLDCMotor motor = BLDCMotor(7);
TrapezoidalPlanner trapezoidal = TrapezoidalPlanner(5.0f, 1.0f, 0.25f, 0.2f);
BLDCDriver3PWM driver = BLDCDriver3PWM(25, 26, 27, 14);
// angle set point variable
float target_angle = 0.0f;
void onTarget(char* cmd) {
command.scalar(&target_angle, cmd);
trapezoidal.setTarget(target_angle);
}
void setup()
{
// use monitoring with serial
Serial.begin(115200);
// enable more verbose output for debugging
// comment out if not needed
SimpleFOCDebug::enable(&Serial);
// initialise magnetic sensor hardware
sensor.init();
// link the motor to the sensor
motor.linkSensor(&sensor);
// driver config
// power supply voltage [V]
driver.voltage_power_supply = 12;
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);
// choose FOC modulation (optional)
//motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// set motion control loop to be used
motor.controller = MotionControlType::velocity;
// contoller configuration
// default parameters in defaults.h
// velocity PI controller parameters
motor.PID_velocity.P = 0.2f;
motor.PID_velocity.I = 20;
motor.PID_velocity.D = 0;
// maximal voltage to be set to the motor
motor.voltage_limit = 6;
// jerk control using voltage voltage ramp
// default value is 300 volts per sec ~ 0.3V per millisecond
motor.PID_velocity.output_ramp = 1000;
// velocity low pass filtering time constant
// the lower the less filtered
motor.LPF_velocity.Tf = 0.01f;
// comment out if not needed
motor.useMonitoring(Serial);
// initialize motor
motor.init();
// align sensor and start FOC
motor.initFOC();
command.add('T', onTarget, "target angle");
//while(1){} // block
trapezoidal.setTarget(target_angle);
// _delay(1000);
//while(1){} // block
}
int32_t downsample = 50; // depending on your MCU's speed, you might want a value between 5 and 50...
int32_t downsample_cnt = 0;
void loop() {
if (downsample > 0 && --downsample_cnt <= 0) {
motor.target = trapezoidal.run();
downsample_cnt = downsample;
}
motor.move();
motor.loopFOC();
command.run();
}
When the code runs trapezoidal.setTarget(target_angle)
the ESP32 resets repeatedly. I had a compilation failure with the exact line
void onTarget(char* cmd){ command.scalar(&target_angle); trapezoidal.setTarget(target_angle); }
so I changed this to
void onTarget(char* cmd){ command.scalar(&target_angle,cmd); trapezoidal.setTarget(target_angle); }
I realise this could be the cause of the issue but I can’t seem to compile without changing command.scalar(&target_angle)
.
Any ideas would be gratefully received.