200 bool publish_twist =
false;
201 bool publish_joint =
false;
203 std::thread{ [
this]() {
return spin(); } }.detach();
205 puts(
"Reading from keyboard");
206 puts(
"---------------------------");
207 puts(
"All commands are in the planning frame");
208 puts(
"Use arrow keys and the '.' and ';' keys to Cartesian jog");
209 puts(
"Use 1|2|3|4|5|6|7 keys to joint jog. 'r' to reverse the direction of jogging.");
210 puts(
"Use 'j' to select joint jog. ");
211 puts(
"Use 't' to select twist ");
212 puts(
"Use 'w' and 'e' to switch between sending command in planning frame or end effector frame");
213 puts(
"'Q' to quit.");
222 catch (
const std::runtime_error&)
228 RCLCPP_DEBUG(nh_->get_logger(),
"value: 0x%02X\n", c);
231 auto twist_msg = std::make_unique<geometry_msgs::msg::TwistStamped>();
232 auto joint_msg = std::make_unique<control_msgs::msg::JointJog>();
234 joint_msg->joint_names.resize(7);
235 joint_msg->joint_names = {
"panda_joint1",
"panda_joint2",
"panda_joint3",
"panda_joint4",
236 "panda_joint5",
"panda_joint6",
"panda_joint7" };
238 joint_msg->velocities.resize(7);
239 std::fill(joint_msg->velocities.begin(), joint_msg->velocities.end(), 0.0);
244 RCLCPP_DEBUG(nh_->get_logger(),
"LEFT");
245 twist_msg->twist.linear.y = -0.5;
246 publish_twist =
true;
249 RCLCPP_DEBUG(nh_->get_logger(),
"RIGHT");
250 twist_msg->twist.linear.y = 0.5;
251 publish_twist =
true;
254 RCLCPP_DEBUG(nh_->get_logger(),
"UP");
255 twist_msg->twist.linear.x = 0.5;
256 publish_twist =
true;
259 RCLCPP_DEBUG(nh_->get_logger(),
"DOWN");
260 twist_msg->twist.linear.x = -0.5;
261 publish_twist =
true;
264 RCLCPP_DEBUG(nh_->get_logger(),
"PERIOD");
265 twist_msg->twist.linear.z = -0.5;
266 publish_twist =
true;
268 case KEYCODE_SEMICOLON:
269 RCLCPP_DEBUG(nh_->get_logger(),
"SEMICOLON");
270 twist_msg->twist.linear.z = 0.5;
271 publish_twist =
true;
274 RCLCPP_DEBUG(nh_->get_logger(),
"1");
275 joint_msg->velocities[0] = joint_vel_cmd_;
276 publish_joint =
true;
279 RCLCPP_DEBUG(nh_->get_logger(),
"2");
280 joint_msg->velocities[1] = joint_vel_cmd_;
281 publish_joint =
true;
284 RCLCPP_DEBUG(nh_->get_logger(),
"3");
285 joint_msg->velocities[2] = joint_vel_cmd_;
286 publish_joint =
true;
289 RCLCPP_DEBUG(nh_->get_logger(),
"4");
290 joint_msg->velocities[3] = joint_vel_cmd_;
291 publish_joint =
true;
294 RCLCPP_DEBUG(nh_->get_logger(),
"5");
295 joint_msg->velocities[4] = joint_vel_cmd_;
296 publish_joint =
true;
299 RCLCPP_DEBUG(nh_->get_logger(),
"6");
300 joint_msg->velocities[5] = joint_vel_cmd_;
301 publish_joint =
true;
304 RCLCPP_DEBUG(nh_->get_logger(),
"7");
305 joint_msg->velocities[6] = joint_vel_cmd_;
306 publish_joint =
true;
309 RCLCPP_DEBUG(nh_->get_logger(),
"r");
310 joint_vel_cmd_ *= -1;
313 RCLCPP_DEBUG(nh_->get_logger(),
"j");
314 request_ = std::make_shared<moveit_msgs::srv::ServoCommandType::Request>();
315 request_->command_type = moveit_msgs::srv::ServoCommandType::Request::JOINT_JOG;
316 if (switch_input_->wait_for_service(std::chrono::seconds(1)))
318 auto result = switch_input_->async_send_request(request_);
319 if (result.get()->success)
321 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Switched to input type: JointJog");
325 RCLCPP_WARN_STREAM(nh_->get_logger(),
"Could not switch input to: JointJog");
330 RCLCPP_DEBUG(nh_->get_logger(),
"t");
331 request_ = std::make_shared<moveit_msgs::srv::ServoCommandType::Request>();
332 request_->command_type = moveit_msgs::srv::ServoCommandType::Request::TWIST;
333 if (switch_input_->wait_for_service(std::chrono::seconds(1)))
335 auto result = switch_input_->async_send_request(request_);
336 if (result.get()->success)
338 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Switched to input type: Twist");
342 RCLCPP_WARN_STREAM(nh_->get_logger(),
"Could not switch input to: Twist");
347 RCLCPP_DEBUG(nh_->get_logger(),
"w");
348 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Command frame set to: " << PLANNING_FRAME_ID);
349 command_frame_id_ = PLANNING_FRAME_ID;
352 RCLCPP_DEBUG(nh_->get_logger(),
"e");
353 RCLCPP_INFO_STREAM(nh_->get_logger(),
"Command frame set to: " << EE_FRAME_ID);
354 command_frame_id_ = EE_FRAME_ID;
357 RCLCPP_DEBUG(nh_->get_logger(),
"quit");
364 twist_msg->header.stamp = nh_->now();
365 twist_msg->header.frame_id = command_frame_id_;
366 twist_pub_->publish(std::move(twist_msg));
367 publish_twist =
false;
369 else if (publish_joint)
371 joint_msg->header.stamp = nh_->now();
372 joint_msg->header.frame_id = PLANNING_FRAME_ID;
373 joint_pub_->publish(std::move(joint_msg));
374 publish_joint =
false;