5#include "TinyRobotics/communication/Message.h"
6#include "TinyRobotics/odometry/IOdometryModel3D.h"
7#include "TinyRobotics/units/Units.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
56
57
58
59
60
61
62
63
64
67 static constexpr int NUM_MOTORS = 4;
70
71
72
74 this->callback = callback;
75 this->userData = userData;
79
80
81
82
83
84
85
86
87
88
89
91 float maxPitchRate = 200.0f,
float maxYawRate = 100.0f)
93 maxRollRate(maxRollRate * 0.01745329252f),
94 maxPitchRate(maxPitchRate * 0.01745329252f),
95 maxYawRate(maxYawRate * 0.01745329252f) {
96 motorPercent.fill(0.0f);
100
101
103 if (motor < 0 || motor >= NUM_MOTORS)
return;
104 motorPercent[motor] = clamp(percent, 0.0f, 100.0f);
107 if (callback && motor == (NUM_MOTORS - 1)) callback(userData);
111 void (*callback)(
void*) =
nullptr;
112 void* userData =
nullptr;
115
116
117
119 if (msg.content == MessageContent::MotorSpeed &&
120 msg.origin_id < NUM_MOTORS) {
121 setMotorPercent(msg.origin_id, msg.value);
128
129
130
134 for (
float p : motorPercent) avg += p;
138 vz = (avg / 100.0f) * maxVz;
142
143
144
145
146
149 float rollInput = ((motorPercent[0] + motorPercent[3]) -
150 (motorPercent[1] + motorPercent[2])) /
153 float pitchInput = ((motorPercent[0] + motorPercent[1]) -
154 (motorPercent[2] + motorPercent[3])) /
157 float yawInput = ((motorPercent[0] + motorPercent[2]) -
158 (motorPercent[1] + motorPercent[3])) /
160 wx = rollInput * maxRollRate;
161 wy = pitchInput * maxPitchRate;
162 wz = yawInput * maxYawRate;
166 std::array<
float, NUM_MOTORS> motorPercent;
172 static float clamp(
float v,
float min,
float max) {
173 return (v < min) ? min : (v > max) ? max : v;
IOdometryModel3D implementation for quadcopters/drones using motor percentages.
Definition: DroneOdometryModel3D.h:65
void registerCallback(void(*callback)(void *), void *userData) override
Register a callback to be invoked on relevant events (e.g., input change).
Definition: DroneOdometryModel3D.h:73
bool onMessage(const Message< float > &msg)
Handle incoming motor speed messages (MessageContent::MotorSpeed, origin_id = motor index).
Definition: DroneOdometryModel3D.h:118
void getAngularVelocity(float &wx, float &wy, float &wz) const override
Compute angular velocity (wx, wy, wz) in rad/s (body frame). Roll and pitch rates are proportional to...
Definition: DroneOdometryModel3D.h:147
DroneOdometryModel3D(float maxVz=5.0f, float maxRollRate=200.0f, float maxPitchRate=200.0f, float maxYawRate=100.0f)
Construct a DroneOdometryModel3D with configurable max rates.
Definition: DroneOdometryModel3D.h:90
void getLinearVelocity(float &vx, float &vy, float &vz) const override
Compute linear velocity (vx, vy, vz) in m/s (body frame). Only vz is modeled (vertical climb/descent)...
Definition: DroneOdometryModel3D.h:131
void setMotorPercent(int motor, float percent)
Set the percentage (0..100) for a given motor (0..3).
Definition: DroneOdometryModel3D.h:102
Abstract interface for 3D odometry models. Provides access to current linear and angular velocities f...
Definition: IOdometryModel3D.h:14
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72