-
Notifications
You must be signed in to change notification settings - Fork 0
/
m5Stack_VL53L5CX_3D_Viewer.ino
403 lines (328 loc) · 10.7 KB
/
m5Stack_VL53L5CX_3D_Viewer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
Read an 8x8 array of distances from the VL53L5CX
Output as float[3] array with normalized x,y coordinates + z distance
Display
*/
#include <M5Stack.h>
#include <Wire.h>
#include <SparkFun_VL53L5CX_Library.h> //http://librarymanager/All#SparkFun_VL53L5CX
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef max
#define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef SENSOR_SIZE
#define SENSOR_SIZE 64
#endif
#ifndef SENSOR_FOV
#define SENSOR_FOV 45
#endif
enum Mode { scan, view, off };
Mode currentMode = off;
SparkFun_VL53L5CX myImager;
VL53L5CX_ResultsData measurementData; // Result data class structure, 1356 byes of RAM
int imageResolution = 0; // Used to pretty print output
int imageWidth = 0; // Used to pretty print output
int points_raw[SENSOR_SIZE];
int points_screen[SENSOR_SIZE][3]; // Includes z depth for color
int z_buffer[SENSOR_SIZE]; // Includes z depth for color
float points_3d[SENSOR_SIZE][3];
float angle_deg_x = 0; // rotation around the X axis
float angle_deg_y = 0; // rotation around the Y axis
float angle_deg_z = 90; // rotation around the Z axis
int dir = 1;
float z_offset = 0.0; // offset on Z axis
float cube_size = 150.0; // cube size (multiplier)
float meanDistance = 0.0;
float tmpMeanDistance = 0.0;
int maxDistance = 800;
int maxZValue = 0;
float TZValue = 0;
float LERP_FACTOR = 0.5;
int threshold = 0;
bool haveData = false;
void setup()
{
//Init M5 with Screen on, speaker off, i2c on, serial on
M5.begin(true, false, true, true);
//Delay Startup
delay(500);
//Init wire
Wire.begin(21, 22); // This resets I2C bus to 100kHz
//Set i2c clock
Wire.setClock(1000000); //Sensor has max I2C freq of 1MHz
Serial.println("Initializing sensor board. This can take up to 10s. Please wait.");
if (myImager.begin() == false)
{
Serial.println(F("Sensor not found - check your wiring. Freezing"));
while (1)
;
}
myImager.setResolution(SENSOR_SIZE); // 4*4 or 8*8
imageResolution = myImager.getResolution(); // Query sensor for current resolution - either 4x4 or 8x8
imageWidth = sqrt(imageResolution); // Calculate printing width
// Using 4x4, min frequency is 1Hz and max is 60Hz
// Using 8x8, min frequency is 1Hz and max is 15Hz
myImager.setRangingFrequency(15);
//m5 screen Setup
M5.Lcd.fillScreen(BLACK);
}
//Draw 3d points on the screen
void draw3dPoints() {
//Draw Points
for (int i = 0; i < imageResolution; i++) {
if (points_screen[i][2] <= 0) continue;
int rs = map(points_screen[i][2],0,maxDistance,5,1);
uint16_t shade;
//if we have a point less than mean
if (meanDistance != 0 && points_screen[i][2] < meanDistance){
shade = GREEN;
}
else{
shade = shadeToRGB(map(points_screen[i][2],0,maxDistance,254,10));
}
M5.Lcd.fillCircle(points_screen[i][0], points_screen[i][1], rs, shade);
}
}
//Draw 3d points on the screen
void drawMesh(uint16_t color) {
//Draw Points
int lowerPixel = imageWidth;
int diagPixel = imageWidth + 1;
int nextPixel = 1;
for (int i = 0; i < imageResolution; i++) {
if (points_screen[i][2] <= 0) continue;
//Draw to lower stride
if (i < imageResolution - imageWidth){
M5.Lcd.drawLine(points_screen[i][0], points_screen[i][1], points_screen[lowerPixel][0], points_screen[lowerPixel][1], color);
//if on last Pixel, don't attempt to draw lines to next or diag
if (i % imageWidth != imageWidth - 1){
//Draw to Next adjacent
M5.Lcd.drawLine(points_screen[i][0], points_screen[i][1], points_screen[nextPixel][0], points_screen[nextPixel][1], color);
//Draw to lower stride next adjacent
M5.Lcd.drawLine(points_screen[i][0], points_screen[i][1], points_screen[diagPixel][0], points_screen[diagPixel][1], color);
}
//On last row now
} else if (i % imageWidth != imageWidth - 1){
//Just draw to next
M5.Lcd.drawLine(points_screen[i][0], points_screen[i][1], points_screen[nextPixel][0], points_screen[nextPixel][1], color);
}
lowerPixel++;
diagPixel++;
nextPixel++;
}
}
void drawStats(){
//Draw Stats
M5.Lcd.setCursor(1,3);
M5.Lcd.printf("True Max Z Value = %f",TZValue);
M5.Lcd.setCursor(1,12);
M5.Lcd.printf("Z Offset = %f",z_offset);
}
//Draw 3d points on the screen
void resetScreenPoints() {
for (int i = 0; i < imageResolution; i++) {
if (points_screen[i][2] <= 0) continue;
int rs = map(points_screen[i][2],0,maxDistance,5,1);
M5.Lcd.fillCircle(points_screen[i][0], points_screen[i][1], rs, BLACK);
}
}
void loop()
{
//Try Get Data
if (currentMode == scan) {
DisplaySensorData();
}
else if (currentMode == view) {
//Draw if exists
if (haveData) {
DrawData();
}
}
// increase the angle slowly
// angle_deg_y += 0.1f * dir;
// if (angle_deg_y >= 30 || angle_deg_y <= -30) {
// dir *= -1;
// }
//Let M5 update.
M5.update();
PollButtons();
}
void DrawData() {
//Reset the points on the screen by drawing over them
drawMesh(BLACK);
//resetScreenPoints();
//Transform point array to 3d
calculate3dPoints();
//Draw Mesh first
drawMesh(WHITE);
//Draw on screen
//draw3dPoints();
//Draw stats
drawStats();
delay(5); // Small delay between polling
}
void DisplaySensorData() {
// Poll sensor for new data
if (myImager.isDataReady() == true)
{
if (myImager.getRangingData(&measurementData)) // Read distance data into array
{
for (int i = 0; i < SENSOR_SIZE; i++)
{
//Get Real Coordinates
// Image will be flipped horizontally and vertically in this scenario
int x = i % imageWidth;
int y = i / imageWidth;
//Get mm
int mm = measurementData.distance_mm[i];
//Add points to array, lerp to value by factor
int c_mm = constrain(mm, 0, maxDistance);
points_raw[i] = round(lerp((float)points_raw[i], (float)c_mm, LERP_FACTOR));
}
haveData = true;
DrawData();
}
}
}
float* rotateX(float& y, float& z, float angle)
{
float s = sin(radians(angle));
float c = cos(radians(angle));
float* result = new float[2];
result[0] = y * c - y * s;
result[1] = y * s + z * c;
return result;
}
float* rotateY(float& x, float& z, float angle)
{
float s = sin(radians(angle));
float c = cos(radians(angle));
float* result = new float[2];
result[0] = x * c + z * s;
result[1] = z * c - x * s;
return result;
}
float* rotateZ(float& x, float& y, float angle)
{
float s = sin(radians(angle));
float c = cos(radians(angle));
float* result = new float[2];
result[0] = x * c - y * s;
result[1] = x * s + y * c;
return result;
}
void calculate3dPoints() {
float startAngle = -((float)SENSOR_FOV) / 2;
float angleInc = ((float)SENSOR_FOV) / (imageWidth - 1);
maxZValue = 0;
TZValue = 0;
float sum = 0.0;
for (int i = 0; i < imageResolution; i++) {
//Back into coords
int ix = (i % imageWidth);
int iy = (i / imageWidth);
float rx = deg2rad(startAngle + ix * angleInc);
float ry = deg2rad(startAngle + iy * angleInc);
float dist = points_raw[i];
points_3d[i][0] = dist * sin(rx) * cos(ry);
points_3d[i][1] = dist * sin(ry);
points_3d[i][2] = dist * cos(rx) * cos(ry);
maxZValue = max(maxZValue,(int)points_3d[i][2]);
points_3d[i][2] = points_3d[i][2] + z_offset;
//declare points for transformation
float x1, y1, z1;
x1 = points_3d[i][0];
y1 = points_3d[i][1];
z1 = points_3d[i][2];
//Rotate on Z
float* temp_Rotation = rotateZ(x1, y1, angle_deg_z);
x1 = temp_Rotation[0];
y1 = temp_Rotation[1];
delete temp_Rotation;
//Rotate on Y
temp_Rotation = rotateY(x1, z1, angle_deg_y);
x1 = temp_Rotation[0];
z1 = temp_Rotation[1];
delete temp_Rotation;
//Rotate on X
temp_Rotation = rotateX(y1, z1, angle_deg_x);
y1 = temp_Rotation[0];
z1 = temp_Rotation[1];
delete temp_Rotation;
//reassign
points_3d [i][0] = x1;
points_3d [i][1] = y1;
points_3d [i][2] = z1;
TZValue = max(TZValue,z1);
// project 3d points into 2d space with perspective divide -- 2D x = x/z, 2D y = y/z
// points_screen[i][0] = round(M5.Lcd.width() / 2 + points_3d [i][0] * cube_size / points_3d [i][2]);
// points_screen[i][1] = round(M5.Lcd.height() / 2 + points_3d [i][1] * cube_size / points_3d [i][2]);
// points_screen[i][2] = constrain(map((int)(points_3d [i][2] * 100), 0, maxDistance, 255, 0), 10, 254);
//
points_screen[i][0] = round(M5.Lcd.width() / 2 + points_3d [i][0] * cube_size / points_3d [i][2]);
points_screen[i][1] = round(M5.Lcd.height() / 2 + points_3d [i][1] * cube_size / points_3d [i][2]);
points_screen[i][2] = round(points_3d[i][2]);
sum += points_3d[i][2];
}
tmpMeanDistance = sum / SENSOR_SIZE;
}
void PollButtons() {
if (M5.BtnA.wasReleased()) {
if (currentMode != scan){
wakeImager();
//angle_deg = 0;
z_offset = -30.0;
currentMode = scan;
} else {
setMean();
}
}
//if long press, set threshold based on center average
// if (M5.BtnA.pressedFor(1000, 500)){
// threshold = mainCenterAverage;
// Serial.println("Threshold Set");
// }
if (M5.BtnB.wasReleased() || M5.BtnB.pressedFor(500, 10)) {
// sleepImager();
// currentMode = view;
zoomIn();
}
if (M5.BtnC.wasReleased() || M5.BtnC.pressedFor(500, 10)) {
zoomOut();
}
}
void zoomIn() {
z_offset -= 0.1;
}
void zoomOut() {
z_offset += 0.1;
}
void wakeImager() {
myImager.setPowerMode(SF_VL53L5CX_POWER_MODE::WAKEUP);
myImager.startRanging();
}
void sleepImager() {
myImager.stopRanging();
myImager.setPowerMode(SF_VL53L5CX_POWER_MODE::SLEEP);
}
void setMean() {
meanDistance = tmpMeanDistance;
}
//0-255 value to a greyscale uint_16_t color shade
uint16_t shadeToRGB(int shade) {
uint16_t red_565 = map(shade, 0, 255, 0, 31);
uint16_t green_565 = map(shade, 0, 255, 0, 63);
uint16_t blue_565 = map(shade, 0, 255, 0, 31);
return (uint16_t)(red_565 << 11) | (green_565 << 5) | blue_565;
}
float deg2rad(float d)
{
float pi = 3.14159265359;
return (d * (pi / 180.0));
}
float lerp(float a, float b, float f)
{
return a + f * (b - a);
}