CIS441 Proj MS 2b

Dependencies:   TextLCD MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Road.cpp Source File

Road.cpp

00001 #include <math.h>
00002 #include "Road.h"
00003 
00004 Road::Road() {
00005     active_cars = 0x00;
00006     last_car = NULL;
00007     Road::road_in_use(this);
00008         
00009     intersection_car = -1;
00010     next_release = 0;
00011     num_cars = 0;
00012     wait_counter = 0; 
00013     
00014     pending_car = new AccCar(num_cars, this, pow(2, num_cars));
00015     pending_car->set_target_speed(rand() % 11 + 5);
00016 }
00017 
00018 Road* Road::road_in_use(Road* new_road) {
00019     static Road* road_in_use = NULL;
00020     if (new_road != NULL) {
00021         road_in_use = new_road;
00022     }
00023     return road_in_use;
00024 }
00025 
00026 int Road::ready(int new_ready) {
00027     static int ready = -1;
00028     if (new_ready != -1) {
00029         ready = new_ready;
00030     }
00031     return ready;
00032 }
00033 
00034 int Road::try_enter_car(int time) {
00035     if( next_release <= time && pending_car != NULL ) {
00036         if( last_car == NULL || last_car->position >= pending_car->speed + 2 ) {           
00037             pending_car->set_forward_car(last_car);
00038             pending_car->reset();
00039             active_cars = active_cars | pending_car->flag;
00040             
00041             last_car = pending_car;
00042             cars[num_cars] = pending_car;
00043             
00044             num_cars++;
00045             
00046             if(num_cars < MAX_CARS) {
00047                 pending_car = new AccCar(num_cars, this, pow(2, num_cars));
00048                 pending_car->set_target_speed(rand() % 11 + 5);
00049                 
00050                 next_release = time + rand() % 3;
00051             } else {
00052                 pending_car = NULL;   
00053             }
00054             
00055             return last_car->id;
00056         }
00057     }
00058     
00059     return -1;
00060 }
00061  
00062  void Road::let_cars_update() {
00063      if( active_cars > 0x00 ) {
00064         go_flags.set(active_cars);
00065      }
00066  }
00067  
00068  void Road::wait_for_car_update() {
00069      if( active_cars > 0x00 ) {
00070         done_flags.wait_all(active_cars);
00071      }
00072  }
00073  
00074  int Road::check_exit_cars(int cars[]) {
00075      int arr_index = 0;
00076      
00077      for( int i=0; i < num_cars; i++ ) {
00078          if( this->cars[i]->position >= 100 ) {
00079             cars[arr_index] = this->cars[i]->id;
00080             arr_index++;
00081             
00082             active_cars = active_cars ^ this->cars[i]->flag;
00083             
00084             this->cars[i]->position = -1;
00085             this->cars[i]->speed = -1;
00086          }
00087      }
00088      
00089      return arr_index;   
00090  }
00091  
00092 void Road::update_car_speed(int id, int speed) {
00093     cars[id]->speed = speed;
00094 }
00095  
00096  void Road::print_status() {
00097      for( int i=0; i < num_cars; i++ ) {
00098          printf("Car %d: %d -> %d\r\n", cars[i]->id, cars[i]->position, cars[i]->speed); 
00099      }   
00100  }
00101  
00102 void Road::publish_car_info() {
00103     for (int i = 0; i < num_cars; i++) {
00104         Communication::publish_car_info(i, cars[i]->position, cars[i]->speed);
00105     }
00106     for (int i = num_cars; i < MAX_CARS; i++) {
00107         Communication::publish_car_info(i, 0, 0);
00108     }
00109 }
00110 
00111 void Road::update_wait_counter() {
00112     for (int i = 0; i < num_cars; i++) {
00113         if (cars[i]->position == 54) {
00114             wait_counter++; 
00115             printf("Wait Counter: %d\r\n", wait_counter); 
00116         } 
00117     } 
00118 } 
00119 
00120 void Road::wait_for_sync() {
00121     done_flags.wait_all(0xF0);
00122 }