59int main(
int argc,
char* argv[])
61 rclcpp::init(argc, argv);
64 const rclcpp::Node::SharedPtr demo_node = std::make_shared<rclcpp::Node>(
"moveit_servo_demo");
68 const std::string param_namespace =
"moveit_servo";
69 const std::shared_ptr<const servo::ParamListener> servo_param_listener =
70 std::make_shared<const servo::ParamListener>(demo_node, param_namespace);
71 const servo::Params servo_params = servo_param_listener->get_params();
74 rclcpp::Publisher<trajectory_msgs::msg::JointTrajectory>::SharedPtr trajectory_outgoing_cmd_pub =
75 demo_node->create_publisher<trajectory_msgs::msg::JointTrajectory>(servo_params.command_out_topic,
76 rclcpp::SystemDefaultsQoS());
84 const auto get_current_pose = [](
const std::string& target_frame,
const moveit::core::RobotStatePtr& robot_state) {
85 return robot_state->getGlobalLinkTransform(target_frame);
90 std::this_thread::sleep_for(std::chrono::seconds(3));
95 robot_state->getJointModelGroup(servo_params.move_group_name);
102 target_pose.
frame_id = K_BASE_FRAME;
105 target_pose.
pose = get_current_pose(K_TIP_FRAME, robot_state);
108 Eigen::Isometry3d terminal_pose = target_pose.
pose;
109 terminal_pose.rotate(Eigen::AngleAxisd(M_PI / 4, Eigen::Vector3d::UnitZ()));
110 terminal_pose.translate(Eigen::Vector3d(0.0, 0.0, -0.1));
113 Eigen::Vector3d linear_step_size{ 0.0, 0.0, -0.001 };
114 Eigen::AngleAxisd angular_step_size(0.01, Eigen::Vector3d::UnitZ());
118 rclcpp::WallRate servo_rate(1 / servo_params.publish_period);
121 std::deque<KinematicState> joint_cmd_rolling_window;
123 updateSlidingWindow(current_state, joint_cmd_rolling_window, servo_params.max_expected_latency, demo_node->now());
125 bool satisfies_linear_tolerance =
false;
126 bool satisfies_angular_tolerance =
false;
127 bool stop_tracking =
false;
128 while (!stop_tracking && rclcpp::ok())
131 target_pose.
pose = get_current_pose(K_TIP_FRAME, robot_state);
132 satisfies_linear_tolerance |= target_pose.
pose.translation().isApprox(terminal_pose.translation(),
133 servo_params.pose_tracking.linear_tolerance);
134 satisfies_angular_tolerance |=
135 target_pose.
pose.rotation().isApprox(terminal_pose.rotation(), servo_params.pose_tracking.angular_tolerance);
136 stop_tracking = satisfies_linear_tolerance && satisfies_angular_tolerance;
139 if (!satisfies_linear_tolerance)
141 target_pose.
pose.translate(linear_step_size);
143 if (!satisfies_angular_tolerance)
145 target_pose.
pose.rotate(angular_step_size);
151 if (status != StatusCode::INVALID)
153 updateSlidingWindow(joint_state, joint_cmd_rolling_window, servo_params.max_expected_latency, demo_node->now());
156 trajectory_outgoing_cmd_pub->publish(msg.value());
158 if (!joint_cmd_rolling_window.empty())
160 robot_state->setJointGroupPositions(joint_model_group, joint_cmd_rolling_window.back().positions);
161 robot_state->setJointGroupVelocities(joint_model_group, joint_cmd_rolling_window.back().velocities);
168 RCLCPP_INFO_STREAM(demo_node->get_logger(),
"REACHED : " << stop_tracking);
169 RCLCPP_INFO(demo_node->get_logger(),
"Exiting demo.");