LP Long Distance IR Vision Robot

Dependencies:   max77650_charger_sample BufferedSerial SX1276GenericLib Adafruit-MotorShield NEO-6m-GPS MAX17055_EZconfig Adafruit_GFX USBDeviceHT Adafruit-PWM-Servo-Driver

Files at this revision

API Documentation at this revision

Comitter:
dev_alexander
Date:
Tue Jul 24 16:58:04 2018 +0000
Parent:
29:f7a0e49b826b
Child:
31:f15747cffb20
Commit message:
Changed the way that the program handles the ble connection. Converted functions that were deprecated to function correctly using the new ble methods of a newer version of mbed-os.

Changed in this revision

UARTService_custom.h 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
--- a/UARTService_custom.h	Tue Jul 24 00:38:18 2018 +0000
+++ b/UARTService_custom.h	Tue Jul 24 16:58:04 2018 +0000
@@ -67,8 +67,8 @@
         GattCharacteristic *charTable[] = {&txCharacteristic, &rxCharacteristic};
         GattService         uartService(UARTServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
 
-        ble.addService(uartService);
-        ble.onDataWritten(this, &UARTService::onDataWritten);
+        ble.gattServer().addService(uartService);
+        ble.gattServer().onDataWritten(this, &UARTService::onDataWritten);
     }
 
     /**
@@ -106,7 +106,7 @@
         size_t         origLength = length;
         const uint8_t *buffer     = static_cast<const uint8_t *>(_buffer);
 
-        if (ble.getGapState().connected) {
+        if (ble.gap().getState().connected) {
             unsigned bufferIndex = 0;
             while (length) {
                 unsigned bytesRemainingInSendBuffer = BLE_UART_SERVICE_MAX_DATA_LEN - sendBufferIndex;
--- a/main.cpp	Tue Jul 24 00:38:18 2018 +0000
+++ b/main.cpp	Tue Jul 24 16:58:04 2018 +0000
@@ -9,10 +9,11 @@
  * https://www.hackster.io/DevinAlex64/getting-started-with-the-max32620fthr-and-lora-f9d8dd\
  */
  
- #include "main.h"
- #include "global_buffers.h"
- #include "GridEye.h"
+#include "main.h"
+#include "global_buffers.h"
+#include "GridEye.h"
 
+#define COMPILE_BLE
 
 /* If the board that is compiled is the master device (BLE enabled MAX32630FTHR),
  * then it needs this library and needs to be configed in order for the device to 
@@ -22,13 +23,122 @@
  * additional setup in the main program.
  */ 
 #if defined(TARGET_MAX32630FTHR) // using the RFM95 board
-    //#include "UARTService_custom.h"
-    //#include "ble/BLE.h"
-    //#include "ble/Gap.h"
     #include "max32630fthr.h"
     MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
 #endif
 
