moveit2
The MoveIt Motion Planning Framework for ROS 2.
utils.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2022, Peter David Fagan
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PickNik Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Peter David Fagan */
36 
37 #include "utils.h"
38 #include <rclcpp/rclcpp.hpp>
41 
42 namespace moveit_py
43 {
44 namespace bind_kinematic_constraints
45 {
46 moveit_msgs::msg::Constraints constructLinkConstraint(const std::string& link_name, const std::string& source_frame,
47  std::optional<std::vector<double>> cartesian_position,
48  std::optional<double> cartesian_position_tolerance,
49  std::optional<std::vector<double>> orientation,
50  std::optional<double> orientation_tolerance)
51 {
52  // check that link cartesian and/or orientation constraints are specified
53  if (!cartesian_position && !orientation)
54  {
55  throw std::invalid_argument("No link cartesian or orientation constraints specified");
56  }
57 
58  moveit_msgs::msg::Constraints constraints_cpp;
59 
60  // merge constraints if necessary
61  if (cartesian_position && orientation)
62  {
63  // define point stamped message
64  geometry_msgs::msg::PointStamped point;
65  point.header.frame_id = source_frame;
66  point.point.x = cartesian_position.value()[0];
67  point.point.y = cartesian_position.value()[1];
68  point.point.z = cartesian_position.value()[2];
69 
70  moveit_msgs::msg::Constraints position_constraints =
71  kinematic_constraints::constructGoalConstraints(link_name, point, cartesian_position_tolerance.value());
72 
73  // define quaternion message
74  geometry_msgs::msg::QuaternionStamped quaternion;
75  quaternion.header.frame_id = source_frame;
76  quaternion.quaternion.x = orientation.value()[0];
77  quaternion.quaternion.y = orientation.value()[1];
78  quaternion.quaternion.z = orientation.value()[2];
79  quaternion.quaternion.w = orientation.value()[3];
80 
81  moveit_msgs::msg::Constraints orientation_constraints =
82  kinematic_constraints::constructGoalConstraints(link_name, quaternion, orientation_tolerance.value());
83 
84  constraints_cpp = kinematic_constraints::mergeConstraints(position_constraints, orientation_constraints);
85  }
86 
87  // generate cartesian constraint
88  else if (cartesian_position)
89  {
90  // define point stamped message
91  geometry_msgs::msg::PointStamped point;
92  point.header.frame_id = source_frame;
93  point.point.x = cartesian_position.value()[0];
94  point.point.y = cartesian_position.value()[1];
95  point.point.z = cartesian_position.value()[2];
96 
97  // instantiate logger
98  auto logger = rclcpp::get_logger("moveit_py");
99  // check point with logger
100  RCLCPP_DEBUG(rclcpp::get_logger("moveit_py"), "Point: %f, %f, %f", point.point.x, point.point.y, point.point.z);
101 
102  constraints_cpp = kinematic_constraints::constructGoalConstraints(link_name, point, *cartesian_position_tolerance);
103  }
104 
105  // generate orientation constraint
106  else
107  {
108  // define quaternion message
109  geometry_msgs::msg::QuaternionStamped quaternion;
110  quaternion.header.frame_id = source_frame;
111  quaternion.quaternion.x = orientation.value()[0];
112  quaternion.quaternion.y = orientation.value()[1];
113  quaternion.quaternion.z = orientation.value()[2];
114  quaternion.quaternion.w = orientation.value()[3];
115  constraints_cpp =
116  kinematic_constraints::constructGoalConstraints(link_name, quaternion, orientation_tolerance.value());
117  }
118 
119  return constraints_cpp;
120 }
121 
122 moveit_msgs::msg::Constraints constructJointConstraint(moveit::core::RobotState& robot_state,
123  moveit::core::JointModelGroup* joint_model_group,
124  double tolerance)
125 {
126  // generate joint constraint message
127  moveit_msgs::msg::Constraints joint_constraints =
128  kinematic_constraints::constructGoalConstraints(robot_state, joint_model_group, tolerance);
129 
130  return joint_constraints;
131 }
132 
133 moveit_msgs::msg::Constraints constructConstraintsFromNode(const std::shared_ptr<rclcpp::Node>& node_name,
134  const std::string& ns)
135 {
136  // construct constraint message
137  moveit_msgs::msg::Constraints constraints_cpp;
138  kinematic_constraints::constructConstraints(node_name, ns, constraints_cpp);
139 
140  return constraints_cpp;
141 }
142 
143 void initKinematicConstraints(py::module& m)
144 {
145  py::module kinematic_constraints = m.def_submodule("kinematic_constraints");
146 
147  kinematic_constraints.def("construct_link_constraint", &constructLinkConstraint, py::arg("link_name"),
148  py::arg("source_frame"), py::arg("cartesian_position") = nullptr,
149  py::arg("cartesian_position_tolerance") = nullptr, py::arg("orientation") = nullptr,
150  py::arg("orientation_tolerance") = nullptr, "Construct a link constraint message");
151  kinematic_constraints.def("construct_joint_constraint", &constructJointConstraint, py::arg("robot_state"),
152  py::arg("joint_model_group"), py::arg("tolerance") = 0.01,
153  "Construct a joint constraint message");
154  kinematic_constraints.def("construct_constraints_from_node", &constructConstraintsFromNode, py::arg("node_name"),
155  py::arg("ns"), "Construct a constraint message from a node");
156 }
157 
158 } // namespace bind_kinematic_constraints
159 } // namespace moveit_py
Representation of a robot's state. This includes position, velocity, acceleration and effort.
Definition: robot_state.h:90
Representation and evaluation of kinematic constraints.
moveit_msgs::msg::Constraints constructGoalConstraints(const moveit::core::RobotState &state, const moveit::core::JointModelGroup *jmg, double tolerance_below, double tolerance_above)
Generates a constraint message intended to be used as a goal constraint for a joint group....
Definition: utils.cpp:152
bool constructConstraints(const rclcpp::Node::SharedPtr &node, const std::string &constraints_param, moveit_msgs::msg::Constraints &constraints)
extract constraint message from node parameters.
Definition: utils.cpp:606
moveit_msgs::msg::Constraints mergeConstraints(const moveit_msgs::msg::Constraints &first, const moveit_msgs::msg::Constraints &second)
Merge two sets of constraints into one.
Definition: utils.cpp:64
void initKinematicConstraints(py::module &m)
Definition: utils.cpp:143
moveit_msgs::msg::Constraints constructJointConstraint(moveit::core::RobotState &robot_state, moveit::core::JointModelGroup *joint_model_group, double tolerance)
Definition: utils.cpp:122
moveit_msgs::msg::Constraints constructConstraintsFromNode(const std::shared_ptr< rclcpp::Node > &node_name, const std::string &ns)
Definition: utils.cpp:133
moveit_msgs::msg::Constraints constructLinkConstraint(const std::string &link_name, const std::string &source_frame, std::optional< std::vector< double >> cartesian_position, std::optional< double > cartesian_position_tolerance, std::optional< std::vector< double >> orientation, std::optional< double > orientation_tolerance)
Definition: utils.cpp:46