Emanuel Kuflik / Mbed OS HW05

Dependencies:   TextLCD

Committer:
MannyK
Date:
Wed Oct 09 16:29:06 2019 +0000
Revision:
0:f17da79e74c9
For Evans

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MannyK 0:f17da79e74c9 1 /* mbed Microcontroller Library
MannyK 0:f17da79e74c9 2 * Copyright (c) 2018 ARM Limited
MannyK 0:f17da79e74c9 3 * SPDX-License-Identifier: Apache-2.0
MannyK 0:f17da79e74c9 4 */
MannyK 0:f17da79e74c9 5
MannyK 0:f17da79e74c9 6 #include "mbed.h"
MannyK 0:f17da79e74c9 7 #include <cctype>
MannyK 0:f17da79e74c9 8 #include "Car.h"
MannyK 0:f17da79e74c9 9 #include "AccCar.h"
MannyK 0:f17da79e74c9 10 #include "TextLCD.h"
MannyK 0:f17da79e74c9 11 #include "Road.h"
MannyK 0:f17da79e74c9 12
MannyK 0:f17da79e74c9 13 Serial pc(USBTX, USBRX);
MannyK 0:f17da79e74c9 14 TextLCD lcd(p15, p16, p17, p18, p19, p20);
MannyK 0:f17da79e74c9 15
MannyK 0:f17da79e74c9 16 #define ROADLENGTH 100
MannyK 0:f17da79e74c9 17
MannyK 0:f17da79e74c9 18 // Read the max number of services to perform from pc input
MannyK 0:f17da79e74c9 19 int read_int(char* prompt) {
MannyK 0:f17da79e74c9 20 int maxService = 0;
MannyK 0:f17da79e74c9 21
MannyK 0:f17da79e74c9 22 pc.printf(prompt);
MannyK 0:f17da79e74c9 23
MannyK 0:f17da79e74c9 24 char input;
MannyK 0:f17da79e74c9 25 while(1) {
MannyK 0:f17da79e74c9 26 input = pc.getc();
MannyK 0:f17da79e74c9 27 pc.putc(input);
MannyK 0:f17da79e74c9 28
MannyK 0:f17da79e74c9 29 if( std::isdigit(input) ) {
MannyK 0:f17da79e74c9 30 maxService = (maxService * 10) + (input-'0');
MannyK 0:f17da79e74c9 31 } else {
MannyK 0:f17da79e74c9 32 pc.putc(input);
MannyK 0:f17da79e74c9 33 break;
MannyK 0:f17da79e74c9 34 }
MannyK 0:f17da79e74c9 35 }
MannyK 0:f17da79e74c9 36
MannyK 0:f17da79e74c9 37 return maxService;
MannyK 0:f17da79e74c9 38 }
MannyK 0:f17da79e74c9 39
MannyK 0:f17da79e74c9 40 // main() runs in its own thread in the OS
MannyK 0:f17da79e74c9 41 int main()
MannyK 0:f17da79e74c9 42 {
MannyK 0:f17da79e74c9 43 // ------------------------------------------------------------------------------
MannyK 0:f17da79e74c9 44 // The following three variables are used for timing statistics, do not modify them
MannyK 0:f17da79e74c9 45 Timer stopwatch; // A timer to keep track of how long the updates take, for statistics purposes
MannyK 0:f17da79e74c9 46 int numberCycles = 0;
MannyK 0:f17da79e74c9 47 int totalUpdateTime = 0;
MannyK 0:f17da79e74c9 48 // ------------------------------------------------------------------------------
MannyK 0:f17da79e74c9 49
MannyK 0:f17da79e74c9 50 Road road;
MannyK 0:f17da79e74c9 51 AccCar car1(1, &road, 0x01);
MannyK 0:f17da79e74c9 52 // AccCar car2(2, &road, 0x02);
MannyK 0:f17da79e74c9 53 // AccCar car3(3, &road, 0x03);
MannyK 0:f17da79e74c9 54 // AccCar car4(4, &road, 0x04);
MannyK 0:f17da79e74c9 55 // AccCar car5(5, &road, 0x05);
MannyK 0:f17da79e74c9 56
MannyK 0:f17da79e74c9 57 road.add_acc_car(&car1);
MannyK 0:f17da79e74c9 58
MannyK 0:f17da79e74c9 59
MannyK 0:f17da79e74c9 60 stopwatch.start();
MannyK 0:f17da79e74c9 61
MannyK 0:f17da79e74c9 62 // car2.set_forward_car(&car1);
MannyK 0:f17da79e74c9 63 // car3.set_forward_car(&car2);
MannyK 0:f17da79e74c9 64 // car4.set_forward_car(&car3);
MannyK 0:f17da79e74c9 65 // car5.set_forward_car(&car4);
MannyK 0:f17da79e74c9 66
MannyK 0:f17da79e74c9 67 car1.reset();
MannyK 0:f17da79e74c9 68 // car2.reset();
MannyK 0:f17da79e74c9 69 // car3.reset();
MannyK 0:f17da79e74c9 70 // car4.reset();
MannyK 0:f17da79e74c9 71 // car5.reset();
MannyK 0:f17da79e74c9 72
MannyK 0:f17da79e74c9 73 int timerForCar2 = rand() % 3 + 1;
MannyK 0:f17da79e74c9 74 int timerForCar3 = rand() % 3 + 1;
MannyK 0:f17da79e74c9 75 int timerForCar4 = rand() % 3 + 1;
MannyK 0:f17da79e74c9 76 int timerForCar5 = rand() % 3 + 1;
MannyK 0:f17da79e74c9 77
MannyK 0:f17da79e74c9 78 stopwatch.reset();
MannyK 0:f17da79e74c9 79
MannyK 0:f17da79e74c9 80 do {
MannyK 0:f17da79e74c9 81
MannyK 0:f17da79e74c9 82 road.let_cars_update();
MannyK 0:f17da79e74c9 83 road.wait_for_car_update();
MannyK 0:f17da79e74c9 84 // ------------------------------------------------------------------
MannyK 0:f17da79e74c9 85 // Timing statistics logic, do not modify
MannyK 0:f17da79e74c9 86 totalUpdateTime += stopwatch.read_ms();
MannyK 0:f17da79e74c9 87 numberCycles++;
MannyK 0:f17da79e74c9 88 stopwatch.reset();
MannyK 0:f17da79e74c9 89 // ------------------------------------------------------------------
MannyK 0:f17da79e74c9 90 // if (numberCycles == timerForCar2){
MannyK 0:f17da79e74c9 91 // road.add_acc_car(&car2);
MannyK 0:f17da79e74c9 92 // }
MannyK 0:f17da79e74c9 93 // if (numberCycles == (timerForCar2+timerForCar3)){
MannyK 0:f17da79e74c9 94 // road.add_acc_car(&car3);
MannyK 0:f17da79e74c9 95 // }
MannyK 0:f17da79e74c9 96 // if (numberCycles == (timerForCar2+timerForCar3+timerForCar4)){
MannyK 0:f17da79e74c9 97 // road.add_acc_car(&car4);
MannyK 0:f17da79e74c9 98 // }
MannyK 0:f17da79e74c9 99 lcd.cls();
MannyK 0:f17da79e74c9 100 pc.printf("1 %d -> %d\n", car1.position, car1.speed);
MannyK 0:f17da79e74c9 101 //pc.printf("1 %d -> %d\n2 %d -> %d\n3 %d -> %d\n4 %d -> %d\n5 %d -> %d", car1.position, car1.speed, car2.position, car2.speed, car3.position, car3.speed, car4.position, car4.speed, car5.position, car5.speed);
MannyK 0:f17da79e74c9 102 lcd.printf("1 %d -> %d\n", car1.position, car1.speed);
MannyK 0:f17da79e74c9 103
MannyK 0:f17da79e74c9 104 } while (car1.position <= ROADLENGTH);
MannyK 0:f17da79e74c9 105 car1.stop();
MannyK 0:f17da79e74c9 106 // car2.stop();
MannyK 0:f17da79e74c9 107 // car3.stop();
MannyK 0:f17da79e74c9 108 // car4.stop();
MannyK 0:f17da79e74c9 109 // car5.stop();
MannyK 0:f17da79e74c9 110
MannyK 0:f17da79e74c9 111 // ----------------------------------------------------------------------
MannyK 0:f17da79e74c9 112 // Timing statistics printout, do not modify
MannyK 0:f17da79e74c9 113 pc.printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0));
MannyK 0:f17da79e74c9 114 totalUpdateTime = 0;
MannyK 0:f17da79e74c9 115 numberCycles = 0;
MannyK 0:f17da79e74c9 116 // ----------------------------------------------------------------------
MannyK 0:f17da79e74c9 117 }