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.

Dependencies:   X_NUCLEO_53L3CX

Committer:
charlesmn
Date:
Wed Jul 21 09:33:43 2021 +0000
Revision:
2:0ea3b4cbcfd1
Parent:
1:996154b22d5b
Child:
3:35a4659d0ae4
Correct printing of results

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:ec1f8c27eda2 1 /*
charlesmn 0:ec1f8c27eda2 2 * This VL53L3 Expansion board test application performs range measurements
charlesmn 1:996154b22d5b 3 * using the onboard embedded sensor, in polling mode.
charlesmn 0:ec1f8c27eda2 4 * Measured ranges are output on the Serial Port, running at 115200 baud.
charlesmn 0:ec1f8c27eda2 5 *
charlesmn 0:ec1f8c27eda2 6 * The Reset button can be used to restart the program.
charlesmn 0:ec1f8c27eda2 7 *
charlesmn 0:ec1f8c27eda2 8 * *** Note :
charlesmn 0:ec1f8c27eda2 9 * Default Mbed build system settings disable printf floating-point support.
charlesmn 0:ec1f8c27eda2 10 * Online builds seem unable to configure this.
charlesmn 0:ec1f8c27eda2 11 * Offline builds can enable printf floating-point support.
charlesmn 0:ec1f8c27eda2 12 * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
charlesmn 0:ec1f8c27eda2 13 * .\mbed-os\platform\mbed_lib.json
charlesmn 0:ec1f8c27eda2 14 *
charlesmn 0:ec1f8c27eda2 15 */
charlesmn 0:ec1f8c27eda2 16
charlesmn 0:ec1f8c27eda2 17 #include <stdio.h>
charlesmn 0:ec1f8c27eda2 18 #include <time.h>
charlesmn 0:ec1f8c27eda2 19
charlesmn 0:ec1f8c27eda2 20 #include "mbed.h"
charlesmn 0:ec1f8c27eda2 21
charlesmn 0:ec1f8c27eda2 22 #include "vl53L3_I2c.h"
charlesmn 0:ec1f8c27eda2 23 #include "vl53lx_platform_user_data.h"
charlesmn 0:ec1f8c27eda2 24 #include "53l3a2_ranging_sensor.h"
charlesmn 0:ec1f8c27eda2 25 #include "VL53L3A2_RangingClass.h"
charlesmn 0:ec1f8c27eda2 26
charlesmn 0:ec1f8c27eda2 27 #define BSP_ERROR_NONE 0
charlesmn 0:ec1f8c27eda2 28
charlesmn 0:ec1f8c27eda2 29
charlesmn 0:ec1f8c27eda2 30 #if (MBED_VERSION > 60300)
charlesmn 0:ec1f8c27eda2 31 UnbufferedSerial pc(USBTX, USBRX);
charlesmn 0:ec1f8c27eda2 32 extern "C" void wait_ms(int ms);
charlesmn 0:ec1f8c27eda2 33 #else
charlesmn 0:ec1f8c27eda2 34 Serial pc(SERIAL_TX, SERIAL_RX);
charlesmn 0:ec1f8c27eda2 35 #endif
charlesmn 0:ec1f8c27eda2 36
charlesmn 0:ec1f8c27eda2 37 VL53L3A2_SENSOR *sensor; //class for sensor commands
charlesmn 0:ec1f8c27eda2 38
charlesmn 0:ec1f8c27eda2 39
charlesmn 0:ec1f8c27eda2 40 /* Private define ------------------------------------------------------------*/
charlesmn 0:ec1f8c27eda2 41 #define POLLING_PERIOD (250U)
charlesmn 0:ec1f8c27eda2 42
charlesmn 1:996154b22d5b 43 #define SENSOR_LEFT_NUMBER 0
charlesmn 1:996154b22d5b 44 #define SENSOR_CENTRE_NUMBER 1
charlesmn 1:996154b22d5b 45 #define SENSOR_RIGHT_NUMBER 2
charlesmn 1:996154b22d5b 46
charlesmn 0:ec1f8c27eda2 47 /* Private variables ---------------------------------------------------------*/
charlesmn 0:ec1f8c27eda2 48 static int32_t status = 0;
charlesmn 1:996154b22d5b 49 uint8_t ToF_sensor = SENSOR_CENTRE_NUMBER; // centre
charlesmn 0:ec1f8c27eda2 50
charlesmn 0:ec1f8c27eda2 51
charlesmn 0:ec1f8c27eda2 52 /* Private function prototypes -----------------------------------------------*/
charlesmn 0:ec1f8c27eda2 53 void MX_TOF_Process(void);
charlesmn 0:ec1f8c27eda2 54
charlesmn 0:ec1f8c27eda2 55
charlesmn 0:ec1f8c27eda2 56
charlesmn 1:996154b22d5b 57 void MX_TOF_Init(void)
charlesmn 0:ec1f8c27eda2 58 {
charlesmn 0:ec1f8c27eda2 59
charlesmn 1:996154b22d5b 60 uint16_t i2c_addr;
charlesmn 1:996154b22d5b 61 uint32_t id;
charlesmn 0:ec1f8c27eda2 62
charlesmn 1:996154b22d5b 63 // shut down sensors
charlesmn 1:996154b22d5b 64 sensor->VL53L3A2_RANGING_SetPowerMode(ToF_sensor, RANGING_SENSOR_POWERMODE_OFF);
charlesmn 1:996154b22d5b 65
charlesmn 1:996154b22d5b 66 wait_ms(100);
charlesmn 0:ec1f8c27eda2 67
charlesmn 0:ec1f8c27eda2 68
charlesmn 1:996154b22d5b 69 /* power on the device, initialize it and change it's address to a unique value.
charlesmn 1:996154b22d5b 70 */
charlesmn 1:996154b22d5b 71 sensor->VL53L3A2_RANGING_Init(ToF_sensor);
charlesmn 1:996154b22d5b 72 if (status) {
charlesmn 1:996154b22d5b 73 printf("VL53L3A2_RANGING_SENSOR_Init failed for sensor %d \n",ToF_sensor);
charlesmn 1:996154b22d5b 74 }
charlesmn 0:ec1f8c27eda2 75
charlesmn 1:996154b22d5b 76 wait_ms(100);
charlesmn 1:996154b22d5b 77 i2c_addr = (0x52 + (ToF_sensor + 1) * 2); /* 0x54, 0x56, 0x58 */
charlesmn 1:996154b22d5b 78 sensor->VL53L3A2_RANGING_SetAddress(ToF_sensor, i2c_addr);
charlesmn 1:996154b22d5b 79 wait_ms(100);
charlesmn 0:ec1f8c27eda2 80 }
charlesmn 0:ec1f8c27eda2 81
charlesmn 0:ec1f8c27eda2 82
charlesmn 0:ec1f8c27eda2 83
charlesmn 1:996154b22d5b 84 void MX_TOF_Process(void)
charlesmn 0:ec1f8c27eda2 85 {
charlesmn 0:ec1f8c27eda2 86
charlesmn 0:ec1f8c27eda2 87 RANGING_SENSOR_Result_t Result;
charlesmn 0:ec1f8c27eda2 88 RANGING_SENSOR_ProfileConfig_t Profile;
charlesmn 2:0ea3b4cbcfd1 89 uint16_t target = 0;
charlesmn 2:0ea3b4cbcfd1 90 uint16_t zone = 0; // vl53l3cx has 1 zone
charlesmn 1:996154b22d5b 91
charlesmn 0:ec1f8c27eda2 92 printf("MX_53L3A2_MultiSensorRanging_Process\n");
charlesmn 0:ec1f8c27eda2 93 Profile.RangingProfile = RS_MULTI_TARGET_LONG_RANGE;
charlesmn 0:ec1f8c27eda2 94 Profile.TimingBudget = 30; /* 16 ms < TimingBudget < 500 ms */
charlesmn 0:ec1f8c27eda2 95 Profile.Frequency = 0; /* not necessary in simple ranging */
charlesmn 0:ec1f8c27eda2 96 Profile.EnableAmbient = 0; /* Enable: 1, Disable: 0 */
charlesmn 0:ec1f8c27eda2 97 Profile.EnableSignal = 0; /* Enable: 1, Disable: 0 */
charlesmn 1:996154b22d5b 98 Profile.EnableInterrupt = 0; // disables interupts
charlesmn 0:ec1f8c27eda2 99 printf("MX_53L3A2_MultiSensorRanging_Process start sensors\n");
charlesmn 0:ec1f8c27eda2 100
charlesmn 1:996154b22d5b 101 wait_ms(100);
charlesmn 1:996154b22d5b 102 sensor->VL53L3A2_RANGING_ConfigProfile(ToF_sensor, &Profile);
charlesmn 1:996154b22d5b 103 if (status != BSP_ERROR_NONE) {
charlesmn 1:996154b22d5b 104 printf("VL53L3A2_RANGING_SENSOR_ConfigProfile failed sensor %d status %d\n",ToF_sensor,status);
charlesmn 0:ec1f8c27eda2 105 }
charlesmn 1:996154b22d5b 106
charlesmn 0:ec1f8c27eda2 107 wait_ms(100);
charlesmn 1:996154b22d5b 108 status = sensor->VL53L3A2_RANGING_Start(ToF_sensor, RS_MODE_BLOCKING_CONTINUOUS);
charlesmn 1:996154b22d5b 109 if (status != BSP_ERROR_NONE) {
charlesmn 0:ec1f8c27eda2 110 printf("VL53L3A2_RANGING_SENSOR_Start failed sensor %d status %d\n",ToF_sensor,status);
charlesmn 0:ec1f8c27eda2 111 }
charlesmn 0:ec1f8c27eda2 112
charlesmn 0:ec1f8c27eda2 113 printf("MX_53L3A2_MultiSensorRanging_Process read sensors\n");
charlesmn 0:ec1f8c27eda2 114
charlesmn 1:996154b22d5b 115 // repeatedly read data and start next measurement
charlesmn 2:0ea3b4cbcfd1 116 // Note in VL53L3CX there is only one Zone so NumberOfZones is not used and is always assumed to be 1
charlesmn 1:996154b22d5b 117 while (1) {
charlesmn 1:996154b22d5b 118 memset( &Result, 0x0,sizeof(RANGING_SENSOR_Result_t));
charlesmn 1:996154b22d5b 119 status = sensor->VL53L3A2_RANGING_GetDistance(ToF_sensor, &Result);
charlesmn 1:996154b22d5b 120 if ((status == BSP_ERROR_NONE) &&
charlesmn 2:0ea3b4cbcfd1 121 ( Result.ZoneResult[zone].NumberOfTargets != 0))
charlesmn 2:0ea3b4cbcfd1 122 {
charlesmn 2:0ea3b4cbcfd1 123 for ( target = 0 ; target < Result.ZoneResult[0].NumberOfTargets; target++)
charlesmn 2:0ea3b4cbcfd1 124 {
charlesmn 2:0ea3b4cbcfd1 125 if (Result.ZoneResult[zone].Status[0] == VL53LX_RANGESTATUS_RANGE_VALID )
charlesmn 2:0ea3b4cbcfd1 126 {
charlesmn 2:0ea3b4cbcfd1 127 printf("\n |---> ");
charlesmn 2:0ea3b4cbcfd1 128 printf("Status = %d, Target %d, Distance = %5d mm",
charlesmn 2:0ea3b4cbcfd1 129 Result.ZoneResult[zone].Status[target],
charlesmn 2:0ea3b4cbcfd1 130 target,
charlesmn 2:0ea3b4cbcfd1 131 Result.ZoneResult[zone].Distance[target]);
charlesmn 2:0ea3b4cbcfd1 132 }
charlesmn 2:0ea3b4cbcfd1 133 }
charlesmn 1:996154b22d5b 134 }
charlesmn 1:996154b22d5b 135
charlesmn 1:996154b22d5b 136 wait_ms(POLLING_PERIOD);
charlesmn 0:ec1f8c27eda2 137 }
charlesmn 0:ec1f8c27eda2 138 }
charlesmn 0:ec1f8c27eda2 139
charlesmn 0:ec1f8c27eda2 140
charlesmn 0:ec1f8c27eda2 141
charlesmn 0:ec1f8c27eda2 142 /*=================================== Main ==================================
charlesmn 0:ec1f8c27eda2 143 =============================================================================*/
charlesmn 0:ec1f8c27eda2 144 int main()
charlesmn 0:ec1f8c27eda2 145 {
charlesmn 1:996154b22d5b 146
charlesmn 1:996154b22d5b 147 pc.baud(115200); // baud rate is important as printf statements take a lot of time
charlesmn 1:996154b22d5b 148
charlesmn 1:996154b22d5b 149 printf("53L3A2 One Sensor Ranging polling demo application\n");
charlesmn 0:ec1f8c27eda2 150
charlesmn 1:996154b22d5b 151 sensor = new VL53L3A2_SENSOR();
charlesmn 1:996154b22d5b 152
charlesmn 1:996154b22d5b 153 MX_TOF_Init(); // initialise sensor
charlesmn 0:ec1f8c27eda2 154
charlesmn 1:996154b22d5b 155 while (1) {
charlesmn 1:996154b22d5b 156 MX_TOF_Process(); // start ranging and collect data
charlesmn 1:996154b22d5b 157 }
charlesmn 0:ec1f8c27eda2 158
charlesmn 0:ec1f8c27eda2 159 }
charlesmn 0:ec1f8c27eda2 160
charlesmn 0:ec1f8c27eda2 161 // needed for later versions of mbed
charlesmn 0:ec1f8c27eda2 162 #if (MBED_VERSION > 60300)
charlesmn 0:ec1f8c27eda2 163 extern "C" void wait_ms(int ms)
charlesmn 0:ec1f8c27eda2 164 {
charlesmn 0:ec1f8c27eda2 165 thread_sleep_for(ms);
charlesmn 0:ec1f8c27eda2 166 }
charlesmn 0:ec1f8c27eda2 167 #endif
charlesmn 1:996154b22d5b 168