Guides the user to their classes
Dependencies: 4DGL-uLCD-SE Course SDFileSystem mbed PinDetect LSM9DS1_Library_cal MBed_Adafruit-GPS-Library
main.cpp@9:5b871fb9f554, 2016-12-08 (annotated)
- Committer:
- kkizirian
- Date:
- Thu Dec 08 21:34:01 2016 +0000
- Revision:
- 9:5b871fb9f554
- Parent:
- 8:3266cc9eda81
- Child:
- 10:5579618161ef
Updated lat and long conversion functions to account for north, south, east, and west
Who changed what in which revision?
User | Revision | Line number | New 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 | wait(.1); |
kkizirian | 5:430f44669f94 | 153 | if (screen_change) { |
kkizirian | 5:430f44669f94 | 154 | uLCD.printf("Compass screen"); |
kkizirian | 5:430f44669f94 | 155 | screen_change = false; |
kkizirian | 5:430f44669f94 | 156 | } |
kkizirian | 3:0ca91f8afec5 | 157 | |
kkizirian | 5:430f44669f94 | 158 | IMU.readMag(); |
kkizirian | 5:430f44669f94 | 159 | h = calculateHeading(IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my)); |
kkizirian | 5:430f44669f94 | 160 | pc.printf("Heading: %f\n", h); |
kkizirian | 5:430f44669f94 | 161 | destinationLat = courseVec[nextClass].getLat(); |
kkizirian | 5:430f44669f94 | 162 | destinationLong = courseVec[nextClass].getLong(); |
kkizirian | 5:430f44669f94 | 163 | float diffLat = destinationLat - myLat; |
kkizirian | 5:430f44669f94 | 164 | float diffLong = destinationLong - myLong; |
kkizirian | 5:430f44669f94 | 165 | angleToDest = computeAngleToDestination(diffLat, diffLong); |
kkizirian | 5:430f44669f94 | 166 | h = angleToDest - h; |
kkizirian | 3:0ca91f8afec5 | 167 | h = h - 90; |
kkizirian | 5:430f44669f94 | 168 | if (h < 0) |
kkizirian | 5:430f44669f94 | 169 | h = h + 360; |
kkizirian | 5:430f44669f94 | 170 | if (h > 360) |
kkizirian | 5:430f44669f94 | 171 | h = h - 360; |
kkizirian | 5:430f44669f94 | 172 | xEnd = 0; |
kkizirian | 5:430f44669f94 | 173 | yEnd = 0; |
kkizirian | 5:430f44669f94 | 174 | float rads = 0.0; |
kkizirian | 5:430f44669f94 | 175 | //uLCD.cls(); |
kkizirian | 5:430f44669f94 | 176 | if (h < 90) { |
kkizirian | 5:430f44669f94 | 177 | rads = h * PI / 180; |
kkizirian | 5:430f44669f94 | 178 | xEnd = floor(63 * sin(rads) + .5); |
kkizirian | 5:430f44669f94 | 179 | yEnd = floor(63 * cos(rads) + .5); |
kkizirian | 5:430f44669f94 | 180 | xEnd = 63 + xEnd; |
kkizirian | 5:430f44669f94 | 181 | yEnd = 63 + yEnd; |
kkizirian | 5:430f44669f94 | 182 | } else if (90 < h < 180) { |
kkizirian | 5:430f44669f94 | 183 | h = h - 90; |
kkizirian | 5:430f44669f94 | 184 | rads = h * PI / 180; |
kkizirian | 5:430f44669f94 | 185 | xEnd = floor(63 * cos(rads) + .5); |
kkizirian | 5:430f44669f94 | 186 | yEnd = floor(63 * sin(rads) + .5); |
kkizirian | 5:430f44669f94 | 187 | xEnd = 63 + xEnd; |
kkizirian | 5:430f44669f94 | 188 | yEnd = 63 - yEnd; |
kkizirian | 5:430f44669f94 | 189 | } else if (180 < h < 270) { |
kkizirian | 5:430f44669f94 | 190 | h = h - 180; |
kkizirian | 5:430f44669f94 | 191 | rads = h * PI / 180; |
kkizirian | 5:430f44669f94 | 192 | xEnd = floor(63 * sin(rads) + .5); |
kkizirian | 5:430f44669f94 | 193 | yEnd = floor(63 * cos(rads) + .5); |
kkizirian | 5:430f44669f94 | 194 | xEnd = 63 - xEnd; |
kkizirian | 5:430f44669f94 | 195 | yEnd = 63 - yEnd; |
kkizirian | 5:430f44669f94 | 196 | } else if (270 < h) { |
kkizirian | 5:430f44669f94 | 197 | h = h - 270; |
kkizirian | 5:430f44669f94 | 198 | rads = h * PI / 180; |
kkizirian | 5:430f44669f94 | 199 | xEnd = floor(63 * cos(rads) + .5); |
kkizirian | 5:430f44669f94 | 200 | yEnd = floor(63 * sin(rads) + .5); |
kkizirian | 5:430f44669f94 | 201 | xEnd = 63 - xEnd; |
kkizirian | 5:430f44669f94 | 202 | yEnd = 63 + yEnd; |
kkizirian | 5:430f44669f94 | 203 | } |
kkizirian | 2:c708e2027970 | 204 | uLCD.cls(); |
kkizirian | 5:430f44669f94 | 205 | uLCD.line(63, 63, xEnd, yEnd, WHITE); |
kkizirian | 5:430f44669f94 | 206 | wait(.1); |
kkizirian | 5:430f44669f94 | 207 | break; |
kkizirian | 5:430f44669f94 | 208 | case 2: |
kkizirian | 5:430f44669f94 | 209 | //uLCD.cls(); |
kkizirian | 7:e4d667beaf33 | 210 | uLCD.locate(0,2); |
kkizirian | 8:3266cc9eda81 | 211 | uLCD.printf(" "); |
kkizirian | 6:ceb6a5426add | 212 | uLCD.locate(0,3); |
kkizirian | 5:430f44669f94 | 213 | uLCD.printf("Time to next class"); |
kkizirian | 6:ceb6a5426add | 214 | uLCD.locate(0, 4); |
kkizirian | 5:430f44669f94 | 215 | uLCD.printf("%02dhrs %02dminutes", hoursToNextClass, minutesToNextClass); |
kkizirian | 6:ceb6a5426add | 216 | uLCD.locate(0, 5); |
kkizirian | 5:430f44669f94 | 217 | uLCD.printf("%02dseconds", secondsToNextClass); |
kkizirian | 6:ceb6a5426add | 218 | uLCD.locate(0, 0); |
kkizirian | 7:e4d667beaf33 | 219 | uLCD.printf("Next class "); |
kkizirian | 5:430f44669f94 | 220 | if (nextClass != currClass) { |
kkizirian | 6:ceb6a5426add | 221 | uLCD.locate(0, 1); |
kkizirian | 5:430f44669f94 | 222 | uLCD.printf(" "); |
kkizirian | 6:ceb6a5426add | 223 | uLCD.locate(0, 1); |
kkizirian | 5:430f44669f94 | 224 | uLCD.printf("%s", courseVec[nextClass].getDisplayString()); |
kkizirian | 5:430f44669f94 | 225 | } |
kkizirian | 5:430f44669f94 | 226 | currClass = nextClass; |
kkizirian | 5:430f44669f94 | 227 | break; |
kkizirian | 5:430f44669f94 | 228 | } |
kkizirian | 0:ce60014510e5 | 229 | } |
kkizirian | 0:ce60014510e5 | 230 | } |
kkizirian | 0:ce60014510e5 | 231 | } |
kkizirian | 0:ce60014510e5 | 232 | |
kkizirian | 2:c708e2027970 | 233 | void readClassFile(vector<Course>& cVec) |
kkizirian | 2:c708e2027970 | 234 | { |
kkizirian | 0:ce60014510e5 | 235 | cVec.clear(); |
kkizirian | 0:ce60014510e5 | 236 | |
kkizirian | 0:ce60014510e5 | 237 | FILE *readFp = fopen("/sd/classdir/classes.txt", "r"); |
kkizirian | 0:ce60014510e5 | 238 | char line[15]; |
kkizirian | 0:ce60014510e5 | 239 | char buildingBuf[4]; |
kkizirian | 0:ce60014510e5 | 240 | char hourBuf[3]; |
kkizirian | 0:ce60014510e5 | 241 | int hour; |
kkizirian | 0:ce60014510e5 | 242 | char minuteBuf[3]; |
kkizirian | 0:ce60014510e5 | 243 | int minute; |
kkizirian | 0:ce60014510e5 | 244 | char ampmBuf[3]; |
kkizirian | 0:ce60014510e5 | 245 | uLCD.cls(); |
kkizirian | 0:ce60014510e5 | 246 | uLCD.locate(0, 1); |
kkizirian | 0:ce60014510e5 | 247 | uLCD.printf("Reading class file..."); |
kkizirian | 2:c708e2027970 | 248 | |
kkizirian | 0:ce60014510e5 | 249 | memset(buildingBuf, 0, sizeof(buildingBuf)); |
kkizirian | 0:ce60014510e5 | 250 | memset(hourBuf, 0, sizeof(hourBuf)); |
kkizirian | 0:ce60014510e5 | 251 | memset(minuteBuf, 0, sizeof(minuteBuf)); |
kkizirian | 0:ce60014510e5 | 252 | memset(ampmBuf, 0, sizeof(ampmBuf)); |
kkizirian | 0:ce60014510e5 | 253 | memset(line, 0, sizeof(line)); |
kkizirian | 0:ce60014510e5 | 254 | |
kkizirian | 0:ce60014510e5 | 255 | if (readFp == NULL) |
kkizirian | 0:ce60014510e5 | 256 | return; |
kkizirian | 0:ce60014510e5 | 257 | else { |
kkizirian | 0:ce60014510e5 | 258 | while (!feof(readFp)) { |
kkizirian | 0:ce60014510e5 | 259 | fgets(line, 15, readFp); |
kkizirian | 0:ce60014510e5 | 260 | if(line[8] == NULL) |
kkizirian | 0:ce60014510e5 | 261 | continue; |
kkizirian | 0:ce60014510e5 | 262 | memcpy(buildingBuf, line, 3); |
kkizirian | 0:ce60014510e5 | 263 | memcpy(hourBuf, &line[4], 2); |
kkizirian | 0:ce60014510e5 | 264 | memcpy(minuteBuf, &line[7], 2); |
kkizirian | 0:ce60014510e5 | 265 | memcpy(ampmBuf, &line[10], 2); |
kkizirian | 0:ce60014510e5 | 266 | |
kkizirian | 0:ce60014510e5 | 267 | string building = buildingBuf; |
kkizirian | 0:ce60014510e5 | 268 | hour = atoi(hourBuf); |
kkizirian | 0:ce60014510e5 | 269 | minute = atoi(minuteBuf); |
kkizirian | 0:ce60014510e5 | 270 | string ampm = ampmBuf; |
kkizirian | 0:ce60014510e5 | 271 | |
kkizirian | 0:ce60014510e5 | 272 | Course temp(building, hour, minute, ampm); |
kkizirian | 0:ce60014510e5 | 273 | cVec.push_back(temp); |
kkizirian | 0:ce60014510e5 | 274 | } |
kkizirian | 0:ce60014510e5 | 275 | } |
kkizirian | 0:ce60014510e5 | 276 | fclose(readFp); |
kkizirian | 0:ce60014510e5 | 277 | return; |
kkizirian | 2:c708e2027970 | 278 | } |
kkizirian | 2:c708e2027970 | 279 | |
kkizirian | 2:c708e2027970 | 280 | void displayCourseVec() |
kkizirian | 2:c708e2027970 | 281 | { |
kkizirian | 2:c708e2027970 | 282 | if (courseVec.size() == 0) { |
kkizirian | 2:c708e2027970 | 283 | uLCD.cls(); |
kkizirian | 2:c708e2027970 | 284 | uLCD.locate(0,0); |
kkizirian | 2:c708e2027970 | 285 | uLCD.printf("No classes input!"); |
kkizirian | 2:c708e2027970 | 286 | uLCD.locate(0,1); |
kkizirian | 2:c708e2027970 | 287 | } else { |
kkizirian | 2:c708e2027970 | 288 | uLCD.cls(); |
kkizirian | 2:c708e2027970 | 289 | uLCD.locate(0,1); |
kkizirian | 2:c708e2027970 | 290 | for (int i = 0; i < courseVec.size(); i++) { |
kkizirian | 2:c708e2027970 | 291 | uLCD.locate(0, i); |
kkizirian | 2:c708e2027970 | 292 | uLCD.printf("%s", courseVec[i].getDisplayString()); |
kkizirian | 2:c708e2027970 | 293 | } |
kkizirian | 2:c708e2027970 | 294 | } |
kkizirian | 3:0ca91f8afec5 | 295 | } |
kkizirian | 3:0ca91f8afec5 | 296 | |
kkizirian | 3:0ca91f8afec5 | 297 | float calculateHeading(float mx, float my) |
kkizirian | 3:0ca91f8afec5 | 298 | { |
kkizirian | 3:0ca91f8afec5 | 299 | float heading = 0.0; |
kkizirian | 3:0ca91f8afec5 | 300 | if (my == 0.0) |
kkizirian | 3:0ca91f8afec5 | 301 | heading = (mx < 0.0) ? 180.0 : 0.0; |
kkizirian | 3:0ca91f8afec5 | 302 | else |
kkizirian | 3:0ca91f8afec5 | 303 | heading = atan2(mx, my)*360.0/(2.0*PI); |
kkizirian | 3:0ca91f8afec5 | 304 | //pc.printf("heading atan=%f \n\r",heading); |
kkizirian | 3:0ca91f8afec5 | 305 | heading -= DECLINATION; //correct for geo location |
kkizirian | 3:0ca91f8afec5 | 306 | if(heading>180.0) heading = heading - 360.0; |
kkizirian | 3:0ca91f8afec5 | 307 | else if(heading<-180.0) heading = 360.0 + heading; |
kkizirian | 3:0ca91f8afec5 | 308 | else if(heading<0.0) heading = 360.0 + heading; |
kkizirian | 3:0ca91f8afec5 | 309 | |
kkizirian | 3:0ca91f8afec5 | 310 | // Convert everything from radians to degrees: |
kkizirian | 3:0ca91f8afec5 | 311 | //heading *= 180.0 / PI; |
kkizirian | 3:0ca91f8afec5 | 312 | |
kkizirian | 3:0ca91f8afec5 | 313 | //pc.printf("Magnetic Heading: %f degress\n\r",heading); |
kkizirian | 3:0ca91f8afec5 | 314 | return heading; |
kkizirian | 3:0ca91f8afec5 | 315 | } |
kkizirian | 3:0ca91f8afec5 | 316 | |
kkizirian | 3:0ca91f8afec5 | 317 | float computeAngleToDestination(float diffLat, float diffLong) |
kkizirian | 3:0ca91f8afec5 | 318 | { |
kkizirian | 3:0ca91f8afec5 | 319 | float angle = 0.0; |
kkizirian | 3:0ca91f8afec5 | 320 | |
kkizirian | 3:0ca91f8afec5 | 321 | if (diffLat > 0) { |
kkizirian | 3:0ca91f8afec5 | 322 | if (diffLong > 0) { |
kkizirian | 3:0ca91f8afec5 | 323 | // in quadrant 1 |
kkizirian | 3:0ca91f8afec5 | 324 | angle = 180*atan2(diffLat,diffLong)/PI; |
kkizirian | 3:0ca91f8afec5 | 325 | } else { |
kkizirian | 3:0ca91f8afec5 | 326 | // in quadrant 2 |
kkizirian | 3:0ca91f8afec5 | 327 | angle = 180*atan2(diffLat,-1 * diffLong)/PI; |
kkizirian | 3:0ca91f8afec5 | 328 | angle = 180 - angle; |
kkizirian | 3:0ca91f8afec5 | 329 | } |
kkizirian | 3:0ca91f8afec5 | 330 | } else { |
kkizirian | 3:0ca91f8afec5 | 331 | if (diffLong > 0) { |
kkizirian | 3:0ca91f8afec5 | 332 | // in quadrant 4 |
kkizirian | 3:0ca91f8afec5 | 333 | angle = 180*atan2(-1*diffLat, diffLong)/PI; |
kkizirian | 3:0ca91f8afec5 | 334 | angle = 360 - angle; |
kkizirian | 3:0ca91f8afec5 | 335 | } else { |
kkizirian | 3:0ca91f8afec5 | 336 | // in quadrant 3 |
kkizirian | 3:0ca91f8afec5 | 337 | angle = 180*atan2(-1*diffLat, -1*diffLong)/PI; |
kkizirian | 3:0ca91f8afec5 | 338 | angle = 180 + angle; |
kkizirian | 3:0ca91f8afec5 | 339 | } |
kkizirian | 3:0ca91f8afec5 | 340 | } |
kkizirian | 3:0ca91f8afec5 | 341 | |
kkizirian | 3:0ca91f8afec5 | 342 | //pc.printf("Angle to Destination: %f degress\n\r",angle); |
kkizirian | 3:0ca91f8afec5 | 343 | return angle; |
kkizirian | 5:430f44669f94 | 344 | } |
kkizirian | 5:430f44669f94 | 345 | |
kkizirian | 5:430f44669f94 | 346 | int getNextClass(vector<Course>& cVec) |
kkizirian | 5:430f44669f94 | 347 | { |
kkizirian | 5:430f44669f94 | 348 | int numIterations = 0; |
kkizirian | 5:430f44669f94 | 349 | for (int i = 0; i < cVec.size(); i++) { |
kkizirian | 5:430f44669f94 | 350 | numIterations++; |
kkizirian | 5:430f44669f94 | 351 | pc.printf("Iteration : %i\ncurrHour: %i\ncourseHour: %i\n", numIterations, currHour, cVec[i].getHour_inMilitaryTime()); |
kkizirian | 5:430f44669f94 | 352 | if (courseVec[i].getHour_inMilitaryTime() < currHour) |
kkizirian | 5:430f44669f94 | 353 | continue; |
kkizirian | 5:430f44669f94 | 354 | else if (courseVec[i].getHour_inMilitaryTime() > currHour) |
kkizirian | 5:430f44669f94 | 355 | return i; |
kkizirian | 5:430f44669f94 | 356 | else if (courseVec[i].getHour_inMilitaryTime() == currHour) { |
kkizirian | 5:430f44669f94 | 357 | if (courseVec[i].getMinute() <= currMinute) |
kkizirian | 5:430f44669f94 | 358 | continue; |
kkizirian | 5:430f44669f94 | 359 | else if (courseVec[i].getMinute() > currMinute) { |
kkizirian | 5:430f44669f94 | 360 | return i; |
kkizirian | 5:430f44669f94 | 361 | } |
kkizirian | 5:430f44669f94 | 362 | } |
kkizirian | 5:430f44669f94 | 363 | if (numIterations == cVec.size()) |
kkizirian | 5:430f44669f94 | 364 | return 0; |
kkizirian | 5:430f44669f94 | 365 | } |
kkizirian | 5:430f44669f94 | 366 | return 0; |
kkizirian | 5:430f44669f94 | 367 | } |
kkizirian | 5:430f44669f94 | 368 | |
kkizirian | 5:430f44669f94 | 369 | void timeToNextClass() |
kkizirian | 6:ceb6a5426add | 370 | { |
kkizirian | 5:430f44669f94 | 371 | hoursToNextClass = courseVec[nextClass].getHour_inMilitaryTime() - currHour; |
kkizirian | 5:430f44669f94 | 372 | if (hoursToNextClass < 0) |
kkizirian | 5:430f44669f94 | 373 | hoursToNextClass += 24; |
kkizirian | 5:430f44669f94 | 374 | |
kkizirian | 5:430f44669f94 | 375 | minutesToNextClass = courseVec[nextClass].getMinute() - currMinute; |
kkizirian | 5:430f44669f94 | 376 | if (minutesToNextClass < 0) { |
kkizirian | 5:430f44669f94 | 377 | hoursToNextClass--; |
kkizirian | 5:430f44669f94 | 378 | minutesToNextClass += 60; |
kkizirian | 5:430f44669f94 | 379 | } |
kkizirian | 6:ceb6a5426add | 380 | |
kkizirian | 5:430f44669f94 | 381 | secondsToNextClass = 60 - currSecond; |
kkizirian | 5:430f44669f94 | 382 | minutesToNextClass--; |
kkizirian | 6:ceb6a5426add | 383 | |
kkizirian | 5:430f44669f94 | 384 | if (minutesToNextClass < 0) { |
kkizirian | 5:430f44669f94 | 385 | hoursToNextClass--; |
kkizirian | 5:430f44669f94 | 386 | minutesToNextClass += 60; |
kkizirian | 5:430f44669f94 | 387 | } |
kkizirian | 6:ceb6a5426add | 388 | |
kkizirian | 5:430f44669f94 | 389 | if (hoursToNextClass < 0) |
kkizirian | 5:430f44669f94 | 390 | hoursToNextClass += 24; |
kkizirian | 6:ceb6a5426add | 391 | } |
kkizirian | 6:ceb6a5426add | 392 | |
kkizirian | 6:ceb6a5426add | 393 | // convert from DDMM.mmmm to decimal |
kkizirian | 9:5b871fb9f554 | 394 | float latToDecimal(float latitudeInDDMM, char northOrSouth) |
kkizirian | 6:ceb6a5426add | 395 | { |
kkizirian | 6:ceb6a5426add | 396 | float latInDec = 0.0; |
kkizirian | 6:ceb6a5426add | 397 | float temp = 0.0; |
kkizirian | 6:ceb6a5426add | 398 | latInDec = latitudeInDDMM / 100; |
kkizirian | 6:ceb6a5426add | 399 | temp = latInDec - floor(latInDec); |
kkizirian | 6:ceb6a5426add | 400 | temp = temp / .6; |
kkizirian | 6:ceb6a5426add | 401 | latInDec = floor(latInDec) + temp; |
kkizirian | 9:5b871fb9f554 | 402 | if (eastOrWest == 'S') |
kkizirian | 9:5b871fb9f554 | 403 | return -1 * latInDec; |
kkizirian | 6:ceb6a5426add | 404 | return latInDec; |
kkizirian | 6:ceb6a5426add | 405 | } |
kkizirian | 6:ceb6a5426add | 406 | |
kkizirian | 9:5b871fb9f554 | 407 | float longToDecimal(float longitudeInDDMM, char eastOrWest) |
kkizirian | 6:ceb6a5426add | 408 | { |
kkizirian | 6:ceb6a5426add | 409 | float longInDec = 0.0; |
kkizirian | 6:ceb6a5426add | 410 | float temp = 0.0; |
kkizirian | 6:ceb6a5426add | 411 | longInDec = longitudeInDDMM / 100; |
kkizirian | 6:ceb6a5426add | 412 | temp = longInDec - floor(longInDec); |
kkizirian | 6:ceb6a5426add | 413 | temp = temp / .6; |
kkizirian | 6:ceb6a5426add | 414 | longInDec = floor(longInDec) + temp; |
kkizirian | 9:5b871fb9f554 | 415 | if (eastOrWest == 'E') |
kkizirian | 9:5b871fb9f554 | 416 | return longInDec; |
kkizirian | 6:ceb6a5426add | 417 | return -1 * longInDec; |
kkizirian | 0:ce60014510e5 | 418 | } |