Posted By

jatkins on 10/13/16


Tagged

parser GPS arduino telemetry


Versions (?)

Telemetry Parser


 / Published in: Processing
 

gps

  1. /* NTX2 Radio Test Part 2
  2.  
  3.   Transmits data via RTTY with a checksum.
  4.  
  5.   Created 2012 by M0UPU as part of a UKHAS Guide on linking NTX2 Modules to Arduino.
  6.   RTTY code from Rob Harrison Icarus Project.
  7.   http://ukhas.org.uk
  8. */
  9.  
  10. #define RADIOPIN 13
  11.  
  12. #include <string.h>
  13. #include <util/crc16.h>
  14.  
  15. char incomingByte, curChar, latitude[9], longitude[9];
  16. String lineLabel, usefulNMEA, curWord;
  17. int takingInput, sign, curCharIndex, i;
  18. const int TAKING_INPUT_FALSE = 0, TAKING_INPUT_LINELABEL = 1, TAKING_INPUT_USEFULNMEA = 2;
  19. char datastring[80];
  20.  
  21. void setup() {
  22. pinMode(RADIOPIN,OUTPUT);
  23. Serial.begin(9600);
  24. }
  25.  
  26. /*
  27.  
  28.   sprintf(datastring,"RTTY TEST BEACON RTTY TEST BEACON"); // Puts the text in the datastring
  29.   unsigned int CHECKSUM = gps_CRC16_checksum(datastring); // Calculates the checksum for this datastring
  30.   char checksum_str[6];
  31.   sprintf(checksum_str, "*%04X\n", CHECKSUM);
  32.   strcat(datastring,checksum_str);
  33.  
  34.   rtty_txstring (datastring);
  35.   delay(2000);
  36. }*/
  37.  
  38.  
  39. void rtty_txstring (char * string)
  40. {
  41.  
  42. /* Simple function to sent a char at a time to
  43.   ** rtty_txbyte function.
  44.   ** NB Each char is one byte (8 Bits)
  45.   */
  46.  
  47. char c;
  48.  
  49. c = *string++;
  50.  
  51. while ( c != '\0')
  52. {
  53. rtty_txbyte (c);
  54. c = *string++;
  55. }
  56. }
  57.  
  58.  
  59. void rtty_txbyte (char c)
  60. {
  61. /* Simple function to sent each bit of a char to
  62.   ** rtty_txbit function.
  63.   ** NB The bits are sent Least Significant Bit first
  64.   **
  65.   ** All chars should be preceded with a 0 and
  66.   ** proceded with a 1. 0 = Start bit; 1 = Stop bit
  67.   **
  68.   */
  69.  
  70. int i;
  71.  
  72. rtty_txbit (0); // Start bit
  73.  
  74. // Send bits for for char LSB first
  75.  
  76. for (i=0;i<8;i++) // Change this here 7 or 8 for ASCII-7 / ASCII-8
  77. {
  78. if (c & 1) rtty_txbit(1);
  79.  
  80. else rtty_txbit(0);
  81.  
  82. c = c >> 1;
  83.  
  84. }
  85.  
  86. rtty_txbit (1); // Stop bit
  87. rtty_txbit (1); // Stop bit
  88. }
  89.  
  90. void rtty_txbit (int bit)
  91. {
  92. if (bit)
  93. {
  94. // high
  95. digitalWrite(RADIOPIN, HIGH);
  96. }
  97. else
  98. {
  99. // low
  100. digitalWrite(RADIOPIN, LOW);
  101.  
  102. }
  103.  
  104. delayMicroseconds(3370); // 300 baud
  105. //delayMicroseconds(10000); // For 50 Baud uncomment this and the line below.
  106. //delayMicroseconds(10150); // You can't do 20150 it just doesn't work as the
  107. // largest value that will produce an accurate delay is 16383
  108. // See : http://arduino.cc/en/Reference/DelayMicroseconds
  109.  
  110. }
  111.  
  112. uint16_t gps_CRC16_checksum (char *string)
  113. {
  114. size_t i;
  115. uint16_t crc;
  116. uint8_t c;
  117.  
  118. crc = 0xFFFF;
  119.  
  120. // Calculate checksum ignoring the first two $s
  121. for (i = 2; i < strlen(string); i++)
  122. {
  123. c = string[i];
  124. crc = _crc_xmodem_update (crc, c);
  125. }
  126.  
  127. return crc;
  128. }
  129.  
  130. void loop() {
  131. if (Serial.available()) {
  132. incomingByte = Serial.read();
  133. if (takingInput == TAKING_INPUT_USEFULNMEA) {
  134. usefulNMEA.concat(incomingByte);
  135. curCharIndex++;
  136. if (incomingByte == '\n') {
  137. takingInput = TAKING_INPUT_FALSE;
  138. curWord = "";
  139. int nmeaLen = usefulNMEA.length();
  140.  
  141. /*
  142.   * IMPORTANT
  143.   *
  144.   * The parameters passed to dtostrf may not work in other areas;
  145.   * I based them on the NMEA data I got at home
  146.   */
  147.  
  148. String rttyStr = "";
  149.  
  150. int curWordIndex = 0;
  151. int curCharIndex = 0;
  152. while (curCharIndex < nmeaLen) {
  153. curChar = usefulNMEA[curCharIndex];
  154. if (curChar == ',') {
  155. switch (curWordIndex) {
  156. case 0: // time
  157. rttyStr += curWord.substring(0, 2) + ":" + curWord.substring(2, 4) + ":" + curWord.substring(4);
  158. break;
  159. case 1: // latitude
  160. sign = usefulNMEA[curCharIndex + 1] == 'S' ? -1 : 1;
  161. dtostrf(sign * (curWord.substring(0, 2).toFloat() + curWord.substring(2).toFloat() / 60), 9, 5, latitude);
  162. rttyStr += ",";
  163. rttyStr += latitude;
  164. break;
  165. case 2: // north/south
  166. break;
  167. case 3: // longitude
  168. sign = usefulNMEA[curCharIndex + 1] == 'W' ? -1 : 1;
  169. dtostrf(sign * (curWord.substring(0, 3).toFloat() + curWord.substring(3).toFloat() / 60), 9, 6, longitude);
  170. rttyStr += ",";
  171. rttyStr += longitude;
  172. break;
  173. case 4: // east/west
  174. break;
  175. case 5:
  176. break;
  177. case 6: // number of satellites
  178. rttyStr += "," + curWord;
  179. break;
  180. case 7:
  181. break;
  182. case 8: // altitude
  183. rttyStr += "," + curWord + " " + usefulNMEA[curCharIndex + 1];
  184. break;
  185. case 9: // altitude units
  186. break;
  187. case 10: // height above WGS84
  188. rttyStr += "," + curWord + " " + usefulNMEA[curCharIndex + 1];
  189. break;
  190. case 11:
  191. break;
  192. case 12:
  193. break;
  194. case 13: // checksum, but this is ignored because the comma doesn't get checked as it's the last one
  195. break;
  196. }
  197. curWord = "";
  198. curWordIndex++;
  199. } else {
  200. curWord.concat(curChar);
  201. }
  202. curCharIndex++;
  203. }
  204. rttyStr += "," + curWord;
  205. rtty_txstring(rttyStr.c_str());
  206. }
  207. } else if (takingInput == TAKING_INPUT_LINELABEL) {
  208. lineLabel.concat(incomingByte);
  209. curCharIndex++;
  210. if (curCharIndex == 6) {
  211. if (lineLabel[2] == 'G' && lineLabel[3] == 'G' && lineLabel[4] == 'A' && lineLabel[5] == ',') {
  212. takingInput = TAKING_INPUT_USEFULNMEA;
  213. usefulNMEA = "";
  214. } else {
  215. takingInput = TAKING_INPUT_FALSE;
  216. }
  217. curCharIndex = 0;
  218. lineLabel = "";
  219. }
  220. } else {
  221. if (incomingByte == '$') {
  222. takingInput = TAKING_INPUT_LINELABEL;
  223. curCharIndex = 0;
  224. }
  225. }
  226. }
  227. }

Report this snippet  

You need to login to post a comment.