diff --git a/cmvr-es/config/devices/motor/tchl_motors.pb.txt b/cmvr-es/config/devices/motor/tchl_motors.pb.txt index 56672f9d..2b656f1f 100644 --- a/cmvr-es/config/devices/motor/tchl_motors.pb.txt +++ b/cmvr-es/config/devices/motor/tchl_motors.pb.txt @@ -1,5 +1,6 @@ # 同川 TC600E TR 单轴模板;未注册为活动设备。 -# 当前按实机 PrD.00=0、电子齿轮1:1配置;减速比需读取实机3D06后填写。 +# prepareInit 会检查 PrD.00=0;关闭全闭环并保存后需给驱动器断电再上电。 +# 按非全闭环、电子齿轮 1:1 配置:电机端编码器每圈 131072 计数,减速比 101。 motor { id: "tchl_motors" diff --git a/cmvr-es/config/devices/motor/tchl_multi_motors.pb.txt b/cmvr-es/config/devices/motor/tchl_multi_motors.pb.txt index eba17303..b8a0a12d 100644 --- a/cmvr-es/config/devices/motor/tchl_multi_motors.pb.txt +++ b/cmvr-es/config/devices/motor/tchl_multi_motors.pb.txt @@ -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" diff --git a/cmvr-es/devices/motor/abstract_motor.h b/cmvr-es/devices/motor/abstract_motor.h index 96cb461a..70959cc6 100644 --- a/cmvr-es/devices/motor/abstract_motor.h +++ b/cmvr-es/devices/motor/abstract_motor.h @@ -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 @@ -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_) { diff --git a/cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_motor.h b/cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_motor.h index 72625384..49c93bd8 100644 --- a/cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_motor.h +++ b/cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_motor.h @@ -15,6 +15,7 @@ public: std::unique_ptr vendor_adapter); std::string typeName() const override { return "TchlMotor"; } + PrepareResult prepareInit() override; bool init() override; bool torqueOn() override; bool torqueOff() override; diff --git a/cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_motor_adapter.h b/cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_motor_adapter.h index 1cc362a6..6d35ac2d 100644 --- a/cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_motor_adapter.h +++ b/cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_motor_adapter.h @@ -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 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 bus_runtime_; + PrepareResult prepare_result_{PrepareResult::Ready}; std::chrono::milliseconds enable_delay_{400}; std::chrono::milliseconds disable_delay_{200}; bool has_position_limits_{false}; diff --git a/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor.cpp b/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor.cpp index 8aba5ec3..873516dc 100644 --- a/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor.cpp +++ b/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor.cpp @@ -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_); diff --git a/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor_adapter.cpp b/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor_adapter.cpp index 36ff9ab7..0e66bac8 100644 --- a/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor_adapter.cpp +++ b/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor_adapter.cpp @@ -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(node_id); + return PrepareResult::Failed; + } + + std::uint16_t full_closed_loop = 0; + if (!bus_runtime_->readSdo( + node_id, kFullClosedLoop, 0x00, full_closed_loop)) { + CMVR_LOG(ERROR) << "[TchlMotorAdapter] failed to read PrD.00 (0x3D00), node=" + << static_cast(node_id); + return PrepareResult::Failed; + } + if (full_closed_loop > 1) { + CMVR_LOG(ERROR) << "[TchlMotorAdapter] invalid PrD.00, node=" + << static_cast(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( + node_id, kFullClosedLoop, 0x00, 0)) { + CMVR_LOG(ERROR) << "[TchlMotorAdapter] failed to disable full closed loop, node=" + << static_cast(node_id); + return prepare_result_; + } + if (!bus_runtime_->writeSdo( + node_id, kSaveParameters, 0x00, kSaveVendorParameters)) { + CMVR_LOG(ERROR) << "[TchlMotorAdapter] failed to save Pr1-PrD parameters, node=" + << static_cast(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(node_id); + return prepare_result_; +} + bool TchlMotorAdapter::initNode(const std::uint8_t node_id) { if (!bus_runtime_ || !bus_runtime_->hasMotor(node_id)) { diff --git a/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor_real_test.cpp b/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor_real_test.cpp index 0f1d9682..09309aa9 100644 --- a/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor_real_test.cpp +++ b/cmvr-es/devices/motor/drivers/ethercat_motor/src/vendor/tchl/tchl_motor_real_test.cpp @@ -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}); diff --git a/cmvr-es/devices/motor/manager/include/motor_manager.h b/cmvr-es/devices/motor/manager/include/motor_manager.h index b8f5797f..372067e8 100644 --- a/cmvr-es/devices/motor/manager/include/motor_manager.h +++ b/cmvr-es/devices/motor/manager/include/motor_manager.h @@ -100,6 +100,8 @@ private: std::unordered_map> motors_by_id_; std::unordered_map> 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> managers_; diff --git a/cmvr-es/devices/motor/manager/src/motor_manager.cpp b/cmvr-es/devices/motor/manager/src/motor_manager.cpp index 81b36a30..b6b21eaf 100644 --- a/cmvr-es/devices/motor/manager/src/motor_manager.cpp +++ b/cmvr-es/devices/motor/manager/src/motor_manager.cpp @@ -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> 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> MotorManager::createEthercatMotors_( motor = std::make_shared( cfg, protocol, std::make_unique(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; diff --git a/cmvr-es/devices/motor/motor_init_result.h b/cmvr-es/devices/motor/motor_init_result.h new file mode 100644 index 00000000..9b751b70 --- /dev/null +++ b/cmvr-es/devices/motor/motor_init_result.h @@ -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