|
1 | 1 | #include <Arduino.h> |
| 2 | + |
| 3 | +#include "modes/obstacle_avoidance.h" |
| 4 | +#include "sensors/hcsr04.h" |
2 | 5 | #include "sensors/mpu6050.h" |
| 6 | +#include "communication/uart.h" |
| 7 | +#include "actuators/ermc1604syg.h" |
| 8 | +#include "actuators/sfm27.h" |
3 | 9 |
|
| 10 | +// -------------------------------------------------- |
| 11 | +// Global subsystem instances |
| 12 | +// -------------------------------------------------- |
| 13 | +UltrasonicManager ultrasonicManager; |
4 | 14 | MotionTracker motionTracker; |
5 | | -unsigned long lastPrintTime = 0; |
| 15 | +UARTProtocol uartProtocol; |
| 16 | +Display display; |
| 17 | +Buzzer buzzer; |
| 18 | + |
| 19 | +// -------------------------------------------------- |
| 20 | +// Obstacle Avoidance controller |
| 21 | +// -------------------------------------------------- |
| 22 | +ObstacleAvoidance obstacleAvoidance( |
| 23 | + &ultrasonicManager, |
| 24 | + &motionTracker, |
| 25 | + &uartProtocol, |
| 26 | + &display, |
| 27 | + &buzzer |
| 28 | +); |
6 | 29 |
|
| 30 | +// -------------------------------------------------- |
| 31 | +// Setup |
| 32 | +// -------------------------------------------------- |
7 | 33 | void setup() { |
8 | 34 | Serial.begin(115200); |
9 | | - while (!Serial && millis() < 3000); |
10 | | - |
11 | | - Serial.println("\n=== MPU6050 Motion Tracker ==="); |
12 | | - Serial.println("Initializing...\n"); |
13 | | - |
14 | | - if (!motionTracker.begin()) { |
15 | | - Serial.println("ERROR: MPU6050 initialization failed!"); |
16 | | - while (1) delay(1000); |
17 | | - } |
18 | | - |
19 | | - Serial.println("MPU6050 initialized successfully!"); |
20 | | - |
21 | | - motionTracker.autoCalibrate(); |
22 | | - |
23 | | - Serial.println("--- Starting measurements ---\n"); |
24 | | - Serial.println("Time(s) | Roll(°) | Pitch(°) | Fwd(g) | Side(g) | Mag(g) | RawX | RawY | RawZ"); |
25 | | - Serial.println("--------|---------|----------|--------|---------|--------|------|------|------"); |
26 | | - |
27 | | - lastPrintTime = millis(); |
| 35 | + delay(1000); |
| 36 | + |
| 37 | + Serial.println(); |
| 38 | + Serial.println("======================================"); |
| 39 | + Serial.println(" Obstacle Avoidance - SYSTEM TEST "); |
| 40 | + Serial.println("======================================"); |
| 41 | + |
| 42 | + obstacleAvoidance.begin(); |
| 43 | + |
| 44 | + Serial.println("Initialization complete."); |
28 | 45 | } |
29 | 46 |
|
| 47 | +// -------------------------------------------------- |
| 48 | +// Loop |
| 49 | +// -------------------------------------------------- |
30 | 50 | void loop() { |
31 | | - motionTracker.update(); |
32 | | - |
33 | | - if (millis() - lastPrintTime >= 100) { |
34 | | - lastPrintTime = millis(); |
35 | | - |
36 | | - float roll = motionTracker.getRoll(); |
37 | | - float pitch = motionTracker.getPitch(); |
38 | | - float fwd = motionTracker.getForwardAcceleration(); |
39 | | - float side = motionTracker.getSideAcceleration(); |
40 | | - float mag = motionTracker.getAccelMagnitude(); |
41 | | - float rawX = motionTracker.getAccelX(); |
42 | | - float rawY = motionTracker.getAccelY(); |
43 | | - float rawZ = motionTracker.getAccelZ(); |
44 | | - |
45 | | - char buf[150]; |
46 | | - snprintf(buf, sizeof(buf), |
47 | | - "%7.2f | %7.2f | %8.2f | %6.3f | %7.3f | %6.3f | %4.2f | %4.2f | %4.2f", |
48 | | - millis()/1000.0, roll, pitch, fwd, side, mag, rawX, rawY, rawZ); |
49 | | - Serial.println(buf); |
50 | | - } |
| 51 | + obstacleAvoidance.update(); |
| 52 | + |
| 53 | + // ---------------------------------------------- |
| 54 | + // Read back telemetry from ObstacleAvoidance |
| 55 | + // ---------------------------------------------- |
| 56 | + ObstacleAvoidance::SensorData data = obstacleAvoidance.getSensorData(); |
| 57 | + |
| 58 | + Serial.print("[STATUS="); |
| 59 | + Serial.print(data.status); |
| 60 | + Serial.print("] "); |
| 61 | + |
| 62 | + Serial.print("SPD="); |
| 63 | + Serial.print(data.speed); |
| 64 | + Serial.print("% | "); |
| 65 | + |
| 66 | + Serial.print("F:"); |
| 67 | + Serial.print(data.frontDist, 1); |
| 68 | + Serial.print("cm "); |
| 69 | + |
| 70 | + Serial.print("B:"); |
| 71 | + Serial.print(data.rearDist, 1); |
| 72 | + Serial.print("cm "); |
| 73 | + |
| 74 | + Serial.print("L:"); |
| 75 | + Serial.print(data.leftDist, 1); |
| 76 | + Serial.print("cm "); |
| 77 | + |
| 78 | + Serial.print("R:"); |
| 79 | + Serial.print(data.rightDist, 1); |
| 80 | + Serial.print("cm | "); |
| 81 | + |
| 82 | + Serial.print("Pitch:"); |
| 83 | + Serial.print(data.pitch, 1); |
| 84 | + Serial.print(" "); |
| 85 | + |
| 86 | + Serial.print("Roll:"); |
| 87 | + Serial.print(data.roll, 1); |
| 88 | + Serial.print(" | "); |
| 89 | + |
| 90 | + Serial.print("AccX:"); |
| 91 | + Serial.print(data.forwardAccel, 2); |
| 92 | + Serial.print(" "); |
| 93 | + |
| 94 | + Serial.print("AccY:"); |
| 95 | + Serial.print(data.sideAccel, 2); |
| 96 | + |
| 97 | + Serial.println(); |
| 98 | + |
| 99 | + delay(250); |
51 | 100 | } |
0 commit comments