Prueba LoRa

Dependencies:   BLE_API SX1276Lib mbed nRF51822

Fork of BLE_Observer by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
Javier117
Date:
Wed Nov 02 20:53:30 2016 +0000
Parent:
7:88f50499af9a
Commit message:
no change

Changed in this revision

BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
SX1276Lib.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 88f50499af9a -r 3b30027c7e8f BLE_API.lib
--- a/BLE_API.lib	Tue Jan 12 11:00:02 2016 +0000
+++ b/BLE_API.lib	Wed Nov 02 20:53:30 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#bfc5b9b6ecf5
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#66159681aa21
diff -r 88f50499af9a -r 3b30027c7e8f SX1276Lib.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SX1276Lib.lib	Wed Nov 02 20:53:30 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Semtech/code/SX1276Lib/#3778e6204cc1
diff -r 88f50499af9a -r 3b30027c7e8f main.cpp
--- a/main.cpp	Tue Jan 12 11:00:02 2016 +0000
+++ b/main.cpp	Wed Nov 02 20:53:30 2016 +0000
@@ -16,28 +16,212 @@
 
 #include "mbed.h"
 #include "ble/BLE.h"
+#include "sx1276-hal.h"
+#include "main.h"
+
+//Configuración del radio LoRa
+#define USE_MODEM_LORA 1
+
+#define RF_FREQUENCY                                    915000000 //
+#define TX_OUTPUT_POWER                                 14 //14 dBm
+
+    #define LORA_BANDWIDTH                              1         // [0: 125 kHz,
+                                                                  //  1: 250 kHz,
+                                                                  //  2: 500 kHz,
+                                                                  //  3: Reserved]
+    #define LORA_SPREADING_FACTOR                       10         // [SF7..SF12]
+    #define LORA_CODINGRATE                             1         // [1: 4/5,
+                                                                  //  2: 4/6,
+                                                                  //  3: 4/7,
+                                                                  //  4: 4/8]
+    #define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
+    #define LORA_SYMBOL_TIMEOUT                         5         // Symbols
+    #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
+    #define LORA_FHSS_ENABLED                           true  
+    #define LORA_NB_SYMB_HOP                            4     
+    #define LORA_IQ_INVERSION_ON                        false
+    #define LORA_CRC_ENABLED                            true
+    
+#define RX_TIMEOUT_VALUE                                3500000   // in us
+#define BUFFER_SIZE                                     40        // Define the payload size here [min:1 max:255]
+
+/*
+ *  Global variables declarations
+ */
+typedef enum
+{
+    LOWPOWER = 0,
+    IDLE,
+    
+    RX,
+    RX_TIMEOUT,
+    RX_ERROR,
+    
+    TX,
+    TX_TIMEOUT,
+    
+    CAD,
+    CAD_DONE
+}AppStates_t;
+
+volatile AppStates_t State = LOWPOWER;
+
+/*!
+ * Radio events function pointer
+ */
+static RadioEvents_t RadioEvents;
+
+/*
+ *  Global variables declarations
+ */
+SX1276MB1xAS Radio( NULL );
+
+uint16_t BufferSize = BUFFER_SIZE;
+uint8_t Buffer[BUFFER_SIZE];
+
+int16_t RssiValue = 0.0;
+int8_t SnrValue = 0.0;
 
 DigitalOut led1(LED1, 1);
+DigitalOut led(LED2, 1);
 Ticker     ticker;
