CaryCoders
/
demo_SX1276_standalone
standalone sx1276 demo program
Fork of SX1276_GPS by
hello.cpp@31:2c813f321db7, 2015-07-14 (annotated)
- Committer:
- ftagius
- Date:
- Tue Jul 14 14:58:23 2015 +0000
- Revision:
- 31:2c813f321db7
- Parent:
- 29:0ea07cc7124b
- Child:
- 32:a2472bbe7c92
refactored sx1276 demo code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ftagius | 29:0ea07cc7124b | 1 | #include "mbed.h" |
ftagius | 29:0ea07cc7124b | 2 | #include "sx1276-hal.h" |
ftagius | 29:0ea07cc7124b | 3 | #include "main.h" |
ftagius | 29:0ea07cc7124b | 4 | #include "debug.h" |
ftagius | 31:2c813f321db7 | 5 | // radfta #include "vt100.h" |
ftagius | 29:0ea07cc7124b | 6 | #include "serial_api.h" |
ftagius | 29:0ea07cc7124b | 7 | #include "GPS.h" |
ftagius | 29:0ea07cc7124b | 8 | #include <math.h> |
ftagius | 29:0ea07cc7124b | 9 | #define PI 3.14159265 |
ftagius | 29:0ea07cc7124b | 10 | |
ftagius | 29:0ea07cc7124b | 11 | void start_hello(void) |
ftagius | 29:0ea07cc7124b | 12 | { |
ftagius | 29:0ea07cc7124b | 13 | int len = get_kbd_str(pcbuf, PCBUF_SIZE); |
ftagius | 29:0ea07cc7124b | 14 | printf("in start gps/hello\r\n"); |
ftagius | 29:0ea07cc7124b | 15 | if (len < 0) { |
ftagius | 29:0ea07cc7124b | 16 | fflush(stdout); |
ftagius | 29:0ea07cc7124b | 17 | Radio.Rx( RX_TIMEOUT_VALUE ); |
ftagius | 29:0ea07cc7124b | 18 | Radio.Tx( TX_TIMEOUT_VALUE ); |
ftagius | 29:0ea07cc7124b | 19 | Radio.Sleep( ); |
ftagius | 29:0ea07cc7124b | 20 | if (gpsEnabled) |
ftagius | 29:0ea07cc7124b | 21 | debug("GPS exchange ended\r\n"); |
ftagius | 29:0ea07cc7124b | 22 | else |
ftagius | 29:0ea07cc7124b | 23 | debug("Hello exchange ended\r\n"); |
ftagius | 29:0ea07cc7124b | 24 | app = APP_CONSOLE; |
ftagius | 29:0ea07cc7124b | 25 | print_help(); |
ftagius | 29:0ea07cc7124b | 26 | printf("> "); |
ftagius | 29:0ea07cc7124b | 27 | fflush(stdout); |
ftagius | 29:0ea07cc7124b | 28 | return; |
ftagius | 29:0ea07cc7124b | 29 | } |
ftagius | 29:0ea07cc7124b | 30 | else |
ftagius | 29:0ea07cc7124b | 31 | { |
ftagius | 29:0ea07cc7124b | 32 | hello(); |
ftagius | 29:0ea07cc7124b | 33 | } |
ftagius | 29:0ea07cc7124b | 34 | } |
ftagius | 29:0ea07cc7124b | 35 | |
ftagius | 29:0ea07cc7124b | 36 | void find_distance() |
ftagius | 29:0ea07cc7124b | 37 | { |
ftagius | 29:0ea07cc7124b | 38 | //float dist = 0; |
ftagius | 29:0ea07cc7124b | 39 | float lat2 = 0; |
ftagius | 29:0ea07cc7124b | 40 | float lon2 = 0; |
ftagius | 29:0ea07cc7124b | 41 | float lat1 = 0; |
ftagius | 29:0ea07cc7124b | 42 | float lon1 = 0; |
ftagius | 29:0ea07cc7124b | 43 | float dLat = 0; |
ftagius | 29:0ea07cc7124b | 44 | float dLon = 0; |
ftagius | 29:0ea07cc7124b | 45 | float c = 0; |
ftagius | 29:0ea07cc7124b | 46 | float a = 0; |
ftagius | 29:0ea07cc7124b | 47 | float d = 0; |
ftagius | 29:0ea07cc7124b | 48 | |
ftagius | 31:2c813f321db7 | 49 | #ifdef radfta |
ftagius | 29:0ea07cc7124b | 50 | gpsd.distance = 0; |
ftagius | 29:0ea07cc7124b | 51 | if (gpsd.l_latitude == 0.0) |
ftagius | 29:0ea07cc7124b | 52 | { |
ftagius | 29:0ea07cc7124b | 53 | gpsd.distance = -1; |
ftagius | 29:0ea07cc7124b | 54 | return; |
ftagius | 29:0ea07cc7124b | 55 | } |
ftagius | 29:0ea07cc7124b | 56 | if (gpsd.l_longitude == 0.0) |
ftagius | 29:0ea07cc7124b | 57 | { |
ftagius | 29:0ea07cc7124b | 58 | gpsd.distance = -1; |
ftagius | 29:0ea07cc7124b | 59 | return; |
ftagius | 29:0ea07cc7124b | 60 | } |
ftagius | 29:0ea07cc7124b | 61 | |
ftagius | 29:0ea07cc7124b | 62 | //lat1 = 35.73244; |
ftagius | 29:0ea07cc7124b | 63 | //lon1 = -78.79333; |
ftagius | 29:0ea07cc7124b | 64 | |
ftagius | 29:0ea07cc7124b | 65 | lat1 = gpsd.r_latitude; |
ftagius | 29:0ea07cc7124b | 66 | lon1 = gpsd.r_longitude; |
ftagius | 29:0ea07cc7124b | 67 | |
ftagius | 29:0ea07cc7124b | 68 | lat2 = gpsd.l_latitude; |
ftagius | 29:0ea07cc7124b | 69 | lon2 = gpsd.l_longitude; |
ftagius | 29:0ea07cc7124b | 70 | //Calculate Distance |
ftagius | 29:0ea07cc7124b | 71 | dLat = (lat2-lat1)*(PI/180); |
ftagius | 29:0ea07cc7124b | 72 | dLon = (lon2-lon1)*(PI/180); |
ftagius | 29:0ea07cc7124b | 73 | a = sin(dLat/2) * sin(dLat/2) + |
ftagius | 29:0ea07cc7124b | 74 | sin(dLon/2) * sin(dLon/2) * cos(lat1*(PI/180)) * cos(lat2*(PI/180)); |
ftagius | 29:0ea07cc7124b | 75 | c = 2 * atan2(sqrt(a), sqrt(1-a)); |
ftagius | 29:0ea07cc7124b | 76 | d = 6371000 * c; |
ftagius | 29:0ea07cc7124b | 77 | |
ftagius | 29:0ea07cc7124b | 78 | //GPS is only precise to 5 meters, so throw out bad data |
ftagius | 29:0ea07cc7124b | 79 | if ( d <= 5) { |
ftagius | 29:0ea07cc7124b | 80 | d = 0; |
ftagius | 29:0ea07cc7124b | 81 | } |
ftagius | 29:0ea07cc7124b | 82 | gpsd.distance = gpsd.distance + d; |
ftagius | 29:0ea07cc7124b | 83 | // printf("distance: %f\r\n", gpsd.distance); |
ftagius | 31:2c813f321db7 | 84 | #endif |
ftagius | 29:0ea07cc7124b | 85 | } |
ftagius | 29:0ea07cc7124b | 86 | |
ftagius | 29:0ea07cc7124b | 87 | void check_gps(void) |
ftagius | 29:0ea07cc7124b | 88 | { |
ftagius | 29:0ea07cc7124b | 89 | static int gps_count=0; |
ftagius | 31:2c813f321db7 | 90 | #ifdef radfta |
ftagius | 29:0ea07cc7124b | 91 | if (gps_count >= 0) |
ftagius | 29:0ea07cc7124b | 92 | { |
ftagius | 29:0ea07cc7124b | 93 | //printf("checking gps\r\n"); |
ftagius | 29:0ea07cc7124b | 94 | gps_count=0; // reset for next cycle |
ftagius | 29:0ea07cc7124b | 95 | if(gpsd.sample()) |
ftagius | 29:0ea07cc7124b | 96 | { |
ftagius | 29:0ea07cc7124b | 97 | led = !led; |
ftagius | 29:0ea07cc7124b | 98 | static int swap=0; |
ftagius | 29:0ea07cc7124b | 99 | //printf("gps sample is true \r\n"); |
ftagius | 29:0ea07cc7124b | 100 | if (gpsd.l_latitude != 0) |
ftagius | 29:0ea07cc7124b | 101 | { |
ftagius | 29:0ea07cc7124b | 102 | if (RADIO_INSTALLED) |
ftagius | 29:0ea07cc7124b | 103 | { |
ftagius | 29:0ea07cc7124b | 104 | cLCD.setCursor(0,1); |
ftagius | 29:0ea07cc7124b | 105 | cLCD.printf("d:NA per:%d ", per); |
ftagius | 29:0ea07cc7124b | 106 | //cLCD.printf("%0.4f %0.4f", gpsd.l_latitude, gpsd.l_longitude); |
ftagius | 29:0ea07cc7124b | 107 | } |
ftagius | 29:0ea07cc7124b | 108 | else |
ftagius | 29:0ea07cc7124b | 109 | { |
ftagius | 29:0ea07cc7124b | 110 | if (swap) |
ftagius | 29:0ea07cc7124b | 111 | { |
ftagius | 29:0ea07cc7124b | 112 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 113 | cLCD.printf("Lat: %0.6f ", gpsd.l_latitude); |
ftagius | 29:0ea07cc7124b | 114 | cLCD.setCursor(0,1); |
ftagius | 29:0ea07cc7124b | 115 | cLCD.printf("Lon: %0.6f ", gpsd.l_longitude); |
ftagius | 29:0ea07cc7124b | 116 | swap = 0; |
ftagius | 29:0ea07cc7124b | 117 | } |
ftagius | 29:0ea07cc7124b | 118 | else |
ftagius | 29:0ea07cc7124b | 119 | { |
ftagius | 29:0ea07cc7124b | 120 | cLCD.setCursor(0,1); |
ftagius | 29:0ea07cc7124b | 121 | cLCD.printf("Lat: %0.6f ", gpsd.l_latitude); |
ftagius | 29:0ea07cc7124b | 122 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 123 | cLCD.printf("Lon: %0.6f ", gpsd.l_longitude); |
ftagius | 29:0ea07cc7124b | 124 | swap = 1; |
ftagius | 29:0ea07cc7124b | 125 | } |
ftagius | 29:0ea07cc7124b | 126 | } |
ftagius | 29:0ea07cc7124b | 127 | |
ftagius | 29:0ea07cc7124b | 128 | if (gpsd.r_latitude != 0) |
ftagius | 29:0ea07cc7124b | 129 | { |
ftagius | 29:0ea07cc7124b | 130 | find_distance(); |
ftagius | 29:0ea07cc7124b | 131 | if (gpsd.distance < 50000 ) |
ftagius | 29:0ea07cc7124b | 132 | { |
ftagius | 29:0ea07cc7124b | 133 | if (gpsd.distance >= 0) |
ftagius | 29:0ea07cc7124b | 134 | cLCD.setCursor(0,1); |
ftagius | 29:0ea07cc7124b | 135 | cLCD.printf("d:%0.1fm per:%d ", gpsd.distance,per); |
ftagius | 29:0ea07cc7124b | 136 | } |
ftagius | 29:0ea07cc7124b | 137 | } |
ftagius | 29:0ea07cc7124b | 138 | } |
ftagius | 29:0ea07cc7124b | 139 | else |
ftagius | 29:0ea07cc7124b | 140 | { |
ftagius | 29:0ea07cc7124b | 141 | cLCD.setCursor(0,1); |
ftagius | 29:0ea07cc7124b | 142 | cLCD.printf("No GPS. per:%d ",per); |
ftagius | 29:0ea07cc7124b | 143 | } |
ftagius | 29:0ea07cc7124b | 144 | } |
ftagius | 29:0ea07cc7124b | 145 | else |
ftagius | 29:0ea07cc7124b | 146 | { |
ftagius | 29:0ea07cc7124b | 147 | //printf("gps sample is false \r\n"); |
ftagius | 29:0ea07cc7124b | 148 | cLCD.setCursor(0,1); |
ftagius | 29:0ea07cc7124b | 149 | cLCD.printf("No GPS. per:%d ",per); |
ftagius | 29:0ea07cc7124b | 150 | } |
ftagius | 29:0ea07cc7124b | 151 | } |
ftagius | 29:0ea07cc7124b | 152 | else |
ftagius | 29:0ea07cc7124b | 153 | { |
ftagius | 29:0ea07cc7124b | 154 | #if 0 |
ftagius | 29:0ea07cc7124b | 155 | if (gps_count % 5) |
ftagius | 29:0ea07cc7124b | 156 | gpsd.flushSerialBuffer(); |
ftagius | 29:0ea07cc7124b | 157 | #endif |
ftagius | 29:0ea07cc7124b | 158 | //printf("not checking gps\r\n"); |
ftagius | 29:0ea07cc7124b | 159 | gps_count++; |
ftagius | 29:0ea07cc7124b | 160 | } |
ftagius | 31:2c813f321db7 | 161 | #endif |
ftagius | 29:0ea07cc7124b | 162 | } |
ftagius | 29:0ea07cc7124b | 163 | |
ftagius | 29:0ea07cc7124b | 164 | void hello(void) |
ftagius | 29:0ea07cc7124b | 165 | { |
ftagius | 29:0ea07cc7124b | 166 | srand ( randomSeed() ); |
ftagius | 29:0ea07cc7124b | 167 | // generate a random wait time |
ftagius | 29:0ea07cc7124b | 168 | int mywait = ((rand()%2 +1)); |
ftagius | 29:0ea07cc7124b | 169 | int i=0; |
ftagius | 29:0ea07cc7124b | 170 | int rcvd=0; |
ftagius | 29:0ea07cc7124b | 171 | int lost=0; |
ftagius | 29:0ea07cc7124b | 172 | |
ftagius | 29:0ea07cc7124b | 173 | for (i=0;i<max_pkts;i++) |
ftagius | 29:0ea07cc7124b | 174 | { |
ftagius | 29:0ea07cc7124b | 175 | |
ftagius | 29:0ea07cc7124b | 176 | if (pkt_data[i]==1) |
ftagius | 29:0ea07cc7124b | 177 | { |
ftagius | 29:0ea07cc7124b | 178 | //printf("rcvd, pkt count=%d\r\n",i); |
ftagius | 29:0ea07cc7124b | 179 | rcvd++; |
ftagius | 29:0ea07cc7124b | 180 | } |
ftagius | 29:0ea07cc7124b | 181 | else if (pkt_data[i] == 2) |
ftagius | 29:0ea07cc7124b | 182 | { |
ftagius | 29:0ea07cc7124b | 183 | //printf("lose, pkt count=%d\r\n",i); |
ftagius | 29:0ea07cc7124b | 184 | lost++; |
ftagius | 29:0ea07cc7124b | 185 | } |
ftagius | 29:0ea07cc7124b | 186 | |
ftagius | 29:0ea07cc7124b | 187 | } |
ftagius | 29:0ea07cc7124b | 188 | if ((rcvd + lost) > 0) |
ftagius | 29:0ea07cc7124b | 189 | { |
ftagius | 29:0ea07cc7124b | 190 | per = 100 - (rcvd*100 / (rcvd + lost)); |
ftagius | 29:0ea07cc7124b | 191 | //printf("rcvd = %d, lost = %d per = %d\r\n",rcvd, lost, per); |
ftagius | 29:0ea07cc7124b | 192 | } |
ftagius | 29:0ea07cc7124b | 193 | else |
ftagius | 29:0ea07cc7124b | 194 | per = 0; |
ftagius | 29:0ea07cc7124b | 195 | |
ftagius | 29:0ea07cc7124b | 196 | |
ftagius | 29:0ea07cc7124b | 197 | switch( State ) |
ftagius | 29:0ea07cc7124b | 198 | { |
ftagius | 29:0ea07cc7124b | 199 | |
ftagius | 29:0ea07cc7124b | 200 | case RX_TIMEOUT: |
ftagius | 29:0ea07cc7124b | 201 | |
ftagius | 29:0ea07cc7124b | 202 | printf("rx timeout in gps/hello loop. "); |
ftagius | 29:0ea07cc7124b | 203 | printf("per=%d\r\n", per); |
ftagius | 31:2c813f321db7 | 204 | // radfta gpsd.r_latitude = 0; |
ftagius | 31:2c813f321db7 | 205 | // radfta gpsd.r_longitude = 0; |
ftagius | 29:0ea07cc7124b | 206 | // printf("rcv - rssi = %d snr = %d msg=%s\r\n", RssiValue, SnrValue, BufferRx); |
ftagius | 29:0ea07cc7124b | 207 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 208 | cLCD.printf("RI:NA SR:NA "); |
ftagius | 29:0ea07cc7124b | 209 | case RX: |
ftagius | 29:0ea07cc7124b | 210 | |
ftagius | 29:0ea07cc7124b | 211 | if( BufferSize > 0 ) |
ftagius | 29:0ea07cc7124b | 212 | { |
ftagius | 29:0ea07cc7124b | 213 | if( strncmp( ( const char* )BufferRx, ( const char* )HelloMsg, 5 ) == 0 ) |
ftagius | 29:0ea07cc7124b | 214 | { |
ftagius | 29:0ea07cc7124b | 215 | led = !led; |
ftagius | 29:0ea07cc7124b | 216 | printf("rcv - rssi = %d snr = %d msg=%s ", RssiValue, SnrValue, BufferRx ); |
ftagius | 29:0ea07cc7124b | 217 | printf("per=%d\r\n", per); |
ftagius | 29:0ea07cc7124b | 218 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 219 | cLCD.printf("RI:%d SR:%d ", RssiValue, SnrValue); |
ftagius | 29:0ea07cc7124b | 220 | } |
ftagius | 29:0ea07cc7124b | 221 | else |
ftagius | 29:0ea07cc7124b | 222 | { |
ftagius | 29:0ea07cc7124b | 223 | // float latitude, longitude; |
ftagius | 31:2c813f321db7 | 224 | #ifdef radfta |
ftagius | 29:0ea07cc7124b | 225 | if(sscanf((char *)BufferRx, "GP,%f,%f", &gpsd.r_latitude, &gpsd.r_longitude ) >= 1) |
ftagius | 29:0ea07cc7124b | 226 | { |
ftagius | 29:0ea07cc7124b | 227 | led = !led; |
ftagius | 29:0ea07cc7124b | 228 | printf("rssi=%d snr=%d rlat=%0.5f rlon=%0.5f llat=%0.5f llon=%0.5f dst=%0.0fm ", RssiValue, SnrValue, gpsd.r_latitude, gpsd.r_longitude, gpsd.l_latitude, gpsd.l_longitude, gpsd.distance); |
ftagius | 29:0ea07cc7124b | 229 | printf("per=%d\r\n", per); |
ftagius | 29:0ea07cc7124b | 230 | cLCD.setCursor(0,0); |
ftagius | 29:0ea07cc7124b | 231 | cLCD.printf("RI:%d SR:%d ", RssiValue,SnrValue); |
ftagius | 29:0ea07cc7124b | 232 | //printf("Msg received - Lat: %0.4f Lon: %0.4f\r\n", latitude, longitude); |
ftagius | 29:0ea07cc7124b | 233 | } |
ftagius | 29:0ea07cc7124b | 234 | else |
ftagius | 29:0ea07cc7124b | 235 | printf("strange received message: %s\r\n", BufferRx); |
ftagius | 31:2c813f321db7 | 236 | #else |
ftagius | 31:2c813f321db7 | 237 | cLCD.setCursor(0,0); |
ftagius | 31:2c813f321db7 | 238 | cLCD.printf("RI:%d SR:%d ", RssiValue,SnrValue); |
ftagius | 31:2c813f321db7 | 239 | printf("received message: %s\r\n", BufferRx); |
ftagius | 31:2c813f321db7 | 240 | #endif |
ftagius | 31:2c813f321db7 | 241 | } |
ftagius | 29:0ea07cc7124b | 242 | } |
ftagius | 29:0ea07cc7124b | 243 | case RX_ERROR: |
ftagius | 29:0ea07cc7124b | 244 | if (gpsEnabled) |
ftagius | 29:0ea07cc7124b | 245 | { |
ftagius | 29:0ea07cc7124b | 246 | check_gps(); |
ftagius | 29:0ea07cc7124b | 247 | } |
ftagius | 31:2c813f321db7 | 248 | #ifdef radfta |
ftagius | 29:0ea07cc7124b | 249 | if (gpsEnabled) |
ftagius | 29:0ea07cc7124b | 250 | sprintf((char *)BufferTx, "GP,%4.5f,%4.5f", gpsd.l_latitude, gpsd.l_longitude); |
ftagius | 29:0ea07cc7124b | 251 | else |
ftagius | 31:2c813f321db7 | 252 | #else |
ftagius | 29:0ea07cc7124b | 253 | strcpy( ( char* )BufferTx, ( char* )HelloMsg ); |
ftagius | 31:2c813f321db7 | 254 | #endif |
ftagius | 29:0ea07cc7124b | 255 | BufferSize=strlen((char *)BufferTx); |
ftagius | 29:0ea07cc7124b | 256 | //printf("Sending new buffer\r\n"); |
ftagius | 29:0ea07cc7124b | 257 | Radio.Send( BufferTx, BufferSize ); |
ftagius | 29:0ea07cc7124b | 258 | State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 259 | |
ftagius | 29:0ea07cc7124b | 260 | break; |
ftagius | 29:0ea07cc7124b | 261 | case TX: |
ftagius | 29:0ea07cc7124b | 262 | //printf("tx state\r\n"); |
ftagius | 29:0ea07cc7124b | 263 | //wait_ms (30); |
ftagius | 29:0ea07cc7124b | 264 | |
ftagius | 29:0ea07cc7124b | 265 | Radio.Rx( RX_TIMEOUT_VALUE / mywait ); |
ftagius | 29:0ea07cc7124b | 266 | State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 267 | break; |
ftagius | 29:0ea07cc7124b | 268 | case TX_TIMEOUT: |
ftagius | 29:0ea07cc7124b | 269 | debug("tx timeout\r\n"); |
ftagius | 29:0ea07cc7124b | 270 | Radio.Tx( TX_TIMEOUT_VALUE ); |
ftagius | 29:0ea07cc7124b | 271 | State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 272 | break; |
ftagius | 29:0ea07cc7124b | 273 | case LOWPOWER: |
ftagius | 29:0ea07cc7124b | 274 | wait_ms (5); |
ftagius | 29:0ea07cc7124b | 275 | break; |
ftagius | 29:0ea07cc7124b | 276 | default: |
ftagius | 29:0ea07cc7124b | 277 | debug("state is set to low power\r\n"); |
ftagius | 29:0ea07cc7124b | 278 | State = LOWPOWER; |
ftagius | 29:0ea07cc7124b | 279 | break; |
ftagius | 29:0ea07cc7124b | 280 | } |
ftagius | 29:0ea07cc7124b | 281 | } |