203 bool publish_twist =
false;
204 bool publish_joint =
false;
206 std::thread{ [
this]() {
return spin(); } }.detach();
208 puts(
"Reading from keyboard");
209 puts(
"---------------------------");
210 puts(
"All commands are in the planning frame");
211 puts(
"Use arrow keys and the '.' and ';' keys to Cartesian jog");
212 puts(
"Use 1|2|3|4|5|6|7 keys to joint jog. 'r' to reverse the direction of jogging.");
213 puts(
"Use 'j' to select joint jog. ");
214 puts(
"Use 't' to select twist ");
215 puts(
"Use 'w' and 'e' to switch between sending command in planning frame or end effector frame");
216 puts(
"'Q' to quit.");
225 catch (
const std::runtime_error&)
231 RCLCPP_DEBUG(nh_->get_logger(),
"value: 0x%02X\n", c);
234 auto twist_msg = std::make_unique<geometry_msgs::msg::TwistStamped>();
235 auto joint_msg = std::make_unique<control_msgs::msg::JointJog>();
237 joint_msg->joint_names.resize(7);
238 joint_msg->joint_names = {
"panda_joint1",
"panda_joint2",
"panda_joint3",
"panda_joint4",
239 "panda_joint5",
"panda_joint6",
"panda_joint7" };
241 joint_msg->velocities.resize(7);
242 std::fill(joint_msg->velocities.begin(), joint_msg->velocities.end(), 0.0);
247 RCLCPP_DEBUG(nh_->get_logger(),
"LEFT");
248 twist_msg->twist.linear.y = -0.5;
249 publish_twist =
true;
252 RCLCPP_DEBUG(nh_->get_logger(),
"RIGHT");
253 twist_msg->twist.linear.y = 0.5;
254 publish_twist =
true;
257 RCLCPP_DEBUG(nh_->get_logger(),
"UP");
258 twist_msg->twist.linear.x = 0.5;
259 publish_twist =
true;
262 RCLCPP_DEBUG(nh_->get_logger(),
"DOWN");
263 twist_msg->twist.linear.x = -0.5;
264 publish_twist =
true;
267 RCLCPP_DEBUG(nh_->get_logger(),
"PERIOD");
268 twist_msg->twist.linear.z = -0.5;
269 publish_twist =
true;
271 case KEYCODE_SEMICOLON:
272 RCLCPP_DEBUG(nh_->get_logger(),
"SEMICOLON");
273 twist_msg->twist.linear.z = 0.5;
274 publish_twist =
true;
277 RCLCPP_DEBUG(nh_->get_logger(),
"1");
278 joint_msg->velocities[0] = joint_vel_cmd_;
279 publish_joint =
true;
282 RCLCPP_DEBUG(nh_->get_logger(),
"2");
283 joint_msg->velocities[1] = joint_vel_cmd_;
284 publish_joint =
true;
287 RCLCPP_DEBUG(nh_->get_logger(),
"3");
288 joint_msg->velocities[2] = joint_vel_cmd_;
289 publish_joint =
true;
292 RCLCPP_DEBUG(nh_->get_logger(),
"4");
293 joint_msg->velocities[3] = joint_vel_cmd_;
294 publish_joint =
true;
297 RCLCPP_DEBUG(nh_->get_logger(),
"5");
298 joint_msg->velocities[4] = joint_vel_cmd_;
299 publish_joint =
true;
302 RCLCPP_DEBUG(nh_->get_logger(),
"6");
303 joint_msg->velocities[5] = joint_vel_cmd_;
304 publish_joint =
true;
307 RCLCPP_DEBUG(nh_->get_logger(),
"7");
308 joint_msg->velocities[6] = joint_vel_cmd_;
309 publish_joint =
true;
312 RCLCPP_DEBUG(nh_->get_logger(),
"r");
313 joint_vel_cmd_ *= -1;
316 RCLCPP_DEBUG(nh_->get_logger(),
"j");
317 request_ = std::make_shared<moveit_msgs::srv::ServoCommandType::Request>();
318 request_->command_type = moveit_msgs::srv::ServoCommandType::Request::JOINT_JOG;
319 if (switch_input_->wait_for_service(std::chrono::seconds(1)))
321 auto result = switch_input_->async_send_request(request_);
322 if (result.get()->success)
324 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Switched to input type: JointJog");
328 RCLCPP_WARN_STREAM(nh_->get_logger(),
"Could not switch input to: JointJog");
333 RCLCPP_DEBUG(nh_->get_logger(),
"t");
334 request_ = std::make_shared<moveit_msgs::srv::ServoCommandType::Request>();
335 request_->command_type = moveit_msgs::srv::ServoCommandType::Request::TWIST;
336 if (switch_input_->wait_for_service(std::chrono::seconds(1)))
338 auto result = switch_input_->async_send_request(request_);
339 if (result.get()->success)
341 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Switched to input type: Twist");
345 RCLCPP_WARN_STREAM(nh_->get_logger(),
"Could not switch input to: Twist");
350 RCLCPP_DEBUG(nh_->get_logger(),
"w");
351 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Command frame set to: " << PLANNING_FRAME_ID);
352 command_frame_id_ = PLANNING_FRAME_ID;
355 RCLCPP_DEBUG(nh_->get_logger(),
"e");
356 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Command frame set to: " << EE_FRAME_ID);
357 command_frame_id_ = EE_FRAME_ID;
360 RCLCPP_DEBUG(nh_->get_logger(),
"quit");
367 twist_msg->header.stamp = nh_->now();
368 twist_msg->header.frame_id = command_frame_id_;
369 twist_pub_->publish(std::move(twist_msg));
370 publish_twist =
false;
372 else if (publish_joint)
374 joint_msg->header.stamp = nh_->now();
375 joint_msg->header.frame_id = PLANNING_FRAME_ID;
376 joint_pub_->publish(std::move(joint_msg));
377 publish_joint =
false;