I’ve had basically the same set of baffling problems with 3 different hardware setups.
I’ve tried the lepton, a bluepill with a darlington output stage (with some buffers and using 5 volts as the motor input, just to try to work on the code), and the b-g431b-ESC1 board.
I’ve tried two different AS5600 encoders.
I recall that I was able to get very similar sketches working with the uno+shield. I used the same sensor. Same motor. The main difference appears to be the 6 pin pwm vs 3 pin.
What happens is that, first of all the pole pair checking is erratic. This seems to have been partly caused by slightly less than ideal pull up resistors in some cases, but I think that was just one of several things that was wrong. I have been able to stabilize the pole pair check results to within about 5 degrees sensor angle each time, however it’s never actually 7 poles. It thinks there are 10 poles with the pole pair check sketch, and bafflingly, a whole range from 4.5 to 9 poles when I try to run the full commander example sketch.
Example sketches are suitably adapted to my sensor and 6 pin pinout, and otherwise minimally changed.
My goal is to get it working in torque control mode so I can see how it sounds and how fast I can go with it, I need about 3000 rpm max, and it is unclear if that is possible with the lepton or even the ESC board. Obviously I would rather use the lepton as the ESC board actually is very expensive to make, ST sells them below cost as dev boards, so using them in a product is pretty sketch and I would rather minimize my engagement with motor driver design as it’s not something I can economically undertake.
When I tell it the motor is 8 poles manually, it will sometimes spin if I give it a boost with my hand. This is a fan actually not a bare motor. Otherwise it will sit there and oscillate. If I reduce the voltage it stops oscillating.
I have adjusted the gap between the magnet and sensor within a fair range and that seems unlikely to be a cause. Apparently these sensors are not that flexible, they are supposed to have a magnetic field exposure of between 30 and 70 mT, which is not that wide a range. I sort of figured if a compass can work these things should work no problem. Maybe not.
However nothing I can see in the hardware can explain the erratic results of the commander pole pair check.
I’m going to experiment with an AS5048B (I2C sensor) I bought from digikey tomorrow but am not optimistic it will lead to anything better.
Here is the commander example I used. I posted all the recent code I used with the ESC in the forum entitled "“b-G431B-ESC1…”
Angle sensor appears to work, open loop works. Should work, right?
/**
* Comprehensive BLDC motor control example using magnetic sensor
*
* Using serial terminal user can send motor commands and configure the motor and FOC in real-time:
* - configure PID controller constants
* - change motion control loops
* - monitor motor variabels
* - set target values
* - check all the configuration values
*
* See more info in docs.simplefoc.com/commander_interface
*/
#include <SimpleFOC.h>
// magnetic sensor instance - SPI
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);
// magnetic sensor instance - MagneticSensorI2C
//MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
// magnetic sensor instance - analog output
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
// BLDC motor & driver instance
BLDCMotor motor = BLDCMotor(7);
BLDCDriver6PWM driver = BLDCDriver6PWM(A_PHASE_UH, A_PHASE_UL, A_PHASE_VH, A_PHASE_VL, A_PHASE_WH, A_PHASE_WL);
// Stepper motor & driver instance
//StepperMotor motor = StepperMotor(50);
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
// commander interface
Commander command = Commander(Serial);
void onMotor(char* cmd){ command.motor(&motor, cmd); }
void setup() {
// 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 driver
motor.linkDriver(&driver);
// choose FOC modulation
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// set control loop type to be used
motor.controller = MotionControlType::torque;
// contoller configuration based on the control type
motor.PID_velocity.P = 0.2f;
motor.PID_velocity.I = 20;
motor.PID_velocity.D = 0;
// default voltage_power_supply
motor.voltage_limit = 4;
// velocity low pass filtering time constant
motor.LPF_velocity.Tf = 0.01f;
// angle loop controller
motor.P_angle.P = 20;
// angle loop velocity limit
motor.velocity_limit = 50;
// use monitoring with serial for motor init
// monitoring port
Serial.begin(38400);
// comment out if not needed
motor.useMonitoring(Serial);
// initialise motor
motor.init();
// align encoder and start FOC
motor.initFOC();
// set the inital target value
motor.target = 2;
// define the motor id
command.add('A', onMotor, "motor");
// Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 2V."));
_delay(1000);
}
void loop() {
// iterative setting FOC phase voltage
motor.loopFOC();
// iterative function setting the outter loop target
// velocity, position or voltage
// if tatget not set in parameter uses motor.target variable
motor.move();
// user communication
command.run();
}