Hi people,
I am trying to run the velocity_motion_control magnetic_sensor example. The problem I am facing is that the motor is not moving continuously as it should. The motor is trying to hold an angle randomly. I want that the motor to move at a fixed velocity. Hardware used is -
MCU - stm32 f103
Encoder - AS5048A magnetic encoder with SPI
I printed the values received from the encoder and the sensor is working and giving correct values. The motor is working as well. But I still don’t get why my motor is trying to hold an angle (the motor either holds at angle 2.52 rad or 3.16 rad). Below is my code -
#include <SimpleFOC.h>
#include <Arduino.h>
// magnetic sensor instance - SPI
MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, PA15);
// BLDC motor & driver instance
BLDCMotor motor = BLDCMotor(7,7.5,469);
BLDCDriver3PWM driver = BLDCDriver3PWM(PA0,PA1,PB0);
// velocity set point variable
float target_velocity = 1;
HardwareSerial uart1 = HardwareSerial(PA10,PA9);
SPIClass encoder_spi = SPIClass(PB5,PB4,PB3);
void setup() {
// use monitoring with serial
uart1.begin(115200);
// enable more verbose output for debugging
// comment out if not needed
SimpleFOCDebug::enable(&uart1);
// initialise magnetic sensor hardware
sensor.init(&encoder_spi);
// link the motor to the sensor
motor.linkSensor(&sensor);
// driver config
// power supply voltage [V]
driver.voltage_power_supply = 12.0f;
driver.voltage_limit = 12.0f;
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);
// 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 = 1.0f;
motor.PID_velocity.I = 20;
motor.PID_velocity.D = 0;
// default voltage_power_supply
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
// default 5ms - try different values to see what is the best.
// the lower the less filtered
motor.LPF_velocity.Tf = 0.01f;
// comment out if not needed
motor.useMonitoring(uart1);
// initialize motor
motor.init();
// align sensor and start FOC
motor.initFOC();
uart1.println(F("Motor ready."));
_delay(1000);
}
void loop() {
// main FOC algorithm function
// the faster you run this function the better
// Arduino UNO loop ~1kHz
// Bluepill loop ~10kHz
motor.loopFOC();
// Motion control function
// velocity, position or voltage (defined in motor.controller)
// this function can be run at much lower frequency than loopFOC() function
// You can also use motor.move() and set the motor.target in the code
motor.move(2);
//uart1.println(sensor.getMechanicalAngle());
}
below is the output i get -
TIM2-CH1 TIM2-CH2 TIM8-CH2N score: -3
TIM2-CH1 TIM2-CH2 TIM1-CH2N score: -3
TIM2-CH1 TIM2-CH2 TIM3-CH3 score: 2
TIM2-CH1 TIM2-CH2 TIM3-CH3 score: 2
TIM2-CH1 TIM2-CH2 TIM8-CH2N score: -3
TIM2-CH1 TIM5-CH2 TIM1-CH2N score: -3
TIM2-CH1 TIM5-CH2 TIM3-CH3 score: 3
TIM2-CH1 TIM5-CH2 TIM3-CH3 score: 3
TIM2-CH1 TIM5-CH2 TIM8-CH2N score: -3
TIM5-CH1 TIM2-CH2 TIM1-CH2N score: -3
TIM5-CH1 TIM2-CH2 TIM3-CH3 score: 3
TIM5-CH1 TIM2-CH2 TIM3-CH3 score: 3
TIM5-CH1 TIM2-CH2 TIM8-CH2N score: -3
TIM5-CH1 TIM2-CH2 TIM1-CH2N score: -3
TIM5-CH1 TIM2-CH2 TIM3-CH3 score: 3
TIM5-CH1 TIM2-CH2 TIM3-CH3 score: 3
TIM5-CH1 TIM2-CH2 TIM8-CH2N score: -3
TIM5-CH1 TIM5-CH2 TIM1-CH2N score: -3
TIM5-CH1 TIM5-CH2 TIM3-CH3 score: 2
TIM5-CH1 TIM5-CH2 TIM3-CH3 score: 2
TIM5-CH1 TIM5-CH2 TIM8-CH2N score: -3
STM32-DRV: best: TIM2-CH1 TIM2-CH2 TIM3-CH3 score: 2
STM32-DRV: Initializing TIM2
STM32-DRV: Timer resolution set to: 1280
STM32-DRV: Configured TIM2_CH1
STM32-DRV: Configured TIM2_CH2
STM32-DRV: Initializing TIM3
STM32-DRV: Timer resolution set to: 1280
STM32-DRV: Configured TIM3_CH3
STM32-DRV: Synchronising 2 timers
STM32-DRV: master timer: TIM2
STM32-DRV: slave timer: TIM3
STM32-DRV: slave counter: 0
STM32-DRV: slave counter: 0
MOT:Monitor enabled!
MOT:Init
MOT:Enable driver.
MOT:Align sensor.
MOT:sensor dir: CW
WARN-MOT:PP check: fail - est. pp: 7.80
Skip dir calib.
MOT:Zero elec. angle: 1.00
ERR-MOT:No current sense.
MOT:Ready.
Motor ready.
Has anyone here faced the same issue ?
In open_loop, the MOSI pin isn't used. So this indicates to me, there is a problem with SPI and driver PWM pins. If the issue would be related to the problem I described in the other thread, setting
– o_lampePID_velocity.Ito zero would have solved the problem. In my tests, the motor never went back to a specific angle, but toggled around any cogging point.I verified all pins. PA1 = Tim2 ch2 ; PA0 = tim2 ch1 ; PB0 = tim3 ch3 ; PB5 = tim3 ch2 ; PB4 = tim3 ch1 ; PB3 = tim2 ch2. do you think that PB3 and PA1 are colliding? becuase if that was the case then i shouldnt have got the correct angle from the sensor or the motor should not be moving
– Akshat_Gupta