diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 1856fbd..1a04772 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/Core/Inc/motors.h b/Core/Inc/motors.h index 45debbf..2283036 100644 --- a/Core/Inc/motors.h +++ b/Core/Inc/motors.h @@ -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(); diff --git a/Core/Src/functions.cpp b/Core/Src/functions.cpp index 90bb494..8f75e73 100644 --- a/Core/Src/functions.cpp +++ b/Core/Src/functions.cpp @@ -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(); diff --git a/Core/Src/motors.cpp b/Core/Src/motors.cpp index 03e6bf0..3293360 100644 --- a/Core/Src/motors.cpp +++ b/Core/Src/motors.cpp @@ -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; + } + } }