mirror of
https://github.com/modelec/MarcelMoteurSTM32.git
synced 2026-01-18 16:47:23 +01:00
changements sur les moteurs
This commit is contained in:
@@ -10,12 +10,18 @@ private:
|
||||
uint16_t targetSpeed;
|
||||
bool isAccelerating;
|
||||
bool isReversing;
|
||||
bool isTurningRight;
|
||||
bool isTurningLeft;
|
||||
|
||||
public:
|
||||
Motor(TIM_TypeDef *timer);
|
||||
void accelerer(int speed);
|
||||
void reculer(int speed);
|
||||
void stop();
|
||||
void stopTurning();
|
||||
bool isStopped();
|
||||
void tournerDroite(int speed);
|
||||
void tournerGauche(int speed);
|
||||
void update();
|
||||
};
|
||||
|
||||
|
||||
@@ -2,32 +2,53 @@
|
||||
|
||||
Motor::Motor(TIM_TypeDef *timer) :
|
||||
tim(timer), currentSpeed(0), targetSpeed(0), isAccelerating(false), isReversing(
|
||||
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() {
|
||||
targetSpeed = 0;
|
||||
this->stopTurning();
|
||||
}
|
||||
|
||||
bool Motor::isStopped() {
|
||||
return currentSpeed == 0;
|
||||
}
|
||||
void Motor::tournerDroite(int speed) {
|
||||
targetSpeed = (speed <= 626) ? speed : 626;
|
||||
isTurningRight = true;
|
||||
isTurningLeft = false;
|
||||
isAccelerating = true;
|
||||
}
|
||||
|
||||
void Motor::tournerGauche(int speed) {
|
||||
targetSpeed = (speed <= 626) ? speed : 626;
|
||||
isTurningRight = false;
|
||||
isTurningLeft = true;
|
||||
isAccelerating = true;
|
||||
}
|
||||
void Motor::stopTurning() {
|
||||
isTurningLeft = false;
|
||||
isTurningRight = false;
|
||||
}
|
||||
|
||||
void Motor::update() {
|
||||
// Gestion de l'accélération/décélération
|
||||
if (isAccelerating && currentSpeed < targetSpeed) {
|
||||
if ((isAccelerating || isReversing) && currentSpeed < targetSpeed) {
|
||||
currentSpeed++;
|
||||
} else if (isReversing && currentSpeed > -targetSpeed) {
|
||||
currentSpeed--;
|
||||
} else if (currentSpeed > targetSpeed) {
|
||||
if(currentSpeed - 25 >= 0) {
|
||||
//Ralentir rapidement
|
||||
@@ -38,15 +59,28 @@ void Motor::update() {
|
||||
}
|
||||
}
|
||||
|
||||
// Mise à jour des registres du timer
|
||||
if (isAccelerating) {
|
||||
//2 et 3 avance
|
||||
tim->CCR2 = currentSpeed;
|
||||
tim->CCR3 = currentSpeed;
|
||||
} else if (isReversing) {
|
||||
// 1 et 4 recule
|
||||
tim->CCR1 = currentSpeed;
|
||||
tim->CCR4 = currentSpeed;
|
||||
// Mise à jour des registres du timer pour contrôler les moteurs
|
||||
if (isTurningRight) {
|
||||
// Tourne à droite en avançant
|
||||
tim->CCR2 = currentSpeed;
|
||||
tim->CCR3 = currentSpeed / 2;
|
||||
} else if (isTurningLeft) {
|
||||
// Tourne à gauche en avançant
|
||||
tim->CCR2 = currentSpeed / 2;
|
||||
tim->CCR3 = currentSpeed;
|
||||
} else {
|
||||
// Mouvements standards
|
||||
if (isAccelerating) {
|
||||
tim->CCR2 = currentSpeed;
|
||||
tim->CCR3 = currentSpeed;
|
||||
} else if (isReversing) {
|
||||
tim->CCR1 = currentSpeed;
|
||||
tim->CCR4 = currentSpeed;
|
||||
}
|
||||
}
|
||||
//Moteurs arrêtés
|
||||
if(targetSpeed == 0 && currentSpeed == 0) {
|
||||
isAccelerating = 0;
|
||||
isReversing = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user