fix(motor): correct Lingkong control and CAN shutdown
This commit is contained in:
parent
f89d5b383f
commit
696d198801
@ -1,5 +1,5 @@
|
||||
# One MG6010E-i6 V3, selected by device_manager as lingkong_can_motors.
|
||||
# CAN ID 1 is provisional: confirm the physical ID before hardware operation.
|
||||
# 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).
|
||||
# All public q/qd values refer to OUTPUT shaft radians/radians per second.
|
||||
motor {
|
||||
@ -18,15 +18,15 @@ motor {
|
||||
# Example SOFTWARE position/speed limits; confirm for the installation.
|
||||
limit_q_lb: -3.14
|
||||
limit_q_ub: 3.14
|
||||
limit_qd: 0.5
|
||||
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_torque_current_a: confirm before enabling per-mille torque APIs.
|
||||
# B1 limit units are not specified by the supplied manual. Leave unset:
|
||||
# acceleration_deg_s2_per_lsb / speed_limit_deg_s_per_lsb.
|
||||
# Pass qdd=0 to keep existing DRIVE acceleration in motion commands.
|
||||
# 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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -237,19 +237,19 @@ device_manager {
|
||||
id: "lingkong_can_motors"
|
||||
type: DEVICE_TYPE_MOTOR_SYSTEM
|
||||
config_file: "devices/motor/lingkong_motors.pb.txt"
|
||||
enable: false
|
||||
enable: true
|
||||
}
|
||||
devices {
|
||||
id: "tchl_joint_motors"
|
||||
type: DEVICE_TYPE_MOTOR_SYSTEM
|
||||
config_file: "devices/motor/tchl_multi_motors.pb.txt"
|
||||
enable: true
|
||||
enable: false
|
||||
}
|
||||
devices {
|
||||
id: "tchl_arm"
|
||||
type: DEVICE_TYPE_ROBOT_ARM
|
||||
config_file: "devices/arm/tchl_arm.pb.txt"
|
||||
enable: true
|
||||
enable: false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
|
||||
#include "socket_can_client_raw.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include <cerrno>
|
||||
|
||||
namespace cmvr {
|
||||
namespace device {
|
||||
@ -49,9 +50,14 @@ namespace cmvr {
|
||||
}
|
||||
|
||||
SocketCanClientRaw::~SocketCanClientRaw() {
|
||||
if (dev_handler_) {
|
||||
stop();
|
||||
}
|
||||
stop();
|
||||
}
|
||||
|
||||
bool SocketCanClientRaw::configureReceiveTimeout(int fd) {
|
||||
// CanReceiver::Stop joins before closing the bus. A silent CAN bus
|
||||
// must therefore return periodically so its stop flag is observed.
|
||||
const timeval timeout{0, 20000}; // 20 ms, not a protocol reply timeout.
|
||||
return ::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == 0;
|
||||
}
|
||||
|
||||
bool SocketCanClientRaw::start() {
|
||||
@ -73,6 +79,12 @@ namespace cmvr {
|
||||
status_ = ErrorCode::CAN_CLIENT_ERROR_BASE;
|
||||
return false;
|
||||
}
|
||||
if (!configureReceiveTimeout(dev_handler_)) {
|
||||
CMVR_LOG(ERROR) << "Cannot configure CAN receive timeout: " << std::strerror(errno);
|
||||
stop();
|
||||
status_ = ErrorCode::CAN_CLIENT_ERROR_BASE;
|
||||
return false;
|
||||
}
|
||||
|
||||
// init config and state
|
||||
int ret;
|
||||
@ -91,6 +103,7 @@ namespace cmvr {
|
||||
if (ret < 0) {
|
||||
CMVR_LOG(ERROR) << "add receive msg id filter error code: " << ret;
|
||||
status_ = ErrorCode::CAN_CLIENT_ERROR_BASE;
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -102,6 +115,7 @@ namespace cmvr {
|
||||
if (ret < 0) {
|
||||
CMVR_LOG(ERROR) << "enable reception of can frame error code: " << ret;
|
||||
status_ = ErrorCode::CAN_CLIENT_ERROR_BASE;
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -120,6 +134,7 @@ namespace cmvr {
|
||||
if (ioctl(dev_handler_, SIOCGIFINDEX, &ifr) < 0) {
|
||||
CMVR_LOG(ERROR) << "ioctl error";
|
||||
status_ = ErrorCode::CAN_CLIENT_ERROR_BASE;
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -133,6 +148,7 @@ namespace cmvr {
|
||||
if (ret < 0) {
|
||||
CMVR_LOG(ERROR) << "bind socket to network interface error code: " << ret;
|
||||
status_ = ErrorCode::CAN_CLIENT_ERROR_BASE;
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -142,10 +158,12 @@ namespace cmvr {
|
||||
}
|
||||
|
||||
bool SocketCanClientRaw::stop() {
|
||||
if (is_started_) {
|
||||
is_started_ = false;
|
||||
is_started_ = false;
|
||||
if (dev_handler_ >= 0) {
|
||||
const int fd = dev_handler_;
|
||||
dev_handler_ = -1;
|
||||
|
||||
int ret = close(dev_handler_);
|
||||
int ret = close(fd);
|
||||
if (ret < 0) {
|
||||
CMVR_LOG(ERROR) << "close error code:" << ret << ", " << getErrorString(ret);
|
||||
return false;
|
||||
@ -201,26 +219,35 @@ namespace cmvr {
|
||||
// buf size must be 8 bytes, every time, we receive only one frame
|
||||
ErrorCode SocketCanClientRaw::receive(std::vector<CanFrame> *const frames,
|
||||
int32_t *const frame_num) {
|
||||
if (!frames || !frame_num) return ErrorCode::CAN_CLIENT_ERROR_FRAME_NUM;
|
||||
const int32_t capacity = *frame_num;
|
||||
frames->clear();
|
||||
*frame_num = 0;
|
||||
if (!is_started_) {
|
||||
CMVR_LOG(ERROR) << "Nvidia can client is not init! Please init first!";
|
||||
return ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED;
|
||||
}
|
||||
|
||||
if (*frame_num > MAX_CAN_RECV_FRAME_LEN || *frame_num < 0) {
|
||||
if (capacity > MAX_CAN_RECV_FRAME_LEN || capacity < 0) {
|
||||
CMVR_LOG(ERROR) << "recv can frame num not in range[0, " << MAX_CAN_RECV_FRAME_LEN
|
||||
<< "], frame_num:" << *frame_num;
|
||||
<< "], frame_num:" << capacity;
|
||||
// TODO(Authors): check the difference of returning frame_num/error_code
|
||||
return ErrorCode::CAN_CLIENT_ERROR_FRAME_NUM;
|
||||
}
|
||||
|
||||
for (int32_t i = 0; i < *frame_num && i < MAX_CAN_RECV_FRAME_LEN; ++i) {
|
||||
for (int32_t i = 0; i < capacity; ++i) {
|
||||
CanFrame cf;
|
||||
auto ret = read(dev_handler_, &recv_frames_[i], sizeof(recv_frames_[i]));
|
||||
// Wait only for the first frame; drain a batch without waiting
|
||||
// for it to fill. No-data and interrupted reads are not errors.
|
||||
auto ret = ::recv(dev_handler_, &recv_frames_[i], sizeof(recv_frames_[i]),
|
||||
i == 0 ? 0 : MSG_DONTWAIT);
|
||||
|
||||
if (ret < 0) {
|
||||
CMVR_LOG(ERROR) << "receive message failed, error code: " << ret;
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) break;
|
||||
CMVR_LOG(ERROR) << "receive message failed: " << std::strerror(errno);
|
||||
return ErrorCode::CAN_CLIENT_ERROR_BASE;
|
||||
}
|
||||
if (ret != sizeof(can_frame)) return ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED;
|
||||
if (recv_frames_[i].can_dlc > CANBUS_MESSAGE_LENGTH ||
|
||||
recv_frames_[i].can_dlc < 0) {
|
||||
CMVR_LOG(ERROR) << "recv_frames_[" << i
|
||||
@ -240,6 +267,7 @@ namespace cmvr {
|
||||
cf.len = recv_frames_[i].can_dlc;
|
||||
std::memcpy(cf.data, recv_frames_[i].data, recv_frames_[i].can_dlc);
|
||||
frames->push_back(cf);
|
||||
++*frame_num;
|
||||
}
|
||||
return ErrorCode::OK;
|
||||
}
|
||||
|
||||
@ -67,7 +67,9 @@ namespace cmvr {
|
||||
*/
|
||||
std::string getErrorString(const int32_t status) override;
|
||||
private:
|
||||
int dev_handler_ = 0;
|
||||
friend class SocketCanClientRawTestPeer;
|
||||
static bool configureReceiveTimeout(int fd);
|
||||
int dev_handler_ = -1;
|
||||
cmvr::msgs::CANCardParameter::CANChannelId port_;
|
||||
cmvr::msgs::CANCardParameter::CANInterface interface_;
|
||||
can_frame send_frames_[MAX_CAN_SEND_FRAME_LEN];
|
||||
|
||||
@ -1,44 +1,95 @@
|
||||
#include "common/base/logging/logger.h"
|
||||
//
|
||||
// Created by lgv on 2025/7/16.
|
||||
//
|
||||
#include "cmvr/msgs/error_code.pb.h"
|
||||
#include "cmvr/msgs/can_card_parameter.pb.h"
|
||||
#include "canbus/can_client/socket/socket_can_client_raw.h"
|
||||
#include "canbus/can_comm/can_receiver.h"
|
||||
#include "cmvr/msgs/robot_detail.pb.h"
|
||||
#include "gtest/gtest.h"
|
||||
namespace cmvr {
|
||||
namespace device {
|
||||
using cmvr::msgs::ErrorCode;
|
||||
using cmvr::msgs::CANCardParameter;
|
||||
|
||||
TEST(SocketCanClientRawTest, simple_test) {
|
||||
CANCardParameter param;
|
||||
param.set_brand(CANCardParameter::SOCKET_CAN_RAW);
|
||||
param.set_channel_id(CANCardParameter::CHANNEL_ID_ZERO);
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
cmvr::config::SocketCanConfig cfg;
|
||||
cfg.set_channel_id(0);
|
||||
SocketCanClientRaw socket_can_client(cfg);
|
||||
namespace cmvr::device {
|
||||
|
||||
// EXPECT_EQ(socket_can_client.start(), ErrorCode::CAN_CLIENT_ERROR_BASE);
|
||||
socket_can_client.start();
|
||||
std::vector<CanFrame> frames;
|
||||
int32_t num = 0;
|
||||
EXPECT_EQ(socket_can_client.send(frames, &num),
|
||||
ErrorCode::OK);
|
||||
++num;
|
||||
EXPECT_EQ(socket_can_client.receive(&frames, &num),
|
||||
ErrorCode::OK);
|
||||
CMVR_LOG(INFO) << frames.at(0).CanFrameString();
|
||||
CanFrame can_frame;
|
||||
can_frame.id = 0x123;
|
||||
can_frame.len = 8;
|
||||
memset(can_frame.data, 0xA3, sizeof(can_frame.data));
|
||||
frames.clear();
|
||||
frames.push_back(can_frame);
|
||||
EXPECT_EQ(socket_can_client.sendSingleFrame(frames),
|
||||
ErrorCode::OK);
|
||||
socket_can_client.stop();
|
||||
// Test the production receive/timeout/stop paths without a CAN adapter,
|
||||
// motor commands, or privileged vcan configuration.
|
||||
class SocketCanClientRawTestPeer {
|
||||
public:
|
||||
static bool attach(SocketCanClientRaw& client, int fd) {
|
||||
if (!client.configureReceiveTimeout(fd)) return false;
|
||||
client.dev_handler_ = fd;
|
||||
client.is_started_ = true;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class SocketCanReceiveTest : public testing::Test {
|
||||
protected:
|
||||
config::SocketCanConfig config;
|
||||
SocketCanClientRaw client{config};
|
||||
int peer = -1;
|
||||
void SetUp() override {
|
||||
int sockets[2];
|
||||
ASSERT_EQ(::socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets), 0);
|
||||
peer = sockets[1];
|
||||
ASSERT_TRUE(SocketCanClientRawTestPeer::attach(client, sockets[0]));
|
||||
}
|
||||
void TearDown() override {
|
||||
client.stop();
|
||||
if (peer >= 0) ::close(peer);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(SocketCanReceiveTest, IdleReceiveReturnsZeroWithoutAnError) {
|
||||
std::vector<CanFrame> frames(1);
|
||||
int32_t count = MAX_CAN_RECV_FRAME_LEN;
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::OK);
|
||||
EXPECT_EQ(count, 0);
|
||||
EXPECT_TRUE(frames.empty());
|
||||
EXPECT_LT(std::chrono::steady_clock::now() - start, std::chrono::milliseconds(500));
|
||||
}
|
||||
|
||||
TEST_F(SocketCanReceiveTest, ReceivedFrameIsReturnedWithActualCount) {
|
||||
can_frame input{};
|
||||
input.can_id = 0x141;
|
||||
input.can_dlc = 8;
|
||||
input.data[0] = 0x9A;
|
||||
ASSERT_EQ(::write(peer, &input, sizeof(input)), sizeof(input));
|
||||
std::vector<CanFrame> frames;
|
||||
int32_t count = MAX_CAN_RECV_FRAME_LEN;
|
||||
ASSERT_EQ(client.receive(&frames, &count), msgs::ErrorCode::OK);
|
||||
ASSERT_EQ(count, 1);
|
||||
ASSERT_EQ(frames.size(), 1U);
|
||||
EXPECT_EQ(frames[0].id, 0x141);
|
||||
EXPECT_EQ(frames[0].len, 8);
|
||||
EXPECT_EQ(frames[0].data[0], 0x9A);
|
||||
}
|
||||
|
||||
TEST_F(SocketCanReceiveTest, ReceiverStopsOnAnIdleBusAndCanRestart) {
|
||||
MessageManager<msgs::RobotDetail> manager;
|
||||
CanReceiver<msgs::RobotDetail> receiver;
|
||||
ASSERT_EQ(receiver.Init(&client, &manager, false), msgs::ErrorCode::OK);
|
||||
for (int attempt = 0; attempt < 3; ++attempt) {
|
||||
ASSERT_EQ(receiver.Start(), msgs::ErrorCode::OK);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
receiver.Stop();
|
||||
EXPECT_LT(std::chrono::steady_clock::now() - start, std::chrono::milliseconds(500));
|
||||
EXPECT_FALSE(receiver.IsRunning());
|
||||
receiver.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(SocketCanReceiveTest, ShortFrameIsRejectedAndStopIsIdempotent) {
|
||||
const char byte = 0;
|
||||
ASSERT_EQ(::write(peer, &byte, 1), 1);
|
||||
std::vector<CanFrame> frames;
|
||||
int32_t count = 1;
|
||||
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED);
|
||||
EXPECT_EQ(count, 0);
|
||||
EXPECT_TRUE(client.stop());
|
||||
EXPECT_TRUE(client.stop());
|
||||
count = 1;
|
||||
EXPECT_EQ(client.receive(&frames, &count), msgs::ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED);
|
||||
EXPECT_EQ(count, 0);
|
||||
}
|
||||
|
||||
} // namespace cmvr::device
|
||||
|
||||
@ -136,7 +136,6 @@ namespace cmvr {
|
||||
}
|
||||
|
||||
int32_t receive_error_count = 0;
|
||||
int32_t receive_none_count = 0;
|
||||
const int32_t ERROR_COUNT_MAX = 10;
|
||||
auto default_period = 10 * 1000; // 10 ms
|
||||
|
||||
@ -160,13 +159,11 @@ namespace cmvr {
|
||||
}
|
||||
|
||||
if (frame_num == 0) {
|
||||
CMVR_LOG_IF_EVERY_N(ERROR, receive_none_count++ > ERROR_COUNT_MAX,
|
||||
ERROR_COUNT_MAX)
|
||||
<< "Received " << receive_none_count << " empty messages.";
|
||||
// A bounded receive timeout on a silent bus is normal,
|
||||
// not a transport error. Return to the stop-flag check.
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(default_period));
|
||||
continue;
|
||||
}
|
||||
receive_none_count = 0;
|
||||
|
||||
for (const auto &frame: buf) {
|
||||
uint8_t len = frame.len;
|
||||
|
||||
@ -26,14 +26,16 @@ public:
|
||||
bool initNode(uint8_t node_id) override;
|
||||
bool configureNode(uint8_t node_id, const config::MotorConfigItem& config);
|
||||
bool probe(uint8_t node_id);
|
||||
// No standalone set-mode/read-mode command exists in this manual. A* commands
|
||||
// select the native loop themselves; do not claim a drive mode readback.
|
||||
bool setMode(uint8_t, msgs::RunMode) override { return false; }
|
||||
msgs::RunMode getMode(uint8_t) override { return msgs::RUN_MODE_UNSPECIFIED; }
|
||||
// Compatibility selection only: no standalone native mode command exists.
|
||||
// Native protocol has no mode readback: getMode returns UNSPECIFIED.
|
||||
// A* commands still select the native loop when an explicit target is sent.
|
||||
bool setMode(uint8_t node_id, msgs::RunMode mode) override;
|
||||
msgs::RunMode getMode(uint8_t node_id) override;
|
||||
void setMotorConversion(uint8_t node_id, double counts, double gear_ratio) override;
|
||||
bool setLimitQ(uint8_t node_id, double upper, double lower);
|
||||
bool setLimitQd(uint8_t node_id, double value);
|
||||
void setLimitQdd(uint8_t node_id, double upper, double lower) override;
|
||||
// Open and capture/hold the current position; roll back to closed on failure.
|
||||
bool torqueOn(uint8_t node_id) override;
|
||||
bool torqueOff(uint8_t node_id) override;
|
||||
// Native stop command has no configurable deceleration argument.
|
||||
@ -67,6 +69,7 @@ private:
|
||||
};
|
||||
bool exchange(uint8_t node_id, const Payload& request, Payload& response);
|
||||
bool simple(uint8_t node_id, uint8_t command);
|
||||
void requireRunning(uint8_t node_id);
|
||||
bool position(uint8_t node_id, double q, double qd);
|
||||
bool velocity(uint8_t node_id, double qd);
|
||||
bool acceleration(uint8_t node_id, double qdd);
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace cmvr::device {
|
||||
@ -11,6 +13,12 @@ namespace {
|
||||
constexpr double kRadToDeg = 180.0 / 3.14159265358979323846;
|
||||
constexpr double kUnavailable = std::numeric_limits<double>::quiet_NaN();
|
||||
bool parameterIndex(uint8_t index) { return index == 10 || index == 12 || index == 14; }
|
||||
std::string hexFrame(const LingkongCanProtocol::Payload& bytes) {
|
||||
std::ostringstream out;
|
||||
out << std::hex << std::setfill('0');
|
||||
for (auto byte : bytes) out << std::setw(2) << unsigned(byte);
|
||||
return out.str();
|
||||
}
|
||||
}
|
||||
|
||||
LingkongCanProtocol::LingkongCanProtocol(
|
||||
@ -98,7 +106,13 @@ bool LingkongCanProtocol::exchange(uint8_t id, const Payload& request, Payload&
|
||||
const bool ok = n->response->exchange(request, response,
|
||||
std::chrono::milliseconds(timeout ? timeout : 100), [&] { return send_(frame); });
|
||||
if (!ok) CMVR_LOG(ERROR) << "[LingkongCanProtocol] send/reply failed: node=" << int(id)
|
||||
<< ", command=" << int(request[0]);
|
||||
<< ", command=" << int(request[0])
|
||||
<< ", tx=" << hexFrame(request) << ", timeout_ms=" << (timeout ? timeout : 100);
|
||||
else if (request[0] == 0x88 || request[0] == 0x80 || request[0] == 0x81 ||
|
||||
request[0] == 0xB1 || (request[0] >= 0xA1 && request[0] <= 0xAD))
|
||||
CMVR_LOG(INFO) << "[LingkongCanProtocol] node=" << int(id)
|
||||
<< ", tx=" << hexFrame(request) << ", rx=" << hexFrame(response)
|
||||
<< "; reply is not proof of target completion";
|
||||
return ok;
|
||||
}
|
||||
|
||||
@ -118,6 +132,37 @@ bool LingkongCanProtocol::probe(uint8_t id)
|
||||
return readStatus(id, status) && readFeedback(id, q, qd);
|
||||
}
|
||||
|
||||
bool LingkongCanProtocol::setMode(uint8_t id, msgs::RunMode mode)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
auto* n = node(id);
|
||||
if (!n) return false;
|
||||
switch (mode) {
|
||||
case msgs::RUN_MODE_PROFILE_POSITION:
|
||||
case msgs::RUN_MODE_PROFILE_VELOCITY:
|
||||
case msgs::RUN_MODE_CYCLIC_SYNC_POSITION:
|
||||
case msgs::RUN_MODE_CYCLIC_SYNC_VELOCITY:
|
||||
case msgs::RUN_MODE_CYCLIC_SYNC_CURRENT:
|
||||
CMVR_LOG(INFO) << "[LingkongCanProtocol] accepted compatibility mode=" << int(mode)
|
||||
<< ", node=" << int(id)
|
||||
<< "; no CAN mode command sent, native loop changes on target command";
|
||||
return true;
|
||||
default:
|
||||
CMVR_LOG(ERROR) << "[LingkongCanProtocol] unsupported mode=" << int(mode)
|
||||
<< ", node=" << int(id)
|
||||
<< "; supports position/velocity setpoints and cyclic torque; no native profile-torque ramp";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
msgs::RunMode LingkongCanProtocol::getMode(uint8_t id)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
// Never present a host-side selection as measured drive state.
|
||||
(void)id;
|
||||
return msgs::RUN_MODE_UNSPECIFIED;
|
||||
}
|
||||
|
||||
void LingkongCanProtocol::setMotorConversion(uint8_t id, double, double gear)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
@ -158,7 +203,14 @@ bool LingkongCanProtocol::readControlParameter(uint8_t id, uint8_t index, int32_
|
||||
bool LingkongCanProtocol::writeControlParameter(uint8_t id, uint8_t index, int32_t value)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
return parameter(id, 0xB1, index, value);
|
||||
const int32_t requested = value;
|
||||
if (!parameter(id, 0xB1, index, value)) return false;
|
||||
if (value != requested) {
|
||||
CMVR_LOG(ERROR) << "[LingkongCanProtocol] parameter not applied exactly: index="
|
||||
<< int(index) << ", requested=" << requested << ", effective=" << value;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LingkongCanProtocol::setLimitQd(uint8_t id, double value)
|
||||
@ -203,13 +255,59 @@ void LingkongCanProtocol::setLimitQdd(uint8_t id, double upper, double lower)
|
||||
bool LingkongCanProtocol::torqueOn(uint8_t id)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
return simple(id, 0x88);
|
||||
auto* n = node(id);
|
||||
if (!n) return false;
|
||||
n->target_valid = false;
|
||||
auto currentAngle = [&]() {
|
||||
Payload request{}, response{};
|
||||
request[0] = 0x92;
|
||||
if (!exchange(id, request, response))
|
||||
throw std::runtime_error("Lingkong hold-enable: cannot read current position (92)");
|
||||
const int64_t angle = signedLe(response.data() + 1, 7);
|
||||
if (angle < std::numeric_limits<int32_t>::min() || angle > std::numeric_limits<int32_t>::max())
|
||||
throw std::runtime_error("Lingkong hold-enable: current position exceeds native A4 target range");
|
||||
return static_cast<int32_t>(angle);
|
||||
};
|
||||
// Fail before enabling if no valid position is available. Never zero the
|
||||
// encoder, reset multi-turn position, or reuse a previous motion target.
|
||||
currentAngle();
|
||||
try {
|
||||
if (!simple(id, 0x88)) throw std::runtime_error("88 run command received no valid reply");
|
||||
requireRunning(id);
|
||||
// Capture again after opening, so holding does not use a stale position.
|
||||
const int32_t angle = currentAngle();
|
||||
Payload request{}, response{};
|
||||
request[0] = 0xA4;
|
||||
putLe(request, 2, 1, 2); // Low motor-side speed ceiling for any capture error.
|
||||
putLe(request, 4, static_cast<uint32_t>(angle), 4);
|
||||
if (!exchange(id, request, response))
|
||||
throw std::runtime_error("A4 current-position hold received no valid reply");
|
||||
requireRunning(id);
|
||||
n->target_q = angle / (100.0 * scale(id));
|
||||
n->target_valid = true;
|
||||
CMVR_LOG(INFO) << "[LingkongCanProtocol] enabled with current-position hold: node="
|
||||
<< int(id) << ", q=" << n->target_q;
|
||||
return true;
|
||||
} catch (const std::exception& error) {
|
||||
const std::string reason = error.what();
|
||||
try {
|
||||
if (!torqueOff(id)) throw std::runtime_error("80 close received no valid reply");
|
||||
} catch (const std::exception& close_error) {
|
||||
throw std::runtime_error("Lingkong hold-enable failed: " + reason +
|
||||
"; rollback also failed: " + close_error.what());
|
||||
}
|
||||
throw std::runtime_error("Lingkong hold-enable failed: " + reason + "; drive closed");
|
||||
}
|
||||
}
|
||||
bool LingkongCanProtocol::torqueOff(uint8_t id)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
if (auto* n = node(id)) n->target_valid = false; // 0x80 clears multi-turn count.
|
||||
return simple(id, 0x80);
|
||||
if (!simple(id, 0x80)) return false;
|
||||
Status status;
|
||||
if (!readStatus(id, status)) throw std::runtime_error("Lingkong: shutdown acknowledged but 9A status read failed");
|
||||
if (status.motor_state != 0x10) throw std::runtime_error("Lingkong: 80 acknowledged but drive is not closed (9A)");
|
||||
return true;
|
||||
}
|
||||
bool LingkongCanProtocol::quickStop(uint8_t id, double /*deceleration*/)
|
||||
{
|
||||
@ -296,6 +394,14 @@ double LingkongCanProtocol::getTau(uint8_t id)
|
||||
return current * 1000.0 / n->config.lingkong().rated_torque_current_a();
|
||||
}
|
||||
|
||||
void LingkongCanProtocol::requireRunning(uint8_t id)
|
||||
{
|
||||
Status status;
|
||||
if (!readStatus(id, status)) throw std::runtime_error("Lingkong: cannot verify drive state (9A read failed); no motion command sent");
|
||||
if (status.error) throw std::runtime_error("Lingkong: drive fault, errorState=" + std::to_string(status.error) + "; inspect 9A before motion");
|
||||
if (status.motor_state != 0) throw std::runtime_error("Lingkong: drive is closed (9A motorState=0x10); call torqueOn explicitly before motion");
|
||||
}
|
||||
|
||||
bool LingkongCanProtocol::position(uint8_t id, double q, double qd)
|
||||
{
|
||||
auto* n = node(id);
|
||||
@ -314,7 +420,9 @@ bool LingkongCanProtocol::position(uint8_t id, double q, double qd)
|
||||
}
|
||||
putLe(request, 4, static_cast<uint32_t>(angle), 4);
|
||||
n->target_valid = false;
|
||||
if (!exchange(id, request, response)) return false;
|
||||
requireRunning(id);
|
||||
if (!exchange(id, request, response))
|
||||
throw std::runtime_error("Lingkong position command received no valid CAN reply: tx=" + hexFrame(request));
|
||||
n->target_q = q;
|
||||
n->target_valid = true;
|
||||
return true;
|
||||
@ -330,17 +438,23 @@ bool LingkongCanProtocol::velocity(uint8_t id, double qd)
|
||||
request[0] = 0xA2;
|
||||
putLe(request, 4, static_cast<uint32_t>(raw), 4);
|
||||
n->target_valid = false;
|
||||
return exchange(id, request, response);
|
||||
requireRunning(id);
|
||||
if (!exchange(id, request, response))
|
||||
throw std::runtime_error("Lingkong velocity command received no valid CAN reply: tx=" + hexFrame(request));
|
||||
return true;
|
||||
}
|
||||
bool LingkongCanProtocol::commandProfilePosition(uint8_t id, double q, double qd, double qdd)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
// Validate the target BEFORE changing a hardware parameter.
|
||||
// A4 carries position and a speed ceiling, not acceleration. Keep parameter
|
||||
// writes separate: B1 support must not be a prerequisite for native motion.
|
||||
auto* n = node(id);
|
||||
int32_t raw;
|
||||
if (!n || !std::isfinite(qd) || qd < 0 || !toInt32(q * scale(id) * 100, raw) ||
|
||||
q < n->config.limit_q_lb() || q > n->config.limit_q_ub()) return false;
|
||||
return acceleration(id, qdd) && position(id, q, qd);
|
||||
if (qdd != 0) CMVR_LOG(WARNING) << "[LingkongCanProtocol] A3/A4 has no acceleration field; qdd is not sent, retaining drive acceleration";
|
||||
if (!position(id, q, qd)) return false;
|
||||
return true;
|
||||
}
|
||||
bool LingkongCanProtocol::commandProfileVelocity(uint8_t id, double qd, double qdd)
|
||||
{
|
||||
@ -349,18 +463,23 @@ bool LingkongCanProtocol::commandProfileVelocity(uint8_t id, double qd, double q
|
||||
int32_t raw;
|
||||
if (!n || !toInt32(qd * scale(id) * 100, raw) ||
|
||||
(n->config.limit_qd() > 0 && std::abs(qd) > n->config.limit_qd())) return false;
|
||||
return acceleration(id, qdd) && velocity(id, qd);
|
||||
// A2 carries only target speed. Do not implicitly issue B1/index 12.
|
||||
if (qdd != 0) CMVR_LOG(WARNING) << "[LingkongCanProtocol] A2 has no acceleration field; qdd is not sent, retaining drive acceleration";
|
||||
if (!velocity(id, qd)) return false;
|
||||
return true;
|
||||
}
|
||||
bool LingkongCanProtocol::commandCyclicPosition(uint8_t id, double q, double qd)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
// Native A4 accepts a speed CEILING, not signed velocity feed-forward.
|
||||
return position(id, q, std::abs(qd));
|
||||
if (!position(id, q, std::abs(qd))) return false;
|
||||
return true;
|
||||
}
|
||||
bool LingkongCanProtocol::commandCyclicVelocity(uint8_t id, double qd)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(mutex_);
|
||||
return velocity(id, qd);
|
||||
if (!velocity(id, qd)) return false;
|
||||
return true;
|
||||
}
|
||||
bool LingkongCanProtocol::commandCyclicTorque(uint8_t id, double tau)
|
||||
{
|
||||
@ -376,7 +495,9 @@ bool LingkongCanProtocol::commandCyclicTorque(uint8_t id, double tau)
|
||||
request[0] = 0xA1;
|
||||
putLe(request, 4, static_cast<uint32_t>(raw), 2);
|
||||
n->target_valid = false;
|
||||
return exchange(id, request, response);
|
||||
requireRunning(id);
|
||||
const bool acknowledged = exchange(id, request, response);
|
||||
return acknowledged;
|
||||
}
|
||||
bool LingkongCanProtocol::reachedTargetQ(uint8_t id)
|
||||
{
|
||||
|
||||
@ -11,7 +11,7 @@ void LingkongMotorResponse::Parse(const uint8_t* bytes, int32_t length, msgs::Ro
|
||||
if (!waiting_ || bytes[0] != request_[0]) return;
|
||||
if ((bytes[0] == 0xB0 || bytes[0] == 0xB1) && bytes[1] != request_[1]) return;
|
||||
if ((bytes[0] == 0x80 || bytes[0] == 0x81 || bytes[0] == 0x88 ||
|
||||
bytes[0] == 0x95 || bytes[0] == 0xB1) &&
|
||||
bytes[0] == 0x95) &&
|
||||
!std::equal(bytes, bytes + 8, request_.begin())) return;
|
||||
if (bytes[0] == 0x8C && request_[1] != 0x10 && bytes[1] != request_[1]) return;
|
||||
std::copy(bytes, bytes + 8, response_.begin());
|
||||
|
||||
@ -26,7 +26,7 @@ protected:
|
||||
bool respond = true, transport_ok = true;
|
||||
int32_t velocity = -60, current = -409;
|
||||
int64_t angle = -36000;
|
||||
uint8_t state = 0x10, error = 0;
|
||||
uint8_t state = 0, error = 0;
|
||||
std::function<void(Payload&)> mutate;
|
||||
config::MotorConfigItem config;
|
||||
|
||||
@ -46,6 +46,8 @@ protected:
|
||||
if (!respond) return true;
|
||||
Payload reply{};
|
||||
std::copy(frame.data, frame.data + 8, reply.begin());
|
||||
if (reply[0] == 0x88) state = 0;
|
||||
if (reply[0] == 0x80) state = 0x10;
|
||||
if (reply[0] == 0x92) put(reply, 1, angle, 7);
|
||||
if (reply[0] == 0x9C || (reply[0] >= 0xA1 && reply[0] <= 0xA8)) {
|
||||
reply[1] = uint8_t(-12);
|
||||
@ -114,6 +116,7 @@ TEST_F(LingkongTest, SignedMultiTurnAndVelocityUseGearRatio) {
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, StatusAndCurrentAreNativeValues) {
|
||||
state = 0x10;
|
||||
LingkongCanProtocol::Status s;
|
||||
ASSERT_TRUE(protocol->readStatus(1, s));
|
||||
EXPECT_EQ(s.motor_state, 0x10);
|
||||
@ -127,6 +130,7 @@ TEST_F(LingkongTest, StatusAndCurrentAreNativeValues) {
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, NativeStatusIsNotDecodedAsCia402) {
|
||||
state = 0x10;
|
||||
LingkongMotor motor(config);
|
||||
// A second node uses the same bus and its own response parser.
|
||||
auto second = config; second.set_id(2);
|
||||
@ -155,24 +159,79 @@ TEST_F(LingkongTest, PositionAndNegativeSpeedPackets) {
|
||||
for (int i = 4; i < 8; ++i) EXPECT_EQ(sent.back().data[i], expected[i]);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, OperationsSendOnlyTheirOwnCommand) {
|
||||
ASSERT_TRUE(protocol->torqueOn(1)); EXPECT_EQ(sent.back().data[0], 0x88);
|
||||
ASSERT_TRUE(protocol->torqueOff(1)); EXPECT_EQ(sent.back().data[0], 0x80);
|
||||
TEST_F(LingkongTest, EnableHoldsCurrentPositionAndOtherOperationsStayExplicit) {
|
||||
ASSERT_TRUE(protocol->torqueOn(1)); EXPECT_EQ(sent[0].data[0], 0x92); EXPECT_EQ(sent[1].data[0], 0x88); EXPECT_EQ(sent.back().data[0], 0x9A);
|
||||
ASSERT_EQ(sent.size(), 6);
|
||||
EXPECT_EQ(sent[4].data[0], 0xA4);
|
||||
ASSERT_TRUE(protocol->torqueOff(1)); EXPECT_EQ(sent[6].data[0], 0x80); EXPECT_EQ(sent.back().data[0], 0x9A);
|
||||
ASSERT_TRUE(protocol->quickStop(1)); EXPECT_EQ(sent.back().data[0], 0x81);
|
||||
ASSERT_TRUE(protocol->brakeRelease(1)); EXPECT_EQ(sent.back().data[0], 0x8C); EXPECT_EQ(sent.back().data[1], 1);
|
||||
ASSERT_TRUE(protocol->calibrateZeroQ(1)); EXPECT_EQ(sent.back().data[0], 0x95);
|
||||
ASSERT_TRUE(protocol->clearError(1)); EXPECT_EQ(sent.back().data[0], 0x9B);
|
||||
EXPECT_EQ(sent.size(), 6);
|
||||
EXPECT_FALSE(protocol->setMode(1, msgs::RUN_MODE_CYCLIC_SYNC_POSITION));
|
||||
EXPECT_EQ(sent.size(), 12);
|
||||
EXPECT_TRUE(protocol->setMode(1, msgs::RUN_MODE_CYCLIC_SYNC_POSITION));
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
EXPECT_EQ(sent.size(), 12);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, ModeSelectionIsLocalAndNeverEnablesOrMoves) {
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
for (const auto mode : {msgs::RUN_MODE_PROFILE_POSITION, msgs::RUN_MODE_PROFILE_VELOCITY,
|
||||
msgs::RUN_MODE_CYCLIC_SYNC_POSITION, msgs::RUN_MODE_CYCLIC_SYNC_VELOCITY,
|
||||
msgs::RUN_MODE_CYCLIC_SYNC_CURRENT}) {
|
||||
ASSERT_TRUE(protocol->setMode(1, mode));
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
EXPECT_TRUE(sent.empty());
|
||||
}
|
||||
EXPECT_FALSE(protocol->setMode(0, msgs::RUN_MODE_PROFILE_POSITION));
|
||||
EXPECT_FALSE(protocol->setMode(1, msgs::RUN_MODE_UNSPECIFIED));
|
||||
EXPECT_FALSE(protocol->setMode(1, msgs::RUN_MODE_TORQUE)); // No native torque ramp.
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
EXPECT_EQ(protocol->getMode(0), msgs::RUN_MODE_UNSPECIFIED);
|
||||
EXPECT_TRUE(sent.empty());
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, CommandsNeverInventHardwareModeReadback) {
|
||||
ASSERT_TRUE(protocol->commandProfilePosition(1, 0.5, 0.1, 0));
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
ASSERT_TRUE(protocol->commandCyclicPosition(1, 0.5, 0.1));
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
ASSERT_TRUE(protocol->commandProfileVelocity(1, -0.1, 0));
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
respond = false;
|
||||
EXPECT_THROW(protocol->commandCyclicVelocity(1, 0.1), std::runtime_error);
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
respond = true;
|
||||
ASSERT_TRUE(protocol->commandCyclicVelocity(1, 0.1));
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, NativePositionDoesNotWriteAccelerationEvenWhenUnitsConfigured) {
|
||||
config.mutable_lingkong()->set_acceleration_deg_s2_per_lsb(1);
|
||||
config.mutable_lingkong()->set_speed_limit_deg_s_per_lsb(1);
|
||||
ASSERT_TRUE(protocol->configureNode(1, config));
|
||||
ASSERT_TRUE(protocol->setMode(1, msgs::RUN_MODE_PROFILE_POSITION));
|
||||
ASSERT_TRUE(protocol->commandProfilePosition(1, 1.0, 0.1, 0.5));
|
||||
ASSERT_EQ(sent.size(), 2);
|
||||
EXPECT_EQ(sent[0].data[0], 0x9A);
|
||||
EXPECT_EQ(sent[1].data[0], 0xA4);
|
||||
EXPECT_EQ(sent[1].data[2], 34); EXPECT_EQ(sent[1].data[3], 0);
|
||||
Payload expected{}; put(expected, 4, 34377, 4);
|
||||
for (int i = 4; i < 8; ++i) EXPECT_EQ(sent[1].data[i], expected[i]);
|
||||
ASSERT_TRUE(protocol->setLimitQd(1, 0.5));
|
||||
EXPECT_EQ(sent.back().data[0], 0xB1); EXPECT_EQ(sent.back().data[1], 14);
|
||||
EXPECT_EQ(sent.back().data[4], 172);
|
||||
EXPECT_EQ(protocol->getMode(1), msgs::RUN_MODE_UNSPECIFIED);
|
||||
EXPECT_EQ(sent.size(), 6);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, ParameterOffsetsAndUnknownUnits) {
|
||||
EXPECT_FALSE(protocol->setLimitQd(1, 0.5));
|
||||
EXPECT_THROW(protocol->setLimitQdd(1, 1, -1), std::runtime_error);
|
||||
EXPECT_FALSE(protocol->commandProfilePosition(1, 0.5, 0.5, 1));
|
||||
EXPECT_TRUE(sent.empty());
|
||||
ASSERT_TRUE(protocol->commandProfilePosition(1, 0.5, 0.5, 1));
|
||||
ASSERT_EQ(sent.size(), 2); EXPECT_EQ(sent.back().data[0], 0xA4);
|
||||
ASSERT_TRUE(protocol->commandProfileVelocity(1, 0.1, 1));
|
||||
ASSERT_EQ(sent.size(), 4); EXPECT_EQ(sent.back().data[0], 0xA2);
|
||||
int32_t value;
|
||||
ASSERT_TRUE(protocol->readControlParameter(1, 10, value)); EXPECT_EQ(value, -123);
|
||||
ASSERT_TRUE(protocol->readControlParameter(1, 12, value)); EXPECT_EQ(value, 123456);
|
||||
@ -188,8 +247,7 @@ TEST_F(LingkongTest, ExplicitParameterUnitsAndTorqueCalibration) {
|
||||
config.mutable_lingkong()->set_rated_torque_current_a(5);
|
||||
ASSERT_TRUE(protocol->configureNode(1, config));
|
||||
ASSERT_TRUE(protocol->commandProfileVelocity(1, 0.25, 1));
|
||||
ASSERT_EQ(sent.size(), 2); EXPECT_EQ(sent[0].data[0], 0xB1); EXPECT_EQ(sent[0].data[1], 12);
|
||||
EXPECT_EQ(sent[1].data[0], 0xA2);
|
||||
ASSERT_EQ(sent.size(), 2); EXPECT_EQ(sent[1].data[0], 0xA2);
|
||||
ASSERT_TRUE(protocol->commandCyclicTorque(1, -1000));
|
||||
Payload expected{}; put(expected, 4, -310, 2);
|
||||
EXPECT_EQ(sent.back().data[0], 0xA1);
|
||||
@ -202,7 +260,7 @@ TEST_F(LingkongTest, MissingRepliesNeverReuseOldData) {
|
||||
EXPECT_TRUE(std::isfinite(protocol->getQ(1)));
|
||||
respond = false;
|
||||
EXPECT_TRUE(std::isnan(protocol->getQ(1)));
|
||||
EXPECT_FALSE(protocol->torqueOn(1));
|
||||
EXPECT_THROW(protocol->torqueOn(1), std::runtime_error);
|
||||
double q = 42, qd = 43;
|
||||
EXPECT_FALSE(protocol->readFeedback(1, q, qd));
|
||||
EXPECT_EQ(q, 42); EXPECT_EQ(qd, 43);
|
||||
@ -248,4 +306,83 @@ TEST_F(LingkongTest, ShortAndUnsolicitedPacketsAreIgnored) {
|
||||
mutate = [&](Payload& p) { parser->Parse(p.data(), 7, nullptr); p[0] = 0; };
|
||||
EXPECT_TRUE(std::isnan(protocol->getQ(1)));
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, ClosedDriveAndFaultNeverDispatchOrAutomaticallyEnable) {
|
||||
state = 0x10;
|
||||
EXPECT_THROW(protocol->commandProfilePosition(1, 0.1, 0.1, 1), std::runtime_error);
|
||||
EXPECT_THROW(protocol->commandProfileVelocity(1, 0.1, 1), std::runtime_error);
|
||||
state = 0; error = 8;
|
||||
EXPECT_THROW(protocol->commandCyclicPosition(1, 0.1, 0.1), std::runtime_error);
|
||||
config.mutable_lingkong()->set_rated_torque_current_a(5);
|
||||
ASSERT_TRUE(protocol->configureNode(1, config));
|
||||
EXPECT_THROW(protocol->commandCyclicTorque(1, 0), std::runtime_error);
|
||||
ASSERT_EQ(sent.size(), 4);
|
||||
for (const auto& frame : sent) EXPECT_EQ(frame.data[0], 0x9A);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, RunAndCloseEchoMustBeConfirmedByRealStatus) {
|
||||
mutate = [](Payload& p) { if (p[0] == 0x9A) p[6] = 0x10; };
|
||||
EXPECT_THROW(protocol->torqueOn(1), std::runtime_error);
|
||||
mutate = [](Payload& p) { if (p[0] == 0x9A) p[6] = 0; };
|
||||
EXPECT_THROW(protocol->torqueOff(1), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, NativeA3AndA4DoNotRequireModeSelectionOrB1) {
|
||||
config.set_limit_qd(0);
|
||||
ASSERT_TRUE(protocol->configureNode(1, config));
|
||||
ASSERT_TRUE(protocol->commandProfilePosition(1, 0.1, 0, 1));
|
||||
ASSERT_EQ(sent.size(), 2);
|
||||
EXPECT_EQ(sent[0].data[0], 0x9A); EXPECT_EQ(sent[1].data[0], 0xA3);
|
||||
sent.clear();
|
||||
ASSERT_TRUE(protocol->commandProfilePosition(1, 0.1, 0.1, 1));
|
||||
ASSERT_EQ(sent.size(), 2);
|
||||
EXPECT_EQ(sent[0].data[0], 0x9A); EXPECT_EQ(sent[1].data[0], 0xA4);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, ParameterReplyIsEffectiveValueNotAnEcho) {
|
||||
mutate = [](Payload& p) { if (p[0] == 0xB1) p[2] = 0x55; };
|
||||
EXPECT_TRUE(protocol->writeControlParameter(1, 12, 123));
|
||||
mutate = [](Payload& p) { if (p[0] == 0xB1) put(p, 4, 124, 4); };
|
||||
EXPECT_FALSE(protocol->writeControlParameter(1, 12, 123));
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, MissingMotionReplyPropagatesAndNeverMarksTargetValid) {
|
||||
mutate = [](Payload& p) { if (p[0] == 0xA4 || p[0] == 0xA2) p[0] = 0; };
|
||||
EXPECT_THROW(protocol->commandProfilePosition(1, 0.1, 0.1, 0), std::runtime_error);
|
||||
EXPECT_FALSE(protocol->reachedTargetQ(1));
|
||||
EXPECT_THROW(protocol->commandProfileVelocity(1, 0.1, 0), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, HoldUsesFreshRawAngleWithoutZeroingOrParameterWrites) {
|
||||
int reads = 0;
|
||||
mutate = [&](Payload& p) { if (p[0] == 0x92) put(p, 1, ++reads == 1 ? -100 : -12345, 7); };
|
||||
ASSERT_TRUE(protocol->torqueOn(1));
|
||||
ASSERT_EQ(sent.size(), 6);
|
||||
Payload expected{}; put(expected, 4, -12345, 4);
|
||||
EXPECT_EQ(sent[4].data[2], 1); EXPECT_EQ(sent[4].data[3], 0);
|
||||
for (int i = 4; i < 8; ++i) EXPECT_EQ(sent[4].data[i], expected[i]);
|
||||
for (const auto& frame : sent) {
|
||||
EXPECT_NE(frame.data[0], 0x95); EXPECT_NE(frame.data[0], 0x19);
|
||||
EXPECT_NE(frame.data[0], 0x91); EXPECT_NE(frame.data[0], 0xB1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, MissingPositionAndOutOfRangeNeverEnable) {
|
||||
respond = false;
|
||||
EXPECT_THROW(protocol->torqueOn(1), std::runtime_error);
|
||||
ASSERT_EQ(sent.size(), 1); EXPECT_EQ(sent[0].data[0], 0x92);
|
||||
respond = true; sent.clear(); angle = int64_t(1) << 40;
|
||||
EXPECT_THROW(protocol->torqueOn(1), std::runtime_error);
|
||||
ASSERT_EQ(sent.size(), 1); EXPECT_EQ(sent[0].data[0], 0x92);
|
||||
}
|
||||
|
||||
TEST_F(LingkongTest, FailedHoldRollsBackToClosedAndInvalidatesTarget) {
|
||||
mutate = [](Payload& p) { if (p[0] == 0xA4) p[0] = 0; };
|
||||
EXPECT_THROW(protocol->torqueOn(1), std::runtime_error);
|
||||
EXPECT_EQ(state, 0x10);
|
||||
ASSERT_GE(sent.size(), 2);
|
||||
EXPECT_EQ(sent[sent.size()-2].data[0], 0x80);
|
||||
EXPECT_EQ(sent.back().data[0], 0x9A);
|
||||
EXPECT_FALSE(protocol->reachedTargetQ(1));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Loading…
Reference in New Issue
Block a user