moveit2
The MoveIt Motion Planning Framework for ROS 2.
cartesianconfiguration.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019 Pilz GmbH & Co. KG
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 Pilz GmbH & Co. KG 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 
36 
37 #include <stdexcept>
38 #include <tf2_eigen/tf2_eigen.hpp>
39 
41 {
43 {
44 }
45 
46 CartesianConfiguration::CartesianConfiguration(const std::string& group_name, const std::string& link_name,
47  const std::vector<double>& config)
48  : RobotConfiguration(group_name), link_name_(link_name), pose_(toPose(config))
49 {
50 }
51 
52 CartesianConfiguration::CartesianConfiguration(const std::string& group_name, const std::string& link_name,
53  const std::vector<double>& config,
54  const moveit::core::RobotModelConstPtr& robot_model)
55  : RobotConfiguration(group_name, robot_model), link_name_(link_name), pose_(toPose(config))
56 {
57  if (robot_model && (!robot_model_->hasLinkModel(link_name_)))
58  {
59  std::string msg{ "Link \"" };
60  msg.append(link_name).append("\" not known to robot model");
61  throw std::invalid_argument(msg);
62  }
63 
64  if (robot_model && (!moveit::core::RobotState(robot_model_).knowsFrameTransform(link_name_)))
65  {
66  std::string msg{ "Transform of \"" };
67  msg.append(link_name).append("\" is unknown");
68  throw std::invalid_argument(msg);
69  }
70 }
71 
72 geometry_msgs::msg::Pose CartesianConfiguration::toPose(const std::vector<double>& pose)
73 {
74  geometry_msgs::msg::Pose pose_msg;
75  pose_msg.position.x = pose.at(0);
76  pose_msg.position.y = pose.at(1);
77  pose_msg.position.z = pose.at(2);
78  pose_msg.orientation.x = pose.at(3);
79  pose_msg.orientation.y = pose.at(4);
80  pose_msg.orientation.z = pose.at(5);
81  pose_msg.orientation.w = pose.at(6);
82 
83  return pose_msg;
84 }
85 
86 geometry_msgs::msg::PoseStamped CartesianConfiguration::toStampedPose(const geometry_msgs::msg::Pose& pose)
87 {
88  geometry_msgs::msg::PoseStamped pose_stamped_msg;
89  pose_stamped_msg.pose = pose;
90  return pose_stamped_msg;
91 }
92 
93 moveit_msgs::msg::RobotState CartesianConfiguration::toMoveitMsgsRobotState() const
94 {
95  if (!robot_model_)
96  {
97  throw std::runtime_error("No robot model set");
98  }
99 
101  rstate.setToDefaultValues();
102  if (hasSeed())
103  {
104  rstate.setJointGroupPositions(group_name_, getSeed().getJoints());
105  }
106 
107  rstate.update();
108 
109  // set to Cartesian pose
110  Eigen::Isometry3d start_pose;
111  tf2::fromMsg(pose_, start_pose);
112  if (!rstate.setFromIK(rstate.getRobotModel()->getJointModelGroup(group_name_), start_pose, link_name_))
113  {
114  std::ostringstream os;
115  os << "No solution for ik \n" << start_pose.translation() << '\n' << start_pose.linear();
116  throw std::runtime_error(os.str());
117  }
118 
119  // Conversion to RobotState msg type
120  moveit_msgs::msg::RobotState robot_state_msg;
121  moveit::core::robotStateToRobotStateMsg(rstate, robot_state_msg, true);
122  return robot_state_msg;
123 }
124 
125 std::ostream& operator<<(std::ostream& os, const CartesianConfiguration& obj)
126 {
127  os << "Group name: \"" << obj.getGroupName() << '\"';
128  os << " | link name: \"" << obj.getLinkName() << '\"';
129  // TODO(henning): fix pose msg serialization
130  // os << "\n" << obj.getPose();
131  return os;
132 }
133 
134 } // namespace pilz_industrial_motion_planner_testutils
Representation of a robot's state. This includes position, velocity, acceleration and effort.
Definition: robot_state.h:90
void setJointGroupPositions(const std::string &joint_group_name, const double *gstate)
Given positions for the variables that make up a group, in the order found in the group (including va...
Definition: robot_state.h:583
const RobotModelConstPtr & getRobotModel() const
Get the robot model this state is constructed for.
Definition: robot_state.h:104
void update(bool force=false)
Update all transforms.
void setToDefaultValues()
Set all joints to their default positions. The default position is 0, or if that is not within bounds...
bool setFromIK(const JointModelGroup *group, const geometry_msgs::msg::Pose &pose, double timeout=0.0, const GroupStateValidityCallbackFn &constraint=GroupStateValidityCallbackFn(), const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions(), const kinematics::KinematicsBase::IKCostFn &cost_function=kinematics::KinematicsBase::IKCostFn())
If the group this state corresponds to is a chain and a solver is available, then the joint values ca...
Class to define a robot configuration in space with the help of cartesian coordinates.
bool hasSeed() const
States if a seed for the cartesian configuration is set.
moveit_msgs::msg::RobotState toMoveitMsgsRobotState() const override
Class to define robot configuration in space.
void robotStateToRobotStateMsg(const RobotState &state, moveit_msgs::msg::RobotState &robot_state, bool copy_attached_bodies=true)
Convert a MoveIt robot state to a robot state message.
std::ostream & operator<<(std::ostream &, const CartesianConfiguration &)