For Evans
Revision 0:f17da79e74c9, committed 2019-10-09
- Comitter:
- MannyK
- Date:
- Wed Oct 09 16:29:06 2019 +0000
- Commit message:
- For Evans
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HW05Code/AccCar.cpp Wed Oct 09 16:29:06 2019 +0000
@@ -0,0 +1,67 @@
+#include "AccCar.h"
+#include <stdlib.h>
+#include <algorithm>
+
+#define TICK 1000
+#define MONITOR_DIST 17
+#define SAFETY_GAP 2
+
+AccCar::AccCar(int id, Road* road, int flag) {
+ this->id = id;
+ this->road = road;
+ this->flag = flag;
+ cycle = 0;
+ this->thread = NULL;
+}
+
+void AccCar::set_forward_car(AccCar* car) {
+ this->forward_car = car;
+}
+
+void AccCar::update() {
+ while (true) {
+ ThisThread::sleep_for(TICK);
+ road->go_flags.wait_all(flag);
+ cycle++;
+
+ position = position + speed;
+
+ if (cycle % 5 == 0) {
+ speed = rand() % 11 + 5;
+ }
+
+ if (forward_car->position - position < MONITOR_DIST) {
+ road->done_flags.wait_all(forward_car->flag, osWaitForever, false);
+
+ int diff = forward_car->position - position;
+ int maxSafeSpeed = diff - SAFETY_GAP;
+
+ speed = std::min(maxSafeSpeed, target_speed);
+ } else {
+ speed = target_speed;
+ }
+
+ road->done_flags.set(flag);
+ }
+}
+
+void AccCar::reset() {
+ road->done_flags.clear(flag);
+
+ if (thread != NULL) {
+ thread->terminate();
+ }
+
+ thread = new Thread();
+ thread->start( callback(this, &AccCar::update) );
+
+ this->position = 0;
+ this->speed = rand() % 11 + 5;
+ this->target_speed = speed;
+}
+
+void AccCar::stop() {
+ if (thread != NULL) {
+ thread->terminate();
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HW05Code/AccCar.h Wed Oct 09 16:29:06 2019 +0000
@@ -0,0 +1,32 @@
+#ifndef _ACC_CAR_H_
+#define _ACC_CAR_H_
+
+#include "mbed.h"
+#include "Car.h"
+#include "Road.h"
+
+class Road;
+class Car;
+
+class AccCar{
+public:
+ int position;
+ int speed;
+ int flag;
+
+ AccCar(int id, Road* road, int flag);
+ void set_forward_car(AccCar* car);
+ void update();
+ void reset();
+ void stop();
+
+protected:
+ int id;
+ int cycle;
+ int target_speed;
+ AccCar* forward_car;
+
+ Road* road;
+ Thread* thread;
+};
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HW05Code/Car.cpp Wed Oct 09 16:29:06 2019 +0000
@@ -0,0 +1,51 @@
+#include "Car.h"
+#include <stdlib.h>
+
+#define TICK 1000
+
+Car::Car(int id, Road* road, int flag) {
+ this->id = id;
+ this->road = road;
+ this->flag = flag;
+
+ cycle = 0;
+
+ this->thread = NULL;
+}
+
+void Car::update() {
+ while (true) {
+ ThisThread::sleep_for(TICK);
+ road->go_flags.wait_all(flag);
+ cycle++;
+
+ position = position + speed;
+
+ if (cycle % 5 == 0) {
+ speed = rand() % 11 + 5;
+ }
+
+ road->done_flags.set(flag);
+ }
+}
+
+void Car::reset(int position, int speed) {
+ road->done_flags.clear(flag);
+
+ if (thread != NULL) {
+ thread->terminate();
+ }
+
+ thread = new Thread();
+ thread->start( callback(this, &Car::update) );
+
+ cycle = 0;
+ this->position = position;
+ this->speed = speed;
+}
+
+void Car::stop() {
+ if (thread != NULL) {
+ thread->terminate();
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HW05Code/Car.h Wed Oct 09 16:29:06 2019 +0000
@@ -0,0 +1,26 @@
+#ifndef _CAR_H_
+#define _CAR_H_
+
+#include "Road.h"
+#include "mbed.h"
+
+class Road;
+
+class Car {
+public:
+ int position;
+ int speed;
+ int flag;
+
+ Car(int id, Road* road, int flag);
+ void update();
+ void reset(int position, int speed);
+ void stop();
+
+protected:
+ int id;
+ int cycle;
+ Road* road;
+ Thread* thread;
+};
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HW05Code/Road.cpp Wed Oct 09 16:29:06 2019 +0000
@@ -0,0 +1,25 @@
+#include "Road.h"
+
+Road::Road() {
+ active_cars = 0x00;
+}
+
+void Road::add_car(Car* car) {
+ this->car1 = car;
+
+ active_cars = active_cars | car->flag;
+}
+
+void Road::add_acc_car(AccCar* car) {
+ this->car2 = car;
+
+ active_cars = active_cars | car->flag;
+}
+
+void Road::let_cars_update() {
+ go_flags.set(active_cars);
+}
+
+void Road::wait_for_car_update() {
+ done_flags.wait_all(active_cars);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HW05Code/Road.h Wed Oct 09 16:29:06 2019 +0000
@@ -0,0 +1,30 @@
+#ifndef _ROAD_H_
+#define _ROAD_H_
+
+#include "mbed.h"
+#include "Car.h"
+#include "AccCar.h"
+
+class Car;
+class AccCar;
+
+
+class Road {
+public:
+ EventFlags go_flags;
+ EventFlags done_flags;
+
+ Road();
+ void add_car(Car* car);
+ void add_acc_car(AccCar* car);
+ void let_cars_update();
+ void wait_for_car_update();
+
+
+private:
+ Car* car1;
+ AccCar* car2;
+
+ int active_cars;
+};
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HW05Code/main.cpp Wed Oct 09 16:29:06 2019 +0000
@@ -0,0 +1,117 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2018 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "mbed.h"
+#include <cctype>
+#include "Car.h"
+#include "AccCar.h"
+#include "TextLCD.h"
+#include "Road.h"
+
+Serial pc(USBTX, USBRX);
+TextLCD lcd(p15, p16, p17, p18, p19, p20);
+
+#define ROADLENGTH 100
+
+// Read the max number of services to perform from pc input
+int read_int(char* prompt) {
+ int maxService = 0;
+
+ pc.printf(prompt);
+
+ char input;
+ while(1) {
+ input = pc.getc();
+ pc.putc(input);
+
+ if( std::isdigit(input) ) {
+ maxService = (maxService * 10) + (input-'0');
+ } else {
+ pc.putc(input);
+ break;
+ }
+ }
+
+ return maxService;
+}
+
+// main() runs in its own thread in the OS
+int main()
+{
+ // ------------------------------------------------------------------------------
+ // The following three variables are used for timing statistics, do not modify them
+ Timer stopwatch; // A timer to keep track of how long the updates take, for statistics purposes
+ int numberCycles = 0;
+ int totalUpdateTime = 0;
+ // ------------------------------------------------------------------------------
+
+ Road road;
+ AccCar car1(1, &road, 0x01);
+// AccCar car2(2, &road, 0x02);
+// AccCar car3(3, &road, 0x03);
+// AccCar car4(4, &road, 0x04);
+// AccCar car5(5, &road, 0x05);
+
+ road.add_acc_car(&car1);
+
+
+ stopwatch.start();
+
+// car2.set_forward_car(&car1);
+// car3.set_forward_car(&car2);
+// car4.set_forward_car(&car3);
+// car5.set_forward_car(&car4);
+
+ car1.reset();
+// car2.reset();
+// car3.reset();
+// car4.reset();
+// car5.reset();
+
+ int timerForCar2 = rand() % 3 + 1;
+ int timerForCar3 = rand() % 3 + 1;
+ int timerForCar4 = rand() % 3 + 1;
+ int timerForCar5 = rand() % 3 + 1;
+
+ stopwatch.reset();
+
+ do {
+
+ road.let_cars_update();
+ road.wait_for_car_update();
+ // ------------------------------------------------------------------
+ // Timing statistics logic, do not modify
+ totalUpdateTime += stopwatch.read_ms();
+ numberCycles++;
+ stopwatch.reset();
+ // ------------------------------------------------------------------
+ // if (numberCycles == timerForCar2){
+// road.add_acc_car(&car2);
+// }
+// if (numberCycles == (timerForCar2+timerForCar3)){
+// road.add_acc_car(&car3);
+// }
+// if (numberCycles == (timerForCar2+timerForCar3+timerForCar4)){
+// road.add_acc_car(&car4);
+// }
+ lcd.cls();
+ pc.printf("1 %d -> %d\n", car1.position, car1.speed);
+ //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);
+ lcd.printf("1 %d -> %d\n", car1.position, car1.speed);
+
+ } while (car1.position <= ROADLENGTH);
+ car1.stop();
+// car2.stop();
+// car3.stop();
+// car4.stop();
+// car5.stop();
+
+ // ----------------------------------------------------------------------
+ // Timing statistics printout, do not modify
+ pc.printf("Average update cycle took: %fms \r\n", (totalUpdateTime*1.0)/(numberCycles*1.0));
+ totalUpdateTime = 0;
+ numberCycles = 0;
+ // ----------------------------------------------------------------------
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Wed Oct 09 16:29:06 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/simon/code/TextLCD/#308d188a2d3a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Wed Oct 09 16:29:06 2019 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#e83fd322bd9177f60ccfd6712849b7db71869f00