Example #1
0
int main(){
	printf("\nInitialization starting...\n");
	if(!elev_init()){
		printf("\nFailed to initialize elevator...\n");
		return 0;
	}
	printf("\nInitialization complete\n");

	int state = BOOT;
	int event = STOP;
	int order_table[N_FLOORS][N_BUTTONS];
	clear_order_table(order_table);
	
	while(1){
		//printf("\nCurrent state: %d, Current event: %d", state, event);

		state = handle_event(state, event, order_table);
		get_orders(order_table, state);
		set_lights(order_table);

		int temp_event = get_direction(order_table, state);
		if(get_emergency(state)){
			event = EMERGENCY_BUTTON;
		}else if(get_obstruction(order_table, state)){
			event = OBSTRUCTION;
		}else if(temp_event != -1){
			event = temp_event;
		}
	}
	return 0;
} // End main
Example #2
0
int main(){
    // Initialize hardware
    if (!elev_init()) {
        printf(__FILE__ ": Unable to initialize elevator hardware\n");
        return 1;
    }
    
    printf("Press STOP button to stop elevator and exit program.\n");

    elev_set_speed(300);
      
    while (1) {
        if (elev_get_floor_sensor_signal() == 0){
            elev_set_speed(300);
        } else if (elev_get_floor_sensor_signal() == N_FLOORS-1){
            elev_set_speed(-300);
        }

        if (elev_get_stop_signal()) {
            elev_set_speed(0);
            break;
        }
    }

    return 0;
}
Example #3
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;
}
Example #4
0
int main(void){

    // Initialize hardware
    if (!elev_init()) {
        printf("Unable to initialize elevator hardware!\n");
        return 1;
    }


    States fsm_state = init;
    States fsm_previous_state = init;
    States fsm_new_state = init;
    
    system_state_t system_state;
    system_state.motor_direction = 0;
    system_state.current_floor = -1;
    
    reset_active_floors(&system_state);
    reset_called_floors(&system_state);
    
    
    while (1){

	    fsm_previous_state = fsm_state;
        fsm_state = fsm_new_state;

        switch (fsm_state){
            case init:
                fsm_new_state = fsm_init_state(&system_state, fsm_previous_state);
                break;
                
            case idle:
                fsm_new_state = fsm_idle_state(&system_state,fsm_previous_state);      
		        break;
                
            case door_open:
                fsm_new_state = fsm_door_open(&system_state, fsm_previous_state);
                break;
            
            case going_down:
                fsm_new_state = fsm_going_down(&system_state, fsm_previous_state);
                break;
            
            case going_up:
                fsm_new_state = fsm_going_up(&system_state, fsm_previous_state);
                break;
                
	        case stop:
		        fsm_new_state = fsm_stop_button_pressed(&system_state, fsm_previous_state);
		        break;

            default:
		        elev_set_stop_lamp(1);
                break;
	    }
    }
}
Example #5
0
int main()
{
    	// Initialize hardware
   	if (!elev_init()) {
        	printf(__FILE__ ": Unable to initialize elevator hardware\n");
        	return 1;
   	}
	
	ctrl_initiateElevator();
    	while (1) {
		ui_checkStop();
		ui_checkButtons();
		ctrl_checkSensor();
		ui_checkObstruction();
		timer_checkDoorTimer();
       	}

    	return 0;
}
Example #6
0
Driver::Driver(EventQueue& logic_queue, bool use_simulator)
  : logic_queue(logic_queue),
    fsm(logic_queue)
{
  std::string driver_string = use_simulator ? "simulated" : "hardware";
  elev_init(use_simulator ? ET_Simulation : ET_Comedi);

  LOG_INFO("Driver started (" << driver_string << ")");

  int current_floor = initialize_position();
  if (current_floor >= 0) {
    LOG_INFO("Elevator is now at floor " << current_floor);
  }
  else {
    throw InitException("Failed to position the elevator at a known floor!");
  }

  fsm.set_floor(current_floor);
}
Example #7
0
// Initialize statemachine
int sm_init(){

	// Initialize hardware
    if (!elev_init())
    {
        printf(__FILE__ ": Unable to initialize elevator hardware\n");
        return 1;
    }

	// Initialize User interface
    if (!ui_init())
    {
        printf(__FILE__ ": Unable to initialize elevator user interface\n");
        return 1;
    }

   return 1;

}
Example #8
0
int main(void){
    printf("Main start 123\n");
    FLAG_REBOOT=1;
    pthread_create(&thread_5,NULL,tcp_receive,NULL);
    pthread_create(&thread_6,NULL,receive_echo,NULL);
    while(FLAG_REBOOT){
        FLAG_REBOOT=0;
        printf("Start while i main\n");
        elev_init();
        pthread_create(&thread_1, NULL, button_func, NULL);
        pthread_create(&thread_2,NULL,elev_logic,NULL);
        pthread_create(&thread_3,NULL,send_udp,NULL);
        pthread_create(&thread_4,NULL,receive_udp,NULL);
        pthread_join(thread_1, NULL);
        pthread_join(thread_2,NULL);
        pthread_join(thread_3, NULL);
        pthread_join(thread_4, NULL);
    }
    return 0;
}
Example #9
0
void start_elevator(){
  	elev_init();
    elev_goto_nearest_floor();
    init_buttonVectors();
    for (int i = 0; i < N_FLOORS; i++){
    		printf("%i\n", internalButtonVector[i]);
   		 }

    lastFloor = elev_get_floor_sensor_signal();
    pthread_mutex_init(&fileLock, NULL);
    pthread_mutex_lock(&fileLock);
    printf("Trying to read internal orders and set internalButtonVector\n");
    internalOrdersText = fopen("internal.txt", "r");
    int character;
    if (internalOrdersText != NULL){
    	for (int i = 0; i < N_FLOORS; i++){
    		character = fgetc(internalOrdersText);
    		if((character-48) == 1){
    			internalButtonVector[i] = 1;
    		}
    		else{
    			internalButtonVector[i] = 0;
    		}


    		printf("%i\n", internalButtonVector[i]);
             elev_set_button_lamp(BUTTON_COMMAND, i, internalButtonVector[i]);
   		 }
    
    fclose(internalOrdersText);
    }
    else{
    	printf("Inititating internalOrdersText\n");
    	init_internalOrdersText();
    }
    pthread_mutex_unlock(&fileLock);

	printf("Hva faen?\n");
	

	init_socket(&broadCastSocket);
	bind_socket(&broadCastSocket, BROADCAST_PORT, "255.255.255.255");
	
  	int reuseEnable = 1;
	if (setsockopt(broadCastSocket, SOL_SOCKET, SO_REUSEADDR, &reuseEnable, sizeof(reuseEnable)) < 0){
	    error("setsockopt");
	}

	init_socket(&sendSock);
	int broadcastEnable = 1;
	if (setsockopt(sendSock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable))){
	  	error("setsockopt");
  	}

   pthread_create(&connectMasterThread, NULL, &connect_to_master, NULL);
   pthread_join(connectMasterThread, NULL);
   printf("First init finished, lytter på port: %i ip: %s\n", slp, masterIPAddr);


	pthread_mutex_init(&internalOrdersLock, NULL);
	pthread_create(&internalOrdersThread, NULL, &thread_check_internal_orders, NULL);

    pthread_mutex_init(&externalOrdersLock, NULL);
	pthread_create(&checkExternalOrderThread, NULL, &check_external_orders, NULL);

	pthread_mutex_init(&receiveLock, NULL);
	pthread_create(&receiveThread, NULL, &thread_udp_receive_from_master, NULL);

	pthread_mutex_init(&sendLock, NULL);
	pthread_create(&threadSendLocDir, NULL, &thread_send_loc_dir_master, NULL);


    printf("Initialization complete\n");
    sleep(1);
	elev_slave_waiting_with_network();
	pthread_join(internalOrdersThread, NULL);
	pthread_join(receiveThread, NULL);


}
Example #10
0
/*
 * Class:     no_ntnu_stud_torbjovn_comedielevator_NativeInterface
 * Method:    elev_init
 * Signature: (Z)V
 */