+Serial pc(USBTX, USBRX);
+
+const unsigned int nMax = 10;
+const float dZero = 0.65; //Distancia de la señal de referencia
+const float pdBZero = -55.0665; //Potencia de la señal de referencia
+//const int coord[4][2] = {{0, 6}, {6, 6}, {0, 0}, {6, 0}}; //Coordenadas de dos beacons
+const float coord[4][2] = {{0.55, 5}, {7.95, 5}, {7.95, 0.68}, {0.65, 0.68}}; //Coordenadas de dos beacons
+//const int coord[2][2] = {{0, 6}, {6, 6}}; //Coordenadas de dos beacons
+const int k = 1; //Grado de estimación
+const int n = 4; //Índice de pérdidas por trayectoria  
+const int nBeacons = 4; //Número de beacons
+float d[nMax];
+float w[nMax];
+
+int8_t rssi[nMax];
+int8_t dir[nMax];
+int8_t addr[nMax];
+int8_t dirAux[nMax];
+int8_t rssiRcv[4] = {0};
+uint8_t i = 0;
+uint16_t cPqt = 1;
+
+//Declaración de funciones
+void printData();
+void getCoord (float weigth[], int c);
 
 void periodicCallback(void)
 {
     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
 }
 
-void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
+{
+    if ((params->peerAddr[0] == 0x82 && params->peerAddr[5] == 0x68 && i<=nMax) || (params->peerAddr[0] == 0x02 && params->peerAddr[5] == 0xb0 && i<=nMax) || (params->peerAddr[0] == 0x01 && params->peerAddr[5] == 0xc4 && i<=nMax) || (params->peerAddr[0] == 0x82 && params->peerAddr[5] == 0xb0 && i<=nMax)) {
+        dir[i] = params->peerAddr[0];
+        dirAux[i] = params->peerAddr[5];
+        rssi[i] = params->rssi;
+        //printf("Dir: [%d][%d], Iter: %d\r\n", dir[i], dirAux, i);
+        i++;
+
+        if (i==nMax) {
+            for (int j = 0; j < nMax; j++) {
+                if (dir[j] == -126 && dirAux[j] == -80) {
+                    addr[j] = 4;
+                    rssiRcv[3] = rssi[j];
+                } else if (dir[j] == 0x01) {
+                    addr[j] = 1;
+                    rssiRcv[0] = rssi[j];
+                } else if (dir[j] == 0x02) {
+                    addr[j] = 3;
+                    rssiRcv[2] = rssi[j];
+                } else if (dir[j] == -126 && dirAux[j] == 104) {
+                    addr[j] = 2;
+                    rssiRcv[1] = rssi[j];
+                }
+                //printf("Dir: %02x RSSI: %d\r\n", addr[j], rssi[j]);
+            }
 
-    printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
-           params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
-           params->rssi, params->isScanResponse, params->type);
-#if DUMP_ADV_DATA
-    for (unsigned index = 0; index < params->advertisingDataLen; index++) {
-        printf("%02x ", params->advertisingData[index]);
+            i=0;
+            for (int u = 0; u < nBeacons; u++) {
+                //printf("RSSI: %d\r\n", rssiRcv[u]);
+                w[u] = 0;
+                if (rssiRcv[u] != 0) {
+                    //c++;
+                    switch (u) {
+                        case 0: // 82
+                            d[0] = dZero*pow((float)10, ((pdBZero-rssiRcv[0])/(10*n)));
+                            w[0] = pow(d[0], (float)(k*-1));
+                            //printf("BLE 1, RSSI: %d, d: %f, w: %f\r\n", rssiRcv[0], d[0], w[0]);
+                            break;
+                        case 1: // 1
+                            d[1] = dZero*pow((float)10, ((pdBZero-rssiRcv[1])/(10*n)));
+                            w[1] = pow(d[1], (float)(k*-1));
+                            //printf("BLE 2, RSSI: %d, d: %f, w: %f\r\n", rssiRcv[1], d[1], w[1]);
+                            break;
+                        case 2: //2
+                            d[2] = dZero*pow((float)10, ((pdBZero-rssiRcv[2])/(10*n)));
+                            w[2] = pow(d[2], (float)(k*-1));
+                            //printf("BLE 3, RSSI: %d, d: %f, w: %f\r\n", rssiRcv[2], d[2], w[2]);
+                            break;
+                        case 3: //68
+                            d[3] = dZero*pow((float)10, ((pdBZero-rssiRcv[3])/(10*n)));
+                            w[3] = pow(d[3], (float)(k*-1));
+                            //printf("BLE 4, RSSI: %d, d: %f, w: %f\r\n", rssiRcv[3], d[3], w[3]);
+                            break;
+                    }
+                }
+
+                rssiRcv[u] = 0;
+            }
+
+            getCoord(w, 4);
+        }
     }
-    printf("\r\n");
-#endif /* DUMP_ADV_DATA */
 }
 
