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

main.cpp

Committer:
charlesmn
Date:
2021-07-14
Revision:
0:ec1f8c27eda2
Child:
1:996154b22d5b

File content as of revision 0:ec1f8c27eda2:

/*
 * This VL53L3 Expansion board test application performs range measurements
 * using the onboard embedded sensor, and satellite boards, in interrupt 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


#include "53l3a2_ranging_sensor.h"

VL53L3A2_SENSOR *sensor; //class for sensor commands


/* Private define ------------------------------------------------------------*/
#define POLLING_PERIOD              (250U)

/* Private variables ---------------------------------------------------------*/
static int32_t status = 0;
uint8_t ToF_sensor = 1;  // 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.
   * 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);
	

	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);

}



 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 */
    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
    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);
    }
}



/*=================================== Main ==================================
=============================================================================*/
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();

	MX_TOF_Init();

  while (1)
  {
     MX_TOF_Process();
  }

}

// needed for later versions of mbed
#if (MBED_VERSION  > 60300)
extern "C" void wait_ms(int ms)
{
    thread_sleep_for(ms);
}
#endif