Velocity motion control with magnetic sensor example not working

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 ?

6 Answers

6

Not sure, what the default for motor.voltage_align is, but it wouldn’t hurt to increase it to 2-5V until this error message disappeares

I increased motor.voltage_sensor_align to 5V. It was 3V earlier. The pole pair check passed but behaviour remained the same. The motor is just coming back to 2.52 rad. If I move it then it comes back to 2.52 rad with the speed I specified in the move() function.

MOT:Monitor enabled!
MOT:Init
MOT:Enable driver.
MOT:Align sensor.
MOT:sensor dir: CW
MOT:PP check: OK!
Skip dir calib.
MOT:Zero elec. angle: 5.74
ERR-MOT:No current sense.
MOT:Ready.
Motor ready.

I think it’s related to this Estimated_current and velocity mode: high current at standstill - #5 by dekutree64

The PID’s integral term never decays, so when you forcibly rotate the motor with velocity target 0, the non-zero velocity “winds up” and then unwinds back to where it was.

hi @dekutree64
I made the motor.PID_velocity.I = 0; term 0. But that did not help. The motor still comes back to 2.52 rad.
I even tried changing the motor.PID_velocity.output_ramp and motor.LPF_velocity.Tf but the motor shows no change in behaviour.

Maybe I’m seeing things, but MOSI (PB5) and the second driver PWM pin PA1 are on the same timer (T2>C2).
Why did you choose these pins (PA0,PA1,PB0) for driver-PWM? Usually Timer1 is chosen for driver-PWM.

hey @o_lampe
These same pin settings are working in openloop velocity mode. I printed the sensor values and moved the motor in openloop velocity mode. I tried printing the electrical angle and that was also correct.
I think the thread shared by @dekutree64 is the bug I am facing.

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 PID_velocity.I to 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