5#include "TinyRobotics/communication/Message.h"
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/coordinates/GPSCoordinate.h"
12namespace tinyrobotics {
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
46template <
typename T =
float>
51 bool begin(T initialAngleDeg,
Coordinate<DistanceM> initialPosition) {
52 position = initialPosition;
53 headingRad = initialAngleDeg * M_PI / 180.0;
64 void end() { is_active =
false; }
68 void update(T accelX, T accelY, T gyroZ_in,
unsigned long nowMillis) {
69 if (!is_active)
return;
70 if (lastUpdateMillis == 0) {
71 lastUpdateMillis = nowMillis;
75 T dt = (nowMillis - lastUpdateMillis) / (T)1000;
76 lastUpdateMillis = nowMillis;
80 headingRad += gyroZ_in * dt;
82 headingRad = normalizeAngleRad(headingRad);
85 T ax_world = accelX * std::cos(headingRad) - accelY * std::sin(headingRad);
86 T ay_world = accelX * std::sin(headingRad) + accelY * std::cos(headingRad);
89 static T vx = 0, vy = 0;
92 speedMPS = std::sqrt(vx * vx + vy * vy);
100 totalDistanceM += std::sqrt(dx * dx + dy * dy);
102 lastDelta = {
static_cast<
float>(dx),
static_cast<
float>(dy),
103 static_cast<
float>(gyroZ_in * dt)};
104 lastAngularVelocity = gyroZ_in;
105 timestamp = millis();
116 float getTheta()
const {
return headingRad; }
132 return Transform2D(position, headingRad * 180.0f / M_PI);
136 bool is_active =
false;
137 float headingRad = 0;
140 unsigned long lastUpdateMillis = 0;
141 Delta2D lastDelta = {0.0f, 0.0f, 0.0f};
142 float lastAngularVelocity = 0.0f;
143 float totalDistanceM = 0.0f;
144 uint32_t timestamp = 0;
150 msgPos.origin = MessageOrigin::IMU;
154 Message<
float> msgHeading{MessageContent::Heading, headingRad,
156 msgHeading.origin = MessageOrigin::IMU;
157 sendMessage(msgHeading);
160 Message<
float> msgSpeed{MessageContent::Speed, speedMPS,
161 Unit::MetersPerSecond};
162 msgSpeed.origin = MessageOrigin::IMU;
163 sendMessage(msgSpeed);
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
Basic 2D IMU dead-reckoning class.
Definition: IMU2D.h:47
float getLinearVelocity() const
Get the current linear velocity (meters/second)
Definition: IMU2D.h:114
void update(T accelX, T accelY, T gyroZ_in, unsigned long nowMillis)
Definition: IMU2D.h:68
Delta2D getLastDelta() const
Get the last delta update (dx, dy, dtheta)
Definition: IMU2D.h:110
Angle getHeading() const
get heading as Angle
Definition: IMU2D.h:121
Distance getTotalDistance() const
Get the total distance traveled.
Definition: IMU2D.h:125
float getTheta() const
Get the current orientation (radians)
Definition: IMU2D.h:116
uint32_t getTimestamp() const
Get time of last update.
Definition: IMU2D.h:129
Speed getSpeed() const
get speed as Speed
Definition: IMU2D.h:123
float getAngularVelocity() const
Get the current angular velocity (radians/second)
Definition: IMU2D.h:112
Coordinate< DistanceM > getPosition() const
get position
Definition: IMU2D.h:119
Interface for representing the navigation state of a robot in 2D space.
Definition: MotionState2D.h:35
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
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72