From 6f1d837b46ed6b8ba9d94721006ff3e9549b3823 Mon Sep 17 00:00:00 2001 From: Keviniscoding666 <1150934237@qq.com> Date: Thu, 12 Nov 2026 16:42:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=8F=8C=E9=9B=B7=E8=BE=BE?= =?UTF-8?q?=E4=BB=BF=E7=9C=9F=E4=B8=8E=E8=87=AA=E5=8A=A8=E4=B8=8A=E5=9D=A1?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=EF=BC=8C=E6=9B=B4=E6=96=B0=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入下置 MID360 前向补盲与双雷达融合,修复北侧走廊导航、坐标适配和数据时效处理。 实现接近坡脚、姿态辅助上坡与平台停车任务,增加独立快模式、命令仲裁及失效停车保护,保留测试与分阶段验收记录。 同步实车尺寸及雷达安装参数,调整 Gazebo 红方默认视角,完善根目录 README、仿真使用和实车迁移说明。 --- README.md | 25 +- .../perception/Point-LIO/include/common_lib.h | 6 +- .../include/sensor_time_rate_limiter.h | 5 +- .../Point-LIO/include/sensor_timestamp.h | 17 + .../perception/Point-LIO/src/laserMapping.cpp | 11 +- minco_ws/src/scan/plan_env/CMakeLists.txt | 6 + .../include/plan_env/cloud_ray_origins.h | 48 + .../scan/plan_env/include/plan_env/grid_map.h | 8 +- minco_ws/src/scan/plan_env/package.xml | 1 + minco_ws/src/scan/plan_env/src/grid_map.cpp | 96 +- .../plan_env/test/test_cloud_ray_origins.cpp | 91 + minco_ws/src/scan/plan_manage/CMakeLists.txt | 1 + .../src/closed_loop_controller.cpp | 59 +- .../scan/plan_manage/src/planner_manager.cpp | 13 +- .../scan/plan_manage/src/scan_replan_fsm.cpp | 6 +- .../test/test_controller_prediction.py | 89 + .../test/test_rectangle_footprint.cpp | 5 + .../plan_manage/test/test_rectangle_fsm.py | 27 +- .../step_blocker/step_blocker_node.py | 44 +- sim_ws/README.md | 65 +- sim_ws/docs/DUAL_MID360.md | 168 ++ sim_ws/docs/DUAL_SAFETY.md | 152 + sim_ws/docs/DUAL_TERRAIN.md | 142 + sim_ws/docs/HANDOFF.md | 89 +- sim_ws/docs/NORTH_CORRIDOR.md | 116 + sim_ws/docs/P2_OMNI.md | 3 + sim_ws/docs/P3_P4_NAVIGATION.md | 18 +- sim_ws/docs/P5_NARROW.md | 7 +- sim_ws/docs/P5_SLOPE.md | 7 +- sim_ws/docs/P6_LIO_TERRAIN.md | 4 + sim_ws/docs/P6_LOCALIZATION.md | 8 +- sim_ws/docs/P6_NAVIGATION.md | 8 +- sim_ws/docs/RAMP_MISSION.md | 431 +++ sim_ws/docs/ROBUSTNESS.md | 9 +- sim_ws/docs/USER_GUIDE.md | 47 +- sim_ws/docs/VEHICLE_GEOMETRY.md | 96 + .../docs/validation/20261110_dual_mid360.json | 369 +++ .../docs/validation/20261110_dual_safety.json | 268 ++ .../20261110_dual_side_evidence.svg | 2542 +++++++++++++++++ .../validation/20261110_dual_terrain.json | 572 ++++ sim_ws/docs/validation/20261110_geometry.json | 1232 ++++++++ .../validation/20261110_north_corridor.json | 217 ++ .../docs/validation/20261112_ramp_fast.json | 1003 +++++++ .../docs/validation/20261112_ramp_health.json | 842 ++++++ .../validation/20261112_ramp_mission.json | 1049 +++++++ .../validation/20261112_ramp_mission_r2.json | 654 +++++ sim_ws/src/br_sim_bringup/CMakeLists.txt | 2 +- .../src/br_sim_bringup/config/dual_lidar.yaml | 20 + .../config/dual_lidar_terrain.yaml | 69 + .../br_sim_bringup/config/lio_navigation.yaml | 5 +- .../br_sim_bringup/config/lio_terrain.yaml | 4 +- .../launch/mid360_sim.launch.py | 17 +- .../launch/ramp_mission.launch.py | 71 + .../src/br_sim_bringup/launch/rviz.launch.py | 13 +- .../br_sim_bringup/launch/scan_lio.launch.py | 8 +- .../launch/scan_lio_navigation.launch.py | 16 +- .../launch/scan_lio_split.launch.py | 4 +- .../launch/scan_lio_terrain.launch.py | 14 +- sim_ws/src/br_sim_bringup/package.xml | 2 + .../br_sim_bringup/rviz/scan_dual_lio.rviz | 158 + .../scripts/dual_lidar_fusion.py | 216 ++ .../scripts/lio_navigation_adapter.py | 72 +- .../scripts/mission_command_mux.py | 99 + .../br_sim_bringup/scripts/ramp_mission.py | 324 +++ .../br_sim_bringup/scripts/scan_cloud_gate.py | 7 +- .../SOURCE_PROVENANCE.md | 34 +- .../launch/mid360_sensors.launch.py | 5 +- .../launch/robocon_robot.launch.py | 29 +- .../scripts/mid360_control.py | 20 +- .../scripts/sim_data_adapter.py | 4 +- .../scripts/spawn_existing_world.py | 30 +- .../sdf/r2027_omni_robot.sdf.xacro | 86 +- .../src/contact_drive.cpp | 12 +- .../src/mid360_sensor.cpp | 42 +- .../robocon_gazebo/worlds/robocon_robot.world | 10 + sim_ws/tools/check_dual_lidar.py | 332 +++ sim_ws/tools/check_lio_corridor.py | 138 + sim_ws/tools/check_lio_terrain.py | 177 +- sim_ws/tools/check_low_obstacles.py | 6 +- sim_ws/tools/check_mid360.py | 28 +- sim_ws/tools/check_omni_robot.py | 23 +- sim_ws/tools/check_point_lio.py | 2 +- sim_ws/tools/check_ramp_mission.py | 256 ++ sim_ws/tools/check_scan_navigation.py | 5 +- sim_ws/tools/check_sensor_noise.py | 6 +- sim_ws/tools/check_sim_lidar.py | 4 +- sim_ws/tools/check_slope_drive.py | 2 +- sim_ws/tools/check_vehicle_geometry.py | 69 + sim_ws/tools/test_dual_acceptance.py | 123 + sim_ws/tools/test_dual_lidar.py | 187 ++ sim_ws/tools/test_dual_terrain.py | 78 + sim_ws/tools/test_lio_navigation_adapter.py | 104 +- sim_ws/tools/test_lio_stamp.cpp | 30 + sim_ws/tools/test_ramp_mission.py | 216 ++ sim_ws/tools/test_spawn_existing_world.py | 56 + 95 files changed, 13667 insertions(+), 244 deletions(-) create mode 100644 minco_ws/src/perception/Point-LIO/include/sensor_timestamp.h create mode 100644 minco_ws/src/scan/plan_env/include/plan_env/cloud_ray_origins.h create mode 100644 minco_ws/src/scan/plan_env/test/test_cloud_ray_origins.cpp create mode 100644 minco_ws/src/scan/plan_manage/test/test_controller_prediction.py create mode 100644 sim_ws/docs/DUAL_MID360.md create mode 100644 sim_ws/docs/DUAL_SAFETY.md create mode 100644 sim_ws/docs/DUAL_TERRAIN.md create mode 100644 sim_ws/docs/NORTH_CORRIDOR.md create mode 100644 sim_ws/docs/RAMP_MISSION.md create mode 100644 sim_ws/docs/VEHICLE_GEOMETRY.md create mode 100644 sim_ws/docs/validation/20261110_dual_mid360.json create mode 100644 sim_ws/docs/validation/20261110_dual_safety.json create mode 100644 sim_ws/docs/validation/20261110_dual_side_evidence.svg create mode 100644 sim_ws/docs/validation/20261110_dual_terrain.json create mode 100644 sim_ws/docs/validation/20261110_geometry.json create mode 100644 sim_ws/docs/validation/20261110_north_corridor.json create mode 100644 sim_ws/docs/validation/20261112_ramp_fast.json create mode 100644 sim_ws/docs/validation/20261112_ramp_health.json create mode 100644 sim_ws/docs/validation/20261112_ramp_mission.json create mode 100644 sim_ws/docs/validation/20261112_ramp_mission_r2.json create mode 100644 sim_ws/src/br_sim_bringup/config/dual_lidar.yaml create mode 100644 sim_ws/src/br_sim_bringup/config/dual_lidar_terrain.yaml create mode 100644 sim_ws/src/br_sim_bringup/launch/ramp_mission.launch.py create mode 100644 sim_ws/src/br_sim_bringup/rviz/scan_dual_lio.rviz create mode 100755 sim_ws/src/br_sim_bringup/scripts/dual_lidar_fusion.py create mode 100755 sim_ws/src/br_sim_bringup/scripts/mission_command_mux.py create mode 100755 sim_ws/src/br_sim_bringup/scripts/ramp_mission.py create mode 100644 sim_ws/tools/check_dual_lidar.py create mode 100644 sim_ws/tools/check_lio_corridor.py create mode 100644 sim_ws/tools/check_ramp_mission.py create mode 100644 sim_ws/tools/check_vehicle_geometry.py create mode 100644 sim_ws/tools/test_dual_acceptance.py create mode 100644 sim_ws/tools/test_dual_lidar.py create mode 100644 sim_ws/tools/test_dual_terrain.py create mode 100644 sim_ws/tools/test_lio_stamp.cpp create mode 100644 sim_ws/tools/test_ramp_mission.py create mode 100644 sim_ws/tools/test_spawn_existing_world.py diff --git a/README.md b/README.md index e2abc5b..4053987 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,14 @@ RoboCon 2027 赛季 BR 机器人的上位机代码。仓库里有**四套**定 复用方案 A 的原 Point-LIO、SCAN 和闭环控制器,支持全向横移;不是新增一套导航算法。 已有真值导航、MID360/IMU 估计导航和两侧指定坡道入口,含仿真时间、失效停车与独立物理验收。 +新增下 MID360 的平地补盲模式使用 `scan_lio.launch.py dual_lidar:=true`;安装位置、前半圈裁剪、 +验收路线及斜侧向低障碍的限制见 [双 MID360 说明](sim_ws/docs/DUAL_MID360.md)。 +双雷达指定坡道的独立入口、三维路径及验收范围见 [双雷达坡道说明](sim_ws/docs/DUAL_TERRAIN.md)。 +自动执行默认起点接近坡脚、专用控制上坡并停在平台的任务入口,见 [分段上坡任务](sim_ws/docs/RAMP_MISSION.md)。 +其中 `profile:=fast` 是独立的 20 秒目标仿真配置;理想、轻度噪声两次完整路线分别为 **19.42 / 19.46 秒仿真时间**, +计时从初始化后任务开始到整车上平台并停稳确认完成,不含 Gazebo 加载与定位初始化。 +默认模式及实车入口保持原配置,使用前请阅读文档中的减速区和验收边界;上述结果不代表实车成绩或每次运行的保证。 + **第一次使用先读 [仿真使用与实车迁移指南](sim_ws/docs/USER_GUIDE.md)**,包括手动发目标、 带窗口自动测试、RViz、关闭重启和实车分阶段接入。以下命令在仓库根目录执行。 @@ -38,8 +46,22 @@ export GAZEBO_MASTER_URI=http://127.0.0.1:11355 ros2 launch br_sim_bringup scan_lio.launch.py ``` +Gazebo 默认从红方(世界坐标 +Y)外侧上方俯看红方半场,仍可用鼠标调整视角。 +上述手动入口默认单雷达,添加 `dual_lidar:=true` 启用双雷达补盲。 + +若要自动从默认起点行驶到北侧坡脚、上坡并停在平台,在相同环境下选择下面**其中一条**: +本入口自带双雷达、Gazebo 和 RViz,初始化完成后自动行驶,无需再发目标。 +**先关闭其他仿真入口,等待启动终端退出;各入口不能同时运行。** + +```bash +# 标准模式 +ros2 launch br_sim_bringup ramp_mission.launch.py autostart:=true +# 快模式:已知北侧路线的 20 秒目标仿真配置 +ros2 launch br_sim_bringup ramp_mission.launch.py profile:=fast autostart:=true +``` + 若希望脚本自动发目标、验证路线,在相同环境下改用以下命令。 -**先关闭手动 launch,等待启动终端完全退出;两种方式互斥。** +**先关闭其他 launch,等待启动终端完全退出。** ```bash /usr/bin/python3 sim_ws/tools/check_lio_navigation.py --gui \ @@ -68,6 +90,7 @@ ros2 launch br_sim_bringup scan_lio.launch.py | 跑方案 C,或看它的评估现状 | [`sru_ws/docs/USAGE.md`](sru_ws/docs/USAGE.md)、[`SOLUTION_C_ASSESSMENT.md`](sru_ws/docs/SOLUTION_C_ASSESSMENT.md) | | 跑方案 D(建图 / 定位导航 / 行为树) | [`nav2_ws/README.md`](nav2_ws/README.md) | | 在笔记本测试原导航程序 | [`sim_ws/README.md`](sim_ws/README.md) | +| 从起点自动上坡停车,或运行 20 秒目标快模式 | [分段上坡任务](sim_ws/docs/RAMP_MISSION.md) | | 学习仿真操作,并把开发结果迁移到实车 | [使用与迁移指南](sim_ws/docs/USER_GUIDE.md) | | 知道哪些东西不入库 | [`.gitignore`](.gitignore) | diff --git a/minco_ws/src/perception/Point-LIO/include/common_lib.h b/minco_ws/src/perception/Point-LIO/include/common_lib.h index ea7ca0e..ff5e615 100755 --- a/minco_ws/src/perception/Point-LIO/include/common_lib.h +++ b/minco_ws/src/perception/Point-LIO/include/common_lib.h @@ -13,6 +13,7 @@ #include #include "time_batching.h" +#include "sensor_timestamp.h" using namespace std; using namespace Eigen; @@ -205,10 +206,7 @@ inline double get_time_sec(const builtin_interfaces::msg::Time & time) inline rclcpp::Time get_ros_time(double timestamp) { - int32_t sec = std::floor(timestamp); - auto nanosec_d = (timestamp - std::floor(timestamp)) * 1e9; - uint32_t nanosec = nanosec_d; - return rclcpp::Time(sec, nanosec); + return rclcpp::Time(point_lio::rounded_sensor_time_ns(timestamp), RCL_ROS_TIME); } #endif diff --git a/minco_ws/src/perception/Point-LIO/include/sensor_time_rate_limiter.h b/minco_ws/src/perception/Point-LIO/include/sensor_time_rate_limiter.h index 2a0592a..cc9405e 100644 --- a/minco_ws/src/perception/Point-LIO/include/sensor_time_rate_limiter.h +++ b/minco_ws/src/perception/Point-LIO/include/sensor_time_rate_limiter.h @@ -9,7 +9,7 @@ namespace point_lio class SensorTimeRateLimiter { public: - bool should_publish(double sensor_time, double frequency_hz) + bool should_publish(double sensor_time, double frequency_hz, bool scan_end = false) { if (!std::isfinite(sensor_time) || !std::isfinite(frequency_hz) || frequency_hz <= 0.0) { return false; @@ -23,7 +23,8 @@ public: constexpr double kTimeEpsilon = 1.0e-9; const double publish_period = 1.0 / frequency_hz; - if (sensor_time - last_publish_sensor_time_ + kTimeEpsilon < publish_period) { + if (sensor_time == last_publish_sensor_time_ || + (!scan_end && sensor_time - last_publish_sensor_time_ + kTimeEpsilon < publish_period)) { return false; } diff --git a/minco_ws/src/perception/Point-LIO/include/sensor_timestamp.h b/minco_ws/src/perception/Point-LIO/include/sensor_timestamp.h new file mode 100644 index 0000000..b5c9c8e --- /dev/null +++ b/minco_ws/src/perception/Point-LIO/include/sensor_timestamp.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace point_lio +{ +// Convert the represented sensor seconds to the nearest ROS nanosecond. +// Truncating the fractional part can lose 1 ns at an exact scan endpoint, +// making a downstream strict pose bracket wait for the next complete scan. +inline std::int64_t rounded_sensor_time_ns(double timestamp) +{ + const auto seconds = static_cast(std::floor(timestamp)); + const auto fraction = std::llround((timestamp - std::floor(timestamp)) * 1e9); + return seconds * INT64_C(1000000000) + fraction; +} +} // namespace point_lio diff --git a/minco_ws/src/perception/Point-LIO/src/laserMapping.cpp b/minco_ws/src/perception/Point-LIO/src/laserMapping.cpp index 8085091..5e01ea6 100644 --- a/minco_ws/src/perception/Point-LIO/src/laserMapping.cpp +++ b/minco_ws/src/perception/Point-LIO/src/laserMapping.cpp @@ -43,6 +43,7 @@ const float MOV_THRESHOLD = 1.5f; string root_dir = ROOT_DIR; bool init_map = false, flg_first_scan = true; +static bool publish_scan_end_odom = false; bool flg_reset = false, flg_exit = false; @@ -632,12 +633,12 @@ template void set_posestamp(T & out) // 发布里程计话题 void publish_odometry(const rclcpp::Publisher::SharedPtr & pubOdomAftMapped, - std::shared_ptr & tf_br) + std::shared_ptr & tf_br, bool scan_end = false) { static point_lio::SensorTimeRateLimiter odom_rate_limiter; const double odom_sensor_time = publish_odometry_without_downsample ? time_current : lidar_end_time; - if (!odom_rate_limiter.should_publish(odom_sensor_time, orig_odom_freq)) { + if (!odom_rate_limiter.should_publish(odom_sensor_time, orig_odom_freq, scan_end)) { return; } @@ -873,6 +874,8 @@ private: icp_client_ = create_client("/gicp_recall"); readParameters(*this); + publish_scan_end_odom = declare_parameter("odometry.publish_scan_end", false); + RCLCPP_INFO(get_logger(), "Scan-end estimated odometry publication: %s", publish_scan_end_odom ? "enabled" : "disabled"); if (blind_center_enable && blind_center.size() == 3U) { p_pre->setBlindCenter(blind_center[0], blind_center[1], blind_center[2]); RCLCPP_INFO(get_logger(), "[Point-LIO] Blind center enabled: [%.3f, %.3f, %.3f]", @@ -1751,6 +1754,10 @@ void LaserMappingNode::processingLoop() /******* Publish odometry downsample *******/ if (!publish_odometry_without_downsample) { publish_odometry(pub_odom_aft_mapped_, tf_broadcaster_); + } else if (publish_scan_end_odom) { + // Publish the actual final processed filter state at time_current. + // Never relabel an earlier state with lidar_end_time or extrapolate. + publish_odometry(pub_odom_aft_mapped_, tf_broadcaster_, true); } /*** add the feature points to map ***/ diff --git a/minco_ws/src/scan/plan_env/CMakeLists.txt b/minco_ws/src/scan/plan_env/CMakeLists.txt index 23d4aae..eb66b1e 100644 --- a/minco_ws/src/scan/plan_env/CMakeLists.txt +++ b/minco_ws/src/scan/plan_env/CMakeLists.txt @@ -41,4 +41,10 @@ ament_export_targets(export_plan_env HAS_LIBRARY_TARGET) ament_export_dependencies( cv_bridge Eigen3 geometry_msgs message_filters nav_msgs pcl_conversions rclcpp sensor_msgs tf2 tf2_ros visualization_msgs) +if(BUILD_TESTING) + find_package(ament_cmake_gtest REQUIRED) + ament_add_gtest(test_cloud_ray_origins test/test_cloud_ray_origins.cpp) + target_link_libraries(test_cloud_ray_origins plan_env) + ament_target_dependencies(test_cloud_ray_origins rclcpp sensor_msgs nav_msgs) +endif() ament_package() diff --git a/minco_ws/src/scan/plan_env/include/plan_env/cloud_ray_origins.h b/minco_ws/src/scan/plan_env/include/plan_env/cloud_ray_origins.h new file mode 100644 index 0000000..2c0f5c0 --- /dev/null +++ b/minco_ws/src/scan/plan_env/include/plan_env/cloud_ray_origins.h @@ -0,0 +1,48 @@ +#ifndef PLAN_ENV_CLOUD_RAY_ORIGINS_H +#define PLAN_ENV_CLOUD_RAY_ORIGINS_H +#include +#include +#include +#include +#include + +namespace plan_env { +// Optional strict schema for multi-lidar WORLD clouds. The original one-origin +// path never constructs this reader and retains its existing contract. +class CloudRayOrigins { + public: + explicit CloudRayOrigins(const sensor_msgs::msg::PointCloud2 & cloud) : cloud_(cloud) { + if (!cloud.point_step || uint64_t(cloud.width)*cloud.point_step>cloud.row_step || + uint64_t(cloud.row_step)*cloud.height>cloud.data.size()) + throw std::invalid_argument("truncated cloud layout"); + const char * names[]={"origin_x","origin_y","origin_z"}; + for (unsigned axis=0;axis<3;++axis) { + unsigned found=0; + for (const auto & field:cloud.fields) if (field.name==names[axis]) { + if (field.datatype!=sensor_msgs::msg::PointField::FLOAT32 || field.count!=1 || + uint64_t(field.offset)+sizeof(float)>cloud.point_step) + throw std::invalid_argument("invalid per-point origin field"); + offsets_[axis]=field.offset;++found; + } + if (found!=1) throw std::invalid_argument("missing or duplicated per-point origin field"); + } + } + std::array at(std::size_t row,std::size_t column) const { + if (row>=cloud_.height || column>=cloud_.width) throw std::out_of_range("origin point index"); + std::array values; + const uint16_t one=1;const bool host_big=*reinterpret_cast(&one)==0; + for (unsigned axis=0;axis<3;++axis) { + uint32_t bits; + std::memcpy(&bits,cloud_.data.data()+row*cloud_.row_step+column*cloud_.point_step+offsets_[axis],4); + if (cloud_.is_bigendian!=host_big) + bits=((bits&0xff)<<24)|((bits&0xff00)<<8)|((bits>>8)&0xff00)|(bits>>24); + std::memcpy(&values[axis],&bits,4); + } + return values; + } + private: + const sensor_msgs::msg::PointCloud2 & cloud_; + std::array offsets_{}; +}; +} // namespace plan_env +#endif diff --git a/minco_ws/src/scan/plan_env/include/plan_env/grid_map.h b/minco_ws/src/scan/plan_env/include/plan_env/grid_map.h index bceb785..ba0b633 100644 --- a/minco_ws/src/scan/plan_env/include/plan_env/grid_map.h +++ b/minco_ws/src/scan/plan_env/include/plan_env/grid_map.h @@ -94,7 +94,8 @@ struct MappingParameters { /* mapping sensor input */ string sensor_type_; - bool cloud_is_world_; + bool cloud_is_world_; + bool cloud_has_ray_origins_{false}; bool need_extrinsic_; Eigen::Matrix4d lidar_extrinsic_; Eigen::Matrix4d depth_extrinsic_; @@ -132,7 +133,8 @@ struct MappingData { // depth image projected point cloud - vector proj_points_; + vector proj_points_; + vector proj_ray_origins_; int proj_points_cnt; // flag buffers for speeding up raycasting @@ -194,6 +196,8 @@ public: void setFootprintHeading(double yaw); int getRectangleOccupancy(Eigen::Vector3d pos, double yaw, double extra_margin = 0.0); bool rotationIsFree(Eigen::Vector3d pos, double from, double to, double extra_margin = 0.0); + // 0 = complete sweep free, 1 = observed collision, -1 = incomplete map. + int rotationOccupancy(Eigen::Vector3d pos, double from, double to, double extra_margin = 0.0); bool rectangleBufferInMap(Eigen::Vector3d pos); void publishMap(); diff --git a/minco_ws/src/scan/plan_env/package.xml b/minco_ws/src/scan/plan_env/package.xml index 1af0762..5d162ad 100644 --- a/minco_ws/src/scan/plan_env/package.xml +++ b/minco_ws/src/scan/plan_env/package.xml @@ -17,5 +17,6 @@ tf2 tf2_ros visualization_msgs + ament_cmake_gtest ament_cmake diff --git a/minco_ws/src/scan/plan_env/src/grid_map.cpp b/minco_ws/src/scan/plan_env/src/grid_map.cpp index 1a446cb..e5b393d 100644 --- a/minco_ws/src/scan/plan_env/src/grid_map.cpp +++ b/minco_ws/src/scan/plan_env/src/grid_map.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include @@ -79,6 +81,11 @@ void GridMap::initMap(rclcpp::Node *node) load_parameter(node_, "grid_map.sensor_type", mp_.sensor_type_, string("lidar")); load_parameter(node_, "grid_map.cloud_is_world", mp_.cloud_is_world_, true); + load_parameter(node_, "grid_map.cloud_has_ray_origins", mp_.cloud_has_ray_origins_, false); + bool reliable_cloud = false; + load_parameter(node_, "grid_map.cloud_reliable", reliable_cloud, false); + if (mp_.cloud_has_ray_origins_ && (!mp_.cloud_is_world_ || mp_.sensor_type_!="lidar")) + throw std::invalid_argument("per-point ray origins require world-frame lidar clouds"); load_parameter(node_, "grid_map.need_extrinsic", mp_.need_extrinsic_, true); mp_.lidar_extrinsic_ << @@ -167,8 +174,10 @@ void GridMap::initMap(rclcpp::Node *node) lidar_pose_sub_ = node_->create_subscription( "sensor_pose", rclcpp::SensorDataQoS(), std::bind(&GridMap::sensorPoseCallback, this, std::placeholders::_1)); + auto cloud_qos = rclcpp::SensorDataQoS(); + if (reliable_cloud) cloud_qos.keep_last(1).reliable(); cloud_sub_ = node_->create_subscription( - "cloud", rclcpp::SensorDataQoS(), + "cloud", cloud_qos, std::bind(&GridMap::cloudCallback, this, std::placeholders::_1)); } @@ -317,16 +326,26 @@ int GridMap::getRectangleOccupancy(Eigen::Vector3d pos, double yaw, double extra bool GridMap::rotationIsFree(Eigen::Vector3d pos, double from, double to, double extra_margin) { - if (!std::isfinite(from) || !std::isfinite(to)) return false; + return rotationOccupancy(pos, from, to, extra_margin) == 0; +} + +int GridMap::rotationOccupancy(Eigen::Vector3d pos, double from, double to, double extra_margin) +{ + if (!pos.allFinite() || !std::isfinite(from) || !std::isfinite(to) || + !std::isfinite(extra_margin) || extra_margin < 0) return -1; const double angle = footprint::angleDifference(to, from); const int steps = std::max(1, static_cast(std::ceil(std::abs(angle)/0.05))); const double radius = std::hypot(0.5*mp_.footprint_length_+mp_.footprint_margin_+extra_margin, 0.5*mp_.footprint_width_+mp_.footprint_margin_+extra_margin); // Nearest sampled angle is at most half a step away. Cover all intervening corners. const double pad = footprint::angularPadding(radius, std::abs(angle)/(2*steps)); - for (int i = 0; i <= steps; ++i) - if (getRectangleOccupancy(pos, from+angle*i/steps, extra_margin+pad) != 0) return false; - return true; + int result = 0; + for (int i = 0; i <= steps; ++i) { + const int occupied = getRectangleOccupancy(pos, from+angle*i/steps, extra_margin+pad); + if (occupied == 1) return 1; // Known collision wins over incomplete samples. + if (occupied != 0) result = -1; + } + return result; } void GridMap::resetAllMapData() @@ -681,6 +700,12 @@ void GridMap::raycastProcess() updateSlidingMap(md_.ray_pos_); md_.raycast_num_ += 1; + // Legacy flags are signed-byte tags. Keep the opt-in multi-origin cache + // within 1..127 and clear on reuse, rather than truncating the int counter. + const bool multi_origin_update=mp_.cloud_has_ray_origins_ && md_.use_cloud_update_; + const char multi_origin_tag=static_cast(1+md_.raycast_num_%127); + if (multi_origin_update && md_.raycast_num_%127==0) + std::fill(md_.flag_traverse_.begin(),md_.flag_traverse_.end(),0); int vox_idx; double length; @@ -700,28 +725,30 @@ void GridMap::raycastProcess() for (int i = 0; i < md_.proj_points_cnt; ++i) { - pt_w = md_.proj_points_[i]; + pt_w = md_.proj_points_[i]; + const bool per_point_origin = mp_.cloud_has_ray_origins_ && md_.use_cloud_update_; + const Eigen::Vector3d ray_origin = per_point_origin ? md_.proj_ray_origins_.at(i) : md_.ray_pos_; // set flag for projected point if (!isInMap(pt_w)) { - pt_w = closetPointInMap(pt_w, md_.ray_pos_); + pt_w = closetPointInMap(pt_w, ray_origin); - length = (pt_w - md_.ray_pos_).norm(); + length = (pt_w - ray_origin).norm(); if (length > mp_.max_ray_length_) { - pt_w = (pt_w - md_.ray_pos_) / length * mp_.max_ray_length_ + md_.ray_pos_; + pt_w = (pt_w - ray_origin) / length * mp_.max_ray_length_ + ray_origin; } vox_idx = setCacheOccupancy(pt_w, 0); } else { - length = (pt_w - md_.ray_pos_).norm(); + length = (pt_w - ray_origin).norm(); if (length > mp_.max_ray_length_) { - pt_w = (pt_w - md_.ray_pos_) / length * mp_.max_ray_length_ + md_.ray_pos_; + pt_w = (pt_w - ray_origin) / length * mp_.max_ray_length_ + ray_origin; vox_idx = setCacheOccupancy(pt_w, 0); } else @@ -738,11 +765,13 @@ void GridMap::raycastProcess() min_y = min(min_y, pt_w(1)); min_z = min(min_z, pt_w(2)); - // raycasting between ray origin and point + max_x = max(max_x, ray_origin.x());max_y = max(max_y, ray_origin.y());max_z = max(max_z, ray_origin.z()); + min_x = min(min_x, ray_origin.x());min_y = min(min_y, ray_origin.y());min_z = min(min_z, ray_origin.z()); + // raycasting between each physical optical origin and its endpoint if (vox_idx != INVALID_IDX) { - if (md_.flag_rayend_[vox_idx] == md_.raycast_num_) + if (!per_point_origin && md_.flag_rayend_[vox_idx] == md_.raycast_num_) { continue; } @@ -752,13 +781,25 @@ void GridMap::raycastProcess() } } - raycaster.setInput(pt_w / mp_.resolution_, md_.ray_pos_ / mp_.resolution_); + raycaster.setInput(pt_w / mp_.resolution_, ray_origin / mp_.resolution_); while (raycaster.step(ray_pt)) { Eigen::Vector3d tmp = (ray_pt + half) * mp_.resolution_; - length = (tmp - md_.ray_pos_).norm(); - + length = (tmp - ray_origin).norm(); + + // Different optical origins can share a traversed voxel and then diverge. + // Do not terminate those rays at the shared voxel; count each free voxel + // once per update so dense dual scans cannot overweight free evidence. + if (per_point_origin) { + Eigen::Vector3i index;posToIndex(tmp,index); + if (!isInMap(index)) continue; + const int address=toAddress(index); + if (md_.flag_traverse_[address]==multi_origin_tag) continue; + md_.flag_traverse_[address]=multi_origin_tag; + setCacheOccupancy(tmp,0); + continue; + } vox_idx = setCacheOccupancy(tmp, 0); if (vox_idx != INVALID_IDX) @@ -1014,6 +1055,17 @@ void GridMap::cloudCallback(const sensor_msgs::msg::PointCloud2::ConstSharedPtr return; } + std::unique_ptr origins; + if (mp_.cloud_has_ray_origins_) { + try { + if (img->header.frame_id!=mp_.frame_id_) throw std::invalid_argument("wrong multi-lidar cloud frame"); + origins=std::make_unique(*img); + } catch (const std::exception & error) { + RCLCPP_ERROR_THROTTLE(node_->get_logger(), *node_->get_clock(), 1000, + "[GridMap] rejected multi-lidar cloud: %s",error.what()); + return; // Never silently use the upper lidar origin for lower points. + } + } pcl::PointCloud latest_cloud; pcl::fromROSMsg(*img, latest_cloud); @@ -1029,7 +1081,8 @@ void GridMap::cloudCallback(const sensor_msgs::msg::PointCloud2::ConstSharedPtr updateSlidingMap(ray_pos); - md_.proj_points_cnt = 0; + md_.proj_points_cnt = 0; + md_.proj_ray_origins_.clear(); for (size_t i = 0; i < latest_cloud.points.size(); ++i) { @@ -1047,7 +1100,13 @@ void GridMap::cloudCallback(const sensor_msgs::msg::PointCloud2::ConstSharedPtr const Eigen::Vector3d pt_sensor(pt.x, pt.y, pt.z); pt_world = sensor_r * pt_sensor + ray_pos; } - const Eigen::Vector3d devi = pt_world - ray_pos; + Eigen::Vector3d point_origin=ray_pos; + if (origins) { + const auto raw=origins->at(i/img->width,i%img->width); + point_origin=Eigen::Vector3d(raw[0],raw[1],raw[2]); + if (!point_origin.allFinite() || !isInMap(point_origin)) continue; + } + const Eigen::Vector3d devi = pt_world - point_origin; const double ray_length = devi.norm(); const bool in_local_range = fabs(devi(0)) <= mp_.local_update_range_(0) && fabs(devi(1)) <= mp_.local_update_range_(1) && @@ -1060,6 +1119,7 @@ void GridMap::cloudCallback(const sensor_msgs::msg::PointCloud2::ConstSharedPtr else md_.proj_points_[md_.proj_points_cnt] = pt_world; + if (origins) md_.proj_ray_origins_.push_back(point_origin); md_.proj_points_cnt++; } diff --git a/minco_ws/src/scan/plan_env/test/test_cloud_ray_origins.cpp b/minco_ws/src/scan/plan_env/test/test_cloud_ray_origins.cpp new file mode 100644 index 0000000..a89a775 --- /dev/null +++ b/minco_ws/src/scan/plan_env/test/test_cloud_ray_origins.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include + +sensor_msgs::msg::PointCloud2 cloud(bool big=false) { + sensor_msgs::msg::PointCloud2 msg;msg.header.frame_id="world"; + msg.height=1;msg.width=2;msg.point_step=24;msg.row_step=48;msg.is_bigendian=big;msg.data.resize(48); + const char * names[]={"x","y","z","origin_x","origin_y","origin_z"}; + for (unsigned i=0;i<6;++i) { + sensor_msgs::msg::PointField field;field.name=names[i];field.offset=i*4;field.datatype=field.FLOAT32;field.count=1;msg.fields.push_back(field); + } + float values[]={2.025f,.025f,.225f,.025f,.025f,.225f, 2.025f,.025f,.225f,.025f,.025f,.825f}; + for (unsigned i=0;i<12;++i) { + uint32_t bits;std::memcpy(&bits,&values[i],4); + if(big)bits=((bits&0xff)<<24)|((bits&0xff00)<<8)|((bits>>8)&0xff00)|(bits>>24); + std::memcpy(msg.data.data()+i*4,&bits,4); + } + return msg; +} + +TEST(CloudRayOrigins, ReadsLittleAndBigEndianWithDifferentOrigins) { + for (bool big:{false,true}) { + auto msg=cloud(big);plan_env::CloudRayOrigins read(msg); + EXPECT_NEAR(read.at(0,0)[2],.225,1e-7);EXPECT_NEAR(read.at(0,1)[2],.825,1e-7); + } +} +TEST(CloudRayOrigins, RejectsMissingTruncatedAndDuplicatedFields) { + auto msg=cloud();msg.fields.pop_back();EXPECT_THROW(plan_env::CloudRayOrigins read(msg),std::invalid_argument); + msg=cloud();msg.data.pop_back();EXPECT_THROW(plan_env::CloudRayOrigins read(msg),std::invalid_argument); + msg=cloud();msg.fields.push_back(msg.fields.back());EXPECT_THROW(plan_env::CloudRayOrigins read(msg),std::invalid_argument); + msg=cloud();msg.fields.back().offset=24;EXPECT_THROW(plan_env::CloudRayOrigins read(msg),std::invalid_argument); +} + +class RayMap : public ::testing::Test { + protected: + virtual bool multiOrigins() const {return true;} + void SetUp() override { + if(!rclcpp::ok()){int argc=0;rclcpp::init(argc,nullptr);} + rclcpp::NodeOptions options; + options.parameter_overrides({ + {"grid_map.cloud_has_ray_origins",multiOrigins()},{"grid_map.need_extrinsic",false}, + {"grid_map.resolution",.05},{"grid_map.sliding_map_size_x",6.0},{"grid_map.sliding_map_size_y",6.0}, + {"grid_map.sliding_map_size_z",2.0},{"grid_map.ground_height",-1.0},{"grid_map.skip_pixel",2}, + {"grid_map.map_sliding_en",false},{"grid_map.obstacles_inflation_z_up",0.0}, + {"grid_map.obstacles_inflation_z_down",0.0},{"grid_map.double_cylinder_radius",0.0}, + {"grid_map.max_ray_length",5.0},{"grid_map.p_hit",.85},{"grid_map.p_miss",.35}, + {"grid_map.p_min",.12},{"grid_map.p_max",.97},{"grid_map.p_occ",.80}}); + node=std::make_shared("multi_origin_grid_test","/multi_origin_test",options);map.initMap(node.get()); + pose=node->create_publisher("sensor_pose",10); + points=node->create_publisher("cloud",10); + nav_msgs::msg::Odometry odom;odom.header.frame_id="world";odom.pose.pose.orientation.w=1.; + odom.pose.pose.position.x=.025;odom.pose.pose.position.y=.025;odom.pose.pose.position.z=.825; + for(int i=0;i<30;++i){pose->publish(odom);rclcpp::spin_some(node);std::this_thread::sleep_for(std::chrono::milliseconds(10));} + } + void run(sensor_msgs::msg::PointCloud2 msg) { + for(int i=0;i<40;++i){points->publish(msg);rclcpp::spin_some(node);std::this_thread::sleep_for(std::chrono::milliseconds(10));} + rclcpp::spin_some(node); + } + std::shared_ptr node;GridMap map; + rclcpp::Publisher::SharedPtr pose; + rclcpp::Publisher::SharedPtr points; +}; +TEST_F(RayMap, BothOriginsClearTheirOwnRaysToTheSameEndpoint) { + const Eigen::Vector3d lower(1.025,.025,.225),upper(1.025,.025,.525),endpoint(2.025,.025,.225); + map.setOccupied(lower);map.setOccupied(upper); + run(cloud()); + EXPECT_EQ(map.getOccupancy(lower),0);EXPECT_EQ(map.getOccupancy(upper),0); + EXPECT_EQ(map.getOccupancy(endpoint),1); +} +TEST_F(RayMap, MissingOriginsCannotFallBackToUpperSensor) { + const Eigen::Vector3d upper(1.025,.025,.525),endpoint(2.025,.025,.225); + map.setOccupied(upper);auto msg=cloud();msg.fields.pop_back();run(msg); + EXPECT_EQ(map.getOccupancy(upper),1);EXPECT_EQ(map.getOccupancy(endpoint),0); +} +TEST_F(RayMap, EndpointOnlyPriorOccupiesWithoutClearingRayToSensor) { + const Eigen::Vector3d between(1.025,.025,.525),endpoint(2.025,.025,.225); + map.setOccupied(between);auto msg=cloud(); + for (unsigned i=0;i<2;++i) + std::memcpy(msg.data.data()+i*24+12,msg.data.data()+i*24,12); + run(msg); + EXPECT_EQ(map.getOccupancy(between),1);EXPECT_EQ(map.getOccupancy(endpoint),1); +} + +class SingleRayMap : public RayMap {protected: bool multiOrigins() const override {return false;}}; +TEST_F(SingleRayMap, DefaultIgnoresOptionalOriginsAndKeepsSingleOriginBehavior) { + const Eigen::Vector3d lower(1.025,.025,.225),upper(1.025,.025,.525),endpoint(2.025,.025,.225); + map.setOccupied(lower);map.setOccupied(upper);run(cloud()); + EXPECT_EQ(map.getOccupancy(lower),1);EXPECT_EQ(map.getOccupancy(upper),0);EXPECT_EQ(map.getOccupancy(endpoint),1); +} diff --git a/minco_ws/src/scan/plan_manage/CMakeLists.txt b/minco_ws/src/scan/plan_manage/CMakeLists.txt index 9e69434..50a671f 100644 --- a/minco_ws/src/scan/plan_manage/CMakeLists.txt +++ b/minco_ws/src/scan/plan_manage/CMakeLists.txt @@ -88,6 +88,7 @@ if(BUILD_TESTING) add_launch_test(test/test_waypoint_parameters.py TIMEOUT 30) add_launch_test(test/test_controller_execution.py TIMEOUT 35) add_launch_test(test/test_rectangle_controller.py TIMEOUT 35) + add_launch_test(test/test_controller_prediction.py TIMEOUT 25) add_launch_test(test/test_rectangle_fsm.py TIMEOUT 35) add_launch_test(test/test_rectangle_launch.py TIMEOUT 30) add_launch_test(test/test_goal_execution_fsm.py TIMEOUT 35) diff --git a/minco_ws/src/scan/plan_manage/src/closed_loop_controller.cpp b/minco_ws/src/scan/plan_manage/src/closed_loop_controller.cpp index af4bc5b..22275eb 100644 --- a/minco_ws/src/scan/plan_manage/src/closed_loop_controller.cpp +++ b/minco_ws/src/scan/plan_manage/src/closed_loop_controller.cpp @@ -49,9 +49,15 @@ public: goal_slow_gain_ = declare_parameter("goal_slow_gain", 2.0); goal_v_floor_ = declare_parameter("goal_v_floor", 0.05); kp_yaw_ = declare_parameter("kp_yaw", 1.5); + state_prediction_limit_ = declare_parameter("state_prediction_limit", 0.0); + goal_braking_acceleration_ = declare_parameter("goal_braking_acceleration", 0.0); + if (!std::isfinite(state_prediction_limit_) || state_prediction_limit_ < 0 || state_prediction_limit_ > .2 || + !std::isfinite(goal_braking_acceleration_) || goal_braking_acceleration_ < 0 || goal_braking_acceleration_ > 2.) + throw std::runtime_error("Invalid optional prediction/braking limits"); max_vx_ = declare_parameter("max_vx", 0.75); max_vy_ = declare_parameter("max_vy", 0.35); - max_vyaw_ = std::min(declare_parameter("max_vyaw", 1.0), kMaxVYawLimit); + const double yaw_limit = declare_parameter("extended_yaw_limit", false) ? 2.0 : kMaxVYawLimit; + max_vyaw_ = std::min(declare_parameter("max_vyaw", 1.0), yaw_limit); finish_dist_ = declare_parameter("finish_dist", 0.15); // 终点朝向对齐。SCAN 的规划器只吃位置(rvizGoalCallback -> waypointCallback // 只读 pose.position),到点朝向由轨迹最后一段切向决定。 @@ -203,6 +209,8 @@ private: } odom_pos_ << msg->pose.pose.position.x, msg->pose.pose.position.y, msg->pose.pose.position.z; odom_yaw_ = tf2::getYaw(msg->pose.pose.orientation); + odom_source_stamp_ = rclcpp::Time(msg->header.stamp); + odom_yaw_rate_ = msg->twist.twist.angular.z; // 实测速度:body_odom 的 twist.linear 是 body 系,转到世界系供速度阻尼使用。 // 没有阻尼时指令 = 前馈 + kp_pos*误差,前馈占主导,车速一旦快于参考点就会 // 冲过头,过冲量约 vel_des/kp_pos(0.25 m/s、kp 0.5 时约 0.5 m)。 @@ -260,6 +268,19 @@ private: return; } const auto current_time = now(); + // Optional control-only constant-velocity prediction of a fresh measured + // state. Never republish/relabel odometry, and never extend its freshness. + Eigen::Vector3d control_position = odom_pos_; + double control_yaw = odom_yaw_; + if (state_prediction_limit_ > 0) { + const double source_age = (current_time-odom_source_stamp_).seconds(); + if (source_age < 0 || source_age > state_prediction_limit_ || + !odom_vel_world_.allFinite() || !std::isfinite(odom_yaw_rate_)) { + publishExecutionFrozen(true); publishStop(); last_update_time_=current_time; return; + } + control_position.head<2>() += odom_vel_world_*source_age; + control_yaw = normalizeAngle(odom_yaw_+odom_yaw_rate_*source_age); + } double dt = (current_time - last_update_time_).seconds(); if (dt < 0.0 || dt > 0.2) dt = 0.0; @@ -273,7 +294,7 @@ private: } const double t_eval = std::min(exec_time_, traj_duration_); Eigen::Vector3d pos_des = traj_[0].evaluateDeBoorT(t_eval); - double yaw_error = normalizeAngle((locked ? status_.heading : estimateDesiredYaw(t_eval, pos_des)) - odom_yaw_); + double yaw_error = normalizeAngle((locked ? status_.heading : estimateDesiredYaw(t_eval, pos_des)) - control_yaw); // 倒车模式:期望朝向与车头几乎相反(目标在正后方)时,全向底盘不必掉头 —— // 保持车头、直接反向平移即可。本车满足三个前提:全向底盘(vx/vy/wz 独立)、 @@ -302,14 +323,14 @@ private: // checked spline: do not create an unchecked shortcut to the task goal. const bool final_window = !have_status_ || status_.is_final; const Eigen::Vector3d vel_des = traj_[1].evaluateDeBoorT(exec_time_); - const Eigen::Vector2d pos_error(pos_des.x() - odom_pos_.x(), pos_des.y() - odom_pos_.y()); + const Eigen::Vector2d pos_error(pos_des.x() - control_position.x(), pos_des.y() - control_position.y()); // 到**轨迹终点**的距离,用于两处:接近目标时的限速、以及到达判定。 Eigen::Vector3d end_pt = traj_[0].evaluateDeBoorT(traj_duration_); if (have_status_ && final_window) end_pt << status_.goal.x, status_.goal.y, status_.goal.z; const double dist_to_goal = - std::hypot(end_pt.x() - odom_pos_.x(), end_pt.y() - odom_pos_.y()); + std::hypot(end_pt.x() - control_position.x(), end_pt.y() - control_position.y()); // 位置死区:误差小于该值时不再做位置修正,只保留前馈与阻尼。 // 全向轮横移会打滑、响应滞后,终点附近几厘米的横向误差去追只会造成 @@ -329,14 +350,20 @@ private: // 不用平方根刹车曲线:0.8 m/s² 下 8 cm 处仍有 0.45 m/s,等于不减速; // 线性斜坡 gain=2 时 8 cm 处限到 0.16 m/s,才真能刹住。 // v_floor 保证末段仍有爬行速度,不会提前停死。 - const double v_cap = std::max(goal_v_floor_, goal_slow_gain_ * std::max(dist_to_goal, 0.0)); + double v_cap = std::max(goal_v_floor_, goal_slow_gain_ * std::max(dist_to_goal, 0.0)); + if (goal_braking_acceleration_ > 0) { + // Leave .10 s of actuation/reaction distance in addition to prediction. + const double a=goal_braking_acceleration_, reaction=.10; + const double brake_cap=std::sqrt(a*a*reaction*reaction+2*a*dist_to_goal)-a*reaction; + v_cap=std::min(v_cap,std::max(goal_v_floor_,brake_cap)); + } const double cmd_norm = vel_cmd.norm(); if (cmd_norm > v_cap && cmd_norm > 1e-6) vel_cmd *= v_cap / cmd_norm; const Eigen::Vector2d vel_world = clampNorm(vel_cmd, std::max(max_vx_, max_vy_)); - const double c = std::cos(odom_yaw_); - const double s = std::sin(odom_yaw_); + const double c = std::cos(control_yaw); + const double s = std::sin(control_yaw); geometry_msgs::msg::Twist command; command.linear.x = std::clamp(c * vel_world.x() + s * vel_world.y(), -max_vx_, max_vx_); command.linear.y = std::clamp(-s * vel_world.x() + c * vel_world.y(), -max_vy_, max_vy_); @@ -370,10 +397,19 @@ private: { // 位置已到位。若开启终点朝向对齐且朝向误差仍超容差,则原地转,不发线速度。 double yaw_err = 0.0; + double measured_yaw_err = 0.0; if (final_window && align_to_goal_yaw_ && (locked ? status_.target_yaw_valid : has_goal_)) - yaw_err = normalizeAngle((locked ? status_.target_yaw : goal_yaw_) - odom_yaw_); + { + yaw_err = normalizeAngle((locked ? status_.target_yaw : goal_yaw_) - control_yaw); + measured_yaw_err = normalizeAngle((locked ? status_.target_yaw : goal_yaw_) - odom_yaw_); + } - if (std::abs(yaw_err) > yaw_goal_tolerance_) + // Prediction may shape commands, but cannot certify a future arrival. + // Retain the safety grant until actual measured heading/rate settle. + const bool measured_alignment_pending = state_prediction_limit_ > 0 && + final_window && align_to_goal_yaw_ && + (std::abs(measured_yaw_err) > yaw_goal_tolerance_ || std::abs(odom_yaw_rate_) > .03); + if (std::abs(yaw_err) > yaw_goal_tolerance_ || measured_alignment_pending) { // 冻结 FSM 重规划:否则对齐途中来一条新轨迹会把 exec_time_ 打回 0, // 变成"转一点、又开一段、再转"的来回摆动 @@ -397,7 +433,7 @@ private: } } // Drift outside the arrival latch must never resume translation at a rotated heading. - if (locked && std::abs(normalizeAngle(status_.heading-odom_yaw_)) > status_.heading_tolerance) { + if (locked && std::abs(normalizeAngle(status_.heading-control_yaw)) > status_.heading_tolerance) { command.linear.x = 0.0; command.linear.y = 0.0; } @@ -433,6 +469,9 @@ private: std::int64_t traj_id_{0}; Eigen::Vector3d odom_pos_{Eigen::Vector3d::Zero()}; double odom_yaw_{0.0}; + double odom_yaw_rate_{0.0}; + rclcpp::Time odom_source_stamp_{0,0,RCL_ROS_TIME}; + double state_prediction_limit_{0.0}, goal_braking_acceleration_{0.0}; double exec_time_{0.0}; rclcpp::Time last_update_time_{0, 0, RCL_ROS_TIME}; double time_forward_, heading_error_threshold_, kp_pos_, kp_yaw_; diff --git a/minco_ws/src/scan/plan_manage/src/planner_manager.cpp b/minco_ws/src/scan/plan_manage/src/planner_manager.cpp index c10d758..13f2ca4 100644 --- a/minco_ws/src/scan/plan_manage/src/planner_manager.cpp +++ b/minco_ws/src/scan/plan_manage/src/planner_manager.cpp @@ -126,7 +126,7 @@ namespace scan_planner PolynomialTraj gl_traj; double dist = (start_pt - local_target_pt).norm(); - double time = pow(pp_.max_vel_, 2) / pp_.max_acc_ > dist ? sqrt(dist / pp_.max_acc_) : (dist - pow(pp_.max_vel_, 2) / pp_.max_acc_) / pp_.max_vel_ + 2 * pp_.max_vel_ / pp_.max_acc_; + double time = pow(pp_.max_vel_, 2) / pp_.max_acc_ > dist ? 2 * sqrt(dist / pp_.max_acc_) : (dist - pow(pp_.max_vel_, 2) / pp_.max_acc_) / pp_.max_vel_ + 2 * pp_.max_vel_ / pp_.max_acc_; if (!flag_randomPolyTraj) { @@ -157,8 +157,14 @@ namespace scan_planner point_set.clear(); flag_too_far = false; Eigen::Vector3d last_pt = gl_traj.evaluate(0); - for (t = 0; t < time; t += ts) + // Uniform samples must include the actual endpoint. The old t