diff --git a/README.md b/README.md index 3dd495c..786ca56 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,7 @@ File mặc định đi kèm gói: `config/dock_global_params.yaml`. Cây đang c | `maneuver/pivot_check_costmap` | `false` | bool | cả hai pivot | Kiểm tra lethal trên các đoạn thẳng và vùng xoay | | `terminal/reverse_distance_padding` | `0.0` | m | cả hai | Khoảng hở cộng thêm cho đoạn cuối | | `terminal/straight_length` | `0.0` | m | cả hai | Độ dài đoạn thẳng cuối mong muốn | +| `dock_straight_length` | unset | m | cả hai | Override `terminal/straight_length` theo marker, hợp lệ trong `[0, 2]` | | `sampling/path_point_spacing` | `0.05` | m | cả hai | Khoảng cách lấy mẫu | `wheel_base` không khai trong YAML: nó là giá trị đo, để tránh cấu hình mâu thuẫn. @@ -192,6 +193,11 @@ không khai một trong hai khoá, `move_base2` ghi YAML `null` để xoá overr sót từ marker trước; DockPlanner đọc `null` như chưa khai. Chỉ có tác dụng khi `dock_anchor: mating_edge`. Khai với `center` hoặc `face` thì DockPlanner từ chối khởi động. +`dock_straight_length` cũng là khoá tuỳ chọn ở root planner namespace, được chép +theo marker và xoá bằng YAML `null` khi marker kế tiếp không khai. Khoá này hợp lệ +với cả ba `dock_anchor`; nếu unset, DockPlanner dùng `terminal/straight_length`. +Đoạn thẳng cuối được đặt theo `max(terminal floor, giá trị hiệu lực)`; tăng giá trị +cần thêm khoảng trống từ vị trí bắt đầu và có thể đổi hình đường đi sang cusp hoặc xoay. `dock_anchor: center` cộng nửa thân dock phía tiếp cận vào khoảng hở; `dock_anchor: face` bỏ qua `footprint_dock` hoàn toàn. `dock_anchor: mating_edge` cũng bỏ qua `footprint_dock`. Nó dành cho goal đã được tầng @@ -222,6 +228,7 @@ terminal là `0.2 m`, và `Xv = max(0.2, 0.474 - 0.40 + 0.10) = 0.2 m`. `dock_anchor: mating_edge`; khai cùng `center` hoặc `face` thì DockPlanner từ chối khởi động. `dock_face_offset` phải hữu hạn và thuộc `(0, 2] m`, còn `dock_pivot_safety_margin` phải hữu hạn và thuộc `[0, 1] m`. +- `dock_straight_length` phải hữu hạn và thuộc `[0, 2] m`, với mọi `dock_anchor`. `max_steering_angle` phải là góc lái tương đương theo bicycle model, không phải servo tick hay góc pivot tại vận tốc bằng không. Giá trị mặc định chỉ là điểm diff --git a/config/dock_global_params.yaml b/config/dock_global_params.yaml index ed4a496..0a78758 100644 --- a/config/dock_global_params.yaml +++ b/config/dock_global_params.yaml @@ -159,6 +159,7 @@ DockPlanner: # khi vào dock, ĐỔI LẠI ăn vào chỗ dành cho cung, nên đặt lớn quá sẽ đẩy plan # sang kiểu có cusp hoặc kiểu xoay tại chỗ. # Với mating_edge + dock_face_offset, terminal floor không còn là rear_extent nên straight_length (ví dụ 0.2 m) quyết định độ dài terminal. + # Marker có thể ghi đè bằng dock_straight_length trong maker_sources.yaml. straight_length: 0.0 sampling: diff --git a/include/dock_planner/dock_planner_config.h b/include/dock_planner/dock_planner_config.h index 81eb752..5e2ff9a 100644 --- a/include/dock_planner/dock_planner_config.h +++ b/include/dock_planner/dock_planner_config.h @@ -133,6 +133,9 @@ struct DockPlannerConfig /** [m] Optional per-marker override for pivot_safety_margin. */ std::optional dock_pivot_safety_margin = std::nullopt; + /** [m] Per-marker override for terminal/straight_length; null means unset. */ + std::optional dock_straight_length = std::nullopt; + /** [m] Maximum straight back-off distance for the differential-only last-resort pivot. 0 disables it. */ double pivot_backoff_max_distance = 0.0; diff --git a/src/dock_calc.cpp b/src/dock_calc.cpp index 1fc72ce..0c4f85a 100644 --- a/src/dock_calc.cpp +++ b/src/dock_calc.cpp @@ -828,8 +828,10 @@ bool DockCalc::makePlanMoveToDock( // Length of the straight terminal leg. The clearance is a floor, not the // target: a robot needs a straight run long enough to settle its heading // before it enters the dock, and that is usually longer than its own overhang. + const double effective_straight_length = + config_.dock_straight_length.value_or(config_.straight_length); const double straight_distance = - std::max(minimum_cusp_distance, config_.straight_length); + std::max(minimum_cusp_distance, effective_straight_length); const double curve_room = start_longitudinal - straight_distance; const bool differential = @@ -1162,13 +1164,15 @@ bool DockCalc::makePlanMoveToDock( "min_radius=%.3f m, straight_distance=%.3f m, " "cusp_reachable=%s (extension=%.3f m, max=%.3f m), " "pivot_enabled=%s (vertex=%.3f m, circumscribed=%.3f m), " - "face_offset=%s, pivot_safety_margin=%.3f m, backoff_pivot=%s.\n", + "face_offset=%s, pivot_safety_margin=%.3f m, " + "straight_length=%.3f m (source=%s), backoff_pivot=%s.\n", differential ? "differential" : "ackermann", start_longitudinal, start_lateral, minimum_turning_radius, straight_distance, cusp_reachable ? "yes" : "no", cusp_extension, config_.maximum_forward_extension, pivot_enabled ? "yes" : "no", pivot_vertex_distance, robot.circumscribed_radius, effective_face_offset_text.c_str(), effective_pivot_safety_margin, + effective_straight_length, config_.dock_straight_length ? "marker" : "default", backoff_status.c_str()); return false; } @@ -1214,7 +1218,8 @@ bool DockCalc::makePlanMoveToDock( "min_radius=%.4f m, start=(long=%.3f m, lateral=%.3f m in approach frame), " "curve_room=%.3f m, cusp_extension=%.3f m (pivot threshold=%.3f m), " "arc_radius=%.4f m, pivot_vertex=%.3f m, pivot_angle=%.4f rad, " - "circumscribed_radius=%.3f m, face_offset=%s, pivot_safety_margin=%.3f m.\n", + "circumscribed_radius=%.3f m, face_offset=%s, pivot_safety_margin=%.3f m, " + "straight_length=%.3f m (source=%s).\n", dock_with_front ? "front" : "rear", shapeName(selected), differential ? "differential" : "ackermann", plan.size(), approach_segment.size(), terminal_segment.size(), @@ -1222,7 +1227,8 @@ bool DockCalc::makePlanMoveToDock( minimum_turning_radius, start_longitudinal, start_lateral, curve_room, cusp_extension, config_.pivot_fallback_extension, curve_radius, pivot_vertex_distance, pivot_angle, robot.circumscribed_radius, - effective_face_offset_text.c_str(), effective_pivot_safety_margin); + effective_face_offset_text.c_str(), effective_pivot_safety_margin, + effective_straight_length, config_.dock_straight_length ? "marker" : "default"); return true; } diff --git a/src/dock_planner.cpp b/src/dock_planner.cpp index a0ce5c6..63aeb37 100644 --- a/src/dock_planner.cpp +++ b/src/dock_planner.cpp @@ -376,12 +376,14 @@ bool DockPlanner::initialize(std::string name, optionalDistanceText(loaded_config.dock_face_offset); const std::string dock_pivot_safety_margin_text = optionalDistanceText(loaded_config.dock_pivot_safety_margin); + const std::string dock_straight_length_text = + optionalDistanceText(loaded_config.dock_straight_length); robot::log_info( "[dock_planner] Initialized dock maneuver (docking edge is derived per plan " "from the side the robot stands on): model=%s, TF %s -> %s, wheel_base=%.4f m, " "max_steering_angle=%.4f rad, minimum_turning_radius=%.4f m, " "pivot_fallback_extension=%.3f m, pivot_safety_margin=%.3f m, " - "dock_face_offset=%s, dock_pivot_safety_margin=%s, " + "dock_face_offset=%s, dock_pivot_safety_margin=%s, dock_straight_length=%s, " "pivot_backoff_max_distance=%.3f m, pivot_check_costmap=%s.\n", loaded_config.requiresAxleTf() ? "ackermann" : "differential", loaded_config.requiresAxleTf() ? loaded_config.front_axle_frame.c_str() : "-", @@ -390,6 +392,7 @@ bool DockPlanner::initialize(std::string name, minimum_turning_radius, loaded_config.pivot_fallback_extension, loaded_config.pivot_safety_margin, dock_face_offset_text.c_str(), dock_pivot_safety_margin_text.c_str(), + dock_straight_length_text.c_str(), loaded_config.pivot_backoff_max_distance, loaded_config.pivot_check_costmap ? "true" : "false"); return true; diff --git a/src/dock_planner_config.cpp b/src/dock_planner_config.cpp index d149948..177afa2 100644 --- a/src/dock_planner_config.cpp +++ b/src/dock_planner_config.cpp @@ -206,7 +206,9 @@ bool DockPlannerConfig::load(const robot::NodeHandle& nh, DockPlannerConfig& con !readNullableOptionalDouble(nh, "dock_face_offset", loaded.dock_face_offset, error) || !readNullableOptionalDouble(nh, "dock_pivot_safety_margin", - loaded.dock_pivot_safety_margin, error)) + loaded.dock_pivot_safety_margin, error) || + !readNullableOptionalDouble(nh, "dock_straight_length", + loaded.dock_straight_length, error)) { return false; } @@ -285,6 +287,14 @@ bool DockPlannerConfig::validate(std::string& error) const } } + if (dock_straight_length.has_value() && + (!std::isfinite(*dock_straight_length) || *dock_straight_length < 0.0 || + *dock_straight_length > 2.0)) + { + error = "parameter 'dock_straight_length' must be finite and in [0, 2.0] m"; + return false; + } + // A differential base has no steered axle: validating axle frames or a // steering limit there would reject a perfectly valid configuration and // stop the plugin from loading at all. diff --git a/test/dock_planner_test.cpp b/test/dock_planner_test.cpp index 0157708..2641d1e 100644 --- a/test/dock_planner_test.cpp +++ b/test/dock_planner_test.cpp @@ -854,6 +854,87 @@ void testDockPivotSafetyMarginOverride() CHECK(!pathsEqualExactly(mating_default_path, overridden_path)); } +void testDockStraightLengthOverride() +{ + robot::NodeHandle root_nh; + YAML::Node parameters(YAML::NodeType::Map); + parameters["kinematics"]["model"] = "differential"; + parameters["dock_anchor"] = "mating_edge"; + parameters["dock_face_offset"] = 0.40; + parameters["dock_pivot_safety_margin"] = 0.10; + parameters["terminal"]["straight_length"] = 0.20; + root_nh.setParam("straight_length_override_test", parameters); + robot::NodeHandle planner_nh(root_nh, "straight_length_override_test"); + dock_planner::DockPlannerConfig config; + std::string error; + const auto start = pose(0.579, 0.292, 0.0); + const auto goal = pose(0.0, 0.0, 0.0); + const auto footprint = robotFootprint065x069(); + + auto check_terminal = [&](const dock_planner::DockPlannerConfig& candidate, + double expected) + { + dock_planner::DockCalc calculator(candidate); + std::vector path; + CHECK(calculator.makePlanMoveToDock(start, goal, footprint, {}, path)); + bool found_pivot = false; + for (std::size_t index = 1; index < path.size(); ++index) + { + if (std::hypot(path[index].position.x - path[index - 1].position.x, + path[index].position.y - path[index - 1].position.y) <= 1e-9) + { + CHECK(near(std::fabs(longitudinal(path[index], goal.pose)), expected)); + found_pivot = true; + break; + } + } + CHECK(found_pivot); + }; + + // Missing and null both preserve the configured terminal default in the path. + CHECK(dock_planner::DockPlannerConfig::load(planner_nh, config, error)); + CHECK(!config.dock_straight_length.has_value()); + CHECK(near(config.straight_length, 0.20)); + check_terminal(config, 0.20); + planner_nh.setParam("dock_straight_length", YAML::Node(YAML::NodeType::Null)); + CHECK(dock_planner::DockPlannerConfig::load(planner_nh, config, error)); + CHECK(!config.dock_straight_length.has_value()); + CHECK(near(config.straight_length, 0.20)); + check_terminal(config, 0.20); + + planner_nh.setParam("dock_straight_length", 0.30); + CHECK(dock_planner::DockPlannerConfig::load(planner_nh, config, error)); + CHECK(config.dock_straight_length.has_value()); + check_terminal(config, 0.30); + + // Unlike the face and margin overrides, this key is valid for all anchors. + planner_nh.setParam("dock_face_offset", YAML::Node(YAML::NodeType::Null)); + planner_nh.setParam("dock_pivot_safety_margin", YAML::Node(YAML::NodeType::Null)); + for (const char* anchor : {"center", "face", "mating_edge"}) + { + planner_nh.setParam("dock_anchor", YAML::Node(anchor)); + CHECK(dock_planner::DockPlannerConfig::load(planner_nh, config, error)); + CHECK(config.dock_straight_length.has_value()); + } + for (const double value : {-0.01, 2.01}) + { + planner_nh.setParam("dock_straight_length", value); + CHECK(!dock_planner::DockPlannerConfig::load(planner_nh, config, error)); + CHECK(error == "parameter 'dock_straight_length' must be finite and in [0, 2.0] m"); + } + // NaN goes through the YAML param tree as text that may not parse back as a double, so the + // reader can reject it before validate() does; either way the key must be named in the error. + planner_nh.setParam("dock_straight_length", std::numeric_limits::quiet_NaN()); + CHECK(!dock_planner::DockPlannerConfig::load(planner_nh, config, error)); + CHECK(error.find("dock_straight_length") != std::string::npos); + std::cout << "dock_straight_length NaN rejected with: " << error << "\n"; + for (const double value : {0.0, 2.0}) + { + planner_nh.setParam("dock_straight_length", value); + CHECK(dock_planner::DockPlannerConfig::load(planner_nh, config, error)); + } +} + void testExistingPlansUnchangedByBackoff() { const auto robot_footprint = apmFootprint(); @@ -1449,6 +1530,7 @@ int main() testMatingEdgeAnchor(); testMatingEdgeFaceOffset(); testDockPivotSafetyMarginOverride(); + testDockStraightLengthOverride(); testExistingPlansUnchangedByBackoff(); testBackoffPivotGates(); testCostmapCheckCallbacks();