This is example code that can get you started with building your own IR vision robot that communicates over LoRa

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

Fork of MAX326xxFTHR_LoRa_Example_test by Devin Alexander

main.cpp

Committer:
dev_alexander
Date:
2018-07-20
Revision:
27:6b549f838f0a
Parent:
26:69aba05f010f
Child:
28:0ed92c590607

File content as of revision 27:6b549f838f0a:

/*
 * Copyright (c) 2018 HELIOS Software GmbH
 * 30826 Garbsen (Hannover) Germany
 * Licensed under the Apache License, Version 2.0);
 */
  
 /*
 * For Wiring Instructions, please visit the link below:
 * https://www.hackster.io/DevinAlex64/getting-started-with-the-max32620fthr-and-lora-f9d8dd\
 */
 
 #include "main.h"
 #include "global_buffers.h"
 #include "GridEye.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 
 * work with a 3.3volt output instead of a 1.8 volt output. 
 * 
 * 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);
 #endif

DigitalOut myled(LED);

//Serial pc(USBTX, USBRX);


#if   defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway
    /* No grid eye object needed. The function the master uses to convert the raw 
     * data that is sent over from the slave deevice that contains the data from the
     * actual grid eye sensor is actually a function that is not part of the grid eye
     * class. this is due to the fact that the grid eye class requires n i2c bus to 
     * be assigned to a phyiscal sensor. In this case, the library for the Grid Eye 
     * sensor has a support functin that is used to convert data that is aquired from 
     * the grid eye sensor. So it is not supposed to be a class specific function.
    */
#elif defined(TARGET_MAX32620FTHR) // Slave Device: Robot
    //Init I2C communications in default I2C bus I2C #1
    I2C i2cGridEye(P3_4,P3_5);
    GridEye gridEye(i2cGridEye);
#endif


/***************************************************************************
 * main()
 **************************************************************************/
