Posted By

jatkins on 10/13/16


Tagged

parser GPS arduino telemetry


Versions (?)

Telemetry Parser


 / Published in: Processing
 

gps

  1. char incomingByte, i, curCharIndex, curWordIndex, curChar, latitude[9], longitude[9];
  2. String lineLabel, usefulNMEA, curWord;
  3. int takingInput, sign;
  4. const int TAKING_INPUT_FALSE = 0, TAKING_INPUT_LINELABEL = 1, TAKING_INPUT_USEFULNMEA = 2;
  5.  
  6. void setup() {
  7. Serial.begin(9600);
  8. }
  9.  
  10. void loop() {
  11. if (Serial.available()) {
  12. incomingByte = Serial.read();
  13. if (takingInput == TAKING_INPUT_USEFULNMEA) {
  14. usefulNMEA.concat(incomingByte);
  15. curCharIndex++;
  16. if (curCharIndex == 67) {
  17. takingInput = TAKING_INPUT_FALSE;
  18. curCharIndex = 0;
  19. curWordIndex = 0;
  20. curWord = "";
  21.  
  22. /*
  23.   * IMPORTANT
  24.   *
  25.   * The parameters passed to dtostrf may not work in other areas;
  26.   * I based them on the NMEA data I got at home
  27.   */
  28. while (curChar = usefulNMEA[curCharIndex]) {
  29. if (curChar == ',') {
  30. switch (curWordIndex) {
  31. case 0: // time
  32. Serial.println("Time: " + curWord.substring(0, 2) + ":" + curWord.substring(2, 4) + ":" + curWord.substring(4));
  33. break;
  34. case 1: // latitude
  35. sign = usefulNMEA[curCharIndex + 1] == 'S' ? -1 : 1;
  36. dtostrf(sign * (curWord.substring(0, 2).toFloat() + curWord.substring(2).toFloat() / 60), 9, 5, latitude);
  37. Serial.print("Latitude: ");
  38. Serial.println(latitude);
  39. break;
  40. case 2: // north/south
  41. break;
  42. case 3: // longitude
  43. sign = usefulNMEA[curCharIndex + 1] == 'W' ? -1 : 1;
  44. dtostrf(sign * (curWord.substring(0, 3).toFloat() + curWord.substring(3).toFloat() / 60), 9, 6, longitude);
  45. Serial.print("Longitude: ");
  46. Serial.println(longitude);
  47. break;
  48. case 4: // east/west
  49. break;
  50. case 5:
  51. break;
  52. case 6: // number of satellites
  53. Serial.println("# satellites: " + curWord);
  54. break;
  55. case 7:
  56. break;
  57. case 8: // altitude
  58. Serial.println("Altitude: " + curWord + " " + usefulNMEA[curCharIndex + 1]);
  59. break;
  60. case 9: // altitude units
  61. break;
  62. case 10: // height above WGS84
  63. Serial.println("Height above WGS84: " + curWord + " " + usefulNMEA[curCharIndex + 1]);
  64. break;
  65. case 11:
  66. break;
  67. case 12:
  68. break;
  69. case 13: // checksum, but this is ignored because the comma doesn't get checked as it's the last one
  70. break;
  71. }
  72. curWord = "";
  73. curWordIndex++;
  74. } else {
  75. curWord.concat(curChar);
  76. }
  77. curCharIndex++;
  78. }
  79. Serial.print("Checksum: ");
  80. Serial.println(curWord);
  81. Serial.println();
  82. }
  83. } else if (takingInput == TAKING_INPUT_LINELABEL) {
  84. lineLabel.concat(incomingByte);
  85. curCharIndex++;
  86. if (curCharIndex == 6) {
  87. if (lineLabel[2] == 'G' && lineLabel[3] == 'G' && lineLabel[4] == 'A' && lineLabel[5] == ',') {
  88. takingInput = TAKING_INPUT_USEFULNMEA;
  89. usefulNMEA = "";
  90. } else {
  91. takingInput = TAKING_INPUT_FALSE;
  92. }
  93. curCharIndex = 0;
  94. lineLabel = "";
  95. }
  96. } else {
  97. if (incomingByte == '$') {
  98. takingInput = TAKING_INPUT_LINELABEL;
  99. curCharIndex = 0;
  100. }
  101. }
  102. }
  103. }

Report this snippet  

You need to login to post a comment.