2018-05-06 08:14:51 +02:00
|
|
|
#include "motor.h"
|
2018-05-08 00:45:08 +02:00
|
|
|
#include "actionneurs.h"
|
2018-05-06 08:14:51 +02:00
|
|
|
|
2018-05-06 12:50:03 +02:00
|
|
|
uint8_t tensionToPWM(float V)
|
2018-05-06 08:14:51 +02:00
|
|
|
{
|
2018-05-06 12:50:03 +02:00
|
|
|
if (V >= PWM_MAX) {
|
2018-05-06 08:14:51 +02:00
|
|
|
return UINT8_MAX;
|
|
|
|
} else if (V <= 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2018-05-06 12:50:03 +02:00
|
|
|
return V * UINT8_MAX / PWM_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t moteurTensionToPWM(float V)
|
|
|
|
{
|
|
|
|
if (V >= MOTOR_CONTROLLER_ALIMENTATION) {
|
|
|
|
V = MOTOR_CONTROLLER_ALIMENTATION;
|
|
|
|
} else if (V <= 0) {
|
|
|
|
V = 0;
|
2018-05-06 08:14:51 +02:00
|
|
|
}
|
2018-05-06 12:50:03 +02:00
|
|
|
return tensionToPWM(V * MOTOR_CONTROLLER_REFERENCE / MOTOR_CONTROLLER_ALIMENTATION);
|
2018-05-06 08:14:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setMoteurTensionRaw(float lVolt, float rVolt, bool lFor, bool rFor)
|
|
|
|
{
|
2018-05-06 12:50:03 +02:00
|
|
|
|
|
|
|
#ifdef INVERSE_L_MOTOR
|
|
|
|
lFor = !lFor;
|
|
|
|
#endif
|
2018-05-06 08:14:51 +02:00
|
|
|
static struct C2FD_PWMs msg;
|
|
|
|
msg.in = 0x00;
|
|
|
|
if (lVolt > 0) {
|
|
|
|
msg.in |= 1 << (lFor ? IN1 : IN2);
|
|
|
|
msg.ena = moteurTensionToPWM(lVolt);
|
|
|
|
} else {
|
|
|
|
// Nothing needs to be changed for this motor controller
|
|
|
|
}
|
|
|
|
|
2018-05-06 12:50:03 +02:00
|
|
|
#ifdef INVERSE_R_MOTOR
|
|
|
|
rFor = !rFor;
|
|
|
|
#endif
|
2018-05-06 08:14:51 +02:00
|
|
|
if (rVolt > 0) {
|
|
|
|
msg.in |= 1 << (rFor ? IN3 : IN4);
|
2018-05-06 18:35:26 +02:00
|
|
|
msg.enb = moteurTensionToPWM(rVolt);
|
2018-05-06 08:14:51 +02:00
|
|
|
} else {
|
|
|
|
// Nothing needs to be changed for this motor controller
|
|
|
|
}
|
|
|
|
|
|
|
|
sendCF(C2FD_PWM, &msg, sizeof(struct C2FD_PWMs));
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMoteurTension(float lVolt, float rVolt)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Gauche
|
|
|
|
bool lFor = lVolt < 0;
|
|
|
|
lVolt = fabs(lVolt);
|
|
|
|
if (lVolt < MOTOR_SATURATION_MIN) {
|
|
|
|
lVolt = 0;
|
|
|
|
} else if (lVolt > MOTOR_SATURATION_MAX) {
|
|
|
|
lVolt = MOTOR_SATURATION_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Droite
|
|
|
|
bool rFor = rVolt < 0;
|
|
|
|
rVolt = fabs(rVolt);
|
|
|
|
if (rVolt < MOTOR_SATURATION_MIN) {
|
|
|
|
rVolt = 0;
|
|
|
|
} else if (rVolt > MOTOR_SATURATION_MAX) {
|
|
|
|
rVolt = MOTOR_SATURATION_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
setMoteurTensionRaw(lVolt, rVolt, lFor, rFor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPWMTension(float lVolt, float rVolt)
|
|
|
|
{
|
|
|
|
setMoteurTension(
|
|
|
|
lVolt * MOTOR_CONTROLLER_ALIMENTATION / MOTOR_CONTROLLER_REFERENCE,
|
|
|
|
rVolt * MOTOR_CONTROLLER_ALIMENTATION / MOTOR_CONTROLLER_REFERENCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct C2FD_PWMs msgBrake = { 0, 0, 0x00 };
|
|
|
|
static struct C2FD_PWMs msgFree = { 0, 0, (1 << IN1) | (1 << IN2) | (1 << IN3) | (1 << IN4) };
|
|
|
|
|
|
|
|
int brake()
|
|
|
|
{
|
|
|
|
sendCF(C2FD_PWM, &msgBrake, sizeof(struct C2FD_PWMs));
|
|
|
|
}
|
|
|
|
|
|
|
|
int freewheel()
|
|
|
|
{
|
|
|
|
sendCF(C2FD_PWM, &msgFree, sizeof(struct C2FD_PWMs));
|
|
|
|
}
|
|
|
|
|
|
|
|
int stop()
|
|
|
|
{
|
|
|
|
brake();
|
2018-05-08 00:45:08 +02:00
|
|
|
stopActionneurs();
|
2018-05-06 08:14:51 +02:00
|
|
|
}
|