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.
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed LSM9DS1_Library_cal
main.cpp
00001 #include "mbed.h" 00002 #include "SDFileSystem.h" 00003 #include "uLCD_4DGL.h" 00004 #include "LSM9DS1.h" 00005 #include <fstream> 00006 #include <iomanip> 00007 using namespace std; 00008 00009 #define START_S 1480719111 00010 00011 # define PI 3.14159 00012 00013 #define LEFT 0xA 00014 #define RIGHT 0xB 00015 #define STOP 0xC 00016 #define GO 0xD 00017 00018 int lightState; 00019 00020 DigitalOut leftSignal(p22); 00021 DigitalOut rightSignal(p23); 00022 DigitalOut brakeLight(p21); 00023 Timer blinkT; 00024 00025 AnalogIn flex(p20); 00026 LSM9DS1 imu(p9, p10, 0xD6, 0x3C); 00027 00028 SDFileSystem sd(p11, p12, p13, p14, "sd"); //mosi -> DI, miso <- DO, slck -> sclck, CS -> CS 00029 uLCD_4DGL lcd(p28, p27, p29); 00030 00031 // speed sensor 00032 InterruptIn hallSensor(p8); 00033 Timer hallT; 00034 int stopped; 00035 00036 float miles = 0; 00037 float speed = 0; 00038 float maxSpeed = 0; 00039 time_t seconds; 00040 00041 Serial pc(USBTX, USBRX); 00042 00043 void store_trip(); 00044 void recall_trips(); 00045 int get_state(); 00046 void pass(); 00047 00048 int main() { 00049 // open the file for reading and appending records 00050 // recall last trip 00051 lcd.cls(); 00052 recall_trips(); 00053 wait(1); 00054 lcd.cls(); 00055 00056 00057 imu.begin(); 00058 if (!imu.begin()) { 00059 lcd.printf("Failed to communicate with LSM9DS1.\n"); 00060 } 00061 imu.calibrate(); 00062 00063 // normal operation loop here 00064 int going = 1; // cyclist is moving 00065 set_time(START_S); // Set RTC time 00066 00067 float theta = 0; 00068 00069 hallSensor.fall(&pass); 00070 hallT.start(); // start the hall sensor timer 00071 00072 leftSignal = 0; 00073 rightSignal = 0; 00074 brakeLight = 0; 00075 00076 while(going) { 00077 00078 seconds = time(NULL) - START_S; // return the seconds passed since start 00079 00080 if (hallT.read() > 6.0 && !stopped) { 00081 speed = 0; 00082 stopped = 1; 00083 } 00084 00085 going = (hallT.read() > 200.0 && stopped) ? 0 : 1; 00086 maxSpeed = (speed > maxSpeed) ? speed : maxSpeed; 00087 00088 lcd.locate(0, 1); 00089 lcd.color(BLUE); 00090 lcd.printf("Distance : %3.1f mi\n\n", miles); 00091 lcd.color(GREEN); 00092 lcd.printf("Speed : %2.1f mph\n\n", speed); 00093 lcd.color(LGREY); 00094 lcd.printf("Time : %3.1f min\n\n", (float)seconds / 60); 00095 00096 lcd.circle(64, 128, 64, GREEN); 00097 lcd.line(64, 128, 64 + 62*cos(theta), 128 - 62*sin(theta), BLACK); 00098 theta = PI - (speed / 9.536); 00099 lcd.line(64, 128, 64 + 62*cos(theta), 128 - 62*sin(theta), WHITE); 00100 00101 00102 // light states code 00103 lightState = get_state(); 00104 pc.printf("STATE %d\n\r", lightState); 00105 00106 if (lightState == STOP) { 00107 brakeLight = 1; 00108 } else if (lightState == GO) { 00109 brakeLight = 0; 00110 rightSignal = 0; 00111 leftSignal = 0; 00112 } else { 00113 if (lightState == RIGHT) { 00114 rightSignal = 1; 00115 } else { 00116 leftSignal = 1; 00117 } 00118 } 00119 wait(1); 00120 } 00121 00122 // store this trip 00123 lcd.cls(); 00124 store_trip(); 00125 00126 // end everything 00127 } 00128 00129 void recall_trips(void) { 00130 // display the most recent trip made on the screen 00131 // display the most impressive trip (longest distance, best speed, least time) 00132 00133 float f_miles; 00134 float f_maxSpeed; 00135 float f_minutes; 00136 ifstream file; 00137 00138 // read out the most recent trip 00139 file.open("/sd/records/recent.txt"); 00140 00141 lcd.color(WHITE); 00142 00143 if (!file.is_open()) { 00144 lcd.locate(0, 1); 00145 lcd.printf("Could not open file\n"); 00146 } else { 00147 file >> f_miles >> f_maxSpeed >> f_minutes; 00148 lcd.locate(0, 1); 00149 lcd.printf("PRIOR TRIP\n\n"); 00150 lcd.printf("Dist : %3.1f mi\n\n", f_miles); 00151 lcd.printf("High : %2.1f mph\n\n", f_maxSpeed); 00152 lcd.printf("Time : %3.1f min\n\n", f_minutes); 00153 } 00154 00155 file.close(); 00156 wait(0.5); 00157 00158 // display the best trip 00159 00160 file.open("/sd/records/best-of.txt"); 00161 00162 if(!file.is_open()) { 00163 lcd.printf("Could not open file\n"); 00164 } else { 00165 // show the best trip 00166 file >> f_miles >> f_maxSpeed >> f_minutes; 00167 lcd.printf("BEST TRIP\n\n"); 00168 lcd.printf("Dist : %3.1f mi\n\n", f_miles); 00169 lcd.printf("High : %2.1f mph\n\n", f_maxSpeed); 00170 lcd.printf("Time : %3.1f min\n\n", f_minutes); 00171 } 00172 00173 file.close(); 00174 wait(0.5); 00175 } 00176 00177 void store_trip(void) { 00178 // store the most recent trip completed 00179 // determine whether this trip was a record, and indicate if so 00180 00181 float minutes; 00182 float f_miles; 00183 float f_maxSpeed; 00184 float f_minutes; 00185 fstream file; 00186 00187 file.open("/sd/records/recent.txt"); 00188 00189 lcd.color(WHITE); 00190 00191 if (!file.is_open()) { 00192 lcd.locate(0, 1); 00193 lcd.printf("Could not open file\n"); 00194 } else { 00195 minutes = (float)seconds / 60; 00196 lcd.locate(0, 1); 00197 lcd.printf("This trip\n\n"); 00198 lcd.printf("Distance : %3.1f mi\n\n", miles); 00199 lcd.printf("Top speed : %2.1f mph\n\n", maxSpeed); 00200 lcd.printf("Time : %3.1f min\n\n", minutes); 00201 // overwrite most recent 00202 file << fixed << setprecision(1) << miles << " " << maxSpeed << " " << minutes << endl; 00203 } 00204 00205 file.close(); 00206 wait(0.5); 00207 00208 file.open("/sd/records/best-of.txt"); 00209 00210 if (!file.is_open()) { 00211 lcd.locate(0, 1); 00212 lcd.printf("Could not open file\n"); 00213 } else { 00214 // retrieve the previous record 00215 file >> f_miles >> f_maxSpeed >> f_minutes; 00216 // check if you beat your best 00217 if (miles >= f_miles && maxSpeed >= f_maxSpeed && minutes >= f_minutes) { 00218 lcd.printf("A new record!"); 00219 file << fixed << setprecision(1) << miles << " " << maxSpeed << " " << minutes << endl; 00220 } 00221 } 00222 00223 file.close(); 00224 wait(0.5); 00225 } 00226 00227 int get_state(void) { 00228 imu.readAccel(); 00229 float f = flex.read(); 00230 int x = imu.ax, y = imu.ay, z = imu.az; 00231 pc.printf("x: %d, y: %d, z: %d, flex: %f\n\r", x,y,z,f); 00232 if (z > 0 && z > abs(y) && z > abs(x) && y < 0 && f >= 0.87) 00233 return STOP; 00234 // if (x < 0 && abs(x) > abs(z) && abs(x) > abs(y) && f <= 0.87) 00235 if (x < 0 && abs(x) > abs(z) && abs(x) > abs(y)) 00236 return LEFT; 00237 if (y > 0 && y > abs(z) && y > abs(x) && f >= 0.87) 00238 return RIGHT; 00239 return GO; 00240 } 00241 00242 void pass(void) { 00243 // interrupt, performed when the hallsensor passes the magnet 00244 stopped = 0; // reset the global 00245 hallT.stop(); 00246 speed = 0.00136364 / (hallT.read() / 3600); // current speed 00247 miles += 0.00136364; // circumference of the tire in miles 00248 hallT.reset(); 00249 hallT.start(); 00250 }
Generated on Sun Aug 7 2022 20:11:33 by
1.7.2