fix(touch): use elapsed visual update time for PBVS

This commit is contained in:
lgv 2026-11-26 17:50:55 +08:00
parent 2ea3dc7467
commit b293b1d6f2
3 changed files with 81 additions and 18 deletions

View File

@ -9,6 +9,7 @@
#include <future>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <vector>
@ -133,7 +134,7 @@ private:
bool stepPreparing();
bool stepApproaching();
void cancelApproach();
bool stepAligning(double dt);
bool stepAligning(double nominal_dt);
// Perception helpers return readiness; waiting keeps stepAligning active.
bool updateAlignmentAnchor(Eigen::Vector3d& p_target_G);
bool updateAlignmentTcpPose();
@ -141,6 +142,7 @@ private:
const Eigen::Matrix4d& T_G_P);
bool mapAlignmentCommandToBase(const Eigen::Matrix4d& T_G_P,
const PbvsController::Output& output,
double pbvs_dt,
device::CartesianVelocity& twist_B);
bool applyAlignmentCommand(const PbvsController::Output& output,
const device::CartesianVelocity& twist_B);
@ -148,6 +150,7 @@ private:
bool stepDwelling();
bool stepRetracting();
void resetPbvsCommandState();
void stopPbvsMotion();
bool buildInitJointPositions(std::vector<double>& positions_out) const;
bool isAtInitPosition(const std::vector<double>& positions) const;
@ -227,6 +230,7 @@ private:
double max_T_B_G_rotation_delta_rad_{0.0};
Clock::time_point phase_start_time_{};
std::optional<Clock::time_point> last_pbvs_update_time_;
Clock::time_point last_touch_pressure_log_time_{};
Clock::time_point last_retract_log_time_{};
Status final_status_after_retract_{Status::DONE};

View File

