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

Committer:
dev_alexander
Date:
Mon Jul 23 23:39:35 2018 +0000
Revision:
28:0ed92c590607
Parent:
27:6b549f838f0a
Child:
29:f7a0e49b826b
Reworked how the buffers are loaded and unloaded. Now prints off data received from the Slave on the Master. This is a stable release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:c43b6919ae15 1 /*
Helmut64 17:98f2528e8399 2 * Copyright (c) 2018 HELIOS Software GmbH
Helmut64 0:c43b6919ae15 3 * 30826 Garbsen (Hannover) Germany
Helmut64 0:c43b6919ae15 4 * Licensed under the Apache License, Version 2.0);
Helmut64 0:c43b6919ae15 5 */
dev_alexander 21:1b92cabe8a3b 6
dev_alexander 21:1b92cabe8a3b 7 /*
dev_alexander 21:1b92cabe8a3b 8 * For Wiring Instructions, please visit the link below:
dev_alexander 21:1b92cabe8a3b 9 * https://www.hackster.io/DevinAlex64/getting-started-with-the-max32620fthr-and-lora-f9d8dd\
dev_alexander 21:1b92cabe8a3b 10 */
dev_alexander 21:1b92cabe8a3b 11
Helmut64 0:c43b6919ae15 12 #include "main.h"
dev_alexander 23:f74a50977593 13 #include "global_buffers.h"
dev_alexander 23:f74a50977593 14 #include "GridEye.h"
Helmut64 0:c43b6919ae15 15
dev_alexander 23:f74a50977593 16
dev_alexander 23:f74a50977593 17 /* If the board that is compiled is the master device (BLE enabled MAX32630FTHR),
dev_alexander 23:f74a50977593 18 * then it needs this library and needs to be configed in order for the device to
dev_alexander 23:f74a50977593 19 * work with a 3.3volt output instead of a 1.8 volt output.
dev_alexander 23:f74a50977593 20 *
dev_alexander 23:f74a50977593 21 * This is only needed for the MAX32630FTHR. The MAX325620FTHR is exampt from this
dev_alexander 23:f74a50977593 22 * additional setup in the main program.
dev_alexander 23:f74a50977593 23 */
dev_alexander 19:9f035b9e65ec 24 #if defined(TARGET_MAX32630FTHR) // using the RFM95 board
dev_alexander 19:9f035b9e65ec 25 #include "max32630fthr.h"
dev_alexander 19:9f035b9e65ec 26 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
dev_alexander 19:9f035b9e65ec 27 #endif
Helmut64 0:c43b6919ae15 28
Helmut64 17:98f2528e8399 29 DigitalOut myled(LED);
Helmut64 0:c43b6919ae15 30
dev_alexander 28:0ed92c590607 31 Serial pc(USBTX, USBRX);
dev_alexander 25:1a031add188a 32
Helmut64 0:c43b6919ae15 33
dev_alexander 23:f74a50977593 34 #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway
dev_alexander 23:f74a50977593 35 /* No grid eye object needed. The function the master uses to convert the raw
dev_alexander 23:f74a50977593 36 * data that is sent over from the slave deevice that contains the data from the
dev_alexander 23:f74a50977593 37 * actual grid eye sensor is actually a function that is not part of the grid eye
dev_alexander 23:f74a50977593 38 * class. this is due to the fact that the grid eye class requires n i2c bus to
dev_alexander 23:f74a50977593 39 * be assigned to a phyiscal sensor. In this case, the library for the Grid Eye
dev_alexander 23:f74a50977593 40 * sensor has a support functin that is used to convert data that is aquired from
dev_alexander 23:f74a50977593 41 * the grid eye sensor. So it is not supposed to be a class specific function.
dev_alexander 23:f74a50977593 42 */
dev_alexander 23:f74a50977593 43 #elif defined(TARGET_MAX32620FTHR) // Slave Device: Robot
dev_alexander 23:f74a50977593 44 //Init I2C communications in default I2C bus I2C #1
dev_alexander 23:f74a50977593 45 I2C i2cGridEye(P3_4,P3_5);
dev_alexander 23:f74a50977593 46 GridEye gridEye(i2cGridEye);
dev_alexander 23:f74a50977593 47 #endif
dev_alexander 19:9f035b9e65ec 48
dev_alexander 23:f74a50977593 49 int main()
dev_alexander 23:f74a50977593 50 {
Helmut64 17:98f2528e8399 51 /*
Helmut64 17:98f2528e8399 52 * inits the Serial or USBSerial when available (230400 baud).
Helmut64 17:98f2528e8399 53 * If the serial uart is not is not connected it swiches to USB Serial
Helmut64 17:98f2528e8399 54 * blinking LED means USBSerial detected, waiting for a connect.
Helmut64 17:98f2528e8399 55 * It waits up to 30 seconds for a USB terminal connections
Helmut64 17:98f2528e8399 56 */
Helmut64 17:98f2528e8399 57 InitSerial(30*1000, &myled);
Helmut64 17:98f2528e8399 58 dprintf("Welcome to the SX1276GenericLib");
Helmut64 18:d5527ce82e6b 59
Helmut64 17:98f2528e8399 60 dprintf("Starting a simple LoRa PingPong");
dev_alexander 23:f74a50977593 61
dev_alexander 23:f74a50977593 62 /* Setup begins here: */
dev_alexander 25:1a031add188a 63 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 25:1a031add188a 64 dprintf("MAX32630FTHR: Master");
dev_alexander 25:1a031add188a 65 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 25:1a031add188a 66 dprintf("MAX32620FTHR: Slave");
dev_alexander 25:1a031add188a 67 #endif
dev_alexander 26:69aba05f010f 68
dev_alexander 23:f74a50977593 69
dev_alexander 23:f74a50977593 70 /***************************************************************************
dev_alexander 23:f74a50977593 71 * Grid Eye Sensor: Non-Buffer Program Variables
dev_alexander 23:f74a50977593 72 **************************************************************************/
dev_alexander 23:f74a50977593 73 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 23:f74a50977593 74 int frame_idx = 0;
dev_alexander 24:e8d03912f303 75 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 23:f74a50977593 76 // none yet
dev_alexander 25:1a031add188a 77 int frame_idx = 0;
dev_alexander 23:f74a50977593 78 #endif
dev_alexander 23:f74a50977593 79
dev_alexander 23:f74a50977593 80
dev_alexander 28:0ed92c590607 81 /***************************************************************************
dev_alexander 28:0ed92c590607 82 * Combined Payload Buffers for LoRa Communications
dev_alexander 28:0ed92c590607 83 **************************************************************************/
dev_alexander 28:0ed92c590607 84 uint8_t BufferTx[BufferSizeTx];
dev_alexander 28:0ed92c590607 85 uint8_t BufferRx[BufferSizeRx];
dev_alexander 28:0ed92c590607 86
dev_alexander 28:0ed92c590607 87 /***************************************************************************
dev_alexander 28:0ed92c590607 88 * BLE Data Buffers
dev_alexander 28:0ed92c590607 89 **************************************************************************/
dev_alexander 28:0ed92c590607 90 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 28:0ed92c590607 91 uint8_t curr_ble_to_slave[size_of_ble];
dev_alexander 28:0ed92c590607 92 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 28:0ed92c590607 93 uint8_t curr_ble_from_master[size_of_ble];
dev_alexander 28:0ed92c590607 94 uint8_t prev_ble_from_master[size_of_ble];
dev_alexander 28:0ed92c590607 95 #endif
dev_alexander 28:0ed92c590607 96
dev_alexander 28:0ed92c590607 97 /***************************************************************************
dev_alexander 28:0ed92c590607 98 * Grid Eye Sensor Data Buffers
dev_alexander 28:0ed92c590607 99 **************************************************************************/
dev_alexander 28:0ed92c590607 100 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 28:0ed92c590607 101 char curr_raw_frame_from_slave[size_of_grid_eye];
dev_alexander 28:0ed92c590607 102 char prev_raw_frame_from_slave[size_of_grid_eye];
dev_alexander 28:0ed92c590607 103 int16_t conv_frame_from_slave[64];
dev_alexander 28:0ed92c590607 104 #elif defined(TARGET_MAX32620FTHR) // Client Device: LoRa Controlled Robot
dev_alexander 28:0ed92c590607 105 char curr_raw_frame_to_master[size_of_grid_eye];
dev_alexander 28:0ed92c590607 106 char prev_raw_frame_to_master[size_of_grid_eye];
dev_alexander 28:0ed92c590607 107 int16_t conv_frame_to_master[64];
dev_alexander 28:0ed92c590607 108 #endif
dev_alexander 28:0ed92c590607 109
dev_alexander 28:0ed92c590607 110 /***************************************************************************
dev_alexander 28:0ed92c590607 111 * GPS Data Buffers
dev_alexander 28:0ed92c590607 112 **************************************************************************/
dev_alexander 28:0ed92c590607 113 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 28:0ed92c590607 114 uint8_t curr_gps_from_slave[size_of_gps];
dev_alexander 28:0ed92c590607 115 uint8_t prev_gps_from_slave[size_of_gps];
dev_alexander 28:0ed92c590607 116 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 28:0ed92c590607 117 uint8_t curr_gps_to_master[size_of_gps];
dev_alexander 28:0ed92c590607 118 #endif
dev_alexander 28:0ed92c590607 119
dev_alexander 28:0ed92c590607 120 /***************************************************************************
dev_alexander 28:0ed92c590607 121 * MAX17055 Data Buffers
dev_alexander 28:0ed92c590607 122 **************************************************************************/
dev_alexander 28:0ed92c590607 123 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 28:0ed92c590607 124 uint8_t curr_MAX17055_from_slave[size_of_MAX17055];
dev_alexander 28:0ed92c590607 125 uint8_t prev_MAX17055_from_slave[size_of_MAX17055];
dev_alexander 28:0ed92c590607 126 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 28:0ed92c590607 127 uint8_t curr_MAX17055_to_master[size_of_MAX17055];
dev_alexander 28:0ed92c590607 128 #endif
dev_alexander 28:0ed92c590607 129
dev_alexander 28:0ed92c590607 130 /***************************************************************************
dev_alexander 28:0ed92c590607 131 * MAX77650 Data Buffers
dev_alexander 28:0ed92c590607 132 **************************************************************************/
dev_alexander 28:0ed92c590607 133 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 28:0ed92c590607 134 uint8_t curr_MAX77650_from_slave[size_of_MAX77650];
dev_alexander 28:0ed92c590607 135 uint8_t prev_MAX77650_from_slave[size_of_MAX77650];
dev_alexander 28:0ed92c590607 136 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 28:0ed92c590607 137 uint8_t curr_MAX77650_to_master[size_of_MAX77650];
dev_alexander 28:0ed92c590607 138 #endif
dev_alexander 28:0ed92c590607 139
dev_alexander 28:0ed92c590607 140 /***************************************************************************
dev_alexander 28:0ed92c590607 141 * Finish Setting up LoRa Radios: This passes in pointers to Buffers to send
dev_alexander 28:0ed92c590607 142 **************************************************************************/
dev_alexander 28:0ed92c590607 143 SX1276PingPongSetup(BufferTx, BufferRx);
dev_alexander 28:0ed92c590607 144
dev_alexander 22:abca9d17d13d 145 while(1)
dev_alexander 28:0ed92c590607 146 {
dev_alexander 23:f74a50977593 147 /***************************************************************************
dev_alexander 23:f74a50977593 148 * Grid Eye Sensor
dev_alexander 23:f74a50977593 149 **************************************************************************/
dev_alexander 23:f74a50977593 150 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 28:0ed92c590607 151 // Check to see if the contents of the previous scan are the same. If they are different then continue with converting
dev_alexander 28:0ed92c590607 152 if( memcmp(prev_raw_frame_from_slave, curr_raw_frame_from_slave, sizeof(curr_raw_frame_from_slave)) != 0 )
dev_alexander 27:6b549f838f0a 153 {
dev_alexander 23:f74a50977593 154 // Convert raw data sent from slave to a 16 bit integer array by calling this
dev_alexander 28:0ed92c590607 155 convRaw8x8Data2Point25degC(curr_raw_frame_from_slave, conv_frame_from_slave);
dev_alexander 26:69aba05f010f 156
dev_alexander 23:f74a50977593 157 // Next Print off the Converted data
dev_alexander 26:69aba05f010f 158 pc.printf("\r\nFrame %d data: \r\n", frame_idx);
dev_alexander 23:f74a50977593 159 uint8_t idx;
dev_alexander 23:f74a50977593 160 float pixel_data;
dev_alexander 23:f74a50977593 161 for (int y = 0; y < 8; y++) {
dev_alexander 23:f74a50977593 162 for (int x = 0; x < 8; x++) {
dev_alexander 23:f74a50977593 163 idx = y*8 + x;
dev_alexander 28:0ed92c590607 164 pixel_data = ((float)conv_frame_from_slave[idx])/4.0;
dev_alexander 26:69aba05f010f 165 pc.printf("%.2f \t", pixel_data);
dev_alexander 23:f74a50977593 166 }
dev_alexander 26:69aba05f010f 167 pc.printf("\r\n\r\n");
dev_alexander 23:f74a50977593 168 }
dev_alexander 26:69aba05f010f 169 pc.printf("\r\n");
dev_alexander 23:f74a50977593 170
dev_alexander 23:f74a50977593 171 // Increment frame counter
dev_alexander 23:f74a50977593 172 frame_idx = frame_idx +1;
dev_alexander 27:6b549f838f0a 173 }
dev_alexander 28:0ed92c590607 174
dev_alexander 25:1a031add188a 175 /* Next copy in data received from current data into buffer used for
dev_alexander 25:1a031add188a 176 * comparison next time the memcmp above is called. This prevents the
dev_alexander 25:1a031add188a 177 * program from converting the same raw data aquired by the grid eye
dev_alexander 25:1a031add188a 178 * sensor on the slave device to the floating point array with the
dev_alexander 25:1a031add188a 179 * 0.25 degrees celsius precision level when there is not new data.
dev_alexander 25:1a031add188a 180 */
dev_alexander 28:0ed92c590607 181 memcpy(prev_raw_frame_from_slave, curr_raw_frame_from_slave, sizeof(curr_raw_frame_from_slave));
dev_alexander 25:1a031add188a 182
dev_alexander 24:e8d03912f303 183 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 23:f74a50977593 184 // Aquire raw data about 8x8 frame from the grid eye sensor in this function call
dev_alexander 28:0ed92c590607 185 gridEye.getRaw8x8FrameData(curr_raw_frame_to_master);
dev_alexander 28:0ed92c590607 186 wait_ms( 10 );
dev_alexander 28:0ed92c590607 187 /*
dev_alexander 28:0ed92c590607 188 //if ( memcmp(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master)) != 0 )
dev_alexander 28:0ed92c590607 189 //{
dev_alexander 25:1a031add188a 190 // Convert raw data sent from slave to a 16 bit integer array by calling this
dev_alexander 28:0ed92c590607 191 convRaw8x8Data2Point25degC(curr_raw_frame_to_master, conv_frame_to_master);
dev_alexander 25:1a031add188a 192
dev_alexander 25:1a031add188a 193 // Next Print off the Converted data
dev_alexander 25:1a031add188a 194 pc.printf("\r\nFrame %d data: \r\n", frame_idx);
dev_alexander 25:1a031add188a 195 uint8_t idx;
dev_alexander 25:1a031add188a 196 float pixel_data;
dev_alexander 25:1a031add188a 197 for (int y = 0; y < 8; y++) {
dev_alexander 25:1a031add188a 198 for (int x = 0; x < 8; x++) {
dev_alexander 25:1a031add188a 199 idx = y*8 + x;
dev_alexander 28:0ed92c590607 200 pixel_data = conv_frame_to_master[idx]/4.0;
dev_alexander 25:1a031add188a 201 pc.printf("%.2f \t", pixel_data);
dev_alexander 25:1a031add188a 202 }
dev_alexander 25:1a031add188a 203 pc.printf("\r\n\r\n");
dev_alexander 25:1a031add188a 204 }
dev_alexander 25:1a031add188a 205 pc.printf("\r\n");
dev_alexander 25:1a031add188a 206
dev_alexander 25:1a031add188a 207 // Increment frame counter
dev_alexander 25:1a031add188a 208 frame_idx = frame_idx +1;
dev_alexander 28:0ed92c590607 209 //}
dev_alexander 28:0ed92c590607 210 */
dev_alexander 28:0ed92c590607 211 memcpy(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master));
dev_alexander 23:f74a50977593 212 #endif
dev_alexander 28:0ed92c590607 213
dev_alexander 28:0ed92c590607 214
dev_alexander 23:f74a50977593 215 /***************************************************************************
dev_alexander 28:0ed92c590607 216 * Fill Payload Buffer With Data From Sensors for LoRa Transmition
dev_alexander 28:0ed92c590607 217 **************************************************************************/
dev_alexander 28:0ed92c590607 218 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 28:0ed92c590607 219 memcpy(&BufferTx[tx_idx_signature], PingMsg, size_signature);
dev_alexander 28:0ed92c590607 220 memcpy(&BufferTx[tx_idx_ble], curr_ble_to_slave, size_of_ble);
dev_alexander 28:0ed92c590607 221 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 28:0ed92c590607 222 memcpy(&BufferTx[tx_idx_signature], PongMsg, size_signature);
dev_alexander 28:0ed92c590607 223 memcpy(&BufferTx[tx_idx_grid_eye], curr_raw_frame_to_master, size_of_grid_eye);
dev_alexander 28:0ed92c590607 224 memcpy(&BufferTx[tx_idx_gps], curr_gps_to_master, size_of_gps);
dev_alexander 28:0ed92c590607 225 memcpy(&BufferTx[tx_idx_MAX17055], curr_MAX17055_to_master, size_of_MAX17055);
dev_alexander 28:0ed92c590607 226 memcpy(&BufferTx[tx_idx_MAX77650], curr_MAX77650_to_master, size_of_MAX77650);
dev_alexander 28:0ed92c590607 227 #endif
dev_alexander 28:0ed92c590607 228
dev_alexander 28:0ed92c590607 229
dev_alexander 28:0ed92c590607 230
dev_alexander 28:0ed92c590607 231 /***************************************************************************
dev_alexander 28:0ed92c590607 232 * Lora Communications
dev_alexander 28:0ed92c590607 233 **************************************************************************/
dev_alexander 25:1a031add188a 234 wait_ms( 10 );
dev_alexander 22:abca9d17d13d 235 SX1276PingPong();
dev_alexander 28:0ed92c590607 236
dev_alexander 28:0ed92c590607 237 /***************************************************************************
dev_alexander 28:0ed92c590607 238 * Fill Global Buffers With Data From Received Payload Buffer
dev_alexander 28:0ed92c590607 239 **************************************************************************/
dev_alexander 28:0ed92c590607 240 /* The master and slave devices will have different requirements for offloading payload */
dev_alexander 28:0ed92c590607 241 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 28:0ed92c590607 242 memcpy(curr_raw_frame_from_slave, &BufferRx[rx_idx_grid_eye], size_of_grid_eye);
dev_alexander 28:0ed92c590607 243 memcpy(curr_gps_from_slave, &BufferRx[rx_idx_gps], size_of_gps);
dev_alexander 28:0ed92c590607 244 memcpy(curr_MAX17055_from_slave, &BufferRx[rx_idx_MAX17055], size_of_MAX17055);
dev_alexander 28:0ed92c590607 245 memcpy(curr_MAX77650_from_slave, &BufferRx[rx_idx_MAX77650], size_of_MAX77650);
dev_alexander 28:0ed92c590607 246 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 28:0ed92c590607 247 memcpy(curr_ble_from_master, &BufferRx[rx_idx_ble], size_of_ble);
dev_alexander 28:0ed92c590607 248 #endif
dev_alexander 23:f74a50977593 249
dev_alexander 23:f74a50977593 250
dev_alexander 23:f74a50977593 251
dev_alexander 23:f74a50977593 252
dev_alexander 23:f74a50977593 253
dev_alexander 22:abca9d17d13d 254 }
dev_alexander 22:abca9d17d13d 255
Helmut64 0:c43b6919ae15 256 }