From 743fcda00e3acd4ae4104a93dc40dc5cd429957a Mon Sep 17 00:00:00 2001 From: dd060606 Date: Mon, 10 Mar 2025 15:20:44 +0100 Subject: [PATCH 1/2] feat: add functions to turn right and left --- .settings/language.settings.xml | 4 +- Core/Inc/motors.h | 12 ++- Core/Src/functions.cpp | 58 +++++++++++---- Core/Src/main.cpp | 3 +- Core/Src/motors.cpp | 128 ++++++++++++++++++++++---------- 5 files changed, 145 insertions(+), 60 deletions(-) diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 1a04772..1856fbd 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 4096bab..45debbf 100644 --- a/Core/Inc/motors.h +++ b/Core/Inc/motors.h @@ -10,15 +10,23 @@ private: uint16_t targetSpeed; bool isAccelerating; bool isReversing; + bool isTurningRightForward = false; + bool isTurningLeftForward = false; + bool isTurningRightBackward = false; + bool isTurningLeftBackward = false; public: Motor(TIM_TypeDef *timer); void accelerer(int speed); void reculer(int speed); - void ralentir(); - void ralentirEnvers(); void stop(); + void tournerDroiteAvancer(int speed); + void tournerGaucheAvancer(int speed); + void tournerDroiteReculer(int speed); + void tournerGaucheReculer(int speed); + void stopTurning(); void update(); + }; diff --git a/Core/Src/functions.cpp b/Core/Src/functions.cpp index bc1b2b7..90bb494 100644 --- a/Core/Src/functions.cpp +++ b/Core/Src/functions.cpp @@ -4,35 +4,63 @@ #include "functions.h" #include "motors.h" - uint32_t lastTick = 0; // Variable pour mémoriser l'heure de la dernière action uint32_t delayTime = 0; // Variable pour définir le délai de la prochaine action +//Demo +uint32_t lastTickDemo = 0; +unsigned int demoStep = 0; + // Fonction pour vérifier si le délai est passé (permet de ne pas bloquer l'exécution) -bool isDelayPassed(uint32_t delay) { - if (HAL_GetTick() - lastTick >= delay) { - lastTick = HAL_GetTick(); // Met à jour lastTick - return true; - } - return false; +bool isDelayPassed(uint32_t delay, uint32_t *lastTick) { + if (HAL_GetTick() - *lastTick >= delay) { + *lastTick = HAL_GetTick(); // Met à jour lastTick + return true; + } + return false; } +void Cpploop(Motor *motor) { -void Cpploop(Motor *motor){ - - GPIOC->ODR ^= (1<<10); - + GPIOC->ODR ^= (1 << 10); //On actualise toute les 10ms et on effectue tous les controles périodiques - if(isDelayPassed(10)) { + if (isDelayPassed(10, &lastTick)) { + + //Demo + if (isDelayPassed(2000, &lastTickDemo)) { + + switch (demoStep) { + case 0: + motor->accelerer(300); + break; + case 1: + motor->tournerGaucheAvancer(600); + break; + case 2: + motor->tournerDroiteAvancer(600); + break; + case 3: + motor->reculer(50); + break; + case 4: + motor->tournerGaucheReculer(100); + break; + case 5: + motor->tournerDroiteReculer(100); + break; + default: + motor->stop(); + break; + } + demoStep++; + + } //On met à jour le statut des moteurs motor->update(); } - - } - #endif /* SRC_FUNCTIONS_C_ */ diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 8cc325c..96a61c2 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -102,8 +102,7 @@ int main(void) { Motor motor(TIM3); //On fait accélérer les moteurs - motor.accelerer(626); - + motor.accelerer(300); /* USER CODE END 2 */ /* Infinite loop */ diff --git a/Core/Src/motors.cpp b/Core/Src/motors.cpp index 84b4973..03e6bf0 100644 --- a/Core/Src/motors.cpp +++ b/Core/Src/motors.cpp @@ -2,7 +2,7 @@ Motor::Motor(TIM_TypeDef *timer) : tim(timer), currentSpeed(0), targetSpeed(0), isAccelerating(false), isReversing( - false) { + false), isTurningLeftBackward(false), isTurningLeftForward(false), isTurningRightBackward(false), isTurningRightForward(false) { } void Motor::accelerer(int speed) { @@ -17,51 +17,101 @@ void Motor::reculer(int speed) { isAccelerating = false; } -void Motor::ralentir() { - targetSpeed = 0; - isAccelerating = true; - isReversing = false; -} - -void Motor::ralentirEnvers() { - targetSpeed = 0; - isReversing = true; - isAccelerating = false; -} - void Motor::stop() { - currentSpeed = 0; targetSpeed = 0; isAccelerating = false; isReversing = false; + this->stopTurning(); +} + +void Motor::tournerDroiteAvancer(int speed) { + targetSpeed = speed; + isTurningRightForward = true; + isTurningLeftForward = false; + isTurningRightBackward = false; + isTurningLeftBackward = false; + isAccelerating = true; +} + +void Motor::tournerGaucheAvancer(int speed) { + targetSpeed = speed; + isTurningLeftForward = true; + isTurningRightForward = false; + isTurningRightBackward = false; + isTurningLeftBackward = false; + 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; } 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 && currentSpeed < targetSpeed) { + currentSpeed++; + } else if (isReversing && currentSpeed > -targetSpeed) { + currentSpeed--; + } else if (!isAccelerating && !isReversing && currentSpeed > targetSpeed) { + currentSpeed--; + } - // Mise à jour des registres du timer - if (isAccelerating) { - //2 et 3 avance - tim->CCR2 = currentSpeed; - tim->CCR3 = 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; + } + } - } else if (isReversing) { - // 1 et 4 recule - tim->CCR1 = currentSpeed; - tim->CCR4 = currentSpeed; - - } - - // Arrêt si vitesse cible atteinte - if (currentSpeed == targetSpeed) { - isAccelerating = false; - isReversing = false; - } + // Arrêt si vitesse cible atteinte + if (currentSpeed == targetSpeed) { + isAccelerating = false; + isReversing = false; + } } From 9df4e1efe39f49613a11a7f4d17f9b5313172def Mon Sep 17 00:00:00 2001 From: dd060606 Date: Tue, 11 Mar 2025 11:32:54 +0100 Subject: [PATCH 2/2] fix: motors update function --- .settings/language.settings.xml | 4 +- Core/Inc/motors.h | 12 ++-- Core/Src/functions.cpp | 12 +--- Core/Src/motors.cpp | 123 +++++++++++--------------------- 4 files changed, 49 insertions(+), 102 deletions(-) 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; + } + } }