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:
Fri Jul 20 16:31:41 2018 +0000
Revision:
26:69aba05f010f
Parent:
25:1a031add188a
Child:
27:6b549f838f0a
Tried to change how the global buffers are instantiated and where they are to be stored.

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 25:1a031add188a 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 26:69aba05f010f 49
dev_alexander 26:69aba05f010f 50 /***************************************************************************
dev_alexander 26:69aba05f010f 51 * Function Prototypes
dev_alexander 26:69aba05f010f 52 **************************************************************************/
dev_alexander 26:69aba05f010f 53 /**
dev_alexander 26:69aba05f010f 54 * @brief This function constructs a payload buffer that is used in transmitting data in a LoRa Message
dev_alexander 26:69aba05f010f 55 */
dev_alexander 26:69aba05f010f 56 //void fillPayloadWithGlobalBufs(uint8_t * payload_buffer_tx);
dev_alexander 26:69aba05f010f 57
dev_alexander 26:69aba05f010f 58 /**
dev_alexander 26:69aba05f010f 59 * @brief This function deconstructs a payload buffer that contains data from a received LoRa Message
dev_alexander 26:69aba05f010f 60 */
dev_alexander 26:69aba05f010f 61 //void fillGlobalBufsWithPayload(uint8_t * payload_buffer_rx);
dev_alexander 26:69aba05f010f 62
dev_alexander 26:69aba05f010f 63 /***************************************************************************
dev_alexander 26:69aba05f010f 64 * main()
dev_alexander 26:69aba05f010f 65 **************************************************************************/
dev_alexander 23:f74a50977593 66 int main()
dev_alexander 23:f74a50977593 67 {
Helmut64 17:98f2528e8399 68 /*
Helmut64 17:98f2528e8399 69 * inits the Serial or USBSerial when available (230400 baud).
Helmut64 17:98f2528e8399 70 * If the serial uart is not is not connected it swiches to USB Serial
Helmut64 17:98f2528e8399 71 * blinking LED means USBSerial detected, waiting for a connect.
Helmut64 17:98f2528e8399 72 * It waits up to 30 seconds for a USB terminal connections
Helmut64 17:98f2528e8399 73 */
Helmut64 17:98f2528e8399 74 InitSerial(30*1000, &myled);
Helmut64 17:98f2528e8399 75 dprintf("Welcome to the SX1276GenericLib");
Helmut64 18:d5527ce82e6b 76
Helmut64 17:98f2528e8399 77 dprintf("Starting a simple LoRa PingPong");
dev_alexander 23:f74a50977593 78
dev_alexander 23:f74a50977593 79 /* Setup begins here: */
dev_alexander 25:1a031add188a 80 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 25:1a031add188a 81 dprintf("MAX32630FTHR: Master");
dev_alexander 25:1a031add188a 82 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 25:1a031add188a 83 dprintf("MAX32620FTHR: Slave");
dev_alexander 25:1a031add188a 84 #endif
dev_alexander 23:f74a50977593 85
dev_alexander 26:69aba05f010f 86
dev_alexander 26:69aba05f010f 87 /***************************************************************************
dev_alexander 26:69aba05f010f 88 * Lora Communication Buffers
dev_alexander 26:69aba05f010f 89 **************************************************************************/
dev_alexander 26:69aba05f010f 90 /* Create Buffers to store both incoming and outgoing LoRa communications */
dev_alexander 26:69aba05f010f 91 BufferTx = new uint8_t[BufferSizeTx];
dev_alexander 26:69aba05f010f 92 BufferRx = new uint8_t[BufferSizeRx];
dev_alexander 26:69aba05f010f 93
dev_alexander 26:69aba05f010f 94
dev_alexander 26:69aba05f010f 95
dev_alexander 26:69aba05f010f 96 /***************************************************************************
dev_alexander 26:69aba05f010f 97 * Store Aliases to the buffers created here in main porgram
dev_alexander 26:69aba05f010f 98 **************************************************************************/
dev_alexander 26:69aba05f010f 99
dev_alexander 26:69aba05f010f 100 /***************************************************************************
dev_alexander 26:69aba05f010f 101 * Continue with Finishing Setup by Calling other functions
dev_alexander 26:69aba05f010f 102 **************************************************************************/
dev_alexander 26:69aba05f010f 103
dev_alexander 26:69aba05f010f 104
dev_alexander 26:69aba05f010f 105 /* Pass in pointers to the two buffers for LoRa communication to the sx1276
dev_alexander 26:69aba05f010f 106 `* Setup so that the LoRa communications know where data needs to be both
dev_alexander 26:69aba05f010f 107 * stored to in a case of Receiving or where to find data in case of a
dev_alexander 26:69aba05f010f 108 * Transmistion occuring.
dev_alexander 26:69aba05f010f 109 */
dev_alexander 26:69aba05f010f 110 SX1276PingPongSetup(BufferTx, BufferRx);
dev_alexander 23:f74a50977593 111
dev_alexander 23:f74a50977593 112 /***************************************************************************
dev_alexander 23:f74a50977593 113 * Grid Eye Sensor: Non-Buffer Program Variables
dev_alexander 23:f74a50977593 114 **************************************************************************/
dev_alexander 23:f74a50977593 115 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 23:f74a50977593 116 int frame_idx = 0;
dev_alexander 24:e8d03912f303 117 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 23:f74a50977593 118 // none yet
dev_alexander 25:1a031add188a 119 int frame_idx = 0;
dev_alexander 26:69aba05f010f 120 char local_curr_raw_frame_data_to_master[size_of_grid_eye];
dev_alexander 26:69aba05f010f 121 char local_prev_raw_frame_data_to_master[size_of_grid_eye];
dev_alexander 26:69aba05f010f 122 int16_t local_conv_frame_data_to_master[64];
dev_alexander 23:f74a50977593 123 #endif
dev_alexander 23:f74a50977593 124
dev_alexander 23:f74a50977593 125
dev_alexander 22:abca9d17d13d 126 while(1)
dev_alexander 23:f74a50977593 127 {
dev_alexander 23:f74a50977593 128 /***************************************************************************
dev_alexander 23:f74a50977593 129 * Grid Eye Sensor
dev_alexander 23:f74a50977593 130 **************************************************************************/
dev_alexander 23:f74a50977593 131 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 23:f74a50977593 132 // Check to see if the contents of the previous scan are the same. If they are different then continue with converting
dev_alexander 26:69aba05f010f 133 //if( memcmp(prev_raw_frame_data_from_slave, curr_raw_frame_data_from_slave, sizeof(curr_raw_frame_data_from_slave)) != 0 )
dev_alexander 26:69aba05f010f 134 //{
dev_alexander 23:f74a50977593 135 // Convert raw data sent from slave to a 16 bit integer array by calling this
dev_alexander 23:f74a50977593 136 convRaw8x8Data2Point25degC(curr_raw_frame_data_from_slave, conv_frame_data_from_slave);
dev_alexander 23:f74a50977593 137
dev_alexander 26:69aba05f010f 138 wait_ms(10);
dev_alexander 26:69aba05f010f 139
dev_alexander 23:f74a50977593 140 // Next Print off the Converted data
dev_alexander 26:69aba05f010f 141 pc.printf("\r\nFrame %d data: \r\n", frame_idx);
dev_alexander 23:f74a50977593 142 uint8_t idx;
dev_alexander 23:f74a50977593 143 float pixel_data;
dev_alexander 23:f74a50977593 144 for (int y = 0; y < 8; y++) {
dev_alexander 23:f74a50977593 145 for (int x = 0; x < 8; x++) {
dev_alexander 23:f74a50977593 146 idx = y*8 + x;
dev_alexander 23:f74a50977593 147 pixel_data = conv_frame_data_from_slave[idx]/4.0;
dev_alexander 26:69aba05f010f 148 pc.printf("%.2f \t", pixel_data);
dev_alexander 23:f74a50977593 149 }
dev_alexander 26:69aba05f010f 150 pc.printf("\r\n\r\n");
dev_alexander 23:f74a50977593 151 }
dev_alexander 26:69aba05f010f 152 pc.printf("\r\n");
dev_alexander 23:f74a50977593 153
dev_alexander 23:f74a50977593 154 // Increment frame counter
dev_alexander 23:f74a50977593 155 frame_idx = frame_idx +1;
dev_alexander 26:69aba05f010f 156 //}
dev_alexander 25:1a031add188a 157
dev_alexander 25:1a031add188a 158 /* Next copy in data received from current data into buffer used for
dev_alexander 25:1a031add188a 159 * comparison next time the memcmp above is called. This prevents the
dev_alexander 25:1a031add188a 160 * program from converting the same raw data aquired by the grid eye
dev_alexander 25:1a031add188a 161 * sensor on the slave device to the floating point array with the
dev_alexander 25:1a031add188a 162 * 0.25 degrees celsius precision level when there is not new data.
dev_alexander 25:1a031add188a 163 */
dev_alexander 25:1a031add188a 164 memcpy(prev_raw_frame_data_from_slave, curr_raw_frame_data_from_slave, sizeof(curr_raw_frame_data_from_slave));
dev_alexander 25:1a031add188a 165
dev_alexander 24:e8d03912f303 166 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 23:f74a50977593 167 // Aquire raw data about 8x8 frame from the grid eye sensor in this function call
dev_alexander 26:69aba05f010f 168 gridEye.getRaw8x8FrameData(local_curr_raw_frame_data_to_master);
dev_alexander 26:69aba05f010f 169 wait_ms( 10 );
dev_alexander 26:69aba05f010f 170 /*
dev_alexander 26:69aba05f010f 171 if ( memcmp(prev_raw_frame_data_to_master, curr_raw_frame_data_to_master, sizeof(curr_raw_frame_data_to_master)) != 0 )
dev_alexander 26:69aba05f010f 172 {
dev_alexander 25:1a031add188a 173 // Convert raw data sent from slave to a 16 bit integer array by calling this
dev_alexander 26:69aba05f010f 174 convRaw8x8Data2Point25degC(local_curr_raw_frame_data_to_master, local_conv_frame_data_to_master);
dev_alexander 26:69aba05f010f 175
dev_alexander 26:69aba05f010f 176 wait_ms(10);
dev_alexander 25:1a031add188a 177
dev_alexander 25:1a031add188a 178 // Next Print off the Converted data
dev_alexander 25:1a031add188a 179 pc.printf("\r\nFrame %d data: \r\n", frame_idx);
dev_alexander 25:1a031add188a 180 uint8_t idx;
dev_alexander 25:1a031add188a 181 float pixel_data;
dev_alexander 25:1a031add188a 182 for (int y = 0; y < 8; y++) {
dev_alexander 25:1a031add188a 183 for (int x = 0; x < 8; x++) {
dev_alexander 25:1a031add188a 184 idx = y*8 + x;
dev_alexander 26:69aba05f010f 185 pixel_data = local_conv_frame_data_to_master[idx]/4.0;
dev_alexander 25:1a031add188a 186 pc.printf("%.2f \t", pixel_data);
dev_alexander 25:1a031add188a 187 }
dev_alexander 25:1a031add188a 188 pc.printf("\r\n\r\n");
dev_alexander 25:1a031add188a 189 }
dev_alexander 25:1a031add188a 190 pc.printf("\r\n");
dev_alexander 25:1a031add188a 191
dev_alexander 25:1a031add188a 192 // Increment frame counter
dev_alexander 25:1a031add188a 193 frame_idx = frame_idx +1;
dev_alexander 26:69aba05f010f 194 }
dev_alexander 26:69aba05f010f 195 */
dev_alexander 26:69aba05f010f 196
dev_alexander 26:69aba05f010f 197 memcpy(curr_raw_frame_data_to_master, local_curr_raw_frame_data_to_master, sizeof(curr_raw_frame_data_to_master));
dev_alexander 26:69aba05f010f 198 memcpy(local_prev_raw_frame_data_to_master, local_curr_raw_frame_data_to_master, sizeof(local_curr_raw_frame_data_to_master));
dev_alexander 23:f74a50977593 199 #endif
dev_alexander 23:f74a50977593 200
dev_alexander 23:f74a50977593 201 /***************************************************************************
dev_alexander 23:f74a50977593 202 * Lora Communications
dev_alexander 23:f74a50977593 203 **************************************************************************/
dev_alexander 26:69aba05f010f 204 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 26:69aba05f010f 205 dprintf("MAX32630FTHR: Master");
dev_alexander 26:69aba05f010f 206 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 26:69aba05f010f 207 memcpy(&BufferTx[tx_idx_signature], PongMsg, size_signature);
dev_alexander 26:69aba05f010f 208 memcpy(&BufferTx[tx_idx_grid_eye], local_curr_raw_frame_data_to_master, size_of_grid_eye);
dev_alexander 26:69aba05f010f 209 #endif
dev_alexander 25:1a031add188a 210 wait_ms( 10 );
dev_alexander 22:abca9d17d13d 211 SX1276PingPong();
dev_alexander 23:f74a50977593 212
dev_alexander 23:f74a50977593 213
dev_alexander 23:f74a50977593 214
dev_alexander 23:f74a50977593 215
dev_alexander 23:f74a50977593 216
dev_alexander 22:abca9d17d13d 217 }
dev_alexander 22:abca9d17d13d 218
Helmut64 0:c43b6919ae15 219 }
dev_alexander 26:69aba05f010f 220
dev_alexander 26:69aba05f010f 221
dev_alexander 26:69aba05f010f 222
dev_alexander 26:69aba05f010f 223
dev_alexander 26:69aba05f010f 224 /**
dev_alexander 26:69aba05f010f 225 * @brief This function constructs a payload buffer that is used in transmitting data in a LoRa Message
dev_alexander 26:69aba05f010f 226 */
dev_alexander 26:69aba05f010f 227 void fillPayloadWithGlobalBufs(uint8_t * BufferTx)
dev_alexander 26:69aba05f010f 228 {
dev_alexander 26:69aba05f010f 229 /* The master and slave devices will have different requirements for creating payload */
dev_alexander 26:69aba05f010f 230 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 26:69aba05f010f 231 memcpy(&BufferTx[tx_idx_signature], PingMsg, size_signature); // SX1276PingPong() uses this to detect if the message was sent from the correct Slave Device
dev_alexander 26:69aba05f010f 232 memcpy(&BufferTx[tx_idx_ble], curr_ble_data_to_slave, size_of_ble);
dev_alexander 26:69aba05f010f 233 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 26:69aba05f010f 234 memcpy(&BufferTx[tx_idx_signature], PongMsg, size_signature); // SX1276PingPong() uses this to detect if the message was sent from the correct Master Device
dev_alexander 26:69aba05f010f 235 memcpy(&BufferTx[tx_idx_grid_eye], curr_raw_frame_data_to_master, size_of_grid_eye);
dev_alexander 26:69aba05f010f 236 memcpy(&BufferTx[tx_idx_gps], curr_gps_data_to_master, size_of_gps);
dev_alexander 26:69aba05f010f 237 memcpy(&BufferTx[tx_idx_MAX17055], curr_MAX17055_to_master, size_of_MAX17055);
dev_alexander 26:69aba05f010f 238 memcpy(&BufferTx[tx_idx_MAX77650], curr_MAX77650_to_master, size_of_MAX77650);
dev_alexander 26:69aba05f010f 239 #endif
dev_alexander 26:69aba05f010f 240 return;
dev_alexander 26:69aba05f010f 241 }
dev_alexander 26:69aba05f010f 242
dev_alexander 26:69aba05f010f 243 /**
dev_alexander 26:69aba05f010f 244 * @brief This function deconstructs a payload buffer that contains data from a received LoRa Message
dev_alexander 26:69aba05f010f 245 */
dev_alexander 26:69aba05f010f 246 void fillGlobalBufsWithPayload(uint8_t * BufferRx)
dev_alexander 26:69aba05f010f 247 {
dev_alexander 26:69aba05f010f 248 /* The master and slave devices will have different requirements for offloading payload */
dev_alexander 26:69aba05f010f 249 #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway
dev_alexander 26:69aba05f010f 250 memcpy(curr_raw_frame_data_from_slave, &BufferRx[rx_idx_grid_eye], size_of_grid_eye);
dev_alexander 26:69aba05f010f 251 memcpy(curr_gps_data_from_slave, &BufferRx[rx_idx_gps], size_of_gps);
dev_alexander 26:69aba05f010f 252 memcpy(curr_MAX17055_from_slave, &BufferRx[rx_idx_MAX17055], size_of_MAX17055);
dev_alexander 26:69aba05f010f 253 memcpy(curr_MAX77650_from_slave, &BufferRx[rx_idx_MAX77650], size_of_MAX77650);
dev_alexander 26:69aba05f010f 254 #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot
dev_alexander 26:69aba05f010f 255 memcpy(curr_ble_data_from_master, &BufferRx[rx_idx_ble], size_of_ble);
dev_alexander 26:69aba05f010f 256 #endif
dev_alexander 26:69aba05f010f 257 return;
dev_alexander 26:69aba05f010f 258 }