moveit2
The MoveIt Motion Planning Framework for ROS 2.
interaction_handler.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
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 Willow Garage 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: Ioan Sucan, Adam Leeper */
36 
37 #pragma once
38 
39 #include <geometry_msgs/msg/pose_stamped.hpp>
42 //#include <moveit/robot_interaction/robot_interaction.h>
43 #include <visualization_msgs/msg/interactive_marker_feedback.hpp>
44 #include <interactive_markers/menu_handler.hpp>
45 #include <tf2_ros/buffer.h>
46 
47 namespace robot_interaction
48 {
49 MOVEIT_CLASS_FORWARD(InteractionHandler); // Defines InteractionHandlerPtr, ConstPtr, WeakPtr... etc
50 MOVEIT_CLASS_FORWARD(RobotInteraction); // Defines RobotInteractionPtr, ConstPtr, WeakPtr... etc
51 MOVEIT_CLASS_FORWARD(KinematicOptionsMap); // Defines KinematicOptionsMapPtr, ConstPtr, WeakPtr... etc
52 
54 struct JointInteraction;
55 struct GenericInteraction;
56 
66 typedef std::function<void(InteractionHandler*, bool)> InteractionHandlerCallbackFn;
67 
77 {
78 public:
79  // Use this constructor if you have an initial RobotState already.
80  InteractionHandler(const RobotInteractionPtr& robot_interaction, const std::string& name,
81  const moveit::core::RobotState& initial_robot_state,
82  const std::shared_ptr<tf2_ros::Buffer>& tf_buffer = std::shared_ptr<tf2_ros::Buffer>());
83 
84  // Use this constructor to start with a default state.
85  InteractionHandler(const RobotInteractionPtr& robot_interaction, const std::string& name,
86  const std::shared_ptr<tf2_ros::Buffer>& tf_buffer = std::shared_ptr<tf2_ros::Buffer>());
87 
88  // DEPRECATED.
89  InteractionHandler(const std::string& name, const moveit::core::RobotState& initial_robot_state,
90  const std::shared_ptr<tf2_ros::Buffer>& tf_buffer = std::shared_ptr<tf2_ros::Buffer>());
91  // DEPRECATED.
92  InteractionHandler(const std::string& name, const moveit::core::RobotModelConstPtr& model,
93  const std::shared_ptr<tf2_ros::Buffer>& tf_buffer = std::shared_ptr<tf2_ros::Buffer>());
94 
96  {
97  }
98 
99  const std::string& getName() const
100  {
101  return name_;
102  }
103 
106 
109 
110  void setUpdateCallback(const InteractionHandlerCallbackFn& callback);
112  void setMeshesVisible(bool visible);
113  bool getMeshesVisible() const;
114  void setControlsVisible(bool visible);
115  bool getControlsVisible() const;
116 
120  void setPoseOffset(const EndEffectorInteraction& eef, const geometry_msgs::msg::Pose& m);
121 
125  void setPoseOffset(const JointInteraction& j, const geometry_msgs::msg::Pose& m);
126 
131  bool getPoseOffset(const EndEffectorInteraction& eef, geometry_msgs::msg::Pose& m);
132 
137  bool getPoseOffset(const JointInteraction& vj, geometry_msgs::msg::Pose& m);
138 
142  void clearPoseOffset(const EndEffectorInteraction& eef);
143 
146  void clearPoseOffset(const JointInteraction& vj);
147 
149  void clearPoseOffsets();
150 
154  void setMenuHandler(const std::shared_ptr<interactive_markers::MenuHandler>& mh);
155 
159  const std::shared_ptr<interactive_markers::MenuHandler>& getMenuHandler();
160 
162  void clearMenuHandler();
163 
169  bool getLastEndEffectorMarkerPose(const EndEffectorInteraction& eef, geometry_msgs::msg::PoseStamped& pose);
170 
176  bool getLastJointMarkerPose(const JointInteraction& vj, geometry_msgs::msg::PoseStamped& pose);
177 
182 
186 
189  void clearLastMarkerPoses();
190 
193  virtual void handleEndEffector(const EndEffectorInteraction& eef,
194  const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr& feedback);
195 
198  virtual void handleJoint(const JointInteraction& vj,
199  const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr& feedback);
200 
203  virtual void handleGeneric(const GenericInteraction& g,
204  const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr& feedback);
205 
208  virtual bool inError(const EndEffectorInteraction& eef) const;
209 
212  virtual bool inError(const JointInteraction& vj) const;
213 
215  virtual bool inError(const GenericInteraction& g) const;
216 
219  void clearError();
220 
221 protected:
222  bool transformFeedbackPose(const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr& feedback,
223  const geometry_msgs::msg::Pose& offset, geometry_msgs::msg::PoseStamped& tpose);
224 
225  const std::string name_;
226  const std::string planning_frame_;
227  std::shared_ptr<tf2_ros::Buffer> tf_buffer_;
228 
229 private:
230  typedef std::function<void(InteractionHandler*)> StateChangeCallbackFn;
231 
232  // Update RobotState using a generic interaction feedback message.
233  // YOU MUST LOCK state_lock_ BEFORE CALLING THIS.
234  void updateStateGeneric(moveit::core::RobotState& state, const GenericInteraction& g,
235  const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr& feedback,
236  StateChangeCallbackFn& callback);
237 
238  // Update RobotState for a new pose of an eef.
239  // YOU MUST LOCK state_lock_ BEFORE CALLING THIS.
240  void updateStateEndEffector(moveit::core::RobotState& state, const EndEffectorInteraction& eef,
241  const geometry_msgs::msg::Pose& pose, StateChangeCallbackFn& callback);
242 
243  // Update RobotState for a new joint position.
244  // YOU MUST LOCK state_lock_ BEFORE CALLING THIS.
245  void updateStateJoint(moveit::core::RobotState& state, const JointInteraction& vj,
246  const geometry_msgs::msg::Pose& pose, StateChangeCallbackFn& callback);
247 
248  // Set the error state for \e name.
249  // Returns true if the error state for \e name changed.
250  // YOU MUST LOCK state_lock_ BEFORE CALLING THIS.
251  bool setErrorState(const std::string& name, bool new_error_state);
252 
255  bool getErrorState(const std::string& name) const;
256 
257  // Contains the (user-programmable) pose offset between the end-effector
258  // parent link (or a virtual joint) and the desired control frame for the
259  // interactive marker. The offset is expressed in the frame of the parent
260  // link or virtual joint. For example, on a PR2 an offset of +0.20 along
261  // the x-axis will move the center of the 6-DOF interactive marker from
262  // the wrist to the finger tips.
263  // PROTECTED BY offset_map_lock_
264  std::map<std::string, geometry_msgs::msg::Pose> offset_map_;
265 
266  // Contains the most recent poses received from interactive marker feedback,
267  // with the offset removed (e.g. in theory, coinciding with the end-effector
268  // parent or virtual joint). This allows a user application to query for the
269  // interactive marker pose (which could be useful for robot control using
270  // gradient-based methods) even when the IK solver failed to find a valid
271  // robot state that satisfies the feedback pose.
272  // PROTECTED BY pose_map_lock_
273  std::map<std::string, geometry_msgs::msg::PoseStamped> pose_map_;
274 
275  std::mutex pose_map_lock_;
276  std::mutex offset_map_lock_;
277 
278  // per group options for doing kinematics.
279  // PROTECTED BY state_lock_ - The POINTER is protected by state_lock_. The
280  // CONTENTS is protected internally.
281  KinematicOptionsMapPtr kinematic_options_map_;
282 
283  // A set of Interactions for which the pose is invalid.
284  // PROTECTED BY state_lock_
285  std::set<std::string> error_state_;
286 
287  // For adding menus (and associated callbacks) to all the
288  // end-effector and virtual-joint interactive markers
289  //
290  // PROTECTED BY state_lock_ - The POINTER is protected by state_lock_. The
291  // CONTENTS is not.
292  std::shared_ptr<interactive_markers::MenuHandler> menu_handler_;
293 
294  // Called when the RobotState maintained by the handler changes.
295  // The caller may, for example, redraw the robot at the new state.
296  // handler is the handler that changed.
297  // error_state_changed is true if an end effector's error state may have
298  // changed.
299  //
300  // PROTECTED BY state_lock_ - the function pointer is protected, but the call
301  // is made without any lock held.
302  std::function<void(InteractionHandler* handler, bool error_state_changed)> update_callback_;
303 
304  // PROTECTED BY state_lock_
305  bool display_meshes_;
306 
307  // PROTECTED BY state_lock_
308  bool display_controls_;
309 
310  // remove '_' characters from name
311  static std::string fixName(std::string name);
312 };
313 } // namespace robot_interaction
Representation of a robot's state. This includes position, velocity, acceleration and effort.
Definition: robot_state.h:90
void clearPoseOffset(const EndEffectorInteraction &eef)
Clear the interactive marker pose offset for the given end-effector.
bool getLastJointMarkerPose(const JointInteraction &vj, geometry_msgs::msg::PoseStamped &pose)
Get the last interactive_marker command pose for a joint.
void clearPoseOffsets()
Clear the pose offset for all end-effectors and virtual joints.
InteractionHandler(const std::string &name, const moveit::core::RobotState &initial_robot_state, const std::shared_ptr< tf2_ros::Buffer > &tf_buffer=std::shared_ptr< tf2_ros::Buffer >())
const std::shared_ptr< interactive_markers::MenuHandler > & getMenuHandler()
Get the menu handler that defines menus and callbacks for all interactive markers drawn by this inter...
virtual void handleGeneric(const GenericInteraction &g, const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr &feedback)
Update the internal state maintained by the handler using information from the received feedback mess...
void setUpdateCallback(const InteractionHandlerCallbackFn &callback)
bool getLastEndEffectorMarkerPose(const EndEffectorInteraction &eef, geometry_msgs::msg::PoseStamped &pose)
Get the last interactive_marker command pose for an end-effector.
virtual void handleJoint(const JointInteraction &vj, const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr &feedback)
Update the internal state maintained by the handler using information from the received feedback mess...
void clearMenuHandler()
Remove the menu handler for this interaction handler.
bool getPoseOffset(const EndEffectorInteraction &eef, geometry_msgs::msg::Pose &m)
Get the offset from EEF to its marker.
void clearError()
Clear any error settings. This makes the markers appear as if the state is no longer invalid.
std::shared_ptr< tf2_ros::Buffer > tf_buffer_
void clearLastJointMarkerPose(const JointInteraction &vj)
Clear the last interactive_marker command pose for the given joint.
const InteractionHandlerCallbackFn & getUpdateCallback() const
virtual bool inError(const EndEffectorInteraction &eef) const
Check if the marker corresponding to this end-effector leads to an invalid state.
InteractionHandler(const RobotInteractionPtr &robot_interaction, const std::string &name, const moveit::core::RobotState &initial_robot_state, const std::shared_ptr< tf2_ros::Buffer > &tf_buffer=std::shared_ptr< tf2_ros::Buffer >())
InteractionHandler(const std::string &name, const moveit::core::RobotModelConstPtr &model, const std::shared_ptr< tf2_ros::Buffer > &tf_buffer=std::shared_ptr< tf2_ros::Buffer >())
void clearLastMarkerPoses()
Clear the last interactive_marker command poses for all end-effectors and joints.
virtual void handleEndEffector(const EndEffectorInteraction &eef, const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr &feedback)
Update the internal state maintained by the handler using information from the received feedback mess...
const std::string & getName() const
void clearLastEndEffectorMarkerPose(const EndEffectorInteraction &eef)
Clear the last interactive_marker command pose for the given end-effector.
bool transformFeedbackPose(const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr &feedback, const geometry_msgs::msg::Pose &offset, geometry_msgs::msg::PoseStamped &tpose)
void setPoseOffset(const EndEffectorInteraction &eef, const geometry_msgs::msg::Pose &m)
Set the offset from EEF to its marker.
void setMenuHandler(const std::shared_ptr< interactive_markers::MenuHandler > &mh)
Set the menu handler that defines menus and callbacks for all interactive markers drawn by this inter...
Maintain a RobotState in a multithreaded environment.
moveit::core::RobotStateConstPtr getState() const
get read-only access to the state.
void setState(const moveit::core::RobotState &state)
Set the state to the new value.
MOVEIT_CLASS_FORWARD(InteractionHandler)
std::function< void(InteractionHandler *, bool)> InteractionHandlerCallbackFn
name
Definition: setup.py:7