在获取Moveit中机械臂当前状态时遇到报错:Failed to fetch current robot state
获取当前位置代码如下:
// Get the current pose of the end effector
geometry_msgs::msg::Pose current_pose = move_group_interface.getCurrentPose().pose;
// Print the current pose
RCLCPP_INFO(node->get_logger(), "Current Pose:");
RCLCPP_INFO(node->get_logger(), "Position: [x: %f, y: %f, z: %f]",
current_pose.position.x, current_pose.position.y, current_pose.position.z);
RCLCPP_INFO(node->get_logger(), "Orientation: [x: %f, y: %f, z: %f, w: %f]",
current_pose.orientation.x, current_pose.orientation.y,
current_pose.orientation.z, current_pose.orientation.w);
经排查发现是未在功能函数中对ROS消息回调进行异步处理,增加异步处理程序:
// Create an executor and spin it in a separate thread
rclcpp::executors::MultiThreadedExecutor executor;
executor.add_node(node);
std::thread executor_thread([&executor]() {
executor.spin();
问题解决。