6#include "TinyRobotics/communication/MessageHandler.h"
7#include "TinyRobotics/communication/MessageSource.h"
8#include "TinyRobotics/control/MotionState2D.h"
9#include "TinyRobotics/coordinates/Coordinate.h"
10#include "TinyRobotics/odometry/OdometryModel2D.h"
11#include "TinyRobotics/units/Units.h"
13namespace tinyrobotics {
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
80 : vehicle(vehicle), speedSource(speedSource), model(model) {
81 model.setSpeedSource(speedSource);
92
93
94
95
96
97
98
99 bool begin(
Coordinate<DistanceM> initialPosition,
float initialTheta = 0.0f) {
100 position = initialPosition;
101 theta = initialTheta;
102 totalDistance = 0.0f;
103 lastUpdateTimeMs = 0;
112
113
114
115
116
119 auto tf = frame.getTransform();
127
128
129
132 float deltaTimeMs = now - lastUpdateTimeMs;
133 if (lastUpdateTimeMs > 0)
update(deltaTimeMs
);
134 lastUpdateTimeMs = now;
146 float getTheta()
const {
return theta; }
165 float vx = v * std::cos(theta);
166 float vy = v * std::sin(theta);
167 return Speed3D(vx, vy, 0.0f,
SpeedUnit::MPS);
179 return MotionState3D(Coordinate<
float>(position.x, position.y, 0),
180 Orientation3D(theta, 0, 0), getSpeed3D(),
181 AngularVelocity3D(0.0f, 0.0f, getAngularVelocity(),
182 AngularVelocityUnit::RadPerSec));
186 return Transform2D(position, getHeading().getValue(AngleUnit::DEG));
192 float totalDistance = 0.0f;
193 Delta2D lastDelta = {0.0f, 0.0f, 0.0f};
194 uint32_t lastUpdateTimeMs = 0;
198 bool is_differential =
false;
203 Coordinate<
float>(position.x, position.y),
205 msgPos.origin = MessageOrigin::Odometry;
209 Message<
float> msgHeading{MessageContent::Heading, theta,
211 msgHeading.origin = MessageOrigin::Odometry;
212 sendMessage(msgHeading);
215 Message<
float> msgSpeed{MessageContent::Speed,
216 getSpeed().getValue(SpeedUnit::MPS),
217 Unit::MetersPerSecond};
218 msgSpeed.origin = MessageOrigin::Odometry;
219 sendMessage(msgSpeed);
223 getMotionState(), Unit::Undefined};
224 msgState.origin = MessageOrigin::Odometry;
225 vehicle.sendMessage(msgState);
229
230
231
232
233
234
235
236
237 void update(uint32_t deltaTimeMs) {
239 model.updateSpeed(deltaTimeMs);
241 float deltaTheta = 0.0f;
242 deltaTheta = model.computeDeltaTheta(deltaTimeMs);
245 theta = normalizeAngleRad(theta);
246 float deltaX = 0.0f, deltaY = 0.0f;
247 model.computeDeltaXY(theta, deltaTimeMs, deltaX, deltaY);
248 position.x += deltaX;
249 position.y += deltaY;
250 lastDelta = {deltaX, deltaY, deltaTheta};
251 totalDistance += std::sqrt(deltaX * deltaX + deltaY * deltaY);
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
Interface for representing the navigation state of a robot in 2D space.
Definition: MotionState2D.h:35
Abstract interface for 2D odometry models.
Definition: IOdometryModel2D.h:9
virtual Angle getSteeringAngle() const =0
Get the current steering angle of the vehicle.
virtual Speed getSpeed() const =0
Get the current speed of the vehicle.
virtual void registerCallback(void(*callback)(void *), void *userData)=0
Register a callback to be invoked on relevant events (e.g., input change, update).
Interface for speed sources.
Definition: ISpeedSource.h:9
Base class for message sources in the TinyRobotics communication framework.
Definition: MessageSource.h:35
void subscribe(MessageHandler &handler, MessageOrigin origin=MessageOrigin::Undefined, MessageContent content=MessageContent::Undefined)
Subscribe a message handler to this source, with optional filtering.
Definition: MessageSource.h:59
Represents the full 3D motion state of a robot or vehicle.
Definition: MotionState3D.h:53
Tracks 2D position and orientation of a robot using velocity and steering data from external sources.
Definition: Odometry2D.h:76
Speed3D getSpeed3D() const
Get the current speed as a Speed3D (x, y in world frame, z: 0)
Definition: Odometry2D.h:163
bool begin(const Frame2D &frame)
Initialize the odometry state from a Frame2D.
Definition: Odometry2D.h:117
void update(uint32_t deltaTimeMs)
Update the odometry state with new speed and steering angle.
Definition: Odometry2D.h:237
float getLinearVelocity() const
Get the current linear velocity (meters/second)
Definition: Odometry2D.h:148
void setState(Coordinate< DistanceM > pos, float th)
Set the odometry state (position and orientation)
Definition: Odometry2D.h:171
Angle getSteeringAngle() const
Get the current steering angle (radians or angular velocity)
Definition: Odometry2D.h:140
MotionState3D getMotionState() const
Get the current motion state as a MotionState3D (with z=0 and pitch/roll=0)
Definition: Odometry2D.h:178
Delta2D getLastDelta() const
Get the last delta update (dx, dy, dtheta)
Definition: Odometry2D.h:160
bool begin(Coordinate< DistanceM > initialPosition, float initialTheta=0.0f)
Initialize the odometry state.
Definition: Odometry2D.h:99
Angle getHeading() const
Get the current heading as an Angle (radians)
Definition: Odometry2D.h:142
Distance getTotalDistance() const
Get the total distance traveled.
Definition: Odometry2D.h:156
void update()
Update the odometry state with new speed and steering angle Call this method in your control loop.
Definition: Odometry2D.h:130
float getTheta() const
Get the current orientation (radians)
Definition: Odometry2D.h:146
Speed getSpeed() const
Get the current speed (meters/second)
Definition: Odometry2D.h:144
float getAngularVelocity() const
Get the current angular velocity (radians/second)
Definition: Odometry2D.h:152
Coordinate< DistanceM > getPosition() const
Get the current 2D position (meters)
Definition: Odometry2D.h:138
Represents a speed measurement with unit conversion support.
Definition: Speed.h:40
DistanceUnit
Supported distance units for conversion and representation.
Definition: Distance.h:10
AngleUnit
Supported angle units for conversion and representation.
Definition: Angle.h:11
SpeedUnit
Supported speed units for conversion and representation.
Definition: Speed.h:10
Represents a 2D incremental motion update (dx, dy, dtheta).
Definition: MotionState2D.h:15
Represents a 2D coordinate frame in a hierarchical frame tree.
Definition: FrameMgr2D.h:130
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72