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 21:44:06 2016 +0000
Revision:
11:e28acc7bf487
Parent:
10:5579618161ef
Child:
13:0eb9fbd63fb2
Removed some unnecessary prints and wait statements

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 9:5b871fb9f554 27 float longToDecimal(float longitudeInDDMM, char eastOrWest), latToDecimal(float latitudeInDDMM, char northOrSouth);
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 5:430f44669f94 86 gps_Serial = new Serial(p28,p27); //serial object for use w/ GPS
kkizirian 5:430f44669f94 87 Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
kkizirian 5:430f44669f94 88 char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
kkizirian 5:430f44669f94 89 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
kkizirian 5:430f44669f94 90 const int refresh_Time = 2000; //refresh time in ms
kkizirian 5:430f44669f94 91
kkizirian 5:430f44669f94 92 myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *)
kkizirian 5:430f44669f94 93 //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf
kkizirian 5:430f44669f94 94
kkizirian 5:430f44669f94 95 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 96 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
kkizirian 5:430f44669f94 97 myGPS.sendCommand(PGCMD_ANTENNA);
kkizirian 5:430f44669f94 98
kkizirian 4:93a4b415fe6c 99 float myLat = 33.775991;
kkizirian 4:93a4b415fe6c 100 float myLong = -84.397128;
kkizirian 2:c708e2027970 101
kkizirian 5:430f44669f94 102 float destinationLat = courseVec[1].getLat();
kkizirian 5:430f44669f94 103 float destinationLong = courseVec[1].getLong();
kkizirian 5:430f44669f94 104
kkizirian 4:93a4b415fe6c 105 screen_change = true;
kkizirian 0:ce60014510e5 106 pc.baud(9600);
kkizirian 5:430f44669f94 107 c = myGPS.read(); //queries the GPS
kkizirian 5:430f44669f94 108 currHour = myGPS.hour - 5; //for eastern time zone
kkizirian 5:430f44669f94 109 if (currHour < 0)
kkizirian 5:430f44669f94 110 currHour += 24;
kkizirian 5:430f44669f94 111 currMinute = myGPS.minute;
kkizirian 5:430f44669f94 112 currSecond = myGPS.seconds;
kkizirian 5:430f44669f94 113 nextClass = getNextClass(courseVec);
kkizirian 2:c708e2027970 114
kkizirian 5:430f44669f94 115 refresh_Timer.start(); //starts the clock on the timer
kkizirian 0:ce60014510e5 116 while(1) {
kkizirian 5:430f44669f94 117 c = myGPS.read(); //queries the GPS
kkizirian 5:430f44669f94 118
kkizirian 5:430f44669f94 119 //if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused
kkizirian 5:430f44669f94 120
kkizirian 5:430f44669f94 121 //check if we recieved a new message from GPS, if so, attempt to parse it,
kkizirian 5:430f44669f94 122 if ( myGPS.newNMEAreceived() ) {
kkizirian 5:430f44669f94 123 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
kkizirian 5:430f44669f94 124 continue;
kkizirian 5:430f44669f94 125 }
kkizirian 5:430f44669f94 126 }
kkizirian 5:430f44669f94 127
kkizirian 5:430f44669f94 128 if (refresh_Timer.read_ms() >= refresh_Time) {
kkizirian 5:430f44669f94 129 refresh_Timer.reset();
kkizirian 6:ceb6a5426add 130 if (myGPS.fix) {
kkizirian 9:5b871fb9f554 131 myLat = latToDecimal(myGPS.latitude, myGPS.lat);
kkizirian 9:5b871fb9f554 132 myLong = longToDecimal(myGPS.longitude, myGPS.lon);
kkizirian 6:ceb6a5426add 133 }
kkizirian 9:5b871fb9f554 134
kkizirian 5:430f44669f94 135 currHour = myGPS.hour - 5; //for eastern time zone
kkizirian 5:430f44669f94 136 if (currHour < 0)
kkizirian 5:430f44669f94 137 currHour += 24;
kkizirian 5:430f44669f94 138 currMinute = myGPS.minute;
kkizirian 5:430f44669f94 139 currSecond = myGPS.seconds;
kkizirian 5:430f44669f94 140 nextClass = getNextClass(courseVec);
kkizirian 5:430f44669f94 141 timeToNextClass();
kkizirian 5:430f44669f94 142
kkizirian 5:430f44669f94 143 switch(current_screen) {
kkizirian 5:430f44669f94 144 case 0:
kkizirian 5:430f44669f94 145 wait(.2);
kkizirian 5:430f44669f94 146 if (screen_change) {
kkizirian 5:430f44669f94 147 displayCourseVec();
kkizirian 5:430f44669f94 148 screen_change = false;
kkizirian 5:430f44669f94 149 }
kkizirian 5:430f44669f94 150 break;
kkizirian 5:430f44669f94 151 case 1:
kkizirian 5:430f44669f94 152 IMU.readMag();
kkizirian 5:430f44669f94 153 h = calculateHeading(IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my));
kkizirian 5:430f44669f94 154 pc.printf("Heading: %f\n", h);
kkizirian 5:430f44669f94 155 destinationLat = courseVec[nextClass].getLat();
kkizirian 5:430f44669f94 156 destinationLong = courseVec[nextClass].getLong();
kkizirian 5:430f44669f94 157 float diffLat = destinationLat - myLat;
kkizirian 5:430f44669f94 158 float diffLong = destinationLong - myLong;
kkizirian 5:430f44669f94 159 angleToDest = computeAngleToDestination(diffLat, diffLong);
kkizirian 5:430f44669f94 160 h = angleToDest - h;
kkizirian 3:0ca91f8afec5 161 h = h - 90;
kkizirian 5:430f44669f94 162 if (h < 0)
kkizirian 5:430f44669f94 163 h = h + 360;
kkizirian 5:430f44669f94 164 if (h > 360)
kkizirian 5:430f44669f94 165 h = h - 360;
kkizirian 5:430f44669f94 166 xEnd = 0;
kkizirian 5:430f44669f94 167 yEnd = 0;
kkizirian 5:430f44669f94 168 float rads = 0.0;
kkizirian 5:430f44669f94 169 //uLCD.cls();
kkizirian 5:430f44669f94 170 if (h < 90) {
kkizirian 5:430f44669f94 171 rads = h * PI / 180;
kkizirian 5:430f44669f94 172 xEnd = floor(63 * sin(rads) + .5);
kkizirian 5:430f44669f94 173 yEnd = floor(63 * cos(rads) + .5);
kkizirian 5:430f44669f94 174 xEnd = 63 + xEnd;
kkizirian 5:430f44669f94 175 yEnd = 63 + yEnd;
kkizirian 5:430f44669f94 176 } else if (90 < h < 180) {
kkizirian 5:430f44669f94 177 h = h - 90;
kkizirian 5:430f44669f94 178 rads = h * PI / 180;
kkizirian 5:430f44669f94 179 xEnd = floor(63 * cos(rads) + .5);
kkizirian 5:430f44669f94 180 yEnd = floor(63 * sin(rads) + .5);
kkizirian 5:430f44669f94 181 xEnd = 63 + xEnd;
kkizirian 5:430f44669f94 182 yEnd = 63 - yEnd;
kkizirian 5:430f44669f94 183 } else if (180 < h < 270) {
kkizirian 5:430f44669f94 184 h = h - 180;
kkizirian 5:430f44669f94 185 rads = h * PI / 180;
kkizirian 5:430f44669f94 186 xEnd = floor(63 * sin(rads) + .5);
kkizirian 5:430f44669f94 187 yEnd = floor(63 * cos(rads) + .5);
kkizirian 5:430f44669f94 188 xEnd = 63 - xEnd;
kkizirian 5:430f44669f94 189 yEnd = 63 - yEnd;
kkizirian 5:430f44669f94 190 } else if (270 < h) {
kkizirian 5:430f44669f94 191 h = h - 270;
kkizirian 5:430f44669f94 192 rads = h * PI / 180;
kkizirian 5:430f44669f94 193 xEnd = floor(63 * cos(rads) + .5);
kkizirian 5:430f44669f94 194 yEnd = floor(63 * sin(rads) + .5);
kkizirian 5:430f44669f94 195 xEnd = 63 - xEnd;
kkizirian 5:430f44669f94 196 yEnd = 63 + yEnd;
kkizirian 5:430f44669f94 197 }
kkizirian 2:c708e2027970 198 uLCD.cls();
kkizirian 5:430f44669f94 199 uLCD.line(63, 63, xEnd, yEnd, WHITE);
kkizirian 5:430f44669f94 200 break;
kkizirian 5:430f44669f94 201 case 2:
kkizirian 5:430f44669f94 202 //uLCD.cls();
kkizirian 7:e4d667beaf33 203 uLCD.locate(0,2);
kkizirian 8:3266cc9eda81 204 uLCD.printf(" ");
kkizirian 6:ceb6a5426add 205 uLCD.locate(0,3);
kkizirian 5:430f44669f94 206 uLCD.printf("Time to next class");
kkizirian 6:ceb6a5426add 207 uLCD.locate(0, 4);
kkizirian 5:430f44669f94 208 uLCD.printf("%02dhrs %02dminutes", hoursToNextClass, minutesToNextClass);
kkizirian 6:ceb6a5426add 209 uLCD.locate(0, 5);
kkizirian 5:430f44669f94 210 uLCD.printf("%02dseconds", secondsToNextClass);
kkizirian 6:ceb6a5426add 211 uLCD.locate(0, 0);
kkizirian 7:e4d667beaf33 212 uLCD.printf("Next class ");
kkizirian 5:430f44669f94 213 if (nextClass != currClass) {
kkizirian 6:ceb6a5426add 214 uLCD.locate(0, 1);
kkizirian 5:430f44669f94 215 uLCD.printf(" ");
kkizirian 6:ceb6a5426add 216 uLCD.locate(0, 1);
kkizirian 5:430f44669f94 217 uLCD.printf("%s", courseVec[nextClass].getDisplayString());
kkizirian 5:430f44669f94 218 }
kkizirian 5:430f44669f94 219 currClass = nextClass;
kkizirian 5:430f44669f94 220 break;
kkizirian 5:430f44669f94 221 }
kkizirian 0:ce60014510e5 222 }
kkizirian 0:ce60014510e5 223 }
kkizirian 0:ce60014510e5 224 }
kkizirian 0:ce60014510e5 225
kkizirian 2:c708e2027970 226 void readClassFile(vector<Course>& cVec)
kkizirian 2:c708e2027970 227 {
kkizirian 0:ce60014510e5 228 cVec.clear();
kkizirian 0:ce60014510e5 229
kkizirian 0:ce60014510e5 230 FILE *readFp = fopen("/sd/classdir/classes.txt", "r");
kkizirian 0:ce60014510e5 231 char line[15];
kkizirian 0:ce60014510e5 232 char buildingBuf[4];
kkizirian 0:ce60014510e5 233 char hourBuf[3];
kkizirian 0:ce60014510e5 234 int hour;
kkizirian 0:ce60014510e5 235 char minuteBuf[3];
kkizirian 0:ce60014510e5 236 int minute;
kkizirian 0:ce60014510e5 237 char ampmBuf[3];
kkizirian 0:ce60014510e5 238 uLCD.cls();
kkizirian 0:ce60014510e5 239 uLCD.locate(0, 1);
kkizirian 0:ce60014510e5 240 uLCD.printf("Reading class file...");
kkizirian 2:c708e2027970 241
kkizirian 0:ce60014510e5 242 memset(buildingBuf, 0, sizeof(buildingBuf));
kkizirian 0:ce60014510e5 243 memset(hourBuf, 0, sizeof(hourBuf));
kkizirian 0:ce60014510e5 244 memset(minuteBuf, 0, sizeof(minuteBuf));
kkizirian 0:ce60014510e5 245 memset(ampmBuf, 0, sizeof(ampmBuf));
kkizirian 0:ce60014510e5 246 memset(line, 0, sizeof(line));
kkizirian 0:ce60014510e5 247
kkizirian 0:ce60014510e5 248 if (readFp == NULL)
kkizirian 0:ce60014510e5 249 return;
kkizirian 0:ce60014510e5 250 else {
kkizirian 0:ce60014510e5 251 while (!feof(readFp)) {
kkizirian 0:ce60014510e5 252 fgets(line, 15, readFp);
kkizirian 0:ce60014510e5 253 if(line[8] == NULL)
kkizirian 0:ce60014510e5 254 continue;
kkizirian 0:ce60014510e5 255 memcpy(buildingBuf, line, 3);
kkizirian 0:ce60014510e5 256 memcpy(hourBuf, &line[4], 2);
kkizirian 0:ce60014510e5 257 memcpy(minuteBuf, &line[7], 2);
kkizirian 0:ce60014510e5 258 memcpy(ampmBuf, &line[10], 2);
kkizirian 0:ce60014510e5 259
kkizirian 0:ce60014510e5 260 string building = buildingBuf;
kkizirian 0:ce60014510e5 261 hour = atoi(hourBuf);
kkizirian 0:ce60014510e5 262 minute = atoi(minuteBuf);
kkizirian 0:ce60014510e5 263 string ampm = ampmBuf;
kkizirian 0:ce60014510e5 264
kkizirian 0:ce60014510e5 265 Course temp(building, hour, minute, ampm);
kkizirian 0:ce60014510e5 266 cVec.push_back(temp);
kkizirian 0:ce60014510e5 267 }
kkizirian 0:ce60014510e5 268 }
kkizirian 0:ce60014510e5 269 fclose(readFp);
kkizirian 0:ce60014510e5 270 return;
kkizirian 2:c708e2027970 271 }
kkizirian 2:c708e2027970 272
kkizirian 2:c708e2027970 273 void displayCourseVec()
kkizirian 2:c708e2027970 274 {
kkizirian 2:c708e2027970 275 if (courseVec.size() == 0) {
kkizirian 2:c708e2027970 276 uLCD.cls();
kkizirian 2:c708e2027970 277 uLCD.locate(0,0);
kkizirian 2:c708e2027970 278 uLCD.printf("No classes input!");
kkizirian 2:c708e2027970 279 uLCD.locate(0,1);
kkizirian 2:c708e2027970 280 } else {
kkizirian 2:c708e2027970 281 uLCD.cls();
kkizirian 2:c708e2027970 282 uLCD.locate(0,1);
kkizirian 2:c708e2027970 283 for (int i = 0; i < courseVec.size(); i++) {
kkizirian 2:c708e2027970 284 uLCD.locate(0, i);
kkizirian 2:c708e2027970 285 uLCD.printf("%s", courseVec[i].getDisplayString());
kkizirian 2:c708e2027970 286 }
kkizirian 2:c708e2027970 287 }
kkizirian 3:0ca91f8afec5 288 }
kkizirian 3:0ca91f8afec5 289
kkizirian 3:0ca91f8afec5 290 float calculateHeading(float mx, float my)
kkizirian 3:0ca91f8afec5 291 {
kkizirian 3:0ca91f8afec5 292 float heading = 0.0;
kkizirian 3:0ca91f8afec5 293 if (my == 0.0)
kkizirian 3:0ca91f8afec5 294 heading = (mx < 0.0) ? 180.0 : 0.0;
kkizirian 3:0ca91f8afec5 295 else
kkizirian 3:0ca91f8afec5 296 heading = atan2(mx, my)*360.0/(2.0*PI);
kkizirian 3:0ca91f8afec5 297 //pc.printf("heading atan=%f \n\r",heading);
kkizirian 3:0ca91f8afec5 298 heading -= DECLINATION; //correct for geo location
kkizirian 3:0ca91f8afec5 299 if(heading>180.0) heading = heading - 360.0;
kkizirian 3:0ca91f8afec5 300 else if(heading<-180.0) heading = 360.0 + heading;
kkizirian 3:0ca91f8afec5 301 else if(heading<0.0) heading = 360.0 + heading;
kkizirian 3:0ca91f8afec5 302
kkizirian 3:0ca91f8afec5 303 // Convert everything from radians to degrees:
kkizirian 3:0ca91f8afec5 304 //heading *= 180.0 / PI;
kkizirian 3:0ca91f8afec5 305
kkizirian 3:0ca91f8afec5 306 //pc.printf("Magnetic Heading: %f degress\n\r",heading);
kkizirian 3:0ca91f8afec5 307 return heading;
kkizirian 3:0ca91f8afec5 308 }
kkizirian 3:0ca91f8afec5 309
kkizirian 3:0ca91f8afec5 310 float computeAngleToDestination(float diffLat, float diffLong)
kkizirian 3:0ca91f8afec5 311 {
kkizirian 3:0ca91f8afec5 312 float angle = 0.0;
kkizirian 3:0ca91f8afec5 313
kkizirian 3:0ca91f8afec5 314 if (diffLat > 0) {
kkizirian 3:0ca91f8afec5 315 if (diffLong > 0) {
kkizirian 3:0ca91f8afec5 316 // in quadrant 1
kkizirian 3:0ca91f8afec5 317 angle = 180*atan2(diffLat,diffLong)/PI;
kkizirian 3:0ca91f8afec5 318 } else {
kkizirian 3:0ca91f8afec5 319 // in quadrant 2
kkizirian 3:0ca91f8afec5 320 angle = 180*atan2(diffLat,-1 * diffLong)/PI;
kkizirian 3:0ca91f8afec5 321 angle = 180 - angle;
kkizirian 3:0ca91f8afec5 322 }
kkizirian 3:0ca91f8afec5 323 } else {
kkizirian 3:0ca91f8afec5 324 if (diffLong > 0) {
kkizirian 3:0ca91f8afec5 325 // in quadrant 4
kkizirian 3:0ca91f8afec5 326 angle = 180*atan2(-1*diffLat, diffLong)/PI;
kkizirian 3:0ca91f8afec5 327 angle = 360 - angle;
kkizirian 3:0ca91f8afec5 328 } else {
kkizirian 3:0ca91f8afec5 329 // in quadrant 3
kkizirian 3:0ca91f8afec5 330 angle = 180*atan2(-1*diffLat, -1*diffLong)/PI;
kkizirian 3:0ca91f8afec5 331 angle = 180 + angle;
kkizirian 3:0ca91f8afec5 332 }
kkizirian 3:0ca91f8afec5 333 }
kkizirian 3:0ca91f8afec5 334
kkizirian 3:0ca91f8afec5 335 //pc.printf("Angle to Destination: %f degress\n\r",angle);
kkizirian 3:0ca91f8afec5 336 return angle;
kkizirian 5:430f44669f94 337 }
kkizirian 5:430f44669f94 338
kkizirian 5:430f44669f94 339 int getNextClass(vector<Course>& cVec)
kkizirian 5:430f44669f94 340 {
kkizirian 5:430f44669f94 341 int numIterations = 0;
kkizirian 5:430f44669f94 342 for (int i = 0; i < cVec.size(); i++) {
kkizirian 5:430f44669f94 343 numIterations++;
kkizirian 5:430f44669f94 344 pc.printf("Iteration : %i\ncurrHour: %i\ncourseHour: %i\n", numIterations, currHour, cVec[i].getHour_inMilitaryTime());
kkizirian 5:430f44669f94 345 if (courseVec[i].getHour_inMilitaryTime() < currHour)
kkizirian 5:430f44669f94 346 continue;
kkizirian 5:430f44669f94 347 else if (courseVec[i].getHour_inMilitaryTime() > currHour)
kkizirian 5:430f44669f94 348 return i;
kkizirian 5:430f44669f94 349 else if (courseVec[i].getHour_inMilitaryTime() == currHour) {
kkizirian 5:430f44669f94 350 if (courseVec[i].getMinute() <= currMinute)
kkizirian 5:430f44669f94 351 continue;
kkizirian 5:430f44669f94 352 else if (courseVec[i].getMinute() > currMinute) {
kkizirian 5:430f44669f94 353 return i;
kkizirian 5:430f44669f94 354 }
kkizirian 5:430f44669f94 355 }
kkizirian 5:430f44669f94 356 if (numIterations == cVec.size())
kkizirian 5:430f44669f94 357 return 0;
kkizirian 5:430f44669f94 358 }
kkizirian 5:430f44669f94 359 return 0;
kkizirian 5:430f44669f94 360 }
kkizirian 5:430f44669f94 361
kkizirian 5:430f44669f94 362 void timeToNextClass()
kkizirian 6:ceb6a5426add 363 {
kkizirian 5:430f44669f94 364 hoursToNextClass = courseVec[nextClass].getHour_inMilitaryTime() - currHour;
kkizirian 5:430f44669f94 365 if (hoursToNextClass < 0)
kkizirian 5:430f44669f94 366 hoursToNextClass += 24;
kkizirian 5:430f44669f94 367
kkizirian 5:430f44669f94 368 minutesToNextClass = courseVec[nextClass].getMinute() - currMinute;
kkizirian 5:430f44669f94 369 if (minutesToNextClass < 0) {
kkizirian 5:430f44669f94 370 hoursToNextClass--;
kkizirian 5:430f44669f94 371 minutesToNextClass += 60;
kkizirian 5:430f44669f94 372 }
kkizirian 6:ceb6a5426add 373
kkizirian 5:430f44669f94 374 secondsToNextClass = 60 - currSecond;
kkizirian 5:430f44669f94 375 minutesToNextClass--;
kkizirian 6:ceb6a5426add 376
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 if (hoursToNextClass < 0)
kkizirian 5:430f44669f94 383 hoursToNextClass += 24;
kkizirian 6:ceb6a5426add 384 }
kkizirian 6:ceb6a5426add 385
kkizirian 6:ceb6a5426add 386 // convert from DDMM.mmmm to decimal
kkizirian 9:5b871fb9f554 387 float latToDecimal(float latitudeInDDMM, char northOrSouth)
kkizirian 6:ceb6a5426add 388 {
kkizirian 6:ceb6a5426add 389 float latInDec = 0.0;
kkizirian 6:ceb6a5426add 390 float temp = 0.0;
kkizirian 6:ceb6a5426add 391 latInDec = latitudeInDDMM / 100;
kkizirian 6:ceb6a5426add 392 temp = latInDec - floor(latInDec);
kkizirian 6:ceb6a5426add 393 temp = temp / .6;
kkizirian 6:ceb6a5426add 394 latInDec = floor(latInDec) + temp;
kkizirian 10:5579618161ef 395 if (northOrSouth == 'S')
kkizirian 9:5b871fb9f554 396 return -1 * latInDec;
kkizirian 6:ceb6a5426add 397 return latInDec;
kkizirian 6:ceb6a5426add 398 }
kkizirian 6:ceb6a5426add 399
kkizirian 9:5b871fb9f554 400 float longToDecimal(float longitudeInDDMM, char eastOrWest)
kkizirian 6:ceb6a5426add 401 {
kkizirian 6:ceb6a5426add 402 float longInDec = 0.0;
kkizirian 6:ceb6a5426add 403 float temp = 0.0;
kkizirian 6:ceb6a5426add 404 longInDec = longitudeInDDMM / 100;
kkizirian 6:ceb6a5426add 405 temp = longInDec - floor(longInDec);
kkizirian 6:ceb6a5426add 406 temp = temp / .6;
kkizirian 6:ceb6a5426add 407 longInDec = floor(longInDec) + temp;
kkizirian 9:5b871fb9f554 408 if (eastOrWest == 'E')
kkizirian 9:5b871fb9f554 409 return longInDec;
kkizirian 6:ceb6a5426add 410 return -1 * longInDec;
kkizirian 0:ce60014510e5 411 }