From 2ecd74b0444a9ee915d4e422ab6d9c8ac336c8f3 Mon Sep 17 00:00:00 2001 From: linbo <1034003879@qq.com> Date: Fri, 30 Oct 2026 16:42:42 +0800 Subject: [PATCH] fix(arm): configure Lingkong three-axis control and use configured AUBO frames --- .../config/devices/arm/lingkong_arm.pb.txt | 30 ++++ .../devices/motor/lingkong_motors.pb.txt | 43 ++++-- cmvr-es/config/manager/device_manager.pb.txt | 6 + cmvr-es/devices/arm/aubo_arm/src/aubo_arm.cpp | 133 ++---------------- .../src/lingkong_can_protocol.cpp | 19 ++- .../tests/lingkong_motor_protocol_test.cpp | 60 ++++++++ 6 files changed, 155 insertions(+), 136 deletions(-) create mode 100644 cmvr-es/config/devices/arm/lingkong_arm.pb.txt diff --git a/cmvr-es/config/devices/arm/lingkong_arm.pb.txt b/cmvr-es/config/devices/arm/lingkong_arm.pb.txt new file mode 100644 index 00000000..3fab18da --- /dev/null +++ b/cmvr-es/config/devices/arm/lingkong_arm.pb.txt @@ -0,0 +1,30 @@ +# Three-axis joint-only group. MoveJ positions follow joint_names order (rad). +# CAN commands are sent per axis; this is not hardware-triggered synchronization. +arm { + robot_arms { + id: "lingkong_arm" + motor { + motor_system_id: "lingkong_can_motors" + motor_group_ids: "lingkong_can_motors" + dof: 3 + joint_names: "MG4005E_AXIS_1" + joint_names: "MG4005E_AXIS_2" + joint_names: "MG4005E_AXIS_3" + upd_freq: 50 + } + motion { + joint_trajectory_smoother { + sample_period_s: 0.02 + quintic_bspline { + control_point_spacing_rad: 0.1 + } + } + move_j { + settle_timeout_s: 2.0 + settle_position_tolerance_rad: 0.002 + settle_velocity_tolerance_rad_s: 0.02 + settle_stable_sample_count: 3 + } + } + } +} diff --git a/cmvr-es/config/devices/motor/lingkong_motors.pb.txt b/cmvr-es/config/devices/motor/lingkong_motors.pb.txt index 83a7853f..d6636f83 100644 --- a/cmvr-es/config/devices/motor/lingkong_motors.pb.txt +++ b/cmvr-es/config/devices/motor/lingkong_motors.pb.txt @@ -1,7 +1,6 @@ -# One MG6010E-i6 V3, selected by device_manager as lingkong_can_motors. -# CAN ID 1 verified by read-only status/angle queries on this installation. -# Bring up can0 at the drive's configured bitrate (manual default 1 Mbps). +# Three MG4005E-i10 V3 motors on can0, CAN node IDs 1/2/3. # All public q/qd values refer to OUTPUT shaft radians/radians per second. +# SOFTWARE limits retained from the previous single-motor installation. motor { id: "lingkong_motors" motor_groups { @@ -13,9 +12,8 @@ motor { motors { motors { id: 1 - joint_name: "MG6010E_AXIS_1" - gear_ratio: 6 - # Example SOFTWARE position/speed limits; confirm for the installation. + joint_name: "MG4005E_AXIS_1" + gear_ratio: 10 limit_q_lb: -3.14 limit_q_ub: 3.14 limit_qd: 10 @@ -23,10 +21,35 @@ motor { current_amp_per_lsb: 0.01611328125 # MG: 66/4096 A/LSB response_timeout_ms: 100 reached_position_tolerance_rad: 0.001 - # rated_torque_current_a: confirm before enabling per-mille torque APIs. - # B0 parameter queries received no reply on this drive. Leave B1 - # parameter unit settings unset until firmware support is verified. - # Native A2/A3/A4 commands preserve the drive's acceleration setting. + # Rated current and B1 parameter conversions remain unspecified. + } + } + motors { + id: 2 + joint_name: "MG4005E_AXIS_2" + gear_ratio: 10 + limit_q_lb: -3.14 + limit_q_ub: 3.14 + limit_qd: 10 + lingkong { + current_amp_per_lsb: 0.01611328125 # MG: 66/4096 A/LSB + response_timeout_ms: 100 + reached_position_tolerance_rad: 0.001 + # Rated current and B1 parameter conversions remain unspecified. + } + } + motors { + id: 3 + joint_name: "MG4005E_AXIS_3" + gear_ratio: 10 + limit_q_lb: -3.14 + limit_q_ub: 3.14 + limit_qd: 10 + lingkong { + current_amp_per_lsb: 0.01611328125 # MG: 66/4096 A/LSB + response_timeout_ms: 100 + reached_position_tolerance_rad: 0.001 + # Rated current and B1 parameter conversions remain unspecified. } } } diff --git a/cmvr-es/config/manager/device_manager.pb.txt b/cmvr-es/config/manager/device_manager.pb.txt index ab9c89fe..8babf1c2 100644 --- a/cmvr-es/config/manager/device_manager.pb.txt +++ b/cmvr-es/config/manager/device_manager.pb.txt @@ -239,6 +239,12 @@ device_manager { config_file: "devices/motor/lingkong_motors.pb.txt" enable: true } + devices { + id: "lingkong_arm" + type: DEVICE_TYPE_ROBOT_ARM + config_file: "devices/arm/lingkong_arm.pb.txt" + enable: true + } devices { id: "tchl_joint_motors" type: DEVICE_TYPE_MOTOR_SYSTEM diff --git a/cmvr-es/devices/arm/aubo_arm/src/aubo_arm.cpp b/cmvr-es/devices/arm/aubo_arm/src/aubo_arm.cpp index 06df334c..f7d3f70c 100644 --- a/cmvr-es/devices/arm/aubo_arm/src/aubo_arm.cpp +++ b/cmvr-es/devices/arm/aubo_arm/src/aubo_arm.cpp @@ -290,26 +290,6 @@ SafetyMode toPublicSafetyMode( return SafetyMode::Unknown; } -std::vector listAuboWorldFrames( - const arcs::common_interface::SyncMovePtr& sync_move) -{ - if (!sync_move) { - return {}; - } - - std::set frame_names{"world", "base", "flange", "tcp"}; - std::vector pending(frame_names.begin(), frame_names.end()); - for (std::size_t index = 0; index < pending.size(); ++index) { - for (const auto& child : sync_move->frameGetChildren(pending[index])) { - if (!child.empty() && sync_move->frameExist(child) && - frame_names.insert(child).second) { - pending.push_back(child); - } - } - } - return {frame_names.begin(), frame_names.end()}; -} - CartesianPose auboPoseFromVector(const std::vector& values, const std::string& context) { @@ -324,14 +304,6 @@ CartesianPose auboPoseFromVector(const std::vector& values, }; } -void insertFrameName(std::set& frame_names, - const std::string& frame_name) -{ - if (!frame_name.empty()) { - frame_names.insert(frame_name); - } -} - bool isAuboTcpFrame(const arcs::common_interface::SyncMovePtr& sync_move, const std::string& frame_name) { @@ -871,110 +843,21 @@ bool AuboArm::isFault() const Result AuboArm::listBaseFrame(std::vector& frame_names) const { frame_names.clear(); - const auto ready = ensureConnected_("ListBaseFrame"); - if (!ready.ok()) { - return ready; + // Listing configured frames does not require a controller connection. + if (!vendor_cfg_.base_frame().empty()) { + frame_names.push_back(vendor_cfg_.base_frame()); } - -#if defined(CMVR_HAS_AUBO_SDK) - try { - // Frame discovery can be slow on older controllers. Keep its longer - // timeout and possible disconnect isolated from state/control RPCs. - arcs::aubo_sdk::RpcClient frame_client; - frame_client.setRequestTimeout(10000); - frame_client.connect(ip_, port_); - frame_client.login(username_, password_); - const auto robot_names = frame_client.getRobotNames(); - if (robot_names.empty()) { - return Result::failure( - ArmErrorCode::RobotNotReady, - "[AuboArm] robot name list is empty"); - } - const auto robot_interface = - frame_client.getRobotInterface(robot_names.front()); - if (!robot_interface || !robot_interface->getSyncMove()) { - return Result::failure( - ArmErrorCode::RobotNotReady, - "[AuboArm] world-frame interface is unavailable"); - } - std::set available_frames{"world", "base"}; - insertFrameName(available_frames, vendor_cfg_.base_frame()); - try { - const auto discovered_frames = - listAuboWorldFrames(robot_interface->getSyncMove()); - available_frames.insert( - discovered_frames.begin(), discovered_frames.end()); - } catch (const std::exception& e) { - // Older Aubo controller releases do not expose - // SyncMove.frameGetChildren even though SDK 0.27.1 declares it. - // Keep the built-in/configured frames usable instead of failing - // the whole gRPC request with JSON-RPC -32601. - CMVR_LOG(WARNING) - << "[AuboArm] ListBaseFrame dynamic discovery unavailable; " - "using built-in/configured frames: " - << e.what(); - } - frame_names.assign(available_frames.begin(), available_frames.end()); - return Result::success(); - } catch (const std::exception& e) { - return Result::failure( - ArmErrorCode::CommandFailed, - std::string("[AuboArm] ListBaseFrame failed: ") + e.what()); - } -#else - return unsupported_("ListBaseFrame"); -#endif + return Result::success(); } Result AuboArm::listTCPFrame(std::vector& frame_names) const { frame_names.clear(); - const auto ready = ensureConnected_("ListTCPFrame"); - if (!ready.ok()) { - return ready; + // Do not enumerate SDK frames or add names absent from the configuration. + if (!vendor_cfg_.tool_frame().empty()) { + frame_names.push_back(vendor_cfg_.tool_frame()); } - -#if defined(CMVR_HAS_AUBO_SDK) - try { - const auto robot_names = sdk_->rpc_client->getRobotNames(); - if (robot_names.empty()) { - return Result::failure( - ArmErrorCode::RobotNotReady, - "[AuboArm] robot name list is empty"); - } - const auto robot_interface = - sdk_->rpc_client->getRobotInterface(robot_names.front()); - if (!robot_interface || !robot_interface->getSyncMove()) { - return Result::failure( - ArmErrorCode::RobotNotReady, - "[AuboArm] world-frame interface is unavailable"); - } - const auto sync_move = robot_interface->getSyncMove(); - std::set available_frames{"tool0", "flange", "tcp"}; - insertFrameName(available_frames, vendor_cfg_.tool_frame()); - try { - for (const auto& frame_name : listAuboWorldFrames(sync_move)) { - if (frame_name != "flange" && frame_name != "tcp" && - isAuboTcpFrame(sync_move, frame_name)) { - available_frames.insert(frame_name); - } - } - } catch (const std::exception& e) { - CMVR_LOG(WARNING) - << "[AuboArm] ListTCPFrame dynamic discovery unavailable; " - "using built-in/configured frames: " - << e.what(); - } - frame_names.assign(available_frames.begin(), available_frames.end()); - return Result::success(); - } catch (const std::exception& e) { - return Result::failure( - ArmErrorCode::CommandFailed, - std::string("[AuboArm] ListTCPFrame failed: ") + e.what()); - } -#else - return unsupported_("ListTCPFrame"); -#endif + return Result::success(); } Result AuboArm::torqueOn() diff --git a/cmvr-es/devices/motor/drivers/lingkong_can/src/lingkong_can_protocol.cpp b/cmvr-es/devices/motor/drivers/lingkong_can/src/lingkong_can_protocol.cpp index 2790897e..1d7ceea5 100644 --- a/cmvr-es/devices/motor/drivers/lingkong_can/src/lingkong_can_protocol.cpp +++ b/cmvr-es/devices/motor/drivers/lingkong_can/src/lingkong_can_protocol.cpp @@ -472,7 +472,24 @@ bool LingkongCanProtocol::commandCyclicPosition(uint8_t id, double q, double qd) { std::lock_guard lock(mutex_); // Native A4 accepts a speed CEILING, not signed velocity feed-forward. - if (!position(id, q, std::abs(qd))) return false; + auto* n = node(id); + if (!n || !std::isfinite(q) || !std::isfinite(qd)) return false; + const double conversion = scale(id); + if (!std::isfinite(conversion) || conversion <= 0) return false; + + // A4's smallest non-zero ceiling is 1 motor degree/s. Keep the planned + // position reference even below that resolution; never pass zero to + // position(), where it selects the configured maximum speed. In particular + // the final zero-velocity sample must remain an A4 position hold, not A3, + // a maximum-speed catch-up, a torque-off, or a new measured-position target. + // This is a software cyclic-position adapter, not hardware-synchronized CSP. + const double minimum_speed = 1.0 / conversion; + const double requested_speed = std::max(std::abs(qd), minimum_speed); + if (n->config.limit_qd() > 0 && requested_speed > n->config.limit_qd()) return false; + // Avoid rounding the exact one-unit lower bound down to zero in position(). + const double speed = std::max(requested_speed, + std::nextafter(minimum_speed, std::numeric_limits::infinity())); + if (!position(id, q, speed)) return false; return true; } bool LingkongCanProtocol::commandCyclicVelocity(uint8_t id, double qd) diff --git a/cmvr-es/devices/motor/drivers/lingkong_can/tests/lingkong_motor_protocol_test.cpp b/cmvr-es/devices/motor/drivers/lingkong_can/tests/lingkong_motor_protocol_test.cpp index f3de6a71..0af07bac 100644 --- a/cmvr-es/devices/motor/drivers/lingkong_can/tests/lingkong_motor_protocol_test.cpp +++ b/cmvr-es/devices/motor/drivers/lingkong_can/tests/lingkong_motor_protocol_test.cpp @@ -299,6 +299,66 @@ TEST_F(LingkongTest, ReachedTargetUsesRealMultiTurnPosition) { ASSERT_TRUE(protocol->quickStop(1)); EXPECT_FALSE(protocol->reachedTargetQ(1)); } +TEST_F(LingkongTest, CyclicLowSpeedAndEndpointKeepExplicitMinimumCeiling) { + config.set_gear_ratio(10); + ASSERT_TRUE(protocol->configureNode(1, config)); + for (double speed : {1e-10, -1e-10, 0.0, -0.0}) { + sent.clear(); + ASSERT_TRUE(protocol->commandCyclicPosition(1, 0.5, speed)); + ASSERT_EQ(sent.size(), 2); + EXPECT_EQ(sent.front().data[0], 0x9A); + EXPECT_EQ(sent.back().data[0], 0xA4); + EXPECT_EQ(sent.back().data[2], 1); + EXPECT_EQ(sent.back().data[3], 0); + Payload expected{}; put(expected, 4, std::llround(0.5 * 10 * 18000 / kPi), 4); + for (int i = 4; i < 8; ++i) EXPECT_EQ(sent.back().data[i], expected[i]); + } +} + +TEST_F(LingkongTest, CyclicPositionPreservesDirectionAndRepresentableSpeed) { + for (double speed : {0.1, -0.1}) { + sent.clear(); + ASSERT_TRUE(protocol->commandCyclicPosition(1, -0.5, speed)); + ASSERT_EQ(sent.size(), 2); + EXPECT_EQ(sent.back().data[0], 0xA4); + EXPECT_EQ(sent.back().data[2], 34); + EXPECT_EQ(sent.back().data[3], 0); + Payload expected{}; put(expected, 4, std::llround(-0.5 * 6 * 18000 / kPi), 4); + for (int i = 4; i < 8; ++i) EXPECT_EQ(sent.back().data[i], expected[i]); + } +} + +TEST_F(LingkongTest, CyclicPositionRejectsUnrepresentableLimitAndInvalidSpeed) { + EXPECT_FALSE(protocol->commandCyclicPosition(1, 0.1, std::numeric_limits::quiet_NaN())); + EXPECT_FALSE(protocol->commandCyclicPosition(1, 0.1, std::numeric_limits::infinity())); + EXPECT_FALSE(protocol->commandCyclicPosition(1, 0.1, 2.0)); + config.set_limit_qd(0.0001); + ASSERT_TRUE(protocol->configureNode(1, config)); + EXPECT_FALSE(protocol->commandCyclicPosition(1, 0.1, 0)); + EXPECT_TRUE(sent.empty()); +} + +TEST_F(LingkongTest, ThreeCyclicAxesRetainIndependentEndpointTargets) { + for (int id = 1; id <= 3; ++id) { + if (id != 1) ASSERT_TRUE(protocol->initNode(id)); + auto axis = config; + axis.set_id(id); + axis.set_gear_ratio(10); + ASSERT_TRUE(protocol->configureNode(id, axis)); + } + for (int id = 1; id <= 3; ++id) { + sent.clear(); + const double target = (id - 2) * 0.5; + ASSERT_TRUE(protocol->commandCyclicPosition(id, target, 0)); + ASSERT_EQ(sent.size(), 2); + EXPECT_EQ(sent.back().id, 0x140 + id); + EXPECT_EQ(sent.back().data[0], 0xA4); + EXPECT_EQ(sent.back().data[2], 1); + Payload expected{}; put(expected, 4, std::llround(target * 10 * 18000 / kPi), 4); + for (int i = 4; i < 8; ++i) EXPECT_EQ(sent.back().data[i], expected[i]); + } +} + TEST_F(LingkongTest, ShortAndUnsolicitedPacketsAreIgnored) { auto* parser = manager->GetMutableProtocolDataById(0x141); Payload old{}; old[0] = 0x92;