whatever/notes.md
2025-11-03 12:29:20 +03:00

3.4 KiB
Raw Blame History

Here is a developer-oriented description for the protocol and requirements needed to control two motors on your ESP32 device via serial communication:

Data Packet Format

  • The controller expects a data packet sent over UART containing exactly three bytes:

    1. Envelope Byte: 0xAA (serves as a sync marker and must be the first byte of every packet)
    2. Motor 1 PWM Value: Ranges from 0 (off) to 255 (full speed)[2][3]
    3. Motor 2 PWM Value: Ranges from 0 (off) to 255 (full speed)[3][2]
  • The packet structure: [0xAA][Motor1_PWM][Motor2_PWM]

Serial Transmission Requirements

  • Transmit at a reliable baud rate compatible with the ESP32 UART (for example, 115200).
  • Always ensure that 0xAA is the leading byte for synchronization. If noise or loss occurs, the receiver will wait for 0xAA before processing the next two bytes as PWM data.[6]

Motor Control Logic

  • Upon receiving a properly formed packet (0xAA followed by two PWM bytes), the ESP32 sets the PWM on Motor 1 and Motor 2 using an 8-bit analog or ledcWrite function. Valid duty cycle values are 0255 (8-bit resolution).[3]
  • The system compares received PWM values against the last command. The motor PWM will only be updated if the new value differs from the previous value, reducing redundant actions and communication overhead.

Safety and Signal Timeout

  • If no valid packet (with 0xAA header) is received for 200 milliseconds, the ESP32 will automatically set both motors PWM to zero, stopping the robot for safety.[6]
  • Developers must ensure that new packets are sent at intervals shorter than 200 ms to keep the motors active and avoid unwanted stops.

Example Packet

To set Motor 1 to 120 and Motor 2 to 180:

[0xAA][0x78][0xB4]

Where:

  • 0xAA = Envelope/sync
  • 0x78 = 120 (Motor 1 PWM)
  • 0xB4 = 180 (Motor 2 PWM)

Summary Table

Byte Value Range Purpose
1 0xAA Envelope / header
2 0255 Motor 1 PWM signal
3 0255 Motor 2 PWM signal

Additional Notes

  • All bytes must arrive without alteration; avoid extra bytes or other headers.
  • If you need to change the sync byte, update the ESP32 firmware accordingly.
  • Tie the GND of the sender to ESP32s GND for reliable UART connection.[6]

This ensures that motor control commands are robust, noise-resistant, and safe for continuous operation.

1 2 3 4 5 6 7 8 9 10