diff --git a/skeleton_project/source/FSM.c b/skeleton_project/source/FSM.c new file mode 100644 index 0000000..a8b0cfa --- /dev/null +++ b/skeleton_project/source/FSM.c @@ -0,0 +1,235 @@ +#include "FSM.h" +#include "timer.h" + +FSM_State* FSM_init(){ + + FSM_State* p_current_state; + + *p_current_state = CLOSED_EMPTY; + + return p_current_state; +} + +void FSM_behaviour(FSM_State* p_current_state){ + + // Ikke tatt hensyn til Stop (timer-cases) og Obstruction enda + + switch(*p_current_state) { + // RETNING FULLHET DØR OBSTRUKSJON + + case UP_EMPTY: + // opp tom lukket false + elevio_motorDirection(DIRN_UP); + elevio_doorOpenLamp(0); + break; + case UP_UNEMPTY: + // opp utom lukket false + elevio_motorDirection(DIRN_UP); + elevio_doorOpenLamp(0); + break; + case DOWN_EMPTY: + // ned tom lukket false + elevio_motorDirection(DIRN_DOWN); + elevio_doorOpenLamp(0); + break; + case DOWN_UNEMPTY: + // ned utom lukket false + elevio_motorDirection(DIRN_DOWN); + elevio_doorOpenLamp(0); + break; + case OPEN_EMPTY: + // stille tom åpen false + elevio_motorDirection(DIRN_STOP); + elevio_doorOpenLamp(1); + break; + case OPEN_UNEMPTY: + // stille utom åpen false + elevio_motorDirection(DIRN_STOP); + elevio_doorOpenLamp(1); + break; + case CLOSED_EMPTY: + // stille tom lukket false + elevio_motorDirection(DIRN_STOP); + elevio_doorOpenLamp(0); + break; + case CLOSED_UNEMPTY: + // stille utom lukket false + elevio_motorDirection(DIRN_STOP); + elevio_doorOpenLamp(0); + break; + case BLOCKED_EMPTY: + // stille tom åpen true + elevio_motorDirection(DIRN_STOP); + elevio_doorOpenLamp(1); + break; + case BLOCKED_UNEMPTY: + // stille utom åpen true + elevio_motorDirection(DIRN_STOP); + elevio_doorOpenLamp(1); + break; + } +} + +void FSM_transition(FSM_State* p_current_state, FSM_Trigger trigger, Queue* p_main_queue, time_t* time){ + + switch(*p_current_state) { + + case UP_EMPTY: + switch(trigger){ + case STOP: + *p_current_state = OPEN_UNEMPTY; + break; + default: + *p_current_state = UP_EMPTY; + } + break; + + case UP_UNEMPTY: + switch(trigger){ + case STOP: + *p_current_state = CLOSED_UNEMPTY; + break; + case ENTERED_FLOOR: + queue_remove_all(p_main_queue, elevio_floorSensor()); + + if (queue_has_off_requests(p_main_queue)){ + *p_current_state = CLOSED_UNEMPTY; + } else { + *p_current_state = CLOSED_EMPTY; + } + + break; + default: + *p_current_state = UP_UNEMPTY; + } + break; + + case DOWN_EMPTY: + switch(trigger){ + case ENTERED_FLOOR: + *p_current_state = OPEN_UNEMPTY; + break; + default: + *p_current_state = DOWN_EMPTY; + } + break; + + case DOWN_UNEMPTY: + switch(trigger){ + case STOP: + *p_current_state = CLOSED_UNEMPTY; + break; + case ENTERED_FLOOR: + queue_remove_all(p_main_queue, elevio_floorSensor()); + + if (queue_has_off_requests(p_main_queue)){ + *p_current_state = CLOSED_UNEMPTY; + } else { + *p_current_state = CLOSED_EMPTY; + } + + break; + default: + *p_current_state = DOWN_UNEMPTY; + } + break; + + case OPEN_EMPTY: + switch(trigger){ + case OBSTRUCTION: + // do something + break; + case TIMER: + // implement waiting for stop-button release before starting timer + //start_timer(time); + if (time_limit(time)){ + *p_current_state = CLOSED_EMPTY; + } + break; + default: + *p_current_state = OPEN_EMPTY; + } + break; + + case OPEN_UNEMPTY: + switch(trigger){ + case STOP: + *p_current_state = OPEN_EMPTY; + break; + case OBSTRUCTION: + // do something + break; + case TIMER: + start_timer(time); + if (time_limit(time)){ + *p_current_state = CLOSED_UNEMPTY; + } + break; + default: + *p_current_state = OPEN_UNEMPTY; + } + break; + + case CLOSED_EMPTY: + switch(trigger){ + case UP: + *p_current_state = UP_EMPTY; + break; + case DOWN: + *p_current_state = DOWN_EMPTY; + break; + case STAY: + *p_current_state = OPEN_UNEMPTY; + break; + default: + *p_current_state = CLOSED_EMPTY; + } + break; + + case CLOSED_UNEMPTY: + switch(trigger){ + case STOP: + if (elevio_floorSensor()!=-1){ + *p_current_state = OPEN_EMPTY; + } + break; + case UP: + *p_current_state = UP_UNEMPTY; + break; + case DOWN: + *p_current_state = DOWN_UNEMPTY; + break; + default: + *p_current_state = CLOSED_UNEMPTY; + } + break; + + case BLOCKED_EMPTY: + switch(trigger){ + case TIMER: + // waits for obstr release + // start_timer(time); + if (time_limit(time)){ + *p_current_state = CLOSED_EMPTY; + } + break; + default: + *p_current_state = BLOCKED_EMPTY; + } + break; + + case BLOCKED_UNEMPTY: + switch(trigger){ + case TIMER: + // waits for obstr release + // start_timer(time); + if (time_limit(time)){ + *p_current_state = CLOSED_UNEMPTY; + } + break; + default: + *p_current_state = BLOCKED_UNEMPTY; + } + break; + } +} \ No newline at end of file diff --git a/skeleton_project/source/state_machine.h b/skeleton_project/source/FSM.h similarity index 59% rename from skeleton_project/source/state_machine.h rename to skeleton_project/source/FSM.h index 9287479..27b3bc2 100644 --- a/skeleton_project/source/state_machine.h +++ b/skeleton_project/source/FSM.h @@ -2,8 +2,9 @@ #include #include #include -#include #include "driver/elevio.h" +#include "queue.h" +#include "timer.h" // enums typedef enum { @@ -21,7 +22,7 @@ typedef enum { CLOSED_EMPTY, CLOSED_UNEMPTY -} State; +} FSM_State; typedef enum { STOP, @@ -31,8 +32,9 @@ typedef enum { UP, DOWN, STAY -} Trigger; +} FSM_Trigger; // functions -void behaviour(State* p_current_state); -void transition(State* p_current_state, Trigger trigger); \ No newline at end of file +FSM_State* FSM_init(void); +void FSM_behaviour(FSM_State* p_current_state); +void FSM_transition(FSM_State* p_current_state, FSM_Trigger trigger, Queue* p_main_queue, time_t* start_time); \ No newline at end of file diff --git a/skeleton_project/source/main.c b/skeleton_project/source/main.c index 130e70e..77c75f6 100644 --- a/skeleton_project/source/main.c +++ b/skeleton_project/source/main.c @@ -4,12 +4,13 @@ #include #include #include "driver/elevio.h" -#include "state_machine.h" +#include "FSM.h" #include "queue.h" #include "run.h" #include "button.h" int main() { + elevio_init(); Queue* p_main_queue = queue_init(); diff --git a/skeleton_project/source/state_machine.c b/skeleton_project/source/state_machine.c deleted file mode 100644 index 704da33..0000000 --- a/skeleton_project/source/state_machine.c +++ /dev/null @@ -1,347 +0,0 @@ -#include "state_machine.h" - -void behaviour(State* p_current_state){ - - switch(*p_current_state) { - // RETNING FULLHET DØR OBSTRUKSJON - case UP_EMPTY: - // opp tom lukket false - elevio_motorDirection(DIRN_UP); - break; - case UP_UNEMPTY: - // opp utom lukket false - elevio_motorDirection(DIRN_UP); - break; - case DOWN_EMPTY: - printf("State: %d\n",*p_current_state); //test - // ned tom lukket false - elevio_motorDirection(DIRN_DOWN); - break; - case DOWN_UNEMPTY: - // ned utom lukket false - elevio_motorDirection(DIRN_DOWN); - break; - case OPEN_EMPTY: - // stille tom åpen false - elevio_motorDirection(DIRN_STOP); - break; - case OPEN_UNEMPTY: - // stille utom åpen false - elevio_motorDirection(DIRN_STOP); - break; - case CLOSED_EMPTY: - // stille tom lukket false - elevio_motorDirection(DIRN_STOP); - break; - case CLOSED_UNEMPTY: - // stille utom lukket false - elevio_motorDirection(DIRN_STOP); - break; - case BLOCKED_EMPTY: - // stille tom åpen true - elevio_motorDirection(DIRN_STOP); - break; - case BLOCKED_UNEMPTY: - // stille utom åpen true - elevio_motorDirection(DIRN_STOP); - break; - } -} - - -void transition(State* p_current_state, Trigger trigger){ - - switch(*p_current_state) { - - case UP_EMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case UP_UNEMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case DOWN_EMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case DOWN_UNEMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case OPEN_EMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case OPEN_UNEMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case CLOSED_EMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case CLOSED_UNEMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case BLOCKED_EMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - - case BLOCKED_UNEMPTY: - switch(trigger){ - case STOP: - // do something - break; - case ENTERED_FLOOR: - // do something - break; - case OBSTRUCTION: - // do something - break; - case TIMER: - // do something - break; - case UP: - // do something - break; - case DOWN: - // do something - break; - case STAY: - // do something - break; - default: - // do something - ; - } - break; - } - -} \ No newline at end of file diff --git a/skeleton_project/source/timer.c b/skeleton_project/source/timer.c new file mode 100644 index 0000000..eb67385 --- /dev/null +++ b/skeleton_project/source/timer.c @@ -0,0 +1,22 @@ +#include "timer.h" + +/* +// ikke peker +time_t start_time; + +void start_timer(){ + start_time = time(NULL); +} + +bool time_limit(){ + return (difftime(time(NULL), start_time) >= 3); +} +*/ + +void start_timer(time_t* start_time){ + *start_time = time(NULL); +} + +bool time_limit(time_t* start_time){ + return (difftime(time(NULL), *start_time) >= 3); +} \ No newline at end of file diff --git a/skeleton_project/source/timer.h b/skeleton_project/source/timer.h new file mode 100644 index 0000000..b68c4bd --- /dev/null +++ b/skeleton_project/source/timer.h @@ -0,0 +1,9 @@ +#pragma once +#include "time.h" +#include +#include +#include + + +void start_timer(time_t* start_time); +bool time_limit(time_t* start_time); \ No newline at end of file