Eric Micallef / Mbed OS smat_controller

Dependencies:   MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers car_controller.cpp Source File

car_controller.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 #include "mbed.h"
00007 #include <cctype>
00008 #include "AccCar.h"
00009 #include "Road.h"
00010 #include "mqtt.h"
00011 
00012 #define MAGIC_NUMBER 2
00013 
00014 #define ACC_DEBUG
00015 
00016 Serial pc(USBTX, USBRX);
00017 
00018 // prints statistics
00019 static void print_wait_time(Road* road)
00020 {
00021     int active_cars = road->get_active_cars();
00022     for( int i = 0; i < active_cars; i++)
00023     {
00024             AccCar* car = road->get_car(i);
00025             assert(car != NULL);
00026             pc.printf("Car %d Road %d wait_time %d \r\n", i,road->get_road_id(), car->wait_time); 
00027     }
00028 }
00029 
00030 // handles adding a new car safely to the intersection
00031 static void add_new_car(Road* road,int* cycles,mqtt* mqtt_singleton)
00032 {
00033         // get rand speed
00034         int temp_speed = rand() %11;
00035         temp_speed += 5;
00036             
00037         // 
00038         assert( temp_speed >=5 && temp_speed <= 15);
00039         
00040         if(road->can_car_enter( temp_speed ))
00041         {
00042             *cycles = 0; // we added a car reset our timer
00043             
00044             int id = road->get_new_car_id();
00045             
00046             AccCar* car = new AccCar(id, road, (1 << id),mqtt_singleton );
00047             assert(car != NULL);
00048             
00049 #ifdef ACC_DEBUG
00050             pc.printf("adding new car to road %d car %d \r\n",road->get_road_id(),id);
00051 #endif
00052 
00053             road->add_acc_car(car);
00054             
00055             AccCar* forward_car = road->get_forward_car(id);
00056             
00057             // lead car will get a null pointer
00058             car->set_forward_car( forward_car ); 
00059             
00060             // this needs to be a rand number [0,15]
00061             car->reset(temp_speed);
00062             
00063         }
00064 }
00065 
00066 void print_positions(Road* road)
00067 {
00068     int active_cars = road->get_active_cars();
00069     for( int i = 0; i < active_cars; i++)
00070     {
00071             AccCar* car = road->get_car(i);
00072             assert(car != NULL);
00073             pc.printf("Car %d Road %d position %d : speed %d cycles: %d car_cycles %d \r\n", i,road->get_road_id(), car->position,car->speed, road->get_road_clock(), car->get_cycles()); 
00074     }
00075 }
00076 
00077 
00078 int start_simulation(mqtt* mqtt_singleton)
00079 {   
00080     // ------------------------------------------------------------------------------
00081     // The following three variables are used for timing statistics, do not modify them
00082     Timer stopwatch;    // A timer to keep track of how long the updates take, for statistics purposes
00083     int numberCycles = 0;
00084     int totalUpdateTime = 0;
00085     
00086     // every 3 cycles we need to add a new car
00087     int new_car_cycles = 0;
00088 
00089     // ------------------------------------------------------------------------------
00090     // seed rand number generator
00091     srand(mqtt_singleton->mqtt_id);
00092     
00093     // make new road based off of ID
00094     Road* road = new Road(mqtt_singleton->mqtt_id);
00095         
00096     // add first cars to the road to kick everything off
00097     add_new_car(road,&new_car_cycles,mqtt_singleton);
00098 
00099 
00100     
00101     stopwatch.start();
00102     
00103     stopwatch.reset();
00104     do {
00105         
00106         // if we get a random number thats right OR if we hit our max of 3
00107         // then add a new car
00108         if( (rand() %3) == MAGIC_NUMBER || new_car_cycles == 3)
00109         {
00110             // add new car to road and then set the forward car to it
00111             add_new_car(road,&new_car_cycles,mqtt_singleton);  
00112         }
00113         
00114         //
00115         road->let_cars_update();
00116         
00117         //
00118         road->wait_for_car_update();
00119 
00120         // ------------------------------------------------------------------
00121         // Timing statistics logic, do not modify
00122         totalUpdateTime += stopwatch.read_ms();
00123         numberCycles++;
00124         new_car_cycles++;
00125         stopwatch.reset();
00126         // ------------------------------------------------------------------
00127         
00128         print_positions(road);
00129         
00130     // TODO figure out how to end simulation like they want
00131     } while (road->simulating()); 
00132     
00133     // ----------------------------------------------------------------------
00134     // Timing statistics printout, do not modify
00135     pc.printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0));
00136     totalUpdateTime = 0;
00137     numberCycles = 0;
00138     
00139     // print wait times
00140     print_wait_time(road);
00141     
00142     // ----------------------------------------------------------------------
00143     road->DESTROY_ALL_CARS();
00144     return 0;
00145 }
00146