JNIEXPORT void JNICALL Java_no_ntnu_stud_torbjovn_comedielevator_NativeInterface_elev_1init
  (JNIEnv * env, jobject obj, jboolean simulated) {
    if (simulated) elev_init(ET_Simulation);
    else elev_init(ET_Comedi);
  }
Example #11
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;
}
Example #12
0
void elevator_thread(){
	int sum=0;
	int stop=0;
	Message message_send;
 	elevator.queue[0] = 0;
	elevator.queue[1] = 0;
	elevator.queue[2] = 0;
	elevator.queue[3] = 0;
 	FILE *fp;
   	char buff[255];

   fp = fopen("local_order_queue.txt", "a+");
	for(int i=0;i<N_FLOORS;i++){
	   fscanf(fp, "%s", buff);
	   printf("%d : %s\n", i, buff );
	   if(buff[0] == '1'){
		   elevator.queue[i]=1;
		   sum++;
		}
		else
			elevator.queue[i]=0;
   	}
   fclose(fp);
	
	if(!sum){
		elev_init();
		elevator.floor_current = 0;
		elevator.direction=DIRN_STOP;
	}
	message_send.elevator.direction = elevator.direction;
	message_send.elevator.floor_current = elevator.floor_current;
	
	char transmit[BUFSIZE];
	serialization(ELEV_UPDATE, message_send, transmit);
	pthread_t message_send_thread;
	pthread_create(&message_send_thread, NULL , tcp_send, (void*)transmit);
	
	int floor_update_not_sent=1;
	while(1){
		if(sum != 0){
			elevator.floor_current = elev_get_floor_sensor_signal();
			int orders_up=0;
			int orders_down=0;
			if(elevator.direction == DIRN_STOP){
				for(int i=elevator.floor_current+1;i<N_FLOORS;i++){
					if(elevator.queue[i] != 0){
						orders_up++;
						puts("Order up");
					}

				}
				for( int i=elevator.floor_current-1;i>=0;i--){
					if(elevator.queue[i] != 0){
						orders_down++;
						puts("Order down");
					}
				}
				if (orders_up == 0 && orders_down == 0){
				elevator.direction=DIRN_STOP;
				}
				else if(orders_up>orders_down){
					elevator.direction=DIRN_UP;
					puts("DIRN UP");
				}
			
				else{
					elevator.direction=DIRN_DOWN;
					puts("DIRN DOWN");
				}
			}
			for(int i=0;i<N_FLOORS;i++){
				message_send.elevator.queue[i]=elevator.queue[i];
			}
			message_send.elevator.direction = elevator.direction;
			message_send.elevator.floor_current = elevator.floor_current;
			char transmit[BUFSIZE];
			serialization(ELEV_UPDATE, message_send, transmit);
			pthread_t message_send_thread;
			pthread_create(&message_send_thread, NULL , tcp_send, (void*)transmit);	
			
			elev_set_motor_direction(elevator.direction);		
		}

		while(sum){
			elevator.floor_current=elev_get_floor_sensor_signal();
			if(elevator.floor_current!=-1){
				elev_set_floor_indicator(elevator.floor_current);
				if((elevator.floor_current==0 && elevator.direction==DIRN_DOWN) || (elevator.floor_current==N_FLOORS&&elevator.direction==DIRN_UP)){
					elevator.direction=DIRN_STOP;
					elev_set_motor_direction(elevator.direction);
					stop=1;
				}
				if(elevator.queue[elevator.floor_current]==1){
					stop=1;
				}
				else if(elevator.queue[elevator.floor_current]==2){
					sum=0;
					if(elevator.direction==DIRN_UP){
						for(int i=elevator.floor_current;i<N_FLOORS;i++){
							if(elevator.queue[i]==1){
								sum++;
							}
						}
						switch (elevator.floor_current){
							case 1:
								if(elev_orders[elevator.floor_current][1]==my_ID){
									stop=1;
									break;
								}
							case 2:
							if(elev_orders[elevator.floor_current][1]==my_ID){
									stop=1;
									break;
								}
							default:
								if(sum==0){
									stop=1;
								}
						}
					}
					else if(elevator.direction==DIRN_DOWN){
						for(int i=elevator.floor_current;i>=0;i--){
							if(elevator.queue[i]==1){
								sum++;
							}
						}
						switch (elevator.floor_current){
							case 1:
								if(elev_orders[elevator.floor_current+2][1]==my_ID){
									stop=1;
									break;
								}
							case 2:
							if(elev_orders[elevator.floor_current+2][1]==my_ID){
									stop=1;
									break;
								}
							default:
								if(sum==0){
									stop=1;
								}
						}
					}					
				}
				if(stop){
					stop=0;
					elev_set_motor_direction(DIRN_STOP);
					elev_set_button_lamp(BUTTON_INSIDE,elevator.floor_current,0);
					elevator.queue[elevator.floor_current]=0;
					printf("Elevator queue updated: \t");
					sum = 0;
					for(int i=0;i<N_FLOORS;i++){
						message_send.elevator.queue[i]=elevator.queue[i];
						sum+=elevator.queue[i];
						printf("%d ", elevator.queue[i]);		
					}

					FILE *fp;
			   		fp = fopen("local_order_queue.txt", "w+");
			   		fprintf(fp,"%d %d %d %d", elevator.queue[0], elevator.queue[1], elevator.queue[2], elevator.queue[3] );
			   		fclose(fp);
					
					message_send.elevator.prev_direction = elevator.direction;	
					elevator.direction = DIRN_STOP;
					message_send.elevator.direction = elevator.direction;
					message_send.elevator.floor_current = elevator.floor_current;
					printf("Floor current: %d\n", message_send.elevator.floor_current);

					for(int i = 0;i<6;i++){
						message_send.orders[i][0] = elev_orders[i][0];
						message_send.orders[i][1] = elev_orders[i][1];
					}
					switch (elevator.floor_current){
						case 0:
							message_send.orders[0][0] = 0;
							message_send.orders[0][1] = my_ID;
							elevator.next_direction = DIRN_UP;
							break;
						case 1:
							if(elev_orders[1][1] == my_ID){
								message_send.orders[1][0] = 0;
								message_send.orders[1][1] = my_ID;
								elevator.next_direction = DIRN_UP;
							}
							else {
								message_send.orders[3][0] = 0;
								message_send.orders[3][1] = my_ID;
								elevator.next_direction = DIRN_DOWN;
							}
							break;
						case 2:
							if(elev_orders[2][1] == my_ID){
								message_send.orders[2][0] = 0;
								message_send.orders[2][1] = my_ID;
								elevator.next_direction = DIRN_UP;
							}
							else {
								message_send.orders[4][0] = 0;
								message_send.orders[4][1] = my_ID;
								elevator.next_direction = DIRN_DOWN;
							}
							break;							
						case 3:
							message_send.orders[5][0] = 0;
							message_send.orders[5][1] = my_ID;
							elevator.next_direction = DIRN_DOWN;
							break;
					}
					puts("ORDER UPDATE");
       				for(int i = 0;i<6;i++){
          				printf("Order: %d \t ID: %d\n",message_send.orders[i][0], message_send.orders[i][1]);
        			}
					
					char transmit[BUFSIZE];
					serialization(ORDER_UPDATE, message_send, transmit);
					pthread_t message_send_thread;
					pthread_create(&message_send_thread, NULL , tcp_send, (void*)transmit);	
					
					floor_update_not_sent=0;
					elev_set_door_open_lamp(1);
				    elev_set_button_lamp(BUTTON_INSIDE,elevator.floor_current,0);
				    sleep(2);
				    elev_set_door_open_lamp(0);
				    sum = 0;
				    switch (elevator.next_direction){
				    	case DIRN_DOWN:
				    		for(int i = elevator.floor_current;i>=0;i--){
				    			sum += elevator.queue[i];
				    		}
				    		if(sum!=0){
				    			elevator.direction = DIRN_DOWN;
				    		}
				    		else {
				    			for(int i = elevator.floor_current;i<N_FLOORS;i++){
				    				sum+=elevator.queue[i];
				    			}
				    			if(sum!=0)
				    				elevator.direction = DIRN_UP;
				    		}
				    		break;
				    	case DIRN_UP:
				    		for(int i = elevator.floor_current;i<N_FLOORS;i++){
				    			sum += elevator.queue[i];
				    		}
				    		if(sum!=0){
				    			elevator.direction = DIRN_UP;
				    		}
				    		else {
				    			for(int i = elevator.floor_current;i>=0;i--){
				    				sum+=elevator.queue[i];
				    			}
				    			if(sum!=0)
				    				elevator.direction = DIRN_DOWN;
				    		}
				    		break;
				    }
				    elev_set_motor_direction(elevator.direction);
				}
				else if(floor_update_not_sent){
					message_send.elevator.direction = elevator.direction;
					message_send.elevator.prev_direction = elevator.direction;
					message_send.elevator.floor_current = elevator.floor_current;
					
					char transmit[BUFSIZE];
					serialization(ELEV_UPDATE, message_send, transmit);
					pthread_t message_send_thread;
					pthread_create(&message_send_thread, NULL , tcp_send, (void*)transmit);	
					floor_update_not_sent=0;
				}
			}
			else
				floor_update_not_sent=1;
		}
	
		for(int i=0;i<N_FLOORS;i++){		
				sum+=elevator.queue[i];
		}

		if(!connection){
			break;
		}
	}
}