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@0:ec1f8c27eda2, 2021-07-14 (annotated)
- Committer:
- charlesmn
- Date:
- Wed Jul 14 16:31:09 2021 +0000
- Revision:
- 0:ec1f8c27eda2
- Child:
- 1:996154b22d5b
First release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:ec1f8c27eda2 | 1 | /* |
charlesmn | 0:ec1f8c27eda2 | 2 | * This VL53L3 Expansion board test application performs range measurements |
charlesmn | 0:ec1f8c27eda2 | 3 | * using the onboard embedded sensor, and satellite boards, in interrupt 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 | |
charlesmn | 0:ec1f8c27eda2 | 38 | #include "53l3a2_ranging_sensor.h" |
charlesmn | 0:ec1f8c27eda2 | 39 | |
charlesmn | 0:ec1f8c27eda2 | 40 | VL53L3A2_SENSOR *sensor; //class for sensor commands |
charlesmn | 0:ec1f8c27eda2 | 41 | |
charlesmn | 0:ec1f8c27eda2 | 42 | |
charlesmn | 0:ec1f8c27eda2 | 43 | /* Private define ------------------------------------------------------------*/ |
charlesmn | 0:ec1f8c27eda2 | 44 | #define POLLING_PERIOD (250U) |
charlesmn | 0:ec1f8c27eda2 | 45 | |
charlesmn | 0:ec1f8c27eda2 | 46 | /* Private variables ---------------------------------------------------------*/ |
charlesmn | 0:ec1f8c27eda2 | 47 | static int32_t status = 0; |
charlesmn | 0:ec1f8c27eda2 | 48 | uint8_t ToF_sensor = 1; // centre |
charlesmn | 0:ec1f8c27eda2 | 49 | |
charlesmn | 0:ec1f8c27eda2 | 50 | |
charlesmn | 0:ec1f8c27eda2 | 51 | /* Private function prototypes -----------------------------------------------*/ |
charlesmn | 0:ec1f8c27eda2 | 52 | void MX_TOF_Process(void); |
charlesmn | 0:ec1f8c27eda2 | 53 | |
charlesmn | 0:ec1f8c27eda2 | 54 | |
charlesmn | 0:ec1f8c27eda2 | 55 | |
charlesmn | 0:ec1f8c27eda2 | 56 | void MX_TOF_Init(void) |
charlesmn | 0:ec1f8c27eda2 | 57 | { |
charlesmn | 0:ec1f8c27eda2 | 58 | |
charlesmn | 0:ec1f8c27eda2 | 59 | uint16_t i2c_addr; |
charlesmn | 0:ec1f8c27eda2 | 60 | uint32_t id; |
charlesmn | 0:ec1f8c27eda2 | 61 | |
charlesmn | 0:ec1f8c27eda2 | 62 | // shut down sensors |
charlesmn | 0:ec1f8c27eda2 | 63 | sensor->VL53L3A2_RANGING_SetPowerMode(ToF_sensor, RANGING_SENSOR_POWERMODE_OFF); |
charlesmn | 0:ec1f8c27eda2 | 64 | |
charlesmn | 0:ec1f8c27eda2 | 65 | wait_ms(100); |
charlesmn | 0:ec1f8c27eda2 | 66 | |
charlesmn | 0:ec1f8c27eda2 | 67 | |
charlesmn | 0:ec1f8c27eda2 | 68 | /* power on the device, initialize it and change it's address. |
charlesmn | 0:ec1f8c27eda2 | 69 | * once the address is updated, the communication with the devices is checked |
charlesmn | 0:ec1f8c27eda2 | 70 | * reading its ID. |
charlesmn | 0:ec1f8c27eda2 | 71 | */ |
charlesmn | 0:ec1f8c27eda2 | 72 | |
charlesmn | 0:ec1f8c27eda2 | 73 | sensor->VL53L3A2_RANGING_SetPowerMode(ToF_sensor, RANGING_SENSOR_POWERMODE_ON); |
charlesmn | 0:ec1f8c27eda2 | 74 | wait_ms(100); |
charlesmn | 0:ec1f8c27eda2 | 75 | |
charlesmn | 0:ec1f8c27eda2 | 76 | |
charlesmn | 0:ec1f8c27eda2 | 77 | sensor->VL53L3A2_RANGING_Init(ToF_sensor); |
charlesmn | 0:ec1f8c27eda2 | 78 | if (status) |
charlesmn | 0:ec1f8c27eda2 | 79 | { |
charlesmn | 0:ec1f8c27eda2 | 80 | printf("VL53L3A2_RANGING_SENSOR_Init failed for sensor %d \n",ToF_sensor); |
charlesmn | 0:ec1f8c27eda2 | 81 | } |
charlesmn | 0:ec1f8c27eda2 | 82 | |
charlesmn | 0:ec1f8c27eda2 | 83 | wait_ms(100); |
charlesmn | 0:ec1f8c27eda2 | 84 | i2c_addr = (0x52 + (ToF_sensor + 1) * 2); /* 0x54, 0x56, 0x58 */ |
charlesmn | 0:ec1f8c27eda2 | 85 | sensor->VL53L3A2_RANGING_SetAddress(ToF_sensor, i2c_addr); |
charlesmn | 0:ec1f8c27eda2 | 86 | printf("VL53L3A2_RANGING_SENSOR_ReadID\n"); |
charlesmn | 0:ec1f8c27eda2 | 87 | wait_ms(100); |
charlesmn | 0:ec1f8c27eda2 | 88 | id =0; |
charlesmn | 0:ec1f8c27eda2 | 89 | |
charlesmn | 0:ec1f8c27eda2 | 90 | sensor->VL53L3A2_RANGING_ReadID(ToF_sensor, &id); |
charlesmn | 0:ec1f8c27eda2 | 91 | printf("#######ToF sensor %d - ID: %04lX\n", 0, id); |
charlesmn | 0:ec1f8c27eda2 | 92 | |
charlesmn | 0:ec1f8c27eda2 | 93 | } |
charlesmn | 0:ec1f8c27eda2 | 94 | |
charlesmn | 0:ec1f8c27eda2 | 95 | |
charlesmn | 0:ec1f8c27eda2 | 96 | |
charlesmn | 0:ec1f8c27eda2 | 97 | void MX_TOF_Process(void) |
charlesmn | 0:ec1f8c27eda2 | 98 | { |
charlesmn | 0:ec1f8c27eda2 | 99 | |
charlesmn | 0:ec1f8c27eda2 | 100 | RANGING_SENSOR_Result_t Result; |
charlesmn | 0:ec1f8c27eda2 | 101 | RANGING_SENSOR_ProfileConfig_t Profile; |
charlesmn | 0:ec1f8c27eda2 | 102 | |
charlesmn | 0:ec1f8c27eda2 | 103 | printf("MX_53L3A2_MultiSensorRanging_Process\n"); |
charlesmn | 0:ec1f8c27eda2 | 104 | Profile.RangingProfile = RS_MULTI_TARGET_LONG_RANGE; |
charlesmn | 0:ec1f8c27eda2 | 105 | Profile.TimingBudget = 30; /* 16 ms < TimingBudget < 500 ms */ |
charlesmn | 0:ec1f8c27eda2 | 106 | Profile.Frequency = 0; /* not necessary in simple ranging */ |
charlesmn | 0:ec1f8c27eda2 | 107 | Profile.EnableAmbient = 0; /* Enable: 1, Disable: 0 */ |
charlesmn | 0:ec1f8c27eda2 | 108 | Profile.EnableSignal = 0; /* Enable: 1, Disable: 0 */ |
charlesmn | 0:ec1f8c27eda2 | 109 | printf("MX_53L3A2_MultiSensorRanging_Process start sensors\n"); |
charlesmn | 0:ec1f8c27eda2 | 110 | |
charlesmn | 0:ec1f8c27eda2 | 111 | wait_ms(100); |
charlesmn | 0:ec1f8c27eda2 | 112 | sensor->VL53L3A2_RANGING_ConfigProfile(ToF_sensor, &Profile); |
charlesmn | 0:ec1f8c27eda2 | 113 | if (status != BSP_ERROR_NONE) |
charlesmn | 0:ec1f8c27eda2 | 114 | { |
charlesmn | 0:ec1f8c27eda2 | 115 | printf("VL53L3A2_RANGING_SENSOR_ConfigProfile failed sensor %d status %d\n",ToF_sensor,status); |
charlesmn | 0:ec1f8c27eda2 | 116 | } |
charlesmn | 0:ec1f8c27eda2 | 117 | |
charlesmn | 0:ec1f8c27eda2 | 118 | wait_ms(100); |
charlesmn | 0:ec1f8c27eda2 | 119 | status = sensor->VL53L3A2_RANGING_Start(ToF_sensor, RS_MODE_BLOCKING_CONTINUOUS); |
charlesmn | 0:ec1f8c27eda2 | 120 | if (status != BSP_ERROR_NONE) |
charlesmn | 0:ec1f8c27eda2 | 121 | { |
charlesmn | 0:ec1f8c27eda2 | 122 | printf("VL53L3A2_RANGING_SENSOR_Start failed sensor %d status %d\n",ToF_sensor,status); |
charlesmn | 0:ec1f8c27eda2 | 123 | } |
charlesmn | 0:ec1f8c27eda2 | 124 | |
charlesmn | 0:ec1f8c27eda2 | 125 | printf("MX_53L3A2_MultiSensorRanging_Process read sensors\n"); |
charlesmn | 0:ec1f8c27eda2 | 126 | |
charlesmn | 0:ec1f8c27eda2 | 127 | // repeatedly read data and start next measurement |
charlesmn | 0:ec1f8c27eda2 | 128 | while (1) |
charlesmn | 0:ec1f8c27eda2 | 129 | { |
charlesmn | 0:ec1f8c27eda2 | 130 | status = sensor->VL53L3A2_RANGING_GetDistance(ToF_sensor, &Result); |
charlesmn | 0:ec1f8c27eda2 | 131 | if (status == BSP_ERROR_NONE) |
charlesmn | 0:ec1f8c27eda2 | 132 | { |
charlesmn | 0:ec1f8c27eda2 | 133 | printf("\n |---> "); |
charlesmn | 0:ec1f8c27eda2 | 134 | printf("Status = %ld, Distance = %5ld mm", |
charlesmn | 0:ec1f8c27eda2 | 135 | Result.ZoneResult[0].Status[0], |
charlesmn | 0:ec1f8c27eda2 | 136 | Result.ZoneResult[0].Distance[0]); |
charlesmn | 0:ec1f8c27eda2 | 137 | } |
charlesmn | 0:ec1f8c27eda2 | 138 | |
charlesmn | 0:ec1f8c27eda2 | 139 | wait_ms(POLLING_PERIOD); |
charlesmn | 0:ec1f8c27eda2 | 140 | } |
charlesmn | 0:ec1f8c27eda2 | 141 | } |
charlesmn | 0:ec1f8c27eda2 | 142 | |
charlesmn | 0:ec1f8c27eda2 | 143 | |
charlesmn | 0:ec1f8c27eda2 | 144 | |
charlesmn | 0:ec1f8c27eda2 | 145 | /*=================================== Main ================================== |
charlesmn | 0:ec1f8c27eda2 | 146 | =============================================================================*/ |
charlesmn | 0:ec1f8c27eda2 | 147 | int main() |
charlesmn | 0:ec1f8c27eda2 | 148 | { |
charlesmn | 0:ec1f8c27eda2 | 149 | |
charlesmn | 0:ec1f8c27eda2 | 150 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:ec1f8c27eda2 | 151 | |
charlesmn | 0:ec1f8c27eda2 | 152 | printf("53L3A2 One Sensor Ranging demo application\n"); |
charlesmn | 0:ec1f8c27eda2 | 153 | |
charlesmn | 0:ec1f8c27eda2 | 154 | sensor = new VL53L3A2_SENSOR(); |
charlesmn | 0:ec1f8c27eda2 | 155 | |
charlesmn | 0:ec1f8c27eda2 | 156 | MX_TOF_Init(); |
charlesmn | 0:ec1f8c27eda2 | 157 | |
charlesmn | 0:ec1f8c27eda2 | 158 | while (1) |
charlesmn | 0:ec1f8c27eda2 | 159 | { |
charlesmn | 0:ec1f8c27eda2 | 160 | MX_TOF_Process(); |
charlesmn | 0:ec1f8c27eda2 | 161 | } |
charlesmn | 0:ec1f8c27eda2 | 162 | |
charlesmn | 0:ec1f8c27eda2 | 163 | } |
charlesmn | 0:ec1f8c27eda2 | 164 | |
charlesmn | 0:ec1f8c27eda2 | 165 | // needed for later versions of mbed |
charlesmn | 0:ec1f8c27eda2 | 166 | #if (MBED_VERSION > 60300) |
charlesmn | 0:ec1f8c27eda2 | 167 | extern "C" void wait_ms(int ms) |
charlesmn | 0:ec1f8c27eda2 | 168 | { |
charlesmn | 0:ec1f8c27eda2 | 169 | thread_sleep_for(ms); |
charlesmn | 0:ec1f8c27eda2 | 170 | } |
charlesmn | 0:ec1f8c27eda2 | 171 | #endif |
charlesmn | 0:ec1f8c27eda2 | 172 |