4#include "TinyRobotics/communication/Message.h"
5#include "TinyRobotics/communication/MessageSource.h"
6#include "TinyRobotics/coordinates/Coordinate.h"
7#include "TinyRobotics/coordinates/FrameMgr2D.h"
8#include "TinyRobotics/units/Distance.h"
9#include "TinyRobotics/utils/LoggerClass.h"
11namespace tinyrobotics {
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
68template <
typename T = DistanceM>
71 RangeSensor(
const Transform2D& tf,
float obstacleDegree = 0) {
75 RangeSensor(
float obstacleDegree = 0) {
86 void update(
float angleDeg,
float distanceM) {
88 setObstacleDistance(distanceM);
94 setObstacleDistance(distance);
98 void end() { is_active_ =
false; }
115 return setObstacleDistance(dist.getValue(
DistanceUnit::M));
121 if (!is_active_)
return false;
122 this->distanceM = distanceM;
125 if (distanceM >= alertDistanceM_ &&
126 std::abs(obstacle_deg_) <= alertAngleDeg_) {
127 Message<
float> msg(MessageContent::Obstacle, distanceM, Unit::Meters);
128 msg.origin = MessageOrigin::LIDAR;
137 msgDistance.origin = MessageOrigin::LIDAR;
138 sendMessage(msgDistance);
141 Message<T> msgAngle(MessageContent::Angle, obstacle_deg_,
143 msgAngle.origin = MessageOrigin::LIDAR;
144 sendMessage(msgAngle);
149 msgLocation.origin = MessageOrigin::LIDAR;
150 sendMessage(msgLocation);
157 alertAngleDeg_ = deg.getValue(
AngleUnit::DEG);
172 setObstacleDistance(distance);
177 lidar_to_world_tf = tf;
178 has_transform_ =
true;
184 if (!has_transform_) {
187 "RangeSensor: No transform set, cannot compute obstacle coordinate");
190 float theta = obstacle_deg_ *
static_cast<
float>(M_PI) / 180.0f;
191 Coordinate<T> obstacle_lidar(distanceM * std::cos(theta),
192 distanceM * std::sin(theta));
193 result = lidar_to_world_tf.apply(obstacle_lidar);
198 operator bool()
const {
return distanceM > 0; }
205 if (distanceM <= 0)
return 1.0f;
206 if (distanceM >= breakingDistance.getValue(
DistanceUnit::M))
209 float factor = distanceM / breakingDistance.getValue(
DistanceUnit::M);
210 return constrain(factor, 0.0f, 1.0f);
214 float obstacle_deg_ = 0;
217 bool has_transform_ =
false;
218 bool is_active_ =
false;
219 float alertAngleDeg_ = 5;
220 float alertDistanceM_ = 1.0f;
Represents an angle with unit conversion and wrap-around support.
Definition: Angle.h:42
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Represents a distance measurement with unit conversion support.
Definition: Distance.h:40
Base class for message sources in the TinyRobotics communication framework.
Definition: MessageSource.h:35
Generic range sensor abstraction for LIDAR, ultrasonic, or similar sensors.
Definition: RangeSensor.h:69
void setTransform(const Transform2D &tf)
Set the sensor-to-world transform.
Definition: RangeSensor.h:176
bool setObstacleDistance(float distanceM)
Definition: RangeSensor.h:120
void update(Angle angle, Distance distance)
Update the sensor with angle and distance objects.
Definition: RangeSensor.h:92
bool begin()
Start the sensor (e.g., initialize hardware)
Definition: RangeSensor.h:80
operator bool() const
Check if the sensor reading is valid (distance > 0)
Definition: RangeSensor.h:198
bool getObstacleCoordinate(Coordinate< T > &result)
Definition: RangeSensor.h:183
void update(float angleDeg, float distanceM)
Update the sensor with angle (deg) and distance (m)
Definition: RangeSensor.h:86
void setObstacleDirectionDegree(float deg)
Defines the angle to the obstacle in degrees: 0 means forward.
Definition: RangeSensor.h:101
void setObstacleAlertAngle(Angle deg)
Set the alert angle threshold for obstacle detection.
Definition: RangeSensor.h:156
bool setObstacleDistance(Distance dist)
Definition: RangeSensor.h:114
bool hasObstacle() const
Return true if there is an obstacle detected (distance > 0)
Definition: RangeSensor.h:201
void end()
Stop the sensor.
Definition: RangeSensor.h:98
void setObstacleDirection(Angle angle)
Set the obstacle direction using an Angle object.
Definition: RangeSensor.h:104
float getObstacleDirectionDegree() const
Definition: RangeSensor.h:110
float getSpeedFactor(Distance breakingDistance) const
Compute a speed factor (0.0 to 1.0) based on the distance to the obstacle.
Definition: RangeSensor.h:204
float getObstacleDistance() const
Definition: RangeSensor.h:167
void setObstacle(float degree, float distance)
Convenience method to set both the obstacle bearing and distance at once.
Definition: RangeSensor.h:170
void setObstacleAlertDistance(Distance dist)
Set the alert distance threshold for obstacle detection.
Definition: RangeSensor.h:161
DistanceUnit
Supported distance units for conversion and representation.
Definition: Distance.h:10
AngleUnit
Supported angle units for conversion and representation.
Definition: Angle.h:11
Unit
Units for message values.
Definition: Common.h:45
@ AngleDegree
Angle in degrees.
@ Meters
Distance in meters.
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72