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

Revision:
31:f15747cffb20
Parent:
30:66f9750cc44c
Child:
32:b108ed6096b0
--- a/main.cpp	Tue Jul 24 16:58:04 2018 +0000
+++ b/main.cpp	Tue Jul 24 18:30:26 2018 +0000
@@ -12,8 +12,7 @@
 #include "main.h"
 #include "global_buffers.h"
 #include "GridEye.h"
-
-#define COMPILE_BLE
+#include "GPS.h"
 
 /* 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,121 +21,127 @@
  * This is only needed for the MAX32630FTHR. The MAX325620FTHR is exampt from this 
  * additional setup in the main program.
  */ 
-#if defined(TARGET_MAX32630FTHR) // using the RFM95 board
-    #include "max32630fthr.h"
-    MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
+ 
+#if defined(TARGET_MAX32620FTHR)
+    GPS gps(P0_1, P0_0, 115200);
 #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';
+#if defined(TARGET_MAX32630FTHR) 
+    #include "max32630fthr.h"
+    MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
+
+    #define COMPILE_BLE
+
+    #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);
+        UARTService *uart;
         
-        /*Set up constants for button state */
-    //    const char PRESSED = '1';
-    //    const char RELEASED = '0';
+        #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;
         
-        /* 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;
+        // 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';
             
-            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;
+            /* 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
 #endif
 
 DigitalOut myled(LED);
@@ -201,36 +206,34 @@
      * BLE Data Buffers
      **************************************************************************/
     #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
-        uint8_t curr_ble_to_slave[size_of_ble];
+        uint8_t curr_ble_to_slave[size_of_ble]; 
+        #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
     #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
         uint8_t curr_ble_from_master[size_of_ble];
         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
      **************************************************************************/
@@ -357,7 +360,22 @@
             //}
             */
             memcpy(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master));
-        #endif        
+        #endif      
+        
+        /***************************************************************************
+         * GPS Sensor 
+         **************************************************************************/
+        #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
+            //None yet
+        #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
+            // The testing hasnt been done yet for the GPS Module. Will do this when more parts come in. 
+            /*
+            if(gps.sample()){
+              pc.printf("%f\t%c\t%f\t%c\t%f\t%f\t%f\n\r",gps.longitude, gps.ns,gps.latitude,gps.ew, gps.alt, gps.geoid, gps.time);
+              pc.printf("%d:%d:%d",gps.hour,gps.minute,gps.seconed);
+            }
+            */
+        #endif
         
         /***************************************************************************
          * Fill Payload Buffer With Data From Sensors for LoRa Transmition