Skip to main content
Associate
January 4, 2024
Solved

Stm32G431B-ESC-1 motor direction with pwm

  • January 4, 2024
  • 1 reply
  • 2436 views

Hi, I have managed to get my ipm motor running with hall sensors. I can run the motor with motor pilot no problem. I can run the motor down to zero rpm and in reverse direction. I have it running with a pwm signal generated on an arduino in forward rotation. The code generated by MC Workbench only allows for forward rotation. Can anyone tell if it is actually possible to get the motor to run in forward and reverse with a pwm signal.

Best answer by TRowe

Ok I managed to get it working. I couldn’t manage to get it working properly by defining a mid point in the pwm though I’m sure it can be done. I ended up defining new values for Ton_nmin and Ton_nmax and Ton-ndelta and creating a new case for ESC_NEGATIVE_RUN.  A bit of messing around but I can now control the motor from min to max speed both pos and neg and stop the motor. Seems to be working as I hoped it would. Thanks

1 reply

cedric H
Technical Moderator
January 5, 2024

Hello @TRowe ,

As you mentioned, the library allows to spin the motor in both direction. It is just a question of the sign of the target speed (which is done by the MC Pilot).

The PWM input command is managed by a state machine in esc.c file and is a bit different.

The target speed in ESC_POSITIVE_RUN is computed as follow :

         new_speed = ((pHandle->Ton_value-pESC_params->Ton_min) * (pESC_params->speed_max_valueRPM - pESC_params->speed_min_valueRPM) / pESC_params->delta_Ton_max) + pESC_params->speed_min_valueRPM;  
with Ton_value computed from PWM input. 
It will be indeed always positive.
The esc state machine is designed to avoid any injury with a drone propeller for instance. It is why we implemented the ESC_ARMING state to guarantee that the motor can not start full speed directly out of reset, even if the flight controller send the max duty cycle.
I do not know what your target application is, but if you want to get rid of the security aspects of the state machine, it is quite easy to set a PWM Ton_0Speed_value targeting 0 speed, and sending a negative speed for all values between 0 and Ton_0Speed_value and positive value for all values between Ton_0Speed_value and Ton_max. But be very careful, without anything else, your motor will run Max speed in reverse direction if there is no PWM signal...(which will be probably the case after a power up)
Hope it helps.
Regards
Cedric
TRoweAuthor
Associate
January 10, 2024

Hi Cedric,

Thank you for the reply, I have been working on trying to implement the changes you have suggested but im not having much luck. At this point i have updated some of the esc.c file in the armed case and the esc positive run case but my motor still only operates in forward rotation exactly as it did before i made the changes. i have added this to the definintions.

#define Ton_0Speed_value 1460

#define PWM_MIN 1060

#define PWM_MAX 1860

#define PWM_RANGE (PWM_MAX - PWM_MIN)

I have added this to the armed case

if (pHandle->Ton_value >= pESC_params->Ton_min)

{

// Determine the direction based on the zero RPM reference (Ton_0Speed_value)

if (pHandle->Ton_value <= Ton_0Speed_value)

{

// Calculate speed for reverse rotation

new_speed = ((Ton_0Speed_value - pHandle->Ton_value) * (pESC_params->speed_min_valueRPM) / (Ton_0Speed_value - pESC_params->Ton_min));

}

else

{

// Calculate speed for forward rotation

new_speed = ((pHandle->Ton_value - Ton_0Speed_value) * (pESC_params->speed_max_valueRPM) / (pESC_params->Ton_max - Ton_0Speed_value));

}

/* Next state */

/* This command sets what will be the first speed ramp after the

I have added this to the esc positive run case

if (pHandle->Ton_value <= Ton_0Speed_value)

{

// Calculate speed for reverse rotation

new_speed = ((Ton_0Speed_value - pHandle->Ton_value) * (pESC_params->speed_min_valueRPM) / (Ton_0Speed_value - pESC_params->Ton_min));

}

else

{

// Calculate speed for forward rotation

new_speed = ((pHandle->Ton_value - Ton_0Speed_value) * (pESC_params->speed_max_valueRPM) / (pESC_params->Ton_max - Ton_0Speed_value));

}

if (MC_GetSTMStateMotor1() == RUN

I have not disabled any of the security as you mentioned in your previous reply, is it a requirement for

introducing negative rotation? if not ill probably just leave it alone. Could you please provide me

some more information as I have tried based on your previous reply but i cant get it working.

Thanks, Tony

TRoweAuthorBest answer
Associate
January 13, 2024

Ok I managed to get it working. I couldn’t manage to get it working properly by defining a mid point in the pwm though I’m sure it can be done. I ended up defining new values for Ton_nmin and Ton_nmax and Ton-ndelta and creating a new case for ESC_NEGATIVE_RUN.  A bit of messing around but I can now control the motor from min to max speed both pos and neg and stop the motor. Seems to be working as I hoped it would. Thanks