ESPHome 2026.1.0b1
Loading...
Searching...
No Matches
rd03d.cpp
Go to the documentation of this file.
1#include "rd03d.h"
2#include "esphome/core/log.h"
3#include <cmath>
4
5namespace esphome::rd03d {
6
7static const char *const TAG = "rd03d";
8
9// Delay before sending configuration commands to allow radar to initialize
10static constexpr uint32_t SETUP_TIMEOUT_MS = 100;
11
12// Data frame format (radar -> host)
13static constexpr uint8_t FRAME_HEADER[] = {0xAA, 0xFF, 0x03, 0x00};
14static constexpr uint8_t FRAME_FOOTER[] = {0x55, 0xCC};
15
16// Command frame format (host -> radar)
17static constexpr uint8_t CMD_FRAME_HEADER[] = {0xFD, 0xFC, 0xFB, 0xFA};
18static constexpr uint8_t CMD_FRAME_FOOTER[] = {0x04, 0x03, 0x02, 0x01};
19
20// RD-03D tracking mode commands
21static constexpr uint16_t CMD_SINGLE_TARGET = 0x0080;
22static constexpr uint16_t CMD_MULTI_TARGET = 0x0090;
23
24// Speed sentinel values (cm/s) - radar outputs these when no valid Doppler measurement
25// FMCW radars detect motion via Doppler shift; targets with these speeds are likely noise
26static constexpr int16_t SPEED_SENTINEL_248 = 248;
27static constexpr int16_t SPEED_SENTINEL_256 = 256;
28
29// Decode coordinate/speed value from RD-03D format
30// Per datasheet: MSB=1 means positive, MSB=0 means negative
31static constexpr int16_t decode_value(uint8_t low_byte, uint8_t high_byte) {
32 int16_t value = ((high_byte & 0x7F) << 8) | low_byte;
33 if ((high_byte & 0x80) == 0) {
34 value = -value;
35 }
36 return value;
37}
38
39// Check if speed value indicates a valid Doppler measurement
40// Zero, ±248, or ±256 cm/s are sentinel values from the radar firmware
41static constexpr bool is_speed_valid(int16_t speed) {
42 int16_t abs_speed = speed < 0 ? -speed : speed;
43 return speed != 0 && abs_speed != SPEED_SENTINEL_248 && abs_speed != SPEED_SENTINEL_256;
44}
45
47 ESP_LOGCONFIG(TAG, "Setting up RD-03D...");
48 this->set_timeout(SETUP_TIMEOUT_MS, [this]() { this->apply_config_(); });
49}
50
52 ESP_LOGCONFIG(TAG, "RD-03D:");
53 if (this->tracking_mode_.has_value()) {
54 ESP_LOGCONFIG(TAG, " Tracking Mode: %s",
55 *this->tracking_mode_ == TrackingMode::SINGLE_TARGET ? "single" : "multi");
56 }
57 if (this->throttle_ > 0) {
58 ESP_LOGCONFIG(TAG, " Throttle: %ums", this->throttle_);
59 }
60#ifdef USE_SENSOR
61 LOG_SENSOR(" ", "Target Count", this->target_count_sensor_);
62#endif
63#ifdef USE_BINARY_SENSOR
64 LOG_BINARY_SENSOR(" ", "Target", this->target_binary_sensor_);
65#endif
66 for (uint8_t i = 0; i < MAX_TARGETS; i++) {
67 ESP_LOGCONFIG(TAG, " Target %d:", i + 1);
68#ifdef USE_SENSOR
69 LOG_SENSOR(" ", "X", this->targets_[i].x);
70 LOG_SENSOR(" ", "Y", this->targets_[i].y);
71 LOG_SENSOR(" ", "Speed", this->targets_[i].speed);
72 LOG_SENSOR(" ", "Distance", this->targets_[i].distance);
73 LOG_SENSOR(" ", "Resolution", this->targets_[i].resolution);
74 LOG_SENSOR(" ", "Angle", this->targets_[i].angle);
75#endif
76#ifdef USE_BINARY_SENSOR
77 LOG_BINARY_SENSOR(" ", "Presence", this->target_presence_[i]);
78#endif
79 }
80}
81
83 while (this->available()) {
84 uint8_t byte = this->read();
85 ESP_LOGVV(TAG, "Received byte: 0x%02X, buffer_pos: %d", byte, this->buffer_pos_);
86
87 // Check if we're looking for frame header
88 if (this->buffer_pos_ < FRAME_HEADER_SIZE) {
89 if (byte == FRAME_HEADER[this->buffer_pos_]) {
90 this->buffer_[this->buffer_pos_++] = byte;
91 } else if (byte == FRAME_HEADER[0]) {
92 // Start over if we see a potential new header
93 this->buffer_[0] = byte;
94 this->buffer_pos_ = 1;
95 } else {
96 this->buffer_pos_ = 0;
97 }
98 continue;
99 }
100
101 // Accumulate data bytes
102 this->buffer_[this->buffer_pos_++] = byte;
103
104 // Check if we have a complete frame
105 if (this->buffer_pos_ == FRAME_SIZE) {
106 // Validate footer
107 if (this->buffer_[FRAME_SIZE - 2] == FRAME_FOOTER[0] && this->buffer_[FRAME_SIZE - 1] == FRAME_FOOTER[1]) {
108 this->process_frame_();
109 } else {
110 ESP_LOGW(TAG, "Invalid frame footer: 0x%02X 0x%02X (expected 0x55 0xCC)", this->buffer_[FRAME_SIZE - 2],
111 this->buffer_[FRAME_SIZE - 1]);
112 }
113 this->buffer_pos_ = 0;
114 }
115 }
116}
117
119 // Apply throttle if configured
120 if (this->throttle_ > 0) {
121 uint32_t now = millis();
122 if (now - this->last_publish_time_ < this->throttle_) {
123 return;
124 }
125 this->last_publish_time_ = now;
126 }
127
128 uint8_t target_count = 0;
129
130 for (uint8_t i = 0; i < MAX_TARGETS; i++) {
131 // Calculate offset for this target's data
132 // Header is 4 bytes, each target is 8 bytes
133 uint8_t offset = FRAME_HEADER_SIZE + (i * TARGET_DATA_SIZE);
134
135 // Extract raw bytes for this target
136 uint8_t x_low = this->buffer_[offset + 0];
137 uint8_t x_high = this->buffer_[offset + 1];
138 uint8_t y_low = this->buffer_[offset + 2];
139 uint8_t y_high = this->buffer_[offset + 3];
140 uint8_t speed_low = this->buffer_[offset + 4];
141 uint8_t speed_high = this->buffer_[offset + 5];
142 uint8_t res_low = this->buffer_[offset + 6];
143 uint8_t res_high = this->buffer_[offset + 7];
144
145 // Decode values per RD-03D format
146 int16_t x = decode_value(x_low, x_high);
147 int16_t y = decode_value(y_low, y_high);
148 int16_t speed = decode_value(speed_low, speed_high);
149 uint16_t resolution = (res_high << 8) | res_low;
150
151 // Check if target is present
152 // Requires non-zero coordinates AND valid speed (not a sentinel value)
153 // FMCW radars detect motion via Doppler; sentinel speed indicates no real target
154 bool has_position = (x != 0 || y != 0);
155 bool has_valid_speed = is_speed_valid(speed);
156 bool target_present = has_position && has_valid_speed;
157 if (target_present) {
158 target_count++;
159 }
160
161#ifdef USE_SENSOR
162 this->publish_target_(i, x, y, speed, resolution);
163#endif
164
165#ifdef USE_BINARY_SENSOR
166 if (this->target_presence_[i] != nullptr) {
167 this->target_presence_[i]->publish_state(target_present);
168 }
169#endif
170 }
171
172#ifdef USE_SENSOR
173 if (this->target_count_sensor_ != nullptr) {
174 this->target_count_sensor_->publish_state(target_count);
175 }
176#endif
177
178#ifdef USE_BINARY_SENSOR
179 if (this->target_binary_sensor_ != nullptr) {
180 this->target_binary_sensor_->publish_state(target_count > 0);
181 }
182#endif
183}
184
185#ifdef USE_SENSOR
186void RD03DComponent::publish_target_(uint8_t target_num, int16_t x, int16_t y, int16_t speed, uint16_t resolution) {
187 TargetSensor &target = this->targets_[target_num];
188 bool valid = is_speed_valid(speed);
189
190 // Publish X coordinate (mm) - NaN if target invalid
191 if (target.x != nullptr) {
192 target.x->publish_state(valid ? static_cast<float>(x) : NAN);
193 }
194
195 // Publish Y coordinate (mm) - NaN if target invalid
196 if (target.y != nullptr) {
197 target.y->publish_state(valid ? static_cast<float>(y) : NAN);
198 }
199
200 // Publish speed (convert from cm/s to mm/s) - NaN if target invalid
201 if (target.speed != nullptr) {
202 target.speed->publish_state(valid ? static_cast<float>(speed) * 10.0f : NAN);
203 }
204
205 // Publish resolution (mm)
206 if (target.resolution != nullptr) {
208 }
209
210 // Calculate and publish distance (mm) - NaN if target invalid
211 if (target.distance != nullptr) {
212 if (valid) {
213 target.distance->publish_state(std::hypot(static_cast<float>(x), static_cast<float>(y)));
214 } else {
215 target.distance->publish_state(NAN);
216 }
217 }
218
219 // Calculate and publish angle (degrees) - NaN if target invalid
220 // Angle is measured from the Y axis (radar forward direction)
221 if (target.angle != nullptr) {
222 if (valid) {
223 float angle = std::atan2(static_cast<float>(x), static_cast<float>(y)) * 180.0f / M_PI;
224 target.angle->publish_state(angle);
225 } else {
226 target.angle->publish_state(NAN);
227 }
228 }
229}
230#endif
231
232void RD03DComponent::send_command_(uint16_t command, const uint8_t *data, uint8_t data_len) {
233 // Send header
234 this->write_array(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER));
235
236 // Send length (command word + data)
237 uint16_t len = 2 + data_len;
238 this->write_byte(len & 0xFF);
239 this->write_byte((len >> 8) & 0xFF);
240
241 // Send command word (little-endian)
242 this->write_byte(command & 0xFF);
243 this->write_byte((command >> 8) & 0xFF);
244
245 // Send data if any
246 if (data != nullptr && data_len > 0) {
247 this->write_array(data, data_len);
248 }
249
250 // Send footer
251 this->write_array(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
252
253 ESP_LOGD(TAG, "Sent command 0x%04X with %d bytes of data", command, data_len);
254}
255
257 if (this->tracking_mode_.has_value()) {
258 uint16_t mode_cmd = (*this->tracking_mode_ == TrackingMode::SINGLE_TARGET) ? CMD_SINGLE_TARGET : CMD_MULTI_TARGET;
259 this->send_command_(mode_cmd);
260 }
261}
262
263} // namespace esphome::rd03d
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:445
void publish_state(bool new_state)
Publish a new state to the front-end.
bool has_value() const
Definition optional.h:92
optional< TrackingMode > tracking_mode_
Definition rd03d.h:85
std::array< TargetSensor, MAX_TARGETS > targets_
Definition rd03d.h:76
void dump_config() override
Definition rd03d.cpp:51
void send_command_(uint16_t command, const uint8_t *data=nullptr, uint8_t data_len=0)
Definition rd03d.cpp:232
binary_sensor::BinarySensor * target_binary_sensor_
Definition rd03d.h:81
std::array< uint8_t, FRAME_SIZE > buffer_
Definition rd03d.h:89
void publish_target_(uint8_t target_num, int16_t x, int16_t y, int16_t speed, uint16_t resolution)
Definition rd03d.cpp:186
std::array< binary_sensor::BinarySensor *, MAX_TARGETS > target_presence_
Definition rd03d.h:80
sensor::Sensor * target_count_sensor_
Definition rd03d.h:77
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:76
void write_byte(uint8_t data)
Definition uart.h:18
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
int speed
Definition fan.h:1
Resolution resolution
Definition msa3xx.h:1
std::string size_t len
Definition helpers.h:566
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
sensor::Sensor * resolution
Definition rd03d.h:35
sensor::Sensor * distance
Definition rd03d.h:34
sensor::Sensor * x
Definition rd03d.h:31
sensor::Sensor * speed
Definition rd03d.h:33
sensor::Sensor * y
Definition rd03d.h:32
sensor::Sensor * angle
Definition rd03d.h:36
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6