/* * This pole interfaces as the . */ #include #include #include // We talk to the GPS Unit with 9600 serial SoftwareSerial mySerial(15, 14); Adafruit_GPS GPS(&mySerial); // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console // Set to 'true' if you want to debug and listen to the raw GPS sentences. #define GPSECHO false // this keeps track of whether we're using the interrupt // off by default! boolean usingInterrupt = false; void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy // the structure of the packet that we're sending to the other pole #define LAT 0 #define LON 1 #define SPEED 2 #define HEADING 3 #define ALTITUDE 4 #define HOURS 5 #define MINUTES 6 #define SECONDS 7 float GPSbuffer[8]; void setup() { // we use serial for debug information Serial.begin(115200); Serial.println(F("GPS Pole Serial Interface")); // // GPS Configuration // // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800 GPS.begin(9600); // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); // uncomment this line to turn on only the "minimum recommended" data //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since // the parser doesn't care about other sentences at this time // Set the update rate GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate // For the parsing code to work nicely and have time to sort thru the data, and // print it out we don't suggest using anything higher than 1 Hz // Request updates on antenna status, comment out to keep quiet GPS.sendCommand(PGCMD_ANTENNA); // the nice thing about this code is you can have a timer0 interrupt go off // every 1 millisecond, and read data from the GPS for you. that makes the // loop code a heck of a lot easier! useInterrupt(true); delay(500); // Ask for firmware version mySerial.println(PMTK_Q_RELEASE); } // Interrupt is called once a millisecond, looks for any new GPS data, and stores it SIGNAL(TIMER0_COMPA_vect) { char c = GPS.read(); // if you want to debug, this is a good time to do it! #ifdef UDR0 if (GPSECHO) if (c) UDR0 = c; // writing direct to UDR0 is much much faster than Serial.print // but only one character can be written at a time. #endif } void useInterrupt(boolean v) { if (v) { // Timer0 is already used for millis() - we'll just interrupt somewhere // in the middle and call the "Compare A" function above OCR0A = 0xAF; TIMSK0 |= _BV(OCIE0A); usingInterrupt = true; } else { // do not call the interrupt function COMPA anymore TIMSK0 &= ~_BV(OCIE0A); usingInterrupt = false; } } uint32_t timer = millis(); void loop() { // in case you are not using the interrupt above, you'll // need to 'hand query' the GPS, not suggested :( if (! usingInterrupt) { // read data from the GPS in the 'main loop' char c = GPS.read(); // if you want to debug, this is a good time to do it! if (GPSECHO) if (c) Serial.print(c); } // if a sentence is received, we can check the checksum, parse it... if (GPS.newNMEAreceived()) { // a tricky thing here is if we print the NMEA sentence, or data // we end up not listening and catching other sentences! // so be very wary if using OUTPUT_ALLDATA and trytng to print out data //Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false return; // we can fail to parse a sentence in which case we should just wait for another } // if millis() or timer wraps around, we'll just reset it if (timer > millis()) timer = millis(); // approximately every 2 seconds or so, print out the current stats and transmit them if (millis() - timer > 2000) { timer = millis(); // reset the timer if (GPS.fix) { // assign variables into buffer to get transmitted GPSbuffer[LAT] = GPS.latitudeDegrees; GPSbuffer[LON] = GPS.longitudeDegrees; GPSbuffer[SPEED] = GPS.speed; GPSbuffer[HEADING] = GPS.angle; GPSbuffer[ALTITUDE] = GPS.altitude; GPSbuffer[HOURS] = GPS.hour; GPSbuffer[MINUTES] = GPS.minute; GPSbuffer[SECONDS] = GPS.seconds; // inspect memory from c[0] to c[sizeof f - 1] Serial.write((byte)1); Serial.write((byte)2); Serial.write((byte)3); Serial.write((byte)4); unsigned char *c = reinterpret_cast(&GPSbuffer[LAT]); Serial.write(c, 4); c = reinterpret_cast(&GPSbuffer[LON]); Serial.write(c, 4); } else { // fake indoor data for testing GPSbuffer[LAT] = 42.362001; GPSbuffer[LON] = -71.097317; GPSbuffer[SPEED] = millis(); GPSbuffer[HEADING] = 45; GPSbuffer[ALTITUDE] = 4000; GPSbuffer[HOURS] = 12; GPSbuffer[MINUTES] = 30; GPSbuffer[SECONDS] = 30; } /* Send the Current GPS Coordinates along on the Serial Connection */ //unsigned char *c = reinterpret_cast(&GPSbuffer[LAT]); //Serial.write(c, 8); /* * long intdegrees; * intdegrees = long (GPSbuffer[LAT]*1000000L); Serial.write(intdegrees & 255); Serial.write((intdegrees >> 8) & 255); Serial.write((intdegrees >> 16) & 255); Serial.write((intdegrees >> 24) & 255);*/ /*intdegrees = long (GPSbuffer[LON]*1000000L); Serial.write(intdegrees & 255); Serial.write((intdegrees >> 8) & 255); Serial.write((intdegrees >> 16) & 255); Serial.write((intdegrees >> 24) & 255);*/ } // Check to see for Serial Input if (Serial.available()) { // store the input as a command char command = (char)Serial.read(); if (command == 'E') { Serial.println("This code will ERASE the data log stored in the FLASH - Permanently!"); Serial.print("Are you sure you want to do this? [Y/N]: "); while (!Serial.available()) { } if (Serial.read() == 'Y') { Serial.println("\nERASING! UNPLUG YOUR ARDUINO WITHIN 5 SECONDS IF YOU DIDNT MEAN TO!"); delay(5000); GPS.sendCommand(PMTK_LOCUS_ERASE_FLASH); Serial.println("Erased"); } } else if (command == 'S') { if (GPS.LOCUS_ReadStatus()) { Serial.print("\n\nLog #"); Serial.print(GPS.LOCUS_serial, DEC); if (GPS.LOCUS_type == LOCUS_OVERLAP) Serial.print(", Overlap, "); else if (GPS.LOCUS_type == LOCUS_FULLSTOP) Serial.print(", Full Stop, Logging"); if (GPS.LOCUS_mode & 0x1) Serial.print(" AlwaysLocate"); if (GPS.LOCUS_mode & 0x2) Serial.print(" FixOnly"); if (GPS.LOCUS_mode & 0x4) Serial.print(" Normal"); if (GPS.LOCUS_mode & 0x8) Serial.print(" Interval"); if (GPS.LOCUS_mode & 0x10) Serial.print(" Distance"); if (GPS.LOCUS_mode & 0x20) Serial.print(" Speed"); Serial.print(", Content "); Serial.print((int)GPS.LOCUS_config); Serial.print(", Interval "); Serial.print((int)GPS.LOCUS_interval); Serial.print(" sec, Distance "); Serial.print((int)GPS.LOCUS_distance); Serial.print(" m, Speed "); Serial.print((int)GPS.LOCUS_speed); Serial.print(" m/s, Status "); if (GPS.LOCUS_status) Serial.print("LOGGING, "); else Serial.print("OFF, "); Serial.print((int)GPS.LOCUS_records); Serial.print(" Records, "); Serial.print((int)GPS.LOCUS_percent); Serial.print("% Used "); } } else if (command == 'G') { Serial.print("\nTime: "); Serial.print(GPS.hour, DEC); Serial.print(':'); Serial.print(GPS.minute, DEC); Serial.print(':'); Serial.print(GPS.seconds, DEC); Serial.print('.'); Serial.println(GPS.milliseconds); Serial.print("Date: "); Serial.print(GPS.day, DEC); Serial.print('/'); Serial.print(GPS.month, DEC); Serial.print("/20"); Serial.println(GPS.year, DEC); Serial.print("Fix: "); Serial.print((int)GPS.fix); Serial.print(" quality: "); Serial.println((int)GPS.fixquality); if (GPS.fix) { // output debug information via serial Serial.print("Location: "); Serial.print(GPS.latitude, 4); Serial.print(GPS.lat); Serial.print(", "); Serial.print(GPS.longitude, 4); Serial.println(GPS.lon); Serial.print("Location (in degrees, works with Google Maps): "); Serial.print(GPS.latitudeDegrees, 4); Serial.print(", "); Serial.println(GPS.longitudeDegrees, 4); Serial.print("Speed (knots): "); Serial.println(GPS.speed); Serial.print("Angle: "); Serial.println(GPS.angle); Serial.print("Altitude: "); Serial.println(GPS.altitude); Serial.print("Satellites: "); Serial.println((int)GPS.satellites); } } } } // Loop