![](/media/cache/profiles/profile_pic_for_github_less_than_1MB.jpg.50x50_q85.jpg)
This is example code that can get you started with building your own IR vision robot that communicates over LoRa
Dependencies: Adafruit-MotorShield Adafruit-PWM-Servo-Driver Adafruit_GFX BufferedSerial MAX17055_EZconfig NEO-6m-GPS SX1276GenericLib USBDeviceHT max32630fthr max77650_charger_sample
Fork of MAX326xxFTHR_LoRa_Example_test by
Diff: main.cpp
- Revision:
- 40:6f8744c366c2
- Parent:
- 39:df7c8bd2212e
- Child:
- 41:1381a49e019e
diff -r df7c8bd2212e -r 6f8744c366c2 main.cpp --- a/main.cpp Wed Aug 01 03:41:07 2018 +0000 +++ b/main.cpp Wed Aug 01 03:52:55 2018 +0000 @@ -8,190 +8,49 @@ * For Wiring Instructions, please visit the link below: * https://www.hackster.io/DevinAlex64/getting-started-with-the-max32620fthr-and-lora-f9d8dd\ */ - + +/*************************************************************************** + * Misc Instantiation + **************************************************************************/\ +#include "mbed.h" #include "main.h" #include "global_buffers.h" -#include "GridEye.h" - //#define DEBUG_GRID_EYE #define RASPBERRY_PI - +DigitalOut myled(LED); +Serial pc(USBTX, USBRX); -/* If the board that is compiled is the master device (BLE enabled MAX32630FTHR), - * then it needs this library and needs to be configed in order for the device to - * work with a 3.3volt output instead of a 1.8 volt output. - * - * This is only needed for the MAX32630FTHR. The MAX325620FTHR is exampt from this - * additional setup in the main program. - */ - -#if defined(TARGET_MAX32620FTHR) - // Definitions for running board off of battery - #define POWER_HOLD_ON 1 - #define POWER_HOLD_OFF 0 - -// MAX77650 - DigitalOut pw_Hold(P2_2, POWER_HOLD_ON); +I2C i2cBus0(P1_6, P1_7); +I2C i2cBus1(P3_4, P3_5); +I2C i2cBus2(P5_7, P6_0); -// Initialize GPS - #include "GPS.h" - GPS gps(P0_1, P0_0, 115200); - -// Motor Driver initialization - #include "Adafruit_MotorShield.h" - #include "Adafruit_PWMServoDriver.h" - - #define MOTOR_OFF 0 - #define MOTOR_FORWARD 150 - #define MOTOR_BACKWARD 150 - #define MOTOR_TURN 100 - - - // Create the motor shield object with the default I2C address - //Adafruit_MotorShield AFMS = Adafruit_MotorShield(p9, p10, 0x60 << 1); - Adafruit_MotorShield AFMS = Adafruit_MotorShield(P3_4, P3_5, 0x60 << 1); - // Or, create it with a different I2C address (say for stacking) - // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); - - // Select which 'port' M1, M2, M3 or M4. In this case, M1 - Adafruit_DCMotor *rightMotor = AFMS.getMotor(1); - Adafruit_DCMotor *leftMotor = AFMS.getMotor(4); - // You can also make another motor on port M2 - //Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2); - +/*************************************************************************** + * MAX32630FTHR Instantiation + **************************************************************************/ +#if defined(TARGET_MAX32630FTHR) + /* If the board that is compiled is the master device (BLE enabled MAX32630FTHR), + * then it needs this library and needs to be configed in order for the device to + * work with a 3.3volt output instead of a 1.8 volt output. + * + * This is only needed for the MAX32630FTHR. The MAX325620FTHR is exampt from this + * additional setup in the main program. + */ + #include "max32630fthr.h" + MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); #endif +/*************************************************************************** + * MAX32620FTHR Instantiation + **************************************************************************/ #if defined(TARGET_MAX32620FTHR) -void MotorController(uint8_t (BLE_data_server)[2]) -{ - uint8_t button_state; - uint8_t direction; - /*Set up constants for direction of motion */ - const char FRONT = '5'; - const char BACK = '6'; - const char LEFT = '7'; - const char RIGHT = '8'; - - /*Set up constants for button state */ - const char PRESSED = '1'; - const char RELEASED = '0'; - - button_state = BLE_data_server[1]; - direction = BLE_data_server[0]; - - switch (button_state) - { - case PRESSED: - //md_left_pwm = static_cast<float>(20/100.0F); - //md_right_pwm = static_cast<float>(20/100.0F); - - switch (direction) - { - case FRONT: - rightMotor->run(FORWARD); - leftMotor->run(FORWARD); - rightMotor->setSpeed(MOTOR_FORWARD); - leftMotor->setSpeed(MOTOR_FORWARD); - break; - case BACK: - rightMotor->run(BACKWARD); - leftMotor->run(BACKWARD); - rightMotor->setSpeed(MOTOR_BACKWARD); - leftMotor->setSpeed(MOTOR_BACKWARD); - break; - case LEFT: - rightMotor->run(FORWARD); - leftMotor->run(BACKWARD); - rightMotor->setSpeed(MOTOR_TURN); - leftMotor->setSpeed(MOTOR_TURN); - break; - case RIGHT: - rightMotor->run(BACKWARD); - leftMotor->run(FORWARD); - rightMotor->setSpeed(MOTOR_TURN); - leftMotor->setSpeed(MOTOR_TURN); - break; - } - break; - case RELEASED: - rightMotor->setSpeed(MOTOR_OFF); - leftMotor->setSpeed(MOTOR_OFF); - break; - } -} #endif -#if defined(TARGET_MAX32630FTHR) - #include "max32630fthr.h" - MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); - #define COMPILE_BLE - - #if defined(COMPILE_BLE) - /* BLE Related MAX32630FTHR Specific stuff */ - #include "ble/BLE.h" - //#include "ble/Gap.h" - #include "UARTService_custom.h" - #include <events/mbed_events.h> - - DigitalOut led2(LED2); - UARTService *uart; - - #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; - * it will have an impact on code-size and power consumption. */ - - #if NEED_CONSOLE_OUTPUT - #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); } - #define READ(STR) { if (uart) uart->read(STR, strlen(STR)); } - - #else - #define DEBUG(...) /* nothing */ - #endif /* #if NEED_CONSOLE_OUTPUT */ - - //Triggered when the robot is disconnected from the mobile app. - void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) - { - BLE &ble = BLE::Instance(); - DEBUG("Disconnected!\n\r"); - DEBUG("Restarting the advertising process\n\r"); - ble.gap().startAdvertising(); - } - - /* This pointer is needed to reference data in the main program when filling - * with data from the BLE - */ - uint8_t * onDataWritten_curr_ble_to_slave; - - // Triggered when there is data written - void onDataWritten(const GattWriteCallbackParams *params) - { - //LED1 will be off when button is pressed, and on when button is released. - led2 = !led2; - uint8_t button_state; - uint8_t direction; - - /* If there is a valid data sent by the mobile app */ - if ((uart != NULL) && (params->handle == uart ->getTXCharacteristicHandle())) { - const uint8_t *packet = params->data; - button_state = packet[3]; - direction = packet[2]; - - // make parameter to send over Lora - onDataWritten_curr_ble_to_slave[0] = direction; - onDataWritten_curr_ble_to_slave[1] = button_state; - - //dprintf("direction: %d\n", direction); - //dprintf("button_state: %d\n", button_state); - } - } - #endif -#endif - -DigitalOut myled(LED); - -Serial pc(USBTX, USBRX); - +/*************************************************************************** + * Grid Eye Sensor Instantiation + **************************************************************************/ +#include "GridEye.h" #if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway /* No grid eye object needed. The function the master uses to convert the raw @@ -201,14 +60,268 @@ * be assigned to a phyiscal sensor. In this case, the library for the Grid Eye * sensor has a support function that is used to convert data that is aquired from * the grid eye sensor. So it is not supposed to be a class specific function. - */ + */ +#elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + GridEye gridEye(i2cBus0); +#endif + +/*************************************************************************** + * GPS Instantiation + **************************************************************************/ +#if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway + // No GPS module on the master device +#elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + #include "GPS.h" + GPS gps(P0_1, P0_0, 115200); // (Tx, Rx, Baud rate) +#endif + +/*************************************************************************** + * MAX77650 Instatiation + **************************************************************************/ +#if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway + // No MAX77650 PMIC on this board +#elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + #include "max77650.h" + + // Definitions for running board off of battery + #define POWER_HOLD_ON 1 + #define POWER_HOLD_OFF 0 + + /* This is the pin that the MAX77650's Power Hold pin is connected to on MAX32620FTHR + * Without programming it to POWER_HOLD_ON, the device cant be started off of battery + * power alone. + */ + DigitalOut pw_Hold(P2_2, POWER_HOLD_ON); + + // Instatiate instance of the Simo Pimic + MAX77650 simo_pmic(i2cBus2); + + // Battery Charger Interrupt + InterruptIn simoIRQ(P2_3); + + void charger_Detect(); + bool chrg_State_fn(); + + void charger_Detect() + { + + uint8_t int_chr_value,temp, int_chr_MASK = 0x04; + int status; + pc.printf("Charger Detected \n"); + status = simo_pmic.readReg(MAX77650::INT_CHG, int_chr_value); + pc.printf("INT_CHR val= %X \n", int_chr_value); + if (status == 1){ + temp = int_chr_value & int_chr_MASK; + pc.printf("INT_CHR anded= %X \n", temp); + if (temp == 4){ + pc.printf("enable charger \n"); + simo_pmic.enCharger(); + } + } + } + + bool chrg_State_fn() + { + uint8_t status_chr_value,temp, chr_status_MASK = 0x02; + int status; + pc.printf("Charger State \n"); + status = simo_pmic.readReg(MAX77650::STAT_CHG_B, status_chr_value); + pc.printf("STAR_CHG_B val= %X \n", status_chr_value); + if (status == 1){ + temp = status_chr_value & chr_status_MASK; + pc.printf("STAR_CHG_B val= %X \n", temp); + if (temp == 2){ + return true; + } + return false; + } + return false; + } + +#endif + + +/*************************************************************************** + * MAX17055 Instatiation + **************************************************************************/ +#if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway + // No MAX77650 PMIC on this board +#elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + #include "max17055.h" + + MAX17055 fuelGauge(i2cBus2); + MAX17055::platform_data design_param; + +#endif + + +/*************************************************************************** + * OLED Instatiation + **************************************************************************/ +#if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway + #include "Adafruit_SSD1306.h" + #include "maxim_bitmap.h" + Adafruit_SSD1306_I2c displayOled(i2cBus1); + + #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot - //Init I2C communications in default I2C bus I2C #1 - // I2C i2cGridEye(SDA,SCL); - //I2C i2cGridEye(P3_4,P3_5); - I2C i2cGridEye(P1_6,P1_7); - GridEye gridEye(i2cGridEye); + /* Currently, there is no OLED display attached to the MAX32620, + * so we are not compiling for it at the moment + */ +#endif + + +/*************************************************************************** + * BLE Instatiation and Related Functions + **************************************************************************/ +#if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway + #include "ble/BLE.h" + //#include "ble/Gap.h" + #include "UARTService_custom.h" + #include <events/mbed_events.h> + + DigitalOut led2(LED2); + UARTService *uart; + + #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; + * it will have an impact on code-size and power consumption. */ + + #if NEED_CONSOLE_OUTPUT + #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); } + #define READ(STR) { if (uart) uart->read(STR, strlen(STR)); } + + #else + #define DEBUG(...) /* nothing */ + #endif /* #if NEED_CONSOLE_OUTPUT */ + + //Triggered when the robot is disconnected from the mobile app. + void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) + { + BLE &ble = BLE::Instance(); + DEBUG("Disconnected!\n\r"); + DEBUG("Restarting the advertising process\n\r"); + ble.gap().startAdvertising(); + } + + /* This pointer is needed to reference data in the main program when filling + * with data from the BLE + */ + uint8_t * onDataWritten_curr_ble_to_slave; + + // Triggered when there is data written + void onDataWritten(const GattWriteCallbackParams *params) + { + //LED1 will be off when button is pressed, and on when button is released. + led2 = !led2; + uint8_t button_state; + uint8_t direction; + + /* If there is a valid data sent by the mobile app */ + if ((uart != NULL) && (params->handle == uart ->getTXCharacteristicHandle())) { + const uint8_t *packet = params->data; + button_state = packet[3]; + direction = packet[2]; + + // make parameter to send over Lora + onDataWritten_curr_ble_to_slave[0] = direction; + onDataWritten_curr_ble_to_slave[1] = button_state; + + //dprintf("direction: %d\n", direction); + //dprintf("button_state: %d\n", button_state); + } + } +#elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + // No BLE chip on the MAX32620FTHR so this is not needed #endif + +/*************************************************************************** + * Motor Controller Instatiation and Related Functions + **************************************************************************/ +#if defined(TARGET_MAX32630FTHR) // Master Device: MAX32630FTHR BLE-to-LoRa Gateway + // There is no motor on this device. +#elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + /* This is a port of a library that was developed by Adafruit for the Arduino. + * This port was done by mbed user syundo0730. */ + #include "Adafruit_MotorShield.h" + #include "Adafruit_PWMServoDriver.h" + + // These are the defined speeds that are selected out of the number range (0, 255). + #define MOTOR_OFF 0 + #define MOTOR_FORWARD 150 + #define MOTOR_BACKWARD 150 + #define MOTOR_TURN 100 + + // Create the motor shield object with the default I2C address + 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. + // Or, create it with a different I2C address (say for stacking) + // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); + // Select which 'port' M1, M2, M3 or M4. In this case, M1 + Adafruit_DCMotor *rightMotor = AFMS.getMotor(1); + // You can also make another motor on port M4 + Adafruit_DCMotor *leftMotor = AFMS.getMotor(4); + + + void MotorController(uint8_t (BLE_data_server)[2]) + { + uint8_t button_state; + uint8_t direction; + /*Set up constants for direction of motion */ + const char FRONT = '5'; + const char BACK = '6'; + const char LEFT = '7'; + const char RIGHT = '8'; + + /*Set up constants for button state */ + const char PRESSED = '1'; + const char RELEASED = '0'; + + + button_state = BLE_data_server[1]; + direction = BLE_data_server[0]; + + switch (button_state) + { + case PRESSED: + switch (direction) + { + case FRONT: + rightMotor->run(FORWARD); + leftMotor->run(FORWARD); + rightMotor->setSpeed(MOTOR_FORWARD); + leftMotor->setSpeed(MOTOR_FORWARD); + break; + case BACK: + rightMotor->run(BACKWARD); + leftMotor->run(BACKWARD); + rightMotor->setSpeed(MOTOR_BACKWARD); + leftMotor->setSpeed(MOTOR_BACKWARD); + break; + case LEFT: + rightMotor->run(FORWARD); + leftMotor->run(BACKWARD); + rightMotor->setSpeed(MOTOR_TURN); + leftMotor->setSpeed(MOTOR_TURN); + break; + case RIGHT: + rightMotor->run(BACKWARD); + leftMotor->run(FORWARD); + rightMotor->setSpeed(MOTOR_TURN); + leftMotor->setSpeed(MOTOR_TURN); + break; + } + break; + case RELEASED: + rightMotor->setSpeed(MOTOR_OFF); + leftMotor->setSpeed(MOTOR_OFF); + break; + } + } +#endif + + + + + int main() { @@ -305,6 +418,7 @@ #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway uint8_t curr_MAX17055_from_slave[size_of_MAX17055]; uint8_t prev_MAX17055_from_slave[size_of_MAX17055]; + #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot uint8_t curr_MAX17055_to_master[size_of_MAX17055]; #endif @@ -318,6 +432,7 @@ #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot uint8_t curr_MAX77650_to_master[size_of_MAX77650]; #endif + /*************************************************************************** * Finish Setting up LoRa Radios: This passes in pointers to Buffers to send @@ -355,6 +470,65 @@ #endif /*************************************************************************** + * Finish Setting up the MAX77650 on the MAX32620FTHR: Only on the Slave Device + **************************************************************************/ + #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway + // No MAX77650 on the MAX32630FTHR + #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + //Set Power Hold High for battery operation MAX77650 on MAX32620FTHR + pw_Hold = POWER_HOLD_ON; + //Simo Pmic MAX77650 init + simo_pmic.initCharger(); + simoIRQ.fall( &charger_Detect ); + simo_pmic.enCharger(); + bool chrg_status; //True = ON False = OFF + #endif + /*************************************************************************** + * Finish Setting up the MAX17650 on the MAX32620FTHR: Only on the Slave Device + **************************************************************************/ + #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway + // No MAX17055 on the MAX32630FTHR + #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + //rsense 5mOhm for MAX32620FTHR + design_param.designcap = 0x0069; //Design Battery Capacity mAh + design_param.ichgterm = 0x0016; // Charge Termination Current for the Battery + design_param.vempty = 0x9B00; // Battery Empty Voltage + design_param.vcharge = 4200; // Battery Charge Voltage can be obtained from MAX77650 configuration + design_param.rsense = 5; //5mOhms + + int soc_FG, tte_FG, ttf_FG, curr_FG,volt_FG, status, alsValue, proxValue; + int oper_f1, volt_mod_FG; + int alsCAL; //Variables + uint8_t prox_data; + uint8_t proxCAL; //Prox Sensor + int sensor_Case_value; + //serial.printf("Test Init and Temp Functions \r\n"); + //status = fuelGauge.forcedExitHyberMode(); + status = fuelGauge.init(design_param); + pc.printf("Init FuelGauge Function Status= %X \r\n",status); + #endif + + /*************************************************************************** + * Finish Setting up the SSD1306 OLED on the MAX32630FTHR: Only on the Master Device + **************************************************************************/ + #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway + displayOled.setTextColor(1); + displayOled.setTextSize(1); + displayOled.setTextWrap(false); + + displayOled.clearDisplay(); + displayOled.printf("%ux%u OLED Display\r\n", displayOled.width(), displayOled.height()); + //displayOled.drawBitmap(0,0,maximLarge,128,32,1); + displayOled.display(); + displayOled.display(); + wait(1.0); + + displayOled.startscrollright(0x00, 0x0F); + #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot + // No oled on the MAX32620FTHR + #endif + + /*************************************************************************** * Finish Setting up the motor controller on the MAX32620FTHR: Only on the Slave Device **************************************************************************/ #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway @@ -377,20 +551,16 @@ while(1) - { + { /*************************************************************************** * BLE Radio Data **************************************************************************/ #if defined(TARGET_MAX32630FTHR) // Master Device: BLE-to-LoRa Gateway - #if defined(COMPILE_BLE) ble.waitForEvent(); - #endif #elif defined(TARGET_MAX32620FTHR) // Slave Device: LoRa Controlled Robot memcpy(curr_ble_from_master, prev_ble_from_master, sizeof(curr_ble_from_master)); #endif -// wait_ms( 1 ); // This is a tiny delay that enables the Bluetooth connection to be able to detect interrupts - /*************************************************************************** * Grid Eye Sensor **************************************************************************/