Advertisement
RenabaReD

tron 19 12 06

Dec 6th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define INTERVAL 66
  2. #define P_TURN 100 // sur 1000 // p=30 pour interv=33
  3. #include "lowlevel.hpp"
  4.  
  5. /* Constantes pour le timeout */
  6. const unsigned long MEASURE_TIMEOUT = 25000UL; // 25ms = ~8m à 340m/s
  7.  
  8. typedef struct {
  9.   bool far_l, far_r, close_l, close_r;
  10. } IR_Capture;
  11.  
  12. typedef enum { INIT, RUN, LEFT, RIGHT, PAUSE, STOP } State ;
  13. bool line_l, line_r; // ligne sur la gauche/droite (capteur infrarouge)
  14. bool obstacle_f; // obstacle devant (capteur ultrason)
  15. bool turn, turn_l; // y a une proba de tourner au cours de la course (marche aléatoire), et une proba pour savoir si on tourne à gauche ou droite
  16. bool setting_up, fatal_error, success, start;
  17. int speedL=0;
  18. int speedR=0;
  19. bool dirL = true;
  20. bool dirR = true;
  21. double prop =1.100;
  22. bool led=false;
  23. int vit=64;
  24. int rand_turn_duration = 400; // ms
  25. int compteur_tour = 0;
  26. const int distance_lim = 1700;
  27.  
  28. State state;
  29.  
  30. void setup() {
  31.   // port série
  32.   Serial.begin(115200);
  33.  
  34.   setup_motors();
  35.   setup_sensors();
  36.  
  37.  
  38.   pinMode(LED_BUILTIN, OUTPUT);
  39.   digitalWrite(LED_BUILTIN, LOW);
  40.  
  41.   state=INIT;
  42. }
  43.  
  44. IR_Capture detect_IR()
  45. {
  46.   // capteurs infrarouges
  47.   IR_Capture cap;
  48.  
  49.   return cap;
  50. }
  51.  
  52. void detect_US()
  53. {
  54.   // capteurs ultrasons
  55.   digitalWrite(PINTRIG, HIGH);
  56.   delayMicroseconds(10);
  57.   digitalWrite(PINTRIG, LOW);
  58.   long measure = pulseIn(PINECHO, HIGH, MEASURE_TIMEOUT);
  59.   //Serial.println(measure);
  60.   obstacle_f = (measure!=0 && measure<distance_lim);
  61. }
  62.  
  63. void refresh()
  64. {
  65.   // met à jour les globals (conditions)
  66.   detect_IR();
  67.   detect_US();
  68.   if (turn) {
  69.     if(compteur_tour<int(rand_turn_duration/INTERVAL)) compteur_tour++;
  70.     else {
  71.       turn=false;
  72.       compteur_tour=0;
  73.     }
  74.     //Serial.println(compteur_tour);
  75.   }
  76.   else {
  77.     turn = (random(1000)<P_TURN);
  78.     turn_l = (random(2)==1); }
  79. }
  80.  
  81. void transition()
  82. {
  83.   if (fatal_error || success)
  84.     {
  85.       state = STOP;
  86.       return;
  87.     }
  88.   switch (state)
  89.   {
  90.     case INIT:
  91.       if (setting_up) {
  92.         state = INIT;
  93.       }
  94.       else {
  95.         if (!obstacle_f && !line_l && !line_r && !turn) {
  96.           state = RUN;
  97.         }
  98.         else if (turn && turn_l) {
  99.           state = LEFT;
  100.         }
  101.         else if (turn && !turn_l) {
  102.           state = RIGHT;
  103.         } else
  104.         {
  105.           state = PAUSE;
  106.         }
  107.       }
  108.       break;
  109.     case RUN:
  110.       if (!line_l && !line_r && !turn && !obstacle_f) {
  111.         state = RUN;
  112.       }
  113.       else if (line_r || (!line_l && !line_r && turn && turn_l) && !obstacle_f) {
  114.         state = LEFT;
  115.       }
  116.       else if (line_l || (!line_l && !line_r && turn && !turn_l) && !obstacle_f) {
  117.         state = RIGHT;
  118.       }
  119.       else if (obstacle_f) {
  120.         state = PAUSE;
  121.       }
  122.       else {
  123.         state = STOP;
  124.       }
  125.       break;
  126.     case LEFT:
  127.       if (!line_l && !line_r && !obstacle_f && !turn) {
  128.         state = RUN;
  129.       }
  130.       else if ((line_r && !line_l)||(turn && turn_l)) { // else if ((line_l || line_r && !obstacle_f) || (!line_l && !line_r && turn && turn_l)) {
  131.         state = LEFT;
  132.       }
  133.       else if (obstacle_f) {
  134.         state = PAUSE;
  135.       }
  136.       else {
  137.         state = STOP;
  138.       }
  139.       break;
  140.     case RIGHT:
  141.       if (!line_l && !line_r && !obstacle_f && !turn) {
  142.         state = RUN;
  143.       }
  144.       else if ((line_l && !line_r)||(turn && !turn_l)) { // (line_l || line_r && !obstacle_f)
  145.         state = RIGHT;
  146.       }
  147.       else if (obstacle_f) {
  148.         state = PAUSE;
  149.       }
  150.       else {
  151.         state = STOP;
  152.       }
  153.       break;
  154.     case PAUSE:
  155.       if (!line_l && !line_r && !obstacle_f) {
  156.         state = RUN;
  157.       }
  158.       else if (!line_l) {
  159.         state = LEFT;
  160.       }
  161.       else if (!line_r && line_l) {
  162.         state = RIGHT;
  163.       }
  164.       else {
  165.         state = PAUSE;
  166.       }
  167.       break;
  168.     case STOP:
  169.       if (start) {
  170.         state = INIT;
  171.       }
  172.       else {
  173.         state = STOP;
  174.       }
  175.       break;
  176.     default:
  177.       state = STOP;
  178.       break;
  179.   }
  180.  
  181. }
  182.  
  183. void tron_loop() {
  184.   refresh();
  185.   transition();
  186.  
  187.   switch (state)
  188.   {
  189.     case INIT:
  190.       Serial.println(F("INIT"));
  191.       state=RUN;
  192.       break;
  193.     case RUN:
  194.       Serial.println(F("RUN"));
  195.       led=false;
  196.       dirL=true;
  197.       dirR=true;
  198.       speedL=vit;
  199.       speedR=vit;
  200.       break;
  201.     case LEFT:
  202.       Serial.println(F("LEFT"));
  203.       led=false;
  204.       dirL=false;
  205.       dirR=true;
  206.       speedL=vit;
  207.       speedR=vit;
  208.       break;
  209.     case RIGHT:
  210.       Serial.println(F("RIGHT"));
  211.       led=false;
  212.       dirL=true;
  213.       dirR=false;
  214.       speedL=vit;
  215.       speedR=vit;
  216.       break;
  217.     case PAUSE:
  218.       Serial.println(F("PAUSE"));
  219.       led=true;
  220.       speedL=0;
  221.       speedR=0;
  222.       break;
  223.     case STOP:
  224.       Serial.println(F("STOP"));
  225.       led=false;
  226.       speedL=0;
  227.       speedR=0;
  228.       break;
  229.     default:
  230.       break;
  231.   }
  232.  
  233.   write_motors(dirL,dirR,speedL,speedR);
  234.   digitalWrite(LED_BUILTIN, (led)?HIGH:LOW);
  235. }
  236.  
  237. void loop() {
  238.   unsigned long t0=millis();
  239.   tron_loop();
  240.   unsigned long elapsed=millis()-t0;
  241.   if (elapsed>INTERVAL) printf("Excessive execution time\n");
  242.   else delay(INTERVAL-elapsed);
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement