moveit2
The MoveIt Motion Planning Framework for ROS 2.
servo_node.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2019, Los Alamos National Security, LLC
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 are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *******************************************************************************/
33 
34 /* Title : servo_node.hpp
35  * Project : moveit_servo
36  * Created : 01/07/2023
37  * Author : Brian O'Neil, Andy Zelenak, Blake Anderson, V Mohammed Ibrahim
38  *
39  * Description : The ROS API for Servo
40  */
41 
42 #pragma once
43 
44 #include <control_msgs/msg/joint_jog.hpp>
45 #include <geometry_msgs/msg/pose_stamped.hpp>
46 #include <geometry_msgs/msg/twist_stamped.hpp>
47 #include <moveit_msgs/srv/servo_command_type.hpp>
48 #include <moveit_msgs/msg/servo_status.hpp>
49 #include <moveit_servo/servo.hpp>
50 #include <rclcpp/rclcpp.hpp>
51 #include <std_msgs/msg/float64_multi_array.hpp>
52 #include <std_srvs/srv/set_bool.hpp>
53 #include <trajectory_msgs/msg/joint_trajectory.hpp>
54 #include <std_msgs/msg/float64_multi_array.hpp>
55 
56 namespace moveit_servo
57 {
58 
59 class ServoNode
60 {
61 public:
62  explicit ServoNode(const rclcpp::NodeOptions& options);
63 
64  ~ServoNode();
65 
66  // Disable copy construction.
67  ServoNode(const ServoNode&) = delete;
68 
69  // Disable copy assignment.
71 
72  // This function is required to make this class a valid NodeClass
73  // see https://docs.ros2.org/latest/api/rclcpp_components/register__node__macro_8hpp.html
74  // Skip linting due to unconventional function naming
75  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface(); // NOLINT
76 
77 private:
81  void servoLoop();
82 
87  void pauseServo(const std::shared_ptr<std_srvs::srv::SetBool::Request>& request,
88  const std::shared_ptr<std_srvs::srv::SetBool::Response>& response);
89 
95  void switchCommandType(const std::shared_ptr<moveit_msgs::srv::ServoCommandType::Request>& request,
96  const std::shared_ptr<moveit_msgs::srv::ServoCommandType::Response>& response);
97 
98  void jointJogCallback(const control_msgs::msg::JointJog::ConstSharedPtr& msg);
99  void twistCallback(const geometry_msgs::msg::TwistStamped::ConstSharedPtr& msg);
100  void poseCallback(const geometry_msgs::msg::PoseStamped::ConstSharedPtr& msg);
101 
102  std::optional<KinematicState> processJointJogCommand(const moveit::core::RobotStatePtr& robot_state);
103  std::optional<KinematicState> processTwistCommand(const moveit::core::RobotStatePtr& robot_state);
104  std::optional<KinematicState> processPoseCommand(const moveit::core::RobotStatePtr& robot_state);
105 
106  // Variables
107 
108  const rclcpp::Node::SharedPtr node_;
109  std::unique_ptr<Servo> servo_;
110  servo::Params servo_params_;
111  planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_;
112 
113  KinematicState last_commanded_state_; // Used when commands go stale;
114  control_msgs::msg::JointJog latest_joint_jog_;
115  geometry_msgs::msg::TwistStamped latest_twist_;
116  geometry_msgs::msg::PoseStamped latest_pose_;
117  rclcpp::Subscription<control_msgs::msg::JointJog>::SharedPtr joint_jog_subscriber_;
118  rclcpp::Subscription<geometry_msgs::msg::TwistStamped>::SharedPtr twist_subscriber_;
119  rclcpp::Subscription<geometry_msgs::msg::PoseStamped>::SharedPtr pose_subscriber_;
120 
121  rclcpp::Publisher<std_msgs::msg::Float64MultiArray>::SharedPtr multi_array_publisher_;
122  rclcpp::Publisher<trajectory_msgs::msg::JointTrajectory>::SharedPtr trajectory_publisher_;
123  rclcpp::Publisher<moveit_msgs::msg::ServoStatus>::SharedPtr status_publisher_;
124 
125  rclcpp::Service<moveit_msgs::srv::ServoCommandType>::SharedPtr switch_command_type_;
126  rclcpp::Service<std_srvs::srv::SetBool>::SharedPtr pause_servo_;
127 
128  // Used for communication with thread
129  std::atomic<bool> stop_servo_;
130  std::atomic<bool> servo_paused_;
131  std::atomic<bool> new_joint_jog_msg_, new_twist_msg_, new_pose_msg_;
132 
133  // Threads used by ServoNode
134  std::thread servo_loop_thread_;
135 
136  // rolling window of joint commands
137  std::deque<KinematicState> joint_cmd_rolling_window_;
138 };
139 
140 } // namespace moveit_servo
ServoNode & operator=(ServoNode &)=delete
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface()
Definition: servo_node.cpp:48
ServoNode(const ServoNode &)=delete
ServoNode(const rclcpp::NodeOptions &options)
Definition: servo_node.cpp:60