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());
85 std::this_thread::sleep_for(std::chrono::seconds(3));
90 robot_state->getJointModelGroup(servo_params.move_group_name);
97 TwistCommand target_twist{
"panda_link0", { 0.0, 0.0, 0.05, 0.0, 0.0, 0.4 } };
100 rclcpp::WallRate rate(1.0 / servo_params.publish_period);
102 std::chrono::seconds timeout_duration(4);
103 std::chrono::seconds time_elapsed(0);
104 const auto start_time = std::chrono::steady_clock::now();
107 std::deque<KinematicState> joint_cmd_rolling_window;
109 updateSlidingWindow(current_state, joint_cmd_rolling_window, servo_params.max_expected_latency, demo_node->now());
117 const auto current_time = std::chrono::steady_clock::now();
118 time_elapsed = std::chrono::duration_cast<std::chrono::seconds>(current_time - start_time);
119 if (time_elapsed > timeout_duration)
121 RCLCPP_INFO_STREAM(demo_node->get_logger(),
"Timed out");
124 else if (status != StatusCode::INVALID)
126 updateSlidingWindow(joint_state, joint_cmd_rolling_window, servo_params.max_expected_latency, demo_node->now());
129 trajectory_outgoing_cmd_pub->publish(msg.value());
131 if (!joint_cmd_rolling_window.empty())
133 robot_state->setJointGroupPositions(joint_model_group, joint_cmd_rolling_window.back().positions);
134 robot_state->setJointGroupVelocities(joint_model_group, joint_cmd_rolling_window.back().velocities);
140 RCLCPP_INFO(demo_node->get_logger(),
"Exiting demo.");
void updateSlidingWindow(KinematicState &next_joint_state, std::deque< KinematicState > &joint_cmd_rolling_window, double max_expected_latency, const rclcpp::Time &cur_time)
Adds a new joint state command to a queue containing commands over a time window. Also modifies the v...