Matthew Goldsmith / Mbed OS cis441projMS2b

Dependencies:   TextLCD MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.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 "Road.h"
00008 #include "AccCar.h"
00009 #include "TextLCD.h"
00010 
00011 // main() runs in its own thread in the OS
00012 int main() {
00013     Communication::init();
00014     wait(1);
00015     printf("starting simulation/r/n");
00016     // ------------------------------------------------------------------------------
00017     // The following three variables are used for timing statistics, do not modify them
00018     Timer stopwatch;    // A timer to keep track of how long the updates take, for statistics purposes
00019     int numberCycles = 0;
00020     int totalUpdateTime = 0;
00021     // ------------------------------------------------------------------------------
00022 
00023     int time = 0;
00024 
00025     Road road1;
00026 
00027     stopwatch.start();
00028     stopwatch.reset();
00029     
00030     int return_cars[MAX_CARS];
00031     
00032     do {
00033         Road::ready(0);
00034         Communication::yield(50); 
00035 
00036         int new_cars = road1.try_enter_car(time);
00037         Communication::yield(50); 
00038 
00039         road1.let_cars_update();
00040         Communication::yield(50); 
00041         printf("main.cpp: update flags raised\n");
00042                 
00043         road1.wait_for_car_update();
00044         printf("main.cpp: finished updating\n"); 
00045                 
00046         printf("\r\nRoad 1 Update %d\r\n", time);
00047         road1.print_status();
00048 
00049         printf("\r\n");
00050         road1.check_exit_cars(return_cars);
00051 
00052         time++;
00053         
00054         // ------------------------------------------------------------------
00055         // Timing statistics logic, do not modify
00056         totalUpdateTime += stopwatch.read_ms();
00057         numberCycles++;
00058         stopwatch.reset();
00059         // ------------------------------------------------------------------
00060 
00061         road1.publish_car_info();
00062         printf("successful publish: car info\r\n");
00063         Communication::publish_road_ready();
00064         
00065         Communication::yield(50); 
00066         while(Road::ready(-1) == 0) {
00067             Communication::yield(50); 
00068         }
00069         road1.update_wait_counter(); 
00070         printf("----------------------\r\n"); 
00071     } while( road1.active_cars > 0x00);
00072     
00073     // ----------------------------------------------------------------------
00074     // Timing statistics printout, do not modify
00075     printf("Average Wait Time: %fs \r\n", road1.wait_counter / 5.0); 
00076     printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0));
00077     totalUpdateTime = 0;
00078     numberCycles = 0;
00079     // ----------------------------------------------------------------------
00080     while (true) {
00081         Communication::publish_road_ready();
00082         wait(0.1); 
00083     }
00084 }