From 16772d4001e0e3be31c55566c918937c9b78c6e3 Mon Sep 17 00:00:00 2001 From: elisaakj Date: Mon, 19 Feb 2024 15:52:24 +0100 Subject: [PATCH 1/2] implemented trigger switch-cases --- skeleton_project/source/FSM.c | 219 +++++++++++ .../source/{state_machine.h => FSM.h} | 15 +- skeleton_project/source/main.c | 22 +- skeleton_project/source/state_machine.c | 347 ------------------ 4 files changed, 246 insertions(+), 357 deletions(-) create mode 100644 skeleton_project/source/FSM.c rename skeleton_project/source/{state_machine.h => FSM.h} (56%) delete mode 100644 skeleton_project/source/state_machine.c diff --git a/skeleton_project/source/FSM.c b/skeleton_project/source/FSM.c new file mode 100644 index 0000000..7af28ae --- /dev/null +++ b/skeleton_project/source/FSM.c @@ -0,0 +1,219 @@ +#include "FSM.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){ + + 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: + // do something + 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: + // do something + 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: + // do something + break; + default: + *p_current_state = BLOCKED_EMPTY; + } + break; + + case BLOCKED_UNEMPTY: + switch(trigger){ + case TIMER: + // do something + 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 56% rename from skeleton_project/source/state_machine.h rename to skeleton_project/source/FSM.h index c431de5..b869063 100644 --- a/skeleton_project/source/state_machine.h +++ b/skeleton_project/source/FSM.h @@ -4,9 +4,9 @@ #include #include #include "driver/elevio.h" +#include "queue.h" -// enums -typedef enum State { +typedef enum FSM_State { UP_EMPTY, UP_UNEMPTY, @@ -21,9 +21,9 @@ typedef enum State { CLOSED_EMPTY, CLOSED_UNEMPTY -} State; +} FSM_State; -typedef enum Trigger { +typedef enum FSM_Trigger { STOP, ENTERED_FLOOR, OBSTRUCTION, @@ -31,8 +31,9 @@ typedef enum Trigger { 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); \ No newline at end of file diff --git a/skeleton_project/source/main.c b/skeleton_project/source/main.c index a41a6bf..d9e2eb1 100644 --- a/skeleton_project/source/main.c +++ b/skeleton_project/source/main.c @@ -4,7 +4,7 @@ #include #include #include "driver/elevio.h" -#include "state_machine.h" +#include "FSM.h" #include "queue.h" @@ -15,6 +15,23 @@ int main(){ Queue* p_main_queue = queue_init(); + while(1) { + run(); + nanosleep(&(struct timespec){0, 20*1000*1000}, NULL); + + if (elevio_stopButton()) { + break; + } + } + + queue_deinit(p_main_queue); + */ + + /* + elevio_init(); + + Queue* p_main_queue = queue_init(); + queue_add(p_main_queue, (Request){2, true, true}); queue_add(p_main_queue, (Request){1, false, false}); queue_add(p_main_queue, (Request){1, true, false}); @@ -28,8 +45,7 @@ int main(){ queue_print(p_main_queue); - nanosleep(&(struct timespec){0, 20*1000*1000}, NULL); - } + nanosleep(&(struct timespec){0, 20*1000*1000}, NULL); */ return 0; diff --git a/skeleton_project/source/state_machine.c b/skeleton_project/source/state_machine.c deleted file mode 100644 index f8cf1d6..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 From 708e10c9d9688cb3630ed3b9767ae375113f8203 Mon Sep 17 00:00:00 2001 From: elisaakj Date: Tue, 20 Feb 2024 16:03:39 +0100 Subject: [PATCH 2/2] timer module implemented --- skeleton_project/source/FSM.c | 26 +++++++++++++++++++++----- skeleton_project/source/FSM.h | 4 ++-- skeleton_project/source/main.c | 15 +++++++++++++++ skeleton_project/source/timer.c | 22 ++++++++++++++++++++++ skeleton_project/source/timer.h | 9 +++++++++ 5 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 skeleton_project/source/timer.c create mode 100644 skeleton_project/source/timer.h diff --git a/skeleton_project/source/FSM.c b/skeleton_project/source/FSM.c index 7af28ae..a8b0cfa 100644 --- a/skeleton_project/source/FSM.c +++ b/skeleton_project/source/FSM.c @@ -1,4 +1,5 @@ #include "FSM.h" +#include "timer.h" FSM_State* FSM_init(){ @@ -69,7 +70,7 @@ void FSM_behaviour(FSM_State* p_current_state){ } } -void FSM_transition(FSM_State* p_current_state, FSM_Trigger trigger, Queue* p_main_queue){ +void FSM_transition(FSM_State* p_current_state, FSM_Trigger trigger, Queue* p_main_queue, time_t* time){ switch(*p_current_state) { @@ -139,7 +140,11 @@ void FSM_transition(FSM_State* p_current_state, FSM_Trigger trigger, Queue* p_ma // do something break; case TIMER: - // do something + // 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; @@ -155,7 +160,10 @@ void FSM_transition(FSM_State* p_current_state, FSM_Trigger trigger, Queue* p_ma // do something break; case TIMER: - // do something + start_timer(time); + if (time_limit(time)){ + *p_current_state = CLOSED_UNEMPTY; + } break; default: *p_current_state = OPEN_UNEMPTY; @@ -199,7 +207,11 @@ void FSM_transition(FSM_State* p_current_state, FSM_Trigger trigger, Queue* p_ma case BLOCKED_EMPTY: switch(trigger){ case TIMER: - // do something + // waits for obstr release + // start_timer(time); + if (time_limit(time)){ + *p_current_state = CLOSED_EMPTY; + } break; default: *p_current_state = BLOCKED_EMPTY; @@ -209,7 +221,11 @@ void FSM_transition(FSM_State* p_current_state, FSM_Trigger trigger, Queue* p_ma case BLOCKED_UNEMPTY: switch(trigger){ case TIMER: - // do something + // waits for obstr release + // start_timer(time); + if (time_limit(time)){ + *p_current_state = CLOSED_UNEMPTY; + } break; default: *p_current_state = BLOCKED_UNEMPTY; diff --git a/skeleton_project/source/FSM.h b/skeleton_project/source/FSM.h index b869063..680388c 100644 --- a/skeleton_project/source/FSM.h +++ b/skeleton_project/source/FSM.h @@ -2,9 +2,9 @@ #include #include #include -#include #include "driver/elevio.h" #include "queue.h" +#include "timer.h" typedef enum FSM_State { UP_EMPTY, @@ -36,4 +36,4 @@ typedef enum FSM_Trigger { // functions 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); \ No newline at end of file +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 d9e2eb1..abea5b9 100644 --- a/skeleton_project/source/main.c +++ b/skeleton_project/source/main.c @@ -6,10 +6,25 @@ #include "driver/elevio.h" #include "FSM.h" #include "queue.h" +#include "timer.h" int main(){ + /* + // test av timer + time_t start_time; + + printf("Starting timer"); + start_timer(&start_time); + + while(!time_limit(&start_time)){ + printf("ikke enda"); + } + + printf("der ja"); + */ + /* elevio_init(); 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