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