gchar pathfinder_explore_least_seen (PathFinder *pf, Tile *tile) { assert (tile != NULL); assert (tile_is_flag_set (tile, TILE_FLAG_HAS_ANT)); assert (ant_get_owner (&tile->with.ant) == 0); gchar dir = DIR_NONE; gchar last_dir = ant_get_direction (&tile->with.ant); DirectionIndex last_dir_index = direction_to_index (last_dir); Cardinals look = { NULL, NULL, NULL, NULL }; map_get_cardinals (pf->map, tile_get_row (tile), tile_get_col (tile), look); guint n_seen = G_MAX_UINT; DirectionIndex di; for (di = DI_FIRST; di < DI_LAST; di++) { Tile *t = look[di]; if (tile_is_flag_set (t, TILE_FLAG_IS_WATER) || tile_is_flag_set (t, TILE_FLAG_HAS_ANT)) continue; guint s = tile_get_seen (t); if (s < n_seen || (s == n_seen && last_dir_index == di)) { n_seen = s; dir = direction_from_index (di); } } return dir; }
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; }