int main() 
{
    /*
     * inits the Serial or USBSerial when available (230400 baud).
     * If the serial uart is not is not connected it swiches to USB Serial
     * blinking LED means USBSerial detected, waiting for a connect.
     * It waits up to 30 seconds for a USB terminal connections 
     */
    InitSerial(30*1000, &myled);
    dprintf("Welcome to the SX1276GenericLib");
  
    dprintf("Starting a simple LoRa PingPong");
    
    /* Setup begins here: */
    #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
        dprintf("MAX32630FTHR: Master");
    #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
        dprintf("MAX32620FTHR: Slave");
    #endif

/***************************************************************************
 * Lora Communication Buffers
 **************************************************************************/
    /* Create Buffers to store both incoming and outgoing LoRa communications */
    uint8_t BufferTx[BufferSizeTx];
    uint8_t BufferRx[BufferSizeRx];
    
/***************************************************************************
 * BLE Data Buffers
 **************************************************************************/
    #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
        uint8_t curr_ble_data_to_slave[size_of_ble];
    #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
        uint8_t curr_ble_data_from_master[size_of_ble];
        uint8_t prev_ble_data_from_master[size_of_ble];
    #endif
/***************************************************************************
 * Grid Eye Sensor Data Buffers
 **************************************************************************/
    #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
        char curr_raw_frame_data_from_slave[size_of_grid_eye];
        char prev_raw_frame_data_from_slave[size_of_grid_eye];
        int16_t conv_frame_data_from_slave[64];
    #elif defined(TARGET_MAX32620FTHR) // Client Device: LoRa Controlled Robot
        char curr_raw_frame_data_to_master[size_of_grid_eye];
        char prev_raw_frame_data_to_master[size_of_grid_eye];
        int16_t conv_frame_data_to_master[64];
    #endif
/***************************************************************************
 * GPS Data Buffers
 **************************************************************************/
    #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
        uint8_t curr_gps_data_from_slave[size_of_gps];
        uint8_t prev_gps_data_from_slave[size_of_gps];
    #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
        uint8_t curr_gps_data_to_master[size_of_gps];
    #endif
/***************************************************************************
 * MAX17055 Data Buffers
 **************************************************************************/
    #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
        uint8_t curr_MAX17055_from_slave[size_of_MAX17055];
        uint8_t prev_MAX17055_from_slave[size_of_MAX17055];
    #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
        uint8_t curr_MAX17055_to_master[size_of_MAX17055];
    #endif
    
/***************************************************************************
 * MAX77650 Data Buffers
 **************************************************************************/
    #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
        uint8_t curr_MAX77650_from_slave[size_of_MAX77650];
        uint8_t prev_MAX77650_from_slave[size_of_MAX77650];
    #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
        uint8_t curr_MAX77650_to_master[size_of_MAX77650];
    #endif
/***************************************************************************
 * Store Aliases to the buffers created here in main porgram
 **************************************************************************/
#if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
createAliasesForGlobalBufs(BufferTx,
                           BufferRx, 
                           curr_ble_data_to_slave,
                           curr_raw_frame_data_from_slave,
                           curr_gps_data_from_slave,
                           curr_MAX17055_from_slave,
                           curr_MAX77650_from_slave
                           );
#elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
createAliasesForGlobalBufs(BufferTx, 
                           BufferRx, 
                           curr_ble_data_from_master,
                           curr_raw_frame_data_to_master, 
                           curr_gps_data_to_master,
                           curr_MAX17055_to_master,
                           curr_MAX77650_to_master
                           );
#endif
/***************************************************************************
 * Continue with Finishing Setup by Calling other functions
 **************************************************************************/
    
    
    /* Pass in pointers to the two buffers for LoRa communication to the sx1276 
    `* Setup so that the LoRa communications know where data needs to be both
     * stored to in a case of Receiving or where to find data in case of a 
     * Transmistion occuring.
     */
    SX1276PingPongSetup(BufferTx, BufferRx);
    
    /***************************************************************************
     * Grid Eye Sensor: Non-Buffer Program Variables
     **************************************************************************/
    #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
        int frame_idx = 0;
    #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
        // none yet
        int frame_idx = 0;
        char local_curr_raw_frame_data_to_master[size_of_grid_eye];
        char local_prev_raw_frame_data_to_master[size_of_grid_eye];
        int16_t local_conv_frame_data_to_master[64];
    #endif
    
    
    while(1) 
    {
        /***************************************************************************
         * Grid Eye Sensor 
         **************************************************************************/
        #if   defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
            /*
            //Check to see if the contents of the previous scan are the same. If they are different then continue with converting
            if( memcmp(prev_raw_frame_data_from_slave, curr_raw_frame_data_from_slave, sizeof(curr_raw_frame_data_from_slave)) != 0 )
            {
                // Convert raw data sent from slave to a 16 bit integer array by calling this
                convRaw8x8Data2Point25degC(curr_raw_frame_data_from_slave, conv_frame_data_from_slave);
                
                wait_ms(10);
                
                // Next Print off the Converted data
                pc.printf("\r\nFrame %d data: \r\n", frame_idx);
                uint8_t idx;
                float pixel_data;
                for (int y = 0; y < 8; y++) {
                    for (int x = 0; x < 8; x++) {
                        idx = y*8 + x;
                        pixel_data = conv_frame_data_from_slave[idx]/4.0;
                        pc.printf("%.2f  \t", pixel_data);
                    }
                    pc.printf("\r\n\r\n");
                }
                pc.printf("\r\n");
                
                // Increment frame counter
                frame_idx = frame_idx +1;
            }
            */
            /* Next copy in data received from current data into buffer used for
             * comparison next time the memcmp above is called. This prevents the 
             * program from converting the same raw data aquired by the grid eye
             * sensor on the slave device to the floating point array with the 
             * 0.25 degrees celsius precision level when there is not new data. 
             */
            //memcpy(prev_raw_frame_data_from_slave, curr_raw_frame_data_from_slave, sizeof(curr_raw_frame_data_from_slave));
            
        #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
            // Aquire raw data about 8x8 frame from the grid eye sensor in this function call 
            gridEye.getRaw8x8FrameData(local_curr_raw_frame_data_to_master);
            wait_ms( 10 );  
/*            
            if ( memcmp(prev_raw_frame_data_to_master, curr_raw_frame_data_to_master, sizeof(curr_raw_frame_data_to_master)) != 0 )
            {
                // Convert raw data sent from slave to a 16 bit integer array by calling this
                convRaw8x8Data2Point25degC(local_curr_raw_frame_data_to_master, local_conv_frame_data_to_master);
                
                wait_ms(10);
                
                // Next Print off the Converted data
                pc.printf("\r\nFrame %d data: \r\n", frame_idx);
                uint8_t idx;
                float pixel_data;
                for (int y = 0; y < 8; y++) {
                    for (int x = 0; x < 8; x++) {
                        idx = y*8 + x;
                        pixel_data = local_conv_frame_data_to_master[idx]/4.0;
                        pc.printf("%.2f  \t", pixel_data);
                    }
                    pc.printf("\r\n\r\n");
                }
                pc.printf("\r\n");
                
                // Increment frame counter
                frame_idx = frame_idx +1;
            }
*/          
            
            memcpy(curr_raw_frame_data_to_master, local_curr_raw_frame_data_to_master, sizeof(curr_raw_frame_data_to_master));
            memcpy(local_prev_raw_frame_data_to_master, local_curr_raw_frame_data_to_master, sizeof(local_curr_raw_frame_data_to_master));
        #endif

        /***************************************************************************
        * Lora Communications
        **************************************************************************/

        wait_ms( 10 );  
        SX1276PingPong();
    
        
        
        
        
    }
    
}