![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A demonstration program to show the use of a VL53L3CX ToF sensor on a F401 board with a x-nucleo shield. Uses Ranging library. Mbed 6.
main.cpp
- Committer:
- charlesmn
- Date:
- 2021-07-22
- Revision:
- 5:2db43935855e
- Parent:
- 3:35a4659d0ae4
File content as of revision 5:2db43935855e:
/* * This VL53L3 Expansion board test application performs range measurements * using the onboard embedded sensor, in polling mode. * Measured ranges are output on the Serial Port, running at 115200 baud. * * The Reset button can be used to restart the program. * * *** Note : * Default Mbed build system settings disable printf floating-point support. * Online builds seem unable to configure this. * Offline builds can enable printf floating-point support. * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md * .\mbed-os\platform\mbed_lib.json * */ #include <stdio.h> #include <time.h> #include "mbed.h" #include "vl53L3_I2c.h" #include "vl53lx_platform_user_data.h" #include "53l3a2_ranging_sensor.h" #include "VL53L3A2_RangingClass.h" #define BSP_ERROR_NONE 0 #if (MBED_VERSION > 60300) UnbufferedSerial pc(USBTX, USBRX); extern "C" void wait_ms(int ms); #else Serial pc(SERIAL_TX, SERIAL_RX); #endif VL53L3A2_SENSOR *sensor; //class for sensor commands /* Private define ------------------------------------------------------------*/ #define POLLING_PERIOD (250U) #define SENSOR_LEFT_NUMBER 0 #define SENSOR_CENTRE_NUMBER 1 #define SENSOR_RIGHT_NUMBER 2 /* Private variables ---------------------------------------------------------*/ static int32_t status = 0; uint8_t ToF_sensor = SENSOR_CENTRE_NUMBER; // centre /* Private function prototypes -----------------------------------------------*/ void MX_TOF_Process(void); void MX_TOF_Init(void) { uint16_t i2c_addr; uint32_t id; // shut down sensors sensor->VL53L3A2_RANGING_SetPowerMode(ToF_sensor, RANGING_SENSOR_POWERMODE_OFF); wait_ms(100); /* power on the device, initialize it and change it's address to a unique value. */ sensor->VL53L3A2_RANGING_Init(ToF_sensor); if (status) { printf("VL53L3A2_RANGING_SENSOR_Init failed for sensor %d \n",ToF_sensor); } wait_ms(100); i2c_addr = (0x52 + (ToF_sensor + 1) * 2); /* 0x54, 0x56, 0x58 */ sensor->VL53L3A2_RANGING_SetAddress(ToF_sensor, i2c_addr); wait_ms(100); } void MX_TOF_Process(void) { RANGING_SENSOR_Result_t Result; RANGING_SENSOR_ProfileConfig_t Profile; uint16_t target = 0; uint16_t zone = 0; // vl53l3cx has 1 zone printf("MX_53L3A2_MultiSensorRanging_Process\n"); Profile.RangingProfile = RS_MULTI_TARGET_LONG_RANGE; Profile.TimingBudget = 30; /* 16 ms < TimingBudget < 500 ms */ Profile.Frequency = 0; /* not necessary in simple ranging */ Profile.EnableAmbient = 0; /* Enable: 1, Disable: 0 */ Profile.EnableSignal = 0; /* Enable: 1, Disable: 0 */ Profile.EnableInterrupt = 0; // disables interupts printf("MX_53L3A2_MultiSensorRanging_Process start sensors\n"); wait_ms(100); sensor->VL53L3A2_RANGING_ConfigProfile(ToF_sensor, &Profile); if (status != BSP_ERROR_NONE) { printf("VL53L3A2_RANGING_SENSOR_ConfigProfile failed sensor %d status %d\n",ToF_sensor,status); } wait_ms(100); status = sensor->VL53L3A2_RANGING_Start(ToF_sensor, RS_MODE_BLOCKING_CONTINUOUS); if (status != BSP_ERROR_NONE) { printf("VL53L3A2_RANGING_SENSOR_Start failed sensor %d status %d\n",ToF_sensor,status); } printf("MX_53L3A2_MultiSensorRanging_Process read sensors\n"); // repeatedly read data and start next measurement // Note in VL53L3CX there is only one Zone so NumberOfZones is not used and is always assumed to be 1 while (1) { memset( &Result, 0x0,sizeof(RANGING_SENSOR_Result_t)); status = sensor->VL53L3A2_RANGING_GetDistance(ToF_sensor, &Result); if ((status == BSP_ERROR_NONE) && ( Result.ZoneResult[zone].NumberOfTargets != 0)) { for ( target = 0 ; target < Result.ZoneResult[0].NumberOfTargets; target++) { if (Result.ZoneResult[zone].Status[target] == VL53LX_RANGESTATUS_RANGE_VALID ) { printf("\n |---> "); printf("Status = %d, Target %d, Distance = %5d mm", Result.ZoneResult[zone].Status[target], target, Result.ZoneResult[zone].Distance[target]); } } } wait_ms(POLLING_PERIOD); } } /*=================================== Main ================================== =============================================================================*/ int main() { pc.baud(115200); // baud rate is important as printf statements take a lot of time printf("53L3A2 One Sensor Ranging polling demo application\n"); sensor = new VL53L3A2_SENSOR(); MX_TOF_Init(); // initialise sensor while (1) { MX_TOF_Process(); // start ranging and collect data } } // needed for later versions of mbed #if (MBED_VERSION > 60300) extern "C" void wait_ms(int ms) { thread_sleep_for(ms); } #endif