Guides the user to their classes

Dependencies:   4DGL-uLCD-SE Course SDFileSystem mbed PinDetect LSM9DS1_Library_cal MBed_Adafruit-GPS-Library

Committer:
kkizirian
Date:
Thu Dec 08 20:20:00 2016 +0000
Revision:
6:ceb6a5426add
Parent:
5:430f44669f94
Child:
7:e4d667beaf33
Added getting actual GPS lat and long;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kkizirian 0:ce60014510e5 1 // Class Scheduler
kkizirian 0:ce60014510e5 2 #include "mbed.h"
kkizirian 0:ce60014510e5 3 #include "uLCD_4DGL.h"
kkizirian 0:ce60014510e5 4 #include <string>
kkizirian 0:ce60014510e5 5 #include <vector>
kkizirian 0:ce60014510e5 6 #include "Course.h"
kkizirian 0:ce60014510e5 7 #include "SDFileSystem.h"
kkizirian 5:430f44669f94 8 #include "MBed_Adafruit_GPS.h"
kkizirian 3:0ca91f8afec5 9 #include "LSM9DS1.h"
kkizirian 2:c708e2027970 10 #include "PinDetect.h"
kkizirian 3:0ca91f8afec5 11 #define PI 3.14159
kkizirian 3:0ca91f8afec5 12 #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.
kkizirian 0:ce60014510e5 13
kkizirian 0:ce60014510e5 14 SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
kkizirian 0:ce60014510e5 15 Serial pc(USBTX, USBRX);
kkizirian 3:0ca91f8afec5 16 uLCD_4DGL uLCD(p13,p14,p15); // serial tx, serial rx, reset pin;
kkizirian 3:0ca91f8afec5 17 PinDetect left(p20);
kkizirian 3:0ca91f8afec5 18 PinDetect right(p18);
kkizirian 5:430f44669f94 19 Serial * gps_Serial;
kkizirian 0:ce60014510e5 20
kkizirian 0:ce60014510e5 21 int update;
kkizirian 0:ce60014510e5 22 vector<Course> courseVec;
kkizirian 4:93a4b415fe6c 23 void readClassFile(vector<Course>& cVec);
kkizirian 5:430f44669f94 24 int getNextClass(vector<Course>& cVec);
kkizirian 2:c708e2027970 25 void displayCourseVec();
kkizirian 3:0ca91f8afec5 26 float calculateHeading(float mx, float my), computeAngleToDestination(float diffLat, float diffLong);
kkizirian 6:ceb6a5426add 27 float longToDecimal(float longitudeInDDMM), latToDecimal(float latitudeInDDMM);
kkizirian 4:93a4b415fe6c 28 int xEnd, yEnd = 0.0;
kkizirian 5:430f44669f94 29 int currHour, currMinute, currSecond = 0;
kkizirian 5:430f44669f94 30 int hoursToNextClass, minutesToNextClass, secondsToNextClass = 0;
kkizirian 5:430f44669f94 31 int nextClass = 0;
kkizirian 5:430f44669f94 32 int currClass = -1;
kkizirian 5:430f44669f94 33 const int refresh_Time = 1000; //refresh time in ms
kkizirian 3:0ca91f8afec5 34 bool screen_refreshed = false;
kkizirian 2:c708e2027970 35 int volatile current_screen = 0;
kkizirian 2:c708e2027970 36 bool volatile screen_change = false;
kkizirian 5:430f44669f94 37 void timeToNextClass();
kkizirian 0:ce60014510e5 38
kkizirian 2:c708e2027970 39 void left_callback(void)
kkizirian 2:c708e2027970 40 {
kkizirian 3:0ca91f8afec5 41 if (courseVec.size() != 0) {
kkizirian 3:0ca91f8afec5 42 current_screen = (3 + current_screen - 1)%3;
kkizirian 3:0ca91f8afec5 43 screen_change = true;
kkizirian 3:0ca91f8afec5 44 } else
kkizirian 3:0ca91f8afec5 45 current_screen = 0;
kkizirian 5:430f44669f94 46 currClass = -1;
kkizirian 5:430f44669f94 47 uLCD.cls();
kkizirian 2:c708e2027970 48 }
kkizirian 2:c708e2027970 49
kkizirian 2:c708e2027970 50 void right_callback(void)
kkizirian 2:c708e2027970 51 {
kkizirian 3:0ca91f8afec5 52 if (courseVec.size() != 0) {
kkizirian 3:0ca91f8afec5 53 current_screen = (3 + current_screen + 1)%3;
kkizirian 3:0ca91f8afec5 54 screen_change = true;
kkizirian 3:0ca91f8afec5 55 } else
kkizirian 3:0ca91f8afec5 56 current_screen = 0;
kkizirian 5:430f44669f94 57 currClass = -1;
kkizirian 5:430f44669f94 58 uLCD.cls();
kkizirian 2:c708e2027970 59 }
kkizirian 2:c708e2027970 60
kkizirian 0:ce60014510e5 61 int main()
kkizirian 0:ce60014510e5 62 {
kkizirian 3:0ca91f8afec5 63 LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
kkizirian 3:0ca91f8afec5 64 float h = 0.0;
kkizirian 3:0ca91f8afec5 65 float angleToDest = 0.0;
kkizirian 3:0ca91f8afec5 66 IMU.begin();
kkizirian 3:0ca91f8afec5 67 if (!IMU.begin()) {
kkizirian 3:0ca91f8afec5 68 pc.printf("Failed to communicate with LSM9DS1.\n");
kkizirian 3:0ca91f8afec5 69 }
kkizirian 3:0ca91f8afec5 70 uLCD.cls();
kkizirian 3:0ca91f8afec5 71 uLCD.printf("Place IMU flat");
kkizirian 3:0ca91f8afec5 72 IMU.calibrate(1);
kkizirian 3:0ca91f8afec5 73 uLCD.cls();
kkizirian 3:0ca91f8afec5 74 uLCD.printf("Rotate IMU 360\ndegrees in \nhorizontal plane");
kkizirian 3:0ca91f8afec5 75 IMU.calibrateMag(0);
kkizirian 3:0ca91f8afec5 76
kkizirian 2:c708e2027970 77 left.mode(PullUp);
kkizirian 2:c708e2027970 78 right.mode(PullUp);
kkizirian 2:c708e2027970 79 left.attach_deasserted(&left_callback);
kkizirian 2:c708e2027970 80 right.attach_deasserted(&right_callback);
kkizirian 2:c708e2027970 81 left.setSampleFrequency();
kkizirian 2:c708e2027970 82 right.setSampleFrequency();
kkizirian 2:c708e2027970 83
kkizirian 0:ce60014510e5 84 readClassFile(courseVec);
kkizirian 5:430f44669f94 85
kkizirian 6:ceb6a5426add 86 Course tempCourse("COC", 4, 00, "PM");
kkizirian 5:430f44669f94 87 courseVec.push_back(tempCourse);
kkizirian 5:430f44669f94 88
kkizirian 5:430f44669f94 89 gps_Serial = new Serial(p28,p27); //serial object for use w/ GPS
kkizirian 5:430f44669f94 90 Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
kkizirian 5:430f44669f94 91 char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
kkizirian 5:430f44669f94 92 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
kkizirian 5:430f44669f94 93 const int refresh_Time = 2000; //refresh time in ms
kkizirian 5:430f44669f94 94
kkizirian 5:430f44669f94 95 myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *)
kkizirian 5:430f44669f94 96 //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf
kkizirian 5:430f44669f94 97
kkizirian 5:430f44669f94 98 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
kkizirian 5:430f44669f94 99 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
kkizirian 5:430f44669f94 100 myGPS.sendCommand(PGCMD_ANTENNA);
kkizirian 5:430f44669f94 101
kkizirian 4:93a4b415fe6c 102 float myLat = 33.775991;
kkizirian 4:93a4b415fe6c 103 float myLong = -84.397128;
kkizirian 2:c708e2027970 104
kkizirian 5:430f44669f94 105 float destinationLat = courseVec[1].getLat();
kkizirian 5:430f44669f94 106 float destinationLong = courseVec[1].getLong();
kkizirian 5:430f44669f94 107
kkizirian 4:93a4b415fe6c 108 screen_change = true;
kkizirian 0:ce60014510e5 109 pc.baud(9600);
kkizirian 5:430f44669f94 110 c = myGPS.read(); //queries the GPS
kkizirian 5:430f44669f94 111 currHour = myGPS.hour - 5; //for eastern time zone
kkizirian 5:430f44669f94 112 if (currHour < 0)
kkizirian 5:430f44669f94 113 currHour += 24;
kkizirian 5:430f44669f94 114 currMinute = myGPS.minute;
kkizirian 5:430f44669f94 115 currSecond = myGPS.seconds;
kkizirian 5:430f44669f94 116 nextClass = getNextClass(courseVec);
kkizirian 2:c708e2027970 117
kkizirian 5:430f44669f94 118 refresh_Timer.start(); //starts the clock on the timer
kkizirian 0:ce60014510e5 119 while(1) {
kkizirian 5:430f44669f94 120 c = myGPS.read(); //queries the GPS
kkizirian 5:430f44669f94 121
kkizirian 5:430f44669f94 122 //if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused
kkizirian 5:430f44669f94 123
kkizirian 5:430f44669f94 124 //check if we recieved a new message from GPS, if so, attempt to parse it,
kkizirian 5:430f44669f94 125 if ( myGPS.newNMEAreceived() ) {
kkizirian 5:430f44669f94 126 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
kkizirian 5:430f44669f94 127 continue;
kkizirian 5:430f44669f94 128 }
kkizirian 5:430f44669f94 129 }
kkizirian 5:430f44669f94 130
kkizirian 5:430f44669f94 131 if (refresh_Timer.read_ms() >= refresh_Time) {
kkizirian 5:430f44669f94 132 refresh_Timer.reset();
kkizirian 6:ceb6a5426add 133 if (myGPS.fix) {
kkizirian 6:ceb6a5426add 134 myLat = latToDecimal(myGPS.latitude);
kkizirian 6:ceb6a5426add 135 myLong = longToDecimal(myGPS.longitude);
kkizirian 6:ceb6a5426add 136 }
kkizirian 6:ceb6a5426add 137
kkizirian 5:430f44669f94 138 currHour = myGPS.hour - 5; //for eastern time zone
kkizirian 5:430f44669f94 139 if (currHour < 0)
kkizirian 5:430f44669f94 140 currHour += 24;
kkizirian 5:430f44669f94 141 currMinute = myGPS.minute;
kkizirian 5:430f44669f94 142 currSecond = myGPS.seconds;
kkizirian 5:430f44669f94 143 nextClass = getNextClass(courseVec);
kkizirian 5:430f44669f94 144 timeToNextClass();
kkizirian 5:430f44669f94 145
kkizirian 5:430f44669f94 146 switch(current_screen) {
kkizirian 5:430f44669f94 147 case 0:
kkizirian 5:430f44669f94 148 wait(.2);
kkizirian 5:430f44669f94 149 if (screen_change) {
kkizirian 5:430f44669f94 150 displayCourseVec();
kkizirian 5:430f44669f94 151 screen_change = false;
kkizirian 5:430f44669f94 152 }
kkizirian 5:430f44669f94 153 break;
kkizirian 5:430f44669f94 154 case 1:
kkizirian 5:430f44669f94 155 wait(.1);
kkizirian 5:430f44669f94 156 if (screen_change) {
kkizirian 5:430f44669f94 157 uLCD.printf("Compass screen");
kkizirian 5:430f44669f94 158 screen_change = false;
kkizirian 5:430f44669f94 159 }
kkizirian 3:0ca91f8afec5 160
kkizirian 5:430f44669f94 161 IMU.readMag();
kkizirian 5:430f44669f94 162 h = calculateHeading(IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my));
kkizirian 5:430f44669f94 163 pc.printf("Heading: %f\n", h);
kkizirian 5:430f44669f94 164 destinationLat = courseVec[nextClass].getLat();
kkizirian 5:430f44669f94 165 destinationLong = courseVec[nextClass].getLong();
kkizirian 5:430f44669f94 166 float diffLat = destinationLat - myLat;
kkizirian 5:430f44669f94 167 float diffLong = destinationLong - myLong;
kkizirian 5:430f44669f94 168 angleToDest = computeAngleToDestination(diffLat, diffLong);
kkizirian 5:430f44669f94 169 h = angleToDest - h;
kkizirian 3:0ca91f8afec5 170 h = h - 90;
kkizirian 5:430f44669f94 171 if (h < 0)
kkizirian 5:430f44669f94 172 h = h + 360;
kkizirian 5:430f44669f94 173 if (h > 360)
kkizirian 5:430f44669f94 174 h = h - 360;
kkizirian 5:430f44669f94 175 xEnd = 0;
kkizirian 5:430f44669f94 176 yEnd = 0;
kkizirian 5:430f44669f94 177 float rads = 0.0;
kkizirian 5:430f44669f94 178 //uLCD.cls();
kkizirian 5:430f44669f94 179 if (h < 90) {
kkizirian 5:430f44669f94 180 rads = h * PI / 180;
kkizirian 5:430f44669f94 181 xEnd = floor(63 * sin(rads) + .5);
kkizirian 5:430f44669f94 182 yEnd = floor(63 * cos(rads) + .5);
kkizirian 5:430f44669f94 183 xEnd = 63 + xEnd;
kkizirian 5:430f44669f94 184 yEnd = 63 + yEnd;
kkizirian 5:430f44669f94 185 } else if (90 < h < 180) {
kkizirian 5:430f44669f94 186 h = h - 90;
kkizirian 5:430f44669f94 187 rads = h * PI / 180;
kkizirian 5:430f44669f94 188 xEnd = floor(63 * cos(rads) + .5);
kkizirian 5:430f44669f94 189 yEnd = floor(63 * sin(rads) + .5);
kkizirian 5:430f44669f94 190 xEnd = 63 + xEnd;
kkizirian 5:430f44669f94 191 yEnd = 63 - yEnd;
kkizirian 5:430f44669f94 192 } else if (180 < h < 270) {
kkizirian 5:430f44669f94 193 h = h - 180;
kkizirian 5:430f44669f94 194 rads = h * PI / 180;
kkizirian 5:430f44669f94 195 xEnd = floor(63 * sin(rads) + .5);
kkizirian 5:430f44669f94 196 yEnd = floor(63 * cos(rads) + .5);
kkizirian 5:430f44669f94 197 xEnd = 63 - xEnd;
kkizirian 5:430f44669f94 198 yEnd = 63 - yEnd;
kkizirian 5:430f44669f94 199 } else if (270 < h) {
kkizirian 5:430f44669f94 200 h = h - 270;
kkizirian 5:430f44669f94 201 rads = h * PI / 180;
kkizirian 5:430f44669f94 202 xEnd = floor(63 * cos(rads) + .5);
kkizirian 5:430f44669f94 203 yEnd = floor(63 * sin(rads) + .5);
kkizirian 5:430f44669f94 204 xEnd = 63 - xEnd;
kkizirian 5:430f44669f94 205 yEnd = 63 + yEnd;
kkizirian 5:430f44669f94 206 }
kkizirian 2:c708e2027970 207 uLCD.cls();
kkizirian 5:430f44669f94 208 uLCD.line(63, 63, xEnd, yEnd, WHITE);
kkizirian 5:430f44669f94 209 wait(.1);
kkizirian 5:430f44669f94 210 break;
kkizirian 5:430f44669f94 211 case 2:
kkizirian 5:430f44669f94 212 //uLCD.cls();
kkizirian 6:ceb6a5426add 213 uLCD.locate(0,3);
kkizirian 5:430f44669f94 214 uLCD.printf("Time to next class");
kkizirian 6:ceb6a5426add 215 uLCD.locate(0, 4);
kkizirian 5:430f44669f94 216 uLCD.printf("%02dhrs %02dminutes", hoursToNextClass, minutesToNextClass);
kkizirian 6:ceb6a5426add 217 uLCD.locate(0, 5);
kkizirian 5:430f44669f94 218 uLCD.printf("%02dseconds", secondsToNextClass);
kkizirian 6:ceb6a5426add 219 uLCD.locate(0, 0);
kkizirian 5:430f44669f94 220 uLCD.printf("Next class");
kkizirian 5:430f44669f94 221 if (nextClass != currClass) {
kkizirian 6:ceb6a5426add 222 uLCD.locate(0, 1);
kkizirian 5:430f44669f94 223 uLCD.printf(" ");
kkizirian 6:ceb6a5426add 224 uLCD.locate(0, 1);
kkizirian 5:430f44669f94 225 uLCD.printf("%s", courseVec[nextClass].getDisplayString());
kkizirian 5:430f44669f94 226 }
kkizirian 5:430f44669f94 227 currClass = nextClass;
kkizirian 5:430f44669f94 228 break;
kkizirian 5:430f44669f94 229 }
kkizirian 0:ce60014510e5 230 }
kkizirian 0:ce60014510e5 231 }
kkizirian 0:ce60014510e5 232 }
kkizirian 0:ce60014510e5 233
kkizirian 2:c708e2027970 234 void readClassFile(vector<Course>& cVec)
kkizirian 2:c708e2027970 235 {
kkizirian 0:ce60014510e5 236 cVec.clear();
kkizirian 0:ce60014510e5 237
kkizirian 0:ce60014510e5 238 FILE *readFp = fopen("/sd/classdir/classes.txt", "r");
kkizirian 0:ce60014510e5 239 char line[15];
kkizirian 0:ce60014510e5 240 char buildingBuf[4];
kkizirian 0:ce60014510e5 241 char hourBuf[3];
kkizirian 0:ce60014510e5 242 int hour;
kkizirian 0:ce60014510e5 243 char minuteBuf[3];
kkizirian 0:ce60014510e5 244 int minute;
kkizirian 0:ce60014510e5 245 char ampmBuf[3];
kkizirian 0:ce60014510e5 246 uLCD.cls();
kkizirian 0:ce60014510e5 247 uLCD.locate(0, 1);
kkizirian 0:ce60014510e5 248 uLCD.printf("Reading class file...");
kkizirian 2:c708e2027970 249
kkizirian 0:ce60014510e5 250 memset(buildingBuf, 0, sizeof(buildingBuf));
kkizirian 0:ce60014510e5 251 memset(hourBuf, 0, sizeof(hourBuf));
kkizirian 0:ce60014510e5 252 memset(minuteBuf, 0, sizeof(minuteBuf));
kkizirian 0:ce60014510e5 253 memset(ampmBuf, 0, sizeof(ampmBuf));
kkizirian 0:ce60014510e5 254 memset(line, 0, sizeof(line));
kkizirian 0:ce60014510e5 255
kkizirian 0:ce60014510e5 256 if (readFp == NULL)
kkizirian 0:ce60014510e5 257 return;
kkizirian 0:ce60014510e5 258 else {
kkizirian 0:ce60014510e5 259 while (!feof(readFp)) {
kkizirian 0:ce60014510e5 260 fgets(line, 15, readFp);
kkizirian 0:ce60014510e5 261 if(line[8] == NULL)
kkizirian 0:ce60014510e5 262 continue;
kkizirian 0:ce60014510e5 263 memcpy(buildingBuf, line, 3);
kkizirian 0:ce60014510e5 264 memcpy(hourBuf, &line[4], 2);
kkizirian 0:ce60014510e5 265 memcpy(minuteBuf, &line[7], 2);
kkizirian 0:ce60014510e5 266 memcpy(ampmBuf, &line[10], 2);
kkizirian 0:ce60014510e5 267
kkizirian 0:ce60014510e5 268 string building = buildingBuf;
kkizirian 0:ce60014510e5 269 hour = atoi(hourBuf);
kkizirian 0:ce60014510e5 270 minute = atoi(minuteBuf);
kkizirian 0:ce60014510e5 271 string ampm = ampmBuf;
kkizirian 0:ce60014510e5 272
kkizirian 0:ce60014510e5 273 Course temp(building, hour, minute, ampm);
kkizirian 0:ce60014510e5 274 cVec.push_back(temp);
kkizirian 0:ce60014510e5 275 }
kkizirian 0:ce60014510e5 276 }
kkizirian 0:ce60014510e5 277 fclose(readFp);
kkizirian 0:ce60014510e5 278 return;
kkizirian 2:c708e2027970 279 }
kkizirian 2:c708e2027970 280
kkizirian 2:c708e2027970 281 void displayCourseVec()
kkizirian 2:c708e2027970 282 {
kkizirian 2:c708e2027970 283 if (courseVec.size() == 0) {
kkizirian 2:c708e2027970 284 uLCD.cls();
kkizirian 2:c708e2027970 285 uLCD.locate(0,0);
kkizirian 2:c708e2027970 286 uLCD.printf("No classes input!");
kkizirian 2:c708e2027970 287 uLCD.locate(0,1);
kkizirian 2:c708e2027970 288 } else {
kkizirian 2:c708e2027970 289 uLCD.cls();
kkizirian 2:c708e2027970 290 uLCD.locate(0,1);
kkizirian 2:c708e2027970 291 for (int i = 0; i < courseVec.size(); i++) {
kkizirian 2:c708e2027970 292 uLCD.locate(0, i);
kkizirian 2:c708e2027970 293 uLCD.printf("%s", courseVec[i].getDisplayString());
kkizirian 2:c708e2027970 294 }
kkizirian 2:c708e2027970 295 }
kkizirian 3:0ca91f8afec5 296 }
kkizirian 3:0ca91f8afec5 297
kkizirian 3:0ca91f8afec5 298 float calculateHeading(float mx, float my)
kkizirian 3:0ca91f8afec5 299 {
kkizirian 3:0ca91f8afec5 300 float heading = 0.0;
kkizirian 3:0ca91f8afec5 301 if (my == 0.0)
kkizirian 3:0ca91f8afec5 302 heading = (mx < 0.0) ? 180.0 : 0.0;
kkizirian 3:0ca91f8afec5 303 else
kkizirian 3:0ca91f8afec5 304 heading = atan2(mx, my)*360.0/(2.0*PI);
kkizirian 3:0ca91f8afec5 305 //pc.printf("heading atan=%f \n\r",heading);
kkizirian 3:0ca91f8afec5 306 heading -= DECLINATION; //correct for geo location
kkizirian 3:0ca91f8afec5 307 if(heading>180.0) heading = heading - 360.0;
kkizirian 3:0ca91f8afec5 308 else if(heading<-180.0) heading = 360.0 + heading;
kkizirian 3:0ca91f8afec5 309 else if(heading<0.0) heading = 360.0 + heading;
kkizirian 3:0ca91f8afec5 310
kkizirian 3:0ca91f8afec5 311 // Convert everything from radians to degrees:
kkizirian 3:0ca91f8afec5 312 //heading *= 180.0 / PI;
kkizirian 3:0ca91f8afec5 313
kkizirian 3:0ca91f8afec5 314 //pc.printf("Magnetic Heading: %f degress\n\r",heading);
kkizirian 3:0ca91f8afec5 315 return heading;
kkizirian 3:0ca91f8afec5 316 }
kkizirian 3:0ca91f8afec5 317
kkizirian 3:0ca91f8afec5 318 float computeAngleToDestination(float diffLat, float diffLong)
kkizirian 3:0ca91f8afec5 319 {
kkizirian 3:0ca91f8afec5 320 float angle = 0.0;
kkizirian 3:0ca91f8afec5 321
kkizirian 3:0ca91f8afec5 322 if (diffLat > 0) {
kkizirian 3:0ca91f8afec5 323 if (diffLong > 0) {
kkizirian 3:0ca91f8afec5 324 // in quadrant 1
kkizirian 3:0ca91f8afec5 325 angle = 180*atan2(diffLat,diffLong)/PI;
kkizirian 3:0ca91f8afec5 326 } else {
kkizirian 3:0ca91f8afec5 327 // in quadrant 2
kkizirian 3:0ca91f8afec5 328 angle = 180*atan2(diffLat,-1 * diffLong)/PI;
kkizirian 3:0ca91f8afec5 329 angle = 180 - angle;
kkizirian 3:0ca91f8afec5 330 }
kkizirian 3:0ca91f8afec5 331 } else {
kkizirian 3:0ca91f8afec5 332 if (diffLong > 0) {
kkizirian 3:0ca91f8afec5 333 // in quadrant 4
kkizirian 3:0ca91f8afec5 334 angle = 180*atan2(-1*diffLat, diffLong)/PI;
kkizirian 3:0ca91f8afec5 335 angle = 360 - angle;
kkizirian 3:0ca91f8afec5 336 } else {
kkizirian 3:0ca91f8afec5 337 // in quadrant 3
kkizirian 3:0ca91f8afec5 338 angle = 180*atan2(-1*diffLat, -1*diffLong)/PI;
kkizirian 3:0ca91f8afec5 339 angle = 180 + angle;
kkizirian 3:0ca91f8afec5 340 }
kkizirian 3:0ca91f8afec5 341 }
kkizirian 3:0ca91f8afec5 342
kkizirian 3:0ca91f8afec5 343 //pc.printf("Angle to Destination: %f degress\n\r",angle);
kkizirian 3:0ca91f8afec5 344 return angle;
kkizirian 5:430f44669f94 345 }
kkizirian 5:430f44669f94 346
kkizirian 5:430f44669f94 347 int getNextClass(vector<Course>& cVec)
kkizirian 5:430f44669f94 348 {
kkizirian 5:430f44669f94 349 int numIterations = 0;
kkizirian 5:430f44669f94 350 for (int i = 0; i < cVec.size(); i++) {
kkizirian 5:430f44669f94 351 numIterations++;
kkizirian 5:430f44669f94 352 pc.printf("Iteration : %i\ncurrHour: %i\ncourseHour: %i\n", numIterations, currHour, cVec[i].getHour_inMilitaryTime());
kkizirian 5:430f44669f94 353 if (courseVec[i].getHour_inMilitaryTime() < currHour)
kkizirian 5:430f44669f94 354 continue;
kkizirian 5:430f44669f94 355 else if (courseVec[i].getHour_inMilitaryTime() > currHour)
kkizirian 5:430f44669f94 356 return i;
kkizirian 5:430f44669f94 357 else if (courseVec[i].getHour_inMilitaryTime() == currHour) {
kkizirian 5:430f44669f94 358 if (courseVec[i].getMinute() <= currMinute)
kkizirian 5:430f44669f94 359 continue;
kkizirian 5:430f44669f94 360 else if (courseVec[i].getMinute() > currMinute) {
kkizirian 5:430f44669f94 361 return i;
kkizirian 5:430f44669f94 362 }
kkizirian 5:430f44669f94 363 }
kkizirian 5:430f44669f94 364 if (numIterations == cVec.size())
kkizirian 5:430f44669f94 365 return 0;
kkizirian 5:430f44669f94 366 }
kkizirian 5:430f44669f94 367 return 0;
kkizirian 5:430f44669f94 368 }
kkizirian 5:430f44669f94 369
kkizirian 5:430f44669f94 370 void timeToNextClass()
kkizirian 6:ceb6a5426add 371 {
kkizirian 5:430f44669f94 372 hoursToNextClass = courseVec[nextClass].getHour_inMilitaryTime() - currHour;
kkizirian 5:430f44669f94 373 if (hoursToNextClass < 0)
kkizirian 5:430f44669f94 374 hoursToNextClass += 24;
kkizirian 5:430f44669f94 375
kkizirian 5:430f44669f94 376 minutesToNextClass = courseVec[nextClass].getMinute() - currMinute;
kkizirian 5:430f44669f94 377 if (minutesToNextClass < 0) {
kkizirian 5:430f44669f94 378 hoursToNextClass--;
kkizirian 5:430f44669f94 379 minutesToNextClass += 60;
kkizirian 5:430f44669f94 380 }
kkizirian 6:ceb6a5426add 381
kkizirian 5:430f44669f94 382 secondsToNextClass = 60 - currSecond;
kkizirian 5:430f44669f94 383 minutesToNextClass--;
kkizirian 6:ceb6a5426add 384
kkizirian 5:430f44669f94 385 if (minutesToNextClass < 0) {
kkizirian 5:430f44669f94 386 hoursToNextClass--;
kkizirian 5:430f44669f94 387 minutesToNextClass += 60;
kkizirian 5:430f44669f94 388 }
kkizirian 6:ceb6a5426add 389
kkizirian 5:430f44669f94 390 if (hoursToNextClass < 0)
kkizirian 5:430f44669f94 391 hoursToNextClass += 24;
kkizirian 6:ceb6a5426add 392 }
kkizirian 6:ceb6a5426add 393
kkizirian 6:ceb6a5426add 394 // convert from DDMM.mmmm to decimal
kkizirian 6:ceb6a5426add 395 float latToDecimal(float latitudeInDDMM)
kkizirian 6:ceb6a5426add 396 {
kkizirian 6:ceb6a5426add 397 float latInDec = 0.0;
kkizirian 6:ceb6a5426add 398 float temp = 0.0;
kkizirian 6:ceb6a5426add 399 latInDec = latitudeInDDMM / 100;
kkizirian 6:ceb6a5426add 400 temp = latInDec - floor(latInDec);
kkizirian 6:ceb6a5426add 401 temp = temp / .6;
kkizirian 6:ceb6a5426add 402 latInDec = floor(latInDec) + temp;
kkizirian 6:ceb6a5426add 403 return latInDec;
kkizirian 6:ceb6a5426add 404 }
kkizirian 6:ceb6a5426add 405
kkizirian 6:ceb6a5426add 406 float longToDecimal(float longitudeInDDMM)
kkizirian 6:ceb6a5426add 407 {
kkizirian 6:ceb6a5426add 408 float longInDec = 0.0;
kkizirian 6:ceb6a5426add 409 float temp = 0.0;
kkizirian 6:ceb6a5426add 410 longInDec = longitudeInDDMM / 100;
kkizirian 6:ceb6a5426add 411 temp = longInDec - floor(longInDec);
kkizirian 6:ceb6a5426add 412 temp = temp / .6;
kkizirian 6:ceb6a5426add 413 longInDec = floor(longInDec) + temp;
kkizirian 6:ceb6a5426add 414 return -1 * longInDec;
kkizirian 0:ce60014510e5 415 }