json_conversions.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Roboception GmbH
3  *
4  * Author: Felix Ruess
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its contributors
17  * may be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef RC_PICK_CLIENT_JSON_CONVERSIONS_H
34 #define RC_PICK_CLIENT_JSON_CONVERSIONS_H
35 
37 
38 #include <rc_common_msgs/ReturnCode.h>
39 
40 #include <rc_pick_client/Box.h>
41 #include <rc_pick_client/Item.h>
42 #include <rc_pick_client/ItemModel.h>
43 #include <rc_pick_client/LoadCarrier.h>
44 #include <rc_pick_client/LoadCarrierWithFillingLevel.h>
45 #include <rc_pick_client/SuctionGrasp.h>
46 #include <rc_pick_client/RegionOfInterest.h>
47 #include <rc_pick_client/Compartment.h>
48 #include <rc_pick_client/CellFillingLevel.h>
49 #include <rc_pick_client/GridSize.h>
50 #include <rc_pick_client/RangeValue.h>
51 
52 #include <rc_pick_client/DetectLoadCarriers.h>
53 #include <rc_pick_client/DeleteLoadCarriers.h>
54 #include <rc_pick_client/DeleteRegionsOfInterest.h>
55 #include <rc_pick_client/GetLoadCarriers.h>
56 #include <rc_pick_client/GetRegionsOfInterest.h>
57 #include <rc_pick_client/SetLoadCarrier.h>
58 #include <rc_pick_client/SetRegionOfInterest.h>
59 #include <rc_pick_client/ComputeGrasps.h>
60 #include <rc_pick_client/ComputeBoxGrasps.h>
61 #include <rc_pick_client/DetectItems.h>
62 #include <rc_pick_client/DetectFillingLevel.h>
63 
64 namespace rc_common_msgs
65 {
66 inline void from_json(const nlohmann::json& j, ReturnCode& r)
67 {
68  j.at("value").get_to(r.value);
69  j.at("message").get_to(r.message);
70 }
71 
72 } // namespace rc_common_msgs
73 
74 namespace rc_pick_client
75 {
76 inline void to_json(nlohmann::json& j, const Box& r)
77 {
78  j["x"] = r.x;
79  j["y"] = r.y;
80  j["z"] = r.z;
81 }
82 
83 inline void from_json(const nlohmann::json& j, Box& r)
84 {
85  j.at("x").get_to(r.x);
86  j.at("y").get_to(r.y);
87  j.at("z").get_to(r.z);
88 }
89 
90 inline void to_json(nlohmann::json& j, const RangeBox& r)
91 {
92  j["min_dimensions"] = r.min_dimensions;
93  j["max_dimensions"] = r.max_dimensions;
94 }
95 
96 inline void from_json(const nlohmann::json& j, RangeBox& r)
97 {
98  j.at("min_dimensions").get_to(r.min_dimensions);
99  j.at("max_dimensions").get_to(r.max_dimensions);
100 }
101 
102 inline void to_json(nlohmann::json& j, const Rectangle& r)
103 {
104  j["x"] = r.x;
105  j["y"] = r.y;
106 }
107 
108 inline void from_json(const nlohmann::json& j, Rectangle& r)
109 {
110  j.at("x").get_to(r.x);
111  j.at("y").get_to(r.y);
112 }
113 
114 inline void to_json(nlohmann::json& j, const RangeRectangle& r)
115 {
116  j["min_dimensions"] = r.min_dimensions;
117  j["max_dimensions"] = r.max_dimensions;
118 }
119 
120 inline void from_json(const nlohmann::json& j, RangeRectangle& r)
121 {
122  j.at("min_dimensions").get_to(r.min_dimensions);
123  j.at("max_dimensions").get_to(r.max_dimensions);
124 }
125 
126 inline void to_json(nlohmann::json& j, const GridSize& r)
127 {
128  j["x"] = r.x;
129  j["y"] = r.y;
130 }
131 
132 inline void from_json(const nlohmann::json& j, GridSize& r)
133 {
134  j.at("x").get_to(r.x);
135  j.at("y").get_to(r.y);
136 }
137 
138 inline void to_json(nlohmann::json& j, const RangeValue& r)
139 {
140  j["min"] = r.min;
141  j["max"] = r.max;
142  j["mean"] = r.mean;
143 }
144 
145 inline void from_json(const nlohmann::json& j, RangeValue& r)
146 {
147  j.at("min").get_to(r.min);
148  j.at("max").get_to(r.max);
149  j.at("mean").get_to(r.mean);
150 }
151 
152 inline void to_json(nlohmann::json& j, const CellFillingLevel& r)
153 {
154  j["cell_size"] = r.cell_size;
155  j["cell_position"] = r.cell_position;
156  j["level_in_percent"] = r.level_in_percent;
157  j["level_free_in_meters"] = r.level_free_in_meters;
158  j["coverage"] = r.coverage;
159 }
160 
161 inline void from_json(const nlohmann::json& j, CellFillingLevel& r)
162 {
163  j.at("cell_size").get_to(r.cell_size);
164  j.at("cell_position").get_to(r.cell_position);
165  j.at("level_in_percent").get_to(r.level_in_percent);
166  j.at("level_free_in_meters").get_to(r.level_free_in_meters);
167  j.at("coverage").get_to(r.coverage);
168 }
169 
170 inline void from_json(const nlohmann::json& j, SuctionGrasp& r)
171 {
172  j.at("uuid").get_to(r.uuid);
173  j.at("item_uuid").get_to(r.item_uuid);
174  j.at("pose").get_to(r.pose.pose);
175  j.at("timestamp").get_to(r.pose.header.stamp);
176  j.at("pose_frame").get_to(r.pose.header.frame_id);
177  j.at("quality").get_to(r.quality);
178  j.at("max_suction_surface_length").get_to(r.max_suction_surface_length);
179  j.at("max_suction_surface_width").get_to(r.max_suction_surface_width);
180 }
181 
182 inline void to_json(nlohmann::json& j, const ItemModel& r)
183 {
184  j["type"] = r.type;
185  if (r.type == r.UNKNOWN)
186  {
187  j["unknown"] = r.unknown;
188  }
189  else if (r.type == r.RECTANGLE)
190  {
191  j["rectangle"] = r.rectangle;
192  }
193 }
194 
195 inline void from_json(const nlohmann::json& j, Item& r)
196 {
197  j.at("uuid").get_to(r.uuid);
198  j.at("grasp_uuids").get_to(r.grasp_uuids);
199  j.at("type").get_to(r.type);
200  j.at("rectangle").get_to(r.rectangle);
201  j.at("pose").get_to(r.pose.pose);
202  j.at("timestamp").get_to(r.pose.header.stamp);
203  j.at("pose_frame").get_to(r.pose.header.frame_id);;
204 }
205 
206 inline void to_json(nlohmann::json& j, const LoadCarrier& r)
207 {
208  j["id"] = r.id;
209  j["outer_dimensions"] = r.outer_dimensions;
210  j["inner_dimensions"] = r.inner_dimensions;
211  j["rim_thickness"] = r.rim_thickness;
212  j["pose"] = r.pose.pose;
213  j["pose_frame"] = r.pose.header.frame_id;
214  // timestamp and overfilled flag is not used when setting load carrier
215 }
216 
217 inline void from_json(const nlohmann::json& j, LoadCarrier& r)
218 {
219  j.at("id").get_to(r.id);
220  j.at("outer_dimensions").get_to(r.outer_dimensions);
221  j.at("inner_dimensions").get_to(r.inner_dimensions);
222  j.at("rim_thickness").get_to(r.rim_thickness);
223  j.at("pose").get_to(r.pose.pose);
224  j.at("pose_frame").get_to(r.pose.header.frame_id);
225  // timestamp is set from enclosing msg
226  if (j.count("overfilled"))
227  j.at("overfilled").get_to(r.overfilled);
228 }
229 
230 inline void from_json(const nlohmann::json& j, LoadCarrierWithFillingLevel& r)
231 {
232  j.at("id").get_to(r.id);
233  j.at("outer_dimensions").get_to(r.outer_dimensions);
234  j.at("inner_dimensions").get_to(r.inner_dimensions);
235  j.at("rim_thickness").get_to(r.rim_thickness);
236  j.at("pose").get_to(r.pose.pose);
237  j.at("pose_frame").get_to(r.pose.header.frame_id);
238  // timestamp is set from enclosing msg
239  j.at("overfilled").get_to(r.overfilled);
240  j.at("overall_filling_level").get_to(r.overall_filling_level);
241  j.at("cells_filling_levels").get_to(r.cells_filling_levels);
242  j.at("filling_level_cell_count").get_to(r.filling_level_cell_count);
243 }
244 
245 inline void to_json(nlohmann::json& j, const Compartment& r)
246 {
247  j["pose"] = r.pose;
248  j["box"] = r.box;
249 }
250 
251 inline void to_json(nlohmann::json& j, const RegionOfInterest& r)
252 {
253  j["id"] = r.id;
254  if (r.primitive.type == shape_msgs::SolidPrimitive::BOX)
255  {
256  j["type"] = "BOX";
257  if (r.primitive.dimensions.size() != 3)
258  {
259  throw std::runtime_error("Solid primitive of type \"box\" needs 3 dimensions.");
260  }
261  j["box"]["x"] = r.primitive.dimensions[shape_msgs::SolidPrimitive::BOX_X];
262  j["box"]["y"] = r.primitive.dimensions[shape_msgs::SolidPrimitive::BOX_Y];
263  j["box"]["z"] = r.primitive.dimensions[shape_msgs::SolidPrimitive::BOX_Z];
264  }
265  else if (r.primitive.type == shape_msgs::SolidPrimitive::SPHERE)
266  {
267  j["type"] = "SPHERE";
268  if (r.primitive.dimensions.size() != 1)
269  {
270  throw std::runtime_error("Solid primitive of type \"sphere\" needs 1 dimension.");
271  }
272  j["sphere"]["radius"] = r.primitive.dimensions[shape_msgs::SolidPrimitive::BOX_X];
273  }
274  else
275  {
276  throw std::runtime_error("Type of solid primitive has to be of type \"box\" or \"sphere\"");
277  }
278  j["pose"] = r.pose.pose;
279  j["pose_frame"] = r.pose.header.frame_id;
280 }
281 
282 inline void from_json(const nlohmann::json& j, RegionOfInterest& r)
283 {
284  j.at("id").get_to(r.id);
285  if (j.at("type") == "BOX")
286  {
287  r.primitive.type = shape_msgs::SolidPrimitive::BOX;
288  r.primitive.dimensions.resize(3);
289  r.primitive.dimensions[shape_msgs::SolidPrimitive::BOX_X] = j.at("box").at("x");
290  r.primitive.dimensions[shape_msgs::SolidPrimitive::BOX_Y] = j.at("box").at("y");
291  r.primitive.dimensions[shape_msgs::SolidPrimitive::BOX_Z] = j.at("box").at("z");
292  }
293  else if (j.at("type") == "SPHERE")
294  {
295  r.primitive.type = shape_msgs::SolidPrimitive::SPHERE;
296  r.primitive.dimensions.resize(1);
297  r.primitive.dimensions[shape_msgs::SolidPrimitive::BOX_X] = j.at("sphere").at("radius");
298  }
299  else
300  {
301  throw std::runtime_error("Type has to be \"BOX\" or \"SPHERE\"");
302  }
303  j.at("pose").get_to(r.pose.pose);
304  j.at("pose_frame").get_to(r.pose.header.frame_id);
305 }
306 
307 inline void to_json(nlohmann::json& j, const CollisionDetection& r)
308 {
309  j["gripper_id"] = r.gripper_id;
310  if (r.pre_grasp_offset.x != 0 || r.pre_grasp_offset.y != 0 || r.pre_grasp_offset.z != 0)
311  {
312  j["pre_grasp_offset"] = r.pre_grasp_offset;
313  }
314 }
315 
316 // Services
317 
318 inline void to_json(nlohmann::json& j, const SetLoadCarrierRequest& r)
319 {
320  j["load_carrier"] = r.load_carrier;
321 }
322 
323 inline void from_json(const nlohmann::json& j, SetLoadCarrierResponse& r)
324 {
325  j.at("return_code").get_to(r.return_code);
326 }
327 
328 inline void to_json(nlohmann::json& j, const GetLoadCarriersRequest& r)
329 {
330  j["load_carrier_ids"] = r.load_carrier_ids;
331 }
332 
333 inline void from_json(const nlohmann::json& j, GetLoadCarriersResponse& r)
334 {
335  j.at("load_carriers").get_to(r.load_carriers);
336  j.at("return_code").get_to(r.return_code);
337 }
338 
339 inline void to_json(nlohmann::json& j, const DeleteLoadCarriersRequest& r)
340 {
341  j["load_carrier_ids"] = r.load_carrier_ids;
342 }
343 
344 inline void from_json(const nlohmann::json& j, DeleteLoadCarriersResponse& r)
345 {
346  j.at("return_code").get_to(r.return_code);
347 }
348 
349 inline void to_json(nlohmann::json& j, const DetectLoadCarriersRequest& r)
350 {
351  j["pose_frame"] = r.pose_frame;
352  j["region_of_interest_id"] = r.region_of_interest_id;
353  j["load_carrier_ids"] = r.load_carrier_ids;
354  if (r.pose_frame == "external")
355  {
356  j["robot_pose"] = r.robot_pose;
357  }
358 }
359 
360 inline void from_json(const nlohmann::json& j, DetectLoadCarriersResponse& r)
361 {
362  j.at("timestamp").get_to(r.timestamp);
363  j.at("load_carriers").get_to(r.load_carriers);
364  for (auto lc : r.load_carriers)
365  {
366  j.at("timestamp").get_to(lc.pose.header.stamp);
367  }
368  j.at("return_code").get_to(r.return_code);
369 }
370 
371 inline void to_json(nlohmann::json& j, const SetRegionOfInterestRequest& r)
372 {
373  j["region_of_interest"] = r.region_of_interest;
374  j["pose_frame"] = r.region_of_interest.pose.header.frame_id;
375  if (r.region_of_interest.pose.header.frame_id == "external")
376  {
377  j["robot_pose"] = r.robot_pose;
378  }
379 }
380 
381 inline void from_json(const nlohmann::json& j, SetRegionOfInterestResponse& r)
382 {
383  j.at("return_code").get_to(r.return_code);
384 }
385 
386 inline void to_json(nlohmann::json& j, const GetRegionsOfInterestRequest& r)
387 {
388  j["region_of_interest_ids"] = r.region_of_interest_ids;
389 }
390 
391 inline void from_json(const nlohmann::json& j, GetRegionsOfInterestResponse& r)
392 {
393  j.at("regions_of_interest").get_to(r.regions_of_interest);
394  j.at("return_code").get_to(r.return_code);
395 }
396 
397 inline void to_json(nlohmann::json& j, const DeleteRegionsOfInterestRequest& r)
398 {
399  j["load_carrier_ids"] = r.region_of_interest_ids;
400 }
401 
402 inline void from_json(const nlohmann::json& j, DeleteRegionsOfInterestResponse& r)
403 {
404  j.at("return_code").get_to(r.return_code);
405 }
406 
407 inline void to_json(nlohmann::json& j, const ComputeGraspsRequest& r)
408 {
409  j["pose_frame"] = r.pose_frame;
410  if (r.pose_frame == "external")
411  {
412  j["robot_pose"] = r.robot_pose;
413  }
414  if (!r.region_of_interest_id.empty())
415  {
416  j["region_of_interest_id"] = r.region_of_interest_id;
417  }
418  if (!r.load_carrier_id.empty())
419  {
420  j["load_carrier_id"] = r.load_carrier_id;
421  j["load_carrier_compartment"] = r.load_carrier_compartment;
422  }
423  j["item_models"] = r.item_models;
424  j["suction_surface_length"] = r.suction_surface_length;
425  j["suction_surface_width"] = r.suction_surface_width;
426  if (!r.collision_detection.gripper_id.empty())
427  {
428  j["collision_detection"] = r.collision_detection;
429  }
430 }
431 
432 inline void from_json(const nlohmann::json& j, ComputeGraspsResponse& r)
433 {
434  j.at("timestamp").get_to(r.timestamp);
435  j.at("load_carriers").get_to(r.load_carriers);
436  j.at("grasps").get_to(r.grasps);
437  j.at("return_code").get_to(r.return_code);
438 }
439 
440 inline void to_json(nlohmann::json& j, const ComputeBoxGraspsRequest& r)
441 {
442  j["pose_frame"] = r.pose_frame;
443  if (r.pose_frame == "external")
444  {
445  j["robot_pose"] = r.robot_pose;
446  }
447  if (!r.region_of_interest_id.empty())
448  {
449  j["region_of_interest_id"] = r.region_of_interest_id;
450  }
451  if (!r.load_carrier_id.empty())
452  {
453  j["load_carrier_id"] = r.load_carrier_id;
454  j["load_carrier_compartment"] = r.load_carrier_compartment;
455  }
456  if (!r.item_models.empty())
457  {
458  j["item_models"] = r.item_models;
459  }
460  j["suction_surface_length"] = r.suction_surface_length;
461  j["suction_surface_width"] = r.suction_surface_width;
462  if (!r.collision_detection.gripper_id.empty())
463  {
464  j["collision_detection"] = r.collision_detection;
465  }
466 }
467 
468 inline void from_json(const nlohmann::json& j, ComputeBoxGraspsResponse& r)
469 {
470  j.at("timestamp").get_to(r.timestamp);
471  j.at("load_carriers").get_to(r.load_carriers);
472  j.at("items").get_to(r.items);
473  j.at("grasps").get_to(r.grasps);
474  j.at("return_code").get_to(r.return_code);
475 }
476 
477 inline void to_json(nlohmann::json& j, const DetectItemsRequest& r)
478 {
479  j["item_models"] = r.item_models;
480  j["pose_frame"] = r.pose_frame;
481  if (r.pose_frame == "external")
482  {
483  j["robot_pose"] = r.robot_pose;
484  }
485  if (!r.region_of_interest_id.empty())
486  {
487  j["region_of_interest_id"] = r.region_of_interest_id;
488  }
489  if (!r.load_carrier_id.empty())
490  {
491  j["load_carrier_id"] = r.load_carrier_id;
492  j["load_carrier_compartment"] = r.load_carrier_compartment;
493  }
494 }
495 
496 inline void from_json(const nlohmann::json& j, DetectItemsResponse& r)
497 {
498  j.at("timestamp").get_to(r.timestamp);
499  j.at("load_carriers").get_to(r.load_carriers);
500  j.at("items").get_to(r.items);
501  j.at("return_code").get_to(r.return_code);
502 }
503 
504 inline void to_json(nlohmann::json& j, const DetectFillingLevelRequest& r)
505 {
506  j["pose_frame"] = r.pose_frame;
507  j["region_of_interest_id"] = r.region_of_interest_id;
508  j["load_carrier_ids"] = r.load_carrier_ids;
509  if (r.pose_frame == "external")
510  {
511  j["robot_pose"] = r.robot_pose;
512  }
513  j["filling_level_cell_count"] = r.filling_level_cell_count;
514 }
515 
516 inline void from_json(const nlohmann::json& j, DetectFillingLevelResponse& r)
517 {
518  j.at("timestamp").get_to(r.timestamp);
519  j.at("load_carriers").get_to(r.load_carriers);
520  for (auto lc : r.load_carriers)
521  {
522  j.at("timestamp").get_to(lc.pose.header.stamp);
523  }
524  j.at("return_code").get_to(r.return_code);
525 }
526 
527 } // namespace rc_pick_client
528 
529 #endif // RC_PICK_CLIENT_JSON_CONVERSIONS_H
nlohmann::json json
Definition: pick_client.h:57
void to_json(nlohmann::json &j, const DetectFillingLevelRequest &r)
void from_json(const nlohmann::json &j, ReturnCode &r)


rc_pick_client
Author(s): Monika Florek-Jasinska
autogenerated on Sat Feb 13 2021 03:41:57