-
Notifications
You must be signed in to change notification settings - Fork 20
/
target.h
232 lines (195 loc) · 6.37 KB
/
target.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "polling_sensor.h"
#define DEBUG_FREQUENCY 1000
#define FAST_OFF_THRESHOLD 100
namespace esphome::ld2450
{
/**
* @brief Target component which provides information about a single target and updates derived sensor components.
*/
class Target : public Component
{
public:
void setup() override;
void loop() override;
void dump_config() override;
/**
* @brief Sets the name of this component
*/
void set_name(const char *name)
{
name_ = name;
}
/**
* @brief Sets the debugging flag, which enables/disables raw value debug output.
*/
void set_debugging(bool flag)
{
debug_ = flag;
}
/**
* @brief Sets the fast of detection flag, which determines how the unoccupied state is determined.
*/
void set_fast_off_detection(bool flag)
{
fast_off_detection_ = flag;
}
/**
* @brief Sets the x position sensor reference
* @param reference polling sensor reference
*/
void set_x_position_sensor(PollingSensor *x_position_sensor)
{
x_position_sensor_ = x_position_sensor;
}
/**
* @brief Sets the y position sensor reference
* @param reference polling sensor reference
*/
void set_y_position_sensor(PollingSensor *y_position_sensor)
{
y_position_sensor_ = y_position_sensor;
}
/**
* @brief Sets the speed sensor reference
* @param reference polling sensor reference
*/
void set_speed_sensor(PollingSensor *speed_sensor)
{
speed_sensor_ = speed_sensor;
}
/**
* @brief Sets the distance resolution sensor reference
* @param reference polling sensor reference
*/
void set_distance_resolution_sensor(PollingSensor *distance_resolution_sensor)
{
distance_resolution_sensor_ = distance_resolution_sensor;
}
/**
* @brief Sets the angle sensor reference
* @param reference polling sensor reference
*/
void set_angle_sensor(PollingSensor *angle_sensor)
{
angle_sensor_ = angle_sensor;
}
/**
* @brief Sets the distance sensor reference
* @param reference polling sensor reference
*/
void set_distance_sensor(PollingSensor *distance_sensor)
{
distance_sensor_ = distance_sensor;
}
/**
* @brief Updates the value in this target object
* @param x The x coordinate of the target
* @param y The y coordinate of the target
* @param speed The speed of the target
* @param resolution The distance resolution of the measurement
*/
void update_values(int16_t x, int16_t y, int16_t speed, int16_t resolution);
/**
* @brief Determines whether this target is currently detected.
* @return true if the target is detected, false otherwise
*/
bool is_present();
/**
* @brief Determines if this target is currently moving
* @return true if the target is moving, false if it is stationary.
*/
bool is_moving()
{
return speed_ != 0;
}
/**
* @brief Time since last last change in values.
* @return timestamp in milliseconds since start
*/
uint32_t get_last_change()
{
return last_change_;
}
/**
* @brief Rests all values in this target
*/
void clear()
{
update_values(0, 0, 0, 0);
}
/**
* @brief Gets the name of this target
*/
const char *get_name()
{
return name_;
}
/**
* Gets the x coordinate (horizontal position) of this targets
*
* @return horizontal position of the target (0 = center)
*/
int16_t get_x()
{
return x_;
}
/**
* Gets the y coordinate (distance from the sensor) of this target
* @return distance in centimeters
*/
int16_t get_y()
{
return y_;
}
/**
* Gets the movement speed of this target
* @return speed in m/s
*/
int16_t get_speed()
{
return speed_;
}
/**
* Gets the distance resolution of this target
* @return distance error
*/
int16_t get_distance_resolution()
{
return resolution_;
}
protected:
/// @brief X (horizontal) coordinate of the target in relation to the sensor
int16_t x_ = 0;
/// @brief Y (distance) coordinate of the target in relation to the sensor
int16_t y_ = 0;
/// @brief speed of the target
int16_t speed_ = 0;
/// @brief distance resolution of the target
int16_t resolution_ = 0;
/// @brief Name of this target
const char *name_ = nullptr;
/// @brief Debugging flag, enables logs for value changes
bool debug_ = false;
/// @brief Determines whether the fast unoccupied detection method is applied
bool fast_off_detection_ = false;
/// @brief time stamp of the last debug message which was sent.
uint32_t last_debug_message_ = 0;
/// @brief time of the last value change
uint32_t last_change_ = 0;
/// @brief sensor reference of the x position sensor
PollingSensor *x_position_sensor_ = nullptr;
/// @brief sensor reference of the y position sensor
PollingSensor *y_position_sensor_ = nullptr;
/// @brief sensor reference of the speed sensor
PollingSensor *speed_sensor_ = nullptr;
/// @brief sensor reference of the distance resolution sensor
PollingSensor *distance_resolution_sensor_ = nullptr;
/// @brief sensor reference of the angle sensor
PollingSensor *angle_sensor_ = nullptr;
/// @brief sensor reference of the distance sensor
PollingSensor *distance_sensor_ = nullptr;
};
} // namespace esphome::ld2450