forked from waveshareteam/ugv_base_ros
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpathfinder.ino
More file actions
464 lines (382 loc) · 12.6 KB
/
pathfinder.ino
File metadata and controls
464 lines (382 loc) · 12.6 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/**
*
* @file pathfinder.ino
* @brief ESP32-based mobile edge computing platform
* @url https://go.workloads.io/pathfinder
* @version 1.3.5
*
*/
#ifndef CORE_DEBUG_LEVEL
// Levels `INFO`, `DEBUG`, and `VERBOSE` may result in a performance decrease
#define CORE_DEBUG_LEVEL LOG_INFO
#endif
#include <ArduinoJson.h>
JsonDocument jsonCmdReceive;
JsonDocument jsonInfoSend;
JsonDocument jsonInfoHttp;
JsonDocument apiResponse;
#include <SCServo.h>
#include <nvs_flash.h>
#include <esp_system.h>
#include <LittleFS.h>
#include <WiFi.h>
#include <WebServer.h>
#include <nvs_flash.h>
#include <Adafruit_SSD1306.h>
#include <INA219_WE.h>
#include <ESP32Encoder.h>
#include <PID_v2.h>
#include <SimpleKalmanFilter.h>
#include <math.h>
#include "ICM_20948.h"
#include "src/battery.h"
#include "src/config.h"
#include "src/oled.h"
#include "src/led.h"
#include "src/json.h"
#include "src/imu.h"
#include "src/motion.h"
#include "src/files.h"
#include "src/extras.h"
#include "src/wifi.h"
#include "src/api.h"
#include "src/api_v2.h"
#include "src/uart.h"
#include "src/http_server.h"
void setup() {
Serial.begin(115200);
while(!Serial) {}
Wire.begin(S_SDA, S_SCL, 400000);
bool initialized = false;
while (!initialized) {
myICM.begin(Wire, AD0_VAL);
Serial.print(F("Initialization of the sensor returned: "));
Serial.println(myICM.statusString());
if (myICM.status != ICM_20948_Stat_Ok) {
Serial.println(F("Trying again..."));
delay(500);
}
else {
initialized = true;
}
}
Serial.println(F("Device connected!"));
bool success = true; // Use success to show if the DMP configuration was successful
// Initialize the DMP. initializeDMP is a weak function. You can overwrite it if you want to e.g. to change the sample rate
success &= (myICM.initializeDMP() == ICM_20948_Stat_Ok);
// Enable the DMP orientation sensor
success &= (myICM.enableDMPSensor(INV_ICM20948_SENSOR_ORIENTATION) == ICM_20948_Stat_Ok);
if (InfoPrint == 1) {
Serial.print("Orientation sensor: ");
Serial.println(myICM.statusString());
}
success &= (myICM.enableDMPSensor(INV_ICM20948_SENSOR_RAW_GYROSCOPE) == ICM_20948_Stat_Ok);
if (InfoPrint == 1) {
Serial.print("Raw gyroscope sensor: ");
Serial.println(myICM.statusString());
}
success &= (myICM.enableDMPSensor(INV_ICM20948_SENSOR_RAW_ACCELEROMETER) == ICM_20948_Stat_Ok);
if (InfoPrint == 1) {
Serial.print("Raw accelerometer sensor: ");
Serial.println(myICM.statusString());
}
// +
// Temporarily disable magnetometer sensor to focus on accel/gyro
// success &= (myICM.enableDMPSensor(INV_ICM20948_SENSOR_MAGNETIC_FIELD_UNCALIBRATED) == ICM_20948_Stat_Ok);
if (InfoPrint == 1) {
Serial.println("Magnetometer sensor: DISABLED for now");
}
// Configuring DMP to output data at multiple ODRs:
// DMP is capable of outputting multiple sensor data at different rates to FIFO.
// Setting value can be calculated as follows:
// Value = (DMP running rate / ODR ) - 1
// E.g. For a 5Hz ODR rate when DMP is running at 55Hz, value = (55/5) - 1 = 10.
success &= (myICM.setDMPODRrate(DMP_ODR_Reg_Quat9, 3) == ICM_20948_Stat_Ok); // Set to the maximum
success &= (myICM.setDMPODRrate(DMP_ODR_Reg_Accel, 3) == ICM_20948_Stat_Ok); // Set to the maximum
success &= (myICM.setDMPODRrate(DMP_ODR_Reg_Gyro, 3) == ICM_20948_Stat_Ok); // Set to the maximum
// +
success &= (myICM.setDMPODRrate(DMP_ODR_Reg_Cpass, 3) == ICM_20948_Stat_Ok);
success &= (myICM.setDMPODRrate(DMP_ODR_Reg_Cpass_Calibr, 3) == ICM_20948_Stat_Ok);
myICM.lowPower(false);
// Enable the FIFO
success &= (myICM.enableFIFO() == ICM_20948_Stat_Ok);
// Enable the DMP
success &= (myICM.enableDMP() == ICM_20948_Stat_Ok);
// Reset DMP
success &= (myICM.resetDMP() == ICM_20948_Stat_Ok);
// Reset FIFO
success &= (myICM.resetFIFO() == ICM_20948_Stat_Ok);
// Check success
if (success) {
Serial.println(F("DMP enabled!"));
} else {
Serial.println(F("Enable DMP failed!"));
Serial.println(F("Please check that you have uncommented line 29 (#define ICM_20948_USE_DMP) in ICM_20948_C.h..."));
Serial.print(F("DMP initialization status: "));
Serial.println(myICM.statusString());
}
ina219_init();
inaDataUpdate();
// set mainType & moduleType.
// mainType: 1.RaspRover, 2.UGV Rover, 3.UGV Beast
// moduleType: 0.Null, 2.PT
mm_settings(mainType, moduleType);
init_oled();
screenLine_0 = "Pathfinder";
screenLine_1 = "version: 1.3.6";
screenLine_2 = "starting...";
screenLine_3 = "";
oled_update();
delay(1200);
// functions for IMU.
// imu_init();
// functions for the leds on ugv.
led_pin_init();
// init the littleFS funcs in files_ctrl.h
screenLine_2 = screenLine_3;
screenLine_3 = "Initialize LittleFS";
oled_update();
if (InfoPrint == 1) {
Serial.println("Initialize LittleFS for Flash files ctrl.");
}
initFS();
// init the funcs in switch_module.h
screenLine_2 = screenLine_3;
screenLine_3 = "Initialize 12V-switch ctrl";
oled_update();
if (InfoPrint == 1) {
Serial.println("Initialize the pins used for 12V-switch ctrl.");
}
motionPinInit();
// servos power up
screenLine_2 = screenLine_3;
screenLine_3 = "Power up the servos";
oled_update();
if (InfoPrint == 1) {
Serial.println("Power up the servos.");
}
delay(500);
screenLine_3 = "WiFi init";
oled_update();
if (InfoPrint == 1) {
Serial.println("WiFi init.");
}
initWifi();
screenLine_3 = "http & web init";
oled_update();
if (InfoPrint == 1) {
Serial.println("http & web init.");
}
initHttpWebServer();
screenLine_3 = "IMU Calibrating";
oled_update();
if (InfoPrint == 1) {
Serial.println("IMU Calibrating");
}
imuCalibration();
screenLine_3 = "Pathfinder started";
oled_update();
if (InfoPrint == 1) {
Serial.println("Pathfinder started.");
}
updateOledWifiInfo();
initEncoders();
pidControllerInit();
screenLine_2 = "Pathfinder ready.";
oled_update();
led_pwm_ctrl(0, 0);
if (InfoPrint == 1) {
Serial.println("Application initialization settings.");
}
createMission("boot", "these cmds run automatically at boot.");
missionPlay("boot", 1);
}
void loop() {
icm_20948_DMP_data_t data;
myICM.readDMPdataFromFIFO(&data);
if ((myICM.status == ICM_20948_Stat_Ok) || (myICM.status == ICM_20948_Stat_FIFOMoreDataAvail)) {
// Debug: Print which data types are available
if (InfoPrint == 1) {
Serial.print("DMP Header: 0x");
Serial.print(data.header, HEX);
Serial.print(" - Quat9: ");
Serial.print((data.header & DMP_header_bitmap_Quat9) > 0 ? "YES" : "NO");
Serial.print(" Accel: ");
Serial.print((data.header & DMP_header_bitmap_Accel) > 0 ? "YES" : "NO");
Serial.print(" Gyro: ");
Serial.print((data.header & DMP_header_bitmap_Gyro) > 0 ? "YES" : "NO");
Serial.print(" Compass: ");
Serial.println((data.header & DMP_header_bitmap_Compass) > 0 ? "YES" : "NO");
}
// Try to read raw sensor data directly (not from DMP)
if (myICM.dataReady()) {
myICM.getAGMT();
ax = myICM.agmt.acc.axes.x;
ay = myICM.agmt.acc.axes.y;
az = myICM.agmt.acc.axes.z;
gx = myICM.agmt.gyr.axes.x;
gy = myICM.agmt.gyr.axes.y;
gz = myICM.agmt.gyr.axes.z;
mx = myICM.agmt.mag.axes.x;
my = myICM.agmt.mag.axes.y;
mz = myICM.agmt.mag.axes.z;
// Calculate roll, pitch, yaw from raw accelerometer data
// Convert to m/s^2 using configurable scale factor
double accel_x = ax / IMU_ACCEL_SCALE_FACTOR;
double accel_y = ay / IMU_ACCEL_SCALE_FACTOR;
double accel_z = az / IMU_ACCEL_SCALE_FACTOR;
// Calculate roll and pitch from accelerometer
icm_roll = atan2(accel_y, sqrt(accel_x * accel_x + accel_z * accel_z));
icm_pitch = atan2(-accel_x, sqrt(accel_y * accel_y + accel_z * accel_z));
// For yaw, we would need magnetometer data, but for now set to 0
// icm_yaw = atan2(mx * cos(icm_pitch) + my * sin(icm_roll) * sin(icm_pitch) + mz * cos(icm_roll) * sin(icm_pitch),
// my * cos(icm_roll) - mz * sin(icm_roll));
icm_yaw = 0.0; // Set to 0 for now
// Check IMU safety - stop movement if pitch exceeds limit
checkIMUSafety();
if (InfoPrint == 1) {
Serial.print("Raw Accel: X=");
Serial.print(ax);
Serial.print(" Y=");
Serial.print(ay);
Serial.print(" Z=");
Serial.println(az);
Serial.print("Raw Gyro: X=");
Serial.print(gx);
Serial.print(" Y=");
Serial.print(gy);
Serial.print(" Z=");
Serial.println(gz);
Serial.print("Raw Mag: X=");
Serial.print(mx);
Serial.print(" Y=");
Serial.print(my);
Serial.print(" Z=");
Serial.println(mz);
Serial.print("Calculated Roll: ");
Serial.print(icm_roll * 180.0 / PI, 2);
Serial.print("° Pitch: ");
Serial.print(icm_pitch * 180.0 / PI, 2);
Serial.print("° Yaw: ");
Serial.print(icm_yaw * 180.0 / PI, 2);
Serial.println("°");
}
}
if ((data.header & DMP_header_bitmap_Quat9) > 0) {
// Scale to +/- 1
// Convert to double. Divide by 2^30
q1 = ((double)data.Quat9.Data.Q1) / 1073741824.0;
q2 = ((double)data.Quat9.Data.Q2) / 1073741824.0;
q3 = ((double)data.Quat9.Data.Q3) / 1073741824.0;
double quat_sum_sq = (q1 * q1) + (q2 * q2) + (q3 * q3);
if (quat_sum_sq <= 1.0) {
q0 = sqrt(1.0 - quat_sum_sq);
} else {
q0 = 0.0; // Handle invalid quaternion
}
if (InfoPrint == 1) {
Serial.print("Raw Quat: Q0=");
Serial.print(q0, 6);
Serial.print(" Q1=");
Serial.print(q1, 6);
Serial.print(" Q2=");
Serial.print(q2, 6);
Serial.print(" Q3=");
Serial.println(q3, 6);
Serial.print("Quat sum sq: ");
Serial.println(quat_sum_sq, 6);
}
// Check for valid quaternion before calculating angles
if (!isnan(q0) && !isnan(q1) && !isnan(q2) && !isnan(q3)) {
q2sqr = q2 * q2;
// roll (x-axis rotation)
t0 = +2.0 * (q0 * q1 + q2 * q3);
t1 = +1.0 - 2.0 * (q1 * q1 + q2sqr);
icm_roll = atan2(t0, t1);
// pitch (y-axis rotation)
t2 = +2.0 * (q0 * q2 - q3 * q1);
t2 = t2 > 1.0 ? 1.0 : t2;
t2 = t2 < -1.0 ? -1.0 : t2;
icm_pitch = asin(t2);
// yaw (z-axis rotation)
t3 = +2.0 * (q0 * q3 + q1 * q2);
t4 = +1.0 - 2.0 * (q2sqr + q3 * q3);
icm_yaw = atan2(t3, t4);
} else {
icm_roll = 0.0;
icm_pitch = 0.0;
icm_yaw = 0.0;
if (InfoPrint == 1) {
Serial.println("Invalid quaternion - setting angles to 0");
}
}
Serial.print(F("r:"));
Serial.print(icm_roll, 2);
Serial.print(F(" p:"));
Serial.print(icm_pitch, 2);
Serial.print(F(" y:"));
Serial.println(icm_yaw, 2);
}
if ((data.header & DMP_header_bitmap_Accel) > 0) {
ax = data.Raw_Accel.Data.X;
ay = data.Raw_Accel.Data.Y;
az = data.Raw_Accel.Data.Z;
if (InfoPrint == 1) {
Serial.print("Accel: X=");
Serial.print(ax);
Serial.print(" Y=");
Serial.print(ay);
Serial.print(" Z=");
Serial.println(az);
}
}
if ((data.header & DMP_header_bitmap_Gyro) > 0) {
gx = data.Raw_Gyro.Data.X;
gy = data.Raw_Gyro.Data.Y;
gz = data.Raw_Gyro.Data.Z;
if (InfoPrint == 1) {
Serial.print("Gyro: X=");
Serial.print(gx);
Serial.print(" Y=");
Serial.print(gy);
Serial.print(" Z=");
Serial.println(gz);
}
}
// Temporarily disable magnetometer data reading
/*
if ((data.header & DMP_header_bitmap_Compass) > 0) {
mx = data.Compass.Data.X;
my = data.Compass.Data.Y;
mz = data.Compass.Data.Z;
if (InfoPrint == 1) {
Serial.print("Compass: X=");
Serial.print(mx);
Serial.print(" Y=");
Serial.print(my);
Serial.print(" Z=");
Serial.println(mz);
}
}
*/
}
serialCtrl();
server.handleClient();
// Check WiFi status and attempt reconnection if needed
checkWifiAndReconnect();
if(runNewJsonCmd) {
jsonCmdReceiveHandler();
jsonCmdReceive.clear();
runNewJsonCmd = false;
}
getLeftSpeed();
LeftPidControllerCompute();
getRightSpeed();
RightPidControllerCompute();
oledInfoUpdate();
// Send sensor data to web interface
if (baseFeedbackFlow) {
baseInfoFeedback();
}
heartBeatCtrl();
}