Effectivement Mike , il y a à gratter !
Ci-dessous le code de test( avec les séquences de mouvement robot commentées volontairement dans la partie "void loop" pour le test se résumant à la partie initialisation + mouvement manuel des roues dans la boucle loop ) et le fichier de traces
=> Le comptage des ticks des 2 encodeurs fonctionnent correctement en tournant les roues à la main ( le code de lecture des ticks par interruption fonctionne donc) !
=> La roue droite ne tourne pas dans la fonction "MotorsSetup" ni non plus dans les conditions normales d'exécution du sketch avec les séquences de mouvement non commentéres dans la partie "void loop" , ce qui explique pourquoi il n'y a pas de comptage de ticks par l'encodeur
=> Pourquoi la roue droite ne tourne t'elle pas lorsque l'encodeur est initialisé ?
Note : En commentant "EncodersSetup(); // Init 2 encoders" dans la boucle "setup" pour un fonctionnement sans encodeurs , la route droite tourne normalement.
Traces de comptage des ticks des 2 encodeurs fonctionnent correctement en tournant les roues à la main
<Ard<Arduino : Setup Encoders> <Arduino : Setup Motors> <Arduino : AvancerRobot()> <EncodA = -1492.00 , EncodB = 0.00> // Le moteur droit ne tourne pas ( donc pas de comptage ticks par encodeur ) <Arduino : ReculerRobot()> <EncodA = 1423.00 , EncodB = 0.00> // Le moteur droit ne tourne pas <Arduino : ArreterRobot()> <Arduino : Start loop <EncodA = 123.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = -401.00> // La roue droite tourne manuellement vers l'avant , comptage ticks OK par encodeur <EncodA = 0.00 , EncodB = -434.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = -305.00 , EncodB = 0.00> // // La roue gauche tourne manuellement vers l'avant , comptage ticks OK par encodeur <EncodA = -319.00 , EncodB = 0.00> <EncodA = -1.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00> <EncodA = 0.00 , EncodB = 0.00>
Le code utilisé
/************************************************************* Motor Shield Arduino - 2-Channel DC Motor - 10/11/2018 *************************************************************/ // ------- Bibiliothèques #include <TimerOne.h> // Pour le timer // ------ Pins MOTORS connectés à l'Arduino MEGA #define PINMOTEURGAUCHEPWMA 3 // Arduino Shield motor gauche #define PINMOTEURDROITPWMB 11 // Arduino Shield motor droit #define PINBRAKEA 9 // Arduino Shield motor gauche #define PINBRAKEB 8 // Arduino Shield motor droit #define PINMOTEURGAUCHEDIRA 12 // Arduino Shield motor gauche #define PINMOTEURDROITDIRB 13 // Arduino Shield motor droit #define PINENCODGAUCHEA 18 // Encodeur A gauche #define PINENCODGAUCHEB 19 // Encodeur B gauche #define PINENCODDROITA 20 // Encodeur A droit #define PINENCODDROITB 21 // Encodeur B droit int SpeedMotor_A=123; int SpeedMotor_B=123; // volatile => pour toute variable qui sera utilise dans les interruptions volatile int countDroit = 0; /* comptage de tick d'encoder qui sera incrémenté sur interruption " On change " sur l'interruption 0 */ volatile int countGauche = 0; /* comptage de tick d'encoder qui sera incrémenté sur interruption " On change " sur l'interruption 1 */ volatile double speedDroit = 0; // vitesse du moteur volatile double speedGauche = 0; // vitesse du moteur void setup() { SerialSetup(); // initialize the serial communication EncodersSetup(); // Init 2 encoders MotorsSetup(); // Init 2 motors sans PID } void loop() { Serial.println("<Arduino : Start loop"); // AvancerRobot(); delay(2000); // ArreterRobot(); delay(20000); // ReculerRobot(); delay(2000); Serial.println("<Arduino : End loop"); } //////////////////////////////////////////////////// void counterDroit() { bool statePINENCODDROITA = digitalRead(PINENCODDROITA); // Serial.println("CounterDroit"); // Serial.print(statePINENCODDROITA); bool statePINENCODDROITB = digitalRead(PINENCODDROITB); // Serial.print(" , "); // Serial.print(statePINENCODDROITA); (statePINENCODDROITA ^ statePINENCODDROITB) ? countDroit-- : countDroit++; // régler ++ et -- pour que faire avancer => + inverser si nécessaire } // Encoder counter 2 void counterGauche() { bool statePINENCODGAUCHEA = digitalRead(PINENCODGAUCHEA); // Serial.println("CounterGauche"); // Serial.print(statePINENCODGAUCHEA); bool statePINENCODGAUCHEB = digitalRead(PINENCODGAUCHEB); // Serial.print(" , "); // Serial.print(statePINENCODGAUCHEB); (statePINENCODGAUCHEA ^ statePINENCODGAUCHEB) ? countGauche++ : countGauche--; // régler ++ et -- pour que faire avancer => + } // Timer pour calculer la vitesse grace aux encodeurs void timerIsr() { // asservissement en vitesse speedDroit = countDroit; countDroit = 0; speedGauche = countGauche; countGauche = 0; Serial.print("<EncodA = "); Serial.print(speedGauche); // 6 ticks par tour x 34 tours par mn Serial.print(" , EncodB = "); Serial.print(speedDroit); Serial.print(">"); Serial.println(""); } //////// Traitement ordres Robot void ArreterRobot() { Serial.println("<Arduino : ArreterRobot()>"); analogWrite(PINMOTEURGAUCHEPWMA, 0); // Stop the motor by inertia , motor turn off analogWrite(PINMOTEURDROITPWMB, 0); digitalWrite(PINBRAKEA, HIGH); // set the brake pins LOW to turn them off: digitalWrite(PINBRAKEB, HIGH); } void AvancerRobot() { // Move the motor forward Serial.println("<Arduino : AvancerRobot()>"); digitalWrite(PINBRAKEA, LOW); // set the brake pins LOW to turn them off: digitalWrite(PINBRAKEB, LOW); digitalWrite(PINMOTEURGAUCHEDIRA, HIGH); // Set direction to High = Forward digitalWrite(PINMOTEURDROITDIRB, LOW); analogWrite(PINMOTEURGAUCHEPWMA, SpeedMotor_A); analogWrite(PINMOTEURDROITPWMB, SpeedMotor_B); } void ReculerRobot() { // Reverse the motors Serial.println("<Arduino : ReculerRobot()>"); digitalWrite(PINBRAKEA, LOW); // set the brake pins LOW to turn them off: digitalWrite(PINBRAKEB, LOW); digitalWrite(PINMOTEURGAUCHEDIRA, LOW); // Set direction to Low = Backward digitalWrite(PINMOTEURDROITDIRB, HIGH); // Set direction to High = Backward analogWrite(PINMOTEURGAUCHEPWMA, SpeedMotor_A); analogWrite(PINMOTEURDROITPWMB, SpeedMotor_B); // } } void MotorsSetup() { // set up all of the pins of the motor shield as outputs: Serial.println("<Arduino : Setup Motors>"); pinMode(PINBRAKEA, OUTPUT); pinMode(PINMOTEURGAUCHEDIRA, OUTPUT); pinMode(PINMOTEURGAUCHEPWMA, OUTPUT); pinMode(PINBRAKEB, OUTPUT); pinMode(PINMOTEURDROITDIRB, OUTPUT); pinMode(PINMOTEURDROITPWMB, OUTPUT); // Move the motor forward AvancerRobot(); delay(1000); ReculerRobot(); delay(1000); ArreterRobot(); } void EncodersSetup() { Serial.println("<Arduino : Setup Encoders>"); // on initialise les entrées et sorties pinMode(PINENCODGAUCHEA, INPUT_PULLUP); pinMode(PINENCODGAUCHEB, INPUT_PULLUP); pinMode(PINENCODDROITA, INPUT_PULLUP); pinMode(PINENCODDROITB, INPUT_PULLUP); // Encoder quadrature counter attachInterrupt(digitalPinToInterrupt(PINENCODGAUCHEA),counterGauche, CHANGE); attachInterrupt(digitalPinToInterrupt(PINENCODDROITA),counterDroit, CHANGE); Timer1.initialize(1000000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz Timer1.attachInterrupt( timerIsr ); } void SerialSetup() { Serial.begin(19200); // initialize the serial communication }