Comment créer un smooth motion sur esp32 piloter par joystick bluetooh

j ai des difficultés a transposer des codes arduino contrôlant des servomoteurs en smoth motion(mouvement lisse consistant a ne pas mettre d à-coup, mouvement fluide) sur un esp32 piloter par un joystick type manette ps3.

lien youtube pour comprendre: How To Make Robots Move Smoothly | Arduino Tutorial - YouTube

voici le code:

#include <Servo.h>

int pot1;
int pot2;
int pot3;
int pot4;
int pot5;
int pot6;

float pot1Scaled;
float pot2Scaled;
float pot3Scaled;
float pot4Scaled;
float pot4aScaled;
float pot5Scaled;
float pot6Scaled;

float pot1Smoothed;
float pot2Smoothed;
float pot3Smoothed;
float pot4Smoothed;
float pot4aSmoothed;
float pot5Smoothed;
float pot6Smoothed;

float pot1SmoothedPrev;
float pot2SmoothedPrev;
float pot3SmoothedPrev;
float pot4SmoothedPrev;
float pot4aSmoothedPrev;
float pot5SmoothedPrev;
float pot6SmoothedPrev;

Servo servo1;
Servo servo2;
Servo servo3;

Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;

void setup() {

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);

  Serial.begin(115200);

  servo1.attach(4);
  servo2.attach(5);
  servo3.attach(6);
  
  servo4.attach(7);
  servo5.attach(8);
  servo6.attach(9);
  servo7.attach(10);

  servo1.writeMicroseconds(1200);
  servo2.writeMicroseconds(1650);
  servo3.writeMicroseconds(1650);
  
  servo4.writeMicroseconds(1500);
  servo5.writeMicroseconds(1550);
  servo6.writeMicroseconds(1500);
  servo7.writeMicroseconds(1450);

}

void loop() {

  pot1 = analogRead(A0);
  pot2 = analogRead(A1);
  pot3 = analogRead(A2);
  pot4 = analogRead(A3);
  pot5 = analogRead(A4);
  pot6 = analogRead(A5);

  // scale all pots for the servo microseconds range

  pot1Scaled = ((pot1 - 512) * -1.6) + 1200;
  pot2Scaled = (pot2 - 512) + 1500;
  pot3Scaled = (pot3 - 512) + 1500;
  pot4Scaled = ((pot4 - 512) * 1.2) + 1500;
  pot4aScaled = ((pot4 - 512) * -1.2) + 1500;
  pot5Scaled = (pot5 - 512) * 1.5;
  pot6Scaled = (pot6 - 512) + 1650;

  pot4Scaled = constrain(pot4Scaled,900,1500);
  pot4aScaled = constrain(pot4aScaled,1500,2100);

  // smooth pots
  
  pot1Smoothed = (pot1Scaled * 0.02) + (pot1SmoothedPrev * 0.98);
  pot2Smoothed = (pot2Scaled * 0.02) + (pot2SmoothedPrev * 0.98);
  pot3Smoothed = (pot3Scaled * 0.02) + (pot3SmoothedPrev * 0.98);
  pot4Smoothed = (pot4Scaled * 0.05) + (pot4SmoothedPrev * 0.95);
  pot4aSmoothed = (pot4aScaled * 0.05) + (pot4aSmoothedPrev * 0.95);
  pot5Smoothed = (pot5Scaled * 0.02) + (pot5SmoothedPrev * 0.98);
  pot6Smoothed = (pot6Scaled * 0.02) + (pot6SmoothedPrev * 0.98);  

  // bookmark previous values

  pot1SmoothedPrev = pot1Smoothed;
  pot2SmoothedPrev = pot2Smoothed;
  pot3SmoothedPrev = pot3Smoothed;
  pot4SmoothedPrev = pot4Smoothed;
  pot4aSmoothedPrev = pot4aSmoothed;
  pot5SmoothedPrev = pot5Smoothed;
  pot6SmoothedPrev = pot6Smoothed;

  Serial.print(pot1Smoothed);
  Serial.print(" , ");
  Serial.print(pot2Smoothed);
  Serial.print(" , ");
  Serial.print(pot3Smoothed);
  Serial.print(" , ");
  Serial.print(pot4Smoothed);
  Serial.print(" , ");
  Serial.print(pot5Smoothed);
  Serial.print(" , ");
  Serial.println(pot6Smoothed);

  // write servos

  servo1.writeMicroseconds(pot1Smoothed);
  servo2.writeMicroseconds(pot6Smoothed - pot5Smoothed);
  servo3.writeMicroseconds(pot6Smoothed + pot5Smoothed);

  servo5.writeMicroseconds(pot2Smoothed);
  servo7.writeMicroseconds(pot2Smoothed);

  servo4.writeMicroseconds(pot4Smoothed);
  servo6.writeMicroseconds(pot4aSmoothed);

  

  delay(5);                      

}

merci beaucoup pour votre aide

:warning:

Merci d’éditer votre post en utilisant le :pencil2: dans la barre d’outil juste sous votre post et rajoutez les balises de de code (c’est difficilement lisible tel quel) :

  • sélectionner la partie du texte qui correspond au code
  • appuyez sur l’icône <code/> dans la barre d’outils pour indiquer que c’est du code

(Assurez vous aussi d’indenter le code correctement dans l’IDE avant de le copier pour le coller ici. Cela se fait en pressant ctrlT sur PC ou cmdT sur un Mac)

il serait bon de (re)lire les recommandations listées dans « Les bonnes pratiques du Forum Francophone”

bonjour c est possible de le modifier?

