TinyRobotics
Loading...
Searching...
No Matches
SpektrumSatelliteMessageSource.h
1#pragma once
3#include "SpektrumSatellite.h"
4
5namespace tinyrobotics {
6
7/**
8 * @class SpektrumSatelliteMessageSource
9 * @ingroup communication
10 * @brief Publishes control messages from a Spektrum satellite receiver to the
11 * TinyRobotics message bus.
12 *
13 * This class bridges a Spektrum satellite receiver and the TinyRobotics message
14 * bus system. It polls the receiver for new frames and, when available,
15 * publishes standardized control messages:
16 * - Roll (degrees)
17 * - Pitch (degrees)
18 * - Yaw (degrees)
19 * - Throttle (percent)
20 *
21 * Each message is published with the appropriate unit and origin
22 * (MessageOrigin::RemoteControl). The channel value range is automatically set
23 * to 0-100 and mapped to -90..+90 degrees for angles.
24 *
25 * This class depends on https://github.com/pschatzmann/SpektrumSatellite
26 * The SpektrumSatellite class must be set up proplerly and passed to the
27 * constructor.
28 *
29 * Usage:
30 * @code
31 * SpektrumSatellite<uint16_t> satellite;
32 * SpektrumSatelliteMessageSource<uint16_t> source(satellite);
33 * // In your main loop:
34 * source.run();
35 * @endcode
36 *
37 * @tparam T Data type for channel values (default: uint16_t)
38 */
39
40template <class T = uint16_t>
42 public:
43 SpektrumSatelliteMessageSource(SpektrumSatellite<T>& satellite)
45 // scale the values from 0 to 1000
47 };
48
49 /// call in your loop to process incoming satellite messages and publish to
50 /// handlers
51 void update() {
52 if (satellite.getFrame()) {
53 float rollDegrees = toDegrees(satellite.getAileron());
54 float pitchDegrees = toDegrees(satellite.getElevator());
55 float yawDegrees = toDegrees(satellite.getRudder());
56 float throttle = 0.1f * satellite.getThrottle();
57
58 publish(Message(MessageContent::Throttle, Unit::Percent),
59 MessageOrigin::RemoteControl);
60
61 // 3D devices
62 publish(Message(MessageContent::Roll,
63 Angle(rollDegrees, Unit::AngleDegree),
64 MessageOrigin::RemoteControl));
65
66 publish(Message(MessageContent::Pitch,
67 Angle(pitchDegrees, Unit::AngleDegree),
68 MessageOrigin::RemoteControl));
69
70 publish(Message(MessageContent::Yaw, Angle(yawDegrees, Unit::AngleDegree),
71 MessageOrigin::RemoteControl));
72
73 // 2D
74 publish(Message(MessageContent::SteeringAngle, rollDegrees,
76 MessageOrigin::RemoteControl);
77 }
78 }
79
80 protected:
81 SpektrumSatellite<T>& satellite;
82
83 float toDegrees(T value) {
84 // Assuming the satellite values are in the range 0-1000, map to -90 to +90
85 return map(value, 0.0f, 1000.0f, -90.0f, 90.0f);
86 }
87};
88
89} // namespace tinyrobotics
Publishes control messages from a Spektrum satellite receiver to the TinyRobotics message bus.
Definition: SpektrumSatelliteMessageSource.h:41
void update()
Definition: SpektrumSatelliteMessageSource.h:51
Unit
Units for message values.
Definition: Common.h:45
@ AngleDegree
Angle in degrees.
@ Percent
Percentage (0-100)