5#include "TinyRobotics/communication/Message.h"
6#include "TinyRobotics/odometry/IOdometryModel2D.h"
7#include "TinyRobotics/units/Units.h"
10namespace tinyrobotics {
13
14
15
16
17
18
21 OdometryDifferentialDriveModel(
Distance wheelBase) : wheelBase(wheelBase) {}
24 leftSpeed = p_speedSource->updateSpeed(deltaTimeMs, 0);
25 rightSpeed = p_speedSource->updateSpeed(deltaTimeMs, 1);
29
30
31
32
35 float vLeft = leftSpeed.getValue(
SpeedUnit::MPS);
36 float vRight = rightSpeed.getValue(
SpeedUnit::MPS);
37 float omega = (vRight - vLeft) / wb;
38 return omega *
static_cast<
float>(deltaTimeMs) / 1000.0f;
41 void computeDeltaXY(
float theta, uint32_t deltaTimeMs,
float& deltaX,
42 float& deltaY)
const override {
44 float vLeft = leftSpeed.getValue(
SpeedUnit::MPS);
45 float vRight = rightSpeed.getValue(
SpeedUnit::MPS);
46 float v = 0.5f * (vLeft + vRight);
47 float dt = deltaTimeMs / 1000.0f;
48 deltaX = v * std::cos(theta) * dt;
49 deltaY = v * std::sin(theta) * dt;
52 if (msg.origin != MessageOrigin::Vehicle &&
53 msg.origin != MessageOrigin::Motor)
56 switch (msg.content) {
57 case MessageContent::SteeringAngle:
58 if (msg.unit != Unit::AngleRadian)
return false;
59 steeringAngle = Angle(msg.value, AngleUnit::RAD);
61 case MessageContent::MotorSpeed:
62 if (msg.unit != Unit::Percent)
return false;
64 switch (msg.origin_id) {
66 p_speedSource->setThrottlePercent(msg.value, 0);
67 leftSpeed = p_speedSource->getSpeed(0);
70 p_speedSource->setThrottlePercent(msg.value, 1);
71 rightSpeed = p_speedSource->getSpeed(1);
86 void setCallback(
void (*callback)(
void*),
void* userData) {
87 this->callback = callback;
88 this->userData = userData;
92 p_speedSource = &speedSource;
95 Speed getSpeed()
const override {
return (leftSpeed + rightSpeed) / 2; }
104 void (*callback)(
void*) =
nullptr;
105 void* userData =
nullptr;
Represents an angle with unit conversion and wrap-around support.
Definition: Angle.h:42
Represents a distance measurement with unit conversion support.
Definition: Distance.h:40
Abstract interface for 2D odometry models.
Definition: IOdometryModel2D.h:9
Interface for speed sources.
Definition: ISpeedSource.h:9
Odometry model for differential drive robots.
Definition: OdometryDifferentialDriveModel.h:19
void computeDeltaXY(float theta, uint32_t deltaTimeMs, float &deltaX, float &deltaY) const override
Compute position change (deltaX, deltaY) for odometry kinematics.
Definition: OdometryDifferentialDriveModel.h:41
Speed getSpeed() const override
Get the current speed of the vehicle.
Definition: OdometryDifferentialDriveModel.h:95
bool onMessage(const Message< float > &msg)
Handle an incoming message (pure virtual).
Definition: OdometryDifferentialDriveModel.h:51
void setSpeedSource(ISpeedSource &speedSource)
Set the speed source (e.g., encoder, estimator) for this model.
Definition: OdometryDifferentialDriveModel.h:91
float computeDeltaTheta(uint16_t deltaTimeMs) const override
Compute heading change (deltaTheta) for differential drive.
Definition: OdometryDifferentialDriveModel.h:33
Angle getSteeringAngle() const override
Get the current steering angle of the vehicle.
Definition: OdometryDifferentialDriveModel.h:96
virtual void updateSpeed(uint32_t deltaTimeMs)
Update the speed estimate based on elapsed time.
Definition: OdometryDifferentialDriveModel.h:23
Represents a speed measurement with unit conversion support.
Definition: Speed.h:40
DistanceUnit
Supported distance units for conversion and representation.
Definition: Distance.h:10
SpeedUnit
Supported speed units for conversion and representation.
Definition: Speed.h:10
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72