@ -441,14 +441,7 @@ void TouchScreenTask::stop() {
void TouchScreenTask::stopUnlocked() {
cancelApproach();
if (arm_) {
try {
arm_->stopL();
} catch (...) {
}
}
pbvs_.resetTwistCommandState();
stopPbvsMotion();
phase_ = Phase::IDLE;
resetRunState();
@ -480,6 +473,7 @@ void TouchScreenTask::resetRunState() {
last_T_B_G_.setIdentity();
max_T_B_G_translation_delta_m_ = 0.0;
max_T_B_G_rotation_delta_rad_ = 0.0;
last_pbvs_update_time_.reset();
last_touch_pressure_log_time_ = Clock::time_point{};
}
@ -1060,7 +1054,7 @@ bool TouchScreenTask::stepApproaching() {
// Keep the original screen anchor and desired pose, but reacquire the live
// TCP pose and start PBVS with zero command history and its own timeout.
tcp_pose_tracker_.clear();
pbvs_.resetTwistCommandState();
resetPbvsCommandState();
align_stable_count_ = 0;
phase_ = Phase::ALIGNING;
phase_start_time_ = Clock::now();
@ -1069,7 +1063,7 @@ bool TouchScreenTask::stepApproaching() {
return true;
}
bool TouchScreenTask::stepAligning(const double dt) {
bool TouchScreenTask::stepAligning(const double nominal_dt) {
const double elapsed =
std::chrono::duration<double>(Clock::now() - phase_start_time_).count();
if (elapsed > config_.alignment().timeout_s()) {
@ -1088,14 +1082,21 @@ bool TouchScreenTask::stepAligning(const double dt) {
if (!updatePbvsTarget(p_target_G, T_G_P)) {
return false;
}
// Camera acquisition can take longer than the TaskManager step period.
const auto update_time = Clock::now();
const double elapsed_since_update = last_pbvs_update_time_
? std::chrono::duration<double>(update_time - *last_pbvs_update_time_).count()
: nominal_dt;
const double pbvs_dt = std::clamp(elapsed_since_update, 0.0001, 0.05);
PbvsController::Output output;
if (!pbvs_.compute(T_G_P, std::clamp(dt, 0.0001, 0.05), output)) {
if (!pbvs_.compute(T_G_P, pbvs_dt, output)) {
enterFailed(Status::ALIGN_COMPUTE_FAILED);
return false;
}
last_pbvs_update_time_ = update_time;
device::CartesianVelocity twist_B;
if (!mapAlignmentCommandToBase(T_G_P, output, twist_B)) {
if (!mapAlignmentCommandToBase(T_G_P, output, pbvs_dt, twist_B)) {
return false;
}
return applyAlignmentCommand(output, twist_B);
@ -1282,7 +1283,7 @@ bool TouchScreenTask::updatePbvsTarget(const Eigen::Vector3d& p_target_G,
const bool refresh_target = !pbvs_target_initialized_ || tracker_.lastSwitched();
if (refresh_target) {
if (tracker_.lastSwitched()) {
pbvs_.resetTwistCommandState();
resetPbvsCommandState();
}
if (!pbvs_.setTargetPose(p_target_G + position_offset_G, R_target)) {
enterFailed(Status::ALIGN_TARGET_SETUP_FAILED);
@ -1296,6 +1297,7 @@ bool TouchScreenTask::updatePbvsTarget(const Eigen::Vector3d& p_target_G,
bool TouchScreenTask::mapAlignmentCommandToBase(const Eigen::Matrix4d& T_G_P,
const PbvsController::Output& output,
const double pbvs_dt,
device::CartesianVelocity& twist_B) {
Eigen::Matrix4d T_B_P = Eigen::Matrix4d::Identity();
try {
@ -1360,6 +1362,7 @@ bool TouchScreenTask::mapAlignmentCommandToBase(const Eigen::Matrix4d& T_G_P,
: 0.0;
CMVR_LOG(DEBUG) << "[PBVS_DEBUG]"
<< " dt_s=" << pbvs_dt
<< " target_G_mm=["
<< p_target_used_G.x() * kMetersToMillimeters << ' '
<< p_target_used_G.y() * kMetersToMillimeters << ' '
@ -1690,6 +1693,11 @@ bool TouchScreenTask::stepRetracting() {
return phase_ != Phase::FAILED;
}
void TouchScreenTask::resetPbvsCommandState() {
pbvs_.resetTwistCommandState();
last_pbvs_update_time_.reset();
}
void TouchScreenTask::stopPbvsMotion() {
if (arm_) {
try {
@ -1697,7 +1705,7 @@ void TouchScreenTask::stopPbvsMotion() {
} catch (...) {
}
}
pbvs_.resetTwistCommandState();
resetPbvsCommandState();
}
bool TouchScreenTask::readCurrentTouchPointPositionBase(Eigen::Vector3d& p_out) const {
@ -2045,7 +2053,7 @@ bool TouchScreenTask::startRetractPhase(const Phase next_phase_after_retract,
void TouchScreenTask::enterFailed(const Status status) {
cancelApproach();
pbvs_.resetTwistCommandState();
resetPbvsCommandState();
if (phase_ == Phase::RETRACTING && retract_start_position_valid_) {
CMVR_LOG(WARNING) << "[TouchScreenTask][RETRACT_FAILED] status=" << statusToString(status)
<< ", max_forward_after_retract_mm=" << max_forward_after_retract_m_ * 1000.0;

View File

@ -354,8 +354,12 @@ public:
calls.push_back("moveL");
return Result::success();
}
Result speedL(const cmvr::device::CartesianVelocity&, double, double,
cmvr::device::FrameType) override { return unsupported(); }
Result speedL(const cmvr::device::CartesianVelocity& velocity, double, double,
cmvr::device::FrameType) override {
if (!speed_l_commands) return unsupported();
speed_l_commands->push_back(velocity);
return Result::success();
}
Result startServoMode(const cmvr::device::ServoOptions&) override { return unsupported(); }
Result servoJ(const cmvr::device::JointPositionCommand&) override { return unsupported(); }
Result servoL(const cmvr::device::CartesianPose&, cmvr::device::FrameType) override { return unsupported(); }
@ -397,6 +401,7 @@ public:
bool move_j_throws{false};
bool torque_on_throws{false};
int stop_l_calls{0};
std::optional<std::vector<cmvr::device::CartesianVelocity>> speed_l_commands;
std::vector<std::string> calls;
std::vector<double> return_target;
cmvr::device::MotionOptions return_options;
@ -787,6 +792,52 @@ TEST_F(TouchScreenFailureReturnTest, SelectedMethodAppliesAfterFinishWhenEnabled
}
}
TEST_F(TouchScreenFailureReturnTest, PbvsUsesElapsedUpdateTimeAndResetsAfterTrackingLoss) {
const auto frame = makeAlignmentTagFrame();
ASSERT_FALSE(frame.empty());
const auto camera = std::make_shared<FailureReturnCamera>();
camera->frame = frame;
config.set_debug_draw_coordinate_frames(false);
auto* alignment = config.mutable_alignment();
alignment->set_timeout_s(10.0);
alignment->mutable_target()->set_mode(cmvr::config::TOUCH_SCREEN_ALIGN_MODE_POSITION_ONLY);
alignment->mutable_error_threshold()->set_x(0.001);
alignment->mutable_error_threshold()->set_y(1.0);
alignment->mutable_error_threshold()->set_z(1.0);
alignment->mutable_pbvs()->mutable_amax6()->set_x(0.1);
auto& commands = arm->speed_l_commands.emplace();
const auto linear_speed = [](const cmvr::device::CartesianVelocity& command) {
return std::hypot(command.vx, command.vy, command.vz);
};
cmvr::task::TouchScreenTask task(config);
ASSERT_TRUE(task.init(arm, nullptr, camera, camera));
ASSERT_TRUE(task.touch(180, 240));
constexpr double nominal_dt = 0.001;
ASSERT_TRUE(task.step(nominal_dt));
ASSERT_EQ(commands.size(), 1u);
EXPECT_NEAR(linear_speed(commands[0]), 0.0001, 1e-8);
std::this_thread::sleep_for(std::chrono::milliseconds(30));
ASSERT_TRUE(task.step(nominal_dt));
ASSERT_EQ(commands.size(), 2u);
const double speed_change = linear_speed(commands[1]) - linear_speed(commands[0]);
EXPECT_GE(speed_change, 0.0025);
EXPECT_LE(speed_change, 0.005 + 1e-8);
camera->frame.release();
const auto stops_before_loss = arm->stop_l_calls;
ASSERT_TRUE(task.step(nominal_dt));
EXPECT_EQ(arm->stop_l_calls, stops_before_loss + 1);
EXPECT_EQ(commands.size(), 2u);
camera->frame = frame;
ASSERT_TRUE(task.step(nominal_dt));
ASSERT_EQ(commands.size(), 3u);
EXPECT_NEAR(linear_speed(commands[2]), 0.0001, 1e-8);
}
TEST_F(TouchScreenFailureReturnTest, RejectsUnknownInitializationMotionMethod) {
using Init = cmvr::config::TouchScreenTaskInitializationConfig;
config.mutable_initialization()->set_motion_method(static_cast<Init::MotionMethod>(99));