mirror of
https://github.com/modelec/MarcelMoteurSTM32.git
synced 2026-01-18 16:47:23 +01:00
function to update motors status each 10ms + add random obstacle detection for testing
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-539313789031368032" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="1663823120161288977" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
@@ -16,7 +16,7 @@
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="-539313789031368032" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<provider class="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" console="false" env-hash="1663823120161288977" id="com.st.stm32cube.ide.mcu.toolchain.armnone.setup.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="MCU ARM GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
|
||||
@@ -4,17 +4,21 @@
|
||||
#include "stm32l0xx_hal.h"
|
||||
|
||||
class Motor {
|
||||
private:
|
||||
TIM_TypeDef *tim;
|
||||
uint16_t currentSpeed;
|
||||
uint16_t targetSpeed;
|
||||
bool isAccelerating;
|
||||
bool isReversing;
|
||||
|
||||
public:
|
||||
Motor(TIM_TypeDef *timer);
|
||||
|
||||
void accelerer(int speed);
|
||||
void reculer(int speed);
|
||||
void ralentir();
|
||||
void ralentirEnvers();
|
||||
void stop();
|
||||
|
||||
private:
|
||||
TIM_TypeDef *tim;
|
||||
void update();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef SRC_FUNCTIONS_C_
|
||||
#define SRC_FUNCTIONS_C_
|
||||
|
||||
#include <cstdlib> // Pour rand()
|
||||
#include <ctime> // Pour time()
|
||||
#include "functions.h"
|
||||
#include "motors.h"
|
||||
|
||||
@@ -8,7 +10,7 @@
|
||||
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
|
||||
|
||||
// Fonction pour vérifier si le délai est passé (permet de ne pas bloquer l'exécution
|
||||
// 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
|
||||
@@ -19,22 +21,40 @@ bool isDelayPassed(uint32_t delay) {
|
||||
|
||||
|
||||
void Cppmain(){
|
||||
|
||||
GPIOC->ODR ^= (1<<10);
|
||||
|
||||
//Simulation de détection d'obstacle
|
||||
static bool obstacleDetecte = false;
|
||||
static uint32_t tempsDebutObstacle = 0;
|
||||
|
||||
Motor motor(TIM3);
|
||||
|
||||
motor.accelerer(626);
|
||||
//On utilise pour les tests un blocage
|
||||
while(!isDelayPassed(2000)) {
|
||||
|
||||
//On actualise toute les 10ms
|
||||
if(isDelayPassed(10)) {
|
||||
// Simulation aléatoire de détection d'obstacle
|
||||
if (!obstacleDetecte && (rand() % 100 < 5)) { // 5% de chance de détecter un obstacle
|
||||
obstacleDetecte = true;
|
||||
tempsDebutObstacle = HAL_GetTick(); // Récupération du temps actuel
|
||||
//On arrête les moteurs
|
||||
motor.stop();
|
||||
}
|
||||
// Vérifier si la détection dure depuis 3 secondes
|
||||
if (obstacleDetecte) {
|
||||
if (HAL_GetTick() - tempsDebutObstacle >= 3000) {
|
||||
//Au bout de 3 secondes, on imagine que l'obstacle est parti pour les tests
|
||||
//On redémarre les moteurs
|
||||
obstacleDetecte = false;
|
||||
motor.accelerer(626);
|
||||
}
|
||||
}
|
||||
//On met à jour le statut des moteurs
|
||||
motor.update();
|
||||
}
|
||||
motor.ralentir();
|
||||
while(!isDelayPassed(500)) {
|
||||
|
||||
}
|
||||
//testChannels();
|
||||
|
||||
//GPIOC->ODR ^= (1<<10);
|
||||
//HAL_Delay(1000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
#include "functions.h"
|
||||
#include <cstdlib> // Pour rand() et srand()
|
||||
#include <ctime> // Pour time()
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
@@ -98,6 +100,9 @@ int main(void)
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
|
||||
|
||||
srand(HAL_GetTick());
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
|
||||
@@ -1,54 +1,66 @@
|
||||
#include <motors.h>
|
||||
|
||||
Motor::Motor(TIM_TypeDef *timer) :
|
||||
tim(timer) {
|
||||
tim(timer), currentSpeed(0), targetSpeed(0), isAccelerating(false), isReversing(
|
||||
false) {
|
||||
}
|
||||
|
||||
void Motor::accelerer(int speed) {
|
||||
tim->CCR1 = 0;
|
||||
tim->CCR2 = 0;
|
||||
uint16_t limit = (speed <= 626) ? speed : 626;
|
||||
for (uint16_t i = 0; i < limit; i++) {
|
||||
tim->CCR4 = i;
|
||||
tim->CCR3 = i;
|
||||
HAL_Delay(5);
|
||||
}
|
||||
targetSpeed = (speed <= 626) ? speed : 626;
|
||||
isAccelerating = true;
|
||||
isReversing = false;
|
||||
}
|
||||
|
||||
void Motor::reculer(int speed) {
|
||||
tim->CCR3 = 0;
|
||||
tim->CCR4 = 0;
|
||||
uint16_t limit = (speed <= 626) ? speed : 626;
|
||||
for (uint16_t i = 0; i < limit; i++) {
|
||||
tim->CCR1 = i;
|
||||
tim->CCR2 = i;
|
||||
HAL_Delay(5);
|
||||
}
|
||||
targetSpeed = (speed <= 626) ? speed : 626;
|
||||
isReversing = true;
|
||||
isAccelerating = false;
|
||||
}
|
||||
|
||||
void Motor::ralentir() {
|
||||
tim->CCR1 = 0;
|
||||
tim->CCR2 = 0;
|
||||
for (uint16_t i = 626; i > 0; i--) {
|
||||
tim->CCR4 = i;
|
||||
tim->CCR3 = i;
|
||||
HAL_Delay(1);
|
||||
}
|
||||
targetSpeed = 0;
|
||||
isAccelerating = true;
|
||||
isReversing = false;
|
||||
}
|
||||
|
||||
void Motor::ralentirEnvers() {
|
||||
tim->CCR3 = 0;
|
||||
tim->CCR4 = 0;
|
||||
for (uint16_t i = 626; i > 0; i--) {
|
||||
tim->CCR1 = i;
|
||||
tim->CCR2 = i;
|
||||
HAL_Delay(1);
|
||||
}
|
||||
targetSpeed = 0;
|
||||
isReversing = true;
|
||||
isAccelerating = false;
|
||||
}
|
||||
|
||||
void Motor::stop() {
|
||||
TIM3->CCR1 = 0;
|
||||
TIM3->CCR2 = 0;
|
||||
TIM3->CCR4 = 0;
|
||||
TIM3->CCR3 = 0;
|
||||
tim->CCR1 = 0;
|
||||
tim->CCR2 = 0;
|
||||
tim->CCR3 = 0;
|
||||
tim->CCR4 = 0;
|
||||
currentSpeed = 0;
|
||||
targetSpeed = 0;
|
||||
isAccelerating = false;
|
||||
isReversing = 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--;
|
||||
}
|
||||
|
||||
// Mise à jour des registres du timer
|
||||
if (isAccelerating) {
|
||||
tim->CCR4 = currentSpeed;
|
||||
tim->CCR3 = currentSpeed;
|
||||
} else if (isReversing) {
|
||||
tim->CCR1 = currentSpeed;
|
||||
tim->CCR2 = currentSpeed;
|
||||
}
|
||||
// Arrêt si vitesse cible atteinte
|
||||
if (currentSpeed == targetSpeed) {
|
||||
isAccelerating = false;
|
||||
isReversing = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user