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.
main.cpp
00001 /* 00002 Date: 04/08/10 00003 Version 0.1.3 00004 00005 Notes 00006 Write strings to display 00007 Update speed reporting based on calculated time 00008 00009 00010 Bugs 00011 Can update displayed strings, but cant stay at constant speed 00012 at value not equal to 0, 10, or 20. during accel it will increase 00013 from 10 to 20 1 at a time. 00014 00015 Known Issues 00016 1.Pin20 seems a bit dodgy. InterruptIn does not work on that pin. 00017 2. Pin19 seems a bit dodgy. InterruptIn does not work on that pin. 00018 00019 */ 00020 00021 #include "mbed.h" 00022 #include "FontPack.h" 00023 00024 using namespace FontPack; 00025 00026 #define true 1 00027 #define false 0 00028 00029 #define MSEC_USEC 1000 00030 /* 00031 we get msec/rev, what we want is miles/hour 00032 [1rev/(PI*Dia)in]*[12in/ft]*[5280ft/1mi]*[1sec/1000ms]*[1hr/3600sec] 00033 % has units of hr/mi, so invert 00034 */ 00035 #define POV_DIAMETER 26.5 00036 #define PI 3.14 00037 00038 #define POV_NUM_SAMPLES 3 00039 00040 #define POV_NUM_COLS 360 //how many cols make up the whole wheel 00041 #define POV_MIN_SPEED_MPH 3 00042 00043 #define POV_NUM_LEDS 8 //leds to paint 00044 #define POV_HALL_DEBOUNCE_US 2000*MSEC_USEC //debug code - long suppression 00045 00046 //#define POV_HALL_DEBOUNCE_US 250*POV_MSEC_TO_USEC 00047 00048 //DigitalOut led[POV_NUM_LEDS] = { p21, p22, p23, p24, p25, p26, p27, p28}; 00049 DigitalOut led[POV_NUM_LEDS] = {p28, p27, p26, p25, p24, p23, p22, p21}; 00050 InterruptIn hall(p17); 00051 Ticker paintTimer; //repeating ISR call 00052 //Timeout suppressHallTimer; //single ISR call 00053 Timer wheelSpeedTimer; //true timer, not an ISR call 00054 /* 00055 CONVERTER when multiplied by msec/rev results in hr/mi 00056 so the inversion of the product is mph 00057 pov_converter = [12in/1ft]*[5280ft/1mi]*[1sec/1000msec]*[1hr/3600s] 00058 pov_converter = pov_diameter/pov_converter 00059 mph = pov_converter/ (ms/rev) 00060 */ 00061 static double POV_CONVERTER=.0056; 00062 00063 static bool hallFlag = false; 00064 static bool hallSuppressed = false; 00065 static bool paintFlag = false; 00066 static int columnCounter = 0; 00067 static int wheelSpeedCounter_ms = 0; 00068 00069 00070 00071 /*FUNCTION DECLARATIONS*/ 00072 static void paintInterrupt(void); 00073 //static void suppressInterrupt(void); 00074 static void hallEffectDetected(void); 00075 static void paint(char canvas[]); 00076 static void updateCanvas(char canvas[], float wheelSpeed_mph); 00077 static int writeString(char canvas[], const char* msg, int index) ; 00078 00079 float updateWheelSpeed(int wheelSpeedCtr_us); 00080 static void turnLightsOn(bool isOn); 00081 00082 int main() { 00083 float wheelSpeed_mph = 0; 00084 int colSpeed_us = 0; 00085 char canvas[POV_NUM_COLS] = {0}; 00086 POV_CONVERTER = POV_DIAMETER / POV_CONVERTER; 00087 00088 turnLightsOn(false); 00089 hall.fall(&hallEffectDetected); // attach the address of the handling routine 00090 hall.mode(PullUp); 00091 wheelSpeedTimer.start(); 00092 paintTimer.attach(&paintInterrupt, 1); 00093 00094 while (true) { 00095 if (hallFlag) { 00096 hallFlag = false; 00097 colSpeed_us = wheelSpeedCounter_ms*MSEC_USEC / POV_NUM_COLS; 00098 wheelSpeed_mph = updateWheelSpeed(wheelSpeedCounter_ms); 00099 updateCanvas(canvas, wheelSpeed_mph); 00100 if (wheelSpeed_mph > POV_MIN_SPEED_MPH) { 00101 //if (true) { 00102 paintFlag = true; 00103 paintTimer.attach_us(&paintInterrupt, colSpeed_us); 00104 columnCounter = 0; 00105 //paintTimer.attach(&paintInterrupt, 1); //debug - msec, slow update 00106 } 00107 hallSuppressed = false; 00108 } 00109 00110 if (paintFlag) { 00111 paintFlag = false; 00112 paint(canvas); 00113 } 00114 } 00115 } 00116 00117 static void paintInterrupt(void) { 00118 ++columnCounter; 00119 paintFlag = true; 00120 } 00121 00122 00123 static void hallEffectDetected(void) { 00124 if (!hallSuppressed) { 00125 hallSuppressed = true; 00126 paintFlag = false; 00127 wheelSpeedCounter_ms = wheelSpeedTimer.read_ms(); 00128 wheelSpeedTimer.reset(); 00129 hallFlag = true; 00130 } 00131 } 00132 00133 static void paint(char canvas[]) { 00134 if (columnCounter >= POV_NUM_COLS) return; 00135 int LED = canvas[columnCounter]; 00136 for (int i=0; i < POV_NUM_LEDS; i++) { 00137 led[i] = (LED >> i) & 0x01; //just get the last bit for comparison 00138 } 00139 } 00140 00141 00142 static void updateCanvas(char canvas[], float wheelSpeed_mph) { 00143 int index = 120; 00144 char msg[30]; 00145 memset(canvas, 0x00, POV_NUM_COLS); 00146 float bike_distance = 0; 00147 //sprintf(msg, "Distance: %2f Speed: %2.2f MPH", wheelSpeed_mph); 00148 // sprintf(msg, "Speed: %2.1fMPH Distance: %2.1f", wheelSpeed_mph); 00149 00150 00151 00152 sprintf(msg, "Speed: %2.1f MPH", wheelSpeed_mph); 00153 00154 index = writeString(canvas, msg, 90); 00155 00156 } 00157 00158 static int writeString(char canvas[], const char* msg, int index) { 00159 00160 while ((msg != NULL) && (*msg != '\0')) { 00161 index = writeChar(*msg, canvas, index); 00162 msg++; 00163 } 00164 return index; 00165 } 00166 00167 00168 static void turnLightsOn(bool isOn) { 00169 for (int i=0; i < POV_NUM_LEDS; i++) { 00170 led[i]=isOn; 00171 } 00172 } 00173 00174 00175 //Aggregates and stores num samples in the averager 00176 //computes running average by always replacing value at index 00177 //with new measured value, then updating sample with new measured value 00178 00179 float updateWheelSpeed(int wheelSpeedCtr_ms) { 00180 static float samples[POV_NUM_SAMPLES] = {0}; 00181 static char index = 0; 00182 static float avgSpeed_mph = 0; 00183 00184 //float lin = POV_CONVERTER/((float) wheelSpeedCtr_ms); 00185 //return lin; 00186 00187 if (wheelSpeedCtr_ms != 0) { 00188 float linearSpeed_mph = POV_CONVERTER/((float) wheelSpeedCtr_ms); 00189 //calculate new running average 00190 avgSpeed_mph = avgSpeed_mph - samples[index]/POV_NUM_SAMPLES + linearSpeed_mph/POV_NUM_SAMPLES; 00191 //store new sample 00192 samples[index] = linearSpeed_mph; 00193 //update averager index 00194 index = ++index % POV_NUM_SAMPLES; 00195 } 00196 00197 return avgSpeed_mph; 00198 }
Generated on Mon Jul 18 2022 11:33:46 by
1.7.2