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
main.cpp@41:1381a49e019e, 2018-08-01 (annotated)
- Committer:
- dev_alexander
- Date:
- Wed Aug 01 08:32:21 2018 +0000
- Revision:
- 41:1381a49e019e
- Parent:
- 40:6f8744c366c2
- Child:
- 42:911770814147
Implemented Battery fuel gauge functionality to be sent over LoRa by sorting the Data into Unions for easy parsing and unparsing. This is also the first revision of a complete system as under specification of the flow chart for this project.
Who changed what in which revision?
User | Revision | Line number | New 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 | 40:6f8744c366c2 | 11 | |
dev_alexander | 40:6f8744c366c2 | 12 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 13 | * Misc Instantiation |
dev_alexander | 40:6f8744c366c2 | 14 | **************************************************************************/\ |
dev_alexander | 40:6f8744c366c2 | 15 | #include "mbed.h" |
dev_alexander | 30:66f9750cc44c | 16 | #include "main.h" |
dev_alexander | 30:66f9750cc44c | 17 | #include "global_buffers.h" |
dev_alexander | 35:bae9b236070b | 18 | //#define DEBUG_GRID_EYE |
dev_alexander | 41:1381a49e019e | 19 | //#define DEBUG_MAX17055 |
dev_alexander | 32:b108ed6096b0 | 20 | #define RASPBERRY_PI |
dev_alexander | 40:6f8744c366c2 | 21 | DigitalOut myled(LED); |
dev_alexander | 40:6f8744c366c2 | 22 | Serial pc(USBTX, USBRX); |
dev_alexander | 23:f74a50977593 | 23 | |
dev_alexander | 40:6f8744c366c2 | 24 | I2C i2cBus0(P1_6, P1_7); |
dev_alexander | 40:6f8744c366c2 | 25 | I2C i2cBus1(P3_4, P3_5); |
dev_alexander | 40:6f8744c366c2 | 26 | I2C i2cBus2(P5_7, P6_0); |
dev_alexander | 32:b108ed6096b0 | 27 | |
dev_alexander | 40:6f8744c366c2 | 28 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 29 | * MAX32630FTHR Instantiation |
dev_alexander | 40:6f8744c366c2 | 30 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 31 | #if defined(TARGET_MAX32630FTHR) |
dev_alexander | 40:6f8744c366c2 | 32 | /* If the board that is compiled is the master device (BLE enabled MAX32630FTHR), |
dev_alexander | 40:6f8744c366c2 | 33 | * then it needs this library and needs to be configed in order for the device to |
dev_alexander | 40:6f8744c366c2 | 34 | * work with a 3.3volt output instead of a 1.8 volt output. |
dev_alexander | 40:6f8744c366c2 | 35 | * |
dev_alexander | 40:6f8744c366c2 | 36 | * This is only needed for the MAX32630FTHR. The MAX325620FTHR is exampt from this |
dev_alexander | 40:6f8744c366c2 | 37 | * additional setup in the main program. |
dev_alexander | 40:6f8744c366c2 | 38 | */ |
dev_alexander | 40:6f8744c366c2 | 39 | #include "max32630fthr.h" |
dev_alexander | 40:6f8744c366c2 | 40 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
dev_alexander | 32:b108ed6096b0 | 41 | #endif |
dev_alexander | 32:b108ed6096b0 | 42 | |
dev_alexander | 40:6f8744c366c2 | 43 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 44 | * MAX32620FTHR Instantiation |
dev_alexander | 40:6f8744c366c2 | 45 | **************************************************************************/ |
dev_alexander | 32:b108ed6096b0 | 46 | #if defined(TARGET_MAX32620FTHR) |
dev_alexander | 32:b108ed6096b0 | 47 | |
dev_alexander | 29:f7a0e49b826b | 48 | #endif |
Helmut64 | 0:c43b6919ae15 | 49 | |
dev_alexander | 31:f15747cffb20 | 50 | |
dev_alexander | 40:6f8744c366c2 | 51 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 52 | * Grid Eye Sensor Instantiation |
dev_alexander | 40:6f8744c366c2 | 53 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 54 | #include "GridEye.h" |
Helmut64 | 0:c43b6919ae15 | 55 | |
dev_alexander | 23:f74a50977593 | 56 | #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway |
dev_alexander | 23:f74a50977593 | 57 | /* No grid eye object needed. The function the master uses to convert the raw |
dev_alexander | 23:f74a50977593 | 58 | * data that is sent over from the slave deevice that contains the data from the |
dev_alexander | 23:f74a50977593 | 59 | * actual grid eye sensor is actually a function that is not part of the grid eye |
dev_alexander | 23:f74a50977593 | 60 | * class. this is due to the fact that the grid eye class requires n i2c bus to |
dev_alexander | 23:f74a50977593 | 61 | * be assigned to a phyiscal sensor. In this case, the library for the Grid Eye |
dev_alexander | 32:b108ed6096b0 | 62 | * sensor has a support function that is used to convert data that is aquired from |
dev_alexander | 23:f74a50977593 | 63 | * the grid eye sensor. So it is not supposed to be a class specific function. |
dev_alexander | 40:6f8744c366c2 | 64 | */ |
dev_alexander | 40:6f8744c366c2 | 65 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 66 | GridEye gridEye(i2cBus0); |
dev_alexander | 40:6f8744c366c2 | 67 | #endif |
dev_alexander | 40:6f8744c366c2 | 68 | |
dev_alexander | 40:6f8744c366c2 | 69 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 70 | * GPS Instantiation |
dev_alexander | 40:6f8744c366c2 | 71 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 72 | #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 73 | // No GPS module on the master device |
dev_alexander | 40:6f8744c366c2 | 74 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 75 | #include "GPS.h" |
dev_alexander | 40:6f8744c366c2 | 76 | GPS gps(P0_1, P0_0, 115200); // (Tx, Rx, Baud rate) |
dev_alexander | 40:6f8744c366c2 | 77 | #endif |
dev_alexander | 40:6f8744c366c2 | 78 | |
dev_alexander | 40:6f8744c366c2 | 79 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 80 | * MAX77650 Instatiation |
dev_alexander | 40:6f8744c366c2 | 81 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 82 | #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 83 | // No MAX77650 PMIC on this board |
dev_alexander | 40:6f8744c366c2 | 84 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 85 | #include "max77650.h" |
dev_alexander | 40:6f8744c366c2 | 86 | |
dev_alexander | 40:6f8744c366c2 | 87 | // Definitions for running board off of battery |
dev_alexander | 40:6f8744c366c2 | 88 | #define POWER_HOLD_ON 1 |
dev_alexander | 40:6f8744c366c2 | 89 | #define POWER_HOLD_OFF 0 |
dev_alexander | 40:6f8744c366c2 | 90 | |
dev_alexander | 40:6f8744c366c2 | 91 | /* This is the pin that the MAX77650's Power Hold pin is connected to on MAX32620FTHR |
dev_alexander | 40:6f8744c366c2 | 92 | * Without programming it to POWER_HOLD_ON, the device cant be started off of battery |
dev_alexander | 40:6f8744c366c2 | 93 | * power alone. |
dev_alexander | 40:6f8744c366c2 | 94 | */ |
dev_alexander | 40:6f8744c366c2 | 95 | DigitalOut pw_Hold(P2_2, POWER_HOLD_ON); |
dev_alexander | 40:6f8744c366c2 | 96 | |
dev_alexander | 40:6f8744c366c2 | 97 | // Instatiate instance of the Simo Pimic |
dev_alexander | 40:6f8744c366c2 | 98 | MAX77650 simo_pmic(i2cBus2); |
dev_alexander | 40:6f8744c366c2 | 99 | |
dev_alexander | 40:6f8744c366c2 | 100 | // Battery Charger Interrupt |
dev_alexander | 40:6f8744c366c2 | 101 | InterruptIn simoIRQ(P2_3); |
dev_alexander | 40:6f8744c366c2 | 102 | |
dev_alexander | 40:6f8744c366c2 | 103 | void charger_Detect(); |
dev_alexander | 40:6f8744c366c2 | 104 | bool chrg_State_fn(); |
dev_alexander | 40:6f8744c366c2 | 105 | |
dev_alexander | 40:6f8744c366c2 | 106 | void charger_Detect() |
dev_alexander | 40:6f8744c366c2 | 107 | { |
dev_alexander | 40:6f8744c366c2 | 108 | |
dev_alexander | 40:6f8744c366c2 | 109 | uint8_t int_chr_value,temp, int_chr_MASK = 0x04; |
dev_alexander | 40:6f8744c366c2 | 110 | int status; |
dev_alexander | 40:6f8744c366c2 | 111 | pc.printf("Charger Detected \n"); |
dev_alexander | 40:6f8744c366c2 | 112 | status = simo_pmic.readReg(MAX77650::INT_CHG, int_chr_value); |
dev_alexander | 40:6f8744c366c2 | 113 | pc.printf("INT_CHR val= %X \n", int_chr_value); |
dev_alexander | 40:6f8744c366c2 | 114 | if (status == 1){ |
dev_alexander | 40:6f8744c366c2 | 115 | temp = int_chr_value & int_chr_MASK; |
dev_alexander | 40:6f8744c366c2 | 116 | pc.printf("INT_CHR anded= %X \n", temp); |
dev_alexander | 40:6f8744c366c2 | 117 | if (temp == 4){ |
dev_alexander | 40:6f8744c366c2 | 118 | pc.printf("enable charger \n"); |
dev_alexander | 40:6f8744c366c2 | 119 | simo_pmic.enCharger(); |
dev_alexander | 40:6f8744c366c2 | 120 | } |
dev_alexander | 40:6f8744c366c2 | 121 | } |
dev_alexander | 40:6f8744c366c2 | 122 | } |
dev_alexander | 40:6f8744c366c2 | 123 | |
dev_alexander | 40:6f8744c366c2 | 124 | bool chrg_State_fn() |
dev_alexander | 40:6f8744c366c2 | 125 | { |
dev_alexander | 40:6f8744c366c2 | 126 | uint8_t status_chr_value,temp, chr_status_MASK = 0x02; |
dev_alexander | 40:6f8744c366c2 | 127 | int status; |
dev_alexander | 40:6f8744c366c2 | 128 | pc.printf("Charger State \n"); |
dev_alexander | 40:6f8744c366c2 | 129 | status = simo_pmic.readReg(MAX77650::STAT_CHG_B, status_chr_value); |
dev_alexander | 40:6f8744c366c2 | 130 | pc.printf("STAR_CHG_B val= %X \n", status_chr_value); |
dev_alexander | 40:6f8744c366c2 | 131 | if (status == 1){ |
dev_alexander | 40:6f8744c366c2 | 132 | temp = status_chr_value & chr_status_MASK; |
dev_alexander | 40:6f8744c366c2 | 133 | pc.printf("STAR_CHG_B val= %X \n", temp); |
dev_alexander | 40:6f8744c366c2 | 134 | if (temp == 2){ |
dev_alexander | 40:6f8744c366c2 | 135 | return true; |
dev_alexander | 40:6f8744c366c2 | 136 | } |
dev_alexander | 40:6f8744c366c2 | 137 | return false; |
dev_alexander | 40:6f8744c366c2 | 138 | } |
dev_alexander | 40:6f8744c366c2 | 139 | return false; |
dev_alexander | 40:6f8744c366c2 | 140 | } |
dev_alexander | 40:6f8744c366c2 | 141 | |
dev_alexander | 40:6f8744c366c2 | 142 | #endif |
dev_alexander | 40:6f8744c366c2 | 143 | |
dev_alexander | 40:6f8744c366c2 | 144 | |
dev_alexander | 40:6f8744c366c2 | 145 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 146 | * MAX17055 Instatiation |
dev_alexander | 40:6f8744c366c2 | 147 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 148 | #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 149 | // No MAX77650 PMIC on this board |
dev_alexander | 40:6f8744c366c2 | 150 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 151 | #include "max17055.h" |
dev_alexander | 40:6f8744c366c2 | 152 | |
dev_alexander | 40:6f8744c366c2 | 153 | MAX17055 fuelGauge(i2cBus2); |
dev_alexander | 40:6f8744c366c2 | 154 | MAX17055::platform_data design_param; |
dev_alexander | 40:6f8744c366c2 | 155 | |
dev_alexander | 40:6f8744c366c2 | 156 | #endif |
dev_alexander | 40:6f8744c366c2 | 157 | |
dev_alexander | 40:6f8744c366c2 | 158 | |
dev_alexander | 40:6f8744c366c2 | 159 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 160 | * OLED Instatiation |
dev_alexander | 40:6f8744c366c2 | 161 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 162 | #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 163 | #include "Adafruit_SSD1306.h" |
dev_alexander | 40:6f8744c366c2 | 164 | #include "maxim_bitmap.h" |
dev_alexander | 40:6f8744c366c2 | 165 | Adafruit_SSD1306_I2c displayOled(i2cBus1); |
dev_alexander | 40:6f8744c366c2 | 166 | |
dev_alexander | 40:6f8744c366c2 | 167 | |
dev_alexander | 32:b108ed6096b0 | 168 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 169 | /* Currently, there is no OLED display attached to the MAX32620, |
dev_alexander | 40:6f8744c366c2 | 170 | * so we are not compiling for it at the moment |
dev_alexander | 40:6f8744c366c2 | 171 | */ |
dev_alexander | 40:6f8744c366c2 | 172 | #endif |
dev_alexander | 40:6f8744c366c2 | 173 | |
dev_alexander | 40:6f8744c366c2 | 174 | |
dev_alexander | 40:6f8744c366c2 | 175 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 176 | * BLE Instatiation and Related Functions |
dev_alexander | 40:6f8744c366c2 | 177 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 178 | #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 179 | #include "ble/BLE.h" |
dev_alexander | 40:6f8744c366c2 | 180 | //#include "ble/Gap.h" |
dev_alexander | 40:6f8744c366c2 | 181 | #include "UARTService_custom.h" |
dev_alexander | 40:6f8744c366c2 | 182 | #include <events/mbed_events.h> |
dev_alexander | 40:6f8744c366c2 | 183 | |
dev_alexander | 40:6f8744c366c2 | 184 | DigitalOut led2(LED2); |
dev_alexander | 40:6f8744c366c2 | 185 | UARTService *uart; |
dev_alexander | 40:6f8744c366c2 | 186 | |
dev_alexander | 40:6f8744c366c2 | 187 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
dev_alexander | 40:6f8744c366c2 | 188 | * it will have an impact on code-size and power consumption. */ |
dev_alexander | 40:6f8744c366c2 | 189 | |
dev_alexander | 40:6f8744c366c2 | 190 | #if NEED_CONSOLE_OUTPUT |
dev_alexander | 40:6f8744c366c2 | 191 | #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); } |
dev_alexander | 40:6f8744c366c2 | 192 | #define READ(STR) { if (uart) uart->read(STR, strlen(STR)); } |
dev_alexander | 40:6f8744c366c2 | 193 | |
dev_alexander | 40:6f8744c366c2 | 194 | #else |
dev_alexander | 40:6f8744c366c2 | 195 | #define DEBUG(...) /* nothing */ |
dev_alexander | 40:6f8744c366c2 | 196 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
dev_alexander | 40:6f8744c366c2 | 197 | |
dev_alexander | 40:6f8744c366c2 | 198 | //Triggered when the robot is disconnected from the mobile app. |
dev_alexander | 40:6f8744c366c2 | 199 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
dev_alexander | 40:6f8744c366c2 | 200 | { |
dev_alexander | 40:6f8744c366c2 | 201 | BLE &ble = BLE::Instance(); |
dev_alexander | 40:6f8744c366c2 | 202 | DEBUG("Disconnected!\n\r"); |
dev_alexander | 40:6f8744c366c2 | 203 | DEBUG("Restarting the advertising process\n\r"); |
dev_alexander | 40:6f8744c366c2 | 204 | ble.gap().startAdvertising(); |
dev_alexander | 40:6f8744c366c2 | 205 | } |
dev_alexander | 40:6f8744c366c2 | 206 | |
dev_alexander | 40:6f8744c366c2 | 207 | /* This pointer is needed to reference data in the main program when filling |
dev_alexander | 40:6f8744c366c2 | 208 | * with data from the BLE |
dev_alexander | 40:6f8744c366c2 | 209 | */ |
dev_alexander | 40:6f8744c366c2 | 210 | uint8_t * onDataWritten_curr_ble_to_slave; |
dev_alexander | 40:6f8744c366c2 | 211 | |
dev_alexander | 40:6f8744c366c2 | 212 | // Triggered when there is data written |
dev_alexander | 40:6f8744c366c2 | 213 | void onDataWritten(const GattWriteCallbackParams *params) |
dev_alexander | 40:6f8744c366c2 | 214 | { |
dev_alexander | 40:6f8744c366c2 | 215 | //LED1 will be off when button is pressed, and on when button is released. |
dev_alexander | 40:6f8744c366c2 | 216 | led2 = !led2; |
dev_alexander | 40:6f8744c366c2 | 217 | uint8_t button_state; |
dev_alexander | 40:6f8744c366c2 | 218 | uint8_t direction; |
dev_alexander | 40:6f8744c366c2 | 219 | |
dev_alexander | 40:6f8744c366c2 | 220 | /* If there is a valid data sent by the mobile app */ |
dev_alexander | 40:6f8744c366c2 | 221 | if ((uart != NULL) && (params->handle == uart ->getTXCharacteristicHandle())) { |
dev_alexander | 40:6f8744c366c2 | 222 | const uint8_t *packet = params->data; |
dev_alexander | 40:6f8744c366c2 | 223 | button_state = packet[3]; |
dev_alexander | 40:6f8744c366c2 | 224 | direction = packet[2]; |
dev_alexander | 40:6f8744c366c2 | 225 | |
dev_alexander | 40:6f8744c366c2 | 226 | // make parameter to send over Lora |
dev_alexander | 40:6f8744c366c2 | 227 | onDataWritten_curr_ble_to_slave[0] = direction; |
dev_alexander | 40:6f8744c366c2 | 228 | onDataWritten_curr_ble_to_slave[1] = button_state; |
dev_alexander | 40:6f8744c366c2 | 229 | |
dev_alexander | 40:6f8744c366c2 | 230 | //dprintf("direction: %d\n", direction); |
dev_alexander | 40:6f8744c366c2 | 231 | //dprintf("button_state: %d\n", button_state); |
dev_alexander | 40:6f8744c366c2 | 232 | } |
dev_alexander | 40:6f8744c366c2 | 233 | } |
dev_alexander | 40:6f8744c366c2 | 234 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 235 | // No BLE chip on the MAX32620FTHR so this is not needed |
dev_alexander | 23:f74a50977593 | 236 | #endif |
dev_alexander | 40:6f8744c366c2 | 237 | |
dev_alexander | 40:6f8744c366c2 | 238 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 239 | * Motor Controller Instatiation and Related Functions |
dev_alexander | 40:6f8744c366c2 | 240 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 241 | #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 242 | // There is no motor on this device. |
dev_alexander | 40:6f8744c366c2 | 243 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 244 | /* This is a port of a library that was developed by Adafruit for the Arduino. |
dev_alexander | 40:6f8744c366c2 | 245 | * This port was done by mbed user syundo0730. */ |
dev_alexander | 40:6f8744c366c2 | 246 | #include "Adafruit_MotorShield.h" |
dev_alexander | 40:6f8744c366c2 | 247 | #include "Adafruit_PWMServoDriver.h" |
dev_alexander | 40:6f8744c366c2 | 248 | |
dev_alexander | 40:6f8744c366c2 | 249 | // These are the defined speeds that are selected out of the number range (0, 255). |
dev_alexander | 40:6f8744c366c2 | 250 | #define MOTOR_OFF 0 |
dev_alexander | 40:6f8744c366c2 | 251 | #define MOTOR_FORWARD 150 |
dev_alexander | 40:6f8744c366c2 | 252 | #define MOTOR_BACKWARD 150 |
dev_alexander | 40:6f8744c366c2 | 253 | #define MOTOR_TURN 100 |
dev_alexander | 40:6f8744c366c2 | 254 | |
dev_alexander | 40:6f8744c366c2 | 255 | // Create the motor shield object with the default I2C address |
dev_alexander | 40:6f8744c366c2 | 256 | Adafruit_MotorShield AFMS = Adafruit_MotorShield(P3_4, P3_5, 0x60 << 1); // You have to shift it over by one due to how mbed does its I2C Addressing. |
dev_alexander | 40:6f8744c366c2 | 257 | // Or, create it with a different I2C address (say for stacking) |
dev_alexander | 40:6f8744c366c2 | 258 | // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); |
dev_alexander | 40:6f8744c366c2 | 259 | // Select which 'port' M1, M2, M3 or M4. In this case, M1 |
dev_alexander | 40:6f8744c366c2 | 260 | Adafruit_DCMotor *rightMotor = AFMS.getMotor(1); |
dev_alexander | 40:6f8744c366c2 | 261 | // You can also make another motor on port M4 |
dev_alexander | 40:6f8744c366c2 | 262 | Adafruit_DCMotor *leftMotor = AFMS.getMotor(4); |
dev_alexander | 40:6f8744c366c2 | 263 | |
dev_alexander | 40:6f8744c366c2 | 264 | |
dev_alexander | 40:6f8744c366c2 | 265 | void MotorController(uint8_t (BLE_data_server)[2]) |
dev_alexander | 40:6f8744c366c2 | 266 | { |
dev_alexander | 40:6f8744c366c2 | 267 | uint8_t button_state; |
dev_alexander | 40:6f8744c366c2 | 268 | uint8_t direction; |
dev_alexander | 40:6f8744c366c2 | 269 | /*Set up constants for direction of motion */ |
dev_alexander | 40:6f8744c366c2 | 270 | const char FRONT = '5'; |
dev_alexander | 40:6f8744c366c2 | 271 | const char BACK = '6'; |
dev_alexander | 40:6f8744c366c2 | 272 | const char LEFT = '7'; |
dev_alexander | 40:6f8744c366c2 | 273 | const char RIGHT = '8'; |
dev_alexander | 40:6f8744c366c2 | 274 | |
dev_alexander | 40:6f8744c366c2 | 275 | /*Set up constants for button state */ |
dev_alexander | 40:6f8744c366c2 | 276 | const char PRESSED = '1'; |
dev_alexander | 40:6f8744c366c2 | 277 | const char RELEASED = '0'; |
dev_alexander | 40:6f8744c366c2 | 278 | |
dev_alexander | 40:6f8744c366c2 | 279 | |
dev_alexander | 40:6f8744c366c2 | 280 | button_state = BLE_data_server[1]; |
dev_alexander | 40:6f8744c366c2 | 281 | direction = BLE_data_server[0]; |
dev_alexander | 40:6f8744c366c2 | 282 | |
dev_alexander | 40:6f8744c366c2 | 283 | switch (button_state) |
dev_alexander | 40:6f8744c366c2 | 284 | { |
dev_alexander | 40:6f8744c366c2 | 285 | case PRESSED: |
dev_alexander | 40:6f8744c366c2 | 286 | switch (direction) |
dev_alexander | 40:6f8744c366c2 | 287 | { |
dev_alexander | 40:6f8744c366c2 | 288 | case FRONT: |
dev_alexander | 40:6f8744c366c2 | 289 | rightMotor->run(FORWARD); |
dev_alexander | 40:6f8744c366c2 | 290 | leftMotor->run(FORWARD); |
dev_alexander | 40:6f8744c366c2 | 291 | rightMotor->setSpeed(MOTOR_FORWARD); |
dev_alexander | 40:6f8744c366c2 | 292 | leftMotor->setSpeed(MOTOR_FORWARD); |
dev_alexander | 40:6f8744c366c2 | 293 | break; |
dev_alexander | 40:6f8744c366c2 | 294 | case BACK: |
dev_alexander | 40:6f8744c366c2 | 295 | rightMotor->run(BACKWARD); |
dev_alexander | 40:6f8744c366c2 | 296 | leftMotor->run(BACKWARD); |
dev_alexander | 40:6f8744c366c2 | 297 | rightMotor->setSpeed(MOTOR_BACKWARD); |
dev_alexander | 40:6f8744c366c2 | 298 | leftMotor->setSpeed(MOTOR_BACKWARD); |
dev_alexander | 40:6f8744c366c2 | 299 | break; |
dev_alexander | 40:6f8744c366c2 | 300 | case LEFT: |
dev_alexander | 40:6f8744c366c2 | 301 | rightMotor->run(FORWARD); |
dev_alexander | 40:6f8744c366c2 | 302 | leftMotor->run(BACKWARD); |
dev_alexander | 40:6f8744c366c2 | 303 | rightMotor->setSpeed(MOTOR_TURN); |
dev_alexander | 40:6f8744c366c2 | 304 | leftMotor->setSpeed(MOTOR_TURN); |
dev_alexander | 40:6f8744c366c2 | 305 | break; |
dev_alexander | 40:6f8744c366c2 | 306 | case RIGHT: |
dev_alexander | 40:6f8744c366c2 | 307 | rightMotor->run(BACKWARD); |
dev_alexander | 40:6f8744c366c2 | 308 | leftMotor->run(FORWARD); |
dev_alexander | 40:6f8744c366c2 | 309 | rightMotor->setSpeed(MOTOR_TURN); |
dev_alexander | 40:6f8744c366c2 | 310 | leftMotor->setSpeed(MOTOR_TURN); |
dev_alexander | 40:6f8744c366c2 | 311 | break; |
dev_alexander | 40:6f8744c366c2 | 312 | } |
dev_alexander | 40:6f8744c366c2 | 313 | break; |
dev_alexander | 40:6f8744c366c2 | 314 | case RELEASED: |
dev_alexander | 40:6f8744c366c2 | 315 | rightMotor->setSpeed(MOTOR_OFF); |
dev_alexander | 40:6f8744c366c2 | 316 | leftMotor->setSpeed(MOTOR_OFF); |
dev_alexander | 40:6f8744c366c2 | 317 | break; |
dev_alexander | 40:6f8744c366c2 | 318 | } |
dev_alexander | 40:6f8744c366c2 | 319 | } |
dev_alexander | 40:6f8744c366c2 | 320 | #endif |
dev_alexander | 40:6f8744c366c2 | 321 | |
dev_alexander | 40:6f8744c366c2 | 322 | |
dev_alexander | 40:6f8744c366c2 | 323 | |
dev_alexander | 40:6f8744c366c2 | 324 | |
dev_alexander | 40:6f8744c366c2 | 325 | |
dev_alexander | 19:9f035b9e65ec | 326 | |
dev_alexander | 23:f74a50977593 | 327 | int main() |
dev_alexander | 23:f74a50977593 | 328 | { |
dev_alexander | 32:b108ed6096b0 | 329 | /* Setup begins here: */ |
dev_alexander | 32:b108ed6096b0 | 330 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 32:b108ed6096b0 | 331 | dprintf("MAX32630FTHR: Master"); |
dev_alexander | 32:b108ed6096b0 | 332 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 333 | dprintf("MAX32620FTHR: Slave"); |
dev_alexander | 32:b108ed6096b0 | 334 | #endif |
dev_alexander | 32:b108ed6096b0 | 335 | |
dev_alexander | 32:b108ed6096b0 | 336 | |
dev_alexander | 32:b108ed6096b0 | 337 | #if defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 338 | //Set Power Hold High for battery operation MAX77650 on MAX32620FTHR |
dev_alexander | 32:b108ed6096b0 | 339 | pw_Hold = POWER_HOLD_ON; |
dev_alexander | 32:b108ed6096b0 | 340 | #endif |
dev_alexander | 32:b108ed6096b0 | 341 | |
Helmut64 | 17:98f2528e8399 | 342 | /* |
Helmut64 | 17:98f2528e8399 | 343 | * inits the Serial or USBSerial when available (230400 baud). |
Helmut64 | 17:98f2528e8399 | 344 | * If the serial uart is not is not connected it swiches to USB Serial |
Helmut64 | 17:98f2528e8399 | 345 | * blinking LED means USBSerial detected, waiting for a connect. |
Helmut64 | 17:98f2528e8399 | 346 | * It waits up to 30 seconds for a USB terminal connections |
Helmut64 | 17:98f2528e8399 | 347 | */ |
Helmut64 | 17:98f2528e8399 | 348 | InitSerial(30*1000, &myled); |
Helmut64 | 17:98f2528e8399 | 349 | dprintf("Welcome to the SX1276GenericLib"); |
Helmut64 | 18:d5527ce82e6b | 350 | |
Helmut64 | 17:98f2528e8399 | 351 | dprintf("Starting a simple LoRa PingPong"); |
dev_alexander | 23:f74a50977593 | 352 | |
dev_alexander | 32:b108ed6096b0 | 353 | |
dev_alexander | 26:69aba05f010f | 354 | |
dev_alexander | 23:f74a50977593 | 355 | |
dev_alexander | 23:f74a50977593 | 356 | /*************************************************************************** |
dev_alexander | 23:f74a50977593 | 357 | * Grid Eye Sensor: Non-Buffer Program Variables |
dev_alexander | 23:f74a50977593 | 358 | **************************************************************************/ |
dev_alexander | 23:f74a50977593 | 359 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 23:f74a50977593 | 360 | int frame_idx = 0; |
dev_alexander | 24:e8d03912f303 | 361 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 362 | #if defined(DEBUG_GRID_EYE_SLAVE) |
dev_alexander | 32:b108ed6096b0 | 363 | int frame_idx = 0; |
dev_alexander | 32:b108ed6096b0 | 364 | #endif |
dev_alexander | 23:f74a50977593 | 365 | #endif |
dev_alexander | 23:f74a50977593 | 366 | |
dev_alexander | 23:f74a50977593 | 367 | |
dev_alexander | 28:0ed92c590607 | 368 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 369 | * Combined Payload Buffers for LoRa Communications |
dev_alexander | 28:0ed92c590607 | 370 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 371 | uint8_t BufferTx[BufferSizeTx]; |
dev_alexander | 28:0ed92c590607 | 372 | uint8_t BufferRx[BufferSizeRx]; |
dev_alexander | 28:0ed92c590607 | 373 | |
dev_alexander | 28:0ed92c590607 | 374 | /*************************************************************************** |
dev_alexander | 32:b108ed6096b0 | 375 | * Identification Buffers |
dev_alexander | 32:b108ed6096b0 | 376 | **************************************************************************/ |
dev_alexander | 32:b108ed6096b0 | 377 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 32:b108ed6096b0 | 378 | uint8_t ID_of_slave[size_signature]; |
dev_alexander | 32:b108ed6096b0 | 379 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 380 | uint8_t ID_of_master[size_signature]; |
dev_alexander | 32:b108ed6096b0 | 381 | #endif |
dev_alexander | 32:b108ed6096b0 | 382 | |
dev_alexander | 32:b108ed6096b0 | 383 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 384 | * BLE Data Buffers |
dev_alexander | 28:0ed92c590607 | 385 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 386 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 31:f15747cffb20 | 387 | uint8_t curr_ble_to_slave[size_of_ble]; |
dev_alexander | 28:0ed92c590607 | 388 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 28:0ed92c590607 | 389 | uint8_t curr_ble_from_master[size_of_ble]; |
dev_alexander | 28:0ed92c590607 | 390 | uint8_t prev_ble_from_master[size_of_ble]; |
dev_alexander | 28:0ed92c590607 | 391 | #endif |
dev_alexander | 28:0ed92c590607 | 392 | |
dev_alexander | 28:0ed92c590607 | 393 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 394 | * Grid Eye Sensor Data Buffers |
dev_alexander | 28:0ed92c590607 | 395 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 396 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 28:0ed92c590607 | 397 | char curr_raw_frame_from_slave[size_of_grid_eye]; |
dev_alexander | 28:0ed92c590607 | 398 | char prev_raw_frame_from_slave[size_of_grid_eye]; |
dev_alexander | 28:0ed92c590607 | 399 | int16_t conv_frame_from_slave[64]; |
dev_alexander | 28:0ed92c590607 | 400 | #elif defined(TARGET_MAX32620FTHR) // Client Device: LoRa Controlled Robot |
dev_alexander | 28:0ed92c590607 | 401 | char curr_raw_frame_to_master[size_of_grid_eye]; |
dev_alexander | 28:0ed92c590607 | 402 | char prev_raw_frame_to_master[size_of_grid_eye]; |
dev_alexander | 28:0ed92c590607 | 403 | int16_t conv_frame_to_master[64]; |
dev_alexander | 28:0ed92c590607 | 404 | #endif |
dev_alexander | 28:0ed92c590607 | 405 | |
dev_alexander | 28:0ed92c590607 | 406 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 407 | * GPS Data Buffers |
dev_alexander | 28:0ed92c590607 | 408 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 409 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 28:0ed92c590607 | 410 | uint8_t curr_gps_from_slave[size_of_gps]; |
dev_alexander | 28:0ed92c590607 | 411 | uint8_t prev_gps_from_slave[size_of_gps]; |
dev_alexander | 28:0ed92c590607 | 412 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 28:0ed92c590607 | 413 | uint8_t curr_gps_to_master[size_of_gps]; |
dev_alexander | 28:0ed92c590607 | 414 | #endif |
dev_alexander | 28:0ed92c590607 | 415 | |
dev_alexander | 28:0ed92c590607 | 416 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 417 | * MAX17055 Data Buffers |
dev_alexander | 28:0ed92c590607 | 418 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 419 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 41:1381a49e019e | 420 | union max17055_u |
dev_alexander | 41:1381a49e019e | 421 | { |
dev_alexander | 41:1381a49e019e | 422 | uint8_t curr_MAX17055_from_slave[size_of_MAX17055]; |
dev_alexander | 41:1381a49e019e | 423 | struct battery |
dev_alexander | 41:1381a49e019e | 424 | { |
dev_alexander | 41:1381a49e019e | 425 | uint8_t soc_FG; // in Battery Percent |
dev_alexander | 41:1381a49e019e | 426 | uint8_t avg_soc_FG; // in Battery Percent |
dev_alexander | 41:1381a49e019e | 427 | float tte_FG; // in seconds |
dev_alexander | 41:1381a49e019e | 428 | float ttf_FG; // in seconds |
dev_alexander | 41:1381a49e019e | 429 | int vcell_FG; // in 78.125uV per bit |
dev_alexander | 41:1381a49e019e | 430 | int curr_FG; // in uAmps |
dev_alexander | 41:1381a49e019e | 431 | int avg_curr_FG; // in uAmps |
dev_alexander | 41:1381a49e019e | 432 | } battery; |
dev_alexander | 41:1381a49e019e | 433 | } max17055_u; |
dev_alexander | 28:0ed92c590607 | 434 | uint8_t prev_MAX17055_from_slave[size_of_MAX17055]; |
dev_alexander | 40:6f8744c366c2 | 435 | |
dev_alexander | 28:0ed92c590607 | 436 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 41:1381a49e019e | 437 | union max17055_u |
dev_alexander | 41:1381a49e019e | 438 | { |
dev_alexander | 41:1381a49e019e | 439 | uint8_t curr_MAX17055_to_master[size_of_MAX17055]; |
dev_alexander | 41:1381a49e019e | 440 | struct battery |
dev_alexander | 41:1381a49e019e | 441 | { |
dev_alexander | 41:1381a49e019e | 442 | uint8_t soc_FG; // in Battery Percent |
dev_alexander | 41:1381a49e019e | 443 | uint8_t avg_soc_FG; // in Battery Percent |
dev_alexander | 41:1381a49e019e | 444 | float tte_FG; // in seconds |
dev_alexander | 41:1381a49e019e | 445 | float ttf_FG; // in seconds |
dev_alexander | 41:1381a49e019e | 446 | int vcell_FG; // in 78.125uV per bit |
dev_alexander | 41:1381a49e019e | 447 | int curr_FG; // in uAmps |
dev_alexander | 41:1381a49e019e | 448 | int avg_curr_FG; // in uAmps |
dev_alexander | 41:1381a49e019e | 449 | } battery; |
dev_alexander | 41:1381a49e019e | 450 | } max17055_u; |
dev_alexander | 28:0ed92c590607 | 451 | #endif |
dev_alexander | 28:0ed92c590607 | 452 | |
dev_alexander | 28:0ed92c590607 | 453 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 454 | * MAX77650 Data Buffers |
dev_alexander | 28:0ed92c590607 | 455 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 456 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 28:0ed92c590607 | 457 | uint8_t curr_MAX77650_from_slave[size_of_MAX77650]; |
dev_alexander | 28:0ed92c590607 | 458 | uint8_t prev_MAX77650_from_slave[size_of_MAX77650]; |
dev_alexander | 41:1381a49e019e | 459 | bool chrg_status = false;; //True = ON False = OFF |
dev_alexander | 28:0ed92c590607 | 460 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 28:0ed92c590607 | 461 | uint8_t curr_MAX77650_to_master[size_of_MAX77650]; |
dev_alexander | 41:1381a49e019e | 462 | bool chrg_status = false; //True = ON False = OFF |
dev_alexander | 28:0ed92c590607 | 463 | #endif |
dev_alexander | 40:6f8744c366c2 | 464 | |
dev_alexander | 28:0ed92c590607 | 465 | |
dev_alexander | 28:0ed92c590607 | 466 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 467 | * Finish Setting up LoRa Radios: This passes in pointers to Buffers to send |
dev_alexander | 28:0ed92c590607 | 468 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 469 | SX1276PingPongSetup(BufferTx, BufferRx); |
dev_alexander | 28:0ed92c590607 | 470 | |
dev_alexander | 32:b108ed6096b0 | 471 | /*************************************************************************** |
dev_alexander | 32:b108ed6096b0 | 472 | * Finish Setting up BLE Radio on the MAX32630FTHR: Only on the Master Device |
dev_alexander | 32:b108ed6096b0 | 473 | **************************************************************************/ |
dev_alexander | 32:b108ed6096b0 | 474 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 32:b108ed6096b0 | 475 | BLE &ble = BLE::Instance(); |
dev_alexander | 32:b108ed6096b0 | 476 | /* Create alias for the BLE data to be saved in function above that |
dev_alexander | 32:b108ed6096b0 | 477 | * writes data to this buffer when function onDataWritten is called |
dev_alexander | 32:b108ed6096b0 | 478 | */ |
dev_alexander | 32:b108ed6096b0 | 479 | onDataWritten_curr_ble_to_slave = curr_ble_to_slave; |
dev_alexander | 32:b108ed6096b0 | 480 | |
dev_alexander | 32:b108ed6096b0 | 481 | /* Initialize BLE */ |
dev_alexander | 32:b108ed6096b0 | 482 | ble.init(); |
dev_alexander | 32:b108ed6096b0 | 483 | ble.gap().onDisconnection(disconnectionCallback); |
dev_alexander | 32:b108ed6096b0 | 484 | ble.gattServer().onDataWritten(onDataWritten); |
dev_alexander | 32:b108ed6096b0 | 485 | uart = new UARTService(ble); |
dev_alexander | 32:b108ed6096b0 | 486 | /* setup advertising */ |
dev_alexander | 32:b108ed6096b0 | 487 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
dev_alexander | 32:b108ed6096b0 | 488 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
dev_alexander | 32:b108ed6096b0 | 489 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
dev_alexander | 32:b108ed6096b0 | 490 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
dev_alexander | 32:b108ed6096b0 | 491 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
dev_alexander | 32:b108ed6096b0 | 492 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
dev_alexander | 32:b108ed6096b0 | 493 | ble.gap().setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
dev_alexander | 32:b108ed6096b0 | 494 | ble.gap().startAdvertising(); |
dev_alexander | 32:b108ed6096b0 | 495 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 496 | /* There is no BLE device that is integrated into the MAX32620FTHR. Also |
dev_alexander | 32:b108ed6096b0 | 497 | * Also this program is meant to use the BLE gateway on the MAX32630FTHR |
dev_alexander | 32:b108ed6096b0 | 498 | */ |
dev_alexander | 32:b108ed6096b0 | 499 | #endif |
dev_alexander | 32:b108ed6096b0 | 500 | |
dev_alexander | 32:b108ed6096b0 | 501 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 502 | * Finish Setting up the MAX77650 on the MAX32620FTHR: Only on the Slave Device |
dev_alexander | 40:6f8744c366c2 | 503 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 504 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 505 | // No MAX77650 on the MAX32630FTHR |
dev_alexander | 40:6f8744c366c2 | 506 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 507 | //Set Power Hold High for battery operation MAX77650 on MAX32620FTHR |
dev_alexander | 40:6f8744c366c2 | 508 | pw_Hold = POWER_HOLD_ON; |
dev_alexander | 40:6f8744c366c2 | 509 | //Simo Pmic MAX77650 init |
dev_alexander | 40:6f8744c366c2 | 510 | simo_pmic.initCharger(); |
dev_alexander | 40:6f8744c366c2 | 511 | simoIRQ.fall( &charger_Detect ); |
dev_alexander | 40:6f8744c366c2 | 512 | simo_pmic.enCharger(); |
dev_alexander | 40:6f8744c366c2 | 513 | #endif |
dev_alexander | 40:6f8744c366c2 | 514 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 515 | * Finish Setting up the MAX17650 on the MAX32620FTHR: Only on the Slave Device |
dev_alexander | 40:6f8744c366c2 | 516 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 517 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 518 | // No MAX17055 on the MAX32630FTHR |
dev_alexander | 40:6f8744c366c2 | 519 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 520 | //rsense 5mOhm for MAX32620FTHR |
dev_alexander | 41:1381a49e019e | 521 | design_param.designcap = 2200; //Design Battery Capacity mAh |
dev_alexander | 40:6f8744c366c2 | 522 | design_param.ichgterm = 0x0016; // Charge Termination Current for the Battery |
dev_alexander | 40:6f8744c366c2 | 523 | design_param.vempty = 0x9B00; // Battery Empty Voltage |
dev_alexander | 40:6f8744c366c2 | 524 | design_param.vcharge = 4200; // Battery Charge Voltage can be obtained from MAX77650 configuration |
dev_alexander | 40:6f8744c366c2 | 525 | design_param.rsense = 5; //5mOhms |
dev_alexander | 40:6f8744c366c2 | 526 | |
dev_alexander | 41:1381a49e019e | 527 | int oper_f1, volt_mod_FG, status; |
dev_alexander | 41:1381a49e019e | 528 | |
dev_alexander | 40:6f8744c366c2 | 529 | //serial.printf("Test Init and Temp Functions \r\n"); |
dev_alexander | 40:6f8744c366c2 | 530 | //status = fuelGauge.forcedExitHyberMode(); |
dev_alexander | 40:6f8744c366c2 | 531 | status = fuelGauge.init(design_param); |
dev_alexander | 40:6f8744c366c2 | 532 | pc.printf("Init FuelGauge Function Status= %X \r\n",status); |
dev_alexander | 40:6f8744c366c2 | 533 | #endif |
dev_alexander | 40:6f8744c366c2 | 534 | |
dev_alexander | 40:6f8744c366c2 | 535 | /*************************************************************************** |
dev_alexander | 40:6f8744c366c2 | 536 | * Finish Setting up the SSD1306 OLED on the MAX32630FTHR: Only on the Master Device |
dev_alexander | 40:6f8744c366c2 | 537 | **************************************************************************/ |
dev_alexander | 40:6f8744c366c2 | 538 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 40:6f8744c366c2 | 539 | displayOled.setTextColor(1); |
dev_alexander | 40:6f8744c366c2 | 540 | displayOled.setTextSize(1); |
dev_alexander | 40:6f8744c366c2 | 541 | displayOled.setTextWrap(false); |
dev_alexander | 40:6f8744c366c2 | 542 | |
dev_alexander | 40:6f8744c366c2 | 543 | displayOled.clearDisplay(); |
dev_alexander | 41:1381a49e019e | 544 | displayOled.printf("%ux%u Please Check Connection to Robot!!\r\n", displayOled.width(), displayOled.height()); |
dev_alexander | 40:6f8744c366c2 | 545 | //displayOled.drawBitmap(0,0,maximLarge,128,32,1); |
dev_alexander | 40:6f8744c366c2 | 546 | displayOled.display(); |
dev_alexander | 40:6f8744c366c2 | 547 | displayOled.display(); |
dev_alexander | 40:6f8744c366c2 | 548 | wait(1.0); |
dev_alexander | 40:6f8744c366c2 | 549 | |
dev_alexander | 40:6f8744c366c2 | 550 | displayOled.startscrollright(0x00, 0x0F); |
dev_alexander | 40:6f8744c366c2 | 551 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 40:6f8744c366c2 | 552 | // No oled on the MAX32620FTHR |
dev_alexander | 40:6f8744c366c2 | 553 | #endif |
dev_alexander | 40:6f8744c366c2 | 554 | |
dev_alexander | 40:6f8744c366c2 | 555 | /*************************************************************************** |
dev_alexander | 32:b108ed6096b0 | 556 | * Finish Setting up the motor controller on the MAX32620FTHR: Only on the Slave Device |
dev_alexander | 32:b108ed6096b0 | 557 | **************************************************************************/ |
dev_alexander | 32:b108ed6096b0 | 558 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 32:b108ed6096b0 | 559 | // No setup on the master device |
dev_alexander | 32:b108ed6096b0 | 560 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 561 | AFMS.begin(); // create with the default frequency 1.6KHz |
dev_alexander | 32:b108ed6096b0 | 562 | //AFMS.begin(1000); // OR with a different frequency, say 1KHz |
dev_alexander | 32:b108ed6096b0 | 563 | |
dev_alexander | 32:b108ed6096b0 | 564 | // Set the speed to start, from 0 (off) to 255 (max speed) |
dev_alexander | 32:b108ed6096b0 | 565 | rightMotor->setSpeed(150); |
dev_alexander | 32:b108ed6096b0 | 566 | rightMotor->run(FORWARD); |
dev_alexander | 32:b108ed6096b0 | 567 | // turn on motor |
dev_alexander | 32:b108ed6096b0 | 568 | rightMotor->run(RELEASE); |
dev_alexander | 32:b108ed6096b0 | 569 | |
dev_alexander | 32:b108ed6096b0 | 570 | leftMotor->setSpeed(150); |
dev_alexander | 32:b108ed6096b0 | 571 | leftMotor->run(FORWARD); |
dev_alexander | 32:b108ed6096b0 | 572 | // turn on motor |
dev_alexander | 32:b108ed6096b0 | 573 | leftMotor->run(RELEASE); |
dev_alexander | 32:b108ed6096b0 | 574 | #endif |
dev_alexander | 32:b108ed6096b0 | 575 | |
dev_alexander | 32:b108ed6096b0 | 576 | |
dev_alexander | 22:abca9d17d13d | 577 | while(1) |
dev_alexander | 40:6f8744c366c2 | 578 | { |
dev_alexander | 23:f74a50977593 | 579 | /*************************************************************************** |
dev_alexander | 30:66f9750cc44c | 580 | * BLE Radio Data |
dev_alexander | 30:66f9750cc44c | 581 | **************************************************************************/ |
dev_alexander | 30:66f9750cc44c | 582 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 30:66f9750cc44c | 583 | ble.waitForEvent(); |
dev_alexander | 30:66f9750cc44c | 584 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 585 | memcpy(curr_ble_from_master, prev_ble_from_master, sizeof(curr_ble_from_master)); |
dev_alexander | 30:66f9750cc44c | 586 | #endif |
dev_alexander | 30:66f9750cc44c | 587 | |
dev_alexander | 30:66f9750cc44c | 588 | /*************************************************************************** |
dev_alexander | 23:f74a50977593 | 589 | * Grid Eye Sensor |
dev_alexander | 23:f74a50977593 | 590 | **************************************************************************/ |
dev_alexander | 23:f74a50977593 | 591 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 28:0ed92c590607 | 592 | // Check to see if the contents of the previous scan are the same. If they are different then continue with converting |
dev_alexander | 32:b108ed6096b0 | 593 | #if defined(RASPBERRY_PI) |
dev_alexander | 32:b108ed6096b0 | 594 | if( memcmp(prev_raw_frame_from_slave, curr_raw_frame_from_slave, sizeof(curr_raw_frame_from_slave)) != 0 ) |
dev_alexander | 32:b108ed6096b0 | 595 | { |
dev_alexander | 32:b108ed6096b0 | 596 | char grid_eye_msg[130]; |
dev_alexander | 32:b108ed6096b0 | 597 | grid_eye_msg[0]=0x55; |
dev_alexander | 32:b108ed6096b0 | 598 | grid_eye_msg[1]=0xaa; |
dev_alexander | 32:b108ed6096b0 | 599 | for (int idx = 0; idx < 128; idx++) |
dev_alexander | 32:b108ed6096b0 | 600 | grid_eye_msg[idx+2] = curr_raw_frame_from_slave[idx]; |
dev_alexander | 32:b108ed6096b0 | 601 | //pc.printf(grid_eye_msg); |
dev_alexander | 32:b108ed6096b0 | 602 | for (int idx = 0; idx < 130; idx++) |
dev_alexander | 32:b108ed6096b0 | 603 | pc.putc(grid_eye_msg[idx]); |
dev_alexander | 35:bae9b236070b | 604 | pc.printf("\r\n"); // seperate the lines |
dev_alexander | 32:b108ed6096b0 | 605 | } |
dev_alexander | 35:bae9b236070b | 606 | #endif |
dev_alexander | 35:bae9b236070b | 607 | #if defined(DEBUG_GRID_EYE) |
dev_alexander | 32:b108ed6096b0 | 608 | if( memcmp(prev_raw_frame_from_slave, curr_raw_frame_from_slave, sizeof(curr_raw_frame_from_slave)) != 0 ) |
dev_alexander | 32:b108ed6096b0 | 609 | { |
dev_alexander | 32:b108ed6096b0 | 610 | // Convert raw data sent from slave to a 16 bit integer array by calling this |
dev_alexander | 32:b108ed6096b0 | 611 | convRaw8x8Data2Point25degC(curr_raw_frame_from_slave, conv_frame_from_slave); |
dev_alexander | 32:b108ed6096b0 | 612 | |
dev_alexander | 32:b108ed6096b0 | 613 | // Next Print off the Converted data |
dev_alexander | 32:b108ed6096b0 | 614 | pc.printf("\r\nFrame %d data: \r\n", frame_idx); |
dev_alexander | 32:b108ed6096b0 | 615 | uint8_t idx; |
dev_alexander | 32:b108ed6096b0 | 616 | float pixel_data; |
dev_alexander | 32:b108ed6096b0 | 617 | for (int y = 0; y < 8; y++) { |
dev_alexander | 32:b108ed6096b0 | 618 | for (int x = 0; x < 8; x++) { |
dev_alexander | 32:b108ed6096b0 | 619 | idx = y*8 + x; |
dev_alexander | 32:b108ed6096b0 | 620 | pixel_data = ((float)conv_frame_from_slave[idx])/4.0; |
dev_alexander | 32:b108ed6096b0 | 621 | pc.printf("%.2f \t", pixel_data); |
dev_alexander | 32:b108ed6096b0 | 622 | } |
dev_alexander | 32:b108ed6096b0 | 623 | pc.printf("\r\n\r\n"); |
dev_alexander | 23:f74a50977593 | 624 | } |
dev_alexander | 32:b108ed6096b0 | 625 | pc.printf("\r\n"); |
dev_alexander | 32:b108ed6096b0 | 626 | |
dev_alexander | 32:b108ed6096b0 | 627 | // Increment frame counter |
dev_alexander | 32:b108ed6096b0 | 628 | frame_idx = frame_idx +1; |
dev_alexander | 23:f74a50977593 | 629 | } |
dev_alexander | 32:b108ed6096b0 | 630 | #endif |
dev_alexander | 32:b108ed6096b0 | 631 | /* Next copy in data received from current data into buffer used for |
dev_alexander | 32:b108ed6096b0 | 632 | * comparison next time the memcmp above is called. This prevents the |
dev_alexander | 32:b108ed6096b0 | 633 | * program from converting the same raw data aquired by the grid eye |
dev_alexander | 32:b108ed6096b0 | 634 | * sensor on the slave device to the floating point array with the |
dev_alexander | 32:b108ed6096b0 | 635 | * 0.25 degrees celsius precision level when there is not new data. |
dev_alexander | 32:b108ed6096b0 | 636 | */ |
dev_alexander | 32:b108ed6096b0 | 637 | |
dev_alexander | 28:0ed92c590607 | 638 | memcpy(prev_raw_frame_from_slave, curr_raw_frame_from_slave, sizeof(curr_raw_frame_from_slave)); |
dev_alexander | 24:e8d03912f303 | 639 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 23:f74a50977593 | 640 | // Aquire raw data about 8x8 frame from the grid eye sensor in this function call |
dev_alexander | 28:0ed92c590607 | 641 | gridEye.getRaw8x8FrameData(curr_raw_frame_to_master); |
dev_alexander | 35:bae9b236070b | 642 | #if defined(RASPBERRY_PI) |
dev_alexander | 35:bae9b236070b | 643 | if( memcmp(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master)) != 0 ) |
dev_alexander | 35:bae9b236070b | 644 | { |
dev_alexander | 35:bae9b236070b | 645 | char grid_eye_msg[130]; |
dev_alexander | 35:bae9b236070b | 646 | grid_eye_msg[0]=0x55; |
dev_alexander | 35:bae9b236070b | 647 | grid_eye_msg[1]=0xaa; |
dev_alexander | 35:bae9b236070b | 648 | for (int idx = 0; idx < 128; idx++) |
dev_alexander | 35:bae9b236070b | 649 | grid_eye_msg[idx+2] = curr_raw_frame_to_master[idx]; |
dev_alexander | 35:bae9b236070b | 650 | //pc.printf(grid_eye_msg); |
dev_alexander | 35:bae9b236070b | 651 | for (int idx = 0; idx < 130; idx++) |
dev_alexander | 35:bae9b236070b | 652 | pc.putc(grid_eye_msg[idx]); |
dev_alexander | 35:bae9b236070b | 653 | pc.printf("\r\n"); // seperate the lines |
dev_alexander | 35:bae9b236070b | 654 | } |
dev_alexander | 35:bae9b236070b | 655 | #endif |
dev_alexander | 35:bae9b236070b | 656 | #if defined(DEBUG_GRID_EYE) |
dev_alexander | 32:b108ed6096b0 | 657 | if( memcmp(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master)) != 0 ) |
dev_alexander | 32:b108ed6096b0 | 658 | { |
dev_alexander | 32:b108ed6096b0 | 659 | // Convert raw data sent from slave to a 16 bit integer array by calling this |
dev_alexander | 32:b108ed6096b0 | 660 | convRaw8x8Data2Point25degC(curr_raw_frame_to_master, conv_frame_to_master); |
dev_alexander | 32:b108ed6096b0 | 661 | |
dev_alexander | 32:b108ed6096b0 | 662 | |
dev_alexander | 32:b108ed6096b0 | 663 | // Next Print off the Converted data |
dev_alexander | 32:b108ed6096b0 | 664 | pc.printf("\r\nFrame %d data: \r\n", frame_idx); |
dev_alexander | 32:b108ed6096b0 | 665 | uint8_t idx; |
dev_alexander | 32:b108ed6096b0 | 666 | float pixel_data; |
dev_alexander | 32:b108ed6096b0 | 667 | for (int y = 0; y < 8; y++) { |
dev_alexander | 32:b108ed6096b0 | 668 | for (int x = 0; x < 8; x++) { |
dev_alexander | 32:b108ed6096b0 | 669 | idx = y*8 + x; |
dev_alexander | 32:b108ed6096b0 | 670 | pixel_data = ((float)conv_frame_to_master[idx])/4.0; |
dev_alexander | 32:b108ed6096b0 | 671 | pc.printf("%.2f \t", pixel_data); |
dev_alexander | 32:b108ed6096b0 | 672 | } |
dev_alexander | 32:b108ed6096b0 | 673 | pc.printf("\r\n\r\n"); |
dev_alexander | 25:1a031add188a | 674 | } |
dev_alexander | 32:b108ed6096b0 | 675 | pc.printf("\r\n"); |
dev_alexander | 32:b108ed6096b0 | 676 | |
dev_alexander | 32:b108ed6096b0 | 677 | // Increment frame counter |
dev_alexander | 32:b108ed6096b0 | 678 | frame_idx = frame_idx +1; |
dev_alexander | 25:1a031add188a | 679 | } |
dev_alexander | 32:b108ed6096b0 | 680 | #endif |
dev_alexander | 33:52c898aca207 | 681 | memcpy(prev_raw_frame_to_master, curr_raw_frame_to_master, sizeof(curr_raw_frame_to_master)); |
dev_alexander | 31:f15747cffb20 | 682 | #endif |
dev_alexander | 31:f15747cffb20 | 683 | |
dev_alexander | 31:f15747cffb20 | 684 | /*************************************************************************** |
dev_alexander | 31:f15747cffb20 | 685 | * GPS Sensor |
dev_alexander | 31:f15747cffb20 | 686 | **************************************************************************/ |
dev_alexander | 31:f15747cffb20 | 687 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 41:1381a49e019e | 688 | // No gps sensor is applied |
dev_alexander | 31:f15747cffb20 | 689 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 31:f15747cffb20 | 690 | // The testing hasnt been done yet for the GPS Module. Will do this when more parts come in. |
dev_alexander | 31:f15747cffb20 | 691 | /* |
dev_alexander | 31:f15747cffb20 | 692 | if(gps.sample()){ |
dev_alexander | 31:f15747cffb20 | 693 | 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); |
dev_alexander | 31:f15747cffb20 | 694 | pc.printf("%d:%d:%d",gps.hour,gps.minute,gps.seconed); |
dev_alexander | 31:f15747cffb20 | 695 | } |
dev_alexander | 31:f15747cffb20 | 696 | */ |
dev_alexander | 31:f15747cffb20 | 697 | #endif |
dev_alexander | 28:0ed92c590607 | 698 | |
dev_alexander | 23:f74a50977593 | 699 | /*************************************************************************** |
dev_alexander | 41:1381a49e019e | 700 | * MAX17055 Fuel Gauge Sensor |
dev_alexander | 41:1381a49e019e | 701 | **************************************************************************/ |
dev_alexander | 41:1381a49e019e | 702 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 41:1381a49e019e | 703 | // Process the soc to see if the oled display needs to be updated or not. |
dev_alexander | 41:1381a49e019e | 704 | #if defined(DEBUG_MAX17055) |
dev_alexander | 41:1381a49e019e | 705 | pc.printf("soc_FG :%d\n" ,max17055_u.battery.soc_FG); |
dev_alexander | 41:1381a49e019e | 706 | pc.printf("avg_soc_FG :%d\n" ,max17055_u.battery.avg_soc_FG); |
dev_alexander | 41:1381a49e019e | 707 | pc.printf("tte_FG :%d\n" ,max17055_u.battery.tte_FG); |
dev_alexander | 41:1381a49e019e | 708 | pc.printf("ttf_FG :%d\n" ,max17055_u.battery.ttf_FG); |
dev_alexander | 41:1381a49e019e | 709 | pc.printf("vcell_FG :%d\n" ,max17055_u.battery.vcell_FG); |
dev_alexander | 41:1381a49e019e | 710 | pc.printf("curr_FG :%d\n" ,max17055_u.battery.curr_FG); |
dev_alexander | 41:1381a49e019e | 711 | pc.printf("avg_curr_FG :%d\n" ,max17055_u.battery.avg_curr_FG); |
dev_alexander | 41:1381a49e019e | 712 | |
dev_alexander | 41:1381a49e019e | 713 | for (int idx = 0; idx < size_of_MAX17055; idx++) |
dev_alexander | 41:1381a49e019e | 714 | { |
dev_alexander | 41:1381a49e019e | 715 | pc.printf("Byte #%d is: %d\n", idx, max17055_u.curr_MAX17055_from_slave[idx]); |
dev_alexander | 41:1381a49e019e | 716 | } |
dev_alexander | 41:1381a49e019e | 717 | #endif |
dev_alexander | 41:1381a49e019e | 718 | if( memcmp(prev_MAX17055_from_slave, curr_MAX17055_from_slave, sizeof(curr_MAX17055_from_slave)) != 0 ) |
dev_alexander | 41:1381a49e019e | 719 | { |
dev_alexander | 41:1381a49e019e | 720 | // Stop the scrolling to allow for a static image to be displayed on OLED |
dev_alexander | 41:1381a49e019e | 721 | displayOled.stopscroll(); |
dev_alexander | 41:1381a49e019e | 722 | |
dev_alexander | 41:1381a49e019e | 723 | displayOled.clearDisplay(); |
dev_alexander | 41:1381a49e019e | 724 | |
dev_alexander | 41:1381a49e019e | 725 | if ( chrg_status == false){ |
dev_alexander | 41:1381a49e019e | 726 | if (max17055_u.battery.soc_FG >= 89) |
dev_alexander | 41:1381a49e019e | 727 | displayOled.drawBitmap(96,0,battFull,32,16,1); |
dev_alexander | 41:1381a49e019e | 728 | else if (max17055_u.battery.soc_FG >= 65 && max17055_u.battery.soc_FG < 89) |
dev_alexander | 41:1381a49e019e | 729 | displayOled.drawBitmap(96,0,batt75,32,16,1); |
dev_alexander | 41:1381a49e019e | 730 | else if (max17055_u.battery.soc_FG >= 45 && max17055_u.battery.soc_FG < 65) |
dev_alexander | 41:1381a49e019e | 731 | displayOled.drawBitmap(96,0,batt50,32,16,1); |
dev_alexander | 41:1381a49e019e | 732 | else if (max17055_u.battery.soc_FG >= 20 && max17055_u.battery.soc_FG < 45) |
dev_alexander | 41:1381a49e019e | 733 | displayOled.drawBitmap(96,0,batt25,32,16,1); |
dev_alexander | 41:1381a49e019e | 734 | else |
dev_alexander | 41:1381a49e019e | 735 | displayOled.drawBitmap(96,0,battEmpty,32,16,1); |
dev_alexander | 41:1381a49e019e | 736 | } |
dev_alexander | 41:1381a49e019e | 737 | |
dev_alexander | 41:1381a49e019e | 738 | if (chrg_status == true){ |
dev_alexander | 41:1381a49e019e | 739 | displayOled.drawBitmap(96,0,battChrging,32,16,1); |
dev_alexander | 41:1381a49e019e | 740 | } |
dev_alexander | 41:1381a49e019e | 741 | |
dev_alexander | 41:1381a49e019e | 742 | |
dev_alexander | 41:1381a49e019e | 743 | //serial.printf("Time to Empty= %dsec \r\n",tte_FG); |
dev_alexander | 41:1381a49e019e | 744 | int tte_minutes = (max17055_u.battery.tte_FG/60); |
dev_alexander | 41:1381a49e019e | 745 | int ttf_minutes = (max17055_u.battery.tte_FG/60); |
dev_alexander | 41:1381a49e019e | 746 | //serial.printf("Time to Empty= %dhours \r\n",tte_FG); |
dev_alexander | 41:1381a49e019e | 747 | //serial.printf("Temp Function Status= %X \r\n",status); |
dev_alexander | 41:1381a49e019e | 748 | //wait(1); |
dev_alexander | 41:1381a49e019e | 749 | //serial.printf("SOC = %d %% \r\n",soc_FG); |
dev_alexander | 41:1381a49e019e | 750 | //serial.printf("Temp Function Status= %X \r\n",status); |
dev_alexander | 41:1381a49e019e | 751 | //serial.printf("Current = %d uamp \r\n",curr_FG); |
dev_alexander | 41:1381a49e019e | 752 | // pc.printf("Voltage = %d uvolts \r\n",max17055_u.battery.vcell_FG); |
dev_alexander | 41:1381a49e019e | 753 | int oper_f1 = max17055_u.battery.vcell_FG /1000000; |
dev_alexander | 41:1381a49e019e | 754 | int volt_mod_FG = (max17055_u.battery.vcell_FG % 1000000)/1000; |
dev_alexander | 41:1381a49e019e | 755 | |
dev_alexander | 41:1381a49e019e | 756 | // pc.printf("Voltage = %d V \r\n",max17055_u.battery.vcell_FG); |
dev_alexander | 41:1381a49e019e | 757 | // pc.printf("Voltage = %d V \r\n",oper_f1); |
dev_alexander | 41:1381a49e019e | 758 | |
dev_alexander | 41:1381a49e019e | 759 | |
dev_alexander | 41:1381a49e019e | 760 | |
dev_alexander | 41:1381a49e019e | 761 | displayOled.setTextCursor(0,0); |
dev_alexander | 41:1381a49e019e | 762 | displayOled.setTextColor(1); |
dev_alexander | 41:1381a49e019e | 763 | displayOled.setTextSize(1); |
dev_alexander | 41:1381a49e019e | 764 | //displayOled.setTextWrap(true); |
dev_alexander | 41:1381a49e019e | 765 | displayOled.printf("MAX17055"); |
dev_alexander | 41:1381a49e019e | 766 | displayOled.setTextCursor(0,8); |
dev_alexander | 41:1381a49e019e | 767 | displayOled.printf("SOC = %d%% \r\n",max17055_u.battery.avg_soc_FG); |
dev_alexander | 41:1381a49e019e | 768 | |
dev_alexander | 41:1381a49e019e | 769 | displayOled.setTextCursor(0,16); |
dev_alexander | 41:1381a49e019e | 770 | if (chrg_status == true){ |
dev_alexander | 41:1381a49e019e | 771 | displayOled.printf("TTF = %dmins \r\n",tte_minutes); |
dev_alexander | 41:1381a49e019e | 772 | }else |
dev_alexander | 41:1381a49e019e | 773 | displayOled.printf("TTE = %dmins \r\n",tte_minutes); |
dev_alexander | 41:1381a49e019e | 774 | displayOled.setTextCursor(0,24); |
dev_alexander | 41:1381a49e019e | 775 | displayOled.printf("I=%duA \r\n",max17055_u.battery.avg_curr_FG); |
dev_alexander | 41:1381a49e019e | 776 | displayOled.setTextCursor(65,24); |
dev_alexander | 41:1381a49e019e | 777 | if (volt_mod_FG > 99){ |
dev_alexander | 41:1381a49e019e | 778 | displayOled.printf("V=%d.%dV \r\n",oper_f1,volt_mod_FG); |
dev_alexander | 41:1381a49e019e | 779 | }else if (volt_mod_FG < 100){ |
dev_alexander | 41:1381a49e019e | 780 | displayOled.printf("V=%d.0%dV \r\n",oper_f1,volt_mod_FG); |
dev_alexander | 41:1381a49e019e | 781 | } |
dev_alexander | 41:1381a49e019e | 782 | |
dev_alexander | 41:1381a49e019e | 783 | displayOled.display(); |
dev_alexander | 41:1381a49e019e | 784 | displayOled.display(); |
dev_alexander | 41:1381a49e019e | 785 | } |
dev_alexander | 41:1381a49e019e | 786 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 41:1381a49e019e | 787 | max17055_u.battery.soc_FG = fuelGauge.get_SOC(); |
dev_alexander | 41:1381a49e019e | 788 | max17055_u.battery.avg_soc_FG = fuelGauge.get_atAvSOC(); |
dev_alexander | 41:1381a49e019e | 789 | max17055_u.battery.tte_FG = fuelGauge.get_TTE(); |
dev_alexander | 41:1381a49e019e | 790 | max17055_u.battery.ttf_FG = fuelGauge.get_TTF(); |
dev_alexander | 41:1381a49e019e | 791 | max17055_u.battery.vcell_FG = fuelGauge.get_Vcell(); |
dev_alexander | 41:1381a49e019e | 792 | max17055_u.battery.curr_FG = fuelGauge.get_Current(design_param); |
dev_alexander | 41:1381a49e019e | 793 | max17055_u.battery.avg_curr_FG = fuelGauge.get_AvgCurrent(design_param); |
dev_alexander | 41:1381a49e019e | 794 | |
dev_alexander | 41:1381a49e019e | 795 | #if defined(DEBUG_MAX17055) |
dev_alexander | 41:1381a49e019e | 796 | pc.printf("soc_FG :%d\n" ,max17055_u.battery.soc_FG); |
dev_alexander | 41:1381a49e019e | 797 | pc.printf("avg_soc_FG :%d\n" ,max17055_u.battery.avg_soc_FG); |
dev_alexander | 41:1381a49e019e | 798 | pc.printf("tte_FG :%d\n" ,max17055_u.battery.tte_FG); |
dev_alexander | 41:1381a49e019e | 799 | pc.printf("ttf_FG :%d\n" ,max17055_u.battery.ttf_FG); |
dev_alexander | 41:1381a49e019e | 800 | pc.printf("vcell_FG :%d\n" ,max17055_u.battery.vcell_FG); |
dev_alexander | 41:1381a49e019e | 801 | pc.printf("curr_FG :%d\n" ,max17055_u.battery.curr_FG); |
dev_alexander | 41:1381a49e019e | 802 | pc.printf("avg_curr_FG :%d\n" ,max17055_u.battery.avg_curr_FG); |
dev_alexander | 41:1381a49e019e | 803 | |
dev_alexander | 41:1381a49e019e | 804 | for (int idx = 0; idx < size_of_MAX17055; idx++) |
dev_alexander | 41:1381a49e019e | 805 | { |
dev_alexander | 41:1381a49e019e | 806 | pc.printf("Byte #%d is: %d\n", idx, max17055_u.curr_MAX17055_to_master[idx]); |
dev_alexander | 41:1381a49e019e | 807 | } |
dev_alexander | 41:1381a49e019e | 808 | #endif |
dev_alexander | 41:1381a49e019e | 809 | #endif |
dev_alexander | 41:1381a49e019e | 810 | |
dev_alexander | 41:1381a49e019e | 811 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 812 | * Fill Payload Buffer With Data From Sensors for LoRa Transmition |
dev_alexander | 28:0ed92c590607 | 813 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 814 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 28:0ed92c590607 | 815 | memcpy(&BufferTx[tx_idx_signature], PingMsg, size_signature); |
dev_alexander | 28:0ed92c590607 | 816 | memcpy(&BufferTx[tx_idx_ble], curr_ble_to_slave, size_of_ble); |
dev_alexander | 28:0ed92c590607 | 817 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 28:0ed92c590607 | 818 | memcpy(&BufferTx[tx_idx_signature], PongMsg, size_signature); |
dev_alexander | 28:0ed92c590607 | 819 | memcpy(&BufferTx[tx_idx_grid_eye], curr_raw_frame_to_master, size_of_grid_eye); |
dev_alexander | 28:0ed92c590607 | 820 | memcpy(&BufferTx[tx_idx_gps], curr_gps_to_master, size_of_gps); |
dev_alexander | 41:1381a49e019e | 821 | memcpy(&BufferTx[tx_idx_MAX17055], max17055_u.curr_MAX17055_to_master, size_of_MAX17055); |
dev_alexander | 28:0ed92c590607 | 822 | memcpy(&BufferTx[tx_idx_MAX77650], curr_MAX77650_to_master, size_of_MAX77650); |
dev_alexander | 28:0ed92c590607 | 823 | #endif |
dev_alexander | 28:0ed92c590607 | 824 | |
dev_alexander | 28:0ed92c590607 | 825 | |
dev_alexander | 28:0ed92c590607 | 826 | |
dev_alexander | 32:b108ed6096b0 | 827 | |
dev_alexander | 28:0ed92c590607 | 828 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 829 | * Lora Communications |
dev_alexander | 28:0ed92c590607 | 830 | **************************************************************************/ |
dev_alexander | 22:abca9d17d13d | 831 | SX1276PingPong(); |
dev_alexander | 28:0ed92c590607 | 832 | |
dev_alexander | 28:0ed92c590607 | 833 | /*************************************************************************** |
dev_alexander | 28:0ed92c590607 | 834 | * Fill Global Buffers With Data From Received Payload Buffer |
dev_alexander | 28:0ed92c590607 | 835 | **************************************************************************/ |
dev_alexander | 28:0ed92c590607 | 836 | /* The master and slave devices will have different requirements for offloading payload */ |
dev_alexander | 28:0ed92c590607 | 837 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 32:b108ed6096b0 | 838 | memcpy(ID_of_slave, &BufferRx[rx_idx_signature], size_signature); |
dev_alexander | 32:b108ed6096b0 | 839 | memcpy(curr_raw_frame_from_slave, &BufferRx[rx_idx_grid_eye], size_of_grid_eye); |
dev_alexander | 32:b108ed6096b0 | 840 | memcpy(curr_gps_from_slave, &BufferRx[rx_idx_gps], size_of_gps); |
dev_alexander | 41:1381a49e019e | 841 | memcpy(max17055_u.curr_MAX17055_from_slave, &BufferRx[rx_idx_MAX17055], size_of_MAX17055); |
dev_alexander | 32:b108ed6096b0 | 842 | memcpy(curr_MAX77650_from_slave, &BufferRx[rx_idx_MAX77650], size_of_MAX77650); |
dev_alexander | 28:0ed92c590607 | 843 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 844 | memcpy(ID_of_master, &BufferRx[rx_idx_signature], size_signature); |
dev_alexander | 32:b108ed6096b0 | 845 | memcpy(curr_ble_from_master, &BufferRx[rx_idx_ble], size_of_ble); |
dev_alexander | 28:0ed92c590607 | 846 | #endif |
dev_alexander | 23:f74a50977593 | 847 | |
dev_alexander | 32:b108ed6096b0 | 848 | /*************************************************************************** |
dev_alexander | 32:b108ed6096b0 | 849 | * Motor Controller controlls go here |
dev_alexander | 32:b108ed6096b0 | 850 | **************************************************************************/ |
dev_alexander | 32:b108ed6096b0 | 851 | #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway |
dev_alexander | 32:b108ed6096b0 | 852 | // No motor controller on master |
dev_alexander | 32:b108ed6096b0 | 853 | #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot |
dev_alexander | 32:b108ed6096b0 | 854 | // Check to see if the contents of the previous ble sdata received are the same. If they are different then change motor controllers registers |
dev_alexander | 32:b108ed6096b0 | 855 | if( memcmp(prev_ble_from_master, curr_ble_from_master, sizeof(curr_ble_from_master)) != 0 ) |
dev_alexander | 32:b108ed6096b0 | 856 | { |
dev_alexander | 32:b108ed6096b0 | 857 | MotorController(curr_ble_from_master); |
dev_alexander | 32:b108ed6096b0 | 858 | } |
dev_alexander | 32:b108ed6096b0 | 859 | #endif |
dev_alexander | 23:f74a50977593 | 860 | |
dev_alexander | 23:f74a50977593 | 861 | |
dev_alexander | 23:f74a50977593 | 862 | |
dev_alexander | 22:abca9d17d13d | 863 | } |
dev_alexander | 22:abca9d17d13d | 864 | |
Helmut64 | 0:c43b6919ae15 | 865 | } |