Advertisement
Mathieubl

[Arduino] Autonomus car [Not Working]

Jun 26th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 15.89 KB | None | 0 0
  1. #include <VirtualWire.h>
  2. #include <Grove_I2C_Motor_Driver.h> //import de la bibliotheque pour controller les moteurs
  3. #include "PinChangeInt.h" //import bibliotheque pour avoir plus que 2 attach interupt
  4.  
  5. #define I2C_ADDRESS 0x0f //adresse de la carte pour controller les moteurs
  6.  
  7. #define captLL 8 // pin du capteur gauche
  8. #define captL 7 // pin du capteur milieu gauche
  9. #define captR 3 // pin du capteur milieu droite
  10. #define captRR 4 // pin du capteur droite
  11. #define captTR 2 // pin du capteur de tour de la roue
  12. #define captLV 1 // pin du capteur de ligne verte /!\Analogique/!\
  13.  
  14. #define speede 60 //vitesse de croisiere
  15. #define speederap 70 //vitesse rapide (lors de la rotation notament (si on le met a la vitesse de croisiere, il bloque))
  16.  
  17. #define tempstour 2000 //temp max que dure le fait de tourner
  18. #define tempsav 300 //temp que la voiture avance avant de tourner
  19. #define timerpcorrd 200 //timer de la petite correction
  20. #define timergcorrd 200 //timer de la grosse correction
  21.  
  22. #define startpoint 8 //point de départ du path finding
  23. #define finishpoint 12 //point d'arriver du path finding
  24. #define usepathfinding true //var qui definit si on utilise la path finding
  25.  
  26. #define lvstep 300 //Step qui permet au capteur de décider si il y a un obstacle ou pas
  27.  
  28. String msg;
  29.  
  30. /*
  31.    Structure qui contient un chemain, ca taille et
  32.    la distance de ce dernier
  33. */
  34. typedef struct pathdist pathdist;
  35. struct pathdist {
  36.   int path[10];
  37.   int taillepath;
  38.   double dist;
  39. };
  40.  
  41. /*
  42.    Variable ou est stocker la map.
  43.    C'est composer d'une masterliste qui contient 16
  44.    liste fille qui contiennt toute 4 nombre.
  45.    Les liste fille represente une intersection. Leurs
  46.    place dans la masterliste represente quelle intesection
  47.    c'est et les nombre dedan represente ce qu'il y a a
  48.    droite, en bas, a gauche et en haut.
  49. */
  50. int point[16][4] = {
  51.   { 2,  6,  0,  0}, // 1
  52.   { 3,  7,  1,  0}, // 2
  53.   { 4,  8,  2,  0}, // 3
  54.   { 5, 10,  3,  0}, // 4
  55.   { 0, 11,  4,  0}, // 5
  56.   { 7,  9,  0,  1}, // 6
  57.   { 8,  0,  6,  2}, // 7
  58.   { 0,  0,  7,  3}, // 8
  59.   {10, 14,  0,  6}, // 9
  60.   {11, 13,  9,  4}, //10
  61.   { 0, 16, 10,  5}, //11
  62.   {13, 15,  0,  0}, //12
  63.   { 0,  0, 12, 10}, //13
  64.   {15,  0,  0,  9}, //14
  65.   {16,  0, 14, 12}, //15
  66.   { 0,  0, 15, 13}  //16
  67. };
  68.  
  69. /*
  70.    Contient la taille du chemain correspondant a celui
  71.    dans point
  72. */
  73. int distance[16][4]  = {
  74.   {    297,     210,       0,       0}, // 1
  75.   {297 * 2,     210,     297,       0}, // 2
  76.   {297 * 2,     210, 297 * 2,       0}, // 3
  77.   {    297, 210 * 3, 2 * 297,       0}, // 4
  78.   {      0, 3 * 210,     297,       0}, // 5
  79.   {    297, 2 * 210,       0,     210}, // 6
  80.   {2 * 297,       0,     297,     210}, // 7
  81.   {      0,       0, 2 * 297,     210}, // 8
  82.   {5 * 294, 2 * 210,       0, 2 * 210}, // 9
  83.   {    297,     210, 5 * 297, 3 * 210}, //10
  84.   {      0, 2 * 210,     297, 3 * 210}, //11
  85.   {4 * 297,     210,       0,       0}, //12
  86.   {      0,       0, 4 * 297,     210}, //13
  87.   {    297,       0,       0, 2 * 210}, //14
  88.   {5 * 297,       0,     297,     210}, //15
  89.   {      0,       0, 5 * 297, 2 * 210}  //16
  90. };
  91.  
  92. /*
  93.    Etats des capteurs.
  94.    (ll = gauche
  95.    l = milieu gauche
  96.    r = milieu droite
  97.    rr = droite
  98.    tr = capteur de toure
  99.    lv = capteur de ligne verte)
  100. */
  101. volatile boolean ll;
  102. volatile boolean l;
  103. volatile boolean r;
  104. volatile boolean rr;
  105. volatile boolean tr;
  106. volatile int lv;
  107.  
  108. volatile char etat; //etat de la voiture (a = avancer, d = tourner 90° droite, g = tourner 90° gauche, r = correction droite, l = correction gauche, s = stop, n = avancer avant de tourner a droite, m = la meme a gauche)
  109. volatile int timer; //temps durant lequelle l'etat ne change pas saufe si on doit tourner
  110.  
  111. char cleanpath[10]; //chemain propre. Suite d'instruction simple que la voiture va comprendre
  112. int dirtypath[10]; //Chemain sale. C'est la ou on a étape par étape les point par lequelle la voiture passe
  113. int pathsize = 0; //Taille de ce chemain
  114. int pathstep = -1; //ou on en est dans ce chemain
  115. boolean changestep = false; //permet de n'encrémenter le step qu'une fois par virage et pas en boucle pdt le virage
  116.  
  117. char dir = 'g'; //direction de base de la voiture
  118.  
  119. boolean changetr = false; //Permet de détecter quand le capteur du pinTR change d'etat
  120. boolean changelv = false; //Permet de détecter quand le capteur du pinLV change d'etat
  121.  
  122. volatile int countlv = 0; //Nombre de cran compter pour la dist qu'on cherche de la ligne verte
  123. volatile int countgene = 0; //Nombre de cran compter pour la dist qu'on cherche avant la ligne verte
  124.  
  125. int ptdepart = 8;
  126. int ptarrive = 7;
  127. int taille = 20;
  128. int dist = 20;
  129.  
  130. /*
  131.    Fonction setup ou on definit le serial, les moteurs,
  132.    les capteurs, l'etat et le path finding si besoin
  133. */
  134. void setup() {
  135.   Serial.begin(9600);
  136.  
  137.   Serial.println("Setup");
  138.   Serial.println("Launch Motor.begin");
  139.   Motor.begin(I2C_ADDRESS);
  140.   Serial.println("Motor.begin launched");
  141.   vw_setup(2000);//radio comm setup
  142.   vw_set_tx_pin(6);//pin setup
  143.  
  144.   pinMode(captLL, INPUT);
  145.   pinMode(captL, INPUT);
  146.   pinMode(captR, INPUT);
  147.   pinMode(captRR, INPUT);
  148.   pinMode(captTR, INPUT);
  149.  
  150.   PCintPort::attachInterrupt(captRR, ai, CHANGE);
  151.   PCintPort::attachInterrupt(captLL, ai, CHANGE);
  152.   PCintPort::attachInterrupt(captR, ai, CHANGE);
  153.   PCintPort::attachInterrupt(captL, ai, CHANGE);
  154.  
  155.   PCintPort::attachInterrupt(captTR, ai_tr, CHANGE);
  156.  
  157.   etat = 'a';
  158.  
  159.   Serial.println("Finding path...");
  160.  
  161.   findpath();
  162.  
  163.   Serial.println("Setup done.");
  164. }
  165.  
  166. /*
  167.    Fonction loop.
  168. */  
  169. void loop() {
  170.  
  171.   Serial.print(analogRead(captLV));
  172.   Serial.print(" - ");
  173.   Serial.print(timer < millis());
  174.   Serial.print(" - ");
  175.   Serial.print(countlv);
  176.   Serial.print(" - ");
  177.   Serial.print(etat);
  178.   Serial.print(" - ");
  179.   Serial.println(pathstep);
  180.   Serial.flush();
  181.  
  182.   msg = (String) "11" + (String) "22" + (String) ptdepart + (String) taille + (String) dist, (String) ptarrive;
  183.  
  184.   /*Serial.println(" - 1");
  185.   Serial.flush();*/
  186.  
  187.   /*Serial.print(rr);
  188.   Serial.print(" - ");
  189.   Serial.print(r);
  190.   Serial.print(" - ");
  191.   Serial.print(l);
  192.   Serial.print(" - ");
  193.   Serial.println(ll);*/
  194.  
  195.   /*ll = digitalRead(captLL);
  196.   l = digitalRead(captL);
  197.   r = digitalRead(captR);
  198.   rr = digitalRead(captRR);
  199.   tr = digitalRead(captTR);
  200.   lv = analogRead(captLV);*/
  201.  
  202.   setOrder();
  203.  
  204.   /*Serial.println(" - 2");
  205.   Serial.flush();*/
  206.  
  207.   order();
  208.  
  209.   /*Serial.println(" - 3");
  210.   Serial.flush();*/
  211.  
  212.   distcount();
  213.  
  214.   /*Serial.println(" - 4");
  215.   Serial.flush();*/
  216.  
  217.   filllv();
  218.  
  219.   /*Serial.println(" - 5");
  220.   Serial.flush();*/
  221.  
  222.   for (int i = 0; i < 3; i++){
  223.     sendInfo((String) i + msg);
  224.   }
  225.  
  226.   /*Serial.println(" - 6");
  227.   Serial.flush();*/
  228.  
  229. }
  230.  
  231. void sendInfo(String msg){
  232.   unsigned char payload[20];
  233.   msg.getBytes(payload, 20);
  234.   vw_send((uint8_t *)payload, msg.length()); // On envoie le message
  235.   vw_wait_tx(); // On attend la fin de l'envoi
  236. }
  237.  
  238. /*
  239.    Fonction qui setup le path finding si nessessaire.
  240.    Elle definit le clearpath
  241. */
  242. void findpath() {
  243.  
  244.   if (usepathfinding) {
  245.     Serial.println("Searching path...");
  246.    
  247.     pathdist path;
  248.     defpath(&path);
  249.  
  250.     path = pathsearch(startpoint, finishpoint, 7, &path);
  251.     pathtrad(&path, cleanpath);
  252.     pathsize = path.taillepath;
  253.  
  254.     for (int i = 0; i < path.taillepath; i++) {
  255.       Serial.print(dirtypath[i]);
  256.       if (i != path.taillepath - 1) {
  257.         Serial.print(", ");
  258.       } else {
  259.         Serial.println();
  260.       }
  261.     }
  262.    
  263.     Serial.println("Path found");
  264.  
  265.   } else {
  266.    
  267.     cleanpath[0] = 'h';
  268.     cleanpath[1] = 'h';
  269.     cleanpath[2] = 'd';
  270.     cleanpath[3] = 'h';
  271.     cleanpath[4] = 'h';
  272.     cleanpath[5] = 'h';
  273.     cleanpath[6] = 'd';
  274.     cleanpath[7] = 'h';
  275.     cleanpath[8] = 'h';
  276.     cleanpath[9] = 'h';
  277.    
  278.     pathsize = 7;
  279.    
  280.     dirtypath[0] = 8;
  281.     dirtypath[1] = 7;
  282.     dirtypath[2] = 6;
  283.     dirtypath[3] = 9;
  284.     dirtypath[4] = 14;
  285.     dirtypath[5] = 15;
  286.     dirtypath[6] = 12;
  287.     dirtypath[7] = 0;
  288.     dirtypath[8] = 0;
  289.     dirtypath[9] = 0;
  290.   }
  291. }
  292.  
  293. /*
  294.    Permet de compter la distance entre deux obstacle
  295. */
  296. void distcount() {
  297.   lv = analogRead(captLV);
  298.  
  299.   if (lv > lvstep && !changelv) {
  300.     changelv = true;
  301.     if (countlv > 0) {
  302.       filllv();
  303.     }
  304.     countlv = 0;
  305.   } else if (!(lv > lvstep) && changelv) {
  306.     changelv = false;
  307.   }
  308.  
  309.   if (tr && !changetr) {
  310.     countgene++;
  311.     changetr = true;
  312.     if (lv < lvstep) {
  313.       countlv++;
  314.     }
  315.   } else if (!tr && changetr && lv < lvstep) {
  316.     changetr = false;
  317.   }
  318. }
  319.  
  320. /*
  321.    Choisis l'action a effectuer en fonction de l'etat
  322. */
  323. void order() {
  324.   switch(etat) {
  325.     case 'd':
  326.     case 'g':
  327.       tourner();
  328.       break;
  329.     case 'r':
  330.       corredroit();
  331.       break;
  332.     case 'l':
  333.       corregauche();
  334.       break;
  335.     case 's':
  336.       stopp();
  337.       break;
  338.     default:
  339.       avancer();
  340.   }
  341. }
  342.  
  343. /*
  344.    Permer de changer l'etat de la voiture en fonction
  345.    des capteurs et de l'etat précedent
  346. */
  347. void setOrder() {
  348.   boolean timee = timer < millis();
  349.  
  350.   if (etat == 'm' && timee ) {
  351.  
  352.     etat = 'd';
  353.     timer = millis() + tempstour;
  354.     timee = false;
  355.  
  356.   } else if (etat == 'n' && timee ) {
  357.     etat = 'g';
  358.     timer = millis() + tempstour;
  359.     timee = false;
  360.  
  361.   }
  362.  
  363.   if (changestep && !( etat == 'm' || etat == 'n' ) ) {
  364.     changestep = false;
  365.   } else if (!changestep && ( etat == 'm' || etat == 'n' ) ) {
  366.     pathstep++;
  367.     changestep = true;
  368.   }
  369.  
  370.  
  371.   if ( !ll && !l && !r && !rr && timee ) {
  372.     // Forward
  373.     etat = 'a';
  374.     timee = false;
  375.  
  376.   } else if ( !ll && !l && !r && rr && timee ) {
  377.     //Big correction right
  378.     etat = 'm';
  379.     timer = millis() + tempsav;
  380.     /*etat = 'r';
  381.     timer = millis() + timergcorrd;*/
  382.     timee = false;
  383.  
  384.   } else if ( ll && !l && !r && !rr && timee ) {
  385.     //Big correction left
  386.     etat = 'n';
  387.     timer = millis() + tempsav;
  388.     /*etat = 'l';
  389.     timer = millis() + timergcorrd;*/
  390.     timee = false;
  391.  
  392.   } else if ( !ll && !l && r && !rr && timee ) {
  393.     // Little correction right
  394.     etat = 'r';
  395.     timer = millis() + timerpcorrd;
  396.     timee = false;
  397.  
  398.   } else if ( !ll && l && !r && !rr && timee ) {
  399.     // Little correction left
  400.     etat = 'l';
  401.     timer = millis() + timerpcorrd;
  402.     timee = false;
  403.  
  404.   } else if ( r && rr && etat != 'g' && etat != 'd' ) {
  405.     // Turn right
  406.     etat = 'm';
  407.     timer = millis() + tempsav;
  408.     timee = false;
  409.  
  410.   } else if ( ll && l && ( !r || !rr ) && etat != 'g' && etat != 'd' ) {
  411.     // Turn left
  412.     etat = 'n';
  413.     timer = millis() + tempsav;
  414.     timee = false;
  415.  
  416.   }
  417. }
  418.  
  419.  
  420. /*
  421.    Fonction récursive permettant de trouver le meilleur
  422.    chemain
  423. */
  424. pathdist pathsearch(int frompt, int topt, int tour, pathdist *pathtemp) {
  425.   pathdist path;
  426.   defpath(&path);
  427.   copiepath(&path, pathtemp);
  428.  
  429.   path.path[path.taillepath] = frompt;
  430.   path.taillepath++;
  431.  
  432.   if (frompt == topt) {
  433.     return path;
  434.   }
  435.  
  436.   else if (tour == 0) {
  437.     path.dist = 9999;
  438.     return path;
  439.   }
  440.  
  441.   else {
  442.     pathdist lst[4];
  443.     int lstsize = 0;
  444.     for (int i = 0; i < 4; i++) {
  445.       if (point[frompt - 1][i] != 0) {
  446.         path.dist += distance[frompt - 1][i];
  447.         lst[lstsize] = pathsearch(point[frompt - 1][i], topt, tour - 1, &path);
  448.         path.dist -= distance[frompt - 1][i];
  449.         lstsize++;
  450.       }
  451.     }
  452.  
  453.     copiepath(&path, &lst[0]);
  454.  
  455.     for (int i = 1; i < lstsize; i++) {
  456.       if ( lst[i].dist < path.dist ) {
  457.         copiepath(&path, &lst[i]);
  458.       }
  459.     }
  460.     return path;
  461.   }
  462. }
  463.  
  464. /*
  465.    Traduit le chemain du path finding (point 1 puis 2 puis 4) en
  466.    direction
  467. */
  468. void pathtrad(pathdist *path, char order[10]) {
  469.   for (int i = 0; i < 10; i++) {
  470.     dirtypath[i] = path->path[i];
  471.   }
  472.   for (int i = 0; i < path->taillepath - 1; i++) {
  473.     if (point[path->path[i] - 1][0] == path->path[i + 1]) {
  474.       order[i] = traddir('d');
  475.     } else if (point[path->path[i] - 1][1] == path->path[i + 1]) {
  476.       order[i] = traddir('b');
  477.     } else if (point[path->path[i] - 1][2] == path->path[i + 1]) {
  478.       order[i] = traddir('g');
  479.     } else {
  480.       order[i] = traddir('h');
  481.     }
  482.   }
  483. }
  484.  
  485.  
  486. /*
  487.    Traduit les dirrection par rapport a la map en dirrectionpar rapport
  488.    a la voiture
  489. */
  490. char traddir(char dire) {
  491.   switch (dir) {
  492.     case 'h':
  493.       switch (dire) {
  494.         case 'h':
  495.           return 'h';
  496.           break;
  497.         case 'd':
  498.           dir = 'd';
  499.           return 'd';
  500.           break;
  501.         case 'b':
  502.           //exit(0);
  503.           break;
  504.         case 'g':
  505.           dir = 'g';
  506.           return 'g';
  507.           break;
  508.       }
  509.       break;
  510.     case 'd':
  511.       switch (dire) {
  512.         case 'h':
  513.           dir = 'h';
  514.           return 'g';
  515.           break;
  516.         case 'd':
  517.           return 'h';
  518.           break;
  519.         case 'b':
  520.           dir = 'b';
  521.           return 'd';
  522.           break;
  523.         case 'g':
  524.           //exit(0);
  525.           break;
  526.       }
  527.       break;
  528.     case 'b':
  529.       switch (dire) {
  530.         case 'h':
  531.           //exit(0);
  532.           break;
  533.         case 'd':
  534.           dir = 'd';
  535.           return 'g';
  536.           break;
  537.         case 'b':
  538.           return 'h';
  539.           break;
  540.         case 'g':
  541.           dir = 'g';
  542.           return 'd';
  543.           break;
  544.       }
  545.       break;
  546.     case 'g':
  547.       switch (dire) {
  548.         case 'h':
  549.           dir = 'h';
  550.           return 'd';
  551.           break;
  552.         case 'd':
  553.           //exit(0);
  554.           break;
  555.         case 'b':
  556.           dir = 'b';
  557.           return 'g';
  558.           break;
  559.         case 'g':
  560.           return 'h';
  561.           break;
  562.       }
  563.       break;
  564.   }
  565. }
  566.  
  567. void ai() {
  568.   //noInterrupts();
  569.   rr = digitalRead(captRR);
  570.   r = digitalRead(captR);
  571.   l = digitalRead(captL);
  572.   ll = digitalRead(captLL);
  573.  
  574.   if ( ( etat == 'd'  && l ) || ( etat == 'g' && r ) ) {
  575.     etat == 'a';
  576.     rr = r = l = ll = 0;
  577.   }
  578.   //interrupts();
  579.   //setOrder();
  580. }
  581.  
  582. void ai_tr() {
  583.   tr = digitalRead(captTR);
  584.  
  585.   distcount();
  586. }
  587.  
  588.  
  589. /*
  590.    Permet de definir des valeurs par défaut dans une var path
  591. */
  592. void defpath(pathdist *path) {
  593.   for (int i = 0; i < 10; i++) {
  594.     path->path[i] = 0;
  595.   }
  596.   path->taillepath = 0;
  597.   path->dist = 0;
  598. }
  599.  
  600. /*
  601.    Permet de copier la deusieme var path dans la 1ere
  602. */
  603. void copiepath(pathdist *patha, pathdist *pathb) {
  604.   for (int i = 0; i < 10; i++) {
  605.     patha->path[i] = pathb->path[i];
  606.   }
  607.   patha->taillepath = pathb->taillepath;
  608.   patha->dist = pathb->dist;
  609. }
  610.  
  611. void filllv() {
  612.   ptdepart = cleanpath[pathstep];
  613.   ptarrive = cleanpath[pathstep + 1];
  614.   taille = countlv;
  615.   dist = countgene - countlv;
  616.  
  617. }
  618.  
  619. /*
  620.    Fonction permettant de savoir ou tourner lors d'une
  621.    intersection
  622. */
  623. void tourner() {
  624.   countgene = 0;
  625.   if (pathstep >= pathsize) {
  626.     etat = 's';
  627.     stopp();
  628.     timer = 9999;
  629.   } else {
  630.     Serial.print("           ");
  631.     Serial.print(pathstep);
  632.     Serial.print(" Direction : ");
  633.     Serial.println(cleanpath[pathstep]);
  634.     if (cleanpath[pathstep] == 'h') {
  635.       avancer();
  636.       etat = 'a';
  637.     } else if (cleanpath[pathstep] == 'd') {
  638.       tournerd();
  639.       etat = 'd';
  640.     } else if (cleanpath[pathstep] == 'g') {
  641.       tournerg();
  642.       etat = 'g';
  643.     }
  644.   }
  645. }
  646.  
  647. /*
  648.    Permer de tourner a gauche
  649. */
  650. void tournerg() {
  651.   Motor.speed(MOTOR1, speederap);
  652.   Motor.speed(MOTOR2, -speederap);
  653. }
  654.  
  655. /*
  656.    Permer de tourner a droite
  657. */
  658. void tournerd() {
  659.   Motor.speed(MOTOR1, -speederap);
  660.   Motor.speed(MOTOR2, speederap);
  661. }
  662.  
  663. /*
  664.    Permet d'avancer
  665. */
  666. void avancer() {
  667.   Motor.speed(MOTOR1, speede);
  668.   Motor.speed(MOTOR2, speede);
  669. }
  670.  
  671. /*
  672.    permet de ce corriger a gauche
  673. */
  674. void corregauche() {
  675.   Motor.speed(MOTOR1, speede);
  676.   Motor.speed(MOTOR2, -speede);
  677. }
  678.  
  679. /*
  680.    permet de ce corriger a droite
  681. */
  682. void corredroit() {
  683.   Motor.speed(MOTOR1, -speede);
  684.   Motor.speed(MOTOR2, speede);
  685. }
  686.  
  687. /*
  688.    permet au lolipanzer de s'arreter
  689. */
  690. void stopp() {
  691.   Motor.speed(MOTOR1, 0);
  692.   Motor.speed(MOTOR2, 0);
  693.   exit(0);
  694. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement