First model
For the first model: Raspberry Pi communicates with ESP32 with UART we use small protocol to ensure that the data is valid, Steps of Communication: Initialize UART on Both Devices: - ESP32: Executes Serial.begin(115200) to configure UART0 (TX0/RX0) at a baud rate of 115200 bits per second (bps).
- RPi4: Configures its UART interface (e.g., using pyserial in Python) to match the 115200 baud rate.
- Hardware Setup: Connect ESP32’s TX pin to RPi4’s RX pin and ESP32’s RX pin to RPi4’s TX pin via GPIO pins. Ground pins are connected to ensure a common reference.
- Purpose: Establishes a serial communication channel for bidirectional data transfer.
ESP32 Requests Data: - The ESP32 initiates communication by sending a single request byte, 0xAA, using Serial.write(0xAA).
- A 100ms delay (delay(100)) is introduced to allow the RPi4 sufficient time to prepare and send the response.
- Purpose: Signals the RPi4 to transmit sensor data (e.g., distances and flags).
RPi4 Sends Data Frame: ESP32 Checks for Data Availability: - The ESP32 checks if at least 8 bytes are available in the serial buffer using Serial.available() >= 8.
- If fewer than 8 bytes are present, the function returns false, indicating no valid data.
- Purpose: Ensures the complete frame is received before processing.
Validate Header: - The ESP32 reads the first byte using Serial.read() and verifies it equals 0xBB.
- If the header is invalid (not 0xBB), the ESP32 skips further processing and returns false.
- Purpose: Confirms the frame is correctly formatted and from the expected source.
Read Sensor Data and Flags: - The ESP32 reads the next 4 bytes into the distances[] array (representing distance measurements).
- Reads the following 2 bytes into the flags[] array (representing status or control flags).
- Purpose: Extracts the payload data for use in safety algorithms (e.g., FCW, BSW).
Verify CRC: - The ESP32 reads the 8th byte as the received CRC.
- Calculates the expected CRC by XORing the 6 payload bytes: distances[0] ^ distances[1] ^ distances[2] ^ distances[3] ^ flags[0] ^ flags[1].
- Compares the received CRC with the expected CRC.
- Purpose: Detects transmission errors to ensure data integrity.
Send Acknowledgment or Rejection: - If CRC Matches: The ESP32 sends 0xCC (ACK) using Serial.write(0xCC) and returns true, indicating successful data reception.
- If CRC Fails: Sends 0xDD (rejection) using Serial.write(0xDD), displays an error message on the LCD (e.g., "Data Error! Re-requesting..."), and returns false.
- Purpose: Provides feedback to the RPi4, enabling retransmission if needed.
|