feat(motor): prepare TCHL semi-closed-loop startup parameters

Prepare every motor in the selected group before normal initialization and report all axes that need a power cycle. Disable and save PrD.00 for TCHL while retaining motor-side encoder conversion, and prevent same-manager retries from bypassing incomplete preparation.

Document the single-axis and six-axis configurations and align the PP test velocity and printed target with the command.

Validation: Debug build and offline initialization checks passed. PP operation was verified on hardware after restoring semi-closed-loop mode.
This commit is contained in:
lgv 2026-11-02 14:42:03 +08:00
parent 13f01feb18
commit 0ca045d59b
11 changed files with 128 additions and 18 deletions

View File

@ -1,5 +1,6 @@
# 同川 TC600E TR 单轴模板;未注册为活动设备。
# 当前按实机 PrD.00=0、电子齿轮1:1配置;减速比需读取实机3D06后填写。
# prepareInit 会检查 PrD.00=0;关闭全闭环并保存后需给驱动器断电再上电。
# 按非全闭环、电子齿轮 1:1 配置:电机端编码器每圈 131072 计数,减速比 101。
motor {
id: "tchl_motors"

View File

@ -1,6 +1,7 @@
# 同川 TC600E TR 七轴模板;设备管理器默认不启用。
# 同川 TC600E TR 六轴模板;设备管理器默认不启用。
# 各轴按实机确认 PrD.00、电子齿轮、编码器分辨率和减速比。
# 以下 131072 / 101 仅为 PrD.00=0、电子齿轮 1:1 的配置示例。
# prepareInit 会统一检查 PrD.00=0,关闭全闭环并保存后,统一给驱动器断电再上电。
# 按非全闭环、电子齿轮 1:1 配置:电机端编码器每圈 131072 计数,减速比按各轴实机填写。
motor {
id: "tchl_motors"

View File

@ -9,6 +9,7 @@
#include "devices/abstract_device.h"
#include "common/base/logging/logger.h"
#include "motor/motor_init_result.h"
#include "motor/motor_protocol_interface.h"
#include <mutex>
@ -50,6 +51,9 @@ namespace cmvr::device{
DeviceKind kind() const noexcept override { return DeviceKind::Motor; }
// Prepare parameters that require a drive restart before normal initialization.
virtual PrepareResult prepareInit() { return PrepareResult::Ready; }
virtual bool setMode(msgs::RunMode mode) {
std::scoped_lock lock(mtx_);
if (!protocol_) {

View File

@ -15,6 +15,7 @@ public:
std::unique_ptr<TchlMotorAdapter> vendor_adapter);
std::string typeName() const override { return "TchlMotor"; }
PrepareResult prepareInit() override;
bool init() override;
bool torqueOn() override;
bool torqueOff() override;

View File

@ -8,6 +8,7 @@
#include "devices/motor/bus_runtime/ethercat/include/ethercat_motor_bus_runtime.h"
#include "devices/motor/drivers/ethercat_motor/include/cia402/cia402_objects.h"
#include "devices/motor/drivers/ethercat_motor/include/vendor/motor_vendor_adapter.h"
#include "devices/motor/motor_init_result.h"
namespace cmvr::device {
@ -16,6 +17,7 @@ public:
explicit TchlMotorAdapter(std::shared_ptr<EthercatMotorBusRuntime> bus_runtime);
~TchlMotorAdapter() override = default;
PrepareResult prepareInit(std::uint8_t node_id);
bool initNode(std::uint8_t node_id) override;
bool writePositionLimits(std::uint8_t node_id,
std::int32_t lower_limit,
@ -39,6 +41,7 @@ private:
bool waitZeroCalibration_(std::uint8_t node_id, std::int32_t& zeroed_position) const;
std::shared_ptr<EthercatMotorBusRuntime> bus_runtime_;
PrepareResult prepare_result_{PrepareResult::Ready};
std::chrono::milliseconds enable_delay_{400};
std::chrono::milliseconds disable_delay_{200};
bool has_position_limits_{false};

View File

@ -16,6 +16,15 @@ TchlMotor::TchlMotor(const config::MotorConfigItem& config,
{
}
PrepareResult TchlMotor::prepareInit()
{
std::scoped_lock lock(mtx_);
if (!hasDependencies_()) {
return PrepareResult::Failed;
}
return vendor_adapter_->prepareInit(node_id_);
}
bool TchlMotor::init()
{
std::scoped_lock lock(mtx_);

View File

@ -15,6 +15,9 @@ namespace {
constexpr std::uint16_t kEnableCommandDelay = 0x3136;
constexpr std::uint16_t kDisableDelay = 0x3137;
constexpr std::uint16_t kSaveParameters = 0x313D;
constexpr std::uint16_t kSaveVendorParameters = 55;
constexpr std::uint16_t kFullClosedLoop = 0x3D00;
constexpr std::uint16_t kSoftLimitEnable = 0x3809;
constexpr std::uint16_t kForceBrakeRelease = 0x381E;
constexpr std::uint16_t kMaximumDelayMs = 500;
@ -34,6 +37,54 @@ TchlMotorAdapter::TchlMotorAdapter(
{
}
PrepareResult TchlMotorAdapter::prepareInit(const std::uint8_t node_id)
{
if (prepare_result_ == PrepareResult::RestartRequired) {
return prepare_result_;
}
if (!bus_runtime_ || !bus_runtime_->hasMotor(node_id)) {
CMVR_LOG(ERROR) << "[TchlMotorAdapter] missing EtherCAT motor during preparation, node="
<< static_cast<int>(node_id);
return PrepareResult::Failed;
}
std::uint16_t full_closed_loop = 0;
if (!bus_runtime_->readSdo<std::uint16_t>(
node_id, kFullClosedLoop, 0x00, full_closed_loop)) {
CMVR_LOG(ERROR) << "[TchlMotorAdapter] failed to read PrD.00 (0x3D00), node="
<< static_cast<int>(node_id);
return PrepareResult::Failed;
}
if (full_closed_loop > 1) {
CMVR_LOG(ERROR) << "[TchlMotorAdapter] invalid PrD.00, node="
<< static_cast<int>(node_id) << ", value=" << full_closed_loop;
return PrepareResult::Failed;
}
if (full_closed_loop == 0 && prepare_result_ == PrepareResult::Ready) {
return PrepareResult::Ready;
}
// A retry after a failed write/save must not treat the staged value as ready.
prepare_result_ = PrepareResult::Failed;
if (full_closed_loop == 1 && !bus_runtime_->writeSdo<std::uint16_t>(
node_id, kFullClosedLoop, 0x00, 0)) {
CMVR_LOG(ERROR) << "[TchlMotorAdapter] failed to disable full closed loop, node="
<< static_cast<int>(node_id);
return prepare_result_;
}
if (!bus_runtime_->writeSdo<std::uint16_t>(
node_id, kSaveParameters, 0x00, kSaveVendorParameters)) {
CMVR_LOG(ERROR) << "[TchlMotorAdapter] failed to save Pr1-PrD parameters, node="
<< static_cast<int>(node_id);
return prepare_result_;
}
prepare_result_ = PrepareResult::RestartRequired;
CMVR_LOG(WARNING) << "[TchlMotorAdapter] full closed loop disabled and saved; "
"power-cycle the drive before initialization, node="
<< static_cast<int>(node_id);
return prepare_result_;
}
bool TchlMotorAdapter::initNode(const std::uint8_t node_id)
{
if (!bus_runtime_ || !bus_runtime_->hasMotor(node_id)) {

View File

@ -77,10 +77,10 @@ TEST(TchlMotorRealTest, ProfilePosition)
auto motor = initMotor(manager);
motor->calibrateZeroQ();
if (!motor->setMode(msgs::RUN_MODE_PROFILE_POSITION) || !motor->torqueOn() ||
!motor->commandProfilePosition(3, -1, kAccelerationRadS2)) {
!motor->commandProfilePosition(3, 1, kAccelerationRadS2)) {
throw std::runtime_error("Profile position command failed");
}
std::cout << "target=" << kTargetPositionRad << " rad" << std::endl;
std::cout << "target=" << 3 << " rad" << std::endl;
printFeedbackFor(motor, std::chrono::seconds{4});
// motor->quickStop();
printFeedbackFor(motor, std::chrono::seconds{1000});

View File

@ -100,6 +100,8 @@ private:
std::unordered_map<std::uint8_t, std::shared_ptr<AbstractMotor>> motors_by_id_;
std::unordered_map<std::string, std::shared_ptr<AbstractMotor>> motors_by_joint_;
bool initialized_{false};
// Recreating motors must not mistake staged startup parameters for active ones.
bool preparation_failed_{false};
static std::mutex registry_mutex_;
static std::unordered_map<std::string, std::weak_ptr<MotorManager>> managers_;

View File

@ -49,6 +49,11 @@ bool MotorManager::init()
if (initialized_) {
return true;
}
if (preparation_failed_) {
CMVR_LOG(ERROR) << "[MotorManager] preparation did not complete; resolve the errors, "
"restart affected drives and recreate the motor manager: " << id_;
return false;
}
if (id_.empty()) {
CMVR_LOG(ERROR) << "[MotorManager] id is empty";
return false;
@ -134,10 +139,40 @@ bool MotorManager::init()
}
bool group_ok = true;
if (motor_group_cfg.bus_type() == config::MOTOR_BUS_CAN) {
std::string restart_required;
std::string preparation_failed;
for (const auto& motor : motors) {
switch (motor->prepareInit()) {
case PrepareResult::Ready:
break;
case PrepareResult::RestartRequired:
restart_required += (restart_required.empty() ? "" : ", ") +
motor->jointName();
group_ok = false;
break;
case PrepareResult::Failed:
preparation_failed += (preparation_failed.empty() ? "" : ", ") +
motor->jointName();
group_ok = false;
break;
}
}
if (!restart_required.empty()) {
CMVR_LOG(ERROR) << "[MotorManager] parameters saved; restart the drives before "
<< "initializing again: group=" << group_name
<< ", joints=[" << restart_required << "]";
}
if (!preparation_failed.empty()) {
CMVR_LOG(ERROR) << "[MotorManager] motor preparation failed: group=" << group_name
<< ", joints=[" << preparation_failed << "]";
}
if (!group_ok) {
preparation_failed_ = true;
}
if (group_ok) {
for (auto& motor : motors) {
if (!motor->init()) {
CMVR_LOG(ERROR) << "[MotorManager] failed to initialize CAN motor: "
CMVR_LOG(ERROR) << "[MotorManager] failed to initialize motor: "
<< motor->jointName();
group_ok = false;
break;
@ -696,11 +731,6 @@ std::vector<std::shared_ptr<AbstractMotor>> MotorManager::createMujocoMotors_(
if (cfg.limit_qdd() > 0.0) {
motor->setLimitQdd(cfg.limit_qdd(), -cfg.limit_qdd());
}
if (!motor->init()) {
CMVR_LOG(ERROR) << "[MotorManager] failed to init MuJoCo motor: "
<< cfg.joint_name();
return {};
}
motors.push_back(std::move(motor));
}
return motors;
@ -751,12 +781,6 @@ std::vector<std::shared_ptr<AbstractMotor>> MotorManager::createEthercatMotors_(
motor = std::make_shared<TchlMotor>(
cfg, protocol, std::make_unique<TchlMotorAdapter>(ethercat_bus_runtime));
}
if (!motor->init()) {
CMVR_LOG(ERROR) << "[MotorManager] failed to init EtherCAT motor: "
<< cfg.joint_name() << ", vendor="
<< config::MotorVendor_Name(group_cfg.vendor());
return {};
}
motors.push_back(std::move(motor));
}
return motors;

View File

@ -0,0 +1,14 @@
#ifndef CMVR_ES_MOTOR_INIT_RESULT_H
#define CMVR_ES_MOTOR_INIT_RESULT_H
namespace cmvr::device {
enum class PrepareResult {
Ready,
RestartRequired,
Failed
};
} // namespace cmvr::device
#endif // CMVR_ES_MOTOR_INIT_RESULT_H