cmvr-es/cmvr-es/algorithms/controllers/pbvs/include/pbvs_controller.h
lgv 37a62fb3e3 feat(arm): add collision-aware planning and refactor motion control
Integrate OMPL RRTConnect and quintic B-spline smoothing into moveP, reuse
the B-spline trajectory generator for moveJ, and remove the TOPPRA paths
and dependencies. Separate joint and Cartesian planners, smoothers, and
the standalone curve math library.

Move speedL execution into SpeedLExecutor, tighten velocity QP feasibility,
and add adaptive speed reduction, braking recovery, feedback-derived TCP
velocity, and grouped cyclic velocity submission. Improve MuJoCo control
cycle synchronization and camera streaming.

Centralize kinematics and collision geometry in RobotModel, expose box
obstacles through CollisionChecker, and extract SelfCollisionMonitor for
protective stops and recovery. Add cancellable moveP pre-approach and
configurable failure recovery to the touch workflow, with updated configs,
documentation, and regression coverage.

Validation: recorded -j8 build and 133 passing tests on 2026-10-08, including
headless MuJoCo checks for eight target poses and seven moveJ returns.
Static scene configuration removal passed 27 follow-up tests and six arm
configuration parses. Staged diff and shell syntax checks pass; no new
hardware tests were run for this commit.
2026-10-13 09:18:06 +08:00

