1+ /* *
2+ ******************************************************************************
3+ * @file VL53L8CH_CNHData.ino
4+ * @brief Example Arduino sketch using VL53L8CH class API with CNH feature.
5+ ******************************************************************************
6+ */
7+
8+ #include < vl53l8ch.h>
9+ #include < vl53lmz_plugin_cnh.h>
10+
11+ #ifdef ARDUINO_SAM_DUE
12+ #define DEV_I2C Wire1
13+ #else
14+ #define DEV_I2C Wire
15+ #endif
16+ #define SerialPort Serial
17+
18+ #define LPN_PIN A3
19+ #define PWREN_PIN 11
20+
21+ VL53L8CH sensor (&DEV_I2C, LPN_PIN);
22+
23+ VL53LMZ_Motion_Configuration cnh_config;
24+ cnh_data_buffer_t cnh_data_buffer;
25+ uint32_t cnh_data_size = 0 ;
26+
27+ int32_t *p_hist = NULL ;
28+ int8_t *p_hist_scaler = NULL ;
29+ int32_t *p_ambient = NULL ;
30+ int8_t *p_ambient_scaler = NULL ;
31+
32+ uint8_t status;
33+ uint8_t resolution = VL53LMZ_RESOLUTION_4X4;
34+
35+ void setup ()
36+ {
37+ SerialPort.begin (460800 );
38+
39+ if (PWREN_PIN >= 0 ) {
40+ pinMode (PWREN_PIN, OUTPUT);
41+ digitalWrite (PWREN_PIN, HIGH);
42+ delay (10 );
43+ }
44+
45+ DEV_I2C.begin ();
46+
47+ status = sensor.begin ();
48+ if (status != VL53LMZ_STATUS_OK) {
49+ SerialPort.println (" Sensor begin failed" );
50+ while (1 );
51+ }
52+
53+ if (status != VL53LMZ_STATUS_OK) {
54+ SerialPort.println (" Sensor init failed" );
55+ while (1 );
56+ }
57+
58+ status = sensor.set_resolution (resolution);
59+ if (status != VL53LMZ_STATUS_OK) {
60+ SerialPort.println (" Set resolution failed" );
61+ while (1 );
62+ }
63+
64+ status = sensor.cnh_init_config (&cnh_config, 0 , 24 , 4 );
65+ if (status != VL53LMZ_STATUS_OK) {
66+ SerialPort.println (" CNH init config failed" );
67+ while (1 );
68+ }
69+
70+ status = sensor.cnh_create_agg_map (&cnh_config,
71+ 16 , 0 , 0 , 1 , 1 , 4 , 4 );
72+ if (status != VL53LMZ_STATUS_OK) {
73+ SerialPort.println (" CNH create agg map failed" );
74+ while (1 );
75+ }
76+
77+ status = sensor.cnh_calc_required_memory (&cnh_config, &cnh_data_size);
78+ if (status != VL53LMZ_STATUS_OK || cnh_data_size > VL53LMZ_CNH_MAX_DATA_BYTES) {
79+ SerialPort.println (" CNH memory size error" );
80+ while (1 );
81+ }
82+
83+ status = sensor.cnh_send_config (&cnh_config);
84+ if (status != VL53LMZ_STATUS_OK) {
85+ SerialPort.println (" CNH send config failed" );
86+ while (1 );
87+ }
88+
89+ status = sensor.create_output_config ();
90+ if (status != VL53LMZ_STATUS_OK) {
91+ SerialPort.println (" Create output config failed" );
92+ while (1 );
93+ }
94+
95+ union Block_header cnh_data_bh;
96+ cnh_data_bh.idx = VL53LMZ_CNH_DATA_IDX;
97+ cnh_data_bh.type = 4 ;
98+ cnh_data_bh.size = cnh_data_size / 4 ;
99+
100+ status = sensor.add_output_block (cnh_data_bh.bytes );
101+ if (status != VL53LMZ_STATUS_OK) {
102+ SerialPort.println (" Add output block CNH failed" );
103+ while (1 );
104+ }
105+
106+ status = sensor.send_output_config_and_start ();
107+ if (status != VL53LMZ_STATUS_OK) {
108+ SerialPort.println (" Start ranging failed" );
109+ while (1 );
110+ }
111+
112+ SerialPort.println (" Sensor and CNH configured, ranging started" );
113+ }
114+
115+ void loop ()
116+ {
117+ VL53LMZ_ResultsData results;
118+ uint8_t data_ready = 0 ;
119+
120+ do {
121+ status = sensor.check_data_ready (&data_ready);
122+ } while (!data_ready);
123+
124+ if (status == VL53LMZ_STATUS_OK && data_ready) {
125+ status = sensor.get_ranging_data (&results);
126+ if (status == VL53LMZ_STATUS_OK) {
127+ print_results (&results);
128+ }
129+
130+ status = sensor.results_extract_block (VL53LMZ_CNH_DATA_IDX, (uint8_t *)cnh_data_buffer, cnh_data_size);
131+ if (status == VL53LMZ_STATUS_OK) {
132+ for (int agg_id = 0 ; agg_id < cnh_config.nb_of_aggregates ; agg_id++) {
133+ sensor.cnh_get_block_addresses (&cnh_config, agg_id, cnh_data_buffer,
134+ &p_hist, &p_hist_scaler,
135+ &p_ambient, &p_ambient_scaler);
136+
137+ float ambient = ((float ) * p_ambient) / (1 << *p_ambient_scaler);
138+ SerialPort.print (" Aggregate " );
139+ SerialPort.print (agg_id);
140+ SerialPort.print (" , Ambient: " );
141+ SerialPort.print (ambient, 1 );
142+ SerialPort.print (" , Bins: " );
143+
144+ for (int bin = 0 ; bin < cnh_config.feature_length ; bin++) {
145+ float bin_val = ((float )p_hist[bin]) / (1 << p_hist_scaler[bin]);
146+ SerialPort.print (bin_val, 1 );
147+ SerialPort.print (" , " );
148+ }
149+ SerialPort.println ();
150+ }
151+ } else {
152+ SerialPort.println (" Failed to extract CNH data block" );
153+ }
154+ }
155+ delay (100 );
156+ }
157+
158+ void print_results (VL53LMZ_ResultsData *res)
159+ {
160+ SerialPort.println (" Ranging Results:" );
161+ for (int i = 0 ; i < resolution; i++) {
162+ if (res->nb_target_detected [i] > 0 ) {
163+ SerialPort.print (" Zone " );
164+ SerialPort.print (i);
165+ SerialPort.print (" : Distance=" );
166+ SerialPort.print (res->distance_mm [i]);
167+ SerialPort.print (" mm, Status=" );
168+ SerialPort.println (res->target_status [i]);
169+ } else {
170+ SerialPort.print (" Zone " );
171+ SerialPort.print (i);
172+ SerialPort.println (" : No target" );
173+ }
174+ }
175+ }
0 commit comments