Simple Ranging example, using Polling, with Expansion Board and Satellite Boards.

Dependencies:   X_NUCLEO_53L1A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  *  This VL53L1X Expansion board test application performs range measurements
00003  *  using the onboard embedded centre sensor, and 2 Satellite boards.
00004  *
00005  *  Measured ranges are ouput on the Serial Port, running at 115200 baud.
00006  *
00007  *  The Black Reset button is used to restart the program.
00008  *
00009  *  *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
00010  *            the X-NUCELO-53L0A1 expansion board are not made/OFF.
00011  *            These links must be made to allow interrupts from the Satellite boards
00012  *            to be received.
00013  *            U11 and U18 must be made/ON to allow interrupts to be received from the
00014  *            INT_L & INT_R positions; or
00015  *            U10 and U15 must be made/ON to allow interrupts to be received from the
00016  *            Alternate INT_L & INT_R positions.
00017  *            The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R
00018  *            positions.
00019  *            INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9.
00020  *            Alternate INT_L is on CN5 Connector pin 2 as D8.
00021  *            INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4.
00022  *            Alternate INT_R is on CN9 Connector pin 5 as D2.
00023  *            The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/
00024  */
00025 
00026 #include <stdio.h>
00027 
00028 #include "mbed.h"
00029 #include "XNucleo53L1A1.h"
00030 #include "VL53L1X_I2C.h"
00031 
00032 #define VL53L1_I2C_SDA   D14
00033 #define VL53L1_I2C_SCL   D15
00034 
00035 static XNucleo53L1A1 *board=NULL;
00036 
00037 volatile bool polling_stop = false;
00038 
00039 void stop_polling(void)
00040 {
00041     polling_stop = true;
00042 }
00043 
00044 /*=================================== Main ==================================
00045 =============================================================================*/
00046 int main()
00047 {
00048     int status = 0;
00049     uint8_t ready_centre = 0;
00050     uint8_t ready_left = 0;
00051     uint8_t ready_right = 0;
00052     uint16_t distance_centre = 0;
00053     uint16_t distance_left = 0;
00054     uint16_t distance_right = 0;
00055 
00056     printf("Hello world!\r\n");
00057 
00058     VL53L1X_DevI2C *dev_I2C = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
00059 
00060     printf("I2C device created!\r\n");
00061 
00062     /* creates the 53L1A1 expansion board singleton obj */
00063     board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2);
00064     printf("board created!\r\n");
00065 
00066     /* init the 53L1A1 expansion board with default values */
00067     status = board->init_board();
00068     if (status) {
00069         printf("Failed to init board!\r\n");
00070         return status;
00071     }
00072 
00073     printf("board initiated! - %d\r\n", status);
00074 
00075     /* Start ranging on the centre sensor */
00076     if (board->sensor_centre != NULL) {
00077         status = board->sensor_centre->vl53l1x_start_ranging();
00078         if (status != 0) {
00079             printf("Centre sensor failed to start ranging!\r\n");
00080             return status;
00081         }
00082     }
00083 
00084     /* Start ranging on the left sensor */
00085     if (board->sensor_left != NULL) {
00086         status = board->sensor_left->vl53l1x_start_ranging();
00087         if (status != 0) {
00088             printf("Left sensor failed to start ranging!\r\n");
00089             return status;
00090         }
00091     }
00092 
00093     /* Start ranging on the right sensor */
00094     if (board->sensor_right != NULL) {
00095         status = board->sensor_right->vl53l1x_start_ranging();
00096         if (status != 0) {
00097             printf("Right sensor failed to start ranging!\r\n");
00098             return status;
00099         }
00100     }
00101 
00102     /* Ranging loop
00103      * Checks each sensor for data ready
00104      */
00105     while (1)
00106     {
00107         if (polling_stop)
00108         {
00109             printf("\r\nEnding loop mode \r\n");
00110             break;
00111         }
00112         if (board->sensor_centre != NULL) {
00113             board->sensor_centre->vl53l1x_check_for_data_ready(&ready_centre);
00114         }
00115         if (board->sensor_left != NULL) {
00116             board->sensor_left->vl53l1x_check_for_data_ready(&ready_left);
00117         }
00118         if (board->sensor_right != NULL) {
00119             board->sensor_right->vl53l1x_check_for_data_ready(&ready_right);
00120         }
00121         if (ready_centre) {
00122             board->sensor_centre->vl53l1x_get_range_status(&ready_centre);
00123             board->sensor_centre->vl53l1x_get_distance(&distance_centre);
00124         }
00125         if (ready_left) {
00126             board->sensor_left->vl53l1x_get_range_status(&ready_left);
00127             board->sensor_left->vl53l1x_get_distance(&distance_left);
00128         }
00129         if (ready_right) {
00130             board->sensor_right->vl53l1x_get_range_status(&ready_right);
00131             board->sensor_right->vl53l1x_get_distance(&distance_right);
00132         }
00133 
00134         if (board->sensor_centre != NULL) {
00135             printf("Distance read by centre sensor is : %d\r\n", distance_centre);
00136         }
00137         if (board->sensor_left != NULL) {
00138             printf("Distance read by left satellite sensor is : %d\r\n", distance_left);
00139         }
00140         if (board->sensor_right != NULL) {
00141             printf("Distance read by right satellite sensor is : %d\r\n", distance_right);
00142         }
00143     }
00144 
00145     return status;
00146 }