Railway Challenge
/
challenge
Uncommenting of part that allow supercaps to charge up from the batteries
dashboard.cpp@30:a04afea230fb, 2022-06-15 (annotated)
- Committer:
- jamesmcildowietfl
- Date:
- Wed Jun 15 11:33:56 2022 +0000
- Revision:
- 30:a04afea230fb
- Parent:
- 22:4cf934f6e989
Forgot to add light pins to header, done now ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rwcjoliver | 0:4788e1df7b55 | 1 | #include <mbed.h> |
rwcjoliver | 0:4788e1df7b55 | 2 | #include "definitions.h" |
rwcjoliver | 0:4788e1df7b55 | 3 | #include "dashboard.h" |
rwcjoliver | 0:4788e1df7b55 | 4 | |
rwcjoliver | 0:4788e1df7b55 | 5 | Dashboard::Dashboard(InterruptIn& hallSensor) : _hallSensor(hallSensor) { // CONSTRUCTOR |
rwcjoliver | 0:4788e1df7b55 | 6 | |
rwcjoliver | 0:4788e1df7b55 | 7 | _hallSensor.rise(this, &Dashboard::tachoInterrupt); // Register everytime hall sensor is detected |
rwcjoliver | 0:4788e1df7b55 | 8 | tachoTimer.start(); // Timer for counting time between hall sensor triggers |
rwcjoliver | 0:4788e1df7b55 | 9 | |
jamesmcildowietfl | 22:4cf934f6e989 | 10 | numberOfMagnets = 8; // How many magnets on the wheels |
rwcjoliver | 0:4788e1df7b55 | 11 | |
rwcjoliver | 0:4788e1df7b55 | 12 | currentSpeed = 0.00f; |
rwcjoliver | 0:4788e1df7b55 | 13 | passedTime_ms = 0.00f; |
rwcjoliver | 0:4788e1df7b55 | 14 | passedTime = 0.00f; |
rwcjoliver | 0:4788e1df7b55 | 15 | lastTime = 0.00f; |
rwcjoliver | 0:4788e1df7b55 | 16 | wheelCircumference = 0.73513268f; // in meters. Radius = 0.117m |
rwcjoliver | 0:4788e1df7b55 | 17 | |
rwcjoliver | 0:4788e1df7b55 | 18 | currentDistance = 0.00f; |
rwcjoliver | 0:4788e1df7b55 | 19 | tachoCounter = 0; |
rwcjoliver | 0:4788e1df7b55 | 20 | currentTime = 0; |
rwcjoliver | 0:4788e1df7b55 | 21 | } |
rwcjoliver | 0:4788e1df7b55 | 22 | |
rwcjoliver | 0:4788e1df7b55 | 23 | void Dashboard::tachoInterrupt() { |
rwcjoliver | 0:4788e1df7b55 | 24 | |
rwcjoliver | 0:4788e1df7b55 | 25 | currentTime = tachoTimer.read_ms(); |
rwcjoliver | 0:4788e1df7b55 | 26 | passedTime = (currentTime - lastTime) / 1000; |
rwcjoliver | 0:4788e1df7b55 | 27 | pc.printf("Passed Time: %.2f\r\n", passedTime); |
rwcjoliver | 0:4788e1df7b55 | 28 | |
rwcjoliver | 0:4788e1df7b55 | 29 | // if (passedTime > 0.03) { |
rwcjoliver | 0:4788e1df7b55 | 30 | |
rwcjoliver | 0:4788e1df7b55 | 31 | // tachoCounter++; // Increment tacho counter = number of strips detected |
rwcjoliver | 0:4788e1df7b55 | 32 | //pc.printf("count = %d\r\n", tachoCounter); |
rwcjoliver | 0:4788e1df7b55 | 33 | |
rwcjoliver | 0:4788e1df7b55 | 34 | // getCurrentSpeed(); |
rwcjoliver | 0:4788e1df7b55 | 35 | // // PASSED TIME IN ms |
rwcjoliver | 0:4788e1df7b55 | 36 | // passedTime_ms = tachoTimer.read_ms() - lastTime;// - 21; // 20ms propogation delay |
rwcjoliver | 0:4788e1df7b55 | 37 | // |
rwcjoliver | 0:4788e1df7b55 | 38 | // if (passedTime_ms > 100) { // IGNORE SHORT PULSES |
rwcjoliver | 0:4788e1df7b55 | 39 | // lastTime = tachoTimer.read_ms(); |
rwcjoliver | 0:4788e1df7b55 | 40 | currentDistance += (wheelCircumference / numberOfMagnets); |
rwcjoliver | 0:4788e1df7b55 | 41 | pc.printf("Current Distance = %.2f\r\n", currentDistance); |
rwcjoliver | 0:4788e1df7b55 | 42 | |
rwcjoliver | 0:4788e1df7b55 | 43 | // passedTime = passedTime_ms / 1000.00f; |
rwcjoliver | 0:4788e1df7b55 | 44 | //// pc.printf("Passed TimeMS Int: %d\r\n", passedTime_ms); |
rwcjoliver | 0:4788e1df7b55 | 45 | //// pc.printf("Passed Time Int: %.2f\r\n", passedTime); |
rwcjoliver | 0:4788e1df7b55 | 46 | //// pc.printf("Tacho Interrupt\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 47 | // } |
rwcjoliver | 0:4788e1df7b55 | 48 | |
rwcjoliver | 0:4788e1df7b55 | 49 | // pc.printf("count = %d\r\n", tachoCounter); |
rwcjoliver | 0:4788e1df7b55 | 50 | |
rwcjoliver | 0:4788e1df7b55 | 51 | wheelFreq = 1 / (passedTime * numberOfMagnets); |
rwcjoliver | 0:4788e1df7b55 | 52 | // pc.printf("wheelFreq = %.2f\r\n", wheelFreq); |
rwcjoliver | 0:4788e1df7b55 | 53 | |
rwcjoliver | 0:4788e1df7b55 | 54 | // tachoCounter = 0; // RESET |
rwcjoliver | 0:4788e1df7b55 | 55 | lastTime = currentTime; |
rwcjoliver | 0:4788e1df7b55 | 56 | |
rwcjoliver | 0:4788e1df7b55 | 57 | |
rwcjoliver | 0:4788e1df7b55 | 58 | if (passedTime > 0.00f) { // Stops dividing by 0 |
rwcjoliver | 0:4788e1df7b55 | 59 | |
rwcjoliver | 0:4788e1df7b55 | 60 | // 1 Hz FREQ = 1 RPS -> x60 for minutes |
rwcjoliver | 0:4788e1df7b55 | 61 | float rpm = wheelFreq * 60.00f; /// MAX RPM AT 15KPH = 340RPM |
rwcjoliver | 0:4788e1df7b55 | 62 | // pc.printf("RPM = %.2f\r\n",rpm); |
rwcjoliver | 0:4788e1df7b55 | 63 | |
rwcjoliver | 0:4788e1df7b55 | 64 | // if (rpm < 15.0f) { |
rwcjoliver | 0:4788e1df7b55 | 65 | |
rwcjoliver | 0:4788e1df7b55 | 66 | float kph = (wheelCircumference * rpm * (60.00f / 1000.00f)); |
rwcjoliver | 0:4788e1df7b55 | 67 | printf("Speed = %.2f\r\n", kph); |
rwcjoliver | 0:4788e1df7b55 | 68 | |
rwcjoliver | 0:4788e1df7b55 | 69 | // if (kph > 17.0f) { |
rwcjoliver | 0:4788e1df7b55 | 70 | // currentSpeed = 0; |
rwcjoliver | 0:4788e1df7b55 | 71 | // } |
rwcjoliver | 0:4788e1df7b55 | 72 | // else { |
rwcjoliver | 0:4788e1df7b55 | 73 | currentSpeed = int(kph); |
rwcjoliver | 0:4788e1df7b55 | 74 | // } |
rwcjoliver | 0:4788e1df7b55 | 75 | // } |
rwcjoliver | 0:4788e1df7b55 | 76 | |
rwcjoliver | 0:4788e1df7b55 | 77 | |
rwcjoliver | 0:4788e1df7b55 | 78 | // } |
rwcjoliver | 0:4788e1df7b55 | 79 | // else { |
rwcjoliver | 0:4788e1df7b55 | 80 | // currentSpeed = 0; |
rwcjoliver | 0:4788e1df7b55 | 81 | // |
rwcjoliver | 0:4788e1df7b55 | 82 | // } |
rwcjoliver | 0:4788e1df7b55 | 83 | } |
rwcjoliver | 0:4788e1df7b55 | 84 | |
rwcjoliver | 0:4788e1df7b55 | 85 | } |
rwcjoliver | 0:4788e1df7b55 | 86 | |
rwcjoliver | 0:4788e1df7b55 | 87 | void Dashboard::getCurrentSpeed() { |
rwcjoliver | 0:4788e1df7b55 | 88 | |
rwcjoliver | 0:4788e1df7b55 | 89 | // USE THESE |
rwcjoliver | 0:4788e1df7b55 | 90 | // tachoCounter |
rwcjoliver | 0:4788e1df7b55 | 91 | // currentTime |
rwcjoliver | 0:4788e1df7b55 | 92 | // lastTime |
rwcjoliver | 0:4788e1df7b55 | 93 | |
rwcjoliver | 0:4788e1df7b55 | 94 | currentTime = tachoTimer.read_ms(); |
rwcjoliver | 0:4788e1df7b55 | 95 | if ((currentTime - lastTime) > 5000) { |
rwcjoliver | 0:4788e1df7b55 | 96 | currentSpeed = 0; |
rwcjoliver | 0:4788e1df7b55 | 97 | } |
rwcjoliver | 0:4788e1df7b55 | 98 | // passedTime = (currentTime - lastTime) / 1000; |
rwcjoliver | 0:4788e1df7b55 | 99 | // pc.printf("Passed Time: %.2f\r\n", passedTime); |
rwcjoliver | 0:4788e1df7b55 | 100 | |
rwcjoliver | 0:4788e1df7b55 | 101 | //pc.printf("count = %d\r\n", tachoCounter); |
rwcjoliver | 0:4788e1df7b55 | 102 | // |
rwcjoliver | 0:4788e1df7b55 | 103 | // wheelFreq = tachoCounter / (passedTime * numberOfMagnets); |
rwcjoliver | 0:4788e1df7b55 | 104 | // pc.printf("wheelFreq = %.2f\r\n", wheelFreq); |
rwcjoliver | 0:4788e1df7b55 | 105 | // |
rwcjoliver | 0:4788e1df7b55 | 106 | // tachoCounter = 0; // RESET |
rwcjoliver | 0:4788e1df7b55 | 107 | // lastTime = currentTime; |
rwcjoliver | 0:4788e1df7b55 | 108 | // |
rwcjoliver | 0:4788e1df7b55 | 109 | // |
rwcjoliver | 0:4788e1df7b55 | 110 | // if (passedTime > 0.00f) { // Stops dividing by 0 |
rwcjoliver | 0:4788e1df7b55 | 111 | // PASSED TIME IN SECONDS = 1000 / TIME IN ms |
rwcjoliver | 0:4788e1df7b55 | 112 | // FREQ = 1/T = 1 * 1000 / T |
rwcjoliver | 0:4788e1df7b55 | 113 | |
rwcjoliver | 0:4788e1df7b55 | 114 | // pc.printf("Passed Time Calc: %.2f\r\n", passedTime); |
rwcjoliver | 0:4788e1df7b55 | 115 | |
rwcjoliver | 0:4788e1df7b55 | 116 | // if ((tachoTimer.read_ms() / 1000.00f) - lastTime < 10.00f) { |
rwcjoliver | 0:4788e1df7b55 | 117 | // wheelFreq = 1 / (numberOfMagnets * passedTime); |
rwcjoliver | 0:4788e1df7b55 | 118 | //// pc.printf("wheelFreq set\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 119 | // } |
rwcjoliver | 0:4788e1df7b55 | 120 | // else { |
rwcjoliver | 0:4788e1df7b55 | 121 | // wheelFreq = 0.00f; // Set frequency to zero if not sensed for more than 10s |
rwcjoliver | 0:4788e1df7b55 | 122 | // passedTime = 0.00f; |
rwcjoliver | 0:4788e1df7b55 | 123 | //// pc.printf("wheelFreq Zeroed\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 124 | // } |
rwcjoliver | 0:4788e1df7b55 | 125 | |
rwcjoliver | 0:4788e1df7b55 | 126 | |
rwcjoliver | 0:4788e1df7b55 | 127 | // pc.printf("wheelFreq = %.4f\r\n", wheelFreq); |
rwcjoliver | 0:4788e1df7b55 | 128 | |
rwcjoliver | 0:4788e1df7b55 | 129 | // 1 Hz FREQ = 1 RPS -> x60 for minutes |
rwcjoliver | 0:4788e1df7b55 | 130 | // float rpm = wheelFreq * 60.00f; /// MAX RPM AT 15KPH = 340RPM |
rwcjoliver | 0:4788e1df7b55 | 131 | // pc.printf("RPM = %.2f\r\n",rpm); |
rwcjoliver | 0:4788e1df7b55 | 132 | // |
rwcjoliver | 0:4788e1df7b55 | 133 | // // rpm x 60 = rph |
rwcjoliver | 0:4788e1df7b55 | 134 | // // speed = distance / time |
rwcjoliver | 0:4788e1df7b55 | 135 | // // kph = rph / (1km x circumference [m]) |
rwcjoliver | 0:4788e1df7b55 | 136 | // |
rwcjoliver | 0:4788e1df7b55 | 137 | // // kph = 2πr × RPM × (60/1000) |
rwcjoliver | 0:4788e1df7b55 | 138 | // float kph = wheelCircumference * rpm * (60.00f / 1000.00f); |
rwcjoliver | 0:4788e1df7b55 | 139 | //// float kph = (rpm * 60.00f) / (1000.00f * wheelCircumference); |
rwcjoliver | 0:4788e1df7b55 | 140 | //// pc.printf("KM/H = %.2f\r\n",kph); |
rwcjoliver | 0:4788e1df7b55 | 141 | // if (kph > 15) { |
rwcjoliver | 0:4788e1df7b55 | 142 | // currentSpeed = 0; |
rwcjoliver | 0:4788e1df7b55 | 143 | // } |
rwcjoliver | 0:4788e1df7b55 | 144 | // else { |
rwcjoliver | 0:4788e1df7b55 | 145 | // currentSpeed = int(kph); |
rwcjoliver | 0:4788e1df7b55 | 146 | // } |
rwcjoliver | 0:4788e1df7b55 | 147 | // } |
rwcjoliver | 0:4788e1df7b55 | 148 | // else { |
rwcjoliver | 0:4788e1df7b55 | 149 | // currentSpeed = 0; |
rwcjoliver | 0:4788e1df7b55 | 150 | // |
rwcjoliver | 0:4788e1df7b55 | 151 | // } |
rwcjoliver | 0:4788e1df7b55 | 152 | } |