O7/TOF/App/Example_1_basic_ranging.c

224 lines
7.4 KiB
C

/*
Copyright (c) 2021, STMicroelectronics - All Rights Reserved
This file : part of VL53L1X ULP and : dual licensed,
either 'STMicroelectronics
Proprietary license'
or 'BSD 3-clause "New" or "Revised" License' , at your option.
*******************************************************************************
'STMicroelectronics Proprietary license'
*******************************************************************************
License terms: STMicroelectronics Proprietary in accordance with licensing
terms at www.st.com/sla0081
STMicroelectronics confidential
Reproduction and Communication of this document : strictly prohibited unless
specifically authorized in writing by STMicroelectronics.
*******************************************************************************
Alternatively, VL53L1X ULP may be distributed under the terms of
'BSD 3-clause "New" or "Revised" License', in which case the following
provisions apply instead of the ones mentioned above :
*******************************************************************************
License terms: BSD 3-clause "New" or "Revised" License.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************
*/
/****************************************/
/* VL53L1X Low Power basic ranging */
/****************************************/
/*
* This example shows how to use the VL53L1X Ultra Low Power driver in a very
* simple example. It initializes the VL53L1X driver, configure the sensor and
* starts a ranging to capture 200 frames.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "sys_app.h"
#include "VL53L1X_ULP_api.h"
extern uint8_t ToF_EventDetected;
/*
uint8_t IsInterruptDetected(uint16_t dev)
{
// To be filled with customer HW. This function should
// return 1 when an interrupt is raised by the ToF on GPIO1 pin (pin7)
if (ToF_EventDetected )
{
APP_LOG(TS_OFF, VLEVEL_L,"############### TOF EVENT DETECTED \r\n");
ToF_EventDetected =0;
return 1;
} else {
return 0;
}
}
*/
int Example_1_basic_ranging(void)
{ /*
int sts_vl53lx_ranging(uint8_t vl_model, uint8_t range_mode,
uint16_t distance_threshold_mm, uint16_t inter_measurement_ms, uint16_t macro_timing,
uint16_t roi_width, uint16_t sigma_mm, uint16_t signal_kcps, uint16_t i_ambient_kcps);
*/
/*********************************/
/* VL53L1X ranging variables */
/*********************************/
uint8_t status, loop;
uint8_t dev;
uint16_t sensor_id;
uint8_t measurement_status;
uint16_t estimated_distance_mm, signal_kcps, sigma_mm, ambient_kcps;
/*********************************/
/* Customer platform */
/*********************************/
/* Default VL53L1X Ultra Low Power I2C address */
dev = 0x52;
/* (Optional) Change I2C address */
// status = VL53L1X_ULP_SetI2CAddress(dev, 0x52); //0x20
// dev = 0x20;
/*********************************/
/* Power on sensor and init */
/*********************************/
/* (Optional) Check if there is a VL53L1X sensor connected */
status = VL53L1X_ULP_GetSensorId(dev, &sensor_id);
APP_LOG(TS_OFF,VLEVEL_L,"VL53L1X address =%X\r\n",sensor_id );
if(status || (sensor_id != 0xEACC))
{
APP_LOG(TS_OFF,VLEVEL_L,"VL53L1X not detected at requested address\n");
return status;
}
/* (Mandatory) Init VL53L1X sensor */
status = VL53L1X_ULP_SensorInit(dev);
if(status)
{
APP_LOG(TS_OFF,VLEVEL_L,"VL53L1X ultra low power Loading failed\n");
//HAL_Delay(100);
return status;
}
APP_LOG(TS_OFF,VLEVEL_L,"VL53L1X ultra low power ready ! \r\n");
/*********************************/
/* Sensor configuration */
/*********************************/
/* (Optional) Program sensor to raise an interrupt ONLY below 300mm */
status = VL53L1X_ULP_SetInterruptConfiguration(dev, 800, 1); //i_distance_threshold_mm
/* (Optional) Program a 10Hz ranging frequency */
status = VL53L1X_ULP_SetInterMeasurementInMs(dev, 100); // range_interval_ms
{
/* Increase the macro timing. This is equivalent as increasing the integration time */
status = VL53L1X_ULP_SetMacroTiming(dev, 100); // micro_timing_ms
/* Enable all the SPADS */
status = VL53L1X_ULP_SetROI(dev, 16); // SPADS { 1 -- 16 }
// STS---001 for ultra low power
/* Reduce the macro timing to minimum. This is equivalent as reducing the integration time */
//status = VL53L1X_ULP_SetMacroTiming(dev, 1);
/* Reduce at maximum the SPADS */
//status = VL53L1X_ULP_SetROI(dev, 4);
// STS ---002 for short distance
/* Example for robust and short distance measurements. Max distance reduced
* but very low number of false-positives */
status |= VL53L1X_ULP_SetSigmaThreshold(dev, 30);
status |= VL53L1X_ULP_SetSignalThreshold(dev, 2000);
// STS --- 003 for long range
/* Relax some limits. Be careful, it can create false-positives !*/
//status |= VL53L1X_ULP_SetSigmaThreshold(dev, 60);
//status |= VL53L1X_ULP_SetSignalThreshold(dev, 1200);
}
/*********************************/
/* Ranging loop */
/*********************************/
status = VL53L1X_ULP_StartRanging(dev);
if(status)
{
APP_LOG(TS_OFF,VLEVEL_L,"VL53L1X_ULP_StartRanging failed with status %u\n", status);
return status;
}
APP_LOG(TS_OFF,VLEVEL_L,"Ranging started. Put your hand close to the sensor to generate an interrupt...\n");
loop = 0;
while(loop < 20)
{
/* Use this external function to detect when a hardware interrupt is
* generated on PIN 7 (GPIO1). It means that a new measurement is ready.
*/
//if(IsInterruptDetected(dev))
{
/* (Mandatory) Clear HW interrupt to restart measurements */
VL53L1X_ULP_ClearInterrupt(dev);
/* Dump debug data */
status = VL53L1X_ULP_DumpDebugData(dev, &measurement_status,
&estimated_distance_mm, &sigma_mm, &signal_kcps, &ambient_kcps);
APP_LOG(TS_OFF,VLEVEL_L,"Target detected! Interrupt raised by sensor, Distance =%d mm \r\n", estimated_distance_mm );
loop++;
}
}
status = VL53L1X_ULP_StopRanging(dev);
APP_LOG(TS_OFF,VLEVEL_L,"End of VL53L1X ultra low power demo\n");
return status;
}