Eric Micallef / Mbed OS smat_controller

Dependencies:   MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Road.cpp Source File

Road.cpp

00001 #include "Road.h"
00002 
00003 #define ROADLENGTH 100
00004 
00005 Road::Road(int id) 
00006 {    
00007     road_id = id;
00008     active_cars = 0;
00009     active_car_bits = 0x00;
00010     road_clock = 0;
00011     // clear out our pointer table
00012     for (int i = 0; i < MAX_CARS_ON_ROAD; ++i)
00013     {
00014         car_table[i] = NULL;
00015     }
00016 }
00017  
00018 void Road::add_acc_car(AccCar* car) {
00019     
00020     // make sure all is well on the road
00021     assert( active_cars < MAX_CARS_ON_ROAD );
00022     
00023     car_table[active_cars] = car; 
00024        
00025     // no need to lock as main thread is only callee
00026     active_car_bits = active_car_bits | car->flag;
00027     active_cars++;
00028 }
00029  
00030 void Road::let_cars_update() {
00031     go_flags.set(active_car_bits);
00032     road_clock++;
00033 }
00034  
00035 void Road::wait_for_car_update() {
00036     done_flags.wait_all(active_car_bits);
00037 }
00038 
00039 bool Road::can_car_enter(int speed)
00040 {
00041     // if no cars on the road enter
00042     if(active_cars == 0)
00043         return true;
00044     
00045     // else make sure you have not exceed the max amount of cars and make sure you can safely enter
00046     // safely being that you maintain your safety gap with your speed
00047     AccCar* caboose = car_table[active_cars-1];
00048     assert( caboose != NULL);    
00049     if(active_cars < MAX_CARS_ON_ROAD && speed <= caboose->position - 2 ) // 2 is safety gap
00050     {
00051         return true;    
00052     }
00053     return false;
00054 }
00055 
00056 // whats the id of next car
00057 // used to set bit flags really
00058 int Road::get_new_car_id()
00059 {
00060     return active_cars;   
00061 }
00062 
00063 // how many active cars are there
00064 int Road::get_active_cars()
00065 {
00066     return active_cars;   
00067 }
00068 
00069 // get a car pointer based off some id
00070 AccCar* Road::get_car(int id)
00071 {
00072     assert(id < MAX_CARS_ON_ROAD);
00073     return car_table[id];    
00074 }
00075 
00076 // 0 is the lead car 
00077 // 1 is the second car and so on
00078 AccCar* Road::get_forward_car(int id)
00079 {
00080     // make sure we don't go out of bounds
00081     assert( id >= 0 && id <= MAX_CARS_ON_ROAD );
00082     if( id > 0 )
00083         return car_table[id-1];
00084     else
00085         return NULL;  // return null means there is no forward car 
00086 }  
00087 
00088 // manages any clean up
00089 void Road::DESTROY_ALL_CARS()
00090 {
00091     for( int i = 0; i < active_cars; i++)
00092     {
00093         AccCar* car = car_table[i];
00094         car->stop();
00095         delete car;
00096     }   
00097 }
00098 
00099 // dumb function to end simulation only returns true when we have 
00100 // 5 cars and the last car is passed 100 or road length
00101 bool Road::simulating()
00102 {
00103     if(active_cars != MAX_CARS_ON_ROAD )
00104         return true;
00105     
00106     else
00107     {
00108         AccCar* car = car_table[active_cars-1];
00109         assert( car != NULL );
00110         if(car->position > ROADLENGTH) // TODO use macro 
00111             return false; 
00112     }
00113     return true;
00114 }
00115 
00116 // typical getter functions
00117 int Road::get_road_id()
00118 {
00119     return road_id;   
00120 }
00121 
00122 int Road::get_road_clock()
00123 {
00124     return road_clock;
00125 }
00126 
00127 AccCar* Road::get_last_car()
00128 {
00129     return car_table[active_cars-1];    
00130 }
00131