+#if defined(COMPILE_BLE)
+    /* BLE Related MAX32630FTHR Specific stuff */
+    #include "ble/BLE.h"
+    //#include "ble/Gap.h"
+    #include "UARTService_custom.h"
+    #include <events/mbed_events.h>
+    
+    DigitalOut led2(LED2);
+    //BLE &ble = BLE::Instance();
+    UARTService *uart;
+    
+    #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
+                                   * it will have an impact on code-size and power consumption. */
+ 
+    #if NEED_CONSOLE_OUTPUT
+    #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
+    #define READ(STR) { if (uart) uart->read(STR, strlen(STR)); }
+    
+    #else
+    #define DEBUG(...) /* nothing */
+    #endif /* #if NEED_CONSOLE_OUTPUT */
+    
+    //Triggered when the robot is disconnected from the mobile app.
+    void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+    {
+        BLE &ble = BLE::Instance();
+        DEBUG("Disconnected!\n\r");
+        DEBUG("Restarting the advertising process\n\r");
+        ble.gap().startAdvertising();
+    }
+    
+    /* This pointer is needed to reference data in the main program when filling 
+     * with data from the BLE 
+     */
+    uint8_t * onDataWritten_curr_ble_to_slave;
+    
+    // Triggered when there is data written
+    void onDataWritten(const GattWriteCallbackParams *params)
+    {    
+        //LED1 will be off when button is pressed, and on when button is released.
+        led2 = !led2;
+        uint8_t button_state;
+        uint8_t direction;
+        /*Set up constants for direction of motion */
+    //    const char FRONT = '5';
+    //    const char BACK = '6'; 
+    //    const char LEFT = '7';
+    //    const char RIGHT = '8';
+        
+        /*Set up constants for button state */
+    //    const char PRESSED = '1';
+    //    const char RELEASED = '0';
+        
+        /* If there is a valid data sent by the mobile app */
+        if ((uart != NULL) && (params->handle == uart ->getTXCharacteristicHandle())) {
+            const uint8_t *packet = params->data;
+            button_state = packet[3];
+            direction = packet[2];
+    
+            // make parameter to send over Lora        
+            onDataWritten_curr_ble_to_slave[0] = direction;
+            onDataWritten_curr_ble_to_slave[1] = button_state;
+            
+            dprintf("direction: %d\n", direction);
+            dprintf("button_state: %d\n", button_state);      
+    
+            /*
+            switch (button_state)
+            {
+                case PRESSED:
+                    md_left_pwm = static_cast<float>(20/100.0F);
+                    md_right_pwm = static_cast<float>(20/100.0F);
+                    
+                    switch (direction)
+                    {
+                        case FRONT:
+                            md_left_dir = 1;
+                            md_left_pwm = 0.7;
+                            md_right_dir = 1;
+                            md_right_pwm = 0.7;
+                            break;
+                        case BACK:
+                            md_left_dir = 0;
+                            md_left_pwm = 0.7;
+                            md_right_dir = 0;
+                            md_right_pwm = 0.7;
+                            break;
+                        case LEFT:
+                            md_left_dir = 0;
+                            md_left_pwm = 0.5;
+                            md_right_dir = 1;
+                            md_right_pwm = 0.5;
+                            break;
+                        case RIGHT:
+                            md_left_dir = 1;
+                            md_left_pwm = 0.5;
+                            md_right_dir = 0;
+                            md_right_pwm = 0.5;
+                            break;
+                    }      
+                    break;
+                case RELEASED:
+                    md_left_pwm = 0;
+                    md_right_pwm = 0;
+                    break;
+            }
+            */
+    
+        }
+    }
+#endif
+
 DigitalOut myled(LED);
 
 Serial pc(USBTX, USBRX);
@@ -97,6 +207,30 @@
         uint8_t prev_ble_from_master[size_of_ble];
     #endif
     
+    #if defined(COMPILE_BLE) 
+    
+        BLE &ble = BLE::Instance();
+        /* Create alias for the BLE data to be saved in function above that 
+         * writes data to this buffer when function onDataWritten is called 
+         */
+        onDataWritten_curr_ble_to_slave = curr_ble_to_slave;
+        
+        /* Initialize BLE */
+        ble.init();
+        ble.gap().onDisconnection(disconnectionCallback);
+        ble.gattServer().onDataWritten(onDataWritten);
+        uart = new UARTService(ble);
+        /* setup advertising */
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+        ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                         (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                         (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+     
+        ble.gap().setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+        ble.gap().startAdvertising();
+    #endif
     /***************************************************************************
      * Grid Eye Sensor Data Buffers
      **************************************************************************/
@@ -148,6 +282,17 @@
     while(1) 
     {        
         /***************************************************************************
+         * BLE Radio Data 
+         **************************************************************************/
+        #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
+            #if defined(COMPILE_BLE) 
+            ble.waitForEvent();
+            #endif
+        #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
+            // NOTHING YET
+        #endif
+        
+        /***************************************************************************
          * Grid Eye Sensor 
          **************************************************************************/
         #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
@@ -212,8 +357,7 @@
             //}
             */
             memcpy(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master));
-        #endif
-        
+        #endif        
         
         /***************************************************************************
          * Fill Payload Buffer With Data From Sensors for LoRa Transmition