Skip to content

Commit

Permalink
finished elevator
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorstronen committed Mar 4, 2024
1 parent e44b4dc commit 8b7510f
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 38 deletions.
3 changes: 2 additions & 1 deletion skeleton_project/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"stdio.h": "c",
"queue.h": "c",
"stdlib.h": "c",
"cmath": "c"
"cmath": "c",
"lamp.h": "c"
}
}
13 changes: 5 additions & 8 deletions skeleton_project/source/FSM.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void FSM_behaviour(FSM* p_fsm, time_t* p_timer, Queue* p_main_queue){
break;
case OPEN_EMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, true);
if (time_limit(p_timer)){
if (time_limit(p_timer, 3)){
printf("Empty elevator, timer ran out\n");
FSM_transition(p_fsm, TIMER, p_main_queue, p_timer);
}
Expand All @@ -58,7 +58,7 @@ void FSM_behaviour(FSM* p_fsm, time_t* p_timer, Queue* p_main_queue){
break;
case OPEN_UNEMPTY:
lamp_toggle(LAMP_DOOR, p_fsm->current_floor, true);
if (time_limit(p_timer)){
if (time_limit(p_timer, 3)){
printf("Unempty elevator, timer ran out\n");
FSM_transition(p_fsm, TIMER, p_main_queue, p_timer);
}
Expand All @@ -79,19 +79,16 @@ void FSM_behaviour(FSM* p_fsm, time_t* p_timer, Queue* p_main_queue){
elevio_motorDirection(DIRN_STOP);
break;
case BLOCKED_EMPTY:
<<<<<<< HEAD
=======
if (time_limit(p_timer)){
if (time_limit(p_timer, 3)){
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)){
if (time_limit(p_timer, 3)){
printf("Blocked unempty elevator, timer ran out\n");
FSM_transition(p_fsm, TIMER, p_main_queue, p_timer);
}
Expand Down Expand Up @@ -169,7 +166,7 @@ void FSM_transition(FSM* p_fsm, FSM_Trigger trigger, Queue* p_main_queue, time_t
queue_remove_all(p_main_queue, elevio_floorSensor());
lamp_toggle(LAMP_CAB, elevio_floorSensor(), false);
lamp_toggle(LAMP_DOWN, elevio_floorSensor(), false);
if (queue_query(p_main_queue, -1, ANY, TRUE)){
if (queue_query(p_main_queue, -1, ANY, TRUE)) {
reset_timer(p_timer);
p_fsm->current_state = OPEN_UNEMPTY;
} else {
Expand Down
38 changes: 35 additions & 3 deletions skeleton_project/source/lamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,56 @@
void lamp_toggle(Lamp m_lamp, int floor, bool enable){
switch(m_lamp){
case LAMP_CURRENT:
while (elevio_floorSensor() != -1) {
elevio_floorIndicator(floor);
}
if (floor == -1) { break; }
if (floor == 0) { printf("lamp floor 0 set\n"); }
elevio_floorIndicator(floor);
break;
case LAMP_UP:
if (floor == -1) { break; }
elevio_buttonLamp(floor, BUTTON_HALL_UP, enable);
break;
case LAMP_DOWN:
if (floor == -1) { break; }
elevio_buttonLamp(floor, BUTTON_HALL_DOWN, enable);
break;
case LAMP_CAB:
if (floor == -1) { break; }
elevio_buttonLamp(floor, BUTTON_CAB, enable);
break;
case LAMP_STOP:
elevio_stopLamp(enable);
break;
case LAMP_DOOR:
if (floor == -1) { break; }
elevio_doorOpenLamp(enable);
break;
}
}

void lamp_disable_all() {
for (int i = 0; i < 4; i++) {
elevio_buttonLamp(i, BUTTON_HALL_UP, 0);
elevio_buttonLamp(i, BUTTON_HALL_DOWN, 0);
elevio_buttonLamp(i, BUTTON_CAB, 0);
elevio_stopLamp(0);
elevio_doorOpenLamp(0);
// elevio_buttonLamp(i, , 0);
}
}

void lamp_startup(time_t* p_timer, int* floor) {
if (time_limit(p_timer, 0.75)) {
lamp_set_all_on_floor((*floor)++);
reset_timer(p_timer);
*floor %= 4;
}
}

void lamp_set_all_on_floor(int floor) {
// printf("Set all on floor was called\n");
lamp_disable_all();
elevio_floorIndicator(floor);
elevio_buttonLamp(floor, BUTTON_HALL_UP, 1);
elevio_buttonLamp(floor, BUTTON_HALL_DOWN, 1);
elevio_buttonLamp(floor, BUTTON_CAB, 1);
}
8 changes: 7 additions & 1 deletion skeleton_project/source/lamp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include "time.h"
#include "timer.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
Expand All @@ -16,4 +18,8 @@ typedef enum{

void lamp_toggle(Lamp m_lamp, int floor, bool enable);

// L3 og L4!
void lamp_disable_all();

void lamp_startup(time_t* p_timer, int* floor);

void lamp_set_all_on_floor(int floor);
25 changes: 24 additions & 1 deletion skeleton_project/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,41 @@
// TODO: check pragma once

int main() {

elevio_init();
time_t* p_timer = timer_init();
FSM* p_fsm = FSM_init();
Queue* p_main_queue = queue_init();
int target_floor = -1;
int startup_floor = 0;

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();

/* Startup */
elevio_motorDirection(DIRN_DOWN);

while(1) {
int current_floor = elevio_floorSensor();
lamp_startup(p_timer, &startup_floor);
if (current_floor != -1) {
p_fsm->current_state = CLOSED_EMPTY;
p_fsm->current_floor = current_floor;
p_fsm->direction = DIR_NONE;
p_fsm->moving = false;
elevio_motorDirection(DIRN_STOP);
lamp_disable_all();
lamp_toggle(LAMP_CURRENT, current_floor, true);
break;
}

nanosleep(&(struct timespec){0, 20*1000*1000}, NULL);
}
printf("Elevator initialized!\n");
// printf("Current floor: %d", p_fsm->current_floor);

/* Run */
while(1) {
run(&target_floor, p_main_queue, p_fsm, p_timer, p_stop_button, pp_up_buttons, pp_down_buttons, pp_cab_buttons);

Expand Down
7 changes: 7 additions & 0 deletions skeleton_project/source/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,32 @@ void queue_print(Queue* p_queue) {
bool queue_query(Queue* p_queue, int floor, QueueQueryItem direction, QueueQueryItem off) {
if (floor == -1) {
if (direction == ANY) {
// printf("floor: ANY, direction: ANY, off: ANY");
return queue_query_off(p_queue, off);
} else {
if (off == ANY) {
// printf("floor: ANY, direction: something, off: ANY");
return queue_query_direction(p_queue, direction);
} else {
// printf("floor: ANY, direction: something, off: something");
return queue_query_off(p_queue, off) && queue_query_direction(p_queue, direction);
}
}
} else {
if (direction == ANY) {
if (off == ANY) {
// printf("floor: something, direction: ANY, off: ANY");
return queue_query_floor(p_queue, floor);
} else {
// printf("floor: something, direction: ANY, off: something");
return queue_query_floor(p_queue, floor) && queue_query_off(p_queue, off);
}
} else {
if (off == ANY) {
// printf("floor: something, direction: something, off: ANY");
return queue_query_floor(p_queue, floor) && queue_query_direction(p_queue, direction);
} else {
// printf("floor: something, direction: something, off: something");
return queue_query_floor(p_queue, floor) && queue_query_off(p_queue, off) && queue_query_direction(p_queue, direction);
}
}
Expand Down
48 changes: 27 additions & 21 deletions skeleton_project/source/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void run(
Button** pp_down_buttons,
Button** pp_cab_buttons
) {
int current_floor = elevio_floorSensor();
button_update(p_stop_button, elevio_stopButton());

for (size_t i = 0; i < 4; i++) {
Expand All @@ -20,8 +21,10 @@ void run(

if (p_stop_button->was_just_pressed) {
FSM_transition(p_fsm, STOP, p_main_queue, p_timer);
// queue_query_floor(p_main_queue, 2);
}
lamp_toggle(LAMP_STOP, -1, true);
}

if (p_stop_button->was_just_released) { lamp_toggle(LAMP_STOP, -1, false); }

if (p_stop_button->pressed) { reset_timer(p_timer); }

Expand All @@ -46,10 +49,10 @@ void run(
}

if (pp_cab_buttons[i]->was_just_pressed) {
if (i > elevio_floorSensor()) {
if (i > current_floor) {
queue_add(p_main_queue, (Request) {i, true, true});
queue_print(p_main_queue);
} else if (i < elevio_floorSensor()) {
} else if (i < current_floor) {
queue_add(p_main_queue, (Request) {i, false, true});
queue_print(p_main_queue);
}
Expand Down Expand Up @@ -78,34 +81,37 @@ void run(
FSM_transition(p_fsm, STAY, p_main_queue, p_timer);
}
} else {
// printf("Floor sensor: %d\n", elevio_floorSensor());
if (elevio_floorSensor() != -1) {
p_fsm->current_floor = elevio_floorSensor();
// printf("Floor sensor: %d\n", current_floor);
if (current_floor != -1) {
p_fsm->current_floor = current_floor;
// Oppdatere current-lys (cursed løsning? hehe)
lamp_toggle(LAMP_CURRENT, p_fsm->current_floor, true);
lamp_toggle(LAMP_CURRENT, current_floor, true);

if (*target_floor > p_fsm->current_floor){ // beveger seg oppover
lamp_toggle(LAMP_CURRENT, p_fsm->current_floor - 1, false);
} else {
lamp_toggle(LAMP_CURRENT, p_fsm->current_floor + 1, false);
}
// if (*target_floor > p_fsm->current_floor){ // beveger seg oppover

// } else {
// lamp_toggle(LAMP_CURRENT, p_fsm->current_floor + 1, false);
// }
// evt:
// lamp_toggle(LAMP_CURRENT, p_fsm->current_floor + 1, false);
// lamp_toggle(LAMP_CURRENT, p_fsm->current_floor - 1, false);
if (queue_query(p_main_queue, elevio_floorSensor(), p_fsm->direction, ANY)) {

// printf("%d ", current_floor);

if (queue_query(p_main_queue, current_floor, p_fsm->direction, ANY)) {
FSM_transition(p_fsm, ENTERED_FLOOR, p_main_queue, p_timer);
}
}

if (*target_floor == elevio_floorSensor()) {
if (*target_floor == current_floor) {
// TODO: also make elevator stop when someone enters in the same direction
// men er dette riktig sted da?

if ((*target_floor > p_fsm->current_floor) && queue_query(p_main_queue, true, false)){
FSM_transition(p_fsm, ENTERED_FLOOR, p_main_queue, p_timer);
} else if ((*target_floor < p_fsm->current_state) && queue_query(p_main_queue, false, false)) {
FSM_transition(p_fsm, ENTERED_FLOOR, p_main_queue, p_timer);
}
// if ((*target_floor > p_fsm->current_floor) && queue_query(p_main_queue, true, false)){
// FSM_transition(p_fsm, ENTERED_FLOOR, p_main_queue, p_timer);
// } else if ((*target_floor < p_fsm->current_state) && queue_query(p_main_queue, false, false)) {
// FSM_transition(p_fsm, ENTERED_FLOOR, p_main_queue, p_timer);
// }
// if ((p_fsm->current_state == UP_EMPTY || p_fsm->current_state == UP_UNEMPTY) && queue_query(p_main_queue, true, false)){ // sjekker om samme retning som heisen. riktig med false?
// FSM_transition(p_fsm, ENTERED_FLOOR, p_main_queue, p_timer);
// } else if ((p_fsm->current_state == DOWN_EMPTY || p_fsm->current_state == DOWN_UNEMPTY) && queue_query(p_main_queue, false, false)) {
Expand All @@ -121,5 +127,5 @@ void run(
FSM_behaviour(p_fsm, p_timer, p_main_queue);

// queue_print(p_main_queue);
// printf("Current state: %d\n", p_fsm->current_state);
printf("Current state: %d\n", p_fsm->current_state);
}
5 changes: 3 additions & 2 deletions skeleton_project/source/timer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "timer.h"

// TODO: rename all functions to start with 'timer'
time_t* timer_init(){
time_t* p_timer = (time_t*)malloc(sizeof(time_t));

Expand All @@ -21,8 +22,8 @@ void reset_timer(time_t* p_timer){
}

// checks whether the timer has ran out of time and resets it if so
bool time_limit(time_t* p_timer){
return (difftime(time(NULL), *p_timer) >= 3);
bool time_limit(time_t* p_timer, int limit){
return (difftime(time(NULL), *p_timer) >= limit);


// reset_timer(p_timer);
Expand Down
2 changes: 1 addition & 1 deletion skeleton_project/source/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
time_t* timer_init();
void timer_deinit(time_t* p_timer);
void reset_timer(time_t* p_timer);
bool time_limit(time_t* p_timer);
bool time_limit(time_t* p_timer, int limit);

0 comments on commit 8b7510f

Please sign in to comment.