ROS topic: opencv Mat转nav_msgs/OccupancyGrid 并发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Zed_Of_Zoe/article/details/116703598

目的

在处理SLAM地图模型时, 通常需要把点云映射到二维图像上进行处理, 需要用到opencv Mat.
处理完成后, 需要将opencv Mat转为ros消息nav_msgs/OccupancyGrid并发布.
其中需要注意物理坐标与图像坐标的转换

涉及的主要全局变量

cv::Mat MapMat; // 二维图像
double minx, miny, maxx, maxy; // 物理坐标的最大/最小值, 单位 m
int xsize, ysize; // 二维图像的尺寸, xsize为rows, ysize为cols
float resolution; // 二维图像的分辨率, 单位 m


vector<PointTypePose> keyPoses6D_normal; // 关键帧的6自由度位姿
vector<pcl::PointCloud<PointType>::Ptr> ground_cloud_key_vec; // 关键帧中的地面点云

物理坐标与图像坐标的转换

由于某些原因, 我使用的物理坐标系与图像坐标系的XY轴是相反的.

点云映射到二维图像

// 初始化MapMat矩阵
PointType pt1, pt2;
pcl::getMinMax3D(*laser_cloud_surround, pt1, pt2);
minx = pt1.z, miny = pt1.x, maxx = pt2.z, maxy = pt2.x;
// 多加20像素以免不越界
xsize = ceil((maxx - minx) / resolution + 20), ysize = ceil((maxy - miny) / resolution + 20);

unsigned char tmp_MapMat[xsize][ysize];
for(int i = 0; i < xsize; i++)
{
    unsigned char *data = *(tmp_MapMat + i);
    for(int j = 0; j < ysize; j++)
        *(data + j) = 127;
}

/*
用点云更新tmp_MapMat
*/
// 更新MapMat示例
int ground_cloud_key_vec_size = ground_cloud_key_vec.size();
int pose_size = keyPoses6D_normal.size();
//ground_cloud_all->clear();
pcl::PointCloud<PointType>::Ptr tmp_ground_cloud_all;
tmp_ground_cloud_all.reset(new pcl::PointCloud<PointType>());

for(int index = 0; index < pose_size; index++)
{
    PointTypePose pt = keyPoses6D_normal[index];
    if(ground_cloud_key_vec_size > index)
    {
        pcl::PointCloud<PointType>::Ptr ground_cloud_key;
        ground_cloud_key.reset(new pcl::PointCloud<PointType>());
    
        ground_cloud_key = transformPointCloud(ground_cloud_key_vec[index], &pt);
        *tmp_ground_cloud_all += *ground_cloud_key;
        for(auto p:ground_cloud_key->points)
        {
            Eigen::Vector2i coor = PointType2MatCoor(p);
            for(int i = -1; i <= 1; i++)
            {
                int x = coor[0] + i;
                unsigned char* data = *(tmp_MapMat + x);
                for(int j = -1; j <= 1; j++)
                {
                    int y = coor[1] + j;
                    if(x<xsize && x>=0 && y<ysize && y>=0)
                    {
                        unsigned char pixel = *(data + y);
                        if(pixel == 255)
                            continue;
                        if(pixel < 255 - free_acc_val)
                            *(data + y) = pixel + free_acc_val;
                        else
                            *(data + y) = 255;
                    }
                }
            }
        }
    }
}

IsMapMatUpdating = true;
MapMat.release();
MapMat = cv::Mat(xsize, ysize, CV_8UC1, tmp_MapMat);
IsMapMatUpdating = false;

物理坐标转图像坐标

其中, 物理坐标转图像坐标的函数,
考虑到MapMat的更新和发布在不同的thread, 重载了一个加入rowscols参数的函数

Eigen::Vector2i PointType2MatCoor(PointType pt)
{
    int coor0 = xsize - ((pt.x - minx) / resolution) - 10;
    int coor1 = ysize - ((pt.y - miny) / resolution) - 10;
    return Eigen::Vector2i(coor0, coor1);
}

Eigen::Vector2i PointType2MatCoor(PointType pt, int rows, int cols)
{
    int coor0 = rows - ((pt.x - minx) / resolution) - 10;
    int coor1 = cols - ((pt.y - miny) / resolution) - 10;
    return Eigen::Vector2i(coor0, coor1);
}

图像坐标转物理坐标

相反地, 图像坐标转物理坐标的函数,
考虑到MapMat的更新和发布在不同的thread, 重载了一个加入rows和cols参数的函数

PointType MatCoor2PointType(Eigen::Vector2i coor)
{
    double x = (xsize - coor[0] - 10) * resolution + minx;
    double y = (ysize - coor[1] - 10) * resolution + miny;
    PointType res;
    res.x = x, res.y = y;
    return res;
}

PointType MatCoor2PointType(Eigen::Vector2i coor, int rows, int cols)
{
    double x = (rows - coor[0] - 10) * resolution + minx;
    double y = (cols - coor[1] - 10) * resolution + miny;
    PointType res;
    res.x = x, res.y = y;
    return res;
}

opencv Mat转nav_msgs/OccupancyGrid 并发布

注意图像坐标系和物理坐标系相反, 体现在对origingridmap.data的赋值

