5#include "TinyRobotics/communication/MessageHandler.h"
6#include "TinyRobotics/odometry/ISpeedSource.h"
7#include "TinyRobotics/units/Speed.h"
9namespace tinyrobotics {
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
61 SpeedFromThrottle(
float maxSpeedMps, uint8_t numMotors = 1) {
62 this->numMotors = numMotors;
63 this->maxSpeedMps = maxSpeedMps;
64 speedMps.resize(numMotors, 0.0f);
68 SpeedFromThrottle(
Speed maxSpeed, uint8_t numMotors = 1)
71 void setMaxSpeed(
Speed speed) {
72 this->maxSpeedMps = speed.getValue(
SpeedUnit::MPS);
78 if (msg.content == MessageContent::Throttle) {
79 setThrottlePercent(msg.value);
87 auto it = calibrationData.begin();
88 while (it != calibrationData.end() && it->first < throttlePercent) {
91 if (it != calibrationData.end() && it->first == throttlePercent) {
92 it->second = speedMps;
94 calibrationData.insert(
95 it, std::pair<
float,
float>(throttlePercent, speedMps));
104 assert(motor < numMotors);
105 speedMps[motor] = getSpeedMPS(throttlePercent);
106 sendSpeedMessage(motor);
111 assert(motor < numMotors);
112 return Speed(speedMps[motor], SpeedUnit::MPS);
116 assert(motor < numMotors);
117 return Speed(speedMps[motor], SpeedUnit::MPS);
120 size_t getMotorCount()
const override {
return numMotors; }
123 std::vector<std::pair<
float,
float>> calibrationData;
124 std::vector<
float> speedMps;
125 uint8_t numMotors = 1;
126 float maxSpeedMps = 0.0f;
130 calibrationData.clear();
137 void sendSpeedMessage(uint8_t motor = 0) {
138 assert(motor < numMotors);
139 Message<
float> msg(MessageContent::Speed, speedMps[motor],
140 Unit::MetersPerSecond, MessageOrigin::System);
141 msg.origin_id = motor;
142 MessageSource::sendMessage(msg);
147 if (calibrationData.empty())
return 0.0f;
149 if (throttlePercent <= calibrationData.front().first)
150 return calibrationData.front().second;
152 if (throttlePercent >= calibrationData.back().first)
153 return calibrationData.back().second;
155 for (size_t i = 1; i < calibrationData.size(); ++i) {
156 float t0 = calibrationData[i - 1].first;
157 float t1 = calibrationData[i].first;
158 if (throttlePercent >= t0 && throttlePercent <= t1) {
159 float s0 = calibrationData[i - 1].second;
160 float s1 = calibrationData[i].second;
161 float alpha = (throttlePercent - t0) / (t1 - t0);
162 return s0 + alpha * (s1 - s0);
Interface for speed sources.
Definition: ISpeedSource.h:9
Interface for handling messages in the TinyRobotics framework.
Definition: MessageHandler.h:18
Estimates vehicle speed from throttle percentage using calibration data.
Definition: SpeedFromThrottle.h:59
Speed getSpeed(uint8_t motor=0) const override
Get the actual speed based on the last throttle value.
Definition: SpeedFromThrottle.h:110
Speed updateSpeed(uint32_t deltaTimeMs, uint8_t motor=0) override
For sources with inertia, call this in your main loop with the elapsed time (in milliseconds) to upda...
Definition: SpeedFromThrottle.h:115
void addSpeedCalibration(float throttlePercent, float speedMps)
Add or update a calibration point (throttlePercent, speedMps)
Definition: SpeedFromThrottle.h:86
void setThrottlePercent(float throttlePercent, uint8_t motor=0) override
Define the actual throttle value.
Definition: SpeedFromThrottle.h:103
void clearCalibration()
Clear all calibration data.
Definition: SpeedFromThrottle.h:100
bool onMessage(const Message< float > &msg) override
Handle Throttle messages to update speed.
Definition: SpeedFromThrottle.h:77
float getSpeedMPS(float throttlePercent) const
Get speed in meters per second by interpolating calibration data.
Definition: SpeedFromThrottle.h:146
Represents a speed measurement with unit conversion support.
Definition: Speed.h:40
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