Etape 63 : Code
OK - Soo - lorsque vous travaillez avec du code, il est idéal d’utiliser quelque chose comme Github pour assurer le suivi des révisions. Je ne fais pas cela. Ainsi, j’ai écrit accidentellement au cours de la dernière version de travail du code. J’ai la dernière version enregistrée, mais je ne sais pas comment il est. C’est probablement un peu buggé.
Mais, puisque aucun de vous ne sera probablement construire ce truc existe et le lit est actuellement en morceaux et non opérationnel, je vais me contenter d’afficher ce que j’ai.
Je partage aussi code d’essai pour contrôler manuellement les moteurs. Si vous cherchez à ce projet pour interfacer avec un Arduino pour les très gros moteurs grâce à un contrôleur de moteur Alltrax, ce code sera plus utile pour vous de toute façon.
Dernière version enregistrée du code de test Robot lit :
/ * Cet exemple de code est dans le domaine public. * / //establish accélérateur pins int leftThrottle = 3 ; LED connectée à la broche numérique 9 int rightThrottle = 5 ; LED connectée à la broche numérique 9 //establish solénoïde pins int leftOn = 7 ; LED connectée à la broche numérique 9 int rightOn = 8 ; LED connectée à la broche numérique 9 //establish contacteur pins int leftReverse = 9 ; int rightReverse = 10 ; La vitesse du lecteur envoyée à la manette des gaz int gospeed = 86 ; volatile int foserious = 0 ; AVANT int FrontLeftSnd = 30 ; int FrontLeftRcv = 31 ; int FrontCenterSnd = 32 ; int FrontCenterRcv = 33 ; int FrontRightSnd = 34 ; int FrontRightRcv = 35 ; CÔTÉ droit int Side1LeftSnd = 36 ; int Side1LeftRcv = 37 ; int Side1CenterSnd = 38 ; int Side1CenterRcv = 39 ; int Side1RightSnd = 40 ; int Side1RightRcv = 41 ; DOS int BackLeftSnd = 42 ; int BackLeftRcv = 43 ; int BackCenterSnd = 44 ; int BackCenterRcv = 45 ; int BackRightSnd = 46 ; int BackRightRcv = 47 ; LEFT SIDE int Side2LeftSnd = 49 ; int Side2LeftRcv = 48 ; int Side2CenterSnd = 51 ; int Side2CenterRcv = 50 ; int Side2RightSnd = 53 ; int Side2RightRcv = 52 ; Tableau de tous les noms de broche d’entrée et de sortie. Utilisé plus tard pour lire tous les capteurs dans une boucle for. int [] SensorOutputs = {FrontLeftSnd, FrontCenterSnd, FrontRightSnd, Side1LeftSnd, Side1CenterSnd, Side1RightSnd, BackLeftSnd, BackCenterSnd, BackRightSnd, Side2LeftSnd, Side2CenterSnd, Side2RightSnd} ; int [] SensorInputs = {FrontLeftRcv, FrontCenterRcv, FrontRightRcv, Side1LeftRcv, Side1CenterRcv, Side1RightRcv, BackLeftRcv, BackCenterRcv, BackRightRcv, Side2LeftRcv, Side2CenterRcv, Side2RightRcv} ; int dontgo = 0 ; nombre de capteurs total int SensorCount = 12 ; int nothingHappening = 0 ; int realCloseLike = 0 ; int goingforward = 0 ; int goingbackward = 0 ; int goingright = 0 ; int goingleft = 0 ; int backhit = 0 ; int fronthit = 0 ; int righthit = 0 ; int lefthit = 0 ; int amountToMove = 1000 ; int choisi ; void setup() {cli (); //stop interruptions TCCR1A = 0; / / set ensemble TCCR1A Registre à 0 TCCR1B = 0; / / Idem pour TCCR1B TCNT1 = 0; //initialize valeur de compteur à 0 / / set compare match Registre des incréments de 1 hz OCR1A = 512; / / = (16 * 10 ^ 6) / (1 * 1024) - 1 (doit être < 65536) / / activer le mode CTC TCCR1B | = (1 << WGM12); / / Set CS10 et CS12 bits pour 1024 Prédiviseur TCCR1B | = (1 << CS12) | (1 << CS10) ; activer les interruptions de minuterie comparer TIMSK1 | = (1 << OCIE1A) ; SEI (); //allow interruptions Serial.begin(9600) ; pinMode (leftOn, sortie) ; pinMode (rightOn, sortie) ; pinMode (leftReverse, sortie) ; pinMode (rightReverse, sortie) ; Assurez-vous que l’alimentation est coupée digitalWrite (leftOn, faible) ; digitalWrite (rightOn, faible) ; digitalWrite (leftReverse, basse) ; digitalWrite (rightReverse, basse) ; capteur pare-chocs broches pinMode (18, INPUT) ; pinMode (19, entrée) ; Capteur de distance définie de sortie broches pinMode (FrontLeftSnd, sortie) ; broche 30 pinMode (FrontCenterSnd, sortie) ; broche 32 pinMode (FrontRightSnd, sortie) ; broche 34 pinMode (Side1LeftSnd, sortie) ; broche 36 pinMode (Side1CenterSnd, sortie) ; broche 38 pinMode (Side1RightSnd, sortie) ; broche 40 pinMode (BackLeftSnd, sortie) ; broche 42 pinMode (BackCenterSnd, sortie) ; broche 44 pinMode (BackRightSnd, sortie) ; broche 46 pinMode (Side2LeftSnd, sortie) ; broche 49 pinMode (Side2CenterSnd, sortie) ; broche 51 pinMode (Side2RightSnd, sortie) ; capteur de distance broche 53 //Set d’entrée des broches pinMode (entrée, FrontLeftRcv) ; broche 31 pinMode (entrée, FrontCenterRcv) ; broche 33 pinMode (entrée, FrontRightRcv) ; broche 35 pinMode (entrée, Side1LeftRcv) ; broche 37 pinMode (entrée, Side1CenterRcv) ; broche 39 pinMode (entrée, Side1RightRcv) ; broche 41 pinMode (entrée, BackLeftRcv) ; broche 43 pinMode (entrée, BackCenterRcv) ; broche 45 pinMode (entrée, BackRightRcv) ; broche 47 pinMode (entrée, Side2LeftRcv) ; code PIN 48 pinMode (entrée, Side2CenterRcv) ; broche 50 pinMode (entrée, Side2RightRcv) ; broche //And 52 attendre un moment delay(3000) ; } void loop() {lookAllAround() ; moveRobot() ; delay(amountToMove) ; slowstop() ; delay(1500);} ISR(TIMER1_COMPA_vect) {//Interrupt à fréquence de 1kHz à mesurer interrupteur reed / / génère l’onde du pouls de fréquence 8kHz/2 = 4 kHz (dure deux cycles pour pleine onde-bascule haut puis basculer en bas) si (lefthit == 0 & & righthit == 0 & & fronthit == 0 & & backhit == 0) {if(digitalRead(18) == HIGH) {hardstop() ; Serial.println(goingbackward) ; Serial.println(goingforward) ; Serial.println(goingleft) ; Serial.println(goingright) ; Serial.println ("FUCK YEAH!") ; if(goingright == 1) {righthit = 1; goingright = 0;} if(goingleft == 1) {lefthit = 1; goingleft = 0;} if(goingbackward == 1) {backhit = 1; goingbackward = 0;} if(goingforward == 1) {fronthit = 1; goingforward = 0;}} } Si (lefthit == 0 & & righthit == 0 & & fronthit == 0 & & backhit == 0) {if(digitalRead(19) == HIGH) {hardstop() ; Serial.println (« FUCK NO!"); Serial.println(goingbackward) ; Serial.println(goingforward) ; Serial.println(goingleft) ; Serial.println(goingright) ; if(goingbackward == 1) {backhit = 1; goingbackward = 0;} if(goingforward == 1) {fronthit = 1; goingforward = 0;} if(goingright == 1) {righthit = 1; goingright = 0;} if(goingleft == 1) {lefthit = 1; goingleft = 0;}} }} void moveRobot() {//first voir si frappé--si frappé tout en se déplaçant dans l’une des directions - auto-pick inverse / /--dans le cas contraire choisir au hasard if(backhit == 1) {amountToMove = 1000 ; backhit = 0; fronthit = 0; lefthit = 0; righthit = 0; pris = 0;} d’autre if(fronthit == 1) {amountToMove = 1000 ; backhit = 0; fronthit = 0; lefthit = 0; righthit = 0; pris = 1;} d’autre if(lefthit == 1) {amountToMove = 500 ; backhit = 0; fronthit = 0; lefthit = 0; righthit = 0; pris = 2;} d’autre if(righthit == 1) {amountToMove = 500 ; backhit = 0; fronthit = 0; lefthit = 0 ; righthit = 0 ; a = 3 ; } else {cueillies = random(3) ; Serial.println ("WTF !?"); amountToMove = 1000 ; } switch(picked) {case 0: goingforward = 1; goingbackward = 0; goingright = 0; goingleft = 0; forwards() ; Serial.println ("aller vers l’avant") ; rupture ; cas 1: goingforward = 0 ; goingbackward = 1 ; goingright = 0 ; goingleft = 0 ; Backwards() ; Serial.println ("go back") ; rupture ; cas 2: goingforward = 0 ; goingbackward = 0 ; goingright = 1 ; goingleft = 0 ; Right() ; Serial.println ("aller droit") ; rupture ; case 3: goingforward = 0 ; goingbackward = 0 ; goingright = 0 ; goingleft = 1 ; Left() ; Serial.println ("aller à gauche") ; rupture ; } delay(1) ; délai entre les lectures de stabilité} void forwards() {//activate le contacteur marche arrière digitalWrite (leftReverse, HIGH) delay(50) digitalWrite (rightReverse, HIGH); delay(100) ; //activate le solénoïdes digitalWrite (leftOn, HIGH); digitalWrite (rightOn, HIGH); //take un souffle delay(500) ; //engage l’accélérateur analogWrite (rightThrottle, gospeed); analogWrite (leftThrottle, gospeed);} void backwards() {//activate le solénoïdes digitalWrite (leftOn, HIGH); digitalWrite (rightOn, HIGH); //take un souffle delay(500); / / engager la manette des gaz analogWrite (rightThrottle gospeed) ; analogWrite (leftThrottle, gospeed) ; } void right() {if(dontgo == 0) {//activate le solénoïdes digitalWrite (leftOn, HIGH) digitalWrite (rightOn, HIGH); //activate le contacteur marche arrière digitalWrite (rightReverse, HIGH); //take un delay(500) de souffle ; //engage la manette des gaz analogWrite (rightThrottle, gospeed); analogWrite (leftThrottle, gospeed);} //reset la variable dontgo = 0;} void left() {if(dontgo == 0) {//activate le solénoïdes digitalWrite (leftOn, HIGH) digitalWrite (rightOn, HIGH) //activate le contacteur marche arrière digitalWrite (leftReverse, HIGH); //take un souffle delay(500) ; //engage la manette des gaz analogWrite (rightThrottle gospeed) ; analogWrite (leftThrottle, gospeed) ; } //reset la variable dontgo = 0 ; } void slowstop() {pour (int fadeValue = gospeed ; fadeValue > = 0; fadeValue-=5) {/ / définit la valeur (gamme de 0 à 255): analogWrite (rightThrottle, fadeValue); analogWrite (leftThrottle, fadeValue); / / attendre 30 millisecondes Voir la gradation delay(500) effet;} digitalWrite (leftOn, faible); digitalWrite (rightOn, faible); digitalWrite (leftReverse, LOW); digitalWrite (rightReverse, basse); int goingforward = 0; int goingbackward = 0; int goingright = 0; int goingleft = 0; delay(2000) ; lookAllAround() ; delay(1000);} void hardstop() {pour (int fadeValue = gospeed ; fadeValue > = 0; fadeValue-=5) {/ / définit la valeur (gamme de 0 à 255): analogWrite (rightThrottle, fadeValue) ; analogWrite (leftThrottle, fadeValue) ; Attendez 30 millisecondes Voir la gradation altDelay(50) effet ; } digitalWrite (leftOn, faible) ; digitalWrite (rightOn, faible) ; digitalWrite (leftReverse, basse) ; digitalWrite (rightReverse, basse) ; } void altDelay(int x) {pour (unsignedInt j’ai = 0; j’ai < = x; i ++) {delayMicroseconds(1000);}} void lookAllAround() {longue durée, pouces, cm; pour (int thisPin = 0; thisPin < 1; thisPin ++) {digitalWrite (SensorOutputs [thisPin], faible); delayMicroseconds(2) ; delayMicroseconds(12) ; digitalWrite (SensorOutputs [thisPin], LOW); digitalWrite (SensorOutputs [thisPin], élevé), durée = pulseIn (SensorInputs [thisPin], HIGH); digitalWrite (SensorOutputs [thisPin], faible); / / convertir le temps en un pouces de distance = microsecondsToInches(duration) ; Serial.Print(SensorOutputs[thisPin]) ; Serial.Print(":") ; Serial.Print(inches) ; Serial.println ("in") ; //Check et vérifier de nouveau / / if (pouces > 10 & & pouces < 25) {/ / realCloseLike = 0; / / for (int checkagain = 0; checkagain < 3; checkagain ++) {/ / delay(100); / / digitalWrite (SensorOutputs [thisPin], faible); / / delayMicroseconds(2); / / digitalWrite (SensorOutputs [thisPin], élevé); / / delayMicroseconds(12); / / digitalWrite (SensorOutputs [thisPin], faible); / / / / durée = pulseIn (SensorInputs [thisPin], élevé); / / pouces = microsecondsToInches(duration); / / digitalWrite (SensorOutputs [thisPin], faible); / / / / delay(100); / / / / si (pouces > 10 & & pouces < 25) {/ / / / Serial.print(SensorOutputs[thisPin]); / / Serial.print (" : "); Serial.Print(inches) ; Serial.println ("in") ; / / realCloseLike = realCloseLike + 1 ; / / if (realCloseLike > 2) {/ / dontgo = 1; / /} / /} / /} / /} //Takes environ 1/2 seconde pour vérifier tous les capteurs @ 50uS delay(100) ; microsecondsToInches(long microseconds) longue}} {/ / selon la fiche technique de la parallaxe pour le PING))), il y a / / 73,746 microsecondes par pouce (c'est-à-dire son voyage à 1130 pieds / / / seconde). Cela donne la distance parcourue par le ping, sortant / / et le retour, donc on divise par 2 pour obtenir la distance de l’obstacle. Voir : < a href = « http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf » >< un href = "http://www.parallax.com/dl/docs/prod/acc/28015-PI... < / a » > http://www.parallax.com/dl/docs/prod/acc/28015-PI...</a>> revenir microsecondes / 74 / 2 ; }
Exemple de code de test de contrôle moteur :
/ * Cet exemple de code est dans le domaine public. * / //establish accélérateur pins int leftThrottle = 3 ; LED connectée à la broche numérique 9 int rightThrottle = 5 ; LED connectée à la broche numérique 9 //establish solénoïde pins int leftOn = 7 ; LED connectée à la broche numérique 9 int rightOn = 8 ; LED connectée à la broche numérique 9 //establish contacteur pins int leftReverse = 9 ; int rightReverse = 10 ; La vitesse du lecteur envoyée à la manette des gaz int gospeed = 82 ; int nothingHappening = 0 ; int realCloseLike = 0 ; int goingforward = 0 ; int goingbackward = 0 ; int goingright = 0 ; int goingleft = 0 ; int backhit = 0 ; int fronthit = 0 ; int righthit = 0 ; int lefthit = 0 ; int amountToMove = 1000 ; int timeToWait = 30000 ; int choisi ; void setup() {cli (); //stop interruptions TCCR1A = 0; / / set ensemble TCCR1A Registre à 0 TCCR1B = 0; / / Idem pour TCCR1B TCNT1 = 0; //initialize valeur de compteur à 0 / / set compare match Registre des incréments de 1 hz OCR1A = 512; / / = (16 * 10 ^ 6) / (1 * 1024) - 1 (doit être < 65536) / / activer le mode CTC TCCR1B | = (1 << WGM12); / / Set CS10 et CS12 bits pour 1024 Prédiviseur TCCR1B | = (1 << CS12) | (1 << CS10) ; activer les interruptions de minuterie comparer TIMSK1 | = (1 << OCIE1A) ; SEI (); //allow interruptions Serial.begin(9600) ; pinMode (leftOn, sortie) ; pinMode (rightOn, sortie) ; pinMode (leftReverse, sortie) ; pinMode (rightReverse, sortie) ; Assurez-vous que l’alimentation est coupée digitalWrite (leftOn, faible) ; digitalWrite (rightOn, faible) ; digitalWrite (leftReverse, basse) ; digitalWrite (rightReverse, basse) ; capteur pare-chocs broches pinMode (18, INPUT) ; pinMode (19, entrée) ; Et attendez un moment delay(3000) ; } void loop() {left() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; backwards() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; forwards() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; right() ; delay(amountToMove) ; slowstop() ; delay(timeToWait); / / / right() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; forwards() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; backwards() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; left() ; delay(amountToMove) ; slowstop() ; delay(timeToWait); / / / forwards() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; backwards() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; forwards() ; delay(amountToMove) ; slowstop() ; delay(timeToWait) ; backwards() ; delay(amountToMove) ; slowstop() ; delay(timeToWait);} ISR(TIMER1_COMPA_vect) {//Interrupt à fréquence de 1kHz à mesurer interrupteur reed / / génère l’onde du pouls de fréquence 8kHz/2 = 4 kHz (dure deux cycles pour pleine onde-bascule haut puis basculer en bas) si (lefthit == 0 & & righthit == 0 & & fronthit == 0 & & backhit == 0) {if(digitalRead(18) == HIGH) {hardstop() ; Serial.println(goingbackward) ; Serial.println(goingforward) ; Serial.println(goingleft) ; Serial.println(goingright) ; Serial.println ("FUCK YEAH!") ; if(goingright == 1) {righthit = 1; goingright = 0;} if(goingleft == 1) {lefthit = 1; goingleft = 0;} if(goingbackward == 1) {backhit = 1; goingbackward = 0;} if(goingforward == 1) {fronthit = 1; goingforward = 0;}} } Si (lefthit == 0 & & righthit == 0 & & fronthit == 0 & & backhit == 0) {if(digitalRead(19) == HIGH) {hardstop() ; Serial.println(goingbackward) ; Serial.println(goingforward) ; Serial.println(goingleft) ; Serial.println(goingright) ; Serial.println (« FUCK NO!") ; if(goingbackward == 1) {backhit = 1; goingbackward = 0;} if(goingforward == 1) {fronthit = 1; goingforward = 0;} if(goingright == 1) {righthit = 1; goingright = 0;} if(goingleft == 1) {lefthit = 1; goingleft = 0;}} }} void moveRobot() {//first voir si frappé--si frappé tout en se déplaçant dans l’une des directions - auto-pick inverse / /--dans le cas contraire choisir au hasard if(backhit == 1) {amountToMove = 1000 ; backhit = 0; fronthit = 0; lefthit = 0; righthit = 0; pris = 0;} d’autre if(fronthit == 1) {amountToMove = 1000 ; backhit = 0; fronthit = 0; lefthit = 0; righthit = 0; pris = 1;} d’autre if(lefthit == 1) {amountToMove = 500 ; backhit = 0; fronthit = 0; lefthit = 0; righthit = 0; pris = 2;} d’autre if(righthit == 1) {amountToMove = 500 ; backhit = 0; fronthit = 0; lefthit = 0 ; righthit = 0 ; a = 3 ; } else {cueillies = random(3) ; Serial.println ("WTF !?"); amountToMove = 1000 ; } switch(picked) {case 0: goingforward = 1; goingbackward = 0; goingright = 0; goingleft = 0; forwards() ; Serial.println ("aller vers l’avant") ; rupture ; cas 1: goingforward = 0 ; goingbackward = 1 ; goingright = 0 ; goingleft = 0 ; Backwards() ; Serial.println ("go back") ; rupture ; cas 2: goingforward = 0 ; goingbackward = 0 ; goingright = 1 ; goingleft = 0 ; Right() ; Serial.println ("aller droit") ; rupture ; case 3: goingforward = 0 ; goingbackward = 0 ; goingright = 0 ; goingleft = 1 ; Left() ; Serial.println ("aller à gauche") ; rupture ; } delay(1) ; délai entre les lectures de stabilité} void forwards() {//activate le contacteur marche arrière digitalWrite (leftReverse, HIGH) delay(50) digitalWrite (rightReverse, HIGH); delay(100) ; //activate le solénoïdes digitalWrite (leftOn, HIGH); digitalWrite (rightOn, HIGH); //take un souffle delay(500) ; //engage l’accélérateur analogWrite (rightThrottle, gospeed); analogWrite (leftThrottle, gospeed);} void backwards() {//activate le solénoïdes digitalWrite (leftOn, HIGH); digitalWrite (rightOn, HIGH); //take un souffle delay(500); / / engager la manette des gaz analogWrite (rightThrottle gospeed) ; analogWrite (leftThrottle, gospeed) ; } void right() {//activate le solénoïdes digitalWrite (leftOn, HIGH) digitalWrite (rightOn, HIGH); //activate le contacteur marche arrière digitalWrite (rightReverse, HIGH); //take un delay(500) de souffle ; //engage la manette des gaz analogWrite (rightThrottle, gospeed); analogWrite (leftThrottle, gospeed);} void left() {//activate le solénoïdes digitalWrite (leftOn, HIGH) digitalWrite (rightOn, HIGH); //activate le contacteur marche arrière digitalWrite (leftReverse, HIGH); //take un delay(500) de souffle ; //engage la manette des gaz analogWrite (rightThrottle, gospeed); analogWrite (leftThrottle gospeed) ; } void slowstop() {pour (int fadeValue = gospeed ; fadeValue > = 0; fadeValue-=5) {/ / définit la valeur (gamme de 0 à 255): analogWrite (rightThrottle, fadeValue); analogWrite (leftThrottle, fadeValue); / / attendre 30 millisecondes Voir la gradation delay(500) effet;} digitalWrite (leftOn, faible); digitalWrite (rightOn, faible); digitalWrite (leftReverse, basse); digitalWrite (rightReverse, LOW); int goingforward = 0; int goingbackward = 0; int goingright = 0; int goingleft = 0;} void hardstop() {pour (int fadeValue = gospeed ; fadeValue > = 0; fadeValue-=5) {/ / définit la valeur (gamme de 0 à 255) : analogWrite (rightThrottle, fadeValue) ; analogWrite (leftThrottle, fadeValue) ; Attendez 30 millisecondes Voir la gradation altDelay(50) effet ; } digitalWrite (leftOn, faible) ; digitalWrite (rightOn, faible) ; digitalWrite (leftReverse, basse) ; digitalWrite (rightReverse, basse) ; } void altDelay(int x) {pour (unsigned int i = 0; j’ai < = x; i ++) {delayMicroseconds(1000);}}