1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
| /*
Name: MoteursCCsynchro.ino
Created: 26.02.2022
Author: jpbbricole
*/
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
// https://www.arduino.cc/reference/en/libraries/jc_button/
//------------------------------------- Moteurs (mot)
enum motDirectionsIndex {motDirSTOP, motDirCW, motDirCCW, motDirBREAK};
enum moteursIndex {moteurA, moteurB};
const int motReference = moteurA;
const int motBreakPulse = 100; // Durée impulsion de freinage
const int motSyncPulse = 7; // Durée impulsion de synchronisation
long motSyncDeltaMax = 10; // Durée impulsion de synchronisation
struct motorDef
{
const int hBridgeA; // Port A du moteur
const int hBridgeB; // Port B
const int rotEncPinA; // Codeur rotatif entrée A
const int rotEncPinB; // Codeur rotatif entrée A
const int rotEncincrement; // Codeur rotatif entrée A
int rotEncPasTour; // Codeur pas au tour
volatile long positionPas; // Position en pas
volatile long positionTours; // Position en tours
int direction; // Sens de rotation CW ou CCW
boolean isRunning; // Si en fonction
};
motorDef mot[] = // Paramétrage des moteurs
{// H-Br. A H-Br. B Encod A Encod B Incrément. Enc pas/tourr Pos. Pas Pos. Tours Direction En fonction
{ A0, A3, 2, 8, -1, 11, 0, 0, motDirCW, false }, // Moteur A
{ 10, 11, 3, 9, 1, 11, 0, 0, motDirCW, false }, // Moteur B
};
const int motNumber = sizeof(mot)/sizeof(mot[0]); // Nombre de moteurs
const int motSpeedDefault = 250; // Vitesse par défaut
//------------------------------------- H-Bridgr (hbri)
const int hbriCmdOn = HIGH;
//------------------------------------- Joystick (joy)
// ToggleButton(pin, dbTime, puEnable, invert);
const int joyUpPin = A1;
const int joyDownPin = A2;
// pin, dbTime, puEnable, invert
Button joyUp(joyUpPin);//, 20, true, true);
Button joyDown(joyDownPin);//, 20, true, false);
//------------------------------------- Ligne de commandes
bool cmdNouvelleRecue = false; // Si une nouvelle commande a été reçue
String cmdTexte = ""; // Texte de la commande
unsigned long displTimer = millis();
void setup()
{
Serial.begin(115200);
Serial.setTimeout(50); // Pour fin des commandes depuis le moniteur
joyUp.begin(); // Initialisation des boutons des joysticks
joyDown.begin();
//--------------------------------- Entrées/Sorties
for (int m = 0; m < motNumber; m ++)
{
pinMode(mot[m].hBridgeA, OUTPUT); // Initialisdation du pont H-Bridge
pinMode(mot[m].hBridgeB, OUTPUT);
digitalWrite(mot[m].hBridgeA, !hbriCmdOn);
digitalWrite(mot[m].hBridgeB, !hbriCmdOn);
pinMode(mot[m].rotEncPinA, INPUT_PULLUP);
pinMode(mot[m].rotEncPinB, INPUT_PULLUP);;
}
attachInterrupt(digitalPinToInterrupt(mot[moteurA].rotEncPinA), rotencAchangeHandling, FALLING);
attachInterrupt(digitalPinToInterrupt(mot[moteurB].rotEncPinA), rotencBchangeHandling, FALLING);
}
void loop()
{
// Affichage périodique des positions moteurA, moteurB et moteurA-moteurB
if (millis()-displTimer >= 1000)
{
Serial.print(mot[motReference].positionPas);
Serial.print("\t");
Serial.print(mot[moteurB].positionPas);
Serial.print("\t");
Serial.println(mot[motReference].positionPas - mot[moteurB].positionPas);
displTimer = millis();
}
//--------------------------------- Lecture des joystick
joyUp.read();
joyDown.read();
if (joyUp.wasPressed() && !mot[motReference].isRunning) // Si joystick UP pressé et moteur arrêté
{
motSynchroReStart();
motRunAll(motDirCW);
mot[motReference].isRunning = true;
Serial.println("Moteurs AVANT ON");
}
if (joyUp.wasReleased() && mot[motReference].isRunning) // Si joystick UP relaché et moteur en fonction
{
motRunAll(motDirBREAK);
mot[motReference].isRunning = false;
Serial.println("Moteurs AVANT OFF");
motSynchro();
}
if (joyDown.wasPressed() && !mot[motReference].isRunning) // Si joystick DOWN pressé et moteur arrêté
{
motSynchroReStart();
motRunAll(motDirCCW);
Serial.println("Moteurs ARRIERE ON");
mot[motReference].isRunning = true;
}
if (joyDown.wasReleased() && mot[motReference].isRunning) // Si joystick DOWN relaché et moteur en fonction
{
motRunAll(motDirBREAK);
mot[motReference].isRunning = false;
Serial.println("Moteurs ARRIERE OFF");
motSynchro();
}
//--------------------------------- Ecoute du port serie
//serialEvent(); // Enlever le commentaire si l'appel ne se fait pas automatiquement
// Pour Leonardo, Micro ou Yún
if (cmdNouvelleRecue) // Si une nouvelle commande depuis le moniteur
{
cmdExecute(cmdTexte);
cmdTexte = "";
cmdNouvelleRecue = false;
}
}
//--------------------------------- Moteur marche tous (0-100)
void motImpuse(int motIndex, int duration, int direction)
{
motRun(motIndex, direction);
delay(duration);
motRun(motIndex, motDirSTOP);
}
//--------------------------------- Moteur marche tous (0-100)
void motRunAll(int sensRot)
{
for (int m = 0; m < motNumber; m ++)
{
motRun(m, sensRot);
}
}
//--------------------------------- Moteur marche (0-100)
void motRun(int motIndex, int motDir) //, boolean withBreak)
{
int hbriCmdA;
int hbriCmdB;
switch (motDir) {
case motDirSTOP:
Serial.println("dir STOP " + String(motIndex));
hbriCmdA = !hbriCmdOn; // Tout OFF
hbriCmdB = !hbriCmdOn;
break;
case motDirCW:
Serial.println("dir CCW " + String(motIndex));
hbriCmdA = hbriCmdOn;
hbriCmdB = !hbriCmdOn;
mot[motIndex].direction = motDirCW;
break;
case motDirCCW:
Serial.println("dir CCW " + String(motIndex));
hbriCmdA = !hbriCmdOn;
hbriCmdB = hbriCmdOn;
mot[motIndex].direction = motDirCCW;
break;
case motDirBREAK:
Serial.println("dir BREAK " + String(motIndex));
hbriCmdA = hbriCmdOn; // Tout ON
hbriCmdB = hbriCmdOn;
break;
default:
break;
}
digitalWrite(mot[motIndex].hBridgeA, hbriCmdA);
digitalWrite(mot[motIndex].hBridgeB, hbriCmdB);
if (motDir == motDirBREAK)
{
delay(motBreakPulse);
digitalWrite(mot[motIndex].hBridgeA, !hbriCmdOn);
digitalWrite(mot[motIndex].hBridgeB, !hbriCmdOn);
Serial.println("dir BREAK OFF " + String(motIndex));
}
}
void motSynchro()
{
for (int m = 0; m < motNumber; m ++)
{
if (m != motReference) // Si pas moteur principal
{
boolean syncOk = false;
while(!syncOk)
{
long motPosDelta = mot[motReference].positionPas - mot[m].positionPas;
int syncDir = (motPosDelta < 0) ? motDirCCW : motDirCW; // Si négatif direction CCW
if (abs(motPosDelta) <= motSyncDeltaMax) // Si synchrone
{
syncOk = true;
}
else
{
Serial.print("\t\tSYNC " + String(motPosDelta));
Serial.print("\tMoteur " + String(m));
Serial.println("\t " + String(motPosDelta));
motImpuse(m, motSyncPulse, syncDir);
delay(5);
}
Serial.print(mot[motReference].positionTours);
Serial.print("\t");
Serial.println(mot[m].positionTours);
}
}
}
}
void motSynchroReStart()
{
for (int m = 0; m < motNumber; m ++)
{
mot[m].positionPas = 0;
mot[m].positionTours = 0;
}
}
//------------------------------------- Rotary encoder Traitement des interruptions
void rotencAchangeHandling() // Sortie A
{
if (!digitalRead(mot[moteurA].rotEncPinA) == !digitalRead(mot[moteurA].rotEncPinB))
{mot[moteurA].positionPas += mot[moteurA].rotEncincrement;} else {mot[moteurA].positionPas -= mot[moteurA].rotEncincrement;}
mot[moteurA].positionTours = mot[moteurA].positionPas / mot[moteurA].rotEncPasTour;
}
void rotencBchangeHandling() // Sortie A
{
if (!digitalRead(mot[moteurB].rotEncPinA) == !digitalRead(mot[moteurB].rotEncPinB))
{mot[moteurB].positionPas += mot[moteurB].rotEncincrement;} else {mot[moteurB].positionPas -= mot[moteurB].rotEncincrement;}
mot[moteurB].positionTours = mot[moteurB].positionPas / mot[moteurB].rotEncPasTour;
}
//===================================== Execution des commandes
void cmdExecute(String commande)
{
commande.toUpperCase();
commande.replace(" ", ""); // Supprimer les espaces
Serial.println("Execution de : " + commande);
int sepPos = commande.indexOf(",");
String cmd = commande.substring(0, sepPos); // Extraction de la commande
String cmdParam = cmd.substring(cmd.indexOf("=")+1); // Extraction du paramètre
int cmdParamInt = cmdParam.toInt();
int cmdObjectIndex = -1; // Numéro de l'objet (MOT1 etc)
if (cmd.startsWith(F("MOTALL"))) // Commande moteur MOT1:50 ou MOT1:-50 (0-100)
{
motRunAll(cmdParamInt);
}
else if (cmd.startsWith(F("MOT"))) // Commande moteur MOT1:50 ou MOT1:-50 (0-100)
{
int cmdObjectIndex = cmdExtractObjectIndex("MOT", cmd)-1;
motRun(cmdObjectIndex, cmdParamInt);
}
//--------------------------------- Pour test
else if (cmd.startsWith(F("TEST")))
{
Serial.print(F("Commande TEST"));
}
else
{
Serial.print(F("Commande inconnue!!! ")); Serial.println(commande);
}
}
/*-----------------------------------------------------------------------------
Retourne l'index de l'objet en commande qui se trouve en fin de commande
MOT2 retourne 2 MOT11 retourne 11
'*-----------------------------------------------------------------------------
*/
int cmdExtractObjectIndex(String cmd, String cmdWindex)
{
cmdWindex.replace(cmd, "");
return cmdWindex.toInt();
}
/*-----------------------------------------------------------------------
Réception de commandes depuis le moniteur
'*------------------------------------------------------------------------
*/
void serialEvent()
{
if (Serial.available())
{
cmdTexte = Serial.readString();
cmdTexte.replace("\n", "");
cmdTexte.replace("\r", "");
cmdNouvelleRecue = true;
}
} |
Partager