Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
bt_conversions.hpp
1 // Copyright (c) 2018 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NAV2_BEHAVIOR_TREE__BT_CONVERSIONS_HPP_
16 #define NAV2_BEHAVIOR_TREE__BT_CONVERSIONS_HPP_
17 
18 #include <string>
19 
20 #include "rclcpp/time.hpp"
21 #include "behaviortree_cpp_v3/behavior_tree.h"
22 #include "geometry_msgs/msg/point.hpp"
23 #include "geometry_msgs/msg/quaternion.hpp"
24 #include "geometry_msgs/msg/pose_stamped.hpp"
25 
26 namespace BT
27 {
28 
29 // The follow templates are required when using these types as parameters
30 // in our BT XML files. They parse the strings in the XML into their corresponding
31 // data type.
32 
38 template<>
39 inline geometry_msgs::msg::Point convertFromString(const StringView key)
40 {
41  // three real numbers separated by semicolons
42  auto parts = BT::splitString(key, ';');
43  if (parts.size() != 3) {
44  throw std::runtime_error("invalid number of fields for point attribute)");
45  } else {
46  geometry_msgs::msg::Point position;
47  position.x = BT::convertFromString<double>(parts[0]);
48  position.y = BT::convertFromString<double>(parts[1]);
49  position.z = BT::convertFromString<double>(parts[2]);
50  return position;
51  }
52 }
53 
59 template<>
60 inline geometry_msgs::msg::Quaternion convertFromString(const StringView key)
61 {
62  // four real numbers separated by semicolons
63  auto parts = BT::splitString(key, ';');
64  if (parts.size() != 4) {
65  throw std::runtime_error("invalid number of fields for orientation attribute)");
66  } else {
67  geometry_msgs::msg::Quaternion orientation;
68  orientation.x = BT::convertFromString<double>(parts[0]);
69  orientation.y = BT::convertFromString<double>(parts[1]);
70  orientation.z = BT::convertFromString<double>(parts[2]);
71  orientation.w = BT::convertFromString<double>(parts[3]);
72  return orientation;
73  }
74 }
75 
81 template<>
82 inline geometry_msgs::msg::PoseStamped convertFromString(const StringView key)
83 {
84  // 7 real numbers separated by semicolons
85  auto parts = BT::splitString(key, ';');
86  if (parts.size() != 9) {
87  throw std::runtime_error("invalid number of fields for PoseStamped attribute)");
88  } else {
89  geometry_msgs::msg::PoseStamped pose_stamped;
90  pose_stamped.header.stamp = rclcpp::Time(BT::convertFromString<int64_t>(parts[0]));
91  pose_stamped.header.frame_id = BT::convertFromString<std::string>(parts[1]);
92  pose_stamped.pose.position.x = BT::convertFromString<double>(parts[2]);
93  pose_stamped.pose.position.y = BT::convertFromString<double>(parts[3]);
94  pose_stamped.pose.position.z = BT::convertFromString<double>(parts[4]);
95  pose_stamped.pose.orientation.x = BT::convertFromString<double>(parts[5]);
96  pose_stamped.pose.orientation.y = BT::convertFromString<double>(parts[6]);
97  pose_stamped.pose.orientation.z = BT::convertFromString<double>(parts[7]);
98  pose_stamped.pose.orientation.w = BT::convertFromString<double>(parts[8]);
99  return pose_stamped;
100  }
101 }
102 
108 template<>
109 inline std::chrono::milliseconds convertFromString<std::chrono::milliseconds>(const StringView key)
110 {
111  return std::chrono::milliseconds(std::stoul(key.data()));
112 }
113 
114 } // namespace BT
115 
116 #endif // NAV2_BEHAVIOR_TREE__BT_CONVERSIONS_HPP_