with the tof code

Dependencies:   mbed Servo ros_lib_kinetic

Committer:
Stumi
Date:
Tue Nov 19 12:55:44 2019 +0000
Revision:
7:8248af58df5a
Parent:
6:2cc2aac35868
Child:
8:262c6c40bff9
Added blue user button function and LED to show power status

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Stumi 0:6021ec1b1449 1 /*--------------------------------------------------------------------------------
Stumi 0:6021ec1b1449 2 Filename: main.cpp
Stumi 0:6021ec1b1449 3 Description: Main program loop
Stumi 0:6021ec1b1449 4 --------------------------------------------------------------------------------*/
Stumi 0:6021ec1b1449 5 #include "mbed.h"
Stumi 0:6021ec1b1449 6 #include "TOF.h"
Stumi 4:36a04230554d 7 #include "Motor.h"
Stumi 4:36a04230554d 8 #include "power.h"
Stumi 6:2cc2aac35868 9 #include "Buzzer.h"
Stumi 5:bc5081f0c063 10 #include "LED.h"
Stumi 0:6021ec1b1449 11
Stumi 7:8248af58df5a 12 DigitalIn user_button(USER_BUTTON);
Stumi 7:8248af58df5a 13 float power_levels[3]; //Array to voltage levels
Stumi 7:8248af58df5a 14
Stumi 0:6021ec1b1449 15 Serial pc(SERIAL_TX, SERIAL_RX, 9600); // set-up serial to pc
Stumi 1:9bc7f95c3c7d 16
Stumi 7:8248af58df5a 17 Ticker power_monitor;
Stumi 7:8248af58df5a 18
Stumi 1:9bc7f95c3c7d 19 //TOF chip shutdown signals
Stumi 7:8248af58df5a 20 DigitalOut TOF1(PC_8);
Stumi 7:8248af58df5a 21 DigitalOut TOF4(PC_11);
Stumi 7:8248af58df5a 22 DigitalOut TOF6(PC_12);
Stumi 7:8248af58df5a 23 DigitalOut TOF7(PD_2);
Stumi 0:6021ec1b1449 24
Stumi 7:8248af58df5a 25 //Class defines
Stumi 4:36a04230554d 26 cAdafruit_VL6180X VL6180X(TOF1,TOF4,TOF6,TOF7); //Define TOF sensor class and initialise devices
Stumi 4:36a04230554d 27 cPower cPower(VBATT, V5, V3);
Stumi 0:6021ec1b1449 28
Stumi 7:8248af58df5a 29 /*--------------------------------------------------------------------------------
Stumi 7:8248af58df5a 30 Function name: power_check
Stumi 7:8248af58df5a 31 Input Parameters: N/A
Stumi 7:8248af58df5a 32 Output Parameters: N/A
Stumi 7:8248af58df5a 33 Description: Routine interrupt to monitor battery levels
Stumi 7:8248af58df5a 34 ----------------------------------------------------------------------------------*/
Stumi 7:8248af58df5a 35 void power_check()
Stumi 7:8248af58df5a 36 {
Stumi 7:8248af58df5a 37 power_levels[0] = cPower.monitor_battery(); //Monitors raw battery
Stumi 7:8248af58df5a 38 power_levels[1] = cPower.monitor_5v_line(); //Monitors 5V line
Stumi 7:8248af58df5a 39 power_levels[2] = cPower.monitor_3v3_line();//Monitors 3V3 Line
Stumi 7:8248af58df5a 40
Stumi 7:8248af58df5a 41 update_power_levels(power_levels[0]); //Sends the raw battery levels to the LED class
Stumi 7:8248af58df5a 42 }
Stumi 7:8248af58df5a 43
Stumi 7:8248af58df5a 44
Stumi 7:8248af58df5a 45 /*--------------------------------------------------------------------------------
Stumi 7:8248af58df5a 46 MAIN CONTROL LOOP
Stumi 7:8248af58df5a 47 ----------------------------------------------------------------------------------*/
Stumi 0:6021ec1b1449 48 int main()
Stumi 0:6021ec1b1449 49 {
Stumi 7:8248af58df5a 50 power_monitor.attach(power_check, 30.0); //Monitor battery every 30 seconds
Stumi 4:36a04230554d 51 uint8_t TOFRange[4]; //Array to store TOF measurements 0-sensor1, 1-sensor4, 2-sensor6, 3-sensor7
Stumi 0:6021ec1b1449 52
Stumi 7:8248af58df5a 53 //Wait for user button to be pressed
Stumi 7:8248af58df5a 54 pc.printf("Press user button to start\n\r");
Stumi 7:8248af58df5a 55 while(user_button != 1) {}
Stumi 7:8248af58df5a 56
Stumi 7:8248af58df5a 57 while(1) {
Stumi 7:8248af58df5a 58
Stumi 1:9bc7f95c3c7d 59 //Perform TOF measurements
Stumi 4:36a04230554d 60 TOFRange[0] = serviceTOF(VL6180X, ADDR1);
Stumi 4:36a04230554d 61 TOFRange[1] = serviceTOF(VL6180X, ADDR4);
Stumi 4:36a04230554d 62 TOFRange[2] = serviceTOF(VL6180X, ADDR6);
Stumi 4:36a04230554d 63 TOFRange[3] = serviceTOF(VL6180X, ADDR7);
Stumi 4:36a04230554d 64
Stumi 7:8248af58df5a 65 Check_for_obstacles(TOFRange); //Run obstacle avoidance
Stumi 0:6021ec1b1449 66 }
Stumi 0:6021ec1b1449 67 }