6#include "TinyRobotics/coordinates/Coordinate.h"
7#include "TinyRobotics/coordinates/Orientation3D.h"
8#include "TinyRobotics/units/Units.h"
9#include "TinyRobotics/odometry/IOdometryModel3D.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
44 : vehicle(vehicle), model(model) {
45 vehicle.subscribe(model);
53
54
55
56
57
58
61 position = initialPosition;
62 orientation = initialOrientation;
69
70
72 uint32_t now = millis();
73 uint32_t deltaTimeMs = now - lastUpdateTimeMs;
74 if (lastUpdateTimeMs > 0) {
75 float vx, vy, vz, wx, wy, wz;
78 float dt =
static_cast<
float>(deltaTimeMs) / 1000.0f;
80 orientation
.roll += wx * dt;
81 orientation
.pitch += wy * dt;
82 orientation
.yaw += wz * dt;
85 float cr = std::cos(orientation
.roll), sr = std::sin(orientation
.roll);
86 float cp = std::cos(orientation
.pitch), sp = std::sin(orientation
.pitch);
87 float cy = std::cos(orientation
.yaw), sy = std::sin(orientation
.yaw);
89 float vx_world = cy * cp * vx + (cy * sp * sr - sy * cr) * vy +
90 (cy * sp * cr + sy * sr) * vz;
91 float vy_world = sy * cp * vx + (sy * sp * sr + cy * cr) * vy +
92 (sy * sp * cr - cy * sr) * vz;
93 float vz_world = -sp * vx + cp * sr * vy + cp * cr * vz;
95 float dx = vx_world * dt;
96 float dy = vy_world * dt;
97 float dz = vz_world * dt;
101 totalDistance += std::sqrt(dx * dx + dy * dy + dz * dz);
104 lastUpdateTimeMs = now;
123 MessageSource& vehicle;
127 float totalDistance = 0.0f;
129 uint32_t lastUpdateTimeMs = 0;
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Represents a 3D distance or position vector with unit support.
Definition: Distance.h:164
Represents a distance measurement with unit conversion support.
Definition: Distance.h:40
Abstract interface for 3D odometry models. Provides access to current linear and angular velocities f...
Definition: IOdometryModel3D.h:14
virtual void getAngularVelocity(float &wx, float &wy, float &wz) const =0
Get the current angular velocity (wx, wy, wz) in rad/s (robot frame).
virtual void getLinearVelocity(float &vx, float &vy, float &vz) const =0
Get the current linear velocity (vx, vy, vz) in m/s (robot frame).
virtual void registerCallback(void(*callback)(void *), void *userData)
Register a callback to be invoked on relevant events (e.g., input change, update).
Definition: IOdometryModel3D.h:22
Tracks 3D position and orientation of a robot using velocity and angular rates.
Definition: Odometry3D.h:41
bool begin(Coordinate< float > initialPosition={0, 0, 0}, Orientation3D initialOrientation=Orientation3D())
Initialize the odometry state.
Definition: Odometry3D.h:59
Distance3D getLastDelta() const
Get the last delta update (dx, dy, dz)
Definition: Odometry3D.h:120
Coordinate< float > getPosition() const
Get the current 3D position (meters)
Definition: Odometry3D.h:111
Orientation3D getOrientation() const
Get the current orientation as Orientation3D (yaw, pitch, roll in radians)
Definition: Odometry3D.h:114
Distance getTotalDistance() const
Get the total distance traveled.
Definition: Odometry3D.h:116
void update()
Update the odometry state with new velocities and angular rates.
Definition: Odometry3D.h:71
Simple 3D orientation class (yaw, pitch, roll in radians)
Definition: Orientation3D.h:13
float roll
Roll angle (radians)
Definition: Orientation3D.h:22
float pitch
Pitch angle (radians)
Definition: Orientation3D.h:21
float yaw
Yaw angle (radians)
Definition: Orientation3D.h:20
DistanceUnit
Supported distance units for conversion and representation.
Definition: Distance.h:10