Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: runStateMachine.c
- * Author: max
- *
- * Created on July 28, 2016, 12:43 PM
- */
- #include "roach.h"
- #include "runStateMachine.h"
- #include "events.h"
- #include "CalibConstants.h"
- #include "Navigation.h"
- #include <stdio.h>
- #include <timers.h>
- #define MAX_SPEED 10
- #define TIME_BACK_AWAY 3000
- #define TIME_ARC_RIGHT 3000
- void RunStateMachine(event_t currentEvent) {
- static state_t currentState = INIT;
- //which state are we in?
- printf("Iteration - State: %d\tEvent: %d\n", currentState, currentEvent);
- switch (currentState) {
- case INIT:
- Forward(MAX_SPEED);
- currentState = FORWARD;
- break;
- case FORWARD:
- switch (currentEvent) {
- case FRONT_BUMP_DOWN:
- Reverse(MAX_SPEED);
- currentState = BACK;
- break;
- case FL_BUMP_DOWN:
- RotateRight(MAX_SPEED);
- InitTimer(0, TIME_TURN_360 / 4);
- currentState = TURNRIGHT;
- break;
- case FR_BUMP_DOWN:
- RotateLeft(MAX_SPEED);
- InitTimer(0, TIME_TURN_360 / 4);
- currentState = TURNLEFT;
- break;
- case ENTERED_DARK:
- Stop();
- currentState = DARKIDLE;
- break;
- }
- break;
- case TURNLEFT:
- switch (currentEvent) {
- case TIMER_0_EXPIRED:
- ClearTimerExpired(0);
- Forward(MAX_SPEED);
- currentState = FORWARD;
- break;
- case FRONT_BUMP_DOWN:
- InitTimer(0, TIME_BACK_AWAY);
- Reverse(MAX_SPEED);
- currentState = BACK;
- break;
- case FL_BUMP_DOWN:
- RotateRight(MAX_SPEED);
- InitTimer(0, TIME_TURN_360 / 4);
- currentState = TURNRIGHT;
- break;
- case ENTERED_DARK:
- StopTimer(0);
- ClearTimerExpired(0);
- Stop();
- currentState = DARKIDLE;
- break;
- }
- break;
- case TURNRIGHT:
- switch (currentEvent) {
- case TIMER_0_EXPIRED:
- ClearTimerExpired(0);
- Forward(MAX_SPEED);
- currentState = FORWARD;
- break;
- case FRONT_BUMP_DOWN:
- InitTimer(0, TIME_BACK_AWAY);
- Reverse(MAX_SPEED);
- currentState = BACK;
- break;
- case FR_BUMP_DOWN:
- RotateLeft(MAX_SPEED);
- InitTimer(0, TIME_TURN_360 / 4);
- currentState = TURNLEFT;
- break;
- case ENTERED_DARK:
- StopTimer(0);
- ClearTimerExpired(0);
- Stop();
- currentState = DARKIDLE;
- break;
- }
- break;
- case BACK:
- switch (currentEvent) {
- case TIMER_0_EXPIRED:
- ClearTimerExpired(0);
- Forward(MAX_SPEED);
- currentState = FORWARD;
- break;
- case BACK_BUMP_DOWN:
- ArcRight(MAX_SPEED);
- InitTimer(0, 3000);
- currentState = ARCRIGHT;
- break;
- case ENTERED_DARK:
- StopTimer(0);
- ClearTimerExpired(0);
- Stop();
- currentState = DARKIDLE;
- break;
- }
- break;
- case ARCRIGHT:
- switch (currentEvent) {
- case TIMER_0_EXPIRED:
- ClearTimerExpired(0);
- Forward(MAX_SPEED);
- currentState = FORWARD;
- break;
- case FRONT_BUMP_DOWN:
- ClearTimerExpired(0);
- Reverse(MAX_SPEED);
- currentState = BACK;
- break;
- case FL_BUMP_DOWN:
- RotateRight(MAX_SPEED);
- InitTimer(0, TIME_TURN_360 / 4);
- currentState = TURNRIGHT;
- break;
- case FR_BUMP_DOWN:
- RotateLeft(MAX_SPEED);
- InitTimer(0, TIME_TURN_360 / 4);
- currentState = TURNLEFT;
- break;
- case ENTERED_DARK:
- StopTimer(0);
- ClearTimerExpired(0);
- Stop();
- currentState = DARKIDLE;
- break;
- }
- break;
- case DARKIDLE:
- switch (currentEvent) {
- case ENTERED_LIGHT:
- Forward(MAX_SPEED);
- currentState = FORWARD;
- break;
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement