standalone sx1276 demo program

Dependencies:   SX1276Lib mbed

Fork of SX1276_GPS by CaryCoders

Committer:
ftagius
Date:
Tue Jun 16 11:53:20 2015 +0000
Revision:
29:0ea07cc7124b
Child:
31:2c813f321db7
working version of the sx1276 radio with integrated gps and lcd;

Who changed what in which revision?

UserRevisionLine numberNew 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 29:0ea07cc7124b 5 #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 29:0ea07cc7124b 49 gpsd.distance = 0;
ftagius 29:0ea07cc7124b 50 if (gpsd.l_latitude == 0.0)
ftagius 29:0ea07cc7124b 51 {
ftagius 29:0ea07cc7124b 52 gpsd.distance = -1;
ftagius 29:0ea07cc7124b 53 return;
ftagius 29:0ea07cc7124b 54 }
ftagius 29:0ea07cc7124b 55 if (gpsd.l_longitude == 0.0)
ftagius 29:0ea07cc7124b 56 {
ftagius 29:0ea07cc7124b 57 gpsd.distance = -1;
ftagius 29:0ea07cc7124b 58 return;
ftagius 29:0ea07cc7124b 59 }
ftagius 29:0ea07cc7124b 60
ftagius 29:0ea07cc7124b 61 //lat1 = 35.73244;
ftagius 29:0ea07cc7124b 62 //lon1 = -78.79333;
ftagius 29:0ea07cc7124b 63
ftagius 29:0ea07cc7124b 64 lat1 = gpsd.r_latitude;
ftagius 29:0ea07cc7124b 65 lon1 = gpsd.r_longitude;
ftagius 29:0ea07cc7124b 66
ftagius 29:0ea07cc7124b 67 lat2 = gpsd.l_latitude;
ftagius 29:0ea07cc7124b 68 lon2 = gpsd.l_longitude;
ftagius 29:0ea07cc7124b 69 //Calculate Distance
ftagius 29:0ea07cc7124b 70 dLat = (lat2-lat1)*(PI/180);
ftagius 29:0ea07cc7124b 71 dLon = (lon2-lon1)*(PI/180);
ftagius 29:0ea07cc7124b 72 a = sin(dLat/2) * sin(dLat/2) +
ftagius 29:0ea07cc7124b 73 sin(dLon/2) * sin(dLon/2) * cos(lat1*(PI/180)) * cos(lat2*(PI/180));
ftagius 29:0ea07cc7124b 74 c = 2 * atan2(sqrt(a), sqrt(1-a));
ftagius 29:0ea07cc7124b 75 d = 6371000 * c;
ftagius 29:0ea07cc7124b 76
ftagius 29:0ea07cc7124b 77 //GPS is only precise to 5 meters, so throw out bad data
ftagius 29:0ea07cc7124b 78 if ( d <= 5) {
ftagius 29:0ea07cc7124b 79 d = 0;
ftagius 29:0ea07cc7124b 80 }
ftagius 29:0ea07cc7124b 81 gpsd.distance = gpsd.distance + d;
ftagius 29:0ea07cc7124b 82 // printf("distance: %f\r\n", gpsd.distance);
ftagius 29:0ea07cc7124b 83
ftagius 29:0ea07cc7124b 84 }
ftagius 29:0ea07cc7124b 85
ftagius 29:0ea07cc7124b 86 void check_gps(void)
ftagius 29:0ea07cc7124b 87 {
ftagius 29:0ea07cc7124b 88 static int gps_count=0;
ftagius 29:0ea07cc7124b 89
ftagius 29:0ea07cc7124b 90 if (gps_count >= 0)
ftagius 29:0ea07cc7124b 91 {
ftagius 29:0ea07cc7124b 92 //printf("checking gps\r\n");
ftagius 29:0ea07cc7124b 93 gps_count=0; // reset for next cycle
ftagius 29:0ea07cc7124b 94 if(gpsd.sample())
ftagius 29:0ea07cc7124b 95 {
ftagius 29:0ea07cc7124b 96 led = !led;
ftagius 29:0ea07cc7124b 97 static int swap=0;
ftagius 29:0ea07cc7124b 98 //printf("gps sample is true \r\n");
ftagius 29:0ea07cc7124b 99 if (gpsd.l_latitude != 0)
ftagius 29:0ea07cc7124b 100 {
ftagius 29:0ea07cc7124b 101 if (RADIO_INSTALLED)
ftagius 29:0ea07cc7124b 102 {
ftagius 29:0ea07cc7124b 103 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 104 cLCD.printf("d:NA per:%d ", per);
ftagius 29:0ea07cc7124b 105 //cLCD.printf("%0.4f %0.4f", gpsd.l_latitude, gpsd.l_longitude);
ftagius 29:0ea07cc7124b 106 }
ftagius 29:0ea07cc7124b 107 else
ftagius 29:0ea07cc7124b 108 {
ftagius 29:0ea07cc7124b 109 if (swap)
ftagius 29:0ea07cc7124b 110 {
ftagius 29:0ea07cc7124b 111 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 112 cLCD.printf("Lat: %0.6f ", gpsd.l_latitude);
ftagius 29:0ea07cc7124b 113 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 114 cLCD.printf("Lon: %0.6f ", gpsd.l_longitude);
ftagius 29:0ea07cc7124b 115 swap = 0;
ftagius 29:0ea07cc7124b 116 }
ftagius 29:0ea07cc7124b 117 else
ftagius 29:0ea07cc7124b 118 {
ftagius 29:0ea07cc7124b 119 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 120 cLCD.printf("Lat: %0.6f ", gpsd.l_latitude);
ftagius 29:0ea07cc7124b 121 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 122 cLCD.printf("Lon: %0.6f ", gpsd.l_longitude);
ftagius 29:0ea07cc7124b 123 swap = 1;
ftagius 29:0ea07cc7124b 124 }
ftagius 29:0ea07cc7124b 125 }
ftagius 29:0ea07cc7124b 126
ftagius 29:0ea07cc7124b 127 if (gpsd.r_latitude != 0)
ftagius 29:0ea07cc7124b 128 {
ftagius 29:0ea07cc7124b 129 find_distance();
ftagius 29:0ea07cc7124b 130 if (gpsd.distance < 50000 )
ftagius 29:0ea07cc7124b 131 {
ftagius 29:0ea07cc7124b 132 if (gpsd.distance >= 0)
ftagius 29:0ea07cc7124b 133 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 134 cLCD.printf("d:%0.1fm per:%d ", gpsd.distance,per);
ftagius 29:0ea07cc7124b 135 }
ftagius 29:0ea07cc7124b 136 }
ftagius 29:0ea07cc7124b 137 }
ftagius 29:0ea07cc7124b 138 else
ftagius 29:0ea07cc7124b 139 {
ftagius 29:0ea07cc7124b 140 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 141 cLCD.printf("No GPS. per:%d ",per);
ftagius 29:0ea07cc7124b 142 }
ftagius 29:0ea07cc7124b 143 }
ftagius 29:0ea07cc7124b 144 else
ftagius 29:0ea07cc7124b 145 {
ftagius 29:0ea07cc7124b 146 //printf("gps sample is false \r\n");
ftagius 29:0ea07cc7124b 147 cLCD.setCursor(0,1);
ftagius 29:0ea07cc7124b 148 cLCD.printf("No GPS. per:%d ",per);
ftagius 29:0ea07cc7124b 149 }
ftagius 29:0ea07cc7124b 150 }
ftagius 29:0ea07cc7124b 151 else
ftagius 29:0ea07cc7124b 152 {
ftagius 29:0ea07cc7124b 153 #if 0
ftagius 29:0ea07cc7124b 154 if (gps_count % 5)
ftagius 29:0ea07cc7124b 155 gpsd.flushSerialBuffer();
ftagius 29:0ea07cc7124b 156 #endif
ftagius 29:0ea07cc7124b 157 //printf("not checking gps\r\n");
ftagius 29:0ea07cc7124b 158 gps_count++;
ftagius 29:0ea07cc7124b 159 }
ftagius 29:0ea07cc7124b 160
ftagius 29:0ea07cc7124b 161 }
ftagius 29:0ea07cc7124b 162
ftagius 29:0ea07cc7124b 163 void hello(void)
ftagius 29:0ea07cc7124b 164 {
ftagius 29:0ea07cc7124b 165 srand ( randomSeed() );
ftagius 29:0ea07cc7124b 166 // generate a random wait time
ftagius 29:0ea07cc7124b 167 int mywait = ((rand()%2 +1));
ftagius 29:0ea07cc7124b 168 int i=0;
ftagius 29:0ea07cc7124b 169 int rcvd=0;
ftagius 29:0ea07cc7124b 170 int lost=0;
ftagius 29:0ea07cc7124b 171
ftagius 29:0ea07cc7124b 172 for (i=0;i<max_pkts;i++)
ftagius 29:0ea07cc7124b 173 {
ftagius 29:0ea07cc7124b 174
ftagius 29:0ea07cc7124b 175 if (pkt_data[i]==1)
ftagius 29:0ea07cc7124b 176 {
ftagius 29:0ea07cc7124b 177 //printf("rcvd, pkt count=%d\r\n",i);
ftagius 29:0ea07cc7124b 178 rcvd++;
ftagius 29:0ea07cc7124b 179 }
ftagius 29:0ea07cc7124b 180 else if (pkt_data[i] == 2)
ftagius 29:0ea07cc7124b 181 {
ftagius 29:0ea07cc7124b 182 //printf("lose, pkt count=%d\r\n",i);
ftagius 29:0ea07cc7124b 183 lost++;
ftagius 29:0ea07cc7124b 184 }
ftagius 29:0ea07cc7124b 185
ftagius 29:0ea07cc7124b 186 }
ftagius 29:0ea07cc7124b 187 if ((rcvd + lost) > 0)
ftagius 29:0ea07cc7124b 188 {
ftagius 29:0ea07cc7124b 189 per = 100 - (rcvd*100 / (rcvd + lost));
ftagius 29:0ea07cc7124b 190 //printf("rcvd = %d, lost = %d per = %d\r\n",rcvd, lost, per);
ftagius 29:0ea07cc7124b 191 }
ftagius 29:0ea07cc7124b 192 else
ftagius 29:0ea07cc7124b 193 per = 0;
ftagius 29:0ea07cc7124b 194
ftagius 29:0ea07cc7124b 195
ftagius 29:0ea07cc7124b 196 switch( State )
ftagius 29:0ea07cc7124b 197 {
ftagius 29:0ea07cc7124b 198
ftagius 29:0ea07cc7124b 199 case RX_TIMEOUT:
ftagius 29:0ea07cc7124b 200
ftagius 29:0ea07cc7124b 201 printf("rx timeout in gps/hello loop. ");
ftagius 29:0ea07cc7124b 202 printf("per=%d\r\n", per);
ftagius 29:0ea07cc7124b 203 gpsd.r_latitude = 0;
ftagius 29:0ea07cc7124b 204 gpsd.r_longitude = 0;
ftagius 29:0ea07cc7124b 205 // printf("rcv - rssi = %d snr = %d msg=%s\r\n", RssiValue, SnrValue, BufferRx);
ftagius 29:0ea07cc7124b 206 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 207 cLCD.printf("RI:NA SR:NA ");
ftagius 29:0ea07cc7124b 208 case RX:
ftagius 29:0ea07cc7124b 209
ftagius 29:0ea07cc7124b 210 if( BufferSize > 0 )
ftagius 29:0ea07cc7124b 211 {
ftagius 29:0ea07cc7124b 212 if( strncmp( ( const char* )BufferRx, ( const char* )HelloMsg, 5 ) == 0 )
ftagius 29:0ea07cc7124b 213 {
ftagius 29:0ea07cc7124b 214 led = !led;
ftagius 29:0ea07cc7124b 215 printf("rcv - rssi = %d snr = %d msg=%s ", RssiValue, SnrValue, BufferRx );
ftagius 29:0ea07cc7124b 216 printf("per=%d\r\n", per);
ftagius 29:0ea07cc7124b 217 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 218 cLCD.printf("RI:%d SR:%d ", RssiValue, SnrValue);
ftagius 29:0ea07cc7124b 219 }
ftagius 29:0ea07cc7124b 220 else
ftagius 29:0ea07cc7124b 221 {
ftagius 29:0ea07cc7124b 222 // float latitude, longitude;
ftagius 29:0ea07cc7124b 223 if(sscanf((char *)BufferRx, "GP,%f,%f", &gpsd.r_latitude, &gpsd.r_longitude ) >= 1)
ftagius 29:0ea07cc7124b 224 {
ftagius 29:0ea07cc7124b 225 led = !led;
ftagius 29:0ea07cc7124b 226 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 227 printf("per=%d\r\n", per);
ftagius 29:0ea07cc7124b 228
ftagius 29:0ea07cc7124b 229 cLCD.setCursor(0,0);
ftagius 29:0ea07cc7124b 230 cLCD.printf("RI:%d SR:%d ", RssiValue,SnrValue);
ftagius 29:0ea07cc7124b 231 //printf("Msg received - Lat: %0.4f Lon: %0.4f\r\n", latitude, longitude);
ftagius 29:0ea07cc7124b 232 }
ftagius 29:0ea07cc7124b 233 else
ftagius 29:0ea07cc7124b 234 printf("strange received message: %s\r\n", BufferRx);
ftagius 29:0ea07cc7124b 235 }
ftagius 29:0ea07cc7124b 236 }
ftagius 29:0ea07cc7124b 237 case RX_ERROR:
ftagius 29:0ea07cc7124b 238 if (gpsEnabled)
ftagius 29:0ea07cc7124b 239 {
ftagius 29:0ea07cc7124b 240 check_gps();
ftagius 29:0ea07cc7124b 241 }
ftagius 29:0ea07cc7124b 242 if (gpsEnabled)
ftagius 29:0ea07cc7124b 243 sprintf((char *)BufferTx, "GP,%4.5f,%4.5f", gpsd.l_latitude, gpsd.l_longitude);
ftagius 29:0ea07cc7124b 244 else
ftagius 29:0ea07cc7124b 245 strcpy( ( char* )BufferTx, ( char* )HelloMsg );
ftagius 29:0ea07cc7124b 246
ftagius 29:0ea07cc7124b 247 BufferSize=strlen((char *)BufferTx);
ftagius 29:0ea07cc7124b 248 //printf("Sending new buffer\r\n");
ftagius 29:0ea07cc7124b 249 Radio.Send( BufferTx, BufferSize );
ftagius 29:0ea07cc7124b 250 State = LOWPOWER;
ftagius 29:0ea07cc7124b 251
ftagius 29:0ea07cc7124b 252 break;
ftagius 29:0ea07cc7124b 253 case TX:
ftagius 29:0ea07cc7124b 254 //printf("tx state\r\n");
ftagius 29:0ea07cc7124b 255 //wait_ms (30);
ftagius 29:0ea07cc7124b 256
ftagius 29:0ea07cc7124b 257 Radio.Rx( RX_TIMEOUT_VALUE / mywait );
ftagius 29:0ea07cc7124b 258 State = LOWPOWER;
ftagius 29:0ea07cc7124b 259 break;
ftagius 29:0ea07cc7124b 260 case TX_TIMEOUT:
ftagius 29:0ea07cc7124b 261 debug("tx timeout\r\n");
ftagius 29:0ea07cc7124b 262 Radio.Tx( TX_TIMEOUT_VALUE );
ftagius 29:0ea07cc7124b 263 State = LOWPOWER;
ftagius 29:0ea07cc7124b 264 break;
ftagius 29:0ea07cc7124b 265 case LOWPOWER:
ftagius 29:0ea07cc7124b 266 wait_ms (5);
ftagius 29:0ea07cc7124b 267 break;
ftagius 29:0ea07cc7124b 268 default:
ftagius 29:0ea07cc7124b 269 debug("state is set to low power\r\n");
ftagius 29:0ea07cc7124b 270 State = LOWPOWER;
ftagius 29:0ea07cc7124b 271 break;
ftagius 29:0ea07cc7124b 272 }
ftagius 29:0ea07cc7124b 273 }