6#include "TinyRobotics/communication/Message.h"
7#include "TinyRobotics/communication/MessageHandler.h"
8#include "TinyRobotics/communication/MessageSource.h"
9#include "TinyRobotics/coordinates/Coordinate.h"
10#include "TinyRobotics/coordinates/Orientation3D.h"
11#include "TinyRobotics/units/AngularVelocity.h"
12#include "TinyRobotics/units/Distance.h"
13#include "TinyRobotics/units/Speed.h"
15namespace tinyrobotics {
18
19
20
21
22
23
24
25
26
27
28
29
36 float yaw, pitch,
roll;
40 Fusion3D() { reset(0); }
42 bool begin(
const Coordinate<
float>& initialPosition = {0, 0, 0},
45 state.x = initialPosition.x;
46 state.y = initialPosition.y;
47 state.z = initialPosition.z;
48 state.yaw = initialOrientation
.yaw;
49 state.pitch = initialOrientation
.pitch;
51 state.vx = state.vy = state
.vz = 0.0f;
57 void predict(uint32_t timeMs,
float ax = 0.0f,
float ay = 0.0f,
59 uint32_t dtMs = timeMs - lastPredictTime;
60 lastPredictTime = timeMs;
61 float dt = dtMs * 0.001f;
62 if (dt <= 0.0f)
return;
63 if (dt > 0.1f) dt = 0.1f;
71 state.x += state.vx * dt;
72 state.y += state.vy * dt;
73 state
.z += state
.vz * dt;
76 state
.roll += (omegaXMeasured - state.gyroBiasX) * dt;
77 state.pitch += (omegaYMeasured - state.gyroBiasY) * dt;
78 state.yaw += (omegaZMeasured - state
.gyroBiasZ) * dt;
85 void updateIMU(uint32_t timeMs,
float omegaX,
float omegaY,
float omegaZ,
86 float ax,
float ay,
float az) {
88 omegaXMeasured = omegaX;
89 omegaYMeasured = omegaY;
90 omegaZMeasured = omegaZ;
91 predict(timeMs, ax, ay, az);
95 void updateGPS(uint32_t timeMs,
float x,
float y,
float z,
float accuracy) {
99 float residualX = x - state.x;
101 float residualY = y - state.y;
103 float residualZ = z - state
.z;
105 state.x += residualX * 0.5f;
106 state.y += residualY * 0.5f;
107 state
.z += residualZ * 0.5f;
111 void updateOrientation(uint32_t timeMs,
float yaw,
float pitch,
float roll) {
121 State3D getState()
const {
return state; }
122 Coordinate<
float> getPosition()
const {
return {state.x, state.y, state.z}; }
123 Speed3D getVelocity()
const {
124 return Speed3D(state.vx, state.vy, state
.vz,
SpeedUnit::MPS);
132 uint32_t lastPredictTime = 0;
133 float omegaXMeasured = 0.0f, omegaYMeasured = 0.0f, omegaZMeasured = 0.0f;
137 state.yaw = normalizeAngleRad(state.yaw);
138 state.pitch = normalizeAngleRad(state.pitch);
142 void reset(uint32_t timeMs = millis()) {
145 state.x = state.y = state
.z = 0.0f;
146 state.vx = state.vy = state
.vz = 0.0f;
147 state.yaw = state.pitch = state
.roll = 0.0f;
148 state.gyroBiasX = state.gyroBiasY = state
.gyroBiasZ = 0.0f;
150 lastPredictTime = timeMs;
151 omegaXMeasured = omegaYMeasured = omegaZMeasured = 0.0f;
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
3D sensor fusion EKF for position, orientation, and velocity estimation.
Definition: Fusion3D.h:30
Base class for message sources in the TinyRobotics communication framework.
Definition: MessageSource.h:35
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
SpeedUnit
Supported speed units for conversion and representation.
Definition: Speed.h:10
Definition: Fusion3D.h:32
float gyroBiasZ
Gyro bias (rad/s)
Definition: Fusion3D.h:37
float roll
Orientation (radians)
Definition: Fusion3D.h:36
float vz
Velocity (m/s)
Definition: Fusion3D.h:35
uint32_t timeMs
Timestamp in ms.
Definition: Fusion3D.h:33
float z
Position (meters)
Definition: Fusion3D.h:34