+        /*
+
+        #if DUMP_ADV_DATA
+            for (unsigned index = 0; index < params->advertisingDataLen; index++) {
+                printf("%02x ", params->advertisingData[index]);
+            }
+            printf("\r\n");
+        #endif */ /* DUMP_ADV_DATA */
+
+/* 
+    Weighted Centroid Localization (WCL) Algorithm
+*/
+void getCoord (float weigth[], int d)
+{
+    int i, j;
+    float nCoord[4][2] = {0};
+    float c[2] = {0};
+    float wAuxSum = 0;
+    State = TX;
+    for (i=0; i<4; i++)
+    {
+        wAuxSum += weigth[i];
+        for (j=0; j<2; j++) {
+            nCoord[i][j] = weigth[i]*coord[i][j];
+            }
+    }
+    
+    for (i=0; i<2; i++) {
+        for (j=0; j<4; j++)
+            c[i] += nCoord[j][i]/wAuxSum;
+    }
+
+    printf("Coordenadas: (%f, %f)\r\n", c[0], c[1]);
+    sprintf((char*) Buffer, "Coordenadas: (%f, %f) No.%d", c[0], c[1], cPqt);  
+    cPqt++;
+    Radio.Send( Buffer, BufferSize );
+    State = LOWPOWER;
+}
+
+
+
 /**
  * This function is called when the ble initialization process has failed
  */
@@ -64,19 +248,105 @@
     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
         return;
     }
- 
-    ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */);
+
+    ble.gap().setScanParams(200 /* scan interval */, 200 /* scan window */);
     ble.gap().startScan(advertisementCallback);
 }
 
 int main(void)
 {
+    // Configuración del radio LoRa
+    bool isMaster = true;
+    // Initialize Radio driver
+    RadioEvents.TxDone = OnTxDone;
+    RadioEvents.RxDone = OnRxDone;
+    RadioEvents.RxError = OnRxError;
+    RadioEvents.TxTimeout = OnTxTimeout;
+    RadioEvents.RxTimeout = OnRxTimeout;
+    RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
+    Radio.Init( &RadioEvents );
     ticker.attach(periodicCallback, 1);
 
+    Radio.SetChannel( HoppingFrequencies[0] ); 
+    
+    Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
+                       LORA_SPREADING_FACTOR, LORA_CODINGRATE,
+                       LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
+                       LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
+                       LORA_IQ_INVERSION_ON, 4000000 );
+
+    Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
+                       LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
+                       LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
+                       LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
+                       LORA_IQ_INVERSION_ON, true );
+
     BLE &ble = BLE::Instance();
     ble.init(bleInitComplete);
 
+    pc.baud(9600);
+    printf("Iniciando BLE Observer\r\n");
+    
+    //Transmisión de prueba LOL
+    const uint8_t PingMsg[] = "TxRx";
+    strcpy( ( char* )Buffer, ( char* )PingMsg );     
+    Radio.Send( Buffer, BufferSize );
+    
+    wait(15);
+
     while (true) {
         ble.waitForEvent();
     }
 }
