
First version (beta) of a GPS application for the QW GPS shield that does its own NMEA parsing. It sends out gps data in the TD_GEOLOCATION format, but replaces the first byte with the temperature. Note that some features of the NMEA library are untested.
Dependencies: QW_NMEA QW_Sensors mbed
main.cpp
00001 #include "mbed.h" 00002 #include "GPS.h" 00003 #include "LinearTempSensor.h" 00004 #define updateinterval 900 00005 00006 DigitalOut LED_0 (PB_6); 00007 DigitalOut LED_1 (PA_7); 00008 DigitalOut LED_2 (PA_6); 00009 DigitalOut LED_3 (PA_5); 00010 InterruptIn SW1(PB_10); 00011 InterruptIn SW2(PA_8); 00012 00013 Ticker hartbeat; 00014 Ticker position_update; 00015 00016 //Virtual serial port over USB 00017 Serial pc(USBTX, USBRX); 00018 Serial modem(PA_9, PA_10); 00019 GPS gps(PA_9, PA_10); 00020 00021 /*Temperature sensor */ 00022 LinearTempSensor sensor(PA_0, 10); 00023 signed char temperature; // Temperature in signed char. 00024 00025 00026 void sertmout(); 00027 bool modem_command_check_ok(char * command); 00028 void modem_setup(); 00029 00030 uint32_t txtimeout = 0; 00031 00032 bool ser_timeout = false; 00033 00034 00035 // Blinking LED Ticker 00036 void beat() 00037 { 00038 LED_0 = !LED_0; 00039 txtimeout++; 00040 } 00041 00042 void sw1interrupt() 00043 { 00044 txtimeout = updateinterval+1; // instantly send 00045 } 00046 00047 void sw2interrupt() 00048 { 00049 txtimeout = updateinterval+1; // instantly send 00050 } 00051 00052 int main() 00053 { 00054 wait(3); 00055 modem_setup(); 00056 float vOut = sensor.Sense(); 00057 pc.printf("\n\rMCP9700 reading: Vout: %.2f mV\n\r", vOut); 00058 LED_0 = 1; 00059 LED_1 = 1; 00060 LED_2 = 1; 00061 LED_3 = 1; 00062 hartbeat.attach(&beat, 1); 00063 SW2.fall(&sw1interrupt); 00064 SW1.fall(&sw2interrupt); 00065 while(!modem_command_check_ok("AT$GPS=1,16,0,65535,1,1\n")) pc.printf("Trying to start GPS...\r\n"); 00066 pc.printf("GPS started!\r\n"); 00067 while(1) { 00068 if(pc.readable()) { 00069 if (pc.getc() == 'a') 00070 modem_command_check_ok("AT$GSND\n"); 00071 } 00072 int result = gps.sample(); 00073 if(result != NO_LOCK) { 00074 if((result == RMC && gps.gprmc_status == 'A')) { 00075 if(txtimeout > updateinterval) { 00076 LED_3 = 0; 00077 hartbeat.detach(); 00078 sensor.Sense(); 00079 temperature = (signed char)rint(sensor.GetAverageTemp()); 00080 pc.printf("\r\nGPRMC Fix: latitude: %f, longitude: %f Temperature: %d\r\n",gps.get_dec_latitude(),gps.get_dec_longitude(),temperature); 00081 char sfcommand[128]; 00082 sprintf(sfcommand, "AT$SF=%02x%s,2,0\n", temperature & 0xff,gps.get_nmea_to_td()); // Remark & 0xff needed to limit the value to two bytes! 00083 modem_command_check_ok(sfcommand); 00084 txtimeout = 0; 00085 LED_3 = 1; 00086 hartbeat.attach(&beat, 1); 00087 } 00088 } else if(txtimeout > updateinterval+5) { // 5 seconds extra in case of no fix (why? -> we're only waiting for GPRMC messages and we may have just skipped one) 00089 LED_2 = 0; 00090 hartbeat.detach(); 00091 sensor.Sense(); 00092 temperature = (signed char)rint(sensor.GetAverageTemp()); 00093 pc.printf("\r\nNo GPRMC Fix! (sending 0xffff... as position data) Temperature: %d\r\n",temperature); 00094 char sfcommand[128]; 00095 sprintf(sfcommand, "AT$SF=%02x01010fffffffffffffffff,2,0\n", temperature & 0xff); // Remark & 0xff needed to limit the value to two bytes! 00096 modem_command_check_ok(sfcommand); 00097 txtimeout = 0; 00098 LED_2 = 1; 00099 hartbeat.attach(&beat, 1); 00100 } 00101 } else if(txtimeout > updateinterval+5) { // 5 seconds extra in case of no fix (why? -> we're only waiting for GPRMC messages and we may have just skipped one) 00102 LED_2 = 0; 00103 hartbeat.detach(); 00104 sensor.Sense(); 00105 temperature = (signed char)rint(sensor.GetAverageTemp()); 00106 pc.printf("\r\nNo GPRMC Fix! (sending 0xffff... as position data) Temperature: %d\r\n",temperature); 00107 char sfcommand[128]; 00108 sprintf(sfcommand, "AT$SF=%02x01010fffffffffffffffff,2,0\n", temperature & 0xff); // Remark & 0xff needed to limit the value to two bytes! 00109 modem_command_check_ok(sfcommand); 00110 txtimeout = 0; 00111 LED_2 = 1; 00112 hartbeat.attach(&beat, 1); 00113 } 00114 } 00115 } 00116 00117 void modem_setup() 00118 { 00119 /* Reset to factory defaults */ 00120 if(modem_command_check_ok("AT&F")) { 00121 pc.printf("Factory reset succesfull\r\n"); 00122 } else { 00123 pc.printf("Factory reset TD120x failed\r\n"); 00124 } 00125 /* Disable local echo */ 00126 modem.printf("ATE0\n"); 00127 if(modem_command_check_ok("ATE0")) { 00128 pc.printf("Local echo disabled\r\n"); 00129 } 00130 /* Write to mem */ 00131 if(modem_command_check_ok("AT&W")) { 00132 pc.printf("Settings saved!\r\n"); 00133 } 00134 } 00135 00136 /* ISR for serial timeout */ 00137 void sertmout() 00138 { 00139 ser_timeout = true; 00140 } 00141 00142 bool modem_command_check_ok(char * command) 00143 { 00144 /* first clear serial data buffers */ 00145 while(modem.readable()) modem.getc(); 00146 /* Timeout for response of the modem */ 00147 Timeout tmout; 00148 ser_timeout = false; 00149 /* Buffer for incoming data */ 00150 char responsebuffer[6]; 00151 /* Flag to set when we get 'OK' response */ 00152 bool ok = false; 00153 bool error = false; 00154 /* Print command to TD120x */ 00155 modem.printf(command); 00156 /* Newline to activate command */ 00157 modem.printf("\n"); 00158 /* Wait untill serial feedback, max 3 seconds before timeout */ 00159 tmout.attach(&sertmout, 3.0); 00160 while(!modem.readable()&& ser_timeout == false); 00161 while(!ok && !ser_timeout && !error) { 00162 if(modem.readable()) { 00163 for(int i = 0; i < 5; i++) { 00164 responsebuffer[i] = responsebuffer[i+1]; 00165 } 00166 responsebuffer[5] = modem.getc(); 00167 if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'O' && responsebuffer[3] == 'K' && responsebuffer[4] == '\r' && responsebuffer[5] == '\n' ) { 00168 ok = true; 00169 } else if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'E' && responsebuffer[3] == 'R' && responsebuffer[4] == 'R' && responsebuffer[5] == 'O' ) { 00170 error = true; 00171 } 00172 } 00173 } 00174 tmout.detach(); 00175 return ok; 00176 }
Generated on Fri Jul 22 2022 15:30:43 by
