Skip to content

Commit

Permalink
Merge pull request #5 from angrycompany16/elisa_branch
Browse files Browse the repository at this point in the history
Elisa branch
  • Loading branch information
elisaakj authored Feb 26, 2024
2 parents f5aa47a + 7a3183a commit 114094e
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 353 deletions.
235 changes: 235 additions & 0 deletions skeleton_project/source/FSM.c
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include "driver/elevio.h"
#include "queue.h"
#include "timer.h"

// enums
typedef enum {
Expand All @@ -21,7 +22,7 @@ typedef enum {

CLOSED_EMPTY,
CLOSED_UNEMPTY
} State;
} FSM_State;

typedef enum {
STOP,
Expand All @@ -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);
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);
3 changes: 2 additions & 1 deletion skeleton_project/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#include <time.h>
#include <unistd.h>
#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();
Expand Down
Loading

0 comments on commit 114094e

Please sign in to comment.