Skip to content

Commit

Permalink
resolved merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
elisaakj committed Mar 4, 2024
2 parents ca1bc5e + 7149ce9 commit 86bc93e
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 47 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Lab work for TTK4235

In this lab we implemented a basic system for controlling an elevator in accordance with some pre-specified system requirements. The elevator was also required to pass a FAT test.

The assignment:
https://github.com/ITK-TTK4235/lab_2/
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"
}
}
61 changes: 38 additions & 23 deletions skeleton_project/source/FSM.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 @@ -20,64 +21,82 @@ 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:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, false);
p_fsm->moving = true;
p_fsm->direction = DIR_UP;
elevio_motorDirection(DIRN_UP);
break;
case UP_UNEMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, false);
p_fsm->moving = true;
p_fsm->direction = DIR_UP;
elevio_motorDirection(DIRN_UP);
break;
case DOWN_EMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, false);
p_fsm->moving = true;
p_fsm->direction = DIR_DOWN;
elevio_motorDirection(DIRN_DOWN);
break;
case DOWN_UNEMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, false);
p_fsm->moving = true;
p_fsm->direction = DIR_DOWN;
elevio_motorDirection(DIRN_DOWN);
break;
case OPEN_EMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, true);
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);
break;
case OPEN_UNEMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, true);
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);
break;
case CLOSED_EMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, false);
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
break;
case CLOSED_UNEMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, false);
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
break;
case BLOCKED_EMPTY:
<<<<<<< HEAD
=======
if (time_limit(p_timer)){
printf("Blocked empty elevator, timer ran out\n");
FSM_transition(p_fsm, TIMER, p_main_queue, p_timer);
}
>>>>>>> 7149ce90f57c397a32f4a367495aa6813fcdd36a
p_fsm->moving = false;
p_fsm->direction = DIR_NONE;
elevio_motorDirection(DIRN_STOP);
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);
break;
}
Expand Down Expand Up @@ -105,12 +124,13 @@ 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());
lamp_toggle(LAMP_CAB, elevio_floorSensor(), false);
lamp_toggle(LAMP_UP, elevio_floorSensor(), false);
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 @@ -120,7 +140,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 @@ -142,12 +163,13 @@ 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());
lamp_toggle(LAMP_CAB, elevio_floorSensor(), false);
lamp_toggle(LAMP_DOWN, elevio_floorSensor(), false);
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 @@ -163,26 +185,25 @@ 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 @@ -211,11 +232,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 @@ -239,11 +261,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 @@ -253,11 +271,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 @@ -35,9 +35,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 @@ -20,16 +20,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 @@ -38,10 +35,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
Loading

0 comments on commit 86bc93e

Please sign in to comment.