1#include "clproto_bindings.hpp"
2#include "parameter_container.hpp"
6#include <state_representation/State.hpp>
7#include <state_representation/space/SpatialState.hpp>
8#include <state_representation/space/cartesian/CartesianState.hpp>
9#include <state_representation/space/cartesian/CartesianPose.hpp>
10#include <state_representation/space/cartesian/CartesianTwist.hpp>
11#include <state_representation/space/cartesian/CartesianWrench.hpp>
12#include <state_representation/space/Jacobian.hpp>
13#include <state_representation/space/joint/JointState.hpp>
14#include <state_representation/space/joint/JointPositions.hpp>
15#include <state_representation/space/joint/JointVelocities.hpp>
16#include <state_representation/space/joint/JointAccelerations.hpp>
17#include <state_representation/space/joint/JointTorques.hpp>
18#include <state_representation/geometry/Shape.hpp>
19#include <state_representation/geometry/Ellipsoid.hpp>
20#include <state_representation/DigitalIOState.hpp>
21#include <state_representation/AnalogIOState.hpp>
25using namespace py_parameter;
28inline py::bytes encode_bytes(
const T&
object) {
29 return py::bytes(
encode(
object));
34 case ParameterType::INT:
36 case ParameterType::INT_ARRAY:
37 return encode_bytes(container_to_parameter<std::vector<int>>(
container));
38 case ParameterType::DOUBLE:
40 case ParameterType::DOUBLE_ARRAY:
41 return encode_bytes(container_to_parameter<std::vector<double>>(
container));
42 case ParameterType::BOOL:
44 case ParameterType::BOOL_ARRAY:
45 return encode_bytes(container_to_parameter<std::vector<bool>>(
container));
46 case ParameterType::STRING:
48 case ParameterType::STRING_ARRAY:
49 return encode_bytes(container_to_parameter<std::vector<std::string>>(
container));
50 case ParameterType::MATRIX:
52 case ParameterType::VECTOR:
55 throw std::invalid_argument(
"This StateType is not a valid Parameter.");
61inline py::object message_to_parameter(
const std::string&
msg) {
62 py::object
PyParameter = py::module_::import(
"state_representation").attr(
"Parameter");
64 if (
param.is_empty()) {
71py::object decode_parameter(
const std::string&
msg) {
73 case ParameterMessageType::INT: {
76 case ParameterMessageType::INT_ARRAY: {
79 case ParameterMessageType::DOUBLE: {
82 case ParameterMessageType::DOUBLE_ARRAY: {
85 case ParameterMessageType::BOOL: {
88 case ParameterMessageType::BOOL_ARRAY: {
91 case ParameterMessageType::STRING: {
94 case ParameterMessageType::STRING_ARRAY: {
97 case ParameterMessageType::VECTOR: {
100 case ParameterMessageType::MATRIX: {
104 throw std::invalid_argument(
"The message is not a valid encoded Parameter.");
109void parameter_message_type(py::module_&
m) {
110 py::enum_<ParameterMessageType>(
m,
"ParameterMessageType")
111 .value(
"UNKNOWN_PARAMETER", ParameterMessageType::UNKNOWN_PARAMETER)
112 .value(
"INT", ParameterMessageType::INT)
113 .value(
"INT_ARRAY", ParameterMessageType::INT_ARRAY)
114 .value(
"DOUBLE", ParameterMessageType::DOUBLE)
115 .value(
"DOUBLE_ARRAY", ParameterMessageType::DOUBLE_ARRAY)
116 .value(
"BOOL", ParameterMessageType::BOOL)
117 .value(
"BOOL_ARRAY", ParameterMessageType::BOOL_ARRAY)
118 .value(
"STRING", ParameterMessageType::STRING)
119 .value(
"STRING_ARRAY", ParameterMessageType::STRING_ARRAY)
120 .value(
"MATRIX", ParameterMessageType::MATRIX)
121 .value(
"VECTOR", ParameterMessageType::VECTOR)
125void message_type(py::module_&
m) {
126 py::enum_<MessageType>(
m,
"MessageType")
127 .value(
"UNKNOWN_MESSAGE", MessageType::UNKNOWN_MESSAGE)
128 .value(
"STATE_MESSAGE", MessageType::STATE_MESSAGE)
129 .value(
"SPATIAL_STATE_MESSAGE", MessageType::SPATIAL_STATE_MESSAGE)
130 .value(
"CARTESIAN_STATE_MESSAGE", MessageType::CARTESIAN_STATE_MESSAGE)
131 .value(
"CARTESIAN_POSE_MESSAGE", MessageType::CARTESIAN_POSE_MESSAGE)
132 .value(
"CARTESIAN_TWIST_MESSAGE", MessageType::CARTESIAN_TWIST_MESSAGE)
133 .value(
"CARTESIAN_ACCELERATION_MESSAGE", MessageType::CARTESIAN_ACCELERATION_MESSAGE)
134 .value(
"CARTESIAN_WRENCH_MESSAGE", MessageType::CARTESIAN_WRENCH_MESSAGE)
135 .value(
"JACOBIAN_MESSAGE", MessageType::JACOBIAN_MESSAGE)
136 .value(
"JOINT_STATE_MESSAGE", MessageType::JOINT_STATE_MESSAGE)
137 .value(
"JOINT_POSITIONS_MESSAGE", MessageType::JOINT_POSITIONS_MESSAGE)
138 .value(
"JOINT_VELOCITIES_MESSAGE", MessageType::JOINT_VELOCITIES_MESSAGE)
139 .value(
"JOINT_ACCELERATIONS_MESSAGE", MessageType::JOINT_ACCELERATIONS_MESSAGE)
140 .value(
"JOINT_TORQUES_MESSAGE", MessageType::JOINT_TORQUES_MESSAGE)
141 .value(
"SHAPE_MESSAGE", MessageType::SHAPE_MESSAGE)
142 .value(
"ELLIPSOID_MESSAGE", MessageType::ELLIPSOID_MESSAGE)
143 .value(
"PARAMETER_MESSAGE", MessageType::PARAMETER_MESSAGE)
144 .value(
"DIGITAL_IO_STATE_MESSAGE", MessageType::DIGITAL_IO_STATE_MESSAGE)
145 .value(
"ANALOG_IO_STATE_MESSAGE", MessageType::ANALOG_IO_STATE_MESSAGE)
149void methods(py::module_&
m) {
150 m.def(
"is_valid", &
is_valid,
"Check if a serialized binary string can be decoded into a support control libraries message type.",
"msg"_a);
151 m.def(
"check_message_type", &
check_message_type,
"Check which control libraries message type a serialized binary string can be decoded as, if at all.",
"msg"_a);
152 m.def(
"check_parameter_message_type", &
check_parameter_message_type,
"Check which control libraries message type a serialized binary string can be decoded as, if at all.",
"msg"_a);
154 m.def(
"pack_fields", [](
const std::vector<std::string>&
encoded_fields) -> py::bytes {
155 char data[CLPROTO_PACKING_MAX_FIELDS * CLPROTO_PACKING_MAX_FIELD_LENGTH];
162 },
"Pack an ordered vector of encoded field messages into a single data array.",
"encoded_fields"_a);
163 m.def(
"unpack_fields", [](
const std::string&
packet) -> std::vector<py::bytes> {
169 },
"Unpack a data array into an ordered vector of encoded field messages.",
"data"_a);
171 m.def(
"encode", [](
const py::object&
object,
const MessageType&
type) -> py::bytes {
174 case MessageType::STATE_MESSAGE:
176 case MessageType::DIGITAL_IO_STATE_MESSAGE:
178 case MessageType::ANALOG_IO_STATE_MESSAGE:
180 case MessageType::SPATIAL_STATE_MESSAGE:
182 case MessageType::CARTESIAN_STATE_MESSAGE:
184 case MessageType::CARTESIAN_POSE_MESSAGE:
186 case MessageType::CARTESIAN_TWIST_MESSAGE:
188 case MessageType::CARTESIAN_ACCELERATION_MESSAGE:
190 case MessageType::CARTESIAN_WRENCH_MESSAGE:
192 case MessageType::JACOBIAN_MESSAGE:
194 case MessageType::JOINT_STATE_MESSAGE:
196 case MessageType::JOINT_POSITIONS_MESSAGE:
198 case MessageType::JOINT_VELOCITIES_MESSAGE:
200 case MessageType::JOINT_ACCELERATIONS_MESSAGE:
202 case MessageType::JOINT_TORQUES_MESSAGE:
204 case MessageType::PARAMETER_MESSAGE:
207 throw std::invalid_argument(
"The message is not a valid encoded StateMessage.");
209 }
catch (
const std::exception&
ex) {
212 },
"Encode a control libraries object into a serialized binary string representation (wire format).", py::arg(
"object"), py::arg(
"type"));
214 m.def(
"decode", [](
const std::string&
msg) -> py::object {
217 case MessageType::STATE_MESSAGE:
219 case MessageType::DIGITAL_IO_STATE_MESSAGE:
221 case MessageType::ANALOG_IO_STATE_MESSAGE:
223 case MessageType::SPATIAL_STATE_MESSAGE:
225 case MessageType::CARTESIAN_STATE_MESSAGE:
227 case MessageType::CARTESIAN_POSE_MESSAGE:
229 case MessageType::CARTESIAN_TWIST_MESSAGE:
231 case MessageType::CARTESIAN_ACCELERATION_MESSAGE:
233 case MessageType::CARTESIAN_WRENCH_MESSAGE:
235 case MessageType::JACOBIAN_MESSAGE:
237 case MessageType::JOINT_STATE_MESSAGE:
239 case MessageType::JOINT_POSITIONS_MESSAGE:
241 case MessageType::JOINT_VELOCITIES_MESSAGE:
243 case MessageType::JOINT_ACCELERATIONS_MESSAGE:
245 case MessageType::JOINT_TORQUES_MESSAGE:
247 case MessageType::PARAMETER_MESSAGE:
248 return decode_parameter(
msg);
250 throw std::invalid_argument(
"Decoding not possible: Unknown or unsupported message type");
252 }
catch (
const std::exception&
ex) {
255 },
"Decode a serialized binary string from wire format into a control libraries object instance.",
"msg"_a);
257 m.def(
"to_json", [](
const std::string&
msg) {
return to_json(
msg); },
"Convert a serialized binary string from wire format into a JSON formatted state message description",
"msg"_a);
258 m.def(
"from_json", [](
const std::string&
json) -> py::bytes {
return from_json(
json); },
"Convert a JSON formatted state message description into a serialized binary string representation (wire format).",
"msg"_a);
261void bind_clproto(py::module_&
m) {
262 py::register_exception<clproto::JsonParsingException>(
m,
"JsonParsingError",
PyExc_RuntimeError);
263 py::register_exception<clproto::DecodingException>(
m,
"DecodingError",
PyExc_RuntimeError);
266 parameter_message_type(
m);
A DecodingException is raised whenever a decoding operation fails due to invalid encoding.
Bindings to encode and decode state objects into serialised binary message.
ParameterMessageType check_parameter_message_type(const std::string &msg)
Check which control libraries parameter type a serialized binary string can be decoded as,...
uint32_t field_length_t
Size type used to indicate number of fields and field data length in pack_fields() and unpack_fields(...
std::string from_json(const std::string &json)
Convert a JSON formatted state message description into a serialized binary string representation (wi...
MessageType check_message_type(const std::string &msg)
Check which control libraries message type a serialized binary string can be decoded as,...
MessageType
The MessageType enumeration contains the possible message types in the clproto.
std::string to_json(const std::string &msg)
Convert a serialized binary string from wire format into a JSON formatted state message description.
void pack_fields(const std::vector< std::string > &fields, char *data)
Pack an ordered vector of encoded field messages into a single data array.
std::string encode(const T &obj)
Encode a control libraries object into a serialized binary string representation (wire format).
std::vector< std::string > unpack_fields(const char *data)
Unpack a data array into an ordered vector of encoded field messages.
bool is_valid(const std::string &msg)
Check if a serialized binary string can be decoded into a support control libraries message type.
Core state variables and objects.