Skip to content

Commit 22ce36a

Browse files
heyisulaonelicodes
andcommitted
Refactor and enhance system initialization and UI
Major refactor of main.cpp to improve initialization order, add detailed serial feedback, and integrate menu and rotary encoder UI. Updated display logic and status reporting in assistant and monitoring modes. Improved UART protocol with acknowledgment tracking and connection status. Adjusted constants and thresholds for sensors and safety. Cleaned up comments and removed redundant I2C initializations from sensor drivers. Added new menu system implementation and improved actuator and sensor update intervals for more responsive operation. Co-Authored-By: Oneli Wijesuriya <247418039+onelicodes@users.noreply.github.com>
1 parent fbf451b commit 22ce36a

34 files changed

Lines changed: 599 additions & 215 deletions

ESP32-S3-Main/platformio.ini

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
111
[env:esp32s3_n16r8_devkit]
212
platform = espressif32
313
board = esp32-s3-devkitc-1
@@ -9,7 +19,6 @@ lib_deps =
919
sparkfun/SparkFun MAX3010x Pulse and Proximity Sensor Library@^1.1.2
1020
adafruit/Adafruit Unified Sensor@^1.1.15
1121
hasenradball/AM2302-Sensor@^1.4.0
12-
ericksimoes/Ultrasonic@^3.0.0
1322
panjkrc/tcs3200@^1.3.1
1423
codingabi/KY040@^1.0.3
1524
powerbroker2/SerialTransfer@^3.1.5

ESP32-S3-Main/src/actuators/ermc1604syg.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ Display::Display() : lcd(LCD_ADDR, LCD_COLS, LCD_ROWS) {
77
}
88

