projet_voilier/Services/DCMotor.c
2020-11-16 19:45:46 +01:00

36 lines
1.1 KiB
C

#include "DCMotor.h"
#include "math.h"
const int MOTOR_PWM_FREQ = 50;
void DCMotor_conf(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpioPwm, int pinPwm, GPIO_TypeDef * gpioDirection, int pinDirection)
{
// On regle la vitesse en valeur absolue, ici a 0
Timer_pwmo_conf(timer, channel, MOTOR_PWM_FREQ, 0);
// Configuration du GPIO
GPIO_conf(gpioPwm, pinPwm, LL_GPIO_MODE_ALTERNATE, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_UP);
// On regle le sens du moteur
GPIO_conf(gpioDirection, pinDirection, LL_GPIO_MODE_OUTPUT, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_DOWN);
GPIO_setPin(gpioDirection, pinDirection, 0);
Timer_start(timer);
}
void DCMotor_setSpeed(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin, float speed)
{
const int dir = speed > 0.0;
Timer_pwmo_setDutyCycle(timer, channel, fabs(speed));
GPIO_setPin(gpio, pin, dir);
}
float DCMotor_getSpeed(TIM_TypeDef * timer, int channel, GPIO_TypeDef * gpio, int pin)
{
const float speedAbs = Timer_pwmo_getDutyCycle(timer, channel);
const int dir = GPIO_readPin(gpio, pin);
return dir ? speedAbs : -speedAbs;
}