TinyRobotics
Loading...
Searching...
No Matches
SpeedFromThrottleWithInertia.h
1
2#pragma once
3#include <Arduino.h>
4
6
7namespace tinyrobotics {
8
9/**
10 * @class SpeedFromThrottleWithInertia
11 * @ingroup odometry
12 * @brief Estimates speed from throttle with acceleration/inertia modeling (for
13 * boats, etc).
14 *
15 * This class extends SpeedFromThrottle by simulating acceleration/inertia:
16 * - Speed ramps toward the target value at a configurable acceleration rate
17 * (m/s^2).
18 * - Call updateSpeed(deltaTimeSec) in your main loop to advance the speed
19 * estimate.
20 * - Useful for boats or vehicles with slow acceleration.
21 *
22 * Example usage:
23 * @code
24 * SpeedFromThrottleWithInertia speedMap(2.0f, 0.2f); // 2 m/s max, 0.2 m/s^2
25 * // In your loop:
26 * speedMap.updateSpeed(dt); // dt in seconds
27 * float speed = speedMap.getSpeed();
28 * @endcode
29 */
31 public:
32 SpeedFromThrottleWithInertia(float maxSpeedMps, float accelerationMps2, uint8_t numMotors = 1)
33 : SpeedFromThrottle(maxSpeedMps, numMotors), acceleration(accelerationMps2) {
34 throttlePercent.resize(numMotors, 0.0f);
35 lastUpdateMs.resize(numMotors, 0);
36 }
37 SpeedFromThrottleWithInertia(Speed maxSpeed, float accelerationMps2, uint8_t numMotors = 1)
38 : SpeedFromThrottle(maxSpeed, numMotors), acceleration(accelerationMps2) {
39 throttlePercent.resize(numMotors, 0.0f);
40 lastUpdateMs.resize(numMotors, 0);
41 }
42
43 void setAcceleration(float a) { acceleration = a; }
44 float getAcceleration() const { return acceleration; }
45
46 // Override to store throttle for inertia simulation
47 void setThrottlePercent(float throttle, uint8_t motor = 0) override {
48 if (motor >= numMotors) return;
49 throttlePercent[motor] = throttle;
50 lastUpdateMs[motor] = millis();
51 // Do not ramp speed here; updateSpeed() will be called in the main loop
52 }
53
54 // Optionally, allow instant set for testing
55 void setSpeedInstant(float speed, uint8_t motor = 0) {
56 if (motor >= numMotors) return;
57 speedMps[motor] = speed;
58 sendSpeedMessage(motor);
59 }
60 // ISpeedSource interface implementation
61 Speed getSpeed(uint8_t motor = 0) const override {
62 if (motor >= numMotors) return Speed(0.0f, SpeedUnit::MPS);
63 return Speed(speedMps[motor], SpeedUnit::MPS);
64 }
65
66 // Call this in your main loop with the elapsed time (in milliseconds)
67 Speed updateSpeed(uint32_t deltaTimeMs, uint8_t motor = 0) override {
68 if (motor >= numMotors) return Speed(0.0f, SpeedUnit::MPS);
69 float target = getSpeedMPS(throttlePercent[motor]);
70 float delta = target - speedMps[motor];
71 float maxDelta = acceleration * deltaTimeMs / 1000.0f; // Convert ms to seconds for maxDelta
72 if (fabs(delta) <= maxDelta) {
73 speedMps[motor] = target;
74 } else {
75 speedMps[motor] += (delta > 0 ? maxDelta : -maxDelta);
76 }
77 sendSpeedMessage(motor);
78 lastUpdateMs[motor] = millis();
79 return Speed(speedMps[motor], SpeedUnit::MPS);
80 }
81
82 protected:
83 float acceleration; // m/s^2
84 std::vector<float> throttlePercent;
85 std::vector<unsigned long> lastUpdateMs;
86
87};
88
89} // namespace tinyrobotics
Estimates speed from throttle with acceleration/inertia modeling (for boats, etc).
Definition: SpeedFromThrottleWithInertia.h:30
void setThrottlePercent(float throttle, uint8_t motor=0) override
Define the actual throttle value.
Definition: SpeedFromThrottleWithInertia.h:47
Speed getSpeed(uint8_t motor=0) const override
Get the actual speed based on the last throttle value.
Definition: SpeedFromThrottleWithInertia.h:61
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: SpeedFromThrottleWithInertia.h:67
Estimates vehicle speed from throttle percentage using calibration data.
Definition: SpeedFromThrottle.h:59
Represents a speed measurement with unit conversion support.
Definition: Speed.h:40
SpeedUnit
Supported speed units for conversion and representation.
Definition: Speed.h:10