Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define INTERVAL 100
- typedef enum { INIT, RUN, LEFT, RIGHT, PAUSE, STOP } State ;
- bool line_l, line_r; // ligne sur la gauche/droite (capteur infrarouge)
- bool obstacle_f; // obstacle devant (capteur ultrason)
- 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
- bool setting_up, fatal_error, success, start;
- State state;
- void setup() {
- // put your setup code here, to run once:
- }
- void detect_IR()
- {
- // capteurs infrarouges
- }
- void detect_US()
- {
- // capteurs ultrasons
- }
- void refresh()
- {
- // met à jour les globals (conditions)
- detect_IR();
- detect_US();
- }
- void transition()
- {
- if (fatal_error || success)
- {
- state = STOP;
- return;
- }
- switch (state)
- {
- case INIT:
- if (setting_up)
- {
- state = INIT;
- }
- else
- {
- if (!obstacle_f && !line_l && !line_r && !turn)
- {
- state = RUN;
- }
- else if (turn && turn_l)
- {
- state = LEFT;
- }
- else if (turn && !turn_l)
- {
- state = RIGHT;
- }
- else
- {
- state = STOP;
- }
- }
- break;
- case RUN:
- if (!line_l && !line_r && !turn && !obstacle_f)
- {
- state = RUN;
- }
- else if (line_r || (!line_l && !line_r && turn && turn_l) && !obstacle_f)
- {
- state = LEFT;
- }
- else if (line_l || (!line_l && !line_r && turn && !turn_l) && !obstacle_f)
- {
- state = RIGHT;
- }
- else if (obstacle_f)
- {
- state = PAUSE;
- }
- else
- {
- state = STOP;
- }
- break;
- case LEFT:
- if (!line_l && !line_r && !obstacle_f)
- {
- state = RUN;
- }
- else if (line_l || line_r && !obstacle_f)
- {
- state = LEFT;
- }
- else if (obstacle_f)
- {
- state = PAUSE;
- }
- else
- {
- state = STOP;
- }
- break;
- case RIGHT:
- if (!line_l && !line_r && !obstacle_f)
- {
- state = RUN;
- }
- else if (line_l || line_r && !obstacle_f)
- {
- state = RIGHT;
- }
- else if (obstacle_f)
- {
- state = PAUSE;
- }
- else
- {
- state = STOP;
- }
- break;
- case PAUSE:
- if (!line_l && !line_r && !obstacle_f)
- {
- state = RUN;
- }
- else if (line_r && !obstacle_f)
- {
- state = LEFT;
- }
- else if (line_l && !obstacle_f)
- {
- state = RIGHT;
- }
- else if (obstacle_f)
- {
- state = PAUSE;
- }
- break;
- case STOP:
- if (start)
- {
- state = INIT;
- }
- else
- {
- state = STOP;
- }
- break;
- default:
- state = STOP;
- break;
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- unsigned long t0=millis();
- refresh();
- transition();
- switch (state)
- {
- case INIT:
- break;
- case RUN:
- break;
- case LEFT:
- break;
- case RIGHT:
- break;
- case PAUSE:
- break;
- case STOP:
- break;
- default:
- break;
- }
- unsigned long elapsed=millis()-t0;
- if (elapsed>INTERVAL) printf("Excessive execution time\n");
- else delay(INTERVAL-elapsed);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement