Control Libraries 6.3.4
All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
clproto.cpp
1#include "clproto.h"
2
3#include <google/protobuf/util/json_util.h>
4#include <google/protobuf/util/type_resolver_util.h>
5
6#include "clproto/encoders.h"
7#include "clproto/decoders.h"
8
9#include <state_representation/State.hpp>
10#include <state_representation/parameters/Parameter.hpp>
11#include <state_representation/space/SpatialState.hpp>
12#include <state_representation/space/cartesian/CartesianState.hpp>
13#include <state_representation/space/cartesian/CartesianPose.hpp>
14#include <state_representation/space/cartesian/CartesianTwist.hpp>
15#include <state_representation/space/cartesian/CartesianAcceleration.hpp>
16#include <state_representation/space/cartesian/CartesianWrench.hpp>
17#include <state_representation/space/Jacobian.hpp>
18#include <state_representation/space/joint/JointState.hpp>
19#include <state_representation/space/joint/JointPositions.hpp>
20#include <state_representation/space/joint/JointVelocities.hpp>
21#include <state_representation/space/joint/JointAccelerations.hpp>
22#include <state_representation/space/joint/JointTorques.hpp>
23
24#include "state_representation/state_message.pb.h"
25
26using namespace state_representation;
27
28namespace clproto {
29
30DecodingException::DecodingException(const std::string& msg) : std::runtime_error(msg) {}
31
32JsonParsingException::JsonParsingException(const std::string& msg) : std::runtime_error(msg) {}
33
34bool is_valid(const std::string& msg) {
35 return check_message_type(msg) != MessageType::UNKNOWN_MESSAGE;
36}
37
38MessageType check_message_type(const std::string& msg) {
39 if (proto::StateMessage message; message.ParseFromString(msg)) {
40 return static_cast<MessageType>(message.message_type_case());
41 }
42
43 /* In theory, sending "raw" messages (message types without the
44 * StateMessage wrapper) could also be supported, though it would
45 * require manually checking each case as in the following snippet:
46
47 if (proto::State message; message.ParseFromString(msg)) {
48 return MessageType::STATE_MESSAGE;
49 } else if (proto::SpatialState message; message.ParseFromString(msg)) {
50 return MessageType::SPATIAL_STATE_MESSAGE;
51 }
52
53 * Because the intention is to encode / decode messages using
54 * this library, and all encoded messages use the StateMessage
55 * wrapper, raw messages are treated as UNKNOWN at this time.
56 */
57
58 return MessageType::UNKNOWN_MESSAGE;
59}
60
62 if (proto::StateMessage message; message.ParseFromString(msg) && message.has_parameter()) {
63 return static_cast<ParameterMessageType>(message.parameter().parameter_value().value_type_case());
64 }
65 return ParameterMessageType::UNKNOWN_PARAMETER;
66}
67
68// --- Serialization methods --- //
69
70void pack_fields(const std::vector<std::string>& fields, char* data) {
71 std::size_t index = 0;
72 field_length_t nfields;
73 field_length_t sizes[CLPROTO_PACKING_MAX_FIELDS];
74
75 // write out the number of fields
76 nfields = static_cast<field_length_t>(fields.size());
77 memcpy(data, &nfields, sizeof(field_length_t));
78 index += sizeof(field_length_t);
79
80 // write out the data size of each field
81 for (std::size_t field = 0; field < nfields; ++field) {
82 sizes[field] = static_cast<field_length_t>(fields.at(field).size());
83 }
84 memcpy(&data[index], sizes, nfields * sizeof(field_length_t));
85 index += nfields * sizeof(field_length_t);
86
87 // write out each field
88 for (std::size_t field = 0; field < nfields; ++field) {
89 memcpy(&data[index], fields.at(field).c_str(), sizes[field]);
90 index += sizes[field];
91 }
92}
93
94std::vector<std::string> unpack_fields(const char* data) {
95 std::size_t index = 0;
96 field_length_t nfields;
97 field_length_t sizes[CLPROTO_PACKING_MAX_FIELDS];
98 char field_buffer[CLPROTO_PACKING_MAX_FIELD_LENGTH];
99 std::vector<std::string> fields;
100
101 // read out the number of fields
102 memcpy(&nfields, data, sizeof(field_length_t));
103 index += sizeof(field_length_t);
104
105 // read out the data size of each field
106 memcpy(sizes, &data[index], nfields * sizeof(field_length_t));
107 index += nfields * sizeof(field_length_t);
108
109 // read out each field
110 for (std::size_t field = 0; field < nfields; ++field) {
111 memcpy(field_buffer, &data[index], sizes[field]);
112 fields.emplace_back(std::string(field_buffer, sizes[field]));
113 index += sizes[field];
114 }
115 return fields;
116}
117
118// --- JSON utilities --- //
119
120std::string to_json(const std::string& msg) {
121 std::string json;
122
123 auto resolver = std::unique_ptr<google::protobuf::util::TypeResolver>{
124 google::protobuf::util::NewTypeResolverForDescriptorPool(
125 "", google::protobuf::DescriptorPool::generated_pool())
126 };
127
128 auto status = google::protobuf::util::BinaryToJsonString(
129 resolver.get(), "/state_representation.proto.StateMessage", msg, std::addressof(json));
130
131 if (!status.ok() || json.size() <= 2) {
132 throw JsonParsingException("Could not parse the binary data into a JSON formatted state message");
133 }
134 return json;
135}
136
137std::string from_json(const std::string& json) {
138 std::string msg;
139
140 auto resolver = std::unique_ptr<google::protobuf::util::TypeResolver>{
141 google::protobuf::util::NewTypeResolverForDescriptorPool(
142 "", google::protobuf::DescriptorPool::generated_pool())
143 };
144
145 auto status = google::protobuf::util::JsonToBinaryString(
146 resolver.get(), "/state_representation.proto.StateMessage", json, std::addressof(msg));
147
148 if (!status.ok()) {
149 throw JsonParsingException("Could not parse a valid state from the JSON message: " + json);
150 }
151
152 return msg;
153}
154
155/* ----------------------
156 * State
157 * ---------------------- */
158template<>
159std::string encode<State>(const State& obj);
160template<>
161State decode(const std::string& msg);
162template<>
163bool decode(const std::string& msg, State& obj);
164template<>
165std::string encode<State>(const State& obj) {
166 proto::StateMessage message;
167 *message.mutable_state() = encoder(obj);
168 return message.SerializeAsString();
169}
170template<>
171State decode(const std::string& msg) {
172 State obj;
173 if (!decode(msg, obj)) {
174 throw DecodingException("Could not decode the message into a State");
175 }
176 return obj;
177}
178template<>
179bool decode(const std::string& msg, State& obj) {
180 try {
181 proto::StateMessage message;
182 if (!(message.ParseFromString(msg)
183 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kState)) {
184 return false;
185 }
186
187 auto state = message.state();
188 obj = State(decoder(state.type()), state.name(), state.empty());
189 std::chrono::time_point<std::chrono::steady_clock> timepoint(timestamp_duration_t(state.timestamp()));
190 obj.set_timestamp(timepoint);
191
192 return true;
193 } catch (...) {
194 return false;
195 }
196}
197
198/* ----------------------
199 * SpatialState
200 * ---------------------- */
201template<>
202std::string encode<SpatialState>(const SpatialState& obj);
203template<>
204SpatialState decode(const std::string& msg);
205template<>
206bool decode(const std::string& msg, SpatialState& obj);
207template<>
208std::string encode<SpatialState>(const SpatialState& obj) {
209 proto::StateMessage message;
210 *message.mutable_spatial_state() = encoder(obj);
211 return message.SerializeAsString();
212}
213template<>
214SpatialState decode(const std::string& msg) {
215 SpatialState obj(StateType::STATE);
216 if (!decode(msg, obj)) {
217 throw DecodingException("Could not decode the message into a SpatialState");
218 }
219 return obj;
220}
221template<>
222bool decode(const std::string& msg, SpatialState& obj) {
223 try {
224 proto::StateMessage message;
225 if (!(message.ParseFromString(msg)
226 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kSpatialState)) {
227 return false;
228 }
229
230 auto spatial_state = message.spatial_state();
231 obj = SpatialState(
232 decoder(spatial_state.state().type()), spatial_state.state().name(), spatial_state.reference_frame(),
233 spatial_state.state().empty());
234
235 return true;
236 } catch (...) {
237 return false;
238 }
239}
240
241/* ----------------------
242 * CartesianState
243 * ---------------------- */
244template<>
245std::string encode<CartesianState>(const CartesianState& obj);
246template<>
247CartesianState decode(const std::string& msg);
248template<>
249bool decode(const std::string& msg, CartesianState& obj);
250template<>
251std::string encode<CartesianState>(const CartesianState& obj) {
252 proto::StateMessage message;
253 *message.mutable_cartesian_state() = encoder(obj);
254 return message.SerializeAsString();
255}
256template<>
257CartesianState decode(const std::string& msg) {
258 CartesianState obj;
259 if (!decode(msg, obj)) {
260 throw DecodingException("Could not decode the message into a CartesianState");
261 }
262 return obj;
263}
264template<>
265bool decode(const std::string& msg, CartesianState& obj) {
266 try {
267 proto::StateMessage message;
268 if (!(message.ParseFromString(msg)
269 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kCartesianState)) {
270 return false;
271 }
272
273 auto state = message.cartesian_state();
274 obj.set_name(state.spatial_state().state().name());
275 obj.set_reference_frame(state.spatial_state().reference_frame());
276 obj.set_position(decoder(state.position()));
277 obj.set_orientation(decoder(state.orientation()));
278 obj.set_linear_velocity(decoder(state.linear_velocity()));
279 obj.set_angular_velocity(decoder(state.angular_velocity()));
280 obj.set_linear_acceleration(decoder(state.linear_acceleration()));
281 obj.set_angular_acceleration(decoder(state.angular_acceleration()));
282 obj.set_force(decoder(state.force()));
283 obj.set_torque(decoder(state.torque()));
284 obj.set_empty(state.spatial_state().state().empty());
285 return true;
286 } catch (...) {
287 return false;
288 }
289}
290
291/* ----------------------
292 * CartesianPose
293 * ---------------------- */
294template<>
295std::string encode<CartesianPose>(const CartesianPose& obj);
296template<>
297CartesianPose decode(const std::string& msg);
298template<>
299bool decode(const std::string& msg, CartesianPose& obj);
300template<>
301std::string encode<CartesianPose>(const CartesianPose& obj) {
302 proto::StateMessage message;
303 auto cartesian_state = encoder(static_cast<CartesianState>(obj));
304 *message.mutable_cartesian_pose()->mutable_spatial_state() = cartesian_state.spatial_state();
305 *message.mutable_cartesian_pose()->mutable_position() = cartesian_state.position();
306 *message.mutable_cartesian_pose()->mutable_orientation() = cartesian_state.orientation();
307 return message.SerializeAsString();
308}
309template<>
310CartesianPose decode(const std::string& msg) {
311 CartesianPose obj;
312 if (!decode(msg, obj)) {
313 throw DecodingException("Could not decode the message into a CartesianPose");
314 }
315 return obj;
316}
317template<>
318bool decode(const std::string& msg, CartesianPose& obj) {
319 try {
320 proto::StateMessage message;
321 if (!(message.ParseFromString(msg)
322 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kCartesianPose)) {
323 return false;
324 }
325 auto pose = message.cartesian_pose();
326 obj.set_name(pose.spatial_state().state().name());
327 obj.set_reference_frame(pose.spatial_state().reference_frame());
328 obj.set_position(decoder(pose.position()));
329 obj.set_orientation(decoder(pose.orientation()));
330 obj.set_empty(pose.spatial_state().state().empty());
331 return true;
332 } catch (...) {
333 return false;
334 }
335}
336
337/* ----------------------
338 * CartesianTwist
339 * ---------------------- */
340template<>
341std::string encode<CartesianTwist>(const CartesianTwist& obj);
342template<>
343CartesianTwist decode(const std::string& msg);
344template<>
345bool decode(const std::string& msg, CartesianTwist& obj);
346template<>
347std::string encode<CartesianTwist>(const CartesianTwist& obj) {
348 proto::StateMessage message;
349 auto cartesian_state = encoder(static_cast<CartesianState>(obj));
350 *message.mutable_cartesian_twist()->mutable_spatial_state() = cartesian_state.spatial_state();
351 *message.mutable_cartesian_twist()->mutable_linear_velocity() = cartesian_state.linear_velocity();
352 *message.mutable_cartesian_twist()->mutable_angular_velocity() = cartesian_state.angular_velocity();
353 return message.SerializeAsString();
354}
355template<>
356CartesianTwist decode(const std::string& msg) {
357 CartesianTwist obj;
358 if (!decode(msg, obj)) {
359 throw DecodingException("Could not decode the message into a CartesianTwist");
360 }
361 return obj;
362}
363template<>
364bool decode(const std::string& msg, CartesianTwist& obj) {
365 try {
366 proto::StateMessage message;
367 if (!(message.ParseFromString(msg)
368 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kCartesianTwist)) {
369 return false;
370 }
371 auto twist = message.cartesian_twist();
372 obj.set_name(twist.spatial_state().state().name());
373 obj.set_reference_frame(twist.spatial_state().reference_frame());
374 obj.set_linear_velocity(decoder(twist.linear_velocity()));
375 obj.set_angular_velocity(decoder(twist.angular_velocity()));
376 obj.set_empty(twist.spatial_state().state().empty());
377 return true;
378 } catch (...) {
379 return false;
380 }
381}
382
383/* ----------------------
384 * CartesianAcceleration
385 * ---------------------- */
386template<>
387std::string encode<CartesianAcceleration>(const CartesianAcceleration& obj);
388template<>
389CartesianAcceleration decode(const std::string& msg);
390template<>
391bool decode(const std::string& msg, CartesianAcceleration& obj);
392template<>
393std::string encode<CartesianAcceleration>(const CartesianAcceleration& obj) {
394 proto::StateMessage message;
395 auto cartesian_state = encoder(static_cast<CartesianState>(obj));
396 *message.mutable_cartesian_acceleration()->mutable_spatial_state() = cartesian_state.spatial_state();
397 *message.mutable_cartesian_acceleration()->mutable_linear_acceleration() = cartesian_state.linear_acceleration();
398 *message.mutable_cartesian_acceleration()->mutable_angular_acceleration() = cartesian_state.angular_acceleration();
399 return message.SerializeAsString();
400}
401template<>
402CartesianAcceleration decode(const std::string& msg) {
404 if (!decode(msg, obj)) {
405 throw DecodingException("Could not decode the message into a CartesianAcceleration");
406 }
407 return obj;
408}
409template<>
410bool decode(const std::string& msg, CartesianAcceleration& obj) {
411 try {
412 proto::StateMessage message;
413 if (!(message.ParseFromString(msg)
414 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kCartesianAcceleration)) {
415 return false;
416 }
417 auto acceleration = message.cartesian_acceleration();
418 obj.set_name(acceleration.spatial_state().state().name());
419 obj.set_reference_frame(acceleration.spatial_state().reference_frame());
420 obj.set_linear_acceleration(decoder(acceleration.linear_acceleration()));
421 obj.set_angular_acceleration(decoder(acceleration.angular_acceleration()));
422 obj.set_empty(acceleration.spatial_state().state().empty());
423 return true;
424 } catch (...) {
425 return false;
426 }
427}
428
429/* ----------------------
430 * CartesianWrench
431 * ---------------------- */
432template<>
433std::string encode<CartesianWrench>(const CartesianWrench& obj);
434template<>
435CartesianWrench decode(const std::string& msg);
436template<>
437bool decode(const std::string& msg, CartesianWrench& obj);
438template<>
439std::string encode<CartesianWrench>(const CartesianWrench& obj) {
440 proto::StateMessage message;
441 auto cartesian_state = encoder(static_cast<CartesianState>(obj));
442 *message.mutable_cartesian_wrench()->mutable_spatial_state() = cartesian_state.spatial_state();
443 *message.mutable_cartesian_wrench()->mutable_force() = cartesian_state.force();
444 *message.mutable_cartesian_wrench()->mutable_torque() = cartesian_state.torque();
445 return message.SerializeAsString();
446}
447template<>
448CartesianWrench decode(const std::string& msg) {
449 CartesianWrench obj;
450 if (!decode(msg, obj)) {
451 throw DecodingException("Could not decode the message into a CartesianWrench");
452 }
453 return obj;
454}
455template<>
456bool decode(const std::string& msg, CartesianWrench& obj) {
457 try {
458 proto::StateMessage message;
459 if (!(message.ParseFromString(msg)
460 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kCartesianWrench)) {
461 return false;
462 }
463 auto wrench = message.cartesian_wrench();
464 obj.set_name(wrench.spatial_state().state().name());
465 obj.set_reference_frame(wrench.spatial_state().reference_frame());
466 obj.set_force(decoder(wrench.force()));
467 obj.set_torque(decoder(wrench.torque()));
468 obj.set_empty(wrench.spatial_state().state().empty());
469 return true;
470 } catch (...) {
471 return false;
472 }
473}
474
475/* ----------------------
476 * Jacobian
477 * ---------------------- */
478template<>
479std::string encode<Jacobian>(const Jacobian& obj);
480template<>
481Jacobian decode(const std::string& msg);
482template<>
483bool decode(const std::string& msg, Jacobian& obj);
484template<>
485std::string encode<Jacobian>(const Jacobian& obj) {
486 proto::StateMessage message;
487 *message.mutable_jacobian() = encoder(obj);
488 return message.SerializeAsString();
489}
490template<>
491Jacobian decode(const std::string& msg) {
492 Jacobian obj;
493 if (!decode(msg, obj)) {
494 throw DecodingException("Could not decode the message into a Jacobian");
495 }
496 return obj;
497}
498template<>
499bool decode(const std::string& msg, Jacobian& obj) {
500 try {
501 proto::StateMessage message;
502 if (!(message.ParseFromString(msg)
503 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kJacobian)) {
504 return false;
505 }
506
507 auto jacobian = message.jacobian();
508 obj = Jacobian(
509 jacobian.state().name(), decoder(jacobian.joint_names()), jacobian.frame(), jacobian.reference_frame());
510 if (!jacobian.state().empty() && !jacobian.data().empty()) {
511 auto raw_data = const_cast<double*>(jacobian.data().data());
512 auto data = Eigen::Map<Eigen::MatrixXd>(raw_data, jacobian.rows(), jacobian.cols());
513 obj.set_data(data);
514 } else {
515 obj.set_empty();
516 }
517 return true;
518 } catch (...) {
519 return false;
520 }
521}
522
523/* ----------------------
524 * JointState
525 * ---------------------- */
526template<>
527std::string encode<JointState>(const JointState& obj);
528template<>
529JointState decode(const std::string& msg);
530template<>
531bool decode(const std::string& msg, JointState& obj);
532template<>
533std::string encode<JointState>(const JointState& obj) {
534 proto::StateMessage message;
535 *message.mutable_joint_state() = encoder(obj);
536 return message.SerializeAsString();
537}
538template<>
539JointState decode(const std::string& msg) {
540 JointState obj;
541 if (!decode(msg, obj)) {
542 throw DecodingException("Could not decode the message into a JointState");
543 }
544 return obj;
545}
546template<>
547bool decode(const std::string& msg, JointState& obj) {
548 try {
549 proto::StateMessage message;
550 if (!(message.ParseFromString(msg)
551 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kJointState)) {
552 return false;
553 }
554
555 auto state = message.joint_state();
556 obj = JointState(state.state().name(), decoder(state.joint_names()));
557 obj.set_positions(decoder(state.positions()));
558 obj.set_velocities(decoder(state.velocities()));
559 obj.set_accelerations(decoder(state.accelerations()));
560 obj.set_torques(decoder(state.torques()));
561 obj.set_empty(state.state().empty());
562 return true;
563 } catch (...) {
564 return false;
565 }
566}
567
568/* ----------------------
569 * JointPositions
570 * ---------------------- */
571template<>
572std::string encode<JointPositions>(const JointPositions& obj);
573template<>
574JointPositions decode(const std::string& msg);
575template<>
576bool decode(const std::string& msg, JointPositions& obj);
577template<>
578std::string encode<JointPositions>(const JointPositions& obj) {
579 proto::StateMessage message;
580 auto joint_state = encoder(static_cast<JointState>(obj));
581 *message.mutable_joint_positions()->mutable_state() = joint_state.state();
582 *message.mutable_joint_positions()->mutable_joint_names() = joint_state.joint_names();
583 *message.mutable_joint_positions()->mutable_positions() = joint_state.positions();
584 return message.SerializeAsString();
585}
586template<>
587JointPositions decode(const std::string& msg) {
588 JointPositions obj;
589 if (!decode(msg, obj)) {
590 throw DecodingException("Could not decode the message into a JointPositions");
591 }
592 return obj;
593}
594template<>
595bool decode(const std::string& msg, JointPositions& obj) {
596 try {
597 proto::StateMessage message;
598 if (!(message.ParseFromString(msg)
599 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kJointPositions)) {
600 return false;
601 }
602
603 auto state = message.joint_positions();
604 obj = JointState(state.state().name(), decoder(state.joint_names()));
605 obj.set_positions(decoder(state.positions()));
606 obj.set_empty(state.state().empty());
607 return true;
608 } catch (...) {
609 return false;
610 }
611}
612
613/* ----------------------
614 * JointVelocities
615 * ---------------------- */
616template<>
617std::string encode<JointVelocities>(const JointVelocities& obj);
618template<>
619JointVelocities decode(const std::string& msg);
620template<>
621bool decode(const std::string& msg, JointVelocities& obj);
622template<>
623std::string encode<JointVelocities>(const JointVelocities& obj) {
624 proto::StateMessage message;
625 auto joint_state = encoder(static_cast<JointState>(obj));
626 *message.mutable_joint_velocities()->mutable_state() = joint_state.state();
627 *message.mutable_joint_velocities()->mutable_joint_names() = joint_state.joint_names();
628 *message.mutable_joint_velocities()->mutable_velocities() = joint_state.velocities();
629 return message.SerializeAsString();
630}
631template<>
632JointVelocities decode(const std::string& msg) {
633 JointVelocities obj;
634 if (!decode(msg, obj)) {
635 throw DecodingException("Could not decode the message into a JointVelocities");
636 }
637 return obj;
638}
639template<>
640bool decode(const std::string& msg, JointVelocities& obj) {
641 try {
642 proto::StateMessage message;
643 if (!(message.ParseFromString(msg)
644 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kJointVelocities)) {
645 return false;
646 }
647
648 auto state = message.joint_velocities();
649 obj = JointState(state.state().name(), decoder(state.joint_names()));
650 obj.set_velocities(decoder(state.velocities()));
651 obj.set_empty(state.state().empty());
652 return true;
653 } catch (...) {
654 return false;
655 }
656}
657
658/* ----------------------
659 * JointAccelerations
660 * ---------------------- */
661template<>
662std::string encode<JointAccelerations>(const JointAccelerations& obj);
663template<>
664JointAccelerations decode(const std::string& msg);
665template<>
666bool decode(const std::string& msg, JointAccelerations& obj);
667template<>
668std::string encode<JointAccelerations>(const JointAccelerations& obj) {
669 proto::StateMessage message;
670 auto joint_state = encoder(static_cast<JointState>(obj));
671 *message.mutable_joint_accelerations()->mutable_state() = joint_state.state();
672 *message.mutable_joint_accelerations()->mutable_joint_names() = joint_state.joint_names();
673 *message.mutable_joint_accelerations()->mutable_accelerations() = joint_state.accelerations();
674 return message.SerializeAsString();
675}
676template<>
677JointAccelerations decode(const std::string& msg) {
679 if (!decode(msg, obj)) {
680 throw DecodingException("Could not decode the message into a JointAccelerations");
681 }
682 return obj;
683}
684template<>
685bool decode(const std::string& msg, JointAccelerations& obj) {
686 try {
687 proto::StateMessage message;
688 if (!(message.ParseFromString(msg)
689 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kJointAccelerations)) {
690 return false;
691 }
692
693 auto state = message.joint_accelerations();
694 obj = JointState(state.state().name(), decoder(state.joint_names()));
695 obj.set_accelerations(decoder(state.accelerations()));
696 obj.set_empty(state.state().empty());
697 return true;
698 } catch (...) {
699 return false;
700 }
701}
702
703/* ----------------------
704 * JointTorques
705 * ---------------------- */
706template<>
707std::string encode<JointTorques>(const JointTorques& obj);
708template<>
709JointTorques decode(const std::string& msg);
710template<>
711bool decode(const std::string& msg, JointTorques& obj);
712template<>
713std::string encode<JointTorques>(const JointTorques& obj) {
714 proto::StateMessage message;
715 auto joint_state = encoder(static_cast<JointState>(obj));
716 *message.mutable_joint_torques()->mutable_state() = joint_state.state();
717 *message.mutable_joint_torques()->mutable_joint_names() = joint_state.joint_names();
718 *message.mutable_joint_torques()->mutable_torques() = joint_state.torques();
719 return message.SerializeAsString();
720}
721template<>
722JointTorques decode(const std::string& msg) {
723 JointTorques obj;
724 if (!decode(msg, obj)) {
725 throw DecodingException("Could not decode the message into a JointTorques");
726 }
727 return obj;
728}
729template<>
730bool decode(const std::string& msg, JointTorques& obj) {
731 try {
732 proto::StateMessage message;
733 if (!(message.ParseFromString(msg)
734 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kJointTorques)) {
735 return false;
736 }
737
738 auto state = message.joint_torques();
739 obj = JointState(state.state().name(), decoder(state.joint_names()));
740 obj.set_torques(decoder(state.torques()));
741 obj.set_empty(state.state().empty());
742 return true;
743 } catch (...) {
744 return false;
745 }
746}
747
748/* ----------------------
749 * Parameter<T>
750 * ---------------------- */
751template<typename T>
752static std::string encode_parameter(const Parameter<T>& obj);
753template<typename T>
754static Parameter<T> decode_parameter(const std::string& msg);
755template<typename T>
756static bool decode_parameter(const std::string& msg, Parameter<T>& obj);
757
758template<typename T>
759static std::string encode_parameter(const Parameter<T>& obj) {
760 proto::StateMessage message;
761 *message.mutable_parameter() = encoder<T>(obj);
762 if (obj.is_empty()) {
763 message.mutable_parameter()->mutable_state()->set_empty(true);
764 }
765 return message.SerializeAsString();
766}
767template<typename T>
768static Parameter<T> decode_parameter(const std::string& msg) {
769 Parameter<T> obj("");
770 if (!decode(msg, obj)) {
771 throw DecodingException("Could not decode the message into a Parameter");
772 }
773 return obj;
774}
775template<typename T>
776static bool decode_parameter(const std::string& msg, Parameter<T>& obj) {
777 try {
778 proto::StateMessage message;
779 if (!(message.ParseFromString(msg)
780 && message.message_type_case() == proto::StateMessage::MessageTypeCase::kParameter)) {
781 return false;
782 }
783 obj = decoder<T>(message.parameter());
784 if (message.parameter().state().empty()) {
785 obj.set_empty();
786 }
787 return true;
788 } catch (...) {
789 return false;
790 }
791}
792
793/* ----------------------
794 * INT
795 * ---------------------- */
796template<>
797std::string encode<Parameter<int>>(const Parameter<int>& obj);
798template<>
799Parameter<int> decode(const std::string& msg);
800template<>
801bool decode(const std::string& msg, Parameter<int>& obj);
802template<>
803std::string encode<Parameter<int>>(const Parameter<int>& obj) {
804 return encode_parameter(obj);
805}
806template<>
807Parameter<int> decode(const std::string& msg) {
808 return decode_parameter<int>(msg);
809}
810template<>
811bool decode(const std::string& msg, Parameter<int>& obj) {
812 return decode_parameter(msg, obj);
813}
814
815/* ----------------------
816 * INT_ARRAY
817 * ---------------------- */
818template<>
819std::string encode<Parameter<std::vector<int>>>(const Parameter<std::vector<int>>& obj);
820template<>
821Parameter<std::vector<int>> decode(const std::string& msg);
822template<>
823bool decode(const std::string& msg, Parameter<std::vector<int>>& obj);
824template<>
825std::string encode<Parameter<std::vector<int>>>(const Parameter<std::vector<int>>& obj) {
826 return encode_parameter(obj);
827}
828template<>
829Parameter<std::vector<int>> decode(const std::string& msg) {
830 return decode_parameter<std::vector<int>>(msg);
831}
832template<>
833bool decode(const std::string& msg, Parameter<std::vector<int>>& obj) {
834 return decode_parameter(msg, obj);
835}
836
837/* ----------------------
838 * DOUBLE
839 * ---------------------- */
840template<>
841std::string encode<Parameter<double>>(const Parameter<double>& obj);
842template<>
843Parameter<double> decode(const std::string& msg);
844template<>
845bool decode(const std::string& msg, Parameter<double>& obj);
846template<>
847std::string encode<Parameter<double>>(const Parameter<double>& obj) {
848 return encode_parameter(obj);
849}
850template<>
851Parameter<double> decode(const std::string& msg) {
852 return decode_parameter<double>(msg);
853}
854template<>
855bool decode(const std::string& msg, Parameter<double>& obj) {
856 return decode_parameter(msg, obj);
857}
858
859/* ----------------------
860 * DOUBLE_ARRAY
861 * ---------------------- */
862template<>
863std::string encode<Parameter<std::vector<double>>>(const Parameter<std::vector<double>>& obj);
864template<>
865Parameter<std::vector<double>> decode(const std::string& msg);
866template<>
867bool decode(const std::string& msg, Parameter<std::vector<double>>& obj);
868template<>
869std::string encode<Parameter<std::vector<double>>>(const Parameter<std::vector<double>>& obj) {
870 return encode_parameter(obj);
871}
872template<>
873Parameter<std::vector<double>> decode(const std::string& msg) {
874 return decode_parameter<std::vector<double>>(msg);
875}
876template<>
877bool decode(const std::string& msg, Parameter<std::vector<double>>& obj) {
878 return decode_parameter(msg, obj);
879}
880
881/* ----------------------
882 * BOOL
883 * ---------------------- */
884template<>
885std::string encode<Parameter<bool>>(const Parameter<bool>& obj);
886template<>
887Parameter<bool> decode(const std::string& msg);
888template<>
889bool decode(const std::string& msg, Parameter<bool>& obj);
890template<>
891std::string encode<Parameter<bool>>(const Parameter<bool>& obj) {
892 return encode_parameter(obj);
893}
894template<>
895Parameter<bool> decode(const std::string& msg) {
896 return decode_parameter<bool>(msg);
897}
898template<>
899bool decode(const std::string& msg, Parameter<bool>& obj) {
900 return decode_parameter(msg, obj);
901}
902
903/* ----------------------
904 * BOOL_ARRAY
905 * ---------------------- */
906template<>
907std::string encode<Parameter<std::vector<bool>>>(const Parameter<std::vector<bool>>& obj);
908template<>
909Parameter<std::vector<bool>> decode(const std::string& msg);
910template<>
911bool decode(const std::string& msg, Parameter<std::vector<bool>>& obj);
912template<>
913std::string encode<Parameter<std::vector<bool>>>(const Parameter<std::vector<bool>>& obj) {
914 return encode_parameter(obj);
915}
916template<>
917Parameter<std::vector<bool>> decode(const std::string& msg) {
918 return decode_parameter<std::vector<bool>>(msg);
919}
920template<>
921bool decode(const std::string& msg, Parameter<std::vector<bool>>& obj) {
922 return decode_parameter(msg, obj);
923}
924
925/* ----------------------
926 * STRING
927 * ---------------------- */
928template<>
929std::string encode<Parameter<std::string>>(const Parameter<std::string>& obj);
930template<>
931Parameter<std::string> decode(const std::string& msg);
932template<>
933bool decode(const std::string& msg, Parameter<std::string>& obj);
934template<>
935std::string encode<Parameter<std::string>>(const Parameter<std::string>& obj) {
936 return encode_parameter(obj);
937}
938template<>
939Parameter<std::string> decode(const std::string& msg) {
940 return decode_parameter<std::string>(msg);
941}
942template<>
943bool decode(const std::string& msg, Parameter<std::string>& obj) {
944 return decode_parameter(msg, obj);
945}
946
947/* ----------------------
948 * STRING_ARRAY
949 * ---------------------- */
950template<>
951std::string encode<Parameter<std::vector<std::string>>>(const Parameter<std::vector<std::string>>& obj);
952template<>
953Parameter<std::vector<std::string>> decode(const std::string& msg);
954template<>
955bool decode(const std::string& msg, Parameter<std::vector<std::string>>& obj);
956template<>
957std::string encode<Parameter<std::vector<std::string>>>(const Parameter<std::vector<std::string>>& obj) {
958 return encode_parameter(obj);
959}
960template<>
961Parameter<std::vector<std::string>> decode(const std::string& msg) {
962 return decode_parameter<std::vector<std::string>>(msg);
963}
964template<>
965bool decode(const std::string& msg, Parameter<std::vector<std::string>>& obj) {
966 return decode_parameter(msg, obj);
967}
968
969/* ----------------------
970 * VECTOR
971 * ---------------------- */
972template<>
973std::string encode<Parameter<Eigen::VectorXd>>(const Parameter<Eigen::VectorXd>& obj);
974template<>
975Parameter<Eigen::VectorXd> decode(const std::string& msg);
976template<>
977bool decode(const std::string& msg, Parameter<Eigen::VectorXd>& obj);
978template<>
979std::string encode<Parameter<Eigen::VectorXd>>(const Parameter<Eigen::VectorXd>& obj) {
980 return encode_parameter(obj);
981}
982template<>
983Parameter<Eigen::VectorXd> decode(const std::string& msg) {
984 return decode_parameter<Eigen::VectorXd>(msg);
985}
986template<>
987bool decode(const std::string& msg, Parameter<Eigen::VectorXd>& obj) {
988 return decode_parameter(msg, obj);
989}
990
991/* ----------------------
992 * MATRIX
993 * ---------------------- */
994template<>
995std::string encode<Parameter<Eigen::MatrixXd>>(const Parameter<Eigen::MatrixXd>& obj);
996template<>
997Parameter<Eigen::MatrixXd> decode(const std::string& msg);
998template<>
999bool decode(const std::string& msg, Parameter<Eigen::MatrixXd>& obj);
1000template<>
1001std::string encode<Parameter<Eigen::MatrixXd>>(const Parameter<Eigen::MatrixXd>& obj) {
1002 return encode_parameter(obj);
1003}
1004template<>
1005Parameter<Eigen::MatrixXd> decode(const std::string& msg) {
1006 return decode_parameter<Eigen::MatrixXd>(msg);
1007}
1008template<>
1009bool decode(const std::string& msg, Parameter<Eigen::MatrixXd>& obj) {
1010 return decode_parameter(msg, obj);
1011}
1012
1013/*-----------------------
1014 * STD::SHARED_PTR<STATE>
1015 * ---------------------- */
1016template<typename T>
1017std::shared_ptr<T> safe_dynamic_pointer_cast(const std::shared_ptr<State>& state) {
1018 auto new_state = std::dynamic_pointer_cast<T>(state);
1019 if (new_state == nullptr) {
1020 throw std::invalid_argument("Dynamic pointer casting of state failed.");
1021 }
1022 return new_state;
1023}
1024
1025template<> std::string encode<std::shared_ptr<State>>(const std::shared_ptr<State>& obj);
1026template<> std::shared_ptr<State> decode(const std::string& msg);
1027template<> bool decode(const std::string& msg, std::shared_ptr<State>& obj);
1028template<> std::string encode<std::shared_ptr<State>>(const std::shared_ptr<State>& obj) {
1029 std::string message;
1030 switch (obj->get_type()) {
1031 case StateType::STATE:
1032 message = encode<State>(*obj);
1033 break;
1034 case StateType::SPATIAL_STATE:
1035 message = encode<SpatialState>(*safe_dynamic_pointer_cast<SpatialState>(obj));
1036 break;
1037 case StateType::CARTESIAN_STATE:
1038 message = encode<CartesianState>(*safe_dynamic_pointer_cast<CartesianState>(obj));
1039 break;
1040 case StateType::CARTESIAN_POSE:
1041 message = encode<CartesianPose>(*safe_dynamic_pointer_cast<CartesianPose>(obj));
1042 break;
1043 case StateType::CARTESIAN_TWIST:
1044 message = encode<CartesianTwist>(*safe_dynamic_pointer_cast<CartesianTwist>(obj));
1045 break;
1046 case StateType::CARTESIAN_ACCELERATION:
1047 message = encode<CartesianAcceleration>(*safe_dynamic_pointer_cast<CartesianAcceleration>(obj));
1048 break;
1049 case StateType::CARTESIAN_WRENCH:
1050 message = encode<CartesianWrench>(*safe_dynamic_pointer_cast<CartesianWrench>(obj));
1051 break;
1052 case StateType::JOINT_STATE:
1053 message = encode<JointState>(*safe_dynamic_pointer_cast<JointState>(obj));
1054 break;
1055 case StateType::JOINT_POSITIONS:
1056 message = encode<JointPositions>(*safe_dynamic_pointer_cast<JointPositions>(obj));
1057 break;
1058 case StateType::JOINT_VELOCITIES:
1059 message = encode<JointVelocities>(*safe_dynamic_pointer_cast<JointVelocities>(obj));
1060 break;
1061 case StateType::JOINT_ACCELERATIONS:
1062 message = encode<JointAccelerations>(*safe_dynamic_pointer_cast<JointAccelerations>(obj));
1063 break;
1064 case StateType::JOINT_TORQUES:
1065 message = encode<JointTorques>(*safe_dynamic_pointer_cast<JointTorques>(obj));
1066 break;
1067 case StateType::JACOBIAN:
1068 message = encode<Jacobian>(*safe_dynamic_pointer_cast<Jacobian>(obj));
1069 break;
1070 case StateType::PARAMETER: {
1071 auto param_ptr = safe_dynamic_pointer_cast<ParameterInterface>(obj);
1072 switch (param_ptr->get_parameter_type()) {
1073 case ParameterType::BOOL:
1074 message = encode<Parameter<bool>>(*safe_dynamic_pointer_cast<Parameter<bool>>(param_ptr));
1075 break;
1076 case ParameterType::BOOL_ARRAY:
1077 message = encode<Parameter<std::vector<bool>>>(*safe_dynamic_pointer_cast<Parameter<std::vector<bool>>>(param_ptr));
1078 break;
1079 case ParameterType::INT:
1080 message = encode<Parameter<int>>(*safe_dynamic_pointer_cast<Parameter<int>>(param_ptr));
1081 break;
1082 case ParameterType::INT_ARRAY:
1083 message = encode<Parameter<std::vector<int>>>(*safe_dynamic_pointer_cast<Parameter<std::vector<int>>>(param_ptr));
1084 break;
1085 case ParameterType::DOUBLE:
1086 message = encode<Parameter<double>>(*safe_dynamic_pointer_cast<Parameter<double>>(param_ptr));
1087 break;
1088 case ParameterType::DOUBLE_ARRAY:
1089 message = encode<Parameter<std::vector<double>>>(*safe_dynamic_pointer_cast<Parameter<std::vector<double>>>(param_ptr));
1090 break;
1091 case ParameterType::STRING:
1092 message = encode<Parameter<std::string>>(*safe_dynamic_pointer_cast<Parameter<std::string>>(param_ptr));
1093 break;
1094 case ParameterType::STRING_ARRAY:
1095 message = encode<Parameter<std::vector<std::string>>>(*safe_dynamic_pointer_cast<Parameter<std::vector<std::string>>>(param_ptr));
1096 break;
1097 case ParameterType::VECTOR:
1098 message = encode<Parameter<Eigen::VectorXd>>(*safe_dynamic_pointer_cast<Parameter<Eigen::VectorXd>>(param_ptr));
1099 break;
1100 case ParameterType::MATRIX:
1101 message = encode<Parameter<Eigen::MatrixXd>>(*safe_dynamic_pointer_cast<Parameter<Eigen::MatrixXd>>(param_ptr));
1102 break;
1103 default:
1104 throw std::invalid_argument("The ParameterType contained by parameter " + param_ptr->get_name() + " is unsupported.");
1105 break;
1106 }
1107 break;
1108 }
1109 default:
1110 throw std::invalid_argument("The StateType contained by state " + obj->get_name() + " is unsupported.");
1111 break;
1112 }
1113 return message;
1114}
1115template<> std::shared_ptr<State> decode(const std::string& msg) {
1116 std::shared_ptr<State> obj;
1117 switch (check_message_type(msg)) {
1118 case MessageType::STATE_MESSAGE:
1119 obj = make_shared_state(State());
1120 break;
1121 case MessageType::SPATIAL_STATE_MESSAGE:
1122 obj = make_shared_state(SpatialState());
1123 break;
1124 case MessageType::CARTESIAN_STATE_MESSAGE:
1125 obj = make_shared_state(CartesianState());
1126 break;
1127 case MessageType::CARTESIAN_POSE_MESSAGE:
1128 obj = make_shared_state(CartesianPose());
1129 break;
1130 case MessageType::CARTESIAN_TWIST_MESSAGE:
1131 obj = make_shared_state(CartesianTwist());
1132 break;
1133 case MessageType::CARTESIAN_ACCELERATION_MESSAGE:
1134 obj = make_shared_state(CartesianAcceleration());
1135 break;
1136 case MessageType::CARTESIAN_WRENCH_MESSAGE:
1137 obj = make_shared_state(CartesianWrench());
1138 break;
1139 case MessageType::JOINT_STATE_MESSAGE:
1140 obj = make_shared_state(JointState());
1141 break;
1142 case MessageType::JOINT_POSITIONS_MESSAGE:
1143 obj = make_shared_state(JointPositions());
1144 break;
1145 case MessageType::JOINT_VELOCITIES_MESSAGE:
1146 obj = make_shared_state(JointVelocities());
1147 break;
1148 case MessageType::JOINT_ACCELERATIONS_MESSAGE:
1149 obj = make_shared_state(JointAccelerations());
1150 break;
1151 case MessageType::JOINT_TORQUES_MESSAGE:
1152 obj = make_shared_state(JointTorques());
1153 break;
1154 case MessageType::JACOBIAN_MESSAGE:
1155 obj = make_shared_state(Jacobian());
1156 break;
1157 case MessageType::PARAMETER_MESSAGE: {
1158 switch (check_parameter_message_type(msg)) {
1159 case ParameterMessageType::BOOL:
1160 obj = make_shared_state(Parameter<bool>(""));
1161 break;
1162 case ParameterMessageType::BOOL_ARRAY:
1163 obj = make_shared_state(Parameter<std::vector<bool>>(""));
1164 break;
1165 case ParameterMessageType::INT:
1166 obj = make_shared_state(Parameter<int>(""));
1167 break;
1168 case ParameterMessageType::INT_ARRAY:
1169 obj = make_shared_state(Parameter<std::vector<int>>(""));
1170 break;
1171 case ParameterMessageType::DOUBLE:
1172 obj = make_shared_state(Parameter<double>(""));
1173 break;
1174 case ParameterMessageType::DOUBLE_ARRAY:
1175 obj = make_shared_state(Parameter<std::vector<double>>(""));
1176 break;
1177 case ParameterMessageType::STRING:
1178 obj = make_shared_state(Parameter<std::string>(""));
1179 break;
1180 case ParameterMessageType::STRING_ARRAY:
1181 obj = make_shared_state(Parameter<std::vector<std::string>>(""));
1182 break;
1183 case ParameterMessageType::VECTOR:
1184 obj = make_shared_state(Parameter<Eigen::VectorXd>(""));
1185 break;
1186 case ParameterMessageType::MATRIX:
1187 obj = make_shared_state(Parameter<Eigen::MatrixXd>(""));
1188 break;
1189 default:
1190 throw std::invalid_argument("The ParameterMessageType contained by this message is unsupported.");
1191 break;
1192 }
1193 break;
1194 }
1195 default:
1196 throw std::invalid_argument("The MessageType contained by this message is unsupported.");
1197 break;
1198 }
1199 if (!decode(msg, obj)) {
1200 throw DecodingException("Could not decode the message into a std::shared_ptr<State>");
1201 }
1202 return obj;
1203}
1204template<> bool decode(const std::string& msg, std::shared_ptr<State>& obj) {
1205 try {
1206 switch (obj->get_type()) {
1207 case StateType::STATE:
1208 obj = make_shared_state(decode<State>(msg));
1209 break;
1210 case StateType::SPATIAL_STATE:
1211 obj = make_shared_state(decode<SpatialState>(msg));
1212 break;
1213 case StateType::CARTESIAN_STATE:
1214 obj = make_shared_state(decode<CartesianState>(msg));
1215 break;
1216 case StateType::CARTESIAN_POSE:
1217 obj = make_shared_state(decode<CartesianPose>(msg));
1218 break;
1219 case StateType::CARTESIAN_TWIST:
1220 obj = make_shared_state(decode<CartesianTwist>(msg));
1221 break;
1222 case StateType::CARTESIAN_ACCELERATION:
1223 obj = make_shared_state(decode<CartesianAcceleration>(msg));
1224 break;
1225 case StateType::CARTESIAN_WRENCH:
1226 obj = make_shared_state(decode<CartesianWrench>(msg));
1227 break;
1228 case StateType::JOINT_STATE:
1229 obj = make_shared_state(decode<JointState>(msg));
1230 break;
1231 case StateType::JOINT_POSITIONS:
1232 obj = make_shared_state(decode<JointPositions>(msg));
1233 break;
1234 case StateType::JOINT_VELOCITIES:
1235 obj = make_shared_state(decode<JointVelocities>(msg));
1236 break;
1237 case StateType::JOINT_ACCELERATIONS:
1238 obj = make_shared_state(decode<JointAccelerations>(msg));
1239 break;
1240 case StateType::JOINT_TORQUES:
1241 obj = make_shared_state(decode<JointTorques>(msg));
1242 break;
1243 case StateType::JACOBIAN:
1244 obj = make_shared_state(decode<Jacobian>(msg));
1245 break;
1246 case StateType::PARAMETER: {
1247 auto param_ptr = safe_dynamic_pointer_cast<ParameterInterface>(obj);
1248 switch (param_ptr->get_parameter_type()) {
1249 case ParameterType::BOOL:
1250 obj = make_shared_state(decode<Parameter<bool>>(msg));
1251 break;
1252 case ParameterType::BOOL_ARRAY:
1253 obj = make_shared_state(decode<Parameter<std::vector<bool>>>(msg));
1254 break;
1255 case ParameterType::INT:
1256 obj = make_shared_state(decode<Parameter<int>>(msg));
1257 break;
1258 case ParameterType::INT_ARRAY:
1259 obj = make_shared_state(decode<Parameter<std::vector<int>>>(msg));
1260 break;
1261 case ParameterType::DOUBLE:
1262 obj = make_shared_state(decode<Parameter<double>>(msg));
1263 break;
1264 case ParameterType::DOUBLE_ARRAY:
1265 obj = make_shared_state(decode<Parameter<std::vector<double>>>(msg));
1266 break;
1267 case ParameterType::STRING:
1268 obj = make_shared_state(decode<Parameter<std::string>>(msg));
1269 break;
1270 case ParameterType::STRING_ARRAY:
1271 obj = make_shared_state(decode<Parameter<std::vector<std::string>>>(msg));
1272 break;
1273 case ParameterType::VECTOR:
1274 obj = make_shared_state(decode<Parameter<Eigen::VectorXd>>(msg));
1275 break;
1276 case ParameterType::MATRIX:
1277 obj = make_shared_state(decode<Parameter<Eigen::MatrixXd>>(msg));
1278 break;
1279 default:
1280 throw std::invalid_argument("The ParameterType contained by parameter " + param_ptr->get_name() + " is unsupported.");
1281 break;
1282 }
1283 break;
1284 }
1285 default:
1286 throw std::invalid_argument("The StateType contained by state " + obj->get_name() + " is unsupported.");
1287 break;
1288 }
1289 return true;
1290 } catch (...) {
1291 return false;
1292 }
1293}
1294
1295// Generic template code for future types:
1296/* ----------------------
1297 * __TYPE__
1298 * ---------------------- */ /*
1299template<> std::string encode<__TYPE__>(const __TYPE__& obj);
1300template<> __TYPE__ decode(const std::string& msg);
1301template<> bool decode(const std::string& msg, __TYPE__& obj);
1302template<> std::string encode<__TYPE__>(const __TYPE__& obj) {
1303 proto::StateMessage message;
1304 // encode
1305 return message.SerializeAsString();
1306}
1307template<> __TYPE__ decode(const std::string& msg) {
1308 __TYPE__ obj;
1309 if (!decode(msg, obj)) {
1310 throw DecodingException("Could not decode the message into a __TYPE__");
1311 }
1312 return obj;
1313}
1314template<> bool decode(const std::string& msg, __TYPE__& obj) {
1315 try {
1316 proto::StateMessage message;
1317 if (!(message.ParseFromString(msg) && message.message_type_case() == proto::StateMessage::MessageTypeCase::k__TYPE__)) {
1318 return false;
1319 }
1320 // decode
1321 return true;
1322 } catch (...) {
1323 return false;
1324 }
1325}
1326*/
1327
1328/* ----------------------
1329 * Parameter<ParamT>
1330 * ---------------------- */ /*
1331template<>
1332std::string encode<Parameter<ParamT>>(const Parameter<ParamT>& obj);
1333template<>
1334Parameter<ParamT> decode(const std::string& msg);
1335template<>
1336bool decode(const std::string& msg, Parameter<ParamT>& obj);
1337template<>
1338std::string encode<Parameter<ParamT>>(const Parameter<ParamT>& obj) {
1339 return encode_parameter(obj);
1340}
1341template<>
1342Parameter<ParamT> decode(const std::string& msg) {
1343 return decode_parameter<ParamT>(msg);
1344}
1345template<>
1346bool decode(const std::string& msg, Parameter<ParamT>& obj) {
1347 return decode_parameter(msg, obj);
1348}
1349*/
1350
1351}
A JsonParsingException is raised whenever a JSON conversion operation fails due to invalid encoding.
Definition: clproto.h:48
Class to define acceleration in cartesian space as 3D linear and angular acceleration vectors.
Class to define CartesianPose in cartesian space as 3D position and quaternion based orientation.
Class to represent a state in Cartesian space.
void set_orientation(const Eigen::Quaterniond &orientation)
Setter of the orientation.
void set_angular_acceleration(const Eigen::Vector3d &angular_acceleration)
Setter of the angular velocity attribute.
void set_force(const Eigen::Vector3d &force)
Setter of the force attribute.
void set_linear_acceleration(const Eigen::Vector3d &linear_acceleration)
Setter of the linear acceleration attribute.
void set_position(const Eigen::Vector3d &position)
Setter of the position.
void set_torque(const Eigen::Vector3d &torque)
Setter of the torque attribute.
void set_linear_velocity(const Eigen::Vector3d &linear_velocity)
Setter of the linear velocity attribute.
void set_angular_velocity(const Eigen::Vector3d &angular_velocity)
Setter of the angular velocity attribute.
Class to define twist in cartesian space as 3D linear and angular velocity vectors.
Class to define wrench in cartesian space as 3D force and torque vectors.
Class to define a robot Jacobian matrix.
Definition: Jacobian.hpp:20
void set_data(const Eigen::MatrixXd &data) override
Setter of the data attribute.
Definition: Jacobian.hpp:396
Class to define accelerations of the joints.
Class to define a positions of the joints.
Class to define a state in joint space.
Definition: JointState.hpp:36
void set_accelerations(const Eigen::VectorXd &accelerations)
Setter of the accelerations attribute.
Definition: JointState.cpp:163
void set_positions(const Eigen::VectorXd &positions)
Setter of the positions attribute.
Definition: JointState.cpp:99
void set_torques(const Eigen::VectorXd &torques)
Setter of the torques attribute.
Definition: JointState.cpp:195
void set_velocities(const Eigen::VectorXd &velocities)
Setter of the velocities attribute.
Definition: JointState.cpp:131
Class to define torques of the joints.
Class to define velocities of the joints.
virtual void set_reference_frame(const std::string &reference_frame)
Setter of the reference frame.
Abstract class to represent a state.
Definition: State.hpp:25
const std::string & get_name() const
Getter of the name as const reference.
Definition: State.cpp:48
virtual void set_name(const std::string &name)
Setter of the name.
Definition: State.cpp:52
void set_timestamp(const std::chrono::time_point< std::chrono::steady_clock > &timepoint)
Setter of the timestamp attribute.
Definition: State.cpp:40
const StateType & get_type() const
Getter of the type attribute.
Definition: State.cpp:19
bool is_empty() const
Getter of the empty attribute.
Definition: State.cpp:23
void set_empty(bool empty=true)
Setter of the empty attribute.
Definition: State.cpp:27
Bindings to encode and decode state objects into serialised binary message.
std::chrono::nanoseconds timestamp_duration_t
Duration type to use when representing chrono timestamps as integer count since epoch.
Definition: clproto.h:31
ParameterMessageType check_parameter_message_type(const std::string &msg)
Check which control libraries parameter type a serialized binary string can be decoded as,...
Definition: clproto.cpp:61
uint32_t field_length_t
Size type used to indicate number of fields and field data length in pack_fields() and unpack_fields(...
Definition: clproto.h:24
state_representation::proto::Parameter encoder(const state_representation::Parameter< ParamT > &parameter)
Encoding helper method for the Parameter type.
std::string from_json(const std::string &json)
Convert a JSON formatted state message description into a serialized binary string representation (wi...
Definition: clproto.cpp:137
MessageType check_message_type(const std::string &msg)
Check which control libraries message type a serialized binary string can be decoded as,...
Definition: clproto.cpp:38
T decode(const std::string &msg)
Decode a serialized binary string from wire format into a control libraries object instance.
MessageType
The MessageType enumeration contains the possible message types in the clproto.
Definition: clproto.h:62
std::vector< FieldT > decoder(const google::protobuf::RepeatedField< FieldT > &message)
Decoding helper method for a RepeatedField message into vector data.
Definition: decoders.h:53
std::string to_json(const std::string &msg)
Convert a serialized binary string from wire format into a JSON formatted state message description.
Definition: clproto.cpp:120
ParameterMessageType
The ParameterMessageType enumeration contains the possible value types contained in a parameter messa...
Definition: clproto.h:91
void pack_fields(const std::vector< std::string > &fields, char *data)
Pack an ordered vector of encoded field messages into a single data array.
Definition: clproto.cpp:70
std::vector< std::string > unpack_fields(const char *data)
Unpack a data array into an ordered vector of encoded field messages.
Definition: clproto.cpp:94
bool is_valid(const std::string &msg)
Check if a serialized binary string can be decoded into a support control libraries message type.
Definition: clproto.cpp:34
Core state variables and objects.