Advertisement
Guest User

Untitled

a guest
Mar 24th, 2014
3,803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 KB | None | 0 0
  1. /*
  2.   6-12-12
  3.   Aaron Weiss
  4.   SparkFun Electronics, Beerware
  5.  
  6.   Example GPS Parser based off of arduiniana.org TinyGPS examples.
  7.  
  8.   Parses NMEA sentences from an EM406 running at 4800bps into readable
  9.   values for latitude, longitude, elevation, date, time, course, and
  10.   speed. Use 115200 baud for your serial port baud rate
  11.  
  12.   For the SparkFun GPS Shield. Make sure the switch is set to DLINE.
  13.  
  14.   Once you get your longitude and latitude you can paste your
  15.   coordinates from the terminal window into Google Maps. Here is the
  16.   link for SparkFun's location.  
  17.   http://maps.google.com/maps?q=40.06477,+-105.20997
  18.  
  19.   Uses the NewSoftSerial library for serial communication with your GPS,
  20.   so connect your GPS TX and RX pin to any digital pin on the Arduino,
  21.   just be sure to define which pins you are using on the Arduino to
  22.   communicate with the GPS module.
  23.  
  24.   REVISIONS:
  25.   1-17-11
  26.     changed values to RXPIN = 2 and TXPIN = to correspond with
  27.     hardware v14+. Hardware v13 used RXPIN = 3 and TXPIN = 2.
  28.  
  29. */
  30.  
  31. // In order for this sketch to work, you will need to download
  32. // TinyGPS and NewSoftSerial library from arduiniana.org and put them
  33. // into the libraries folder in your ardiuno directory.
  34. #include <SoftwareSerial.h>
  35. #include <TinyGPS.h>
  36.  
  37. // Define which pins you will use on the Arduino to communicate with your
  38. // GPS. In this case, the GPS module's TX pin will connect to the
  39. // Arduino's RXPIN which is pin 3.
  40. #define RXPIN 3
  41. #define TXPIN 2
  42.  
  43. // This is the serial rate for your terminal program. It must be this
  44. // fast because we need to print everything before a new sentence
  45. // comes in. If you slow it down, the messages might not be valid and
  46. // you will likely get checksum errors.
  47. // Set this value equal to the baud rate of your terminal program
  48. #define TERMBAUD  115200
  49.  
  50. // Set this value equal to the baud rate of your GPS
  51. #define GPSBAUD  4800
  52.  
  53. // Create an instance of the TinyGPS object
  54. TinyGPS gps;
  55. // Initialize the NewSoftSerial library to the pins you defined above
  56. SoftwareSerial uart_gps(RXPIN, TXPIN);
  57.  
  58. // This is where you declare prototypes for the functions that will be
  59. // using the TinyGPS library.
  60. void getgps(TinyGPS &gps);
  61.  
  62. // In the setup function, you need to initialize two serial ports; the
  63. // standard hardware serial port (Serial()) to communicate with your
  64. // terminal program an another serial port (NewSoftSerial()) for your
  65. // GPS.
  66. void setup()
  67. {
  68.  
  69.   // Sets baud rate of your terminal program
  70.   Serial.begin(TERMBAUD);
  71.   // Sets baud rate of your GPS
  72.   uart_gps.begin(GPSBAUD);
  73.  
  74.   Serial.println("");
  75.   Serial.println("GPS Shield QuickStart Example Sketch v12");
  76.   Serial.println("       ...waiting for lock...           ");
  77.   Serial.println("");
  78. }
  79.  
  80. // This is the main loop of the code. All it does is check for data on
  81. // the RX pin of the ardiuno, makes sure the data is valid NMEA sentences,
  82. // then jumps to the getgps() function.
  83. void loop()
  84. {
  85.   while(uart_gps.available())     // While there is data on the RX pin...
  86.   {
  87.       int c = uart_gps.read();    // load the data into a variable...
  88.       if(gps.encode(c))      // if there is a new valid sentence...
  89.       {
  90.         getgps(gps);         // then grab the data.
  91.       }
  92.   }
  93. }
  94.  
  95. // The getgps function will get and print the values we want.
  96. void getgps(TinyGPS &gps)
  97. {
  98.   // To get all of the data into varialbes that you can use in your code,
  99.   // all you need to do is define variables and query the object for the
  100.   // data. To see the complete list of functions see keywords.txt file in
  101.   // the TinyGPS and NewSoftSerial libs.
  102.  
  103.   // Define the variables that will be used
  104.   float latitude, longitude;
  105.   // Then call this function
  106.   gps.f_get_position(&latitude, &longitude);
  107.   // You can now print variables latitude and longitude
  108.   Serial.print("Lat/Long: ");
  109.   Serial.print(latitude,5);
  110.   Serial.print(", ");
  111.   Serial.println(longitude,5);
  112.  
  113.   // Same goes for date and time
  114.   int year;
  115.   byte month, day, hour, minute, second, hundredths;
  116.   gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
  117.   // Print data and time
  118.   Serial.print("Date: "); Serial.print(month, DEC); Serial.print("/");
  119.   Serial.print(day, DEC); Serial.print("/"); Serial.print(year);
  120.   Serial.print("  Time: "); Serial.print(hour, DEC); Serial.print(":");
  121.   Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC);
  122.   Serial.print("."); Serial.println(hundredths, DEC);
  123.   //Since month, day, hour, minute, second, and hundr
  124.  
  125.   // Here you can print the altitude and course values directly since
  126.   // there is only one value for the function
  127.   Serial.print("Altitude (meters): "); Serial.println(gps.f_altitude());  
  128.   // Same goes for course
  129.   Serial.print("Course (degrees): "); Serial.println(gps.f_course());
  130.   // And same goes for speed
  131.   Serial.print("Speed(kmph): "); Serial.println(gps.f_speed_kmph());
  132.   //Serial.println();
  133.  
  134.   // Here you can print statistics on the sentences.
  135.   unsigned long chars;
  136.   unsigned short sentences, failed_checksum;
  137.   gps.stats(&chars, &sentences, &failed_checksum);
  138.   //Serial.print("Failed Checksums: ");Serial.print(failed_checksum);
  139.   //Serial.println(); Serial.println();
  140.  
  141.   // Here you can print the number of satellites in view
  142.   Serial.print("Satellites: ");
  143.   Serial.println(gps.satellites());
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement