Advertisement
Ollivier

Controle2ServosRotatifsParJoystick

Jul 30th, 2020 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.58 KB | None | 0 0
  1. // ------------------------
  2. // Servo rotatif :
  3. // - valeurs entre 0 et 180
  4. // - 0 : vitesse max dans un sens
  5. // - 90 moteur à l'arrêt
  6. // - 180 vitesse max dans l'autre sens
  7. // ------------------------
  8. // But : utiliser deux servos rotatifs comme moteurs (roues)
  9. // Vitesse et direction contrôlées par joystick
  10. // Principe utilisé :
  11. // la direction du joystick vertical (haut ou bas) indique la vitesse "de base" des moteurs
  12. // la vitesse d'un moteur est modifié pour tourner
  13. // - la direction du joystick horiz (droite ou gauche) donne la direction en étant > 0 ou < 0
  14. // --> < 0 : gauche || > 0 : droite
  15. // - la valeur du joystick indique le pourcentage à retirer de la vitesse du moteur concerné
  16. // --> direction à droite on retire x% de la vitesse du moteur droit pour tourner à droite
  17. // --> direction à gauche on retire x% de la vitesse du moteur gauche pour tourner à gauche
  18. // ------------------------
  19.  
  20.  
  21. #include <Servo.h>
  22. //#define DEBUG
  23.  
  24. Servo servoRotatifDroit;
  25. Servo servoRotatifGauche;
  26.  
  27. const int brochePotVitesse = A5;
  28. const int brochePotDirection = A0;
  29.  
  30. int valPotVitesse;
  31. int valPotDirection;
  32. int valVitesseDroit;
  33. int valVitesseGauche;
  34. int valeurMoteurDroit;
  35. int valeurMoteurGauche;
  36.  
  37. void setup() {
  38.  
  39. #ifdef DEBUG
  40.   //Serial.begin(9600);
  41. #endif
  42.  
  43.   servoRotatifDroit.attach(11);
  44.   servoRotatifGauche.attach(6);
  45.   pinMode(brochePotVitesse, INPUT);
  46.   pinMode(brochePotDirection, INPUT);
  47.  
  48. }
  49.  
  50. void loop() {
  51.  
  52.   valPotVitesse = analogRead(brochePotVitesse);
  53.   valVitesseDroit = map(valPotVitesse, 0, 1023, 0, 180);
  54.   valVitesseGauche = map(valPotVitesse, 0, 1023, 180, 0); // vu que les servos sont les même on considère que celui-ci est retourné
  55.   valPotDirection = map(analogRead(brochePotDirection), 0, 1023, -100, 100);
  56.  
  57. #ifdef DEBUG
  58.   // =DEBUG=====================================================
  59.   /**/Serial.println ("--------------------");
  60.   /**/Serial.println ("Etat potentiomètre");
  61.   /**/Serial.println ("--------------------");
  62.   /**/Serial.print ("vitesse Droite : ");
  63.   /**/Serial.println (valVitesseDroit);
  64.   /**/Serial.print ("vitesse Gauche : ");
  65.   /**/Serial.println (valVitesseGauche);
  66.   /**/Serial.print ("Direction : ");
  67.   /**/Serial.println (valPotDirection);
  68.   /**/Serial.println ("--------------------");
  69.   /**/Serial.println ();
  70.   // =DEBUG=FIN=================================================
  71. #endif
  72.  
  73.   if (valVitesseDroit > 93 || valVitesseDroit < 87) {  // si la vitesse n'est pas en position centrale (marge de 3)
  74.     // on avance ou on recule
  75.     if (valPotDirection <= 5 && valPotDirection >= -5) {  // si direction est en position centrale (neutre) (marge de 5)
  76.  
  77.       valeurMoteurDroit = valVitesseDroit;
  78.       valeurMoteurGauche = valVitesseGauche;
  79.  
  80.       if (valVitesseDroit > 93) {
  81. #ifdef DEBUG
  82.         // =DEBUG=====================================================
  83.         Serial.println ("Tout droit avant");
  84.         // =DEBUG=FIN=================================================
  85. #endif
  86.        
  87.       } else if (valVitesseDroit < 87) {
  88.  
  89. #ifdef DEBUG
  90.         // =DEBUG=====================================================
  91.         Serial.println ("Tout droit arriere");
  92.         // =DEBUG=FIN=================================================
  93. #endif
  94.        
  95.       }
  96.  
  97.     } else if (valPotDirection < -5) { // tourne gauche
  98.       if (valVitesseGauche < 87) {  // avance (Rappel : inversion du moteur gauche)
  99.  
  100. #ifdef DEBUG
  101.         // =DEBUG=====================================================
  102.         Serial.println ("Avance Gauche");
  103.         // =DEBUG=FIN=================================================
  104. #endif
  105.  
  106.         valeurMoteurGauche = valVitesseGauche - (((90 - valVitesseGauche) * valPotDirection) / 100);
  107.         valeurMoteurDroit = valVitesseDroit;
  108.  
  109.       } else if (valVitesseGauche > 93) {  // recule (Rappel : inversion du moteur gauche)
  110.        
  111. #ifdef DEBUG
  112.         // =DEBUG=====================================================
  113.         Serial.println ("Recule Gauche");
  114.         // =DEBUG=FIN=================================================
  115. #endif
  116.  
  117.         valeurMoteurGauche = valVitesseGauche + (((valVitesseGauche - 90) * valPotDirection) / 100);
  118.         valeurMoteurDroit = valVitesseDroit;
  119.  
  120.       }
  121.     } else if (valPotDirection > 5  ) { // tourne droite
  122.       if (valVitesseDroit > 93) {  // avance
  123.        
  124. #ifdef DEBUG
  125.         // =DEBUG=====================================================
  126.         Serial.println ("Avance Droite");
  127.         // =DEBUG=FIN=================================================
  128. #endif
  129.  
  130.         valeurMoteurDroit = valVitesseDroit - (((valVitesseDroit - 90) * valPotDirection) / 100);
  131.         valeurMoteurGauche = valVitesseGauche;
  132.  
  133.       } else if (valVitesseDroit < 87) {  // recule
  134.  
  135. #ifdef DEBUG
  136.         // =DEBUG=====================================================
  137.         Serial.println ("Recule Droite");
  138.         // =DEBUG=FIN=================================================
  139. #endif
  140.  
  141.         valeurMoteurDroit = valVitesseDroit + (((90 - valVitesseDroit) * valPotDirection) / 100);
  142.         valeurMoteurGauche = valVitesseGauche;
  143.  
  144.       }
  145.     }
  146.   } else if (valVitesseDroit < 93 && valVitesseDroit > 87) {
  147.  
  148.     // Rajouter la rotation sur plac droite et gauche
  149.     if (valPotDirection < -5) {
  150.  
  151.       valeurMoteurDroit = map(valPotDirection, 0, 1023, 0, 180);
  152.       valeurMoteurGauche = map(valPotDirection, 0, 1023, 0, 180);
  153.  
  154.     } else if (valPotDirection > 5) {
  155.  
  156.       valeurMoteurDroit = map(valPotDirection, 0, 1023, 180, 0);
  157.       valeurMoteurGauche = map(valPotDirection, 0, 1023, 180, 0);
  158.  
  159.     } else if (valPotDirection > -5 && valPotDirection < 5) {
  160.  
  161. #ifdef DEBUG
  162.       // =DEBUG=====================================================
  163.       Serial.println ("Stop");
  164.       // =DEBUG=FIN=================================================
  165. #endif
  166.      
  167.       valeurMoteurDroit = 90;
  168.       valeurMoteurGauche = 90;
  169.  
  170.     }
  171.   }
  172.  
  173. #ifdef DEBUG
  174.   // =DEBUG=====================================================
  175.   Serial.println ();
  176.   Serial.println ("--------------------");
  177.   Serial.println ("Etat moteur");
  178.   Serial.println ("--------------------");
  179.   Serial.print ("Valeur Moteur Droit : ");
  180.   Serial.println (valeurMoteurDroit);
  181.   Serial.print ("Valeur Moteur Gauche : ");
  182.   Serial.println (valeurMoteurGauche);
  183.   Serial.println ("--------------------");
  184.   Serial.println ();
  185.   Serial.println ("<<==============================>>");
  186.   Serial.println ();
  187.   // =DEBUG=FIN=================================================
  188. #endif
  189.  
  190.   servoRotatifDroit.write(valeurMoteurDroit);
  191.   servoRotatifGauche.write(valeurMoteurGauche);
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement