Skip to content

Commit b7310c2

Browse files
heyisulaonelicodes
andcommitted
Add obstacle avoidance and monitoring modes
Introduced new obstacle avoidance and monitoring system modes with dedicated source files. Updated main.cpp to use ObstacleAvoidance, refactored sensor and actuator initialization, and added telemetry output. Adjusted color sensor thresholds and logic for improved detection, updated pin assignments, and revised motor speed constants for better control. Co-Authored-By: Oneli Wijesuriya <247418039+onelicodes@users.noreply.github.com>
1 parent 928cf17 commit b7310c2

11 files changed

Lines changed: 1176 additions & 135 deletions

File tree

ESP32-S3-Main/src/config/constants.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
#define FOLLOW_DISTANCE_MAX 200 // cm
1717
#define FOLLOW_DISTANCE_DEFAULT 100 // cm
1818

19-
// Navigation Constants
20-
#define LINE_THRESHOLD 512 // Analog threshold for line detection
21-
#define MAX_WAYPOINTS 10
22-
#define POSITION_PRECISION 5 // cm
23-
2419
// Battery Constants
2520
#define BATTERY_LOW_VOLTAGE 10.5 // V
2621
#define BATTERY_CRITICAL_VOLTAGE 10.0 // V
@@ -31,8 +26,8 @@
3126
#define HEARTBEAT_INTERVAL 1000 // ms
3227

3328
// Motor Speed Constants
34-
#define MOTOR_SPEED_MIN 20 // %
35-
#define MOTOR_SPEED_MAX 100 // %
29+
#define MOTOR_SPEED_MIN 100 // %
30+
#define MOTOR_SPEED_MAX 255 // %
3631
#define MOTOR_SPEED_DEFAULT 50 // %
3732

3833
// Health Monitoring Constants

ESP32-S3-Main/src/config/pins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#define ROTARY_CLK 38
4747

4848
// Color Sensor
49-
#define COLOR_OUT 0
49+
#define COLOR_OUT 14
5050
#define COLOR_S0 47
5151
#define COLOR_S1 48
5252
#define COLOR_S2 20

ESP32-S3-Main/src/config/thresholds.h

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,22 @@
99
#define LIGHT_MIN 200 // Lux
1010
#define LIGHT_MAX 800 // Lux
1111

12-
// Color Detection Thresholds
13-
#define BLACK_SUM_MAX 10 // Below this = too dark to read
14-
// WHITE (Patients) - all channels relatively high AND similar
15-
#define WHITE_R_MIN 12
16-
#define WHITE_G_MIN 12
17-
#define WHITE_B_MIN 12
18-
#define WHITE_BALANCE_TOLERANCE 6 // R, G, B should be within 5 of each other
19-
// BLUE (Surgeons/Doctors) - blue higher than red
20-
#define BLUE_B_MIN 10
21-
#define BLUE_R_MAX 8
22-
#define BLUE_DIFF_MIN 3 // Blue should be at least 3 higher than red
23-
// GREEN (Nurses) - green higher than others
24-
#define GREEN_G_MIN 8
25-
#define GREEN_R_MAX 6
26-
#define GREEN_B_MAX 6
27-
#define GREEN_DIFF_MIN 2 // Green should be at least 2 higher
28-
// RED (Medical Students) - red higher than green
29-
#define RED_R_MIN 8
30-
#define RED_G_MAX 5
31-
#define RED_DIFF_MIN 3 // Red should be at least 3 higher than green
32-
// PURPLE (Minor Staff) - both red and blue high, green low
33-
#define PURPLE_R_MIN 8
34-
#define PURPLE_B_MIN 8
35-
#define PURPLE_G_MAX 5
36-
#define PURPLE_SUM_MIN 20 // Total must be reasonably high
12+
// Color Sensor Thresholds
13+
#define BLACK_SUM_MAX 30
14+
#define WHITE_R_MIN 25
15+
#define WHITE_G_MIN 25
16+
#define WHITE_B_MIN 30
17+
#define WHITE_BALANCE_TOLERANCE 15
18+
#define BLUE_B_MIN 18
19+
#define BLUE_DIFF_MIN 5
20+
#define GREEN_G_MIN 12
21+
#define GREEN_DIFF_MIN 3
22+
#define RED_R_MIN 20
23+
#define RED_DIFF_MIN 10
3724
// Temporal averaging
38-
#define COLOR_AVG_SAMPLES 5
39-
// Auto-calibration
40-
#define AMBIENT_ADAPT_RATE 0.05f // Slow adaptation
41-
#define AMBIENT_UPDATE_THRESHOLD 15 // Ignore strong colors
25+
#define COLOR_AVG_SAMPLES 5
26+
#define AMBIENT_ADAPT_RATE 0.05f
27+
#define AMBIENT_UPDATE_THRESHOLD 15
4228

4329

4430
// Collision Detection

ESP32-S3-Main/src/main.cpp

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,100 @@
11
#include <Arduino.h>
2+
3+
#include "modes/obstacle_avoidance.h"
4+
#include "sensors/hcsr04.h"
25
#include "sensors/mpu6050.h"
6+
#include "communication/uart.h"
7+
#include "actuators/ermc1604syg.h"
8+
#include "actuators/sfm27.h"
39

10+
// --------------------------------------------------
11+
// Global subsystem instances
12+
// --------------------------------------------------
13+
UltrasonicManager ultrasonicManager;
414
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+
);
629

30+
// --------------------------------------------------
31+
// Setup
32+
// --------------------------------------------------
733
void setup() {
834
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.");
2845
}
2946

47+
// --------------------------------------------------
48+
// Loop
49+
// --------------------------------------------------
3050
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);
51100
}

0 commit comments

Comments
 (0)