LD2410Async
Asynchronous Arduino ESP32 library for the LD2410 mmWave radar sensor
Loading...
Searching...
No Matches
changeDistanceResolution.ino
Go to the documentation of this file.
1/**
2 * @brief Example: Changing detection resolution on LD2410
3 *
4 * @details
5 * This sketch demonstrates how to:
6 * 1. Initialize the radar on Serial1.
7 * 2. Query all current configuration values from the sensor.
8 * 3. Clone the config into a local variable.
9 * 4. Modify the distance resolution.
10 * 5. Apply the new config.
11 * 6. Reboot the sensor to ensure changes take effect.
12 *
13 * @warning
14 * Important:
15 * Make sure to adjust RADAR_RX_PIN and ADAR_TX_PIN to match you actual wiring.
16 */
17
18#include <Arduino.h>
19#include "LD2410Async.h"
20
21 // ========================= USER CONFIGURATION =========================
22
23 // UART pins for the LD2410 sensor
24#define RADAR_RX_PIN 16 // ESP32 pin that receives data from the radar (radar TX)
25#define RADAR_TX_PIN 17 // ESP32 pin that transmits data to the radar (radar RX)
26
27// UART baudrate for the radar sensor (default is 256000)
28#define RADAR_BAUDRATE 256000
29
30// ======================================================================
31
32/**
33* Create a HardwareSerial instance (ESP32 has multiple UARTs) bound to UART1
34*/
35HardwareSerial RadarSerial(1);
36
37/**
38* @brief Creates LD2410Async object bound to the serial port defined in RadarSerial
39*/
41
42/**
43* @brief Callback after the reboot command has been confirmed
44*
45* @details
46* This method just checks and prints the result
47*/
50 Serial.println("Radar reboot initiated.");
51 }
52 else {
53 Serial.println("Failed to init reboot.");
54 }
55}
56
57/**
58* @brief Callback after applying modified config
59*
60* @details
61* After checking if the async configureAllConfigSettingsAsync() method was successful (always do that!), it will initiate a
62* reboot of the sensor to activate the changed distance resolution.
63*
64* @note
65* Always check the result of the async command that has triggered the callback. Otherweise async commands can fail, timeout or get canceled without you relizing it.
66* In callback methods is is generally advised to access members of the LD2410Async instance via the sender pointer.
67* This ensures that you are allways working with the correct instance, which is important if you have multiple LD2410Async instances.
68* Also keep in mind that callbacks should be as short and efficient as possible to avoid blocking the background task of the library.
69*/
72 Serial.println("Config applied successfully. Rebooting radar...");
73 if (!sender->rebootAsync(onReboot)) {
74 //It is good practive to check the return value of commands for true/false.
75 //False indicates that the command could not be sent (e.g. because another async command is still pending)
76 Serial.println("Error! Could not send reboot command to the sensor");
77 };
78 //alternatively you could also call the LD2410Async instance directly instead of using the pointer in sender:
79 //radar.rebootAsync(onReboot);
80 //However, working with the reference in sender is a good practice since it will always point to the LD2410Async instance that triggered the callback (important if you have several instances resp. more than one LD2410 connected to your board)..
81 }
82 else {
83 Serial.println("Failed to apply config.");
84 }
85}
86
87/**
88* @brief Callback after requesting config data
89*
90* @details
91* This method is called when the config data has been received from the sensor.
92* After checking if the async requestAllConfigSettingsAsync() method was successful (always do that!), it clones the config using getConfigData(),
93* modifies the distance resolution and applies the new config using configureAllConfigSettingsAsync()
94*
95* @note
96* Always check the result of the async command that has triggered the callback. Otherweise async commands can fail, timeout or get canceled without you relizing it.
97* In callback methods is is generally advised to access members of the LD2410Async instance via the sender pointer.
98* This ensures that you are allways working with the correct instance, which is important if you have multiple LD2410Async instances.
99* Also keep in mind that callbacks should be as short and efficient as possible to avoid blocking the background task of the library.
100*/
103 Serial.println("Failed to request config data.");
104 return;
105 }
106
107 Serial.println("Config data received. Cloning and modifying...");
108
109 // Clone the current config
111
112 // Example modification: change resolution
113 // RESOLUTION_75CM or RESOLUTION_20CM
115
116 // Apply updated config to the radar
117 if (!sender->configureAllConfigSettingsAsync(cfg, false, onConfigApplied)) {
118 //It is good practive to check the return value of commands for true/false.
119 //False indicates that the command could not be sent (e.g. because another async command is still pending)
120 Serial.println("Error! Could not update config on the sensor");
121 };
122 //alternatively you could also call the LD2410Async instance directly instead of using the pointer in sender:
123 //radar.configureAllConfigSettingsAsync(cfg, false, onConfigApplied);
124 //However, working with the reference in sender is a good practice since it will always point to the LD2410Async instance that triggered the callback (important if you have several instances resp. more than one LD2410 connected to your board)..
125}
126
127/**
128* @brief Arduino setup function which initializes the radar and starts the config change process
129*
130* @details
131* begin() starts the background task of the LD2410Async library which automatically handles
132* incoming data and triggers callbacks. The onDetectionDataReceived callback is registered to
133* receive detection data.
134* requestAllConfigSettingsAsync() will fetch all config data from the sensor and triggers the
135* onConfigReceived callback when done.
136*/
137void setup() {
138 // Initialize USB serial for debug output
139 Serial.begin(115200);
140 while (!Serial) {
141 ; // wait for Serial Monitor
142 }
143 Serial.println("LD2410Async example: change resolution and reboot");
144
145 // Initialize Serial1 with user-defined pins and baudrate
147
148 // Start the radar background task (parses incoming data frames)
149 if (radar.begin()) {
150 Serial.println("Radar task started successfully.");
151
152 // Request all config data, then modify in callback
154 //It is good practive to check the return value of commands for true/false.
155 //False indicates that the command could not be sent (e.g. because another async command is still pending)
156 Serial.println("Error! Could not start config data request");
157 }
158 }
159 else {
160 Serial.println("Error! Could not start radar task.");
161 }
162
163}
164
165/**
166* @brief Arduino loop function which does nothing
167*
168* @details
169* The LD2410Async library runs a FreeRTOS background task that automatically handles all jobs that are related to the radar sensor.
170* Therefore the main loop doesnt have to da any LD2410 related work and is free for anything else you might want to do.
171*/
172void loop() {
173 // Nothing to do here.
174 // The library handles communication and invokes callbacks automatically.
175 delay(1000);
176}
void onReboot(LD2410Async *sender, LD2410Async::AsyncCommandResult result)
Callback after the reboot command has been confirmed.
HardwareSerial RadarSerial(1)
void onConfigReceived(LD2410Async *sender, LD2410Async::AsyncCommandResult result)
Callback after requesting config data.
void setup()
Arduino setup function which initializes the radar and starts the config change process.
void onConfigApplied(LD2410Async *sender, LD2410Async::AsyncCommandResult result)
Callback after applying modified config.
LD2410Async radar(RadarSerial)
Creates LD2410Async object bound to the serial port defined in RadarSerial.
#define RADAR_BAUDRATE
#define RADAR_RX_PIN
Example: Changing detection resolution on LD2410.
#define RADAR_TX_PIN
void loop()
Arduino loop function which does nothing.
Asynchronous driver class for the LD2410 human presence radar sensor.
Definition LD2410Async.h:38
bool rebootAsync(AsyncCommandCallback callback)
Reboots the sensor.
bool begin()
Starts the background task that continuously reads data from the sensor.
AsyncCommandResult
Result of an asynchronous command execution.
Definition LD2410Async.h:47
@ SUCCESS
Command completed successfully and ACK was received.
bool configureAllConfigSettingsAsync(const LD2410Types::ConfigData &configToWrite, bool writeAllConfigData, AsyncCommandCallback callback)
Applies a full ConfigData struct to the LD2410.
LD2410Types::ConfigData getConfigData() const
Returns a clone of the current configuration data of the radar.
bool requestAllConfigSettingsAsync(AsyncCommandCallback callback)
Requests all configuration settings from the sensor.
@ RESOLUTION_20CM
Each gate is about 0.20 m, max range about 1.8 m.
DistanceResolution distanceResolution
Current distance resolution. A reboot is required to activate changes after configureAllConfigSetting...