if(!IsMapMatUpdating && IsMapUpdated)
{
    Mat MapMat_copy = MapMat.clone();
    int rows = MapMat_copy.rows, cols = MapMat_copy.cols;
    nav_msgs::OccupancyGrid gridmap;
    gridmap.info.resolution = resolution;
    PointType origin = MatCoor2PointType(Eigen::Vector2i(rows-1, cols-1), rows, cols);
    gridmap.info.origin.position.x = origin.x;
    gridmap.info.origin.position.y = origin.y;
    gridmap.info.origin.position.z = 1.0;
    gridmap.info.origin.orientation.x = 0.0;
    gridmap.info.origin.orientation.y = 0.0;
    gridmap.info.origin.orientation.z = 0.0;
    gridmap.info.origin.orientation.w = 1.0;
    gridmap.info.width = rows;
    gridmap.info.height = cols;
    gridmap.data.resize(rows * cols);
    for(int x = 0; x < rows; x++)
    {
        unsigned char * data = MapMat_copy.ptr<uchar>(rows-1 - x);
        for(int y = 0; y < cols; y++)
            gridmap.data[y * rows + x] = 100 - *(data + cols-1 - y) * 100 / 255;
    }
    gridmap.header.stamp = ros::Time().fromSec(time_now);
    gridmap.header.frame_id = "/map";
    pubMapMatRVIZ.publish(gridmap);
}
#include <ros/ros.h> #include <nav_msgs/OccupancyGrid.h> #include <sensor_msgs/Image.h> #include <sensor_msgs/RegionOfInterest.h> #include <sensor_msgs/LaserScan.h> #include <tf2/LinearMath/Quaternion.h> #include <tf2_ros/transform_broadcaster.h> #include <tf2_ros/transform_listener.h> #include <tf2_geometry_msgs/tf2_geometry_msgs.h> #include <tf2/LinearMath/Matrix3x3.h> #include <geometry_msgs/TransformStamped.h> #include <geometry_msgs/PoseWithCovarianceStamped.h> #include <cv_bridge/cv_bridge.h> #include <opencv2/opencv.hpp> #include <std_msgs/String.h> #include <std_srvs/Empty.h> #include <vector> #include <cmath> #include <tf2/LinearMath/Matrix3x3.h> #include <deque> #include <nav_msgs/Odometry.h> // 添加Odometry消息 nav_msgs::OccupancyGrid map_msg; cv::Mat map_cropped; cv::Mat map_temp; cv::Mat map_match; sensor_msgs::RegionOfInterest map_roi_info; std::vector<cv::Point2f> scan_points; std::vector<cv::Point2f> best_transform; ros::ServiceClient clear_costmaps_client; std::string base_frame; std::string odom_frame; std::string laser_frame; std::string laser_topic; float lidar_x = 250, lidar_y = 250, lidar_yaw = 0; float deg_to_rad = M_PI / 180.0; int cur_sum = 0; int clear_countdown = -1; int scan_count = 0; // 全局:保存上一次滤波后的状态 double s_x = 0.0, s_y = 0.0, s_yaw = 0.0; bool s_init = false; double alpha; // 在 main 里通过 param 读取 // 添加全局发布ros::Publisher robot_pose_pub; // 用于发布机器人位姿 // 初始姿态回调函数 void initialPoseCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& msg) { try { static tf2_ros::Buffer tfBuffer; static tf2_ros::TransformListener tfListener(tfBuffer); // 1. 查询从 base_frame 到 laser_frame 的换 geometry_msgs::TransformStamped transformStamped = tfBuffer.lookupTransform(base_frame, laser_frame, ros::Time(0), ros::Duration(1.0)); // 2. 创建一个 stamped pose 用于换 geometry_msgs::PoseStamped base_pose, laser_pose; base_pose.header = msg->header; base_pose.pose = msg->pose.pose; // 3. 将 base_frame 的位姿换为 laser_frame 的位姿 tf2::doTransform(base_pose, laser_pose, transformStamped); // 4. 从换后的消息中提取位置和方向信息 double x = msg->pose.pose.position.x; double y = msg->pose.pose.position.y; tf2::Quaternion q( laser_pose.pose.orientation.x, laser_pose.pose.orientation.y, laser_pose.pose.orientation.z, laser_pose.pose.orientation.w); // 5. 将四元数换为yaw角度 tf2::Matrix3x3 m(q); double roll, pitch, yaw; m.getRPY(roll, pitch, yaw); // 6. 将地图坐标换为裁切后的地图坐标 lidar_x = (x - map_msg.info.origin.position.x) / map_msg.info.resolution - map_roi_info.x_offset; lidar_y = (y - map_msg.info.origin.position.y) / map_msg.info.resolution - map_roi_info.y_offset; // 7. 设置yaw角度 lidar_yaw = -yaw; clear_countdown = 30; } catch (tf2::TransformException &ex) { ROS_WARN("无法获取从 %s 到 %s 的: %s", base_frame.c_str(), laser_frame.c_str(), ex.what()); } } void crop_map(); void processMap(); void mapCallback(const nav_msgs::OccupancyGrid::ConstPtr& msg) { map_msg = *msg; crop_map(); processMap(); } void crop_map() { // 显示地图信息 std_msgs::Header header = map_msg.header; nav_msgs::MapMetaData info = map_msg.info; //用来统计地图有效区域的变量 int xMax,xMin,yMax,yMin ; xMax=xMin= info.width/2; yMax=yMin= info.height/2; bool bFirstPoint = true; // 把地图数据换成图片 cv::Mat map_raw(info.height, info.width, CV_8UC1, cv::Scalar(128)); // 灰色背景 for(int y = 0; y < info.height; y++) { for(int x = 0; x < info.width; x++) { int index = y * info.width + x; // 直接使用map_msg.data中的值 map_raw.at<uchar>(y, x) = static_cast<uchar>(map_msg.data[index]); // 统计有效区域 if(map_msg.data[index] == 100) { if(bFirstPoint) { xMax = xMin = x; yMax = yMin = y; bFirstPoint = false; continue; } xMin = std::min(xMin, x); xMax = std::max(xMax, x); yMin = std::min(yMin, y); yMax = std::max(yMax, y); } } } // 计算有效区域的中心点坐标 int cen_x = (xMin + xMax)/2; int cen_y = (yMin + yMax)/2; // 按照有效区域对地图进行裁剪 int new_half_width = abs(xMax - xMin)/2 + 50; int new_half_height = abs(yMax - yMin)/2 + 50; int new_origin_x = cen_x - new_half_width; int new_origin_y = cen_y - new_half_height; int new_width = new_half_width*2; int new_height = new_half_height*2; if(new_origin_x < 0) new_origin_x = 0; if((new_origin_x + new_width) > info.width) new_width = info.width - new_origin_x; if(new_origin_y < 0) new_origin_y = 0; if((new_origin_y + new_height) > info.height) new_height = info.height - new_origin_y; cv::Rect roi(new_origin_x, new_origin_y, new_width, new_height); cv::Mat roi_map = map_raw(roi).clone(); map_cropped = roi_map; // 地图的裁减信息 map_roi_info.x_offset = new_origin_x; map_roi_info.y_offset = new_origin_y; map_roi_info.width = new_width; map_roi_info.height = new_height; geometry_msgs::PoseWithCovarianceStamped init_pose; init_pose.pose.pose.position.x = 0.0; init_pose.pose.pose.position.y = 0.0; init_pose.pose.pose.position.y = 0.0; init_pose.pose.pose.orientation.x = 0.0; init_pose.pose.pose.orientation.y = 0.0; init_pose.pose.pose.orientation.z = 0.0; init_pose.pose.pose.orientation.w = 1.0; geometry_msgs::PoseWithCovarianceStamped::ConstPtr init_pose_ptr(new geometry_msgs::PoseWithCovarianceStamped(init_pose)); initialPoseCallback(init_pose_ptr); } bool check(float x, float y, float yaw); void scanCallback(const sensor_msgs::LaserScan::ConstPtr& msg) { scan_points.clear(); double angle = msg->angle_min; for (size_t i = 0; i < msg->ranges.size(); ++i) { if (msg->ranges[i] >= msg->range_min && msg->ranges[i] <= msg->range_max) { float x = msg->ranges[i] * cos(angle) / map_msg.info.resolution; float y = -msg->ranges[i] * sin(angle) / map_msg.info.resolution; scan_points.push_back(cv::Point2f(x, y)); } angle += msg->angle_increment; } if(scan_count == 0) scan_count ++; while (ros::ok()) { if (!map_cropped.empty()) { // 计算三种情况下的雷达点坐标数组 std::vector<cv::Point2f> transform_points, clockwise_points, counter_points; int max_sum = 0; float best_dx = 0, best_dy = 0, best_dyaw = 0; for (const auto& point : scan_points) { // 情况一:原始角度 float rotated_x = point.x * cos(lidar_yaw) - point.y * sin(lidar_yaw); float rotated_y = point.x * sin(lidar_yaw) + point.y * cos(lidar_yaw); transform_points.push_back(cv::Point2f(rotated_x + lidar_x, lidar_y - rotated_y)); // 情况二:顺时针旋1度 float clockwise_yaw = lidar_yaw + deg_to_rad; rotated_x = point.x * cos(clockwise_yaw) - point.y * sin(clockwise_yaw); rotated_y = point.x * sin(clockwise_yaw) + point.y * cos(clockwise_yaw); clockwise_points.push_back(cv::Point2f(rotated_x + lidar_x, lidar_y - rotated_y)); // 情况三:逆时针旋1度 float counter_yaw = lidar_yaw - deg_to_rad; rotated_x = point.x * cos(counter_yaw) - point.y * sin(counter_yaw); rotated_y = point.x * sin(counter_yaw) + point.y * cos(counter_yaw); counter_points.push_back(cv::Point2f(rotated_x + lidar_x, lidar_y - rotated_y)); } // 计算15种变换方式的匹配值 std::vector<cv::Point2f> offsets = {{0,0}, {1,0}, {-1,0}, {0,1}, {0,-1}}; std::vector<std::vector<cv::Point2f>> point_sets = {transform_points, clockwise_points, counter_points}; std::vector<float> yaw_offsets = {0, deg_to_rad, -deg_to_rad}; for (int i = 0; i < offsets.size(); ++i) { for (int j = 0; j < point_sets.size(); ++j) { int sum = 0; for (const auto& point : point_sets[j]) { float px = point.x + offsets[i].x; float py = point.y + offsets[i].y; if (px >= 0 && px < map_temp.cols && py >= 0 && py < map_temp.rows) { sum += map_temp.at<uchar>(py, px); } } if (sum > max_sum) { max_sum = sum; best_dx = offsets[i].x; best_dy = offsets[i].y; best_dyaw = yaw_offsets[j]; } } } // 更新雷达位置和角度 lidar_x += best_dx; lidar_y += best_dy; lidar_yaw += best_dyaw; // 判断匹配循环是否可以终止 if(check(lidar_x , lidar_y , lidar_yaw)) { break; } } } if(clear_countdown > -1) clear_countdown --; if(clear_countdown == 0) { std_srvs::Empty srv; clear_costmaps_client.call(srv); } } std::deque<std::tuple<float, float, float>> data_queue; const size_t max_size = 10; bool check(float x, float y, float yaw) { if(x == 0 && y == 0 && yaw == 0) { data_queue.clear(); return true; } // 添加新数据 data_queue.push_back(std::make_tuple(x, y, yaw)); // 如果队列超过最大大小,移除最旧的数据 if (data_queue.size() > max_size) { data_queue.pop_front(); } // 如果队列已满,检查第一个和最后一个元素 if (data_queue.size() == max_size) { auto& first = data_queue.front(); auto& last = data_queue.back(); float dx = std::abs(std::get<0>(last) - std::get<0>(first)); float dy = std::abs(std::get<1>(last) - std::get<1>(first)); float dyaw = std::abs(std::get<2>(last) - std::get<2>(first)); // 如果所有差值的绝对值都小于5,清空队列退出循环 if (dx < 5 && dy < 5 && dyaw < 5*deg_to_rad) { data_queue.clear(); return true; } } return false; } cv::Mat createGradientMask(int size) { cv::Mat mask(size, size, CV_8UC1); int center = size / 2; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { double distance = std::hypot(x - center, y - center); int value = cv::saturate_cast<uchar>(255 * std::max(0.0, 1.0 - distance / center)); mask.at<uchar>(y, x) = value; } } return mask; } void processMap() { if (map_cropped.empty()) return; map_temp = cv::Mat::zeros(map_cropped.size(), CV_8UC1); cv::Mat gradient_mask = createGradientMask(101); // 创建一个101x101的渐变掩模 for (int y = 0; y < map_cropped.rows; y++) { for (int x = 0; x < map_cropped.cols; x++) { if (map_cropped.at<uchar>(y, x) == 100) // 障碍物 { int left = std::max(0, x - 50); int top = std::max(0, y - 50); int right = std::min(map_cropped.cols - 1, x + 50); int bottom = std::min(map_cropped.rows - 1, y + 50); cv::Rect roi(left, top, right - left + 1, bottom - top + 1); cv::Mat region = map_temp(roi); int mask_left = 50 - (x - left); int mask_top = 50 - (y - top); cv::Rect mask_roi(mask_left, mask_top, roi.width, roi.height); cv::Mat mask = gradient_mask(mask_roi); cv::max(region, mask, region); } } } } void pose_tf() { if (scan_count == 0) return; if (map_cropped.empty() || map_msg.data.empty()) return; // 一阶低通滤波状态 static bool s_init = false; static double s_x = 0.0, s_y = 0.0, s_yaw = 0.0; static tf2_ros::Buffer tfBuffer; static tf2_ros::TransformListener tfListener(tfBuffer); // 1. 计算原始位姿 double raw_x = (lidar_x + map_roi_info.x_offset) * map_msg.info.resolution + map_msg.info.origin.position.x; double raw_y = (lidar_y + map_roi_info.y_offset) * map_msg.info.resolution + map_msg.info.origin.position.y; double raw_yaw = -lidar_yaw; // 2. 低通滤波 if (!s_init) { s_x = raw_x; s_y = raw_y; s_yaw = raw_yaw; s_init = true; } else { double dy = raw_yaw - s_yaw; while (dy > M_PI) dy -= 2.0 * M_PI; while (dy < -M_PI) dy += 2.0 * M_PI; s_x = alpha * raw_x + (1.0 - alpha) * s_x; s_y = alpha * raw_y + (1.0 - alpha) * s_y; s_yaw = s_yaw + alpha * dy; } // 3. 构造 map -> base_link 变换 tf2::Transform map_to_base; tf2::Quaternion q; // ← 把 q 提到此处,使后续都能访问 q.setRPY(0, 0, s_yaw); map_to_base.setOrigin(tf2::Vector3(s_x, s_y, 0.0)); map_to_base.setRotation(q); // 发布 map -> base_link geometry_msgs::TransformStamped map_to_base_msg; map_to_base_msg.header.stamp = ros::Time::now(); map_to_base_msg.header.frame_id = "map"; map_to_base_msg.child_frame_id = base_frame; map_to_base_msg.transform.translation.x = s_x; map_to_base_msg.transform.translation.y = s_y; map_to_base_msg.transform.translation.z = 0.0; map_to_base_msg.transform.rotation = tf2::toMsg(q); // 现在 q 可见 static tf2_ros::TransformBroadcaster br; br.sendTransform(map_to_base_msg); // 发布机器人里程计 nav_msgs::Odometry robot_odom; robot_odom.header.stamp = ros::Time::now(); robot_odom.header.frame_id = "map"; robot_odom.child_frame_id = base_frame; robot_odom.pose.pose.position.x = s_x; robot_odom.pose.pose.position.y = s_y; robot_odom.pose.pose.position.z = 0.0; robot_odom.pose.pose.orientation = tf2::toMsg(q); robot_pose_pub.publish(robot_odom); // 7. 查询 odom -> laser_frame geometry_msgs::TransformStamped odom_to_laser; try { odom_to_laser = tfBuffer.lookupTransform(odom_frame, laser_frame, ros::Time(0)); } catch (tf2::TransformException &ex) { ROS_WARN("%s", ex.what()); return; } // 8. 计算 map -> odom tf2::Transform odom_to_laser_tf2, map_to_laser; tf2::fromMsg(odom_to_laser.transform, odom_to_laser_tf2); // 注意此处用 s_y 而不是 x_y map_to_laser.setOrigin(tf2::Vector3(s_x, s_y, 0.0)); map_to_laser.setRotation(q); tf2::Transform map_to_odom = map_to_laser * odom_to_laser_tf2.inverse(); // 9. 发布 map -> odom geometry_msgs::TransformStamped map_to_odom_msg; map_to_odom_msg.header.stamp = ros::Time::now(); map_to_odom_msg.header.frame_id = "map"; map_to_odom_msg.child_frame_id = odom_frame; map_to_odom_msg.transform = tf2::toMsg(map_to_odom); br.sendTransform(map_to_odom_msg); } int main(int argc, char** argv) { setlocale(LC_ALL,""); ros::init(argc, argv, "lidar_loc"); // 读取参数 ros::NodeHandle private_nh("~"); private_nh.param<std::string>("base_frame", base_frame, "base_footprint"); private_nh.param<std::string>("odom_frame", odom_frame, "odom"); private_nh.param<std::string>("laser_frame", laser_frame, "scan"); private_nh.param<std::string>("laser_topic", laser_topic, "base_scan"); private_nh.param("filter_alpha", alpha, 0.1); ros::NodeHandle nh; ros::Duration(1).sleep(); // 初始化发布器 - 用于机器人位姿 robot_pose_pub = nh.advertise<nav_msgs::Odometry>("lidar_odom", 10); ros::Subscriber map_sub = nh.subscribe("map", 1, mapCallback); ros::Subscriber scan_sub = nh.subscribe(laser_topic, 1, scanCallback); ros::Subscriber initial_pose_sub = nh.subscribe("initialpose", 1, initialPoseCallback); clear_costmaps_client = nh.serviceClient<std_srvs::Empty>("move_base/clear_costmaps"); ros::Rate rate(30); // tf发送频率 while (ros::ok()) { pose_tf(); ros::spinOnce(); rate.sleep(); } return 0; }为什么我使用这个激光定位算法且加入滤波后,机器人轨迹还是会有跳变或者起伏,请你帮我全面分析一下
08-03
#include "ucar/ucar.hpp" #include <tf2/LinearMath/Quaternion.h> #include <ros/console.h> FileTransfer::FileTransfer(const std::string& baseDir ) : m_baseDir(baseDir), m_running(false), m_serverThread() { createDirectory(m_baseDir); } FileTransfer::~FileTransfer() { stopServer(); } // 功能1:保存字符串到本地文件 bool FileTransfer::saveToFile(const std::string& content, const std::string& filename) { std::string fullPath = m_baseDir + "/" + filename; std::ofstream file(fullPath); if (!file.is_open()) { std::cerr << "无法打开文件: " << fullPath << std::endl; return false; } file << content; file.close(); std::cout << "文件已保存到: " << fullPath << std::endl; return true; } // 功能2:发送文件到指定IP bool FileTransfer::sendTo(const std::string& serverIP, int port, const std::string& localFile, const std::string& remotePath ) { // 1. 读取文件内容 std::ifstream file(localFile, std::ios::binary | std::ios::ate); if (!file.is_open()) { std::cerr << "无法打开本地文件: " << localFile << std::endl; return false; } size_t fileSize = file.tellg(); file.seekg(0, std::ios::beg); std::vector<char> buffer(fileSize); if (!file.read(buffer.data(), fileSize)) { std::cerr << "读取文件错误: " << localFile << std::endl; return false; } file.close(); // 2. 创建Socket int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { std::cerr << "创建Socket失败: " << strerror(errno) << std::endl; return false; } // 3. 连接服务器 sockaddr_in serverAddr; memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(port); if (inet_pton(AF_INET, serverIP.c_str(), &serverAddr.sin_addr) <= 0) { std::cerr << "无效的IP地址: " << serverIP << std::endl; close(sock); return false; } if (connect(sock, (sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) { std::cerr << "连接服务器失败: " << strerror(errno) << std::endl; close(sock); return false; } // 4. 发送文件信息 if (!sendData(sock, remotePath, buffer.data(), fileSize)) { close(sock); return false; } close(sock); std::cout << "文件已发送到 " << serverIP << ":" << port << " 保存为 " << remotePath << std::endl; return true; } // 功能3:阻塞等待接收文件读取内容 std::string FileTransfer::receiveAndRead(int port, int timeoutMs ) { // 确保服务器正在运行 if (!m_running) { startServer(port); } // 等待文件到达 ReceivedFile file; if (!waitForFile(file, timeoutMs)) { throw std::runtime_error("等待文件超时或服务器停止"); } stopServer(); // 返回文件内容 return std::string(file.content.data(), file.content.size()); } // 启动服务器(后台线程) void FileTransfer::startServer(int port) { if (m_running) { stopServer(); } m_running = true; m_serverThread = std::thread(&FileTransfer::serverThreadFunc, this, port); } // 停止服务器 void FileTransfer::stopServer() { if (m_running) { m_running = false; if (m_serverThread.joinable()) { m_serverThread.join(); } } } // 创建目录 bool FileTransfer::createDirectory(const std::string& path) { if (mkdir(path.c_str(), 0777) == -1 && errno != EEXIST) { std::cerr << "无法创建目录: " << path << " - " << strerror(errno) << std::endl; return false; } return true; } // 发送数据到socket bool FileTransfer::sendData(int sock, const std::string& remotePath, const char* data, size_t size) { // 1. 发送路径长度和路径 uint32_t pathLen = htonl(static_cast<uint32_t>(remotePath.size())); if (send(sock, &pathLen, sizeof(pathLen), 0) != sizeof(pathLen)) { std::cerr << "发送路径长度失败" << std::endl; return false; } if (send(sock, remotePath.c_str(), remotePath.size(), 0) != static_cast<ssize_t>(remotePath.size())) { std::cerr << "发送路径失败" << std::endl; return false; } // 2. 发送文件长度和内容 uint32_t netSize = htonl(static_cast<uint32_t>(size)); if (send(sock, &netSize, sizeof(netSize), 0) != sizeof(netSize)) { std::cerr << "发送文件长度失败" << std::endl; return false; } ssize_t totalSent = 0; while (totalSent < static_cast<ssize_t>(size)) { ssize_t sent = send(sock, data + totalSent, size - totalSent, 0); if (sent < 0) { std::cerr << "发送文件内容失败: " << strerror(errno) << std::endl; return false; } totalSent += sent; } return true; } // 服务器线程函数 void FileTransfer::serverThreadFunc(int port) { // 1. 创建Socket int listenSock = socket(AF_INET, SOCK_STREAM, 0); if (listenSock < 0) { std::cerr << "创建Socket失败: " << strerror(errno) << std::endl; return; } // 2. 设置SO_REUSEADDR int opt = 1; setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); // 3. 绑定端口 sockaddr_in serverAddr; memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = INADDR_ANY; serverAddr.sin_port = htons(port); if (bind(listenSock, (sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) { std::cerr << "绑定端口失败: " << strerror(errno) << std::endl; close(listenSock); return; } // 4. 开始监听 if (listen(listenSock, 5) < 0) { std::cerr << "监听失败: " << strerror(errno) << std::endl; close(listenSock); return; } std::cout << "文件接收服务器启动,监听端口: " << port << std::endl; // 5. 接受连接循环 bool receivedFile = false; while (m_running&& !receivedFile) { // 设置超时以定期检查停止条件 fd_set readSet; FD_ZERO(&readSet); FD_SET(listenSock, &readSet); timeval timeout{0, 100000}; // 100ms int ready = select(listenSock + 1, &readSet, nullptr, nullptr, &timeout); if (ready < 0) { if (errno != EINTR) { std::cerr << "select错误: " << strerror(errno) << std::endl; } continue; } if (ready == 0) continue; // 超时,继续循环 // 6. 接受客户端连接 sockaddr_in clientAddr; socklen_t clientLen = sizeof(clientAddr); int clientSock = accept(listenSock, (sockaddr*)&clientAddr, &clientLen); if (clientSock < 0) { if (errno != EAGAIN && errno != EWOULDBLOCK) { std::cerr << "接受连接失败: " << strerror(errno) << std::endl; } continue; } char clientIP[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN); std::cout << "收到来自 " << clientIP << " 的文件传输请求" << std::endl; // 7. 接收文件 ReceivedFile file; if (receiveFile(clientSock, file)) { std::lock_guard<std::mutex> lock(m_mutex); m_receivedFiles.push(std::move(file)); receivedFile = true; m_cv.notify_one(); // 通知等待线程 } close(clientSock); } close(listenSock); std::cout << "文件接收服务器已停止" << std::endl; } // 接收文件 bool FileTransfer::receiveFile(int sock, ReceivedFile& file) { // 1. 接收路径长度和路径 uint32_t pathLen; if (recv(sock, &pathLen, sizeof(pathLen), 0) != sizeof(pathLen)) { std::cerr << "接收路径长度失败" << std::endl; return false; } pathLen = ntohl(pathLen); std::vector<char> pathBuffer(pathLen + 1); ssize_t received = recv(sock, pathBuffer.data(), pathLen, 0); if (received != static_cast<ssize_t>(pathLen)) { std::cerr << "接收路径失败" << std::endl; return false; } pathBuffer[pathLen] = '\0'; file.filename = pathBuffer.data(); // 2. 接收文件长度 uint32_t fileSize; if (recv(sock, &fileSize, sizeof(fileSize), 0) != sizeof(fileSize)) { std::cerr << "接收文件长度失败" << std::endl; return false; } fileSize = ntohl(fileSize); // 3. 接收文件内容 file.content.resize(fileSize); ssize_t totalReceived = 0; while (totalReceived < static_cast<ssize_t>(fileSize) && m_running) { ssize_t bytes = recv(sock, file.content.data() + totalReceived, fileSize - totalReceived, 0); if (bytes <= 0) { if (bytes < 0) { std::cerr << "接收文件内容失败: " << strerror(errno) << std::endl; } return false; } totalReceived += bytes; } std::cout << "成功接收文件: " << file.filename << " (" << fileSize << " 字节)" << std::endl; return true; } // 等待文件到达 bool FileTransfer::waitForFile(ReceivedFile& file, int timeoutMs) { std::unique_lock<std::mutex> lock(m_mutex); // 只要有文件就立即返回 auto pred = [&] { return !m_receivedFiles.empty(); // 只需检查队列是否非空 }; if (timeoutMs > 0) { if (!m_cv.wait_for(lock, std::chrono::milliseconds(timeoutMs), pred)) { return false; // 超时 } } else { m_cv.wait(lock, pred); } if (m_receivedFiles.empty()) return false; file = std::move(m_receivedFiles.front()); m_receivedFiles.pop(); return true; } // 接收文件读取两行内容 bool FileTransfer::receiveAndReadTwoStrings(int port, std::string& outStr1, std::string& outStr2, int timeoutMs) { // 确保服务器正在运行 if (!m_running) { startServer(port); } // 等待文件到达 ReceivedFile file; if (!waitForFile(file, timeoutMs)) { throw std::runtime_error("等待文件超时或服务器停止"); } // 读取文件内容 std::string content(file.content.data(), file.content.size()); // 查找第一行结尾 size_t pos = content.find('\n'); if (pos == std::string::npos) { // 没有换行符,整个内容作为第一行 outStr1 = content; outStr2 = ""; } else { // 提取第一行 outStr1 = content.substr(0, pos); // 提取第二行(跳过换行符) outStr2 = content.substr(pos + 1); // 移除第二行可能的多余换行符 if (!outStr2.empty() && outStr2.back() == '\n') { outStr2.pop_back(); } } return true; } bool Ucar::navigateTo(const geometry_msgs::Pose& target) { move_base_msgs::MoveBaseGoal goal; goal.target_pose.header.frame_id = "map"; goal.target_pose.pose = target; move_base_client_.sendGoal(goal); return move_base_client_.waitForResult(navigation_timeout_); } // void Ucar::recovery() { // setlocale(LC_ALL, ""); // std_srvs::Empty srv; // if (clear_costmaps_client_.call(srv)) { // ROS_INFO("代价地图清除成功"); // } else { // ROS_ERROR("代价地图清除服务调用失败"); // } // } Ucar::Ucar(ros::NodeHandle& nh) : nh_(nh), retry_count_(0), current_state_(State::INIT) , control_rate_(10) ,move_base_client_("move_base", true) { latest_odom_.reset(); // 等待move_base服务器就绪(参考网页6的actionlib使用规范) if(!move_base_client_.waitForServer(ros::Duration(5.0))) { setlocale(LC_ALL, ""); ROS_ERROR("无法连接move_base服务器"); } scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1); // 初始化代价地图清除服务 clear_costmaps_client_ = nh_.serviceClient<std_srvs::Empty>("/move_base/clear_costmaps"); setlocale(LC_ALL, ""); // 标记所有的点位总共21个点位 nh_.param("pose1_x", pose_1.position.x, 1.67); nh_.param("pose1_y", pose_1.position.y, 0.04); pose_1.orientation.x = 0.0; pose_1.orientation.y = 0.0; pose_1.orientation.z = 0.707; pose_1.orientation.w = 0.707; nh_.param("pose2_x", pose_2.position.x, 1.83); nh_.param("pose2_y", pose_2.position.y, 0.380); pose_2.orientation.x = 0.0; pose_2.orientation.y = 0.0; pose_2.orientation.z = 1; pose_2.orientation.w = 0; nh_.param("pose3_x", pose_3.position.x, 1.0); nh_.param("pose3_y", pose_3.position.y, 0.494); pose_3.orientation.x = 0.0; pose_3.orientation.y = 0.0; pose_3.orientation.z = 1; pose_3.orientation.w = 0; nh_.param("pose4_x", pose_4.position.x, 0.15166891130059032); nh_.param("pose4_y", pose_4.position.y, 0.5446138339072089); pose_4.orientation.x = 0.0; pose_4.orientation.y = 0.0; pose_4.orientation.z = 0.707; pose_4.orientation.w = 0.707; nh_.param("pose5_x", pose_5.position.x, 0.387605134459779); nh_.param("pose5_y", pose_5.position.y, 0.949243539748394); pose_5.orientation.x = 0.0; pose_5.orientation.y = 0.0; pose_5.orientation.z = 0.0; pose_5.orientation.w = 1.0; nh_.param("pose6_x", pose_6.position.x, 1.0250469329539987); nh_.param("pose6_y", pose_6.position.y, 1.0430107266961729); pose_6.orientation.x = 0.0; pose_6.orientation.y = 0.0; pose_6.orientation.z = 0.0; pose_6.orientation.w = 1.0; nh_.param("pose7_x", pose_7.position.x, 1.715746358650675); nh_.param("pose7_y", pose_7.position.y, 1.0451169673664757); pose_7.orientation.x = 0.0; pose_7.orientation.y = 0.0; pose_7.orientation.z = 0.707; pose_7.orientation.w = 0.707; nh_.param("pose8_x", pose_8.position.x, 1.820954899866641); nh_.param("pose8_y", pose_8.position.y, 1.405578846446346); pose_8.orientation.x = 0.0; pose_8.orientation.y = 0.0; pose_8.orientation.z = 1; pose_8.orientation.w = 0; nh_.param("pose9_x", pose_9.position.x, 1.287663212010699); nh_.param("pose9_y", pose_9.position.y, 1.4502232396357953); pose_9.orientation.x = 0.0; pose_9.orientation.y = 0.0; pose_9.orientation.z = 1; pose_9.orientation.w = 0; nh_.param("pose10_x", pose_10.position.x, 0.433025661760874); nh_.param("pose10_y", pose_10.position.y, 1.5362058814619577); pose_10.orientation.x = 0.0; pose_10.orientation.y = 0.0; pose_10.orientation.z = 0.707; pose_10.orientation.w = 0.707; //开始进入视觉定位区域 中心点加上四个中线点对应四面墙 nh_.param("pose_center_x", pose_11.position.x, 1.13); nh_.param("pose_center_y", pose_11.position.y, 3.04); nh_.param("pose_center_x", pose_12.position.x, 1.13); nh_.param("pose_center_y", pose_12.position.y, 3.04); nh_.param("pose_center_x", pose_13.position.x, 1.13); nh_.param("pose_center_y", pose_13.position.y, 3.04); nh_.param("pose_center_x", pose_14.position.x, 1.13); nh_.param("pose_center_y", pose_14.position.y, 3.04); nh_.param("pose_center_x", pose_15.position.x, 1.13); nh_.param("pose_center_y", pose_15.position.y, 3.04); //退出视觉区域进入路灯识别区域 nh_.param("pose_center_x", pose_16.position.x, 1.13); nh_.param("pose_center_y", pose_16.position.y, 3.04); nh_.param("pose_center_x", pose_17.position.x, 1.13); nh_.param("pose_center_y", pose_17.position.y, 3.04); nh_.param("pose_center_x", pose_18.position.x, 1.13); nh_.param("pose_center_y", pose_18.position.y, 3.04); //退出路灯识别区域,进入巡线区域 注意两个点位取一个,必须标定正确的方向 nh_.param("pose_center_x", pose_19.position.x, 1.13); nh_.param("pose_center_y", pose_19.position.y, 3.04); pose_19.orientation.x = 0.0; pose_19.orientation.y = 0.0; pose_19.orientation.z = 0.707; pose_19.orientation.w = 0.707; nh_.param("pose_center_x", pose_20.position.x, 1.13); nh_.param("pose_center_y", pose_20.position.y, 3.04); pose_20.orientation.x = 0.0; pose_20.orientation.y = 0.0; pose_20.orientation.z = 0.707; pose_20.orientation.w = 0.707; //result_sub_ = nh_.subscribe("result_of_object", 1, &Ucar::detectionCallback, this); result_client_ = nh_.serviceClient<rfbot_yolov8_ros::DetectGood>("object_realsense_recognization"); cmd_vel_pub = nh_.advertise<geometry_msgs::Twist>("/cmd_vel", 10); odom_sub = nh_.subscribe("/odom", 10, &Ucar::odomCallback, this); interaction_client = nh_.serviceClient<ucar::ObjectDetection>("object_detection"); ROS_INFO_STREAM("状态机初始化完成"); } void Ucar::navigate_to_aruco_place(){ setlocale(LC_ALL, ""); navigateTo(pose_1);ROS_INFO("到达pose_1"); navigateTo(pose_2);ROS_INFO("到达pose_2"); navigateTo(pose_3);ROS_INFO("到达pose_3"); } void Ucar::navigate_to_recongnition_place(){ setlocale(LC_ALL, ""); navigateTo(pose_4);ROS_INFO("到达pose_4"); navigateTo(pose_5);ROS_INFO("到达pose_5"); navigateTo(pose_6);ROS_INFO("到达pose_6"); navigateTo(pose_7);ROS_INFO("到达pose_7"); navigateTo(pose_8);ROS_INFO("到达pose_8"); navigateTo(pose_9);ROS_INFO("到达pose_9"); navigateTo(pose_10);ROS_INFO("到达pose_10"); } void Ucar::run() { while (ros::ok()) { ros::spinOnce(); switch(current_state_) { case State::INIT: {handleInit(); break;} case State::ARUCO: {navigate_to_aruco_place(); ROS_INFO("导航到二维码区域"); see_aruco(); if(target_object=="vegetable") playAudioFile("/home/ucar/ucar_ws/src/wav/sc.wav"); if(target_object=="fruit") playAudioFile("/home/ucar/ucar_ws/src/wav/sg.wav"); if(target_object=="dessert") playAudioFile("/home/ucar/ucar_ws/src/wav/tp.wav"); break;} case State::FIND: {navigate_to_recongnition_place(); spin_to_find();//着找目标 break;} case State::GAZEBO: {gazebo(); break;} } control_rate_.sleep(); } } void Ucar::handleInit() { setlocale(LC_ALL, ""); ROS_DEBUG("执行初始化流程"); current_state_ = State::ARUCO; } void Ucar::see_aruco() { qr_found_ = false; image_transport::ImageTransport it(nh_); // 2. 创建订阅器,订阅/usb_cam/image_raw话题 image_transport::Subscriber sub = it.subscribe( "/usb_cam/image_raw", 1, &Ucar::imageCallback, this ); std::cout << "已订阅摄像头话题,等待图像数据..." << std::endl; // 3. 创建ZBar扫描器 scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1); target_object = ""; // 重置结果 // 4. 设置超时检测 const int MAX_SCAN_DURATION = 30; // 最大扫描时间(秒) auto scan_start_time = std::chrono::steady_clock::now(); // 5. ROS事件循环 ros::Rate loop_rate(30); // 30Hz while (ros::ok() && !qr_found_) { // 检查超时 auto elapsed = std::chrono::steady_clock::now() - scan_start_time; if (std::chrono::duration_cast<std::chrono::seconds>(elapsed).count() > MAX_SCAN_DURATION) { std::cout << "扫描超时" << std::endl; break; } ros::spinOnce(); loop_rate.sleep(); } // 6. 清理资源 if (!qr_found_) target_object = ""; } void Ucar::imageCallback(const sensor_msgs::ImageConstPtr& msg) { try { // 7. 将ROS图像消息换为OpenCV格式 cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); cv::Mat frame = cv_ptr->image; cv::Mat gray; cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY); // 8. 准备ZBar图像 cv::Mat gray_cont = gray.clone(); zbar::Image image(gray.cols, gray.rows, "Y800", gray_cont.data, gray.cols * gray.rows); // 9. 扫描二维码 if (scanner.scan(image) >= 0) { for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) { // 获取原始结果 std::string rawResult = symbol->get_data(); // 在存储到全局变量前将首字母换为小写 target_object = toLowerFirstChar(rawResult); ROS_INFO("识别到二维码: %s", target_object.c_str()); current_state_ = State::FIND; qr_found_ = true; // 在视频帧上绘制结果 std::vector<cv::Point> points; for (int i = 0; i < symbol->get_location_size(); i++) { points.push_back(cv::Point(symbol->get_location_x(i), symbol->get_location_y(i))); } // 绘制二维码边界框 cv::RotatedRect rect = cv::minAreaRect(points); cv::Point2f vertices[4]; rect.points(vertices); for (int i = 0; i < 4; i++) { cv::line(frame, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 2); } // 显示原始二维码内容 cv::putText(frame, rawResult, cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 0), 2); // 显示换后的结果 cv::putText(frame, "Stored: " + target_object, cv::Point(10, 60), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 2); // 显示最终结果画面 cv::imshow("QR Code Scanner", frame); cv::waitKey(250); // 短暂显示结果 } } // 显示当前帧 cv::imshow("QR Code Scanner", frame); cv::waitKey(1); } catch (cv_bridge::Exception& e) { ROS_ERROR("cv_bridge异常: %s", e.what()); } } //播放音频文件函数 bool Ucar::playAudioFile(const std::string& filePath) { // 创建子进程播放音频 pid_t pid = fork(); if (pid == 0) { // 子进程 // 尝试使用 aplay (适用于 WAV 文件) execlp("aplay", "aplay", "-q", filePath.c_str(), (char*)NULL); // 如果 aplay 失败,尝试 ffplay execlp("ffplay", "ffplay", "-nodisp", "-autoexit", "-loglevel", "quiet", filePath.c_str(), (char*)NULL); // 如果 ffplay 失败,尝试 mpg123 (适用于 MP3) execlp("mpg123", "mpg123", "-q", filePath.c_str(), (char*)NULL); // 如果所有播放器都不可用,退出 exit(1); } else if (pid > 0) { // 父进程 int status; waitpid(pid, &status, 0); return WIFEXITED(status) && WEXITSTATUS(status) == 0; } else { // fork 失败 perror("fork failed"); return false; } } // 辅助函数:将字符串首字母换为小写 std::string Ucar::toLowerFirstChar(const std::string& str) { if (str.empty()) return str; std::string result = str; result[0] = static_cast<char>(std::tolower(static_cast<unsigned char>(result[0]))); return result; } // 在odomCallback中更新最新里程计数据 void Ucar::odomCallback(const nav_msgs::Odometry::ConstPtr& msg) { latest_odom_ = msg; // 保存最新里程计数据 if (!position_recorded) { initial_position = msg->pose.pose.position; position_recorded = true; //ROS_INFO("初始位置记录完成: (%.3f, %.3f)", // initial_position.x, initial_position.y); } } void Ucar::moveLeftDistance(double distance_m, double linear_speed) { // 订阅里程计话题(根据实际机器人配置修改话题名) // 等待位置记录完成 position_recorded = false; while (!position_recorded && ros::ok()) { ros::spinOnce(); ros::Duration(0.1).sleep(); } // 计算理论运动时间 double duration = distance_m / linear_speed; //ROS_INFO("开始左移: 距离=%.2fm, 速度=%.2fm/s, 理论时间=%.1fs", // distance_m, linear_speed, duration); // 创建控制消息 geometry_msgs::Twist move_cmd; move_cmd.linear.y = linear_speed; // 全向移动Y轴速度控制左右移动 // 记录开始时间 auto start_time = ros::Time::now(); // 持续发送控制命令 while ((ros::Time::now() - start_time).toSec() < duration && ros::ok()) { cmd_vel_pub.publish(move_cmd); ros::Duration(0.05).sleep(); // 50ms控制周期 } // 发送停止指令 move_cmd.linear.y = 0; cmd_vel_pub.publish(move_cmd); //ROS_INFO("左移完成"); } void Ucar::moveRightDistance(double distance_m, double linear_speed) { // 等待位置记录完成 position_recorded = false; while (!position_recorded && ros::ok()) { ros::spinOnce(); ros::Duration(0.1).sleep(); } // 计算理论运动时间 double duration = distance_m / linear_speed; //ROS_INFO("开始右移: 距离=%.2fm, 速度=%.2fm/s, 理论时间=%.1fs", // distance_m, linear_speed, duration); // 创建控制消息 geometry_msgs::Twist move_cmd; move_cmd.linear.y = -linear_speed; // 全向移动Y轴速度控制左右移动 // 记录开始时间 auto start_time = ros::Time::now(); // 持续发送控制命令 while ((ros::Time::now() - start_time).toSec() < duration && ros::ok()) { cmd_vel_pub.publish(move_cmd); ros::Duration(0.05).sleep(); // 50ms控制周期 } // 发送停止指令 move_cmd.linear.y = 0; cmd_vel_pub.publish(move_cmd); //ROS_INFO("右移完成"); } void Ucar::moveForwardDistance(double distance_m, double linear_speed) { // 确保速度为正值(前进方向) if (linear_speed < 0) { ROS_WARN("前进速度应为正值,自动取绝对值"); linear_speed = fabs(linear_speed); } // 等待位置记录完成 position_recorded = false; while (!position_recorded && ros::ok()) { ros::spinOnce(); ros::Duration(0.1).sleep(); } // 计算理论运动时间 double duration = distance_m / linear_speed; //ROS_INFO("开始前进: 距离=%.2fm, 速度=%.2fm/s, 理论时间=%.1fs", // distance_m, linear_speed, duration); // 创建控制消息 geometry_msgs::Twist move_cmd; move_cmd.linear.x = linear_speed; // X轴速度控制前后移动 // 记录开始时间 auto start_time = ros::Time::now(); // 持续发送控制命令 while ((ros::Time::now() - start_time).toSec() < duration && ros::ok()) { cmd_vel_pub.publish(move_cmd); ros::Duration(0.05).sleep(); // 50ms控制周期 } // 发送停止指令 move_cmd.linear.x = 0.0; cmd_vel_pub.publish(move_cmd); ROS_INFO("前进完成"); } //输入1逆时针,输入-1顺时针 void Ucar::rotateCounterClockwise5Degrees(int a) { // 设置旋参数 const double angular_speed = 0.2; // rad/s (约11.5度/秒) const double degrees = 5.0; const double duration = degrees * (M_PI / 180.0) / angular_speed; // 约0.436秒 //if(a==1) //ROS_INFO("开始逆时针旋5度: 速度=%.2f rad/s, 时间=%.3f秒", // angular_speed, duration); //else if(a==-1) //ROS_INFO("开始顺时针旋5度: 速度=%.2f rad/s, 时间=%.3f秒", // angular_speed, duration); // 创建控制消息 geometry_msgs::Twist twist_msg; twist_msg.angular.z = angular_speed*a; // 正值表示逆时针 // 记录开始时间 auto start_time = ros::Time::now(); // 持续发送控制命令 while ((ros::Time::now() - start_time).toSec() < duration && ros::ok()) { cmd_vel_pub.publish(twist_msg); ros::Duration(0.05).sleep(); // 20Hz控制周期 } // 发送停止命令(确保接收) twist_msg.angular.z = 0.0; for (int i = 0; i < 3; i++) { cmd_vel_pub.publish(twist_msg); ros::Duration(0.02).sleep(); } if(a==1) ROS_INFO("逆时针旋5度完成"); else if (a==-1) ROS_INFO("顺时针旋5度完成"); } void Ucar::rotateCounterClockwise360Degrees() { // 设置旋参数 const double angular_speed = 1; // rad/s (约11.5度/秒) const double degrees = 360.0; const double duration = degrees * (M_PI / 180.0) / angular_speed; // 约0.436秒 // 创建控制消息 geometry_msgs::Twist twist_msg; twist_msg.angular.z = angular_speed; // 正值表示逆时针 // 记录开始时间 auto start_time = ros::Time::now(); // 持续发送控制命令 while ((ros::Time::now() - start_time).toSec() < duration && ros::ok()) { cmd_vel_pub.publish(twist_msg); ros::Duration(0.05).sleep(); // 20Hz控制周期 } // 发送停止命令(确保接收) twist_msg.angular.z = 0.0; for (int i = 0; i < 3; i++) { cmd_vel_pub.publish(twist_msg); ros::Duration(0.02).sleep(); } } void Ucar::left_and_right_move_old(){ rfbot_yolov8_ros::DetectGood srv; while((find_object_x2_old+find_object_x1_old)/2>324){ if((find_object_x2_old+find_object_x1_old)>340) moveLeftDistance(0.15,0.1);//控制小车往左移动15cm else moveLeftDistance(0.05,0.1);//控制小车往左移动5cm moveForwardDistance(0.05,0.1); Ucar::result_client_.call(srv); for (size_t j = 0; j < srv.response.goodName.size(); ++j) { ROS_INFO("响应结果:"); ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", srv.response.u1[j], srv.response.v1[j], srv.response.u2[j], srv.response.v2[j]); //ROS_INFO("置信度: %f", srv.response.confidence[j]); find_object_x1_old=srv.response.u1[0]; find_object_y1_old=srv.response.v1[0]; find_object_x2_old=srv.response.u2[0]; find_object_y2_old=srv.response.v2[0]; } ROS_INFO("当前目标中心横坐标为:%d",(find_object_x2_old+find_object_x1_old)/2); } while((find_object_x2_old+find_object_x1_old)/2<316){ if((find_object_x2_old+find_object_x1_old)<300) moveRightDistance(0.15,0.1);//控制小车往右移动15cm else moveRightDistance(0.05,0.1);//控制小车往右移动5cm moveForwardDistance(0.05,0.1); Ucar::result_client_.call(srv); for (size_t j = 0; j < srv.response.goodName.size(); ++j) { ROS_INFO("响应结果:"); ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", srv.response.u1[j], srv.response.v1[j], srv.response.u2[j], srv.response.v2[j]); //ROS_INFO("置信度: %f", srv.response.confidence[j]); find_object_x1_old=srv.response.u1[0]; find_object_y1_old=srv.response.v1[0]; find_object_x2_old=srv.response.u2[0]; find_object_y2_old=srv.response.v2[0]; } ROS_INFO("当前目标中心横坐标为:%d",(find_object_x2_old+find_object_x1_old)/2); } ROS_INFO("左右移动完成"); } //前进函数(涵盖第二种停靠算法) void Ucar::go(){ rfbot_yolov8_ros::DetectGood srv; while(1){ moveForwardDistance(0.05,0.1);//控制小车前进 Ucar::result_client_.call(srv); for (size_t j = 0; j < srv.response.goodName.size(); ++j) { ROS_INFO("响应结果:"); ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", srv.response.u1[j], srv.response.v1[j], srv.response.u2[j], srv.response.v2[j]); //ROS_INFO("置信度: %f", srv.response.confidence[j]); find_object_x1_new=srv.response.u1[0]; find_object_y1_new=srv.response.v1[0]; find_object_x2_new=srv.response.u2[0]; find_object_y2_new=srv.response.v2[0]; } //图像左边先到达边线——>逆时针往右 if(find_object_x2_new==640&&find_object_y1_new==0&&find_object_x1_new!=0&&find_object_y2_new>=350){ if(find_object_x1_new>240||(find_object_x2_new-find_object_x1_new)<=(find_object_y2_new-find_object_y1_new)){ rotateCounterClockwise5Degrees(1); rotateCounterClockwise5Degrees(1); rotateCounterClockwise5Degrees(1); rotateCounterClockwise5Degrees(1); rotateCounterClockwise5Degrees(1); moveRightDistance(0.35,0.1); ROS_INFO("大摆头"); break; } else if(find_object_x1_new>120){ rotateCounterClockwise5Degrees(1); rotateCounterClockwise5Degrees(1); rotateCounterClockwise5Degrees(1); moveRightDistance(0.25,0.1); ROS_INFO("中摆头"); break; } else{ rotateCounterClockwise5Degrees(1);//逆时针 moveRightDistance(0.1,0.1); ROS_INFO("小摆头"); break; } } //图像右边先到达边线——>顺时针往左 else if(find_object_x1_new==0&&find_object_y1_new==0&&find_object_x2_new!=640&&find_object_y2_new>=350){ if(find_object_x2_new<400||(find_object_x2_new-find_object_x1_new)<=(find_object_y2_new-find_object_y1_new)){ rotateCounterClockwise5Degrees(-1); rotateCounterClockwise5Degrees(-1); rotateCounterClockwise5Degrees(-1); rotateCounterClockwise5Degrees(-1); rotateCounterClockwise5Degrees(-1); moveLeftDistance(0.35,0.1); ROS_INFO("大摆头"); break; } else if(find_object_x2_new<520){ rotateCounterClockwise5Degrees(-1); rotateCounterClockwise5Degrees(-1); rotateCounterClockwise5Degrees(-1); moveLeftDistance(0.25,0.1); ROS_INFO("中摆头"); break; } else{ rotateCounterClockwise5Degrees(-1);//顺时针 moveLeftDistance(0.1,0.1); ROS_INFO("小摆头"); break; } } } } void Ucar::visualservo(){ rfbot_yolov8_ros::DetectGood srv; //左移右移 left_and_right_move_old(); //提取长宽比 Ucar::result_client_.call(srv); for (size_t j = 0; j < srv.response.goodName.size(); ++j) { ROS_INFO("响应结果:"); ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", srv.response.u1[j], srv.response.v1[j], srv.response.u2[j], srv.response.v2[j]); //ROS_INFO("置信度: %f", srv.response.confidence[j]); find_object_x1_old=srv.response.u1[0]; find_object_y1_old=srv.response.v1[0]; find_object_x2_old=srv.response.u2[0]; find_object_y2_old=srv.response.v2[0]; } changkuanbi_old=(find_object_x2_old-find_object_x1_old)/(find_object_y2_old-find_object_y1_old); ROS_INFO("长宽比为:%f",changkuanbi_old);go(); if(find_object=="dessert1") playAudioFile("/home/ucar/ucar_ws/src/wav/kl.wav"); if(find_object=="dessert2") playAudioFile("/home/ucar/ucar_ws/src/wav/dg.wav"); if(find_object=="dessert3") playAudioFile("/home/ucar/ucar_ws/src/wav/nn.wav"); if(find_object=="vegetable1") playAudioFile("/home/ucar/ucar_ws/src/wav/lj.wav"); if(find_object=="vegetable2") playAudioFile("/home/ucar/ucar_ws/src/wav/xhs.wav"); if(find_object=="vegetable3") playAudioFile("/home/ucar/ucar_ws/src/wav/td.wav"); if(find_object=="fruit1") playAudioFile("/home/ucar/ucar_ws/src/wav/xj.wav"); if(find_object=="fruit2") playAudioFile("/home/ucar/ucar_ws/src/wav/pg.wav"); if(find_object=="fruit3") playAudioFile("/home/ucar/ucar_ws/src/wav/xg.wav"); // if(abs(changkuanbi_old-1.666666667)<0.05){ // ROS_INFO("比较接近16:9,不需要旋"); // //前进 // go(); // } // else { // //先逆时针个10度 // rotateCounterClockwise5Degrees(1); // rotateCounterClockwise5Degrees(1); // Ucar::result_client_.call(srv); // for (size_t j = 0; j < srv.response.goodName.size(); ++j) { // ROS_INFO("响应结果:"); // ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", // srv.response.u1[j], // srv.response.v1[j], // srv.response.u2[j], // srv.response.v2[j]); // //ROS_INFO("置信度: %f", srv.response.confidence[j]); // find_object_x1_new=srv.response.u1[0]; // find_object_y1_new=srv.response.v1[0]; // find_object_x2_new=srv.response.u2[0]; // find_object_y2_new=srv.response.v2[0]; // } // changkuanbi_new=(find_object_x2_new-find_object_x1_new)/(find_object_y2_new-find_object_y1_new); // ROS_INFO("长宽比为:%f",changkuanbi_new); // if(changkuanbi_new<changkuanbi_old)//方向错了 // { // while(abs(changkuanbi_new-1.666666667)>0.4)//不准就再 // { // rotateCounterClockwise5Degrees(-1); // Ucar::result_client_.call(srv); // for (size_t j = 0; j < srv.response.goodName.size(); ++j) { // ROS_INFO("响应结果:"); // ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", // srv.response.u1[j], // srv.response.v1[j], // srv.response.u2[j], // srv.response.v2[j]); // //ROS_INFO("置信度: %f", srv.response.confidence[j]); // find_object_x1_new=srv.response.u1[0]; // find_object_y1_new=srv.response.v1[0]; // find_object_x2_new=srv.response.u2[0]; // find_object_y2_new=srv.response.v2[0]; // //保持正对目标 // while((find_object_x2_new+find_object_x1_new)/2>324) // {moveLeftDistance(0.05,0.1);//控制小车往左移动5cm // Ucar::result_client_.call(srv); // for (size_t j = 0; j < srv.response.goodName.size(); ++j) { // ROS_INFO("响应结果:"); // ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", // srv.response.u1[j], // srv.response.v1[j], // srv.response.u2[j], // srv.response.v2[j]); // //ROS_INFO("置信度: %f", srv.response.confidence[j]); // find_object_x1_new=srv.response.u1[0]; // find_object_y1_new=srv.response.v1[0]; // find_object_x2_new=srv.response.u2[0]; // find_object_y2_new=srv.response.v2[0]; // } // changkuanbi_new=(find_object_x2_new-find_object_x1_new)/(find_object_y2_new-find_object_y1_new); // ROS_INFO("长宽比为:%f",changkuanbi_new);} // while((find_object_x2_new+find_object_x1_new)/2<316) // {moveRightDistance(0.05,0.1);//控制小车往右移动5cm // Ucar::result_client_.call(srv); // for (size_t j = 0; j < srv.response.goodName.size(); ++j) { // ROS_INFO("响应结果:"); // ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", // srv.response.u1[j], // srv.response.v1[j], // srv.response.u2[j], // srv.response.v2[j]); // //ROS_INFO("置信度: %f", srv.response.confidence[j]); // find_object_x1_new=srv.response.u1[0]; // find_object_y1_new=srv.response.v1[0]; // find_object_x2_new=srv.response.u2[0]; // find_object_y2_new=srv.response.v2[0]; // } // changkuanbi_new=(find_object_x2_new-find_object_x1_new)/(find_object_y2_new-find_object_y1_new); // ROS_INFO("长宽比为:%f",changkuanbi_new);} // } // } // } // else{//方向对了 // while(abs(changkuanbi_new-1.666666667)>0.4)//不准就再 // { // rotateCounterClockwise5Degrees(1); // //保持正对目标 // while((find_object_x2_new+find_object_x1_new)/2>324) moveLeftDistance(0.05,0.1);//控制小车往左移动5cm // while((find_object_x2_new+find_object_x1_new)/2<316) moveRightDistance(0.05,0.1);//控制小车往右移动5cm // Ucar::result_client_.call(srv); // for (size_t j = 0; j < srv.response.goodName.size(); ++j) { // ROS_INFO("响应结果:"); // ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", // srv.response.u1[j], // srv.response.v1[j], // srv.response.u2[j], // srv.response.v2[j]); // //ROS_INFO("置信度: %f", srv.response.confidence[j]); // find_object_x1_new=srv.response.u1[0]; // find_object_y1_new=srv.response.v1[0]; // find_object_x2_new=srv.response.u2[0]; // find_object_y2_new=srv.response.v2[0]; // } // changkuanbi_new=(find_object_x2_new-find_object_x1_new)/(find_object_y2_new-find_object_y1_new); // ROS_INFO("长宽比为:%f",changkuanbi_new); // } // } // //前进 // go(); // } ROS_INFO("导航完成"); ros::Duration(3000).sleep(); current_state_ = State::GAZEBO; } void Ucar::spin_to_find(){ setlocale(LC_ALL, ""); for(int i=0;i<9;i++){ navigateTo(pose_center[i]);//导航到i号点位 ros::Duration(3).sleep();//停留3秒 rfbot_yolov8_ros::DetectGood srv; Ucar::result_client_.call(srv); for (size_t j = 0; j < srv.response.goodName.size(); ++j) { ROS_INFO("响应结果:"); ROS_INFO("位置: x1: %d, y1: %d, x2: %d, y2: %d", srv.response.u1[j], srv.response.v1[j], srv.response.u2[j], srv.response.v2[j]); //ROS_INFO("置信度: %f", srv.response.confidence[j]); find_object_x1_old=srv.response.u1[0]; find_object_y1_old=srv.response.v1[0]; find_object_x2_old=srv.response.u2[0]; find_object_y2_old=srv.response.v2[0]; pose_center_object[i] = srv.response.goodName[0]; //看看这个方向有没有目标 } if(pose_center_object[i].find(target_object)!=std::string::npos&&(find_object_x2_old-find_object_x1_old)>=(find_object_y2_old-find_object_y1_old)) {ROS_INFO("本次任务目标识别成功"); find_object=pose_center_object[i]; visualservo(); break;} else{ ROS_INFO("本次任务目标识别失败,尝试下一个目标"); continue; } } } void Ucar::gazebo(){ navigateTo(pose_gazebo); //主从机通信 FileTransfer ft("/home/ucar/ucar_ws/src/ucar"); ft.saveToFile(target_object, "target.txt"); //电脑的ip ft.sendTo("192.168.1.100", 8080, "/home/ucar/ucar_ws/src/ucar/target.txt", "/home/zzs/gazebo_test_ws/src/ucar2/target.txt"); try { // 示例3:等待接收文件读取内容 std::cout << "等待接收文件..." << std::endl; ft.receiveAndReadTwoStrings(8080, gazebo_object, gazebo_room); std::cout << "接收到的内容:\n" << gazebo_object << "\n"<<gazebo_room << std::endl; } catch (const std::exception& e) { std::cerr << "错误: " << e.what() << std::endl;} ros::Duration(30000).sleep();//停留3秒 } int main(int argc, char** argv) { ros::init(argc, argv, "multi_room_navigation_node"); ros::NodeHandle nh; Ucar ucar(nh); ucar.run(); return 0; } 帮我修改上述代码,其中导航到nh_.param("pose_center_x", pose_11.position.x, 1.13); nh_.param("pose_center_y", pose_11.position.y, 3.04); nh_.param("pose_center_x", pose_12.position.x, 1.13); nh_.param("pose_center_y", pose_12.position.y, 3.04); nh_.param("pose_center_x", pose_13.position.x, 1.13); nh_.param("pose_center_y", pose_13.position.y, 3.04); nh_.param("pose_center_x", pose_14.position.x, 1.13); nh_.param("pose_center_y", pose_14.position.y, 3.04); nh_.param("pose_center_x", pose_15.position.x, 1.13); nh_.param("pose_center_y", pose_15.position.y, 3.04);时候时,删除原来旋十五度停下的情况,改成每个点都停留,然后开始原地旋,每旋0.2秒停顿0.5秒,然后旋360度之后,前往下一个点,在其中任意一个点识别到目标时,就不前往之后的点而是直接进入视觉定位,最后直接前往 nh_.param("pose_center_x", pose_16.position.x, 1.13); nh_.param("pose_center_y", pose_16.position.y, 3.04);
最新发布
08-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值