375 lines
8.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#ifndef CMVR_PBVS_CONTROLLER_H
#define CMVR_PBVS_CONTROLLER_H
#include <array>
#include <Eigen/Dense>
#include "common/types/arm/arm_types.h"
namespace cmvr
{
/**
* @brief 基于相对 3D 位姿的 PBVS 控制器。
*
* 坐标系:
*
* G : Screen Tag 坐标系,同时作为屏幕参考坐标系
* P : TCP / 触控点坐标系
*
* 输入:
*
* ^G T_P_des : TCP 相对于 Screen Tag 的目标位姿
* ^G T_P_cur : TCP 相对于 Screen Tag 的当前位姿
*
* 输出:
*
* ^G V_P =
*
* [ vx ]
* [ vy ]
* [ vz ]
* [ wx ]
* [ wy ]
* [ wz ]
*
* 即 TCP 在 Screen Tag 坐标系 G 下表达的 6D Cartesian Twist。
*
* 位置误差:
*
* e_p = p_des - p_cur
*
* 姿态误差:
*
* R_err = R_des * R_cur^T
* e_R = Log(R_err)^vee
*
* 控制律:
*
* v = Kp * e_p
* w = Kr * e_R
*
* 本类只负责视觉伺服 6D Twist 计算,不负责:
*
* - AprilTag 检测
* - RobotArm
* - G -> Base 坐标转换
* - IK
* - speedL 下发
*/
class PbvsController
{
public:
enum class ComputeStatus
{
OK = 0,
TARGET_NOT_SET,
INVALID_INPUT,
INVALID_DT,
INVALID_TARGET,
INVALID_CURRENT_POSE
};
struct Output
{
// 当前位置误差,单位 m
Eigen::Vector3d position_error_G{Eigen::Vector3d::Zero()};
// 当前姿态误差 rotation-vector,单位 rad
Eigen::Vector3d rotation_error_G{Eigen::Vector3d::Zero()};
// PBVS 计算得到的原始线速度,单位 m/s
Eigen::Vector3d raw_linear_velocity_G{Eigen::Vector3d::Zero()};
// PBVS 计算得到的原始角速度,单位 rad/s
Eigen::Vector3d raw_angular_velocity_G{Eigen::Vector3d::Zero()};
// 经过限幅 / 加速度 / 滤波后的线速度
Eigen::Vector3d linear_velocity_G{Eigen::Vector3d::Zero()};
// 经过限幅 / 加速度 / 滤波后的角速度
Eigen::Vector3d angular_velocity_G{Eigen::Vector3d::Zero()};
// 可直接取出的 CartesianVelocity。
//
// 注意:
// 这里仍然是在 G / ScreenTag frame 下表达。
device::CartesianVelocity twist_G{};
bool position_reached{false};
bool orientation_reached{false};
bool reached{false};
bool valid{false};
};
public:
PbvsController();
/**
* @brief 设置完整目标位姿。
*
* @param T_G_P_des TCP(P) 相对于 ScreenTag(G) 的目标位姿。
*/
bool setTargetPose(const Eigen::Matrix4d& T_G_P_des);
/**
* @brief 使用目标位置 + 目标姿态设置期望位姿。
*/
bool setTargetPose(
const Eigen::Vector3d& position_G,
const Eigen::Matrix3d& rotation_G_P);
/**
* @brief 当前是否已经设置有效目标。
*/
bool hasTarget() const { return target_valid_; }
/**
* @brief 获取目标位姿。
*/
const Eigen::Matrix4d& targetPose() const { return T_G_P_des_; }
/**
* @brief 根据当前 TCP 位姿计算 PBVS 速度命令。
*
* @param T_G_P_cur 当前 TCP 相对于 Screen Tag 的位姿。
* @param dt 控制周期,单位 s。
* @param output 输出结果。
*
* @return 成功返回 true。
*/
bool compute(
const Eigen::Matrix4d& T_G_P_cur,
double dt,
Output& output);
/**
* @brief 便捷接口,只输出 CartesianVelocity。
*
* 注意输出仍然是 G frame。
*/
bool compute(
const Eigen::Matrix4d& T_G_P_cur,
double dt,
device::CartesianVelocity& twist_G_out);
/**
* @brief 设置位置比例增益。
*
* x/y/z 分别控制。
*/
void setPositionGain(const Eigen::Vector3d& kp);
/**
* @brief 设置姿态比例增益。
*
* rx/ry/rz 分别控制。
*/
void setRotationGain(const Eigen::Vector3d& kr);
/**
* @brief 设置六维速度上限。
*
* [vx vy vz wx wy wz]
*
* 前三维 m/s;
* 后三维 rad/s。
*/
void setVelocityLimit6(const std::array<double, 6>& vmax6);
/**
* @brief 设置六维加速度限制。
*
* 前三维 m/s^2;
* 后三维 rad/s^2。
*
* <= 0 表示对应维度不限制。
*/
void setAccelerationLimit6(const std::array<double, 6>& amax6);
/**
* @brief 设置六维误差阈值。
*
* [x y z rx ry rz]
*
* 前三维 m;
* 后三维 rad。
*/
void setTolerance6(const std::array<double, 6>& tolerance6);
/**
* @brief 设置一阶低通 alpha。
*
* alpha = 1:不滤波。
*
* 0 < alpha < 1:
*
* cmd = alpha * current + (1 - alpha) * previous
*/
void setTwistFilterAlpha(double alpha);
/**
* @brief 是否启用每个轴。
*
* 默认 6DoF 全部开启。
*
* 例如以后如果不希望控制 yaw:
*
* enabled[5] = false;
*/
void setAxisEnabled(const std::array<bool, 6>& enabled);
/**
* @brief 清空速度历史,但保留目标。
*/
void resetTwistCommandState();
/**
* @brief 清空整个 PBVS 状态和目标。
*/
void reset();
ComputeStatus lastComputeStatus() const { return last_compute_status_; }
static const char* statusToString(ComputeStatus status);
const Eigen::Vector3d& lastPositionError() const { return last_position_error_G_; }
const Eigen::Vector3d& lastRotationError() const { return last_rotation_error_G_; }
const Eigen::Vector3d& positionGain() const { return kp_position_; }
const Eigen::Vector3d& rotationGain() const { return kp_rotation_; }
const std::array<double, 6>& velocityLimit6() const { return vmax6_; }
const std::array<double, 6>& accelerationLimit6() const { return amax6_; }
const std::array<double, 6>& tolerance6() const { return tolerance6_; }
double twistFilterAlpha() const { return twist_lpf_alpha_; }
const Eigen::Matrix<double, 6, 1>& lastTwistCommandG() const
{
return last_twist_cmd_G_;
}
bool lastReached() const { return last_reached_; }
private:
static bool isFiniteTransform(const Eigen::Matrix4d& T);
static bool hasValidBottomRow(
const Eigen::Matrix4d& T,
double tolerance = 1e-6);
static bool isValidTransform(const Eigen::Matrix4d& T);
/**
* @brief 将可能有微小数值误差的旋转矩阵投影到 SO(3)。
*/
static Eigen::Matrix3d projectToSO3(const Eigen::Matrix3d& R);
/**
* @brief 计算空间旋转误差,在 G frame 表达。
*
* R_err = R_des * R_cur^T
*
* e_R = Log(R_err)^vee
*/
static Eigen::Vector3d rotationError(
const Eigen::Matrix3d& R_des,
const Eigen::Matrix3d& R_cur);
static double clampValue(double value, double lower, double upper);
static device::CartesianVelocity toCartesianVelocity(
const Eigen::Matrix<double, 6, 1>& twist);
private:
// ---------------- target ----------------
Eigen::Matrix4d T_G_P_des_{Eigen::Matrix4d::Identity()};
bool target_valid_{false};
// ---------------- gains ----------------
Eigen::Vector3d kp_position_{2.0, 2.0, 1.5};
Eigen::Vector3d kp_rotation_{1.5, 1.5, 1.5};
// ---------------- velocity limits ----------------
//
// [vx vy vz wx wy wz]
//
std::array<double, 6> vmax6_{{
0.10,
0.10,
0.05,
0.50,
0.50,
0.50
}};
// ---------------- acceleration limits ----------------
std::array<double, 6> amax6_{{
0.50,
0.50,
0.30,
2.0,
2.0,
2.0
}};
// ---------------- tolerance ----------------
std::array<double, 6> tolerance6_{{
0.0015, // x 1.5 mm
0.0015, // y 1.5 mm
0.0020, // z 2.0 mm
0.05, // rx ~2.9 deg
0.05, // ry
0.05 // rz
}};
// ---------------- axis enable ----------------
std::array<bool, 6> axis_enabled_{{
true,
true,
true,
true,
true,
true
}};
// ---------------- LPF ----------------
double twist_lpf_alpha_{1.0};
// ---------------- command history ----------------
Eigen::Matrix<double, 6, 1> previous_twist_cmd_G_{
Eigen::Matrix<double, 6, 1>::Zero()
};
bool has_previous_twist_{false};
// ---------------- last output ----------------
Eigen::Vector3d last_position_error_G_{Eigen::Vector3d::Zero()};
Eigen::Vector3d last_rotation_error_G_{Eigen::Vector3d::Zero()};
Eigen::Matrix<double, 6, 1> last_twist_cmd_G_{
Eigen::Matrix<double, 6, 1>::Zero()
};
bool last_reached_{false};
ComputeStatus last_compute_status_{ComputeStatus::TARGET_NOT_SET};
};
} // namespace cmvr
#endif // CMVR_PBVS_CONTROLLER_H