99
bool Display::begin() {
10-
// Initialize Wire with the project-defined pins
11-
Wire.begin(I2C_SDA, I2C_SCL);
12-
Wire.setClock(I2C_FREQUENCY);
1310
// Initialize the LCD and turn backlight on
1411
lcd.init(); // initialize the PCF8574 I/O expander and LCD
15-
lcd.backlight(); // ensure backlight is ON (you requested always ON)
12+
lcd.backlight(); // ensure backlight is ON
1613

1714
lcd.clear();
1815

@@ -26,8 +23,6 @@ bool Display::begin() {
2623
lcd.setCursor(0, 3);
2724
lcd.print(" Backlight: ON ");
2825

29-
lcd.print(" Backlight: ON ");
30-
3126
delay(500);
3227
return true;
3328
}
@@ -58,22 +53,18 @@ void Display::print(float value, int decimals) {
5853
lcd.print(value, decimals);
5954
}
6055

61-
void Display::displayStatus(String mode, int battery, float distance, int hr, int spo2) {
56+
void Display::displayStatus(int battery, float temp, float humidity, int modeNum) {
6257
clear();
6358
setCursor(0, 0);
64-
// truncate if needed
65-
String line1 = mode + " Bat:" + String(battery) + "%";
66-
if (line1.length() > LCD_COLS) line1 = line1.substring(0, LCD_COLS);
59+
String line1 = "Battery: " + String(battery) + "%";
6760
print(line1);
6861

6962
setCursor(0, 1);
70-
String line2 = "Following: " + String(distance, 1) + "m";
71-
if (line2.length() > LCD_COLS) line2 = line2.substring(0, LCD_COLS);
63+
String line2 = "T:" + String(temp, 1) + "C H:" + String(humidity, 0) + "%";
7264
print(line2);
7365

7466
setCursor(0, 2);
75-
String line3 = "HR:" + String(hr) + " SpO2:" + String(spo2) + "%";
76-
if (line3.length() > LCD_COLS) line3 = line3.substring(0, LCD_COLS);
67+
String line3 = "Active Mode: " + String(modeNum);
7768
print(line3);
7869

7970
setCursor(0, 3);

ESP32-S3-Main/src/actuators/ermc1604syg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Display {
2525
void print(int value);
2626
void print(float value, int decimals = 1);
2727

28-
void displayStatus(String mode, int battery, float distance, int hr, int spo2);
28+
void displayStatus(int battery, float temp, float humidity, int modeNum);
2929
void displayError(String error);
3030
};
3131

ESP32-S3-Main/src/actuators/le0066.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void LEDArray::setStatus(bool leftOn, bool rightOn) {
3535
}
3636

3737
void LEDArray::update() {
38-
// Reserved for any periodic updates if needed
38+
3939
}
4040

4141
void LEDArray::controlFromFirebase(int lightadj_left, int lightadj_right) {

ESP32-S3-Main/src/actuators/sfm27.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ void Buzzer::setVolume(int vol) {
2424
volume = constrain(vol, 0, 100);
2525
}
2626

27-
// For active buzzers, we ignore frequency and use steady push-pull
2827
void Buzzer::drive(uint16_t duration_ms, uint16_t freq) {
2928
if (volume == 0) {
3029
stop();

ESP32-S3-Main/src/communication/firebase_manager.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bool FirebaseManager::ready() {
2424
}
2525

2626
void FirebaseManager::update() {
27-
// Reserved for future background tasks if needed
27+
2828
}
2929

3030
bool FirebaseManager::sendData(const FirebaseTxData& d) {
@@ -59,7 +59,7 @@ bool FirebaseManager::sendData(const FirebaseTxData& d) {
5959
// Compartment - SEND ONLY
6060
json.set("compartment", d.compartment);
6161

62-
if (!Firebase.updateNode(fbdo, "/telemetry", json)) {
62+
if (!Firebase.updateNode(fbdo, "/", json)) {
6363
Serial.print("Firebase TX failed: ");
6464
Serial.println(fbdo.errorReason());
6565
return false;
@@ -71,12 +71,11 @@ bool FirebaseManager::sendData(const FirebaseTxData& d) {
7171
bool FirebaseManager::receiveData(FirebaseRxData& d) {
7272
if (!ready()) return false;
7373

74-
// Non-blocking throttling: Don't fetch from server too fast
7574
static unsigned long lastRX = 0;
7675
if (millis() - lastRX < 1000) return true; // Keep old data if less than 1s passed
7776
lastRX = millis();
7877

79-
if (!Firebase.getJSON(fbdo, "/controls")) {
78+
if (!Firebase.getJSON(fbdo, "/")) {
8079
Serial.print("Firebase RX failed: ");
8180
Serial.println(fbdo.errorReason());
8281
return false;
@@ -95,13 +94,15 @@ bool FirebaseManager::receiveData(FirebaseRxData& d) {
9594
json.get(jd, "buzzersound");
9695
if (jd.success) d.buzzersound = jd.intValue;
9796

97+
9898
// LED brightness control - RECEIVE ONLY
9999
json.get(jd, "lightadj_left");
100100
if (jd.success) d.lightadj_left = jd.intValue;
101101

102102
json.get(jd, "lightadj_right");
103103
if (jd.success) d.lightadj_right = jd.intValue;
104104

105+
105106
// Sensor start/stop flags - RECEIVE ONLY
106107
json.get(jd, "colour_start");
107108
if (jd.success) d.colour_start = jd.boolValue;

ESP32-S3-Main/src/communication/firebase_manager.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,16 @@ struct FirebaseTxData {
1010
int angular; // Send angular velocity
1111
int battery; // Send battery level
1212
int voltage; // Send voltage
13-
14-
// Environment - SEND ONLY
1513
int temp; // Send temperature
1614
int humidity; // Send humidity
1715
int lightlevel; // Send light amount from path LDRs
18-
19-
// Health - SEND ONLY
2016
int hr; // Send heart rate
2117
int sp02; // Send SpO2 level
22-
23-
// Ultrasonic - SEND ONLY
2418
int ultrasonic_center; // Send center distance
2519
int ultrasonic_left; // Send left distance
2620
int ultrasonic_rear; // Send rear distance
2721
int ultrasonic_right; // Send right distance
28-
29-
// Color - SEND ONLY
3022
String colour; // Send detected color (RED/BLUE/GREEN/WHITE/UNKNOWN)
31-
32-
// Compartment - SEND ONLY
3323
int compartment; // Send compartment state (0=open, 255=closed)
3424
};
3525

ESP32-S3-Main/src/communication/uart.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
UARTProtocol::UARTProtocol() {
66
serial = &Serial1;
77
lastSendTime = 0;
8+
lastSentCommand = CMD_STOP;
9+
isWaitingForAck = false;
10+
lastAckTime = 0;
811
}
912

1013
void UARTProtocol::begin() {
@@ -18,11 +21,15 @@ void UARTProtocol::sendMotorCommand(MotorCommand cmd, uint8_t speed) {
1821
uint8_t speedValue = constrain(speed, 0, 100);
1922

2023
// Put command and speed into distinct slots
21-
transfer.txObj(cmdValue, 0); // Slot 0: Command
22-
transfer.txObj(speedValue, 1); // Slot 1: Speed
24+
transfer.txObj(cmdValue, 0);
25+
transfer.txObj(speedValue, 1);
2326

24-
// Send exactly 2 bytes of data objects
2527
transfer.sendData(2);
28+
29+
// Update tracking state
30+
lastSentCommand = cmd;
31+
lastSendTime = millis();
32+
isWaitingForAck = true;
2633
}
2734

2835

@@ -36,7 +43,21 @@ bool UARTProtocol::receiveAcknowledgment(MotorCommand &cmd, uint8_t &speed) {
3643
// Read command and speed from receive buffer
3744
transfer.rxObj(cmd, 0);
3845
transfer.rxObj(speed, 1);
39-
return true; // acknowledgment received
46+
47+
// Verify if this matches what we sent
48+
if (isWaitingForAck && cmd == lastSentCommand) {
49+
isWaitingForAck = false;
50+
lastAckTime = millis();
51+
return true; // Correct acknowledgment received
52+
}
53+
54+
return true; // Received some packet, even if not the specific ACK we waited for
4055
}
56+
57+
// Check for timeout
58+
if (isWaitingForAck && (millis() - lastSendTime > 500)) {
59+
isWaitingForAck = false;
60+
}
61+
4162
return false; // no data yet
4263
}

ESP32-S3-Main/src/communication/uart.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ class UARTProtocol {
2323

2424
unsigned long lastSendTime;
2525

26+
MotorCommand lastSentCommand;
27+
bool isWaitingForAck;
28+
unsigned long lastAckTime;
2629

2730
public:
2831
UARTProtocol();
2932
void begin();
3033
void sendMotorCommand(MotorCommand cmd, uint8_t speed);
3134
void sendEmergencyStop();
3235
bool receiveAcknowledgment(MotorCommand &cmd, uint8_t &speed);
36+
37+
bool isLastCommandAcked() const { return !isWaitingForAck; }
38+
bool isConnected() const { return (millis() - lastAckTime < 2000); }
3339
};
3440

3541
#endif

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define I2C_FREQUENCY 100000
88

99
// Ultrasonic Constants
10-
#define MAX_ULTRASONIC_DISTANCE 200 // cm
10+
#define MAX_ULTRASONIC_DISTANCE 100 // cm
1111
#define MIN_SAFE_DISTANCE 20 // cm
1212
#define SOUND_SPEED 0.034 // cm/microsecond
1313

@@ -31,6 +31,6 @@
3131
#define SPO2_MIN 95 // %
3232
#define SPO2_MAX 100 // %
3333

34-
#define MPU_ALPHA 0.96f // Start here, tune between 0.96-0.98
34+
#define MPU_ALPHA 0.96f // Complementary filter constant
3535

3636
#endif

0 commit comments

Comments
 (0)