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

Revision:
1:996154b22d5b
Parent:
0:ec1f8c27eda2
Child:
2:0ea3b4cbcfd1
--- a/main.cpp	Wed Jul 14 16:31:09 2021 +0000
+++ b/main.cpp	Tue Jul 20 15:12:06 2021 +0000
@@ -1,6 +1,6 @@
 /*
  * This VL53L3 Expansion board test application performs range measurements
- * using the onboard embedded sensor, and satellite boards, in interrupt mode.
+ * 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.
@@ -34,18 +34,19 @@
 Serial pc(SERIAL_TX, SERIAL_RX);
 #endif
 
-
-#include "53l3a2_ranging_sensor.h"
-
 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 = 1;  // centre
+uint8_t ToF_sensor = SENSOR_CENTRE_NUMBER;  // centre
 
 
 /* Private function prototypes -----------------------------------------------*/
@@ -53,90 +54,76 @@
 
 
 
- void MX_TOF_Init(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); 
+    uint16_t i2c_addr;
+    uint32_t id;
 
-	wait_ms(100);
+    // 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.
-   * once the address is updated, the communication with the devices is checked
-   * reading its ID.
-   */
-
-	sensor->VL53L3A2_RANGING_SetPowerMode(ToF_sensor, RANGING_SENSOR_POWERMODE_ON); 
-	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);
+    }
 
-	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);
-			printf("VL53L3A2_RANGING_SENSOR_ReadID\n");
-	wait_ms(100);
-	id =0;
-
-	sensor->VL53L3A2_RANGING_ReadID(ToF_sensor, &id);
-	printf("#######ToF sensor %d - ID: %04lX\n", 0, id);
-
+    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)
+void MX_TOF_Process(void)
 {
 
     RANGING_SENSOR_Result_t Result;
     RANGING_SENSOR_ProfileConfig_t Profile;
-	
+
     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);
+    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)
-    {
+    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
-    while (1)
-    {
-	  status = sensor->VL53L3A2_RANGING_GetDistance(ToF_sensor, &Result);
-	  if (status == BSP_ERROR_NONE)
-	  {
-		printf("\n |---> ");
-		printf("Status = %ld, Distance = %5ld mm",
-				Result.ZoneResult[0].Status[0],
-				Result.ZoneResult[0].Distance[0]);
-	  }
 
-	  wait_ms(POLLING_PERIOD);
+    // repeatedly read data and start next measurement
+    while (1) {
+	    memset( &Result, 0x0,sizeof(RANGING_SENSOR_Result_t));
+        status = sensor->VL53L3A2_RANGING_GetDistance(ToF_sensor, &Result);
+        if ((status == BSP_ERROR_NONE) && 
+            ( Result.NumberOfZones != 0) && 
+            (Result.ZoneResult[0].Status[0] == BSP_ERROR_NONE )) {
+	            printf("\n |---> ");
+	            printf("Status = %d, Distance = %5d mm",
+	                   Result.ZoneResult[0].Status[0],
+	                   Result.ZoneResult[0].Distance[0]);
+        }
+
+        wait_ms(POLLING_PERIOD);
     }
 }
 
@@ -146,19 +133,18 @@
 =============================================================================*/
 int main()
 {
-	
-	pc.baud(115200);  // baud rate is important as printf statements take a lot of time
-	
-	printf("53L3A2 One Sensor Ranging demo application\n");
-	
-	sensor = new VL53L3A2_SENSOR();
+
+    pc.baud(115200);  // baud rate is important as printf statements take a lot of time
+
+    printf("53L3A2 One Sensor Ranging polling demo application\n");
 
-	MX_TOF_Init();
+    sensor = new VL53L3A2_SENSOR();
+
+    MX_TOF_Init();  // initialise sensor
 
-  while (1)
-  {
-     MX_TOF_Process();
-  }
+    while (1) {
+        MX_TOF_Process(); // start ranging and collect data
+    }
 
 }
 
@@ -169,4 +155,4 @@
     thread_sleep_for(ms);
 }
 #endif
-  
+