forked from moveit/moveit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo_node.cpp
357 lines (314 loc) · 12.9 KB
/
servo_node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*******************************************************************************
* BSD 3-Clause License
*
* Copyright (c) 2019, Los Alamos National Security, LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
/* Title : servo_node.cpp
* Project : moveit_servo
* Created : 12/31/2018
* Author : Andy Zelenak, V Mohammed Ibrahim
*
*/
#include <moveit_servo/servo_node.hpp>
#include <realtime_tools/thread_priority.hpp>
namespace
{
const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit_servo.servo_node");
} // namespace
namespace moveit_servo
{
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr ServoNode::get_node_base_interface()
{
return node_->get_node_base_interface();
}
ServoNode::~ServoNode()
{
stop_servo_ = true;
if (servo_loop_thread_.joinable())
servo_loop_thread_.join();
}
ServoNode::ServoNode(const rclcpp::NodeOptions& options)
: node_{ std::make_shared<rclcpp::Node>("servo_node", options) }
, stop_servo_{ false }
, servo_paused_{ false }
, new_joint_jog_msg_{ false }
, new_twist_msg_{ false }
, new_pose_msg_{ false }
{
if (!options.use_intra_process_comms())
{
RCLCPP_WARN_STREAM(LOGGER, "Intra-process communication is disabled, consider enabling it by adding: "
"\nextra_arguments=[{'use_intra_process_comms' : True}]\nto the Servo composable node "
"in the launch file");
}
// Check if a realtime kernel is available
if (realtime_tools::has_realtime_kernel())
{
if (realtime_tools::configure_sched_fifo(servo_params_.thread_priority))
{
RCLCPP_INFO_STREAM(LOGGER, "Realtime kernel available, higher thread priority has been set.");
}
else
{
RCLCPP_WARN_STREAM(LOGGER, "Could not enable FIFO RT scheduling policy.");
}
}
else
{
RCLCPP_WARN_STREAM(LOGGER, "Realtime kernel is recommended for better performance.");
}
std::shared_ptr<servo::ParamListener> servo_param_listener =
std::make_shared<servo::ParamListener>(node_, "moveit_servo");
// Create Servo instance
planning_scene_monitor_ = createPlanningSceneMonitor(node_, servo_param_listener->get_params());
servo_ = std::make_unique<Servo>(node_, servo_param_listener, planning_scene_monitor_);
servo_params_ = servo_->getParams();
// Create subscriber for jointjog
joint_jog_subscriber_ = node_->create_subscription<control_msgs::msg::JointJog>(
servo_params_.joint_command_in_topic, rclcpp::SystemDefaultsQoS(),
[this](const control_msgs::msg::JointJog::ConstSharedPtr& msg) { return jointJogCallback(msg); });
// Create subscriber for twist
twist_subscriber_ = node_->create_subscription<geometry_msgs::msg::TwistStamped>(
servo_params_.cartesian_command_in_topic, rclcpp::SystemDefaultsQoS(),
[this](const geometry_msgs::msg::TwistStamped::ConstSharedPtr& msg) { return twistCallback(msg); });
// Create subscriber for pose
pose_subscriber_ = node_->create_subscription<geometry_msgs::msg::PoseStamped>(
servo_params_.pose_command_in_topic, rclcpp::SystemDefaultsQoS(),
[this](const geometry_msgs::msg::PoseStamped::ConstSharedPtr& msg) { return poseCallback(msg); });
if (servo_params_.command_out_type == "trajectory_msgs/JointTrajectory")
{
trajectory_publisher_ = node_->create_publisher<trajectory_msgs::msg::JointTrajectory>(
servo_params_.command_out_topic, rclcpp::SystemDefaultsQoS());
}
else if (servo_params_.command_out_type == "std_msgs/Float64MultiArray")
{
multi_array_publisher_ = node_->create_publisher<std_msgs::msg::Float64MultiArray>(servo_params_.command_out_topic,
rclcpp::SystemDefaultsQoS());
}
// Create status publisher
status_publisher_ =
node_->create_publisher<moveit_msgs::msg::ServoStatus>(servo_params_.status_topic, rclcpp::SystemDefaultsQoS());
// Create service to enable switching command type
switch_command_type_ = node_->create_service<moveit_msgs::srv::ServoCommandType>(
"~/switch_command_type", [this](const std::shared_ptr<moveit_msgs::srv::ServoCommandType::Request>& request,
const std::shared_ptr<moveit_msgs::srv::ServoCommandType::Response>& response) {
return switchCommandType(request, response);
});
// Create service to pause/unpause servoing
pause_servo_ = node_->create_service<std_srvs::srv::SetBool>(
"~/pause_servo", [this](const std::shared_ptr<std_srvs::srv::SetBool::Request>& request,
const std::shared_ptr<std_srvs::srv::SetBool::Response>& response) {
return pauseServo(request, response);
});
// Start the servoing loop
servo_loop_thread_ = std::thread(&ServoNode::servoLoop, this);
}
void ServoNode::pauseServo(const std::shared_ptr<std_srvs::srv::SetBool::Request>& request,
const std::shared_ptr<std_srvs::srv::SetBool::Response>& response)
{
servo_paused_ = request->data;
response->success = (servo_paused_ == request->data);
if (servo_paused_)
{
servo_->setCollisionChecking(false);
response->message = "Servoing disabled";
}
else
{
servo_->setCollisionChecking(true);
response->message = "Servoing enabled";
}
}
void ServoNode::switchCommandType(const std::shared_ptr<moveit_msgs::srv::ServoCommandType::Request>& request,
const std::shared_ptr<moveit_msgs::srv::ServoCommandType::Response>& response)
{
const bool is_valid = (request->command_type >= static_cast<int8_t>(CommandType::MIN)) &&
(request->command_type <= static_cast<int8_t>(CommandType::MAX));
if (is_valid)
{
servo_->setCommandType(static_cast<CommandType>(request->command_type));
}
else
{
RCLCPP_WARN_STREAM(LOGGER, "Unknown command type " << request->command_type << " requested");
}
response->success = (request->command_type == static_cast<int8_t>(servo_->getCommandType()));
}
void ServoNode::jointJogCallback(const control_msgs::msg::JointJog::ConstSharedPtr& msg)
{
latest_joint_jog_ = *msg;
new_joint_jog_msg_ = true;
}
void ServoNode::twistCallback(const geometry_msgs::msg::TwistStamped::ConstSharedPtr& msg)
{
latest_twist_ = *msg;
new_twist_msg_ = true;
}
void ServoNode::poseCallback(const geometry_msgs::msg::PoseStamped::ConstSharedPtr& msg)
{
latest_pose_ = *msg;
new_pose_msg_ = true;
}
std::optional<KinematicState> ServoNode::processJointJogCommand()
{
std::optional<KinematicState> next_joint_state = std::nullopt;
// Reject any other command types that had arrived simultaneously.
new_twist_msg_ = new_pose_msg_ = false;
const bool command_stale = (node_->now() - latest_joint_jog_.header.stamp) >=
rclcpp::Duration::from_seconds(servo_params_.incoming_command_timeout);
if (!command_stale)
{
JointJogCommand command{ latest_joint_jog_.joint_names, latest_joint_jog_.velocities };
next_joint_state = servo_->getNextJointState(command);
}
else
{
auto result = servo_->smoothHalt(last_commanded_state_);
new_joint_jog_msg_ = result.first;
if (new_joint_jog_msg_)
{
next_joint_state = result.second;
}
else
{
RCLCPP_INFO_STREAM(LOGGER, "Joint jog command timed out, will not publish outgoing commands.");
}
}
return next_joint_state;
}
std::optional<KinematicState> ServoNode::processTwistCommand()
{
std::optional<KinematicState> next_joint_state = std::nullopt;
// Mark latest twist command as processed.
// Reject any other command types that had arrived simultaneously.
new_joint_jog_msg_ = new_pose_msg_ = false;
const bool command_stale = (node_->now() - latest_twist_.header.stamp) >=
rclcpp::Duration::from_seconds(servo_params_.incoming_command_timeout);
if (!command_stale)
{
const Eigen::Vector<double, 6> velocities{ latest_twist_.twist.linear.x, latest_twist_.twist.linear.y,
latest_twist_.twist.linear.z, latest_twist_.twist.angular.x,
latest_twist_.twist.angular.y, latest_twist_.twist.angular.z };
const TwistCommand command{ latest_twist_.header.frame_id, velocities };
next_joint_state = servo_->getNextJointState(command);
}
else
{
auto result = servo_->smoothHalt(last_commanded_state_);
new_twist_msg_ = result.first;
if (new_twist_msg_)
{
next_joint_state = result.second;
}
else
{
RCLCPP_INFO_STREAM(LOGGER, "Twist command timed out, will not publish outgoing commands.");
}
}
return next_joint_state;
}
std::optional<KinematicState> ServoNode::processPoseCommand()
{
std::optional<KinematicState> next_joint_state = std::nullopt;
// Mark latest pose command as processed.
// Reject any other command types that had arrived simultaneously.
new_joint_jog_msg_ = new_twist_msg_ = false;
const bool command_stale = (node_->now() - latest_pose_.header.stamp) >=
rclcpp::Duration::from_seconds(servo_params_.incoming_command_timeout);
if (!command_stale)
{
const PoseCommand command = poseFromPoseStamped(latest_pose_);
next_joint_state = servo_->getNextJointState(command);
}
else
{
auto result = servo_->smoothHalt(last_commanded_state_);
new_pose_msg_ = result.first;
if (new_pose_msg_)
{
next_joint_state = result.second;
}
else
{
RCLCPP_INFO_STREAM(LOGGER, "Pose command timed out, will not publish outgoing commands.");
}
}
return next_joint_state;
}
void ServoNode::servoLoop()
{
moveit_msgs::msg::ServoStatus status_msg;
std::optional<KinematicState> next_joint_state = std::nullopt;
rclcpp::WallRate servo_frequency(1 / servo_params_.publish_period);
while (rclcpp::ok() && !stop_servo_)
{
// Skip processing if servoing is disabled.
if (servo_paused_)
continue;
next_joint_state = std::nullopt;
const CommandType expected_type = servo_->getCommandType();
if (expected_type == CommandType::JOINT_JOG && new_joint_jog_msg_)
{
next_joint_state = processJointJogCommand();
}
else if (expected_type == CommandType::TWIST && new_twist_msg_)
{
next_joint_state = processTwistCommand();
}
else if (expected_type == CommandType::POSE && new_pose_msg_)
{
next_joint_state = processPoseCommand();
}
else if (new_joint_jog_msg_ || new_twist_msg_ || new_pose_msg_)
{
new_joint_jog_msg_ = new_twist_msg_ = new_pose_msg_ = false;
RCLCPP_WARN_STREAM(LOGGER, "Command type has not been set, cannot accept input");
}
if (next_joint_state && (servo_->getStatus() != StatusCode::INVALID))
{
if (servo_params_.command_out_type == "trajectory_msgs/JointTrajectory")
{
trajectory_publisher_->publish(composeTrajectoryMessage(servo_->getParams(), next_joint_state.value()));
}
else if (servo_params_.command_out_type == "std_msgs/Float64MultiArray")
{
multi_array_publisher_->publish(composeMultiArrayMessage(servo_->getParams(), next_joint_state.value()));
}
last_commanded_state_ = next_joint_state.value();
}
status_msg.code = static_cast<int8_t>(servo_->getStatus());
status_msg.message = servo_->getStatusMessage();
status_publisher_->publish(status_msg);
servo_frequency.sleep();
}
}
} // namespace moveit_servo
#include "rclcpp_components/register_node_macro.hpp"
RCLCPP_COMPONENTS_REGISTER_NODE(moveit_servo::ServoNode)