Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
dashboard.cpp
00001 #include <mbed.h> 00002 #include "definitions.h" 00003 #include "dashboard.h" 00004 00005 Dashboard::Dashboard(InterruptIn& hallSensor) : _hallSensor(hallSensor) { // CONSTRUCTOR 00006 00007 _hallSensor.rise(this, &Dashboard::tachoInterrupt); // Register everytime hall sensor is detected 00008 tachoTimer.start(); // Timer for counting time between hall sensor triggers 00009 00010 numberOfMagnets = 8; // How many magnets on the wheels 00011 00012 currentSpeed = 0.00f; 00013 passedTime_ms = 0.00f; 00014 passedTime = 0.00f; 00015 lastTime = 0.00f; 00016 wheelCircumference = 0.73513268f; // in meters. Radius = 0.117m 00017 00018 currentDistance = 0.00f; 00019 tachoCounter = 0; 00020 currentTime = 0; 00021 } 00022 00023 void Dashboard::tachoInterrupt() { 00024 00025 currentTime = tachoTimer.read_ms(); 00026 passedTime = (currentTime - lastTime) / 1000; 00027 pc.printf("Passed Time: %.2f\r\n", passedTime); 00028 00029 // if (passedTime > 0.03) { 00030 00031 // tachoCounter++; // Increment tacho counter = number of strips detected 00032 //pc.printf("count = %d\r\n", tachoCounter); 00033 00034 // getCurrentSpeed(); 00035 // // PASSED TIME IN ms 00036 // passedTime_ms = tachoTimer.read_ms() - lastTime;// - 21; // 20ms propogation delay 00037 // 00038 // if (passedTime_ms > 100) { // IGNORE SHORT PULSES 00039 // lastTime = tachoTimer.read_ms(); 00040 currentDistance += (wheelCircumference / numberOfMagnets); 00041 pc.printf("Current Distance = %.2f\r\n", currentDistance); 00042 00043 // passedTime = passedTime_ms / 1000.00f; 00044 //// pc.printf("Passed TimeMS Int: %d\r\n", passedTime_ms); 00045 //// pc.printf("Passed Time Int: %.2f\r\n", passedTime); 00046 //// pc.printf("Tacho Interrupt\r\n"); 00047 // } 00048 00049 // pc.printf("count = %d\r\n", tachoCounter); 00050 00051 wheelFreq = 1 / (passedTime * numberOfMagnets); 00052 // pc.printf("wheelFreq = %.2f\r\n", wheelFreq); 00053 00054 // tachoCounter = 0; // RESET 00055 lastTime = currentTime; 00056 00057 00058 if (passedTime > 0.00f) { // Stops dividing by 0 00059 00060 // 1 Hz FREQ = 1 RPS -> x60 for minutes 00061 float rpm = wheelFreq * 60.00f; /// MAX RPM AT 15KPH = 340RPM 00062 // pc.printf("RPM = %.2f\r\n",rpm); 00063 00064 // if (rpm < 15.0f) { 00065 00066 float kph = (wheelCircumference * rpm * (60.00f / 1000.00f)); 00067 printf("Speed = %.2f\r\n", kph); 00068 00069 // if (kph > 17.0f) { 00070 // currentSpeed = 0; 00071 // } 00072 // else { 00073 currentSpeed = int(kph); 00074 // } 00075 // } 00076 00077 00078 // } 00079 // else { 00080 // currentSpeed = 0; 00081 // 00082 // } 00083 } 00084 00085 } 00086 00087 void Dashboard::getCurrentSpeed() { 00088 00089 // USE THESE 00090 // tachoCounter 00091 // currentTime 00092 // lastTime 00093 00094 currentTime = tachoTimer.read_ms(); 00095 if ((currentTime - lastTime) > 5000) { 00096 currentSpeed = 0; 00097 } 00098 // passedTime = (currentTime - lastTime) / 1000; 00099 // pc.printf("Passed Time: %.2f\r\n", passedTime); 00100 00101 //pc.printf("count = %d\r\n", tachoCounter); 00102 // 00103 // wheelFreq = tachoCounter / (passedTime * numberOfMagnets); 00104 // pc.printf("wheelFreq = %.2f\r\n", wheelFreq); 00105 // 00106 // tachoCounter = 0; // RESET 00107 // lastTime = currentTime; 00108 // 00109 // 00110 // if (passedTime > 0.00f) { // Stops dividing by 0 00111 // PASSED TIME IN SECONDS = 1000 / TIME IN ms 00112 // FREQ = 1/T = 1 * 1000 / T 00113 00114 // pc.printf("Passed Time Calc: %.2f\r\n", passedTime); 00115 00116 // if ((tachoTimer.read_ms() / 1000.00f) - lastTime < 10.00f) { 00117 // wheelFreq = 1 / (numberOfMagnets * passedTime); 00118 //// pc.printf("wheelFreq set\r\n"); 00119 // } 00120 // else { 00121 // wheelFreq = 0.00f; // Set frequency to zero if not sensed for more than 10s 00122 // passedTime = 0.00f; 00123 //// pc.printf("wheelFreq Zeroed\r\n"); 00124 // } 00125 00126 00127 // pc.printf("wheelFreq = %.4f\r\n", wheelFreq); 00128 00129 // 1 Hz FREQ = 1 RPS -> x60 for minutes 00130 // float rpm = wheelFreq * 60.00f; /// MAX RPM AT 15KPH = 340RPM 00131 // pc.printf("RPM = %.2f\r\n",rpm); 00132 // 00133 // // rpm x 60 = rph 00134 // // speed = distance / time 00135 // // kph = rph / (1km x circumference [m]) 00136 // 00137 // // kph = 2πr × RPM × (60/1000) 00138 // float kph = wheelCircumference * rpm * (60.00f / 1000.00f); 00139 //// float kph = (rpm * 60.00f) / (1000.00f * wheelCircumference); 00140 //// pc.printf("KM/H = %.2f\r\n",kph); 00141 // if (kph > 15) { 00142 // currentSpeed = 0; 00143 // } 00144 // else { 00145 // currentSpeed = int(kph); 00146 // } 00147 // } 00148 // else { 00149 // currentSpeed = 0; 00150 // 00151 // } 00152 }
Generated on Thu Jul 28 2022 02:21:05 by
