int elev_init(void) { int i; // Init hardware if (!io_init()) return 0; // Zero all floor button lamps for (i = 0; i < N_FLOORS; ++i) { if (i != 0) elev_set_button_lamp(BUTTON_CALL_DOWN, i, 0); if (i != N_FLOORS - 1) elev_set_button_lamp(BUTTON_CALL_UP, i, 0); elev_set_button_lamp(BUTTON_COMMAND, i, 0); } // Clear stop lamp, door open lamp, and set floor indicator to ground floor. elev_set_stop_lamp(0); elev_set_door_open_lamp(0); elev_set_floor_indicator(0); // Return success. return 1; }
void addOrder( elev_button_type_t button_type, int floor ) { switch( button_type ) { case BUTTON_COMMAND: if ( queue.noDir[ floor ] == 0 ) { queue.noDir[ floor ] = 1; elev_set_button_lamp( button_type, floor, 1 ); } break; case BUTTON_CALL_UP: if ( queue.upDir[ floor ] == 0 ){ queue.upDir[ floor ] = 1; elev_set_button_lamp( button_type, floor, 1 ); } break; case BUTTON_CALL_DOWN: if ( queue.downDir[ floor ] == 0 ) { queue.downDir[ floor ] = 1; elev_set_button_lamp( button_type, floor, 1 ); } break; default: break; } }
int udp_receive_from_master(){ // printf("Listening for orders on port: %i, \n", slp); char buf[BUF_SIZE]; int msgType; memset(&buf, 0, sizeof(buf)); int floor; int buttonDirn; int dummyElevatorID; if (udp_listen(buf, receiveSock, "-1") == 1){ // printf("UDP recieved succesfully\n"); deserialize(buf, &floor, &buttonDirn, &msgType, &dummyElevatorID); if (msgType == 1){ printf("externalButtonVector har fått element i seg\n"); externalButtonVector[floor][buttonDirn] = 1; } else if (msgType == 0){ printf("Received acknowledgement request\n"); } else if (msgType == 4){ externalOrdersMatrix[floor][buttonDirn] = 1; elev_set_button_lamp(buttonDirn, floor, 1); printf("Got syncing new order: floor is %i, buttondDirn is %i \n", floor, buttonDirn); } else if (msgType == 5){ externalOrdersMatrix[floor][buttonDirn] = 0; printf("buttonDirn er: %i\n", buttonDirn); elev_set_button_lamp(buttonDirn, floor, 0); printf("Got syncing order confirmation: floor is %i, buttondDirn is %i \n", floor, buttonDirn); } else if (msgType == 10){ backup = 1; printf("Backup is %i\n", backup); } else if (msgType == 11){ backup = 0; printf("Backup is %i\n", backup); } else if (msgType == 20){ // printf("Received are you alive message in slave\n"); } return 1; } else{ return 0; } }
void delOrder( int floor, direction_t rideDir ) { queue.noDir[ floor ] = 0; queue.upDir[ floor ] = 0; queue.downDir[ floor ] = 0; if ( floor != (N_FLOORS - 1) ) elev_set_button_lamp( BUTTON_CALL_UP, floor, 0 ); if ( floor != 0 ) elev_set_button_lamp( BUTTON_CALL_DOWN, floor, 0 ); elev_set_button_lamp( BUTTON_COMMAND, floor, 0 ); }
void elev_reset_all_lamps(void) { elev_set_speed(0); elev_set_stop_lamp(0); elev_set_door_open_lamp(0); int floor; for (floor = 0; floor < N_FLOORS; ++floor) { elev_set_button_lamp(ELEV_DIR_DOWN, floor, 0); elev_set_button_lamp(ELEV_DIR_UP, floor, 0); elev_set_button_lamp(ELEV_DIR_COMMAND, floor, 0); } }
States fsm_door_open(system_state_t* system_state, States fsm_previous_state){ elev_set_direction(system_state, 0); elev_set_door_open_lamp(1); if (!fsm_pause_3_sec(system_state)) return stop; elev_set_door_open_lamp(0); check_floor(system_state); if (fsm_previous_state == idle){ elev_set_button_lamp (BUTTON_COMMAND, system_state->current_floor,0); orders_clearAt(system_state, system_state->current_floor); if (system_state->current_floor < 3) elev_set_button_lamp (BUTTON_CALL_UP, system_state->current_floor,0); if (system_state->current_floor > 0) elev_set_button_lamp (BUTTON_CALL_DOWN, system_state->current_floor,0); return idle; } else if (fsm_previous_state == going_down) { for (int i = system_state->current_floor; i >= 0; --i){ if (system_state->elev_active_floors[i] || system_state->elev_called_floors_up[i] || system_state->elev_called_floors_down[i]){ system_state->motor_direction = -1; return going_down; } } return idle; } else if (fsm_previous_state == going_up){ for (int i = system_state->current_floor; i < N_FLOORS; ++i){ if (system_state->elev_active_floors[i] || system_state->elev_called_floors_up[i] || system_state->elev_called_floors_down[i]){ system_state->motor_direction = 1; return going_up; } } } return idle; }
void check_call_buttons(system_state_t* system_state){ for (int i = 1; i < N_FLOORS; ++i){ if (elev_get_button_signal(BUTTON_CALL_DOWN,i)){ system_state->elev_called_floors_down[i] = 1; elev_set_button_lamp(BUTTON_CALL_DOWN, i, 1); } } for (int i = 0; i < N_FLOORS - 1; ++i){ if (elev_get_button_signal(BUTTON_CALL_UP,i)){ system_state->elev_called_floors_up[i] = 1; elev_set_button_lamp(BUTTON_CALL_UP, i, 1); } } }
void resetFloorLights(int floor) { elev_set_button_lamp(BUTTON_COMMAND, floor, false); switch (floor) { case 0: elev_set_button_lamp(BUTTON_CALL_UP, floor, false); break; case N_FLOORS-1: elev_set_button_lamp(BUTTON_CALL_DOWN, floor, false); break; default: elev_set_button_lamp(BUTTON_CALL_UP, floor, false); elev_set_button_lamp(BUTTON_CALL_DOWN, floor, false); break; } }
int elev_init(void) { int init_success = io_init(); if (!init_success){ return 0; } for (int f = 0; f < N_FLOORS; f++) { for (elev_button_type_t b = 0; b < N_BUTTONS; b++){ elev_set_button_lamp(b, f, 0); } } //drive down to valid floor if (elev_get_floor_sensor_signal() == -1){ elev_set_motor_direction(DIRN_DOWN); } while (elev_get_floor_sensor_signal() == -1) { ///Keep driving down until reached a floor } elev_set_motor_direction(DIRN_STOP); elev_set_stop_lamp(0); elev_set_door_open_lamp(0); elev_set_floor_indicator(elev_get_floor_sensor_signal()); return 1; }
void check_cmd_buttons(system_state_t* system_state){ for (int i = 0; i < N_FLOORS; ++i){ if(elev_get_button_signal(BUTTON_COMMAND, i)){ system_state->elev_active_floors[i] = 1; elev_set_button_lamp(BUTTON_COMMAND, i, 1); } } }
/** * check_buttons: * * Checks all UI buttons and updates the order queue. * * Returns: 1 if new orders are present, or 0. * */ int ui_check_buttons(void) { for (int button = 0; button < N_BUTTONS; button++) { for (int floor = 0; floor < N_FLOORS; floor++) { if (elev_get_button_signal(button, floor)) { elev_set_button_lamp(button, floor, 1); add_order(button, floor); } } } }
void *order_monitor(){ int floor, panel; struct node * iter = &head; time_t current_time; int anyorder = 0; while(1){ time(¤t_time); /* Loop command orders on local elevator */ panel = COMMAND; for(floor = 0; floor < N_FLOORS; floor++){ if(iter->elevinfo.current_orders[floor][panel].active) elev_set_button_lamp(panel,floor,1); else elev_set_button_lamp(panel, floor, 0); } /* Loop call up and call down panel on all elevators */ for(floor = 0; floor < N_FLOORS; floor++){ for(panel = CALL_UP; panel<COMMAND; panel++){ if(!((floor==0 && panel==CALL_DOWN) || (floor==N_FLOORS-1 && panel==CALL_UP))){ /* Not considering invalid buttons.*/ while(iter!=0){ if(iter->elevinfo.active){ if(iter->elevinfo.current_orders[floor][panel].active){ /* Check timestamp - if timeout, then take over order */ if(current_time - iter->elevinfo.current_orders[floor][panel].timestamp > MAXWAIT_ORDER_TIME){ order_register_new_order(&head, floor, panel); } anyorder = 1; break; } } iter = iter->next; } if(anyorder) elev_set_button_lamp(panel,floor,1); else elev_set_button_lamp(panel, floor, 0); anyorder = 0; } /* Restart scan */ iter = &head; } } } }
void elev_init(void) { int init_success = io_init(); assert(init_success && "Unable to initialize elevator hardware!"); for (int f = 0; f < N_FLOORS; f++) { for (elev_button_type_t b = 0; b < N_BUTTONS; b++){ elev_set_button_lamp(b, f, 0); } } elev_set_stop_lamp(0); elev_set_door_open_lamp(0); elev_set_floor_indicator(0); }
void check_internal_orders(){ //checks if we have an order, and sets the light; for (int i = 0; i < N_FLOORS; i++){ int buttonSignal = elev_get_button_signal(BUTTON_COMMAND, i); if(buttonSignal == 1){ internalButtonVector[i] = buttonSignal; elev_set_button_lamp(BUTTON_COMMAND, i, 1); } } write_to_internalOrdersText(); }
void button_check(){ Message message_send; elev_button_type_t button_type; int floor; int button_is_pressed=0; puts("Button check thread started"); while (1){ for(floor = 0;floor<N_FLOORS;floor++){ for(button_type = 0;button_type<N_BUTTONS;button_type++){ if(elev_get_button_signal(button_type,floor)==1){ if(button_type==BUTTON_INSIDE){ elev_set_button_lamp(BUTTON_INSIDE,floor,1); elevator.queue[floor]=1; } pthread_mutex_lock(&mutex); button_is_pressed=1; message_send.button.floor=floor; message_send.button.type=button_type; pthread_mutex_unlock(&mutex); while(elev_get_button_signal(button_type,floor)); break; } } if(button_is_pressed) break; } if(button_is_pressed){ puts("Button is pressed, creating thread\n"); button_is_pressed=0; char transmit[BUFSIZE]; serialization(BUTTON_CHECK, message_send, transmit); pthread_t message_send_thread; pthread_create(&message_send_thread, NULL , tcp_send, (void*)transmit); if(message_send.button.type==BUTTON_INSIDE){ 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); } } if(!connection){ break; } } }
int elev_init(void) { int init_success = io_init(); if (!init_success){ return 0; } for (int f = 0; f < N_FLOORS; f++) { for (elev_button_type_t b = 0; b < N_BUTTONS; b++){ elev_set_button_lamp(b, f, 0); } } elev_set_stop_lamp(0); elev_set_door_open_lamp(0); elev_set_floor_indicator(0); return 1; }
void elev_init(void) { puts("ELEVATOR INIT CALLED"); int init_success = io_init(); assert(init_success && "Unable to initialize elevator hardware!"); elev_set_stop_lamp(0); elev_set_door_open_lamp(0); for (int f = 0; f < N_FLOORS; f++) { for (elev_button_type_t b = 0; b < N_BUTTONS; b++){ elev_set_button_lamp(b, f, 0); } } while(elev_get_floor_sensor_signal() != 0){ elev_set_motor_direction(DIRN_DOWN); } elev_set_motor_direction(DIRN_STOP); elev_set_floor_indicator(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); }
void* client_message_handler(void *msg_recv){ puts("Client message handler started"); pthread_mutex_init(&mutex,NULL); char receive[BUFSIZE]; bzero(receive,BUFSIZE); char *buf = (char*)msg_recv; for(int i = 0;i<BUFSIZE;i++){ receive[i] = buf[i]; } Message message_receive = deserialization(receive); switch(message_receive.type){ case ELEV_ORDER: puts("ELEV ORDER"); elev_set_button_lamp(BUTTON_CALL_UP, 0, message_receive.orders[0][0]); elev_set_button_lamp(BUTTON_CALL_UP, 1, message_receive.orders[1][0]); elev_set_button_lamp(BUTTON_CALL_UP, 2, message_receive.orders[2][0]); elev_set_button_lamp(BUTTON_CALL_DOWN, 1, message_receive.orders[3][0]); elev_set_button_lamp(BUTTON_CALL_DOWN, 2, message_receive.orders[4][0]); elev_set_button_lamp(BUTTON_CALL_DOWN, 3, message_receive.orders[5][0]); for(int i=0;i<6;i++){ //Global queue elev_orders[i][0] = message_receive.orders[i][0]; elev_orders[i][1] = message_receive.orders[i][1]; queue[i][0] = message_receive.orders[i][0]; queue[i][1] = message_receive.orders[i][1]; } puts("\nNew queue"); for(int i = 0;i<6;i++){ printf("Order: %d \t ID: %d\n",queue[i][0],queue[i][1]); } printf("Message_recieve.ID: %d\n", message_receive.ID); printf("My ID: %d\n", my_ID); if(message_receive.ID==my_ID){ //Sets local queue based on ID puts("Order recieved"); pthread_mutex_lock(&mutex); if(elevator.queue[elevator.floor_current]==0){ elevator.queue[message_receive.elevator.new_floor_order]=2; } pthread_mutex_unlock(&mutex); //elevator.new_floor_order=1; } break; case ORDER_UPDATE: puts("ORDER UPDATE"); for(int i=0;i<6;i++){ //Global queue elev_orders[i][0] = message_receive.orders[i][0]; elev_orders[i][1] = message_receive.orders[i][1]; if(my_ID == message_receive.orders[i][1]){ if(i == 0){ if(elevator.queue[0]==0){ elevator.queue[0] = 1; } } else if(i == 1 || i == 3){ if(elevator.queue[1]==0){ elevator.queue[1] = 1; } } else if(i == 2 || i == 4){ if(elevator.queue[2]==0){ elevator.queue[2] = 1; } } else if(i == 5) if(elevator.queue[3]==0){ elevator.queue[3] = 1; } } } printf("Elev queue: "); for(int i = 0;i<N_FLOORS;i++){ printf("%d ",elevator.queue[i]); } printf("\n"); break; case BUTTON_LAMP: puts("BUTTON LAMP"); elev_set_button_lamp(BUTTON_CALL_UP, 0, message_receive.orders[0][0]); elev_set_button_lamp(BUTTON_CALL_UP, 1, message_receive.orders[1][0]); elev_set_button_lamp(BUTTON_CALL_UP, 2, message_receive.orders[2][0]); elev_set_button_lamp(BUTTON_CALL_DOWN, 1, message_receive.orders[3][0]); elev_set_button_lamp(BUTTON_CALL_DOWN, 2, message_receive.orders[4][0]); elev_set_button_lamp(BUTTON_CALL_DOWN, 3, message_receive.orders[5][0]); break; case ID_UPDATE: puts("ID UPDATE"); my_ID = message_receive.ID; char id_upd[BUFSIZE]; Message message_send_ID; message_send_ID.ID = my_ID; serialization(ID_UPDATE, message_send_ID, id_upd); pthread_t message_send_thread_ID; pthread_create(&message_send_thread_ID, NULL , tcp_send, (void*)id_upd); break; case NETCHECK: puts("NETCHECK"); master_checkin = 1; char net_check[BUFSIZE]; Message message_send; serialization(NETCHECK, message_send, net_check); pthread_t message_send_thread; pthread_create(&message_send_thread, NULL , tcp_send, (void*)net_check); sleep(6); master_checkin = 0; sleep(5); if(master_checkin == 0){ puts("Master disconnected\n"); connection = 0; } break; } }
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; } } }
/* * Class: no_ntnu_stud_torbjovn_comedielevator_NativeInterface * Method: elev_set_button_lamp * Signature: (III)V */ JNIEXPORT void JNICALL Java_no_ntnu_stud_torbjovn_comedielevator_NativeInterface_elev_1set_1button_1lamp (JNIEnv * env, jobject obj, jint button, jint floor, jint value) { elev_set_button_lamp(getButtonType(button), floor, value); }
int inner_button_light_switch(int floor, int value) { elev_set_button_lamp(BUTTON_COMMAND, floor, value); return 0; }
int button_up_light_switch(int floor, int value) { elev_set_button_lamp(BUTTON_CALL_UP, floor, value); return 0; }
void ElevatorFSM::buttonPressed(int floor, elev_button_type_t buttonType) { localElevator->setFloor(floor, buttonType); elev_set_button_lamp(buttonType, floor, true); }
void* goto_floor_with_network(int targetFloor){ // Baserer meg her på at heis befinner seg i etasje //Try to reconnect //If orders in orderlist int currentFloor; int directionIndex; int flag = 0; // Funksjoner som sjekker bestillinger kan implementeres som tråder og fjernes fra tilstandsmaskiner printf("Searching for orders\n"); //While løkke kjører så lenge bestillingslister har elementer i seg printf("Element in ButtonVector, internalButtonVector: %i externalButtonVector: %i\n", !check_if_internalButtonVector_is_empty(), !check_if_externalButtonVector_is_empty() ); print_button_vectors(); currentFloor = elev_get_floor_sensor_signal(); lastFloor = currentFloor; printf("TargetFloor etter end_order_in_direction %i \n", targetFloor); direction = get_direction(currentFloor, targetFloor); directionIndex = direction_to_index(direction); // Til bruk når vi aksesser externalButtonVector printf("Direction er %i directionIndex er %i\n", direction, directionIndex ); set_motor_direction(lastFloor, targetFloor); time_t start, end; double elapsed = 0; time(&start); while(currentFloor != targetFloor){ //Heis beveger seg mot targetFloor i gitt retning if (elapsed > 5 ){ connectStatus = 0; printf("Motor Disconnected\n"); while(!((currentFloor != -1) && (currentFloor != lastFloor) )){ elapsed = 0; time(&start); currentFloor = elev_get_floor_sensor_signal(); } connectStatus = 1; printf("Motor connected\n"); } else{ connectStatus = 1; time(&end); } elapsed = difftime(end, start); if((currentFloor != -1) && (currentFloor != lastFloor) ){ //Elevator have reached new floor time(&start); lastFloor = currentFloor; elev_set_floor_indicator(lastFloor); if (internalButtonVector[currentFloor]){ pthread_mutex_lock(&elevLock); internalButtonVector[currentFloor] = 0; pthread_mutex_unlock(&elevLock); elev_set_motor_direction(DIRN_STOP); printf("Open door 1\n"); elev_set_button_lamp(BUTTON_COMMAND, currentFloor, 0); open_door(); flag = 1; } if(externalButtonVector[currentFloor][directionIndex] ){ pthread_mutex_lock(&elevLock); externalButtonVector[currentFloor][directionIndex] = 0; send_order_handeled_confirmation(currentFloor, directionIndex); pthread_mutex_unlock(&elevLock); if (flag != 1){ elev_set_motor_direction(DIRN_STOP); printf("Open door 1\n"); elev_set_button_lamp(BUTTON_COMMAND, currentFloor, 0); open_door(); flag = 0; } } } set_motor_direction(lastFloor, targetFloor); pthread_mutex_lock(&elevLock); targetFloor = end_order_in_direction(direction, targetFloor); // Oppdaterer targetfloor under reise pthread_mutex_unlock(&elevLock); // printf("LastFloor er %i\n", lastFloor); currentFloor = elev_get_floor_sensor_signal(); // sleep(0.2); //Forsinke prints } //Exit while -> framme ved targetFloor if ( (currentFloor == targetFloor) ){ lastFloor = currentFloor; elev_set_floor_indicator(lastFloor); elev_set_motor_direction(DIRN_STOP); printf("Open door 2, framme ved targetFloor %i\n", targetFloor); open_door(); elev_set_button_lamp(BUTTON_COMMAND, targetFloor, 0); pthread_mutex_lock(&elevLock); internalButtonVector[targetFloor] = 0; externalButtonVector[targetFloor][0] = 0; // I denne bestillingsslettingen kan det fort ligge bug externalButtonVector[targetFloor][1] = 0; pthread_mutex_unlock(&elevLock); send_order_handeled_confirmation(targetFloor, 0); send_order_handeled_confirmation(targetFloor, 1); direction = 0; } // sleep(0.2); return NULL; }