Simple ranging example, using polling, with only the X-Nucleo_53L1A1 expansion board.

Dependencies:   X_NUCLEO_53L1A1_mbed

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.
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     uint16_t distance_centre = 0;
00051 
00052     printf("Hello world!\r\n");
00053 
00054     vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
00055 
00056     printf("I2C device created!\r\n");
00057 
00058     /* creates the 53L1A1 expansion board singleton obj */
00059     board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2);
00060     printf("board created!\r\n");
00061 
00062     /* init the 53L1A1 expansion board with default values */
00063     status = board->init_board();
00064     if (status) {
00065         printf("Failed to init board!\r\n");
00066         return status;
00067     }
00068 
00069     printf("board initiated! - %d\r\n", status);
00070 
00071     /* Start ranging on the centre sensor */
00072     if (board->sensor_centre != NULL) {
00073         status = board->sensor_centre->VL53L1X_StartRanging();
00074         if (status != 0) {
00075             printf("Centre sensor failed to start ranging!\r\n");
00076             return status;
00077         }
00078     }
00079 
00080     /* Ranging loop
00081      * Checks each sensor for data ready
00082      */
00083     while (1)
00084     {
00085         if (polling_stop)
00086         {
00087             printf("\r\nEnding loop mode \r\n");
00088             break;
00089         }
00090         if (board->sensor_centre != NULL) {
00091             board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre);
00092         }
00093         if (ready_centre) {
00094             board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre);
00095             board->sensor_centre->VL53L1X_GetDistance(&distance_centre);
00096         }
00097 
00098         if (board->sensor_centre != NULL) {
00099             printf("Distance read by sensor is : %d\r\n", distance_centre);
00100         }
00101     }
00102 
00103     return status;
00104 }