Exemplo n.º 1
0
sm_state_t sm_undefined(int queues[N_QUEUES][N_FLOORS]){

	ui_set_door_open_lamp(0); //Closes the door after stop state

	if(elev_get_floor_sensor_signal()==-1){
		elev_set_speed(-100);
		queue_clear_all_orders(queues);
		return STATE_UNDEFINED;	
	}
	return STATE_IDLE;
}
Exemplo n.º 2
0
sm_state_t sm_stop(int queues[N_QUEUES][N_FLOORS]) {
	 
    elev_set_speed(0); //Stops the elevator
    ui_set_stop_lamp(1); //Light the stop light

	//Checks if command queues has order if not return stop state.
	if(queue_command_has_orders(queues) && !ui_get_stop_signal())
    {
    	ui_set_stop_lamp(0);//Turns of the stop light
		ui_set_door_open_lamp(0);
        return STATE_UNDEFINED;
    }

	else
	{
		if(elev_get_floor_sensor_signal() >= 0)
		{
		ui_set_door_open_lamp(1);
		}
		queue_clear_all_orders(queues); //Clear all the queue orders.
		return STATE_STOP;
	}
	
}
Exemplo n.º 3
0
int ui_init(void)
{
	int i;
	// Zero all floor button lamps
    for (i = 0; i < N_FLOORS; ++i) {
        if (i != 0)
            ui_set_button_lamp(BUTTON_CALL_DOWN, i, 0);

        if (i != N_FLOORS-1)
            ui_set_button_lamp(BUTTON_CALL_UP, i, 0);

        ui_set_button_lamp(BUTTON_COMMAND, i, 0);
    }

    // Clear stop lamp, door open lamp, sets floor indicator to ground floor
    ui_set_stop_lamp(0);
    ui_set_door_open_lamp(0);
	ui_set_floor_indicator(0);
    // Return success.
    return 1;
}
Exemplo n.º 4
0
//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;
}
Exemplo n.º 5
0
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;
	}

}