//Makes ui check for button input from elevator panel. void sm_check_order_button_signals(int queues[N_QUEUES][N_FLOORS]) { ui_button_signals(queues); }
//1 is up and 0 is down current_state_t sm_door_open(int queues[N_QUEUES][N_FLOORS], int previousState) { elev_set_speed(0); int timer = 0; int currentFloor = elev_get_floor_sensor_signal(); ui_set_door_open_lamp(1); startTimer(); //bool while(timer != 1) { if(elev_get_stop_signal()) { return STATE_STOP; } //listens for button signals, to take stop and order while the door is open ui_button_signals(queues); timer = checkTimer(3); } while(elev_get_obstruction_signal() != 0) { if(elev_get_stop_signal()) { ui_set_door_open_lamp(0); return STATE_STOP; } ui_set_door_open_lamp(1); } ui_set_door_open_lamp(0); if (previousState == STATE_UP) { queues[QUEUE_COMMAND][currentFloor] = 0; //if on the top floor if(currentFloor == (N_FLOORS-1)) { queues[QUEUE_DOWN][currentFloor] = 0; } //if orders over current in up queue else if(!queue_up_empty(queues, QUEUE_UP, currentFloor)) { queues[QUEUE_UP][currentFloor] = 0; } //if no orders over current in up queue AND orders below in down queue else if(queue_up_empty(queues, QUEUE_UP, currentFloor) && !queue_down_empty(queues, QUEUE_DOWN, currentFloor)) { queues[QUEUE_DOWN][currentFloor] = 0; return STATE_DOWN; } return STATE_UP; } else if(previousState == STATE_DOWN) { queues[QUEUE_COMMAND][currentFloor] = 0; //if on bottom floor if(currentFloor == 0) { queues[QUEUE_UP][currentFloor] = 0; //return IDLE?????????????????????????????? } //if orders below in down queue else if(!queue_down_empty(queues, QUEUE_DOWN, currentFloor)) { queues[QUEUE_DOWN][currentFloor] = 0; } //if no orders below in down queue and orders over in up queue else if(queue_down_empty(queues, QUEUE_DOWN, currentFloor) && !queue_up_empty(queues, QUEUE_UP, currentFloor)) { queues[QUEUE_UP][currentFloor] = 0; return STATE_UP; } return STATE_DOWN; } return STATE_IDLE; }
sm_state_t sm_open_door(int queues[N_QUEUES][N_FLOORS],sm_state_t previousState){ elev_set_speed(0); //Stops the elevator int currentFloor = elev_get_floor_sensor_signal(); //Gets the current floor the elevator. // If the elevator has an obstruction between floors stop the elevator while(elev_get_obstruction_signal() && currentFloor == -1) { return previousState; } if(currentFloor == -1) { return previousState; } ui_set_door_open_lamp(1); //Open doors startTimer(); while(checkTimer(3) != 1){ if(elev_get_floor_sensor_signal() == -1) { break; //return previousState? both possible depends on wanted behaviour } else if(ui_get_stop_signal()) //Checks stop button for input while the door is open. { return STATE_STOP; } ui_button_signals(queues); //Checks for button input while the door is open. } while(elev_get_obstruction_signal() != 0){ ui_button_signals(queues); //Checks for button input while the obstruction is active. if(ui_get_stop_signal()) //Checks stop button for input when obstructed. { return STATE_STOP; } ui_set_door_open_lamp(1); //Sets door lamp to Open. } ui_set_door_open_lamp(0); //Sets door lamp to closed. //If it came from the state of moving up the it checks the following cases. if (previousState == STATE_MOVE_UP) { queues[QUEUE_COMMAND][currentFloor] = 0; //Reached ordered command floor and clears queue element. //If the top floor is reached it switches state to check if there is any orders bellow. if(currentFloor == N_FLOORS-1) { queues[QUEUE_DOWN][currentFloor] = 0; //Clear ordered floor in down queue. return STATE_MOVE_DOWN; } //Reached queue element in up that is ordered and clears it. else if(!queue_from_and_up_empty(queues, QUEUE_UP, currentFloor)) { queues[QUEUE_UP][currentFloor] = 0; } //If no orders over current in up queue AND orders below in down queue. else if(queue_from_and_up_empty(queues, QUEUE_UP, currentFloor) && !queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor)) { queues[QUEUE_DOWN][currentFloor] = 0; return STATE_MOVE_DOWN; } //More orders in up queue so returns to its previous state and continoues. return STATE_MOVE_UP; } //If it came from the state of moving down the it checks the following cases. else if(previousState == STATE_MOVE_DOWN) { queues[QUEUE_COMMAND][currentFloor] = 0; //Reached ordered command floor and clears queue element //If the bottom floor is reached it switches state to check if there is any orders abow. if(currentFloor == 0) { queues[QUEUE_UP][currentFloor] = 0; return STATE_MOVE_UP; } //Reached queue element in down that is ordered and clears it. else if(!queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor)) { queues[QUEUE_DOWN][currentFloor] = 0; } //If no orders below in down queue AND orders over in up queue. else if(queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor) && !queue_from_and_up_empty(queues, QUEUE_UP, currentFloor)) { queues[QUEUE_UP][currentFloor] = 0; return STATE_MOVE_UP; } //More orders in down queue so returns to its previous state and continoues. return STATE_MOVE_DOWN; } //Returns to previous state else if(previousState == STATE_MOVE_UP) { return STATE_MOVE_UP; } else { return STATE_MOVE_DOWN; } }