14static constexpr uint32_t SETUP_TIMEOUT_MS = 100;
17static constexpr uint8_t FRAME_HEADER[] = {0xAA, 0xFF, 0x03, 0x00};
18static constexpr uint8_t FRAME_FOOTER[] = {0x55, 0xCC};
21static constexpr uint8_t CMD_FRAME_HEADER[] = {0xFD, 0xFC, 0xFB, 0xFA};
22static constexpr uint8_t CMD_FRAME_FOOTER[] = {0x04, 0x03, 0x02, 0x01};
25static constexpr uint16_t CMD_SINGLE_TARGET = 0x0080;
26static constexpr uint16_t CMD_MULTI_TARGET = 0x0090;
30static constexpr int16_t SPEED_SENTINEL_248 = 248;
31static constexpr int16_t SPEED_SENTINEL_256 = 256;
35static constexpr int16_t decode_value(uint8_t low_byte, uint8_t high_byte) {
37 if ((high_byte & 0x80) == 0) {
45static constexpr bool is_speed_valid(int16_t speed) {
47 return speed != 0 && abs_speed != SPEED_SENTINEL_248 && abs_speed != SPEED_SENTINEL_256;
51 ESP_LOGCONFIG(TAG,
"Setting up RD-03D...");
55void RD03DComponent::dump_config() {
56 ESP_LOGCONFIG(TAG,
"RD-03D:");
59 TAG,
" Tracking Mode: %s",
63 ESP_LOGCONFIG(TAG,
" Throttle: %" PRIu32
"ms", this->
throttle_);
66 LOG_SENSOR(
" ",
"Target Count", this->target_count_sensor_);
68#ifdef USE_BINARY_SENSOR
69 LOG_BINARY_SENSOR(
" ",
"Target", this->target_binary_sensor_);
71 for (uint8_t i = 0; i < MAX_TARGETS; i++) {
72 ESP_LOGCONFIG(TAG,
" Target %d:", i + 1);
74 LOG_SENSOR(
" ",
"X", this->
targets_[i].
x);
75 LOG_SENSOR(
" ",
"Y", this->
targets_[i].
y);
76 LOG_SENSOR(
" ",
"Speed", this->
targets_[i].speed);
77 LOG_SENSOR(
" ",
"Distance", this->
targets_[i].distance);
79 LOG_SENSOR(
" ",
"Angle", this->
targets_[i].angle);
81#ifdef USE_BINARY_SENSOR
87void RD03DComponent::loop() {
89 ESP_LOGVV(TAG,
"Received byte: 0x%02X, buffer_pos: %d",
byte, this->
buffer_pos_);
94 this->
buffer_[this->buffer_pos_++] = byte;
95 }
else if (
byte == FRAME_HEADER[0]) {
98 this->buffer_pos_ = 1;
100 this->buffer_pos_ = 0;
106 this->
buffer_[this->buffer_pos_++] = byte;
109 if (this->buffer_pos_ == FRAME_SIZE) {
111 if (this->
buffer_[FRAME_SIZE - 2] == FRAME_FOOTER[0] && this->
buffer_[FRAME_SIZE - 1] == FRAME_FOOTER[1]) {
114 ESP_LOGW(TAG,
"Invalid frame footer: 0x%02X 0x%02X (expected 0x55 0xCC)", this->
buffer_[FRAME_SIZE - 2],
115 this->
buffer_[FRAME_SIZE - 1]);
117 this->buffer_pos_ = 0;
132 uint8_t target_count = 0;
134 for (uint8_t i = 0; i < MAX_TARGETS; i++) {
137 uint8_t offset = FRAME_HEADER_SIZE + (i * TARGET_DATA_SIZE);
140 uint8_t x_low = this->
buffer_[offset + 0];
141 uint8_t x_high = this->
buffer_[offset + 1];
142 uint8_t y_low = this->
buffer_[offset + 2];
143 uint8_t y_high = this->
buffer_[offset + 3];
144 uint8_t speed_low = this->
buffer_[offset + 4];
145 uint8_t speed_high = this->
buffer_[offset + 5];
146 uint8_t res_low = this->
buffer_[offset + 6];
147 uint8_t res_high = this->
buffer_[offset + 7];
150 int16_t
x = decode_value(x_low, x_high);
151 int16_t
y = decode_value(y_low, y_high);
152 int16_t
speed = decode_value(speed_low, speed_high);
158 bool has_position = (
x != 0 ||
y != 0);
159 bool has_valid_speed = is_speed_valid(
speed);
160 bool target_present = has_position && has_valid_speed;
161 if (target_present) {
169#ifdef USE_BINARY_SENSOR
175 PUBLISH_STATE(this->target_count_sensor_, target_count);
178#ifdef USE_BINARY_SENSOR
179 PUBLISH_STATE(this->target_binary_sensor_, target_count > 0);
189 PUBLISH_STATE(target.
x,
valid ?
static_cast<float>(
x) : NAN);
192 PUBLISH_STATE(target.
y,
valid ?
static_cast<float>(
y) : NAN);
195 PUBLISH_STATE(target.
speed,
valid ?
static_cast<float>(
speed) * 10.0f : NAN);
211 if (target.
angle !=
nullptr) {
213 float angle = std::atan2(
static_cast<float>(
x),
static_cast<float>(
y)) * 180.0f / std::numbers::pi_v<float>;
224 this->
write_array(CMD_FRAME_HEADER,
sizeof(CMD_FRAME_HEADER));
227 uint16_t
len = 2 + data_len;
236 if (data !=
nullptr && data_len > 0) {
241 this->
write_array(CMD_FRAME_FOOTER,
sizeof(CMD_FRAME_FOOTER));
243 ESP_LOGD(TAG,
"Sent command 0x%04X with %d bytes of data", command, data_len);