mirror of
https://github.com/modelec/MarcelMoteurSTM32.git
synced 2026-01-18 16:47:23 +01:00
upgrade mis à jour pour fonctionner avec le pid)
This commit is contained in:
@@ -6,10 +6,10 @@
|
||||
class Motor {
|
||||
private:
|
||||
TIM_TypeDef *tim;
|
||||
uint16_t rightCurrentSpeed;
|
||||
uint16_t leftCurrentSpeed;
|
||||
uint16_t rightTargetSpeed;
|
||||
uint16_t leftTargetSpeed;
|
||||
int16_t rightCurrentSpeed;
|
||||
int16_t leftCurrentSpeed;
|
||||
int16_t rightTargetSpeed;
|
||||
int16_t leftTargetSpeed;
|
||||
bool isAccelerating;
|
||||
bool isReversing;
|
||||
bool isTurningRight;
|
||||
@@ -19,8 +19,8 @@ public:
|
||||
Motor(TIM_TypeDef *timer);
|
||||
void setRightTargetSpeed(int pwm);
|
||||
void setLeftTargetSpeed(int pwm);
|
||||
float getRightCurrentSpeed();
|
||||
float getLeftCurrentSpeed();
|
||||
int16_t getRightCurrentSpeed();
|
||||
int16_t getLeftCurrentSpeed();
|
||||
void accelerer(int speed);
|
||||
void reculer(int speed);
|
||||
void stop();
|
||||
|
||||
@@ -48,6 +48,7 @@ bool isDelayPassed(uint32_t delay) {
|
||||
void determinationCoefPosition(Point objectifPoint, Point pointActuel){
|
||||
PidPosition pid(0,0,0,0,0,0,objectifPoint);
|
||||
|
||||
|
||||
std::array<double, 2> vitesse = pid.updateNouvelOrdreVitesse(pointActuel);
|
||||
PidVitesse pidG(0, 0, 0, vitesse[0]);
|
||||
PidVitesse pidD(0, 0, 0, vitesse[1]);
|
||||
@@ -136,6 +137,9 @@ void ModelecOdometryLoop() {
|
||||
Point targetPoint(0.20, 0.20,0, StatePoint::FINAL);
|
||||
determinationCoefPosition(currentPoint,targetPoint);
|
||||
|
||||
sprintf(msg, "X: %.3f m, Y: %.3f m, Theta: %.3f rad\r\n", x, y, theta);
|
||||
HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY);
|
||||
|
||||
motor.update();
|
||||
|
||||
}
|
||||
|
||||
@@ -39,10 +39,10 @@ void Motor::setLeftTargetSpeed(int pwm){
|
||||
}
|
||||
|
||||
}
|
||||
float Motor::getRightCurrentSpeed(){
|
||||
int16_t Motor::getRightCurrentSpeed(){
|
||||
return this->rightCurrentSpeed;
|
||||
}
|
||||
float Motor::getLeftCurrentSpeed(){
|
||||
int16_t Motor::getLeftCurrentSpeed(){
|
||||
return this->leftCurrentSpeed;
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ void Motor::stopTurning() {
|
||||
void Motor::update() {
|
||||
// Mise à jour des vitesses pour chaque moteur
|
||||
// Moteur gauche
|
||||
if (leftCurrentSpeed < leftTargetSpeed) {
|
||||
/*if (leftCurrentSpeed < leftTargetSpeed) {
|
||||
leftCurrentSpeed++;
|
||||
} else if (leftCurrentSpeed > leftTargetSpeed) {
|
||||
if (leftCurrentSpeed - 25 >= 0) {
|
||||
@@ -116,6 +116,28 @@ void Motor::update() {
|
||||
if (isStopped()) {
|
||||
isAccelerating = false;
|
||||
isReversing = false;
|
||||
}*/
|
||||
|
||||
// Appliquer targetSpeed dans currentSpeed
|
||||
this->leftCurrentSpeed = this->leftTargetSpeed;
|
||||
this->rightCurrentSpeed = this->rightTargetSpeed;
|
||||
|
||||
// Contrôle moteur gauche
|
||||
if (this->leftCurrentSpeed >= 0) {
|
||||
this->tim->CCR2 = static_cast<uint16_t>(this->leftCurrentSpeed); // avant
|
||||
this->tim->CCR1 = 0;
|
||||
} else {
|
||||
this->tim->CCR1 = static_cast<uint16_t>(-this->leftCurrentSpeed); // arrière
|
||||
this->tim->CCR2 = 0;
|
||||
}
|
||||
|
||||
// Contrôle moteur droit
|
||||
if (this->rightCurrentSpeed >= 0) {
|
||||
this->tim->CCR3 = static_cast<uint16_t>(this->rightCurrentSpeed); // avant
|
||||
this->tim->CCR4 = 0;
|
||||
} else {
|
||||
this->tim->CCR4 = static_cast<uint16_t>(-this->rightCurrentSpeed); // arrière
|
||||
this->tim->CCR3 = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,16 +43,29 @@ float Pid::getKd(){
|
||||
|
||||
//Methodes
|
||||
int Pid::getPWMCommand(float vitesse){
|
||||
/*
|
||||
* Passer du rpm à m/s : 75/60 = 1.25 (nb tour en 1s). circonférence = pi*diamètre roues. Vitesse en m/s = circonférence * nb tour/s
|
||||
*
|
||||
*/
|
||||
if(vitesse >= 0.235){ //si on dépasse la vmax qu'on peut atteindre, on bride
|
||||
return 626;
|
||||
} else{
|
||||
float pwm = (626*vitesse) / 0.235; //produit en croix
|
||||
return std::floor(pwm);
|
||||
}
|
||||
constexpr float VITESSE_MAX = 0.235f; // m/s
|
||||
constexpr int PWM_MAX = 626; // commande PWM max
|
||||
constexpr int PWM_MIN = 30; // zone morte (optionnelle)
|
||||
|
||||
// Saturation de la vitesse
|
||||
if (vitesse > VITESSE_MAX){
|
||||
vitesse = VITESSE_MAX;
|
||||
}
|
||||
else if (vitesse < -VITESSE_MAX){
|
||||
vitesse = -VITESSE_MAX;
|
||||
}
|
||||
|
||||
// Conversion m/s -> PWM avec conservation du signe
|
||||
float pwm = (PWM_MAX * std::abs(vitesse)) / VITESSE_MAX;
|
||||
|
||||
// Application d'un seuil minimal pour éviter les très faibles commandes
|
||||
if (pwm < PWM_MIN){
|
||||
pwm = 0;
|
||||
}
|
||||
|
||||
|
||||
// Renvoi avec le bon signe
|
||||
return (vitesse >= 0) ? std::floor(pwm) : -std::floor(pwm);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user