- NavigationRequest: thêm relative_yaw [rad], NaN = không dùng, đi kèm được với goal_frame/relative_distance thay vì loại trừ. - resolveDeferredGoal(): thêm nhánh has_yaw. Trước đây relative_distance = 0 khiến goal CHÉP NGUYÊN pose hiện tại kể cả orientation — chặng rotate coi như đã tới đích ngay khi nhận, không quay độ nào. Giờ tính yaw nền (từ pose hiện tại, hoặc từ frame nếu goal_frame có) rồi cộng relative_yaw, dựng lại quaternion đủ 4 thành phần. Log "Goal received" thêm yaw= để chặng quay không còn trông giống hệt chặng vị trí. - mission_adapter_bridge.cpp: chở relative_yaw qua nguyên vẹn. - Thêm 3 dòng debug (nhóm debug_plan, mặc định bật) trong runController(): kết quả isGoalReached() khi true (candidate=0, chưa từng tính lệnh), v/omega trước arbiter, v/omega sau arbiter — phân biệt "chưa từng có lệnh" / "lệnh bị cắt" / "lỗi ngoài move_base2". Bật khối log đã có sẵn nhưng bị comment từ trước. - Binding rotate: PNKXRotateLocalPlanner -> HybridRotateLocalPlanner. - 13 test mới (deferred_goal_test.cpp + 2 ca bridge): quay giữ nguyên x/y chỉ đổi yaw, góc là delta theo hướng hiện tại, dấu âm quay ngược, quaternion chuẩn hoá, go_straight không bị đụng hướng, distance+yaw đi cùng nhau, frame+yaw lấy nền là hướng robot chứ không phải hướng frame đích, mất pose bị từ chối, NaN passthrough. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
337 lines
11 KiB
C++
337 lines
11 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* move_base2 — quy đích đến muộn về pose tuyệt đối (ControlLoop::resolveDeferredGoal).
|
|
*
|
|
* Mission layer sinh chặng lúc robot còn chưa tới chỗ xuất phát, nên đích của chặng có thể tới dưới
|
|
* ba dạng chưa-phải-pose: một TF frame, một quãng đường tương đối, một góc quay tương đối. Cả ba
|
|
* được quy về pose tuyệt đối đúng một lần, tại `submit`, và test này khoá phép quy đó.
|
|
*
|
|
* Vì sao đáng test riêng: một chặng quay tại chỗ từng chạy suốt mà robot không quay độ nào, vì
|
|
* nhánh quãng-tương-đối chép NGUYÊN pose hiện tại (gồm cả orientation) rồi cộng 0 mét — goal trùng
|
|
* khít chỗ đang đứng nên goal checker báo đã tới ngay lập tức.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include <string>
|
|
|
|
#include <move_base2/control_loop.h>
|
|
|
|
#include "fake_ports.h"
|
|
|
|
using move_base2::ControlLoop;
|
|
using move_base2::ControlLoopConfig;
|
|
using move_base2::ControlLoopDeps;
|
|
using move_base2::MotionProfile;
|
|
using move_base2::NavigationRequest;
|
|
using move_base2::testing::FakeActionPort;
|
|
using move_base2::testing::FakeClockPort;
|
|
using move_base2::testing::FakeControllerPort;
|
|
using move_base2::testing::FakeCostmapStatusPort;
|
|
using move_base2::testing::FakeEnvironmentPort;
|
|
using move_base2::testing::FakeMissionPort;
|
|
using move_base2::testing::FakePlannerPort;
|
|
using move_base2::testing::FakePosePort;
|
|
using move_base2::testing::FakeRecoveryPort;
|
|
|
|
namespace
|
|
{
|
|
|
|
constexpr double kNaN = std::numeric_limits<double>::quiet_NaN();
|
|
|
|
ControlLoopConfig baseConfig()
|
|
{
|
|
ControlLoopConfig config;
|
|
|
|
config.state_machine.planner_patience = 0.5; // [s]
|
|
config.state_machine.controller_patience = 0.5; // [s]
|
|
config.state_machine.oscillation_timeout = 0.0; // tắt
|
|
config.state_machine.recovery_enabled = false;
|
|
|
|
config.velocity.max_vel_x = 0.5; // [m/s]
|
|
config.velocity.min_vel_x = -0.2; // [m/s]
|
|
config.velocity.max_vel_theta = 1.0; // [rad/s]
|
|
config.velocity.max_accel_x = 100.0; // [m/s^2]
|
|
config.velocity.max_accel_theta = 100.0; // [rad/s^2]
|
|
|
|
config.nominal_control_period = 0.05; // [s]
|
|
|
|
config.position.global_planner_name = "FakeGlobalPlanner";
|
|
config.position.local_planner_name = "FakeLocalPlanner";
|
|
config.docking = config.position;
|
|
config.go_straight = config.position;
|
|
config.rotate = config.position;
|
|
config.rotate.local_planner_name = "FakeRotatePlanner";
|
|
|
|
return config;
|
|
}
|
|
|
|
/// @brief Yaw [rad] của một pose, để đọc lại kết quả đã quy.
|
|
double yawOf(const robot_geometry_msgs::PoseStamped& pose)
|
|
{
|
|
const auto& q = pose.pose.orientation;
|
|
return std::atan2(2.0 * (q.w * q.z + q.x * q.y), 1.0 - 2.0 * (q.y * q.y + q.z * q.z));
|
|
}
|
|
|
|
/**
|
|
* @class Fixture
|
|
* @brief Control loop nối cổng giả, đủ để `submit` chạy tới lượt lập plan đầu tiên.
|
|
*/
|
|
class Fixture
|
|
{
|
|
public:
|
|
Fixture() : recovery_(0)
|
|
{
|
|
pose_.setPosition(2.0, 3.0); // [m]
|
|
pose_.setYaw(0.0); // [rad]
|
|
|
|
deps_.clock = &clock_;
|
|
deps_.pose = &pose_;
|
|
deps_.planner = &planner_;
|
|
deps_.controller = &controller_;
|
|
deps_.recovery = &recovery_;
|
|
deps_.mission = &mission_;
|
|
deps_.action = &action_;
|
|
deps_.costmap_status = &costmap_status_;
|
|
deps_.environment = &environment_;
|
|
|
|
std::string error;
|
|
EXPECT_TRUE(loop_.configure(baseConfig(), deps_, error)) << error;
|
|
}
|
|
|
|
/// @brief Chặng quay tại chỗ: 0 m tịnh tiến cộng một góc.
|
|
static NavigationRequest rotationRequest(double relative_yaw)
|
|
{
|
|
NavigationRequest request;
|
|
request.profile = MotionProfile::kRotate;
|
|
request.goal.header.frame_id = "map";
|
|
request.goal.pose.orientation.w = 1.0;
|
|
request.relative_distance = 0.0; // [m]
|
|
request.relative_yaw = relative_yaw;
|
|
return request;
|
|
}
|
|
|
|
/// @brief Chặng đi thẳng, không góc — dạng đã chạy từ trước khi có relative_yaw.
|
|
static NavigationRequest straightRequest(double relative_distance)
|
|
{
|
|
NavigationRequest request;
|
|
request.profile = MotionProfile::kGoStraight;
|
|
request.goal.header.frame_id = "map";
|
|
request.goal.pose.orientation.w = 1.0;
|
|
request.relative_distance = relative_distance;
|
|
return request;
|
|
}
|
|
|
|
bool submit(const NavigationRequest& request, std::string& reason)
|
|
{
|
|
return loop_.submit(request, reason);
|
|
}
|
|
|
|
/// @brief Đích đã quy, đọc từ cổng planner sau khi lượt lập plan đầu tiên được phát đi.
|
|
const robot_geometry_msgs::PoseStamped& resolvedGoal()
|
|
{
|
|
loop_.step();
|
|
return planner_.pendingGoal();
|
|
}
|
|
|
|
FakePosePort& pose()
|
|
{
|
|
return pose_;
|
|
}
|
|
|
|
private:
|
|
ControlLoop loop_;
|
|
ControlLoopDeps deps_;
|
|
FakeClockPort clock_;
|
|
FakePosePort pose_;
|
|
FakePlannerPort planner_;
|
|
FakeControllerPort controller_;
|
|
FakeRecoveryPort recovery_;
|
|
FakeMissionPort mission_;
|
|
FakeActionPort action_;
|
|
FakeCostmapStatusPort costmap_status_;
|
|
FakeEnvironmentPort environment_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TEST(DeferredGoal, RotationKeepsThePositionAndTurnsTheHeading)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(0.0);
|
|
|
|
std::string reason;
|
|
ASSERT_TRUE(fixture.submit(Fixture::rotationRequest(M_PI_2), reason)) << reason;
|
|
|
|
const auto& goal = fixture.resolvedGoal();
|
|
EXPECT_NEAR(goal.pose.position.x, 2.0, 1e-9) << "[m] chặng quay không được dịch chuyển robot";
|
|
EXPECT_NEAR(goal.pose.position.y, 3.0, 1e-9);
|
|
EXPECT_NEAR(yawOf(goal), M_PI_2, 1e-9);
|
|
}
|
|
|
|
TEST(DeferredGoal, RotationIsRelativeToTheHeadingTheRobotHasNow)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(1.0); // [rad]
|
|
|
|
std::string reason;
|
|
ASSERT_TRUE(fixture.submit(Fixture::rotationRequest(0.5), reason)) << reason;
|
|
|
|
// Góc là DELTA, không phải hướng tuyệt đối: cùng một yêu cầu gửi ở hai hướng khác nhau phải cho
|
|
// hai đích khác nhau.
|
|
EXPECT_NEAR(yawOf(fixture.resolvedGoal()), 1.5, 1e-9);
|
|
}
|
|
|
|
TEST(DeferredGoal, ANegativeAngleTurnsTheOtherWay)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(0.0);
|
|
|
|
std::string reason;
|
|
ASSERT_TRUE(fixture.submit(Fixture::rotationRequest(-M_PI_4), reason)) << reason;
|
|
EXPECT_NEAR(yawOf(fixture.resolvedGoal()), -M_PI_4, 1e-9);
|
|
}
|
|
|
|
TEST(DeferredGoal, TheResolvedQuaternionIsNormalised)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(0.3);
|
|
|
|
std::string reason;
|
|
// submit() chạy isQuaternionValid() ngay sau khi quy; một quaternion dựng thiếu thành phần sẽ bị
|
|
// từ chối ở đó chứ không đi tiếp.
|
|
ASSERT_TRUE(fixture.submit(Fixture::rotationRequest(2.0), reason)) << reason;
|
|
|
|
const auto& q = fixture.resolvedGoal().pose.orientation;
|
|
EXPECT_NEAR(std::sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w), 1.0, 1e-9);
|
|
EXPECT_DOUBLE_EQ(q.x, 0.0) << "robot đi trên mặt phẳng: chỉ z-w được mang giá trị";
|
|
EXPECT_DOUBLE_EQ(q.y, 0.0);
|
|
}
|
|
|
|
TEST(DeferredGoal, AStraightLegWithoutAnAngleKeepsTheCurrentHeading)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(0.7); // [rad]
|
|
|
|
std::string reason;
|
|
ASSERT_TRUE(fixture.submit(Fixture::straightRequest(1.0), reason)) << reason;
|
|
|
|
// relative_yaw = NaN phải để orientation nguyên vẹn. Một giá trị 0 ở đây thay vì NaN sẽ ép mọi
|
|
// chặng go_straight phải kết thúc ở hướng 0 rad.
|
|
const auto& goal = fixture.resolvedGoal();
|
|
EXPECT_NEAR(yawOf(goal), 0.7, 1e-9);
|
|
EXPECT_NEAR(goal.pose.position.x, 2.0 + std::cos(0.7), 1e-9) << "[m] tiến theo trục thân robot";
|
|
EXPECT_NEAR(goal.pose.position.y, 3.0 + std::sin(0.7), 1e-9);
|
|
}
|
|
|
|
TEST(DeferredGoal, DistanceAndAngleCanBeAskedForTogether)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(0.0);
|
|
|
|
NavigationRequest request = Fixture::straightRequest(2.0);
|
|
request.relative_yaw = M_PI_2;
|
|
|
|
std::string reason;
|
|
ASSERT_TRUE(fixture.submit(request, reason)) << reason;
|
|
|
|
// Quãng đường tính theo hướng LÚC NHẬN chặng, không theo hướng sau khi quay.
|
|
const auto& goal = fixture.resolvedGoal();
|
|
EXPECT_NEAR(goal.pose.position.x, 4.0, 1e-9);
|
|
EXPECT_NEAR(goal.pose.position.y, 3.0, 1e-9);
|
|
EXPECT_NEAR(yawOf(goal), M_PI_2, 1e-9);
|
|
}
|
|
|
|
TEST(DeferredGoal, AnAbsoluteGoalIsLeftAlone)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(0.0);
|
|
|
|
NavigationRequest request;
|
|
request.profile = MotionProfile::kPosition;
|
|
request.goal.header.frame_id = "map";
|
|
request.goal.pose.position.x = 9.0; // [m]
|
|
request.goal.pose.position.y = 8.0; // [m]
|
|
request.goal.pose.orientation.z = std::sin(0.5 * 1.2);
|
|
request.goal.pose.orientation.w = std::cos(0.5 * 1.2);
|
|
|
|
std::string reason;
|
|
ASSERT_TRUE(fixture.submit(request, reason)) << reason;
|
|
|
|
const auto& goal = fixture.resolvedGoal();
|
|
EXPECT_NEAR(goal.pose.position.x, 9.0, 1e-9);
|
|
EXPECT_NEAR(goal.pose.position.y, 8.0, 1e-9);
|
|
EXPECT_NEAR(yawOf(goal), 1.2, 1e-9);
|
|
}
|
|
|
|
TEST(DeferredGoal, AnAngleAppliesOnTopOfAGoalTakenFromAFrame)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(0.2); // [rad] hướng robot
|
|
fixture.pose().setFramePose("dock_target", 5.0, 6.0, 1.0); // [m], [rad] hướng của trạm
|
|
|
|
NavigationRequest request;
|
|
request.profile = MotionProfile::kRotate;
|
|
request.goal_frame = "dock_target";
|
|
request.relative_yaw = 0.1; // [rad]
|
|
|
|
std::string reason;
|
|
ASSERT_TRUE(fixture.submit(request, reason)) << reason;
|
|
|
|
const auto& goal = fixture.resolvedGoal();
|
|
EXPECT_NEAR(goal.pose.position.x, 5.0, 1e-9) << "[m] vị trí vẫn tới từ frame";
|
|
EXPECT_NEAR(goal.pose.position.y, 6.0, 1e-9);
|
|
// Góc quay nói về ROBOT, nên nền của nó là hướng robot (0.2), không phải hướng của trạm (1.0).
|
|
EXPECT_NEAR(yawOf(goal), 0.3, 1e-9);
|
|
}
|
|
|
|
TEST(DeferredGoal, ARotationWithoutARobotPoseIsRefused)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setAvailable(false); // mất định vị
|
|
|
|
std::string reason;
|
|
EXPECT_FALSE(fixture.submit(Fixture::rotationRequest(M_PI_2), reason))
|
|
<< "không có pose thì không có nền để cộng góc; đoán ở đây là quay mù";
|
|
EXPECT_FALSE(reason.empty());
|
|
}
|
|
|
|
TEST(DeferredGoal, AFrameAndADistanceTogetherAreStillRefused)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setFramePose("dock_target", 5.0, 6.0, 0.0);
|
|
|
|
NavigationRequest request;
|
|
request.profile = MotionProfile::kPosition;
|
|
request.goal_frame = "dock_target";
|
|
request.relative_distance = 1.0; // [m]
|
|
|
|
std::string reason;
|
|
EXPECT_FALSE(fixture.submit(request, reason))
|
|
<< "hai nguồn VỊ TRÍ cùng lúc vẫn không có thứ tự nào hiển nhiên đúng; chỉ góc mới đi kèm được";
|
|
}
|
|
|
|
TEST(DeferredGoal, ANonFiniteAngleIsTreatedAsNoAngle)
|
|
{
|
|
Fixture fixture;
|
|
fixture.pose().setYaw(0.4);
|
|
|
|
NavigationRequest request = Fixture::straightRequest(0.0);
|
|
request.relative_yaw = kNaN;
|
|
|
|
std::string reason;
|
|
ASSERT_TRUE(fixture.submit(request, reason)) << reason;
|
|
EXPECT_NEAR(yawOf(fixture.resolvedGoal()), 0.4, 1e-9);
|
|
}
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|