204 bool publish_twist =
false;
205 bool publish_joint =
false;
207 std::thread{ [
this]() {
return spin(); } }.detach();
209 puts(
"Reading from keyboard");
210 puts(
"---------------------------");
211 puts(
"All commands are in the planning frame");
212 puts(
"Use arrow keys and the '.' and ';' keys to Cartesian jog");
213 puts(
"Use 1|2|3|4|5|6|7 keys to joint jog. 'r' to reverse the direction of jogging.");
214 puts(
"Use 'j' to select joint jog. ");
215 puts(
"Use 't' to select twist ");
216 puts(
"Use 'w' and 'e' to switch between sending command in planning frame or end effector frame");
217 puts(
"'Q' to quit.");
226 catch (
const std::runtime_error&)
232 RCLCPP_DEBUG(nh_->get_logger(),
"value: 0x%02X\n", c);
235 auto twist_msg = std::make_unique<geometry_msgs::msg::TwistStamped>();
236 auto joint_msg = std::make_unique<control_msgs::msg::JointJog>();
238 joint_msg->joint_names.resize(7);
239 joint_msg->joint_names = {
"panda_joint1",
"panda_joint2",
"panda_joint3",
"panda_joint4",
240 "panda_joint5",
"panda_joint6",
"panda_joint7" };
242 joint_msg->velocities.resize(7);
243 std::fill(joint_msg->velocities.begin(), joint_msg->velocities.end(), 0.0);
248 RCLCPP_DEBUG(nh_->get_logger(),
"LEFT");
249 twist_msg->twist.linear.y = -0.5;
250 publish_twist =
true;
253 RCLCPP_DEBUG(nh_->get_logger(),
"RIGHT");
254 twist_msg->twist.linear.y = 0.5;
255 publish_twist =
true;
258 RCLCPP_DEBUG(nh_->get_logger(),
"UP");
259 twist_msg->twist.linear.x = 0.5;
260 publish_twist =
true;
263 RCLCPP_DEBUG(nh_->get_logger(),
"DOWN");
264 twist_msg->twist.linear.x = -0.5;
265 publish_twist =
true;
268 RCLCPP_DEBUG(nh_->get_logger(),
"PERIOD");
269 twist_msg->twist.linear.z = -0.5;
270 publish_twist =
true;
272 case KEYCODE_SEMICOLON:
273 RCLCPP_DEBUG(nh_->get_logger(),
"SEMICOLON");
274 twist_msg->twist.linear.z = 0.5;
275 publish_twist =
true;
278 RCLCPP_DEBUG(nh_->get_logger(),
"1");
279 joint_msg->velocities[0] = joint_vel_cmd_;
280 publish_joint =
true;
283 RCLCPP_DEBUG(nh_->get_logger(),
"2");
284 joint_msg->velocities[1] = joint_vel_cmd_;
285 publish_joint =
true;
288 RCLCPP_DEBUG(nh_->get_logger(),
"3");
289 joint_msg->velocities[2] = joint_vel_cmd_;
290 publish_joint =
true;
293 RCLCPP_DEBUG(nh_->get_logger(),
"4");
294 joint_msg->velocities[3] = joint_vel_cmd_;
295 publish_joint =
true;
298 RCLCPP_DEBUG(nh_->get_logger(),
"5");
299 joint_msg->velocities[4] = joint_vel_cmd_;
300 publish_joint =
true;
303 RCLCPP_DEBUG(nh_->get_logger(),
"6");
304 joint_msg->velocities[5] = joint_vel_cmd_;
305 publish_joint =
true;
308 RCLCPP_DEBUG(nh_->get_logger(),
"7");
309 joint_msg->velocities[6] = joint_vel_cmd_;
310 publish_joint =
true;
313 RCLCPP_DEBUG(nh_->get_logger(),
"r");
314 joint_vel_cmd_ *= -1;
317 RCLCPP_DEBUG(nh_->get_logger(),
"j");
318 request_ = std::make_shared<moveit_msgs::srv::ServoCommandType::Request>();
319 request_->command_type = moveit_msgs::srv::ServoCommandType::Request::JOINT_JOG;
320 if (switch_input_->wait_for_service(std::chrono::seconds(1)))
322 auto result = switch_input_->async_send_request(request_);
323 if (result.get()->success)
325 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Switched to input type: JointJog");
329 RCLCPP_WARN_STREAM(nh_->get_logger(),
"Could not switch input to: JointJog");
334 RCLCPP_DEBUG(nh_->get_logger(),
"t");
335 request_ = std::make_shared<moveit_msgs::srv::ServoCommandType::Request>();
336 request_->command_type = moveit_msgs::srv::ServoCommandType::Request::TWIST;
337 if (switch_input_->wait_for_service(std::chrono::seconds(1)))
339 auto result = switch_input_->async_send_request(request_);
340 if (result.get()->success)
342 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Switched to input type: Twist");
346 RCLCPP_WARN_STREAM(nh_->get_logger(),
"Could not switch input to: Twist");
351 RCLCPP_DEBUG(nh_->get_logger(),
"w");
352 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Command frame set to: " << PLANNING_FRAME_ID);
353 command_frame_id_ = PLANNING_FRAME_ID;
356 RCLCPP_DEBUG(nh_->get_logger(),
"e");
357 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Command frame set to: " << EE_FRAME_ID);
358 command_frame_id_ = EE_FRAME_ID;
361 RCLCPP_DEBUG(nh_->get_logger(),
"quit");
368 twist_msg->header.stamp = nh_->now();
369 twist_msg->header.frame_id = command_frame_id_;
370 twist_pub_->publish(std::move(twist_msg));
371 publish_twist =
false;
373 else if (publish_joint)
375 joint_msg->header.stamp = nh_->now();
376 joint_msg->header.frame_id = PLANNING_FRAME_ID;
377 joint_pub_->publish(std::move(joint_msg));
378 publish_joint =
false;