Objective
The objective of this tutorial is to understand how to connect, using a motor controller, a 12V motor to a CrcDuino. It also shows how to control, in the most basic way, said motor using a quadrature relative encoder (like the one built into goBilda motors). Using an encoder allows to precisely monitor a motor rotation in time.
...
Objectif
L’objectif de ce tutoriel est de comprendre comment connecter, à l’aide d’un contrôleur de moteur, un moteur de 12V au CRCduino. Cela montre aussi comment contrôler, de la manière la plus simple possible, le-dit moteur à l’aide d’un encodeur relatif à quadrature (comme ceux présents dans les moteurs goBilda). Utiliser un encodeur permet de précisément mesurer la rotation d’un moteur.
Schéma électrique et présentation complète
View file | ||
---|---|---|
|
Code
Download the Arduino IDE .ino fileTéléchargez le code de démonstration:
View file | ||
---|---|---|
|
This is a transcript of the codeQui est ici retranscrit:
Code Block | ||
---|---|---|
| ||
//************************************************************************************************************************** // Tutoriel CrcDuino Tutorial 4 - BasicContrôle powerde motorbase controld'un usingmoteur ande encoderpuissance // Inspired by: https://www.norwegiancreations.com/2017/09/arduino-tutorial-using-millis-instead-of-delay/ // and https://www.norwegiancreations.com/2018/10/arduino-tutorial-avoiding-the-overflow-issue-when-using-millis-and-micros/ // The code below uses the millis() native Arduino function, a command that returns the number of milliseconds since the board started running its current sketch, to measure elapsed time. // NOTE: To simplify code and focus on the encoder use, this version of the code does not cover the case where the BTN_RUN is still pressed when the encoder reaches its position. //à l'aide d'un encodeur //************************************************************************************************************************** #include <CrcLib.h> // La librairie "Encoder" library bypar Paul Stoffregen mustdoit firstpréalablement beêtre installedinstallée via le "Arduino Library Manager") // Guide How-to-used'utilisation (en anglais) https://www.pjrc.com/teensy/td_libs_Encoder.html //#define ENCODER_OPTIMIZE_INTERRUPTS #include <Encoder.h> //Renaming pins with significant names C'est pratique d'avoir un nom pour chaque port de contrôle #define MC_1 CRC_PWM_1 #define BTN_RUNAVANCE CRC_DIG_1 //Motor1 encoderConstantes variable: "Encoder" library pre-requisite Encoder motor1Enco(CRC_ENCO_A, CRC_ENCO_B)const int POSITION_ARRET = 1994; //This encoderTrouvé ispar pluggedessai-erreur intopour theseune 2rotation pinsde on360° theavec boardnos moteurs. //ProgramCertains executionmoteurs variable(comme boolle motorRotatinggoBilda) =ont LOW;cette //information willdans allow Motor_1 rotation //Program constantsla "specsheet" const int STOPVITESSE_POSITIONMOTEUR = 199413; //Found by trial and error to correspond to a 360deg rotation on my setup. Some motor (like the Bilda) have this information in their specs sheets const int MOTOR_SPEED = 13; //127*0.10=13 10% × 127 = 13 // Variable pour l'encodeur du moteur: dépend de la librairie "Encoder" Encoder enco(CRC_ENCO_A, CRC_ENCO_B); // L'encodeur est branché à ces deux ports sur le CRCduino //************************************************************************************************************************** void setup() { CrcLib::Initialize(); Serial.begin(2000000); //High baud rate for faster printing on screen 2000000 permet de bloquer moins longtemps lors d'un affichage sur la console CrcLib::SetDigitalPinMode(BTN_RUNAVANCE, INPUT); CrcLib::InitializePwmOutput(MC_1); //Use Utilisez CrcLib::InitializePwmOutput(MC_1,1 true); topour rapidlyfacilement reverserenverser thisla motor rotation direction allde overrotation thedu codemoteur sans avoir à modifier le //At power-up, establish a known encoder position value reste du programme // BestÉtablir practiceune wouldposition bede toréférence insteadau assigndébut ade physicall'exécution limit switch indicating// theIdéalement, zeroune position,« limit andswitch » havepermettrait theau robot searchd'automatiquement trouver la // that position itself« zéro » atau sequencedébut startdu inprogramme. theEn void loop() part. This is called "homing"anglais, cela s'appelle le « homing ». motor1Encoenco.write(0); } void loop() { CrcLib::Update(); Serial.println(motor1Encoenco.read()); //Continuously Afficher printen oncontinu screenla theposition motordu positionmoteur if(CrcLib::GetDigitalInput(BTN_RUNAVANCE) == 0LOW) { //Start rotationLorsque whenle buttonbouton isest pressedappuyé, débuter la {rotation motorRotating = HIGHCrcLib::SetPwmOutput(MC_1, VITESSE_MOTEUR); } if(motorRotating == HIGH && (motor1Encoenco.read() <>= STOPPOSITION_POSITIONARRET) ){ //Encoder positionLorsque notl'encodeur yetatteint reachedsa position maximale, {arrêter CrcLib::SetPwmOutput(MC_1,MOTOR_SPEED); } else { CrcLib::SetPwmOutput(MC_1,0); } } |
Info |
---|
Want smoother, more precise control on a motor position?This tutorial presents the most basic way to control the position; we run at a fixed rotation speed until the position is reached, and we stop abruptly. It's not really a nice way to do things; inertia will make the rotation stop a little over the desired position, and abrupt stops are not very good for motors.For example, it's much nicer, when you're in a car approaching a red light, if the driver starts braking ahead of time, gradually reducing the speed until you reach the intersection. Wouldn't it be nice if there was a way for the CrcDuino to know that the motor is getting closer and closer to the desired position, and adjust the speed accordingly to gradually come to a smooth stop? A quite simple, and much documented, way to do so is by incorporating a PID control to your program. Search, as an example, "Arduino PID control position motor" in your favorite search engine to start your exploration of this popular technique. As a short introduction, such a controller should compare the actual position of the motor (the controlled value) and the desired position (the setpoint). Using mathematical calculations (based on the P, I and D factor values you decide to use), the system will adjust the motor speed (the controlled value) depending on the difference between the two position (the error) Comment avoir un contrôle plus précis, plus lisse de la position ?Ce tutoriel présente la façon la plus simple de contrôler la position: le moteur va à une vitesse fixe jusqu'à ce qu’il atteigne la position voulue, puis s’arrête brusquement. Ce n’est pas idéal – l’inertie fera que le moteur s’arrêtera un peu après la position voulue, et les arrêts brusques ne sont pas agréables pour les moteurs. Par exemple, c’est beaucoup plus agréable, lorsque vous êtes dans une auto approchant un feu rouge, si le conducteur commence à ralentir un peu à l’avance et de manière graduelle. Et si le CRCduino pouvait lui aussi savoir que le moteur s’approche de sa position voulue et ralentir peu à peu ? Une façon très simple et largement documentée de faire cela est d’incorporer un contrôle PID à votre programme. Vous pouvez chercher « Arduino contrôle moteur PID » dans votre moteur de recherche préféré pour commencer en apprendre un peu plus sur cette technique populaire. De manière brève, un contrôleur PID compare la différence entre la position voulue et la position actuelle (l’erreur). En utilisant certaines approximations mathématiques et trois paramètres (P, I et D, d’où le nom de la technique), le système ajuste la vitesse du moteur (valeur contrôlée) pour réduire au maximum l’erreur. |