Beispiel #1
0
int main()
{
    // Initialize hardware
    if (!elev_init()) {
        printf(__FILE__ ": Unable to initialize elevator hardware\n");
        return 1;
    }
    printf("Best Elevator Software,  version: 2.27.14\n" );
    initialize();
    lastFloorReached = N_FLOORS+1;
    while (1) {
        queueIsEmpty = emptyQueue();
        obstructionSignal = elev_get_obstruction_signal();
        stoppedSignal = elev_get_stop_signal();
        thisFloor = elev_get_floor_sensor_signal();
        duration = clock()-start;

        if ( timerActive && duration > seconds ) {
            timerActive = 0;
            duration = 0;
            stateMachine( TIMEROUT );
        }

        if ( obstructionSignal != lastObstructionSignal ) {
            if ( obstructionSignal == 1 )
                stateMachine( OBSTRON );
            if ( obstructionSignal == 0 )
                stateMachine( OBSTROFF );
            lastObstructionSignal = obstructionSignal;
        }

        if ( stoppedSignal != lastStoppedSignal ) {
            if ( stoppedSignal == 1 )
                stateMachine( STOP );
            lastStoppedSignal = stoppedSignal;
        }

        if ( thisFloor != -1 && ( lastFloorReached != thisFloor ) ) {
            if ( thisFloor-lastFloorReached < 0 )
                direction = DOWN;
            if ( thisFloor-lastFloorReached > 0 )
                direction = UP;
            lastFloorReached = thisFloor;
            elev_set_floor_indicator( thisFloor );
        }

        if ( order() && directionToGo( thisFloor, getNextOrder( thisFloor ) ) == 0 )
            stateMachine( NEWORDERCURRENTFLOOR );

        if ( thisFloor != -1 && directionToGo( thisFloor , getNextOrder( thisFloor ) ) == 0 && !timerActive )
            stateMachine( FLOORREACHED );

        if( queueIsEmpty == 1 && order() )
            stateMachine( NEWORDEREMPTYQUEUE );
    }
    return 0;
}
Beispiel #2
0
/* From DOOR_OPENED, go to DOOR_CLOSED */
static void
action_opened_close (void)
{
  if (elev_get_obstruction_signal())
    {
      door_set_timer();
      return;
    }

  elev_set_door_open_lamp(0);
  door_last_state = door_current_state;
  door_current_state = DOOR_CLOSED;
}
Beispiel #3
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;
}
Beispiel #4
0
sm_state_t sm_move_up(int queues[N_QUEUES][N_FLOORS]){

	//Checks if the door is forced open if it is stop the elevator and light open door.
	if(elev_get_obstruction_signal()) 
	{
		return STATE_OPEN_DOOR;
	}

	//Sets currentFloor to the last floor seen.
	int currentFloor = ui_get_floor_indicator();

	//Sets currentFloor to the current floor it is at.
	if(elev_get_floor_sensor_signal()>=0)
	{
     currentFloor = elev_get_floor_sensor_signal();
	}
	
	//Sets the motor speed to 100 upwards.
	elev_set_speed(100);

	//Checks if there are orders in the up- or (command- and up-) queue at current floor.
	if(queues[QUEUE_UP][currentFloor] ||(queues[QUEUE_COMMAND][currentFloor] && queues[QUEUE_UP][currentFloor]))
	{	
		if(elev_get_floor_sensor_signal() != -1) //Checks if it is at a floor.
		{
		  return STATE_OPEN_DOOR;
		}
		else 
		{
		  return STATE_MOVE_UP;
		}
	}

	//Checks if the command queue has an order at current floor and that the elevator is at an actual floor.
	else if(queues[QUEUE_COMMAND][currentFloor] && (elev_get_floor_sensor_signal() != -1))
	{
		return STATE_OPEN_DOOR;
	}
	
	//Checks if the command queue has order above the current floor
	else if(!queue_from_and_up_empty(queues, QUEUE_COMMAND, currentFloor))
	{
		return STATE_MOVE_UP;
	}

	//Checks if the top floor is ordered in the down queue and the up queue does not have orders above the current floor
	else if(queues[QUEUE_DOWN][N_FLOORS-1] == 1 && queue_from_and_up_empty(queues, QUEUE_UP, currentFloor))
	{	
			//To make sure this conidtion only opens the door when the elevator is on the top floor.
			if(currentFloor != N_FLOORS-1)
			{
				return STATE_MOVE_UP;
			}
			else
			{
				return STATE_OPEN_DOOR;
			}
	}

	//Checks if the down queue has order on current floor and there is no orders over the current floor in up queue.
	else if(queues[QUEUE_DOWN][currentFloor] && queue_from_and_up_empty(queues, QUEUE_UP, currentFloor))
	{
		if(elev_get_floor_sensor_signal() != -1) //Checks if it is at a floor.
		{
		  return STATE_OPEN_DOOR;
		}
		else 
		{
		  return STATE_MOVE_UP;
		}
	}
	
	//Checks if it is order over current floor in up or down queue.
	else if(!queue_from_and_up_empty(queues, QUEUE_UP, currentFloor)||!queue_from_and_up_empty(queues, QUEUE_DOWN, currentFloor))
	{
		return STATE_MOVE_UP;
	}

	//Finished all upwards orders.
	else
		return STATE_IDLE;
}
Beispiel #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;
	}

}
Beispiel #6
0
sm_state_t sm_move_down(int queues[N_QUEUES][N_FLOORS]){

	//Checks if the door is forced open if it is stop the elevator and light open door and stops the elevator.
	if(elev_get_obstruction_signal()) 
	{
		return STATE_OPEN_DOOR;
	}
	
	//Sets currentFloor to the last floor seen.
	int currentFloor = ui_get_floor_indicator();

	//Sets currentFloor to the current floor it is at.
	if(elev_get_floor_sensor_signal()>=0)
	{
	  currentFloor = elev_get_floor_sensor_signal();
	}
	
	//Sets the motor speed to 100 downwards.
	elev_set_speed(-100);

	//Checks if there are orders in the down- or (command- and down-) queue at current floor.
	if((queues[QUEUE_DOWN][currentFloor]||((queues[QUEUE_COMMAND][currentFloor]) && queues[QUEUE_DOWN][currentFloor])))
	{
		if(elev_get_floor_sensor_signal() != -1) //Checks if it is at a floor.
		{
		  return STATE_OPEN_DOOR;
		}
		else
		{
		  return STATE_MOVE_DOWN;
		}
	}

	//Checks if the command queue has an order at current floor and that the elevator is at an actual floor.
	else if(queues[QUEUE_COMMAND][currentFloor] && (elev_get_floor_sensor_signal() != -1))
	{
		return STATE_OPEN_DOOR;
	}

	//Checks if the down queue has any orders bellow current floor.
	else if(!queue_from_and_down_empty(queues, QUEUE_COMMAND, currentFloor))
	{
		return STATE_MOVE_DOWN;
	}
	
	//Checks if the bottom floor is ordered in the up queue and the down queue does not have orders bellow the current floor.
	else if(queues[QUEUE_UP][0] == 1 && queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor))
	{		

		if(currentFloor != 0)
		{
			return STATE_MOVE_DOWN;
		}
		else
		{
		  return STATE_OPEN_DOOR;
		}

	}
	
	//Checks if the up queue has order on current floor and there is no orders over the current floor in the down queue.
	else if(queues[QUEUE_UP][currentFloor] && queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor))
	{
		if(elev_get_floor_sensor_signal() != -1) //Checks if it is at a floor.
		{
		  return STATE_OPEN_DOOR;
		}
		else
		{
		  return STATE_MOVE_DOWN;
		}
	}

	//Checks if it is order bellow current floor in up or down queue.
	else if(!queue_from_and_down_empty(queues, QUEUE_UP, currentFloor) || !queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor))
	{
		return STATE_MOVE_DOWN;
	}
	
	//Finished all downwards orders.
	else
	{
	return STATE_IDLE;
	}
}
/*
 * Class:     no_ntnu_stud_torbjovn_comedielevator_NativeInterface
 * Method:    elev_get_obstruction_signal
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_no_ntnu_stud_torbjovn_comedielevator_NativeInterface_elev_1get_1obstruction_1signal
  (JNIEnv * env, jobject obj) {
    return elev_get_obstruction_signal();
  }
Beispiel #8
0
int main()
{
    if (!elev_init()) {
        printf(__FILE__ ": Unable to initialize elevator hardware\n");
        return 1;
    }


    slettAlleOrdre();
    elev_set_speed(0);    
    initHeisstyring();

    int forgjeEtasje = 0;
    int etasje;
    bool forgjeStopp;
    bool stopp;
    bool forgjeObstruksjon;
    bool obstruksjon;
    bool forgjeOpp[4];
    bool opp[4];
    bool forgjeInne[4];
    bool inne[4];
    bool forgjeNed[4];
    bool ned[4];

    while(1)
    {
	stopp = elev_get_stop_signal();
	obstruksjon = elev_get_obstruction_signal();
	etasje = elev_get_floor_sensor_signal();
	
	for(int i = 0; i < N_FLOORS; i++) // poller masse, fix
	{
	    if(i != N_FLOORS-1)
		opp[i] = elev_get_button_signal(BUTTON_CALL_UP, i);
    	    
	    if(i != 0)
		ned[i] = elev_get_button_signal(BUTTON_CALL_DOWN, i);
	    
	    inne[i] = elev_get_button_signal(BUTTON_COMMAND, i);
	}

	for(int i = 0; i < N_FLOORS; i++)
	{
	    if(opp[i] && !forgjeOpp[i])
		leggTilOrdre(i, false, true);
	    
	    if(ned[i] && !forgjeNed[i])
		leggTilOrdre(i, false, false);
	    
	    if(inne[i] && !forgjeInne[i])
		leggTilOrdre(i, true, true);
	    
	    forgjeOpp[i] = opp[i];
	    forgjeNed[i] = ned[i];
	    forgjeInne[i] = inne[i];
	}

	if(harTimetUt())
	    timeOut();

	if (etasje != -1 && etasje != forgjeEtasje)
	{
	    elev_set_floor_indicator(etasje);
	    etasjeAnkommet(etasje);
	    forgjeEtasje = etasje;
	}

	if(obstruksjon && !forgjeObstruksjon)
	{
	    forgjeObstruksjon = obstruksjon;
	    forgjeEtasje = -1;
	    if(obstruksjon)
		obstruksjonPa();
	    else
		obstruksjonAv();
	}
	
	if(stopp && !forgjeStopp)
	{
	    forgjeStopp = stopp;
	    forgjeEtasje = -1;
	    if(stopp)
		nodStopp();
	}
    }	
    return 0;
}