mirror of
https://github.com/modelec/MarcelMoteurSTM32.git
synced 2026-01-18 16:47:23 +01:00
fix: motors update function
This commit is contained in:
@@ -10,20 +10,16 @@ private:
|
||||
uint16_t targetSpeed;
|
||||
bool isAccelerating;
|
||||
bool isReversing;
|
||||
bool isTurningRightForward = false;
|
||||
bool isTurningLeftForward = false;
|
||||
bool isTurningRightBackward = false;
|
||||
bool isTurningLeftBackward = false;
|
||||
bool isTurningRight;
|
||||
bool isTurningLeft;
|
||||
|
||||
public:
|
||||
Motor(TIM_TypeDef *timer);
|
||||
void accelerer(int speed);
|
||||
void reculer(int speed);
|
||||
void stop();
|
||||
void tournerDroiteAvancer(int speed);
|
||||
void tournerGaucheAvancer(int speed);
|
||||
void tournerDroiteReculer(int speed);
|
||||
void tournerGaucheReculer(int speed);
|
||||
void tournerDroite(int speed);
|
||||
void tournerGauche(int speed);
|
||||
void stopTurning();
|
||||
void update();
|
||||
|
||||
|
||||
@@ -35,19 +35,13 @@ void Cpploop(Motor *motor) {
|
||||
motor->accelerer(300);
|
||||
break;
|
||||
case 1:
|
||||
motor->tournerGaucheAvancer(600);
|
||||
motor->tournerGauche(300);
|
||||
break;
|
||||
case 2:
|
||||
motor->tournerDroiteAvancer(600);
|
||||
motor->tournerDroite(300);
|
||||
break;
|
||||
case 3:
|
||||
motor->reculer(50);
|
||||
break;
|
||||
case 4:
|
||||
motor->tournerGaucheReculer(100);
|
||||
break;
|
||||
case 5:
|
||||
motor->tournerDroiteReculer(100);
|
||||
motor->reculer(300);
|
||||
break;
|
||||
default:
|
||||
motor->stop();
|
||||
|
||||
@@ -2,19 +2,21 @@
|
||||
|
||||
Motor::Motor(TIM_TypeDef *timer) :
|
||||
tim(timer), currentSpeed(0), targetSpeed(0), isAccelerating(false), isReversing(
|
||||
false), isTurningLeftBackward(false), isTurningLeftForward(false), isTurningRightBackward(false), isTurningRightForward(false) {
|
||||
false), isTurningRight(false), isTurningLeft(false) {
|
||||
}
|
||||
|
||||
void Motor::accelerer(int speed) {
|
||||
targetSpeed = (speed <= 626) ? speed : 626;
|
||||
isAccelerating = true;
|
||||
isReversing = false;
|
||||
this->stopTurning();
|
||||
}
|
||||
|
||||
void Motor::reculer(int speed) {
|
||||
targetSpeed = (speed <= 626) ? speed : 626;
|
||||
isReversing = true;
|
||||
isAccelerating = false;
|
||||
this->stopTurning();
|
||||
}
|
||||
|
||||
void Motor::stop() {
|
||||
@@ -24,94 +26,49 @@ void Motor::stop() {
|
||||
this->stopTurning();
|
||||
}
|
||||
|
||||
void Motor::tournerDroiteAvancer(int speed) {
|
||||
targetSpeed = speed;
|
||||
isTurningRightForward = true;
|
||||
isTurningLeftForward = false;
|
||||
isTurningRightBackward = false;
|
||||
isTurningLeftBackward = false;
|
||||
isAccelerating = true;
|
||||
void Motor::tournerDroite(int speed) {
|
||||
targetSpeed = (speed <= 626) ? speed : 626;
|
||||
isTurningRight = true;
|
||||
isTurningLeft = false;
|
||||
isAccelerating = true;
|
||||
}
|
||||
|
||||
void Motor::tournerGaucheAvancer(int speed) {
|
||||
targetSpeed = speed;
|
||||
isTurningLeftForward = true;
|
||||
isTurningRightForward = false;
|
||||
isTurningRightBackward = false;
|
||||
isTurningLeftBackward = false;
|
||||
isAccelerating = true;
|
||||
void Motor::tournerGauche(int speed) {
|
||||
targetSpeed = (speed <= 626) ? speed : 626;
|
||||
isTurningRight = false;
|
||||
isTurningLeft = true;
|
||||
isAccelerating = true;
|
||||
}
|
||||
|
||||
void Motor::tournerDroiteReculer(int speed) {
|
||||
targetSpeed = speed;
|
||||
isTurningRightBackward = true;
|
||||
isTurningLeftBackward = false;
|
||||
isTurningRightForward = false;
|
||||
isTurningLeftForward = false;
|
||||
isReversing = true;
|
||||
}
|
||||
|
||||
void Motor::tournerGaucheReculer(int speed) {
|
||||
targetSpeed = speed;
|
||||
isTurningLeftBackward = true;
|
||||
isTurningRightBackward = false;
|
||||
isTurningRightForward = false;
|
||||
isTurningLeftForward = false;
|
||||
isReversing = true;
|
||||
}
|
||||
|
||||
void Motor::stopTurning() {
|
||||
isTurningLeftForward = false;
|
||||
isTurningLeftBackward = false;
|
||||
isTurningRightBackward = false;
|
||||
isTurningRightForward = false;
|
||||
isTurningLeft = false;
|
||||
isTurningRight = false;
|
||||
}
|
||||
|
||||
void Motor::update() {
|
||||
// Gestion de l'accélération/décélération
|
||||
if (isAccelerating && currentSpeed < targetSpeed) {
|
||||
currentSpeed++;
|
||||
} else if (isReversing && currentSpeed > -targetSpeed) {
|
||||
currentSpeed--;
|
||||
} else if (!isAccelerating && !isReversing && currentSpeed > targetSpeed) {
|
||||
currentSpeed--;
|
||||
}
|
||||
// Gestion de l'accélération/décélération
|
||||
if ((isAccelerating || isReversing) && currentSpeed < targetSpeed) {
|
||||
currentSpeed++;
|
||||
} else if (!isAccelerating && !isReversing && currentSpeed > targetSpeed) {
|
||||
currentSpeed--;
|
||||
}
|
||||
|
||||
// Mise à jour des registres du timer pour contrôler les moteurs
|
||||
if (isTurningRightForward) {
|
||||
// Turn right while moving forward: Left motor full speed, Right motor slower
|
||||
tim->CCR2 = currentSpeed; // Left forward
|
||||
tim->CCR3 = currentSpeed / 2; // Right forward (slower)
|
||||
}
|
||||
else if (isTurningLeftForward) {
|
||||
// Turn left while moving forward: Right motor full speed, Left motor slower
|
||||
tim->CCR2 = currentSpeed / 2; // Left forward (slower)
|
||||
tim->CCR3 = currentSpeed; // Right forward
|
||||
}
|
||||
else if (isTurningRightBackward) {
|
||||
// Turn right while reversing: Left motor full speed, Right motor slower
|
||||
tim->CCR1 = currentSpeed; // Left backward
|
||||
tim->CCR4 = currentSpeed / 2; // Right backward (slower)
|
||||
}
|
||||
else if (isTurningLeftBackward) {
|
||||
// Turn left while reversing: Right motor full speed, Left motor slower
|
||||
tim->CCR1 = currentSpeed / 2; // Left backward (slower)
|
||||
tim->CCR4 = currentSpeed; // Right backward
|
||||
}
|
||||
else {
|
||||
// Normal forward/backward movement
|
||||
if (isAccelerating) {
|
||||
tim->CCR2 = currentSpeed;
|
||||
tim->CCR3 = currentSpeed;
|
||||
} else if (isReversing) {
|
||||
tim->CCR1 = currentSpeed;
|
||||
tim->CCR4 = currentSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
// Arrêt si vitesse cible atteinte
|
||||
if (currentSpeed == targetSpeed) {
|
||||
isAccelerating = false;
|
||||
isReversing = false;
|
||||
}
|
||||
// Mise à jour des registres du timer pour contrôler les moteurs
|
||||
if (isTurningRight) {
|
||||
// Turn right while moving forward: Left motor full speed, Right motor slower
|
||||
tim->CCR2 = currentSpeed; // Left forward
|
||||
tim->CCR3 = currentSpeed / 2; // Right forward (slower)
|
||||
} else if (isTurningLeft) {
|
||||
// Turn left while moving forward: Right motor full speed, Left motor slower
|
||||
tim->CCR2 = currentSpeed / 2; // Left forward (slower)
|
||||
tim->CCR3 = currentSpeed; // Right forward
|
||||
} else {
|
||||
// Normal forward/backward movement
|
||||
if (isAccelerating) {
|
||||
tim->CCR2 = currentSpeed;
|
||||
tim->CCR3 = currentSpeed;
|
||||
} else if (isReversing) {
|
||||
tim->CCR1 = currentSpeed;
|
||||
tim->CCR4 = currentSpeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user