+
+
+void OnTxDone( void )
+{
+    Radio.SetChannel( HoppingFrequencies[0] );
+    Radio.Sleep( );
+    State = TX;
+    //debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
+}
+
+void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
+{
+    Radio.SetChannel( HoppingFrequencies[0] );
+    Radio.Sleep( );
+    BufferSize = size;
+    memcpy( Buffer, payload, BufferSize );
+    RssiValue = rssi;
+    SnrValue = snr;
+    State = RX;
+    //debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
+}
+
+void OnTxTimeout( void )
+{
+    Radio.SetChannel( HoppingFrequencies[0] );
+    Radio.Sleep( );
+    State = TX_TIMEOUT;
+    //debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
+}
+
+void OnRxTimeout( void )
+{
+    Radio.SetChannel( HoppingFrequencies[0] );
+    Radio.Sleep( );
+    Buffer[ BufferSize ] = 0;
+    State = RX_TIMEOUT;
+    //debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
+}
+
+void OnRxError( void )
+{
+    Radio.SetChannel( HoppingFrequencies[0] );
+    Radio.Sleep( );
+    State = RX_ERROR;
+    //debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
+}
+
+void OnFhssChangeChannel( uint8_t channelIndex )
+{
+    Radio.SetChannel( HoppingFrequencies[channelIndex] );
+    //debug_if( DEBUG_MESSAGE, "F%d-", channelIndex);
+}
\ No newline at end of file
diff -r 88f50499af9a -r 3b30027c7e8f main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Wed Nov 02 20:53:30 2016 +0000
@@ -0,0 +1,115 @@
+/*
+ / _____)             _              | |
+( (____  _____ ____ _| |_ _____  ____| |__
+ \____ \| ___ |    (_   _) ___ |/ ___)  _ \
+ _____) ) ____| | | || |_| ____( (___| | | |
+(______/|_____)_|_|_| \__)_____)\____)_| |_|
+    ( C )2014 Semtech
+
+Description: Contains the callbacks for the IRQs and any application related details
+
+License: Revised BSD License, see LICENSE.TXT file include in the project
+
+Maintainer: Miguel Luis and Gregory Cristian
+*/
+#ifndef __MAIN_H__
+#define __MAIN_H__
+
+
+/*!
+ * Frequency hopping frequencies table
+ */
+const uint32_t HoppingFrequencies[] =
+{
+    916500000,
+    923500000,
+    906500000,
+    917500000,
+    917500000,
+    909000000,
+    903000000,
+    916000000,
+    912500000,
+    926000000,
+    925000000,
+    909500000,
+    913000000,
+    918500000,
+    918500000,
+    902500000,
+    911500000,
+    926500000,
+    902500000,
+    922000000,
+    924000000,
+    903500000,
+    913000000,
+    922000000,
+    926000000,
+    910000000,
+    920000000,
+    922500000,
+    911000000,
+    922000000,
+    909500000,
+    926000000,
+    922000000,
+    918000000,
+    925500000,
+    908000000,
+    917500000,
+    926500000,
+    908500000,
+    916000000,
+    905500000,
+    916000000,
+    903000000,
+    905000000,
+    915000000,
+    913000000,
+    907000000,
+    910000000,
+    926500000,
+    925500000,
+    911000000
+};
+
+/*
+ * Callback functions prototypes
+ */
+/*!
+ * @brief Function to be executed on Radio Tx Done event
+ */
+void OnTxDone( void );
+
+/*!
+ * @brief Function to be executed on Radio Rx Done event
+ */
+void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
+
+/*!
+ * @brief Function executed on Radio Tx Timeout event
+ */
+void OnTxTimeout( void );
+
+/*!
+ * @brief Function executed on Radio Rx Timeout event
+ */
+void OnRxTimeout( void );
+
+/*!
+ * @brief Function executed on Radio Rx Error event
+ */
+void OnRxError( void );
+
+/*!
+ * @brief Function executed on Radio Fhss Change Channel event
+ */
+void OnFhssChangeChannel( uint8_t channelIndex );
+
+/*!
+ * @brief Function executed on CAD Done event
+ */
+void OnCadDone( void );
+
+#endif // __MAIN_H__
\ No newline at end of file
diff -r 88f50499af9a -r 3b30027c7e8f nRF51822.lib
--- a/nRF51822.lib	Tue Jan 12 11:00:02 2016 +0000
+++ b/nRF51822.lib	Wed Nov 02 20:53:30 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#3cc0718d98d0
+https://developer.mbed.org/users/Javier117/code/nRF51822/#86bab11e2a7f