Oui, comme l'explique @J-M-L, il suffit de cliquer sur l'icône crayon qui se trouve sous ton 1er message pour ouvrir celui-ci en mode édition

Merci d’avoir rajouté les balises.

Quelle est votre demande exactement ?

Votre ESP32 n’a pas les pins A0 … A5 donc il faudra adapter cela en prenant une des nombreuses pins ayant cette possibilité (cf les docs de votre module)

  • L’ADC d’un esp32 fait un échantillonnage sur 12 bits et non pas 10 comme les arduinos habituels et donc la ou vous voyez des formules avec 1023 il faut mettre 4095. (Dans votre cas au vu de la formule - idem pour 512 c’est 2048 qu’il faut mettre à la place pour avoir la fonction affine qui transforme la lecture en position du servo)

  • la tension est 3.3V et pas 5V donc attention aux branchements). L’ADC s’arrête à 3.2V donc c’est cette valeur qui donne 4095 et non pas 3.3V (une tension entre 3.2 et 3.3V donnera donc la valeur max et il n’y a pas de linéarité sur cette plage. Idem entre 0 et 0.1V où vous aurez 0)

Attention si vous avez besoin du wifi vous ne pouvez pas utiliser les pins liées à l’ADC2 de votre ESP.

merci pour votre aide...

j ai donc écrit le code pour deux servos diriger avec lx et rx du joystick ps3 avec sa bibliothèque

j ai donc commencer a écrire le code.j ai suivi vos conseilles .j ai remplacer 512 par 2048 .J ai branché les servos en sorti 3.3 v et le signal sur le pin 27 et 13 .j ai laisser volontairement 512 pour l essai.

le servo 1 ne bouge pas du tout (valeur 512 changé par 2048)
le servo 5 bouge très très légerement.( valeur 512)

je suis novice dans ce domaine et ce code est certainement pas au point

voici mon code

#include <Ps3Controller.h>
#include <ESP32_Servo.h> 


int pozycjaRX;
int pozycjaLX;

float pozycjaLXScaled;
float pozycjaLXSmoothed;
float pozycjaLXSmoothedPrev;


float pozycjaRXScaled;
float pozycjaRXSmoothed;
float pozycjaRXSmoothedPrev;


Servo servo1;
Servo servo5;

void onConnect(){

  Serial.println("Connected.");}


void setup() {

  
  

  Serial.begin(115200);

  Ps3.attachOnConnect(onConnect);

  Ps3.begin("a4:d9:31:2b:8d:c1");

  Serial.println("Ready.");

  servo1.attach(13);
  servo5.attach(27);

  servo1.writeMicroseconds(1600);
  servo5.writeMicroseconds(1600);
}




void loop() {

  pozycjaRX = map ((Ps3.data.analog.stick.rx),-128,127,180,0);
  pozycjaLX = map ((Ps3.data.analog.stick.lx),-128,127,180,0);

  
  

  // scale all pots for the servo microseconds range

  pozycjaRXScaled = ((pozycjaRX - 2048) * -1.6) + 1200;
  pozycjaLXScaled = (pozycjaLX - 512) + 1500;
  

  

  // smooth pots
  
  pozycjaRXSmoothed = (pozycjaRXScaled * 0.02) + (pozycjaRXSmoothedPrev * 0.98);
  pozycjaLXSmoothed = (pozycjaLXScaled * 0.02) + (pozycjaLXSmoothedPrev * 0.98);
  

  // bookmark previous values

  pozycjaRXSmoothedPrev = pozycjaRXSmoothed;
  pozycjaLXSmoothedPrev = pozycjaLXSmoothed;
  

  Serial.print(pozycjaRXSmoothed);
  Serial.print(" , ");
  Serial.print(pozycjaLXSmoothed);
  Serial.print(" , ");
  

  // write servos

  servo1.writeMicroseconds(pozycjaRXSmoothed);

  servo5.writeMicroseconds(pozycjaLXSmoothed);
  

  
  

  delay(5);                      
}

merci pour votre aide

Quelle est cette bibliothèque ? (Lien vers son github) fonctionne elle sur esp32 ?
Est-ce celle ci ?

oui tout a fait ca marche bien sur esp32.
il y a beaucoup de video sur youtube a ce sujet.
les servomoteurs fonctionnent bien

voici un exemple de code basic pour servomoteur et joystick ps3

#include <Ps3Controller.h>
#include <ESP32_Servo.h> 

Servo serwo;

Servo serwo2;

int pozycjaLX=90;

int pozycjaRX=90;





void onConnect(){

    Serial.println("Connected.");}



void setup(){

    Serial.begin(115200);

    Ps3.attachOnConnect(onConnect);

    Ps3.begin("a4:d9:31:2b:8d:c1");

    Serial.println("Ready.");

    


    

    serwo.attach(5, 500, 2400);//pin,czestotliwości min i max

    

    
    serwo2.attach(13, 500, 2400);//pin,czestotliwości min i max

}

void loop(){

      
      

      pozycjaLX=map((Ps3.data.analog.stick.lx),-128,127,180,0);

      pozycjaRX=map((Ps3.data.analog.stick.rx),-128,127,180,0);
       
         
     serwo.write(pozycjaLX);
     serwo2.write(pozycjaRX);
      
} 

est-ce que ce code fonctionne pour vous ?

Ce code fonctionne chez moi.
Reste plus qu a l’adapter pour les mouvements lisses.

l'approche prise dans le code de départ était de prendre 98% de la valeur précédente et 2% de la nouvelle valeur pour "amortir" les variations

donc il vous faut mémoriser cette valeur précédente

oui c est bien ca
Vous avez des suggestions sur ce code?

comme c'était fait dans le code précédent

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.