Skip to content

Commit

Permalink
Merge pull request #6 from angrycompany16/david_branch_2
Browse files Browse the repository at this point in the history
It's a bit bruteforced, but stop button, obstruction and better queue…
  • Loading branch information
angrycompany16 authored Mar 4, 2024
2 parents 3c4fc7f + 7d5c63f commit 83f3753
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 47 deletions.
3 changes: 2 additions & 1 deletion skeleton_project/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"button.h": "c",
"stdio.h": "c",
"queue.h": "c",
"stdlib.h": "c"
"stdlib.h": "c",
"cmath": "c"
}
}
58 changes: 35 additions & 23 deletions skeleton_project/source/FSM.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ FSM* FSM_init(){
if (p_fsm!= NULL) {
p_fsm->current_state = CLOSED_EMPTY;
p_fsm->current_floor = 2;
p_fsm->direction = DIR_NONE;
p_fsm->moving = false;
} else {
printf("Error allocating memory for button\n");
Expand All @@ -21,65 +22,80 @@ void FSM_deinit(FSM* p_fsm) {
}

void FSM_behaviour(FSM* p_fsm, time_t* p_timer, Queue* p_main_queue){

// Ikke tatt hensyn til Stop (timer-cases) og Obstruction enda

switch(p_fsm->current_state) {
case UP_EMPTY:
p_fsm->moving = true;
p_fsm->direction = DIR_UP;
elevio_motorDirection(DIRN_UP);
elevio_doorOpenLamp(0);
break;
case UP_UNEMPTY:
p_fsm->moving = true;
p_fsm->direction = DIR_UP;
elevio_motorDirection(DIRN_UP);
elevio_doorOpenLamp(0);
break;
case DOWN_EMPTY:
p_fsm->moving = true;
p_fsm->direction = DIR_DOWN;
elevio_motorDirection(DIRN_DOWN);
elevio_doorOpenLamp(0);
break;
case DOWN_UNEMPTY:
p_fsm->moving = true;
p_fsm->direction = DIR_DOWN;
elevio_motorDirection(DIRN_DOWN);
elevio_doorOpenLamp(0);
break;
case OPEN_EMPTY:
if (time_limit(p_timer)){
printf("timer empty\n");
printf("Empty elevator, timer ran out\n");
FSM_transition(p_fsm, TIMER, p_main_queue, p_timer);
}
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
elevio_doorOpenLamp(1);
break;
case OPEN_UNEMPTY:
if (time_limit(p_timer)){
printf("timer unempty\n");
printf("Unempty elevator, timer ran out\n");
FSM_transition(p_fsm, TIMER, p_main_queue, p_timer);
}
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
elevio_doorOpenLamp(1);
break;
case CLOSED_EMPTY:
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
elevio_doorOpenLamp(0);
break;
case CLOSED_UNEMPTY:
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
elevio_doorOpenLamp(0);
break;
case BLOCKED_EMPTY:
if (time_limit(p_timer)){
printf("Blocked empty elevator, timer ran out\n");
FSM_transition(p_fsm, TIMER, p_main_queue, p_timer);
}
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
elevio_doorOpenLamp(1);
break;
case BLOCKED_UNEMPTY:
if (time_limit(p_timer)){
printf("Blocked unempty elevator, timer ran out\n");
FSM_transition(p_fsm, TIMER, p_main_queue, p_timer);
}
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
elevio_doorOpenLamp(1);
break;
Expand Down Expand Up @@ -107,11 +123,12 @@ void FSM_transition(FSM* p_fsm, FSM_Trigger trigger, Queue* p_main_queue, time_t
switch(trigger){
case STOP:
p_fsm->current_state = CLOSED_UNEMPTY;
queue_clear(p_main_queue);
break;
case ENTERED_FLOOR:
queue_remove_all(p_main_queue, elevio_floorSensor());

if (queue_has_off_requests(p_main_queue)){
if (queue_query(p_main_queue, -1, ANY, TRUE)){
reset_timer(p_timer);
p_fsm->current_state = OPEN_UNEMPTY;
} else {
Expand All @@ -122,7 +139,8 @@ void FSM_transition(FSM* p_fsm, FSM_Trigger trigger, Queue* p_main_queue, time_t
break;
default:
p_fsm->current_state = UP_UNEMPTY;
}reset_timer(p_timer);
}
// reset_timer(p_timer);
break;

case DOWN_EMPTY:
Expand All @@ -143,11 +161,12 @@ void FSM_transition(FSM* p_fsm, FSM_Trigger trigger, Queue* p_main_queue, time_t
switch(trigger){
case STOP:
p_fsm->current_state = CLOSED_UNEMPTY;
queue_clear(p_main_queue);
break;
case ENTERED_FLOOR:
queue_remove_all(p_main_queue, elevio_floorSensor());

if (queue_has_off_requests(p_main_queue)){
if (queue_query(p_main_queue, -1, ANY, TRUE)){
reset_timer(p_timer);
p_fsm->current_state = OPEN_UNEMPTY;
} else {
Expand All @@ -164,27 +183,26 @@ void FSM_transition(FSM* p_fsm, FSM_Trigger trigger, Queue* p_main_queue, time_t
case OPEN_EMPTY:
switch(trigger){
case OBSTRUCTION:
// do something
p_fsm->current_state = BLOCKED_UNEMPTY;
break;
case TIMER:
// TODO: implement waiting for stop-button release before starting timer
p_fsm->current_state = CLOSED_EMPTY;
break;
default:
p_fsm->current_state = OPEN_EMPTY;
// reset_timer(p_timer);
}
break;

case OPEN_UNEMPTY:
switch(trigger){
case STOP:
p_fsm->current_state = OPEN_EMPTY;
queue_clear(p_main_queue);
reset_timer(p_timer);

break;
case OBSTRUCTION:
// do something
p_fsm->current_state = BLOCKED_UNEMPTY;
break;
case TIMER:
p_fsm->current_state = CLOSED_UNEMPTY;
Expand Down Expand Up @@ -213,11 +231,12 @@ void FSM_transition(FSM* p_fsm, FSM_Trigger trigger, Queue* p_main_queue, time_t
}
break;

case CLOSED_UNEMPTY:
case CLOSED_UNEMPTY:
switch(trigger){
case STOP:
if (elevio_floorSensor()!=-1){
p_fsm->current_state = OPEN_EMPTY;
queue_clear(p_main_queue);
reset_timer(p_timer);
}
break;
Expand All @@ -240,11 +259,7 @@ void FSM_transition(FSM* p_fsm, FSM_Trigger trigger, Queue* p_main_queue, time_t
case BLOCKED_EMPTY:
switch(trigger){
case TIMER:
// waits for obstr release
// start_timer(time);
// if (time_limit(time)){
// p_fsm->current_state = CLOSED_EMPTY;
// }
p_fsm->current_state = CLOSED_EMPTY;
break;
default:
p_fsm->current_state = BLOCKED_EMPTY;
Expand All @@ -254,11 +269,8 @@ void FSM_transition(FSM* p_fsm, FSM_Trigger trigger, Queue* p_main_queue, time_t
case BLOCKED_UNEMPTY:
switch(trigger){
case TIMER:
// waits for obstr release
// start_timer(time);
// if (time_limit(time)){
// p_fsm->current_state = CLOSED_UNEMPTY;
// }
printf("lolz");
p_fsm->current_state = CLOSED_UNEMPTY;
break;
default:
p_fsm->current_state = BLOCKED_UNEMPTY;
Expand Down
7 changes: 7 additions & 0 deletions skeleton_project/source/FSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ typedef enum {
STAY
} FSM_Trigger;

typedef enum {
DIR_NONE = -1,
DIR_DOWN,
DIR_UP,
} ELEVATOR_DIRECTION;

typedef struct {
FSM_State current_state;
int current_floor;
int direction;
bool moving;
} FSM;

Expand Down
20 changes: 9 additions & 11 deletions skeleton_project/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ int main() {
Queue* p_main_queue = queue_init();
int target_floor = -1;

Button* up_buttons[] = { button_init(), button_init(), button_init(), button_init() };
Button* down_buttons[] = { button_init(), button_init(), button_init(), button_init() };
Button* cab_buttons[] = { button_init(), button_init(), button_init(), button_init() };
Button* pp_up_buttons[] = { button_init(), button_init(), button_init(), button_init() };
Button* pp_down_buttons[] = { button_init(), button_init(), button_init(), button_init() };
Button* pp_cab_buttons[] = { button_init(), button_init(), button_init(), button_init() };
Button* p_stop_button = button_init();

while(1) {
run(&target_floor, p_main_queue, p_fsm, p_timer, up_buttons, down_buttons, cab_buttons);

if (elevio_stopButton()) {
break;
}
run(&target_floor, p_main_queue, p_fsm, p_timer, p_stop_button, pp_up_buttons, pp_down_buttons, pp_cab_buttons);

nanosleep(&(struct timespec){0, 20*1000*1000}, NULL);
}
Expand All @@ -36,10 +33,11 @@ int main() {
FSM_deinit(p_fsm);
timer_deinit(p_timer);
for (int i = 0; i < 4; i++) {
button_deinit(up_buttons[i]);
button_deinit(down_buttons[i]);
button_deinit(cab_buttons[i]);
button_deinit(pp_up_buttons[i]);
button_deinit(pp_down_buttons[i]);
button_deinit(pp_cab_buttons[i]);
}
button_deinit(p_stop_button);

return 0;

Expand Down
66 changes: 60 additions & 6 deletions skeleton_project/source/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Queue* queue_init() {
Queue* p_queue = (Queue*)malloc(sizeof(Queue));

// maybe not necessary?
if (p_queue == NULL) {
printf("Queue object could not be made\n");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -44,31 +43,86 @@ void queue_remove_all(Queue* p_queue, int floor) {
}
}

void queue_clear(Queue* p_queue) {
for (size_t i = 0; i < p_queue->youngest_queue_element; i++) {
queue_remove(p_queue, i);
i--;
}
}

void queue_print(Queue* p_queue) {
printf("Printing queue...\n");
for (size_t i = 0; i < p_queue->youngest_queue_element; i++) {
print_request(&p_queue->queue[i]);
}
}

bool queue_has_off_requests(Queue* p_queue) {
// bool queue_has_off_requests(Queue* p_queue) {
// for (size_t i = 0; i < p_queue->youngest_queue_element; i++) {
// if (p_queue->queue[i].off == true) {
// return true;
// }
// }
// return false;
// }

bool queue_query(Queue* p_queue, int floor, QueueQueryItem direction, QueueQueryItem off) {
if (floor == -1) {
if (direction == ANY) {
return queue_query_off(p_queue, off);
} else {
if (off == ANY) {
return queue_query_direction(p_queue, direction);
} else {
return queue_query_off(p_queue, off) && queue_query_direction(p_queue, direction);
}
}
} else {
if (direction == ANY) {
if (off == ANY) {
return queue_query_floor(p_queue, floor);
} else {
return queue_query_floor(p_queue, floor) && queue_query_off(p_queue, off);
}
} else {
if (off == ANY) {
return queue_query_floor(p_queue, floor) && queue_query_direction(p_queue, direction);
} else {
return queue_query_floor(p_queue, floor) && queue_query_off(p_queue, off) && queue_query_direction(p_queue, direction);
}
}
}
return false;
}

static bool queue_query_floor(Queue* p_queue, int floor) {
for (size_t i = 0; i < p_queue->youngest_queue_element; i++) {
if (p_queue->queue[i].off == true) {
if (p_queue->queue[i].floor == floor) {
return true;
}
}
return false;
}

bool queue_query(Queue* p_queue, bool up, bool off) {
static bool queue_query_direction(Queue* p_queue, bool direction) {
for (size_t i = 0; i < p_queue->youngest_queue_element; i++) {
if (p_queue->queue[i].off == off && p_queue->queue[i].up == up) {
if (p_queue->queue[i].direction == direction) {
return true;
}
}
}
return false;
}

static bool queue_query_off(Queue* p_queue, bool off) {
for (size_t i = 0; i < p_queue->youngest_queue_element; i++) {
if (p_queue->queue[i].off == off) {
return true;
}
}
return false;

}

Request* queue_find_first_off_request(Queue* p_queue) {
for (size_t i = 0; i < p_queue->youngest_queue_element; i++) {
if (p_queue->queue[i].off == true) {
Expand Down
15 changes: 13 additions & 2 deletions skeleton_project/source/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ typedef struct {
size_t youngest_queue_element;
} Queue;

typedef enum {
FALSE,
TRUE,
ANY,
} QueueQueryItem;

Queue* queue_init(void);

void queue_deinit(Queue* p_queue);
Expand All @@ -21,10 +27,15 @@ void queue_remove(Queue* p_queue, size_t pos);

void queue_remove_all(Queue* p_queue, int floor);

void queue_clear(Queue* p_queue);

void queue_print(Queue* p_queue);

bool queue_has_off_requests(Queue* p_queue);
// bool queue_has_off_requests(Queue* p_queue);

bool queue_query(Queue* p_queue, bool up, bool off);
bool queue_query(Queue* p_queue, int floor, QueueQueryItem direction, QueueQueryItem off);
static bool queue_query_floor(Queue* p_queue, int floor);
static bool queue_query_direction(Queue* p_queue, bool direction);
static bool queue_query_off(Queue* p_queue, bool off);

Request* queue_find_first_off_request(Queue* p_queue);
Loading

0 comments on commit 83f3753

Please sign in to comment.