TinyRobotics
Loading...
Searching...
No Matches
MessageHandlerPrint.h
1#pragma once
3#include "Print.h"
4
5namespace tinyrobotics {
6
7// from enum class MessageContent in Message.h - update this array to match all
8// values in the MessageContent enum String arrays for enums (shared by all
9// handlers) Update this array to match all values in the MessageContent enum
10// (see Message.h)
11constexpr const char* messageContentStr[] = {
12 "Undefined", // 0
13 "Angle", // 1
14 "Pitch", // 2
15 "Roll", // 3
16 "Yaw", // 4
17 "Throttle", // 5
18 "Speed", // 6
19 "SteeringAngle", // 7
20 "Turn", // 8
21 "Heading", // 9
22 "MotorSpeed", // 10
23 "Position", // 11
24 "PositionGPS", // 12
25 "Distance", // 13
26 "Temperature", // 14
27 "Error", // 15
28 "Density" // 16
29 "MotionState" // 17
30 "Obstacle" // 18
31};
32
33// from enum class Unit in Common.h - update this array to match all values in
34// the Unit enum
35constexpr const char* unitStr[] = {
36 "Undefined", // 0
37 "Percent", // 1
38 "MetersPerSecond", // 2
39 "RadiansPerSecond", // 3
40 "Meters", // 4
41 "Centimeters", // 5
42 "Millimeters", // 5
43 "AngleDegree", // 6
44 "AngleRadian", // 7
45 "TemperatureC", // 8
46 "TemperatureF", // 9
47 "Pixel" // 10
48};
49
50// from enum class MessageOrigin in Message.h - update this array to match all
51// values in the MessageOrigin enum
52constexpr const char* originStr[] = {
53 "Undefined", // 0
54 "RemoteControl", // 1
55 "Autonomy", // 2
56 "Sensor", // 3
57 "System", // 4
58 "Motor", // 5
59 "Servo", // 6
60 "Rudder", // 7
61 "Aileron", // 8
62 "Elevator", // 9
63 "IMU", // 10
64 "LIDAR", // 11
65 "Camera", // 12
66 "GPS", // 13
67 "Vehicle", // 14
68 "Odometry", // 15
69 "Navigation", // 16
70 "User" // 17
71};
72
73/**
74 * @brief Message handler that writes all received messages as raw binary to a
75 * Print stream.
76 *
77 * This class implements the MessageHandler interface and outputs the content of
78 * all received messages (float, Coordinate<float>, GPSCoordinate) as raw binary
79 * data to the provided Print object (e.g., Serial, file, etc).
80 *
81 * Example usage:
82 * @code
83 * MessageHandlerBinary printer(Serial);
84 * source.subscribe(printer);
85 * @endcode
86 *
87 * This is useful for efficient logging, binary protocols, or communication with
88 * systems that expect binary-encoded messages.
89 * @ingroup communication
90 */
92 public:
93 MessageHandlerBinary() : printer_(*static_cast<Print*>(nullptr)) {}
94 MessageHandlerBinary(Print& printer) : printer_(printer) {}
95 bool onMessage(const Message<float>& msg) override {
96 size_t written = printer_.write((const uint8_t*)&msg, sizeof(msg));
97 return written == sizeof(msg);
98 }
99
100 bool onMessage(const Message<Coordinate<float>>& msg) override {
101 size_t written = printer_.write((const uint8_t*)&msg, sizeof(msg));
102 return written == sizeof(msg);
103 }
104
105 bool onMessage(const Message<GPSCoordinate>& msg) override {
106 size_t written = printer_.write((const uint8_t*)&msg, sizeof(msg));
107 return written == sizeof(msg);
108 }
109
110 bool onMessage(const Message<MotionState3D>& msg) override {
111 size_t written = printer_.write((const uint8_t*)&msg, sizeof(msg));
112 return written == sizeof(msg);
113 }
114
115 void setOutput(Print& printer) { printer_ = printer; }
116
117 protected:
118 Print& printer_;
119};
120
121/**
122 * @brief Message handler that prints all received messages to a Print stream.
123 *
124 * This class implements the MessageHandler interface and outputs the content of
125 * all received messages (float, Coordinate<float>, GPSCoordinate) in a
126 * human-readable format to the provided Print object (e.g., Serial, file, etc).
127 * It uses string arrays to print the message type, unit, and source as readable
128 * text.
129 *
130 * Example usage:
131 * @code
132 * MessageHandlerPrint printer(Serial);
133 * source.subscribe(printer);
134 * @endcode
135 *
136 * This is useful for debugging, logging, or monitoring message traffic in a
137 * robotics system.
138 * @ingroup communication
139 */
140class MessageHandlerPrint : public MessageHandler {
141 public:
142 MessageHandlerPrint() : printer_(*static_cast<Print*>(nullptr)) {}
143 MessageHandlerPrint(Print& printer) : printer_(printer) {}
144 void setOutput(Print& printer) { printer_ = printer; }
145 bool onMessage(const Message<float>& msg) override {
146 printer_.print("[Message] Type: ");
147 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
148 printer_.print(", Value: ");
149 printer_.print(msg.value);
150 printer_.print(" ");
151 printer_.print(unitStr[static_cast<int>(msg.unit)]);
152 printer_.print(", Source: ");
153 printer_.println(originStr[static_cast<int>(msg.origin)]);
154 return true; // Indicate that the message was handled
155 }
156
157 bool onMessage(const Message<Coordinate<float>>& msg) override {
158 printer_.print("[Message] Type: ");
159 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
160 printer_.print(", X: ");
161 printer_.print(msg.value.x);
162 printer_.print(", Y: ");
163 printer_.print(msg.value.y);
164 printer_.print(", Unit: ");
165 printer_.print(unitStr[static_cast<int>(msg.unit)]);
166 printer_.print(", Source: ");
167 printer_.println(originStr[static_cast<int>(msg.origin)]);
168 return true; // Indicate that the message was handled
169 }
170
171 bool onMessage(const Message<GPSCoordinate>& msg) override {
172 printer_.print("[Message] Type: ");
173 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
174 printer_.print(", Lat: ");
175 printer_.print(msg.value.latitude);
176 printer_.print(", Lon: ");
177 printer_.print(msg.value.longitude);
178 printer_.print(", Alt: ");
179 printer_.print(msg.value.altitude);
180 printer_.print(", Unit: ");
181 printer_.print(unitStr[static_cast<int>(msg.unit)]);
182 printer_.print(", Source: ");
183 printer_.println(originStr[static_cast<int>(msg.origin)]);
184 return true; // Indicate that the message was handled
185 }
186
187 bool onMessage(const Message<MotionState3D>& msg) override {
188 printer_.print("[Message] Type: MotionState3D");
189 printer_.print(", Pos: (");
190 printer_.print(msg.value.getPosition().x);
191 printer_.print(", ");
192 printer_.print(msg.value.getPosition().y);
193 printer_.print(", ");
194 printer_.print(msg.value.getPosition().z);
195 printer_.print(")");
196 printer_.print(", Orientation: (");
197 printer_.print(msg.value.getOrientation().roll);
198 printer_.print(", ");
199 printer_.print(msg.value.getOrientation().pitch);
200 printer_.print(", ");
201 printer_.print(msg.value.getOrientation().yaw);
202 printer_.print(")");
203 printer_.print(", Speed: (");
204 printer_.print(msg.value.getSpeed().x);
205 printer_.print(", ");
206 printer_.print(msg.value.getSpeed().y);
207 printer_.print(", ");
208 printer_.print(msg.value.getSpeed().z);
209 printer_.print(")");
210 printer_.print(", AngularVel: (");
211 printer_.print(msg.value.getAngularVelocity().x);
212 printer_.print(", ");
213 printer_.print(msg.value.getAngularVelocity().y);
214 printer_.print(", ");
215 printer_.print(msg.value.getAngularVelocity().z);
216 printer_.print(")");
217 printer_.println("");
218 return true;
219 }
220
221 protected:
222 Print& printer_;
223};
224
225/**
226 * @brief Message handler that prints all received messages as XML to a Print
227 * stream.
228 *
229 * This class implements the MessageHandler interface and outputs the content of
230 * all received messages (float, Coordinate<float>, GPSCoordinate) in XML format
231 * to the provided Print object (e.g., Serial, file, etc). It uses the same
232 * string arrays as MessageHandlerPrint to print the message type, unit, and
233 * source as readable text.
234 *
235 * Example usage:
236 * @code
237 * MessageHandlerPrintXML printer(Serial);
238 * source.subscribe(printer);
239 * @endcode
240 *
241 * This is useful for logging, exporting, or integrating message traffic with
242 * systems that consume XML data.
243 * @ingroup communication
244 */
246 public:
247 MessageHandlerPrintXML() : printer_(*static_cast<Print*>(nullptr)) {}
248 MessageHandlerPrintXML(Print& printer) : printer_(printer) {}
249 void setOutput(Print& printer) { printer_ = printer; }
250
251 bool onMessage(const Message<float>& msg) override {
252 printer_.print("<Message type=\"");
253 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
254 printer_.print("\" unit=\"");
255 printer_.print(unitStr[static_cast<int>(msg.unit)]);
256 printer_.print("\" source=\"");
257 printer_.print(originStr[static_cast<int>(msg.origin)]);
258 printer_.print("\" value=\"");
259 printer_.print(msg.value);
260 printer_.println("\"/>");
261 return true;
262 }
263
264 bool onMessage(const Message<Coordinate<float>>& msg) override {
265 printer_.print("<Message type=\"");
266 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
267 printer_.print("\" unit=\"");
268 printer_.print(unitStr[static_cast<int>(msg.unit)]);
269 printer_.print("\" source=\"");
270 printer_.print(originStr[static_cast<int>(msg.origin)]);
271 printer_.print("\"><X>");
272 printer_.print(msg.value.x);
273 printer_.print("</X><Y>");
274 printer_.print(msg.value.y);
275 printer_.println("</Y></Message>");
276 return true;
277 }
278
279 bool onMessage(const Message<GPSCoordinate>& msg) override {
280 printer_.print("<Message type=\"");
281 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
282 printer_.print("\" unit=\"");
283 printer_.print(unitStr[static_cast<int>(msg.unit)]);
284 printer_.print("\" source=\"");
285 printer_.print(originStr[static_cast<int>(msg.origin)]);
286 printer_.print("\"><Lat>");
287 printer_.print(msg.value.latitude);
288 printer_.print("</Lat><Lon>");
289 printer_.print(msg.value.longitude);
290 printer_.print("</Lon><Alt>");
291 printer_.print(msg.value.altitude);
292 printer_.println("</Alt></Message>");
293 return true;
294 }
295
296 bool onMessage(const Message<MotionState3D>& msg) override {
297 printer_.print("<Message type=\"MotionState3D\"");
298 printer_.print(" pos=\"");
299 printer_.print(msg.value.getPosition().x);
300 printer_.print(",");
301 printer_.print(msg.value.getPosition().y);
302 printer_.print(",");
303 printer_.print(msg.value.getPosition().z);
304 printer_.print("\" orientation=\"");
305 printer_.print(msg.value.getOrientation().roll);
306 printer_.print(",");
307 printer_.print(msg.value.getOrientation().pitch);
308 printer_.print(",");
309 printer_.print(msg.value.getOrientation().yaw);
310 printer_.print("\" speed=\"");
311 printer_.print(msg.value.getSpeed().x);
312 printer_.print(",");
313 printer_.print(msg.value.getSpeed().y);
314 printer_.print(",");
315 printer_.print(msg.value.getSpeed().z);
316 printer_.print("\" angularVel=\"");
317 printer_.print(msg.value.getAngularVelocity().x);
318 printer_.print(",");
319 printer_.print(msg.value.getAngularVelocity().y);
320 printer_.print(",");
321 printer_.print(msg.value.getAngularVelocity().z);
322 printer_.println("\"/>");
323 return true;
324 }
325
326 protected:
327 Print& printer_;
328};
329
330/**
331 * @brief Message handler that prints all received messages as JSON to a Print
332 * stream.
333 *
334 * This class implements the MessageHandler interface and outputs the content of
335 * all received messages (float, Coordinate<float>, GPSCoordinate) in JSON
336 * format to the provided Print object (e.g., Serial, file, etc). It uses the
337 * shared string arrays to print the message type, unit, and source as readable
338 * text.
339 *
340 * Example usage:
341 * @code
342 * MessageHandlerPrintJSON printer(Serial);
343 * source.subscribe(printer);
344 * @endcode
345 *
346 * This is useful for logging, exporting, or integrating message traffic with
347 * systems that consume JSON data.
348 * @ingroup communication
349 */
351 public:
352 MessageHandlerPrintJSON() : printer_(*static_cast<Print*>(nullptr)) {}
353 MessageHandlerPrintJSON(Print& printer) : printer_(printer) {}
354 void setOutput(Print& printer) { printer_ = printer; }
355
356 bool onMessage(const Message<float>& msg) override {
357 printer_.print("{\"type\":\"");
358 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
359 printer_.print("\",\"value\":");
360 printer_.print(msg.value);
361 printer_.print(",\"unit\":\"");
362 printer_.print(unitStr[static_cast<int>(msg.unit)]);
363 printer_.print("\",\"source\":\"");
364 printer_.print(originStr[static_cast<int>(msg.origin)]);
365 printer_.println("\"}");
366 return true;
367 }
368
369 bool onMessage(const Message<Coordinate<float>>& msg) override {
370 printer_.print("{\"type\":\"");
371 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
372 printer_.print("\",\"x\":");
373 printer_.print(msg.value.x);
374 printer_.print(",\"y\":");
375 printer_.print(msg.value.y);
376 printer_.print(",\"unit\":\"");
377 printer_.print(unitStr[static_cast<int>(msg.unit)]);
378 printer_.print("\",\"source\":\"");
379 printer_.print(originStr[static_cast<int>(msg.origin)]);
380 printer_.println("\"}");
381 return true;
382 }
383
384 bool onMessage(const Message<GPSCoordinate>& msg) override {
385 printer_.print("{\"type\":\"");
386 printer_.print(messageContentStr[static_cast<int>(msg.content)]);
387 printer_.print("\",\"lat\":");
388 printer_.print(msg.value.latitude);
389 printer_.print(",\"lon\":");
390 printer_.print(msg.value.longitude);
391 printer_.print(",\"alt\":");
392 printer_.print(msg.value.altitude);
393 printer_.print(",\"unit\":\"");
394 printer_.print(unitStr[static_cast<int>(msg.unit)]);
395 printer_.print("\",\"source\":\"");
396 printer_.print(originStr[static_cast<int>(msg.origin)]);
397 printer_.println("\"}");
398 return true;
399 }
400
401 bool onMessage(const Message<MotionState3D>& msg) override {
402 printer_.print("{\"type\":\"MotionState3D\"");
403 printer_.print(",\"pos\":[");
404 printer_.print(msg.value.getPosition().x);
405 printer_.print(",");
406 printer_.print(msg.value.getPosition().y);
407 printer_.print(",");
408 printer_.print(msg.value.getPosition().z);
409 printer_.print("],\"orientation\":[");
410 printer_.print(msg.value.getOrientation().roll);
411 printer_.print(",");
412 printer_.print(msg.value.getOrientation().pitch);
413 printer_.print(",");
414 printer_.print(msg.value.getOrientation().yaw);
415 printer_.print("],\"speed\":[");
416 printer_.print(msg.value.getSpeed().x);
417 printer_.print(",");
418 printer_.print(msg.value.getSpeed().y);
419 printer_.print(",");
420 printer_.print(msg.value.getSpeed().z);
421 printer_.print("],\"angularVel\":[");
422 printer_.print(msg.value.getAngularVelocity().x);
423 printer_.print(",");
424 printer_.print(msg.value.getAngularVelocity().y);
425 printer_.print(",");
426 printer_.print(msg.value.getAngularVelocity().z);
427 printer_.println("]}");
428 return true;
429 }
430
431 protected:
432 Print& printer_;
433};
434
435} // namespace tinyrobotics
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Represents a geodetic GPS coordinate with latitude, longitude, and optional altitude.
Definition: GPSCoordinate.h:52
Message handler that writes all received messages as raw binary to a Print stream.
Definition: MessageHandlerPrint.h:91
bool onMessage(const Message< float > &msg) override
Handle an incoming message (pure virtual).
Definition: MessageHandlerPrint.h:95
Message handler that prints all received messages as JSON to a Print stream.
Definition: MessageHandlerPrint.h:350
bool onMessage(const Message< float > &msg) override
Handle an incoming message (pure virtual).
Definition: MessageHandlerPrint.h:356
Message handler that prints all received messages as XML to a Print stream.
Definition: MessageHandlerPrint.h:245
bool onMessage(const Message< float > &msg) override
Handle an incoming message (pure virtual).
Definition: MessageHandlerPrint.h:251
Message handler that prints all received messages to a Print stream.
Definition: MessageHandlerPrint.h:140
bool onMessage(const Message< float > &msg) override
Handle an incoming message (pure virtual).
Definition: MessageHandlerPrint.h:145
Interface for handling messages in the TinyRobotics framework.
Definition: MessageHandler.h:18
Represents the full 3D motion state of a robot or vehicle.
Definition: MotionState3D.h:53
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72