Compare commits

...

4 Commits

Author SHA1 Message Date
e714f73601 build(ethercat): synchronize rebuilt x86 userspace artifacts
Update the bundled EtherCAT CLI and libraries, including libethercat.so.1.3.0 and its symlinks. Synchronize installation metadata with the project path and align build documentation with the igc driver. Retain the machine-specific master MAC locally. Verified CLI version, library SONAME/symlinks, script syntax and the application build on device 206.
2026-11-12 11:59:07 +08:00
70237a9559 config: document dual-arm junction ports and add QUIC platform template
Document Port 1 as right-arm positions 1-7 and Port 2 as left-arm positions 8-14. Add the platform_199 QUIC connection template while keeping the QUIC task disabled. Keep all device entries disabled in version control and retain device 206 runtime switches locally. All 44 protobuf configuration files parse successfully.
2026-11-12 11:59:07 +08:00
d6a1635b1b feat(motor): select mixed-protocol groups by motor system ID
Preserve exact single-group selection and allow a distinct MotorConfig.id to select all groups in a dedicated configuration. Share validation with DeviceFactory and reject ambiguous group IDs, duplicate motor IDs and duplicate joint names. All 9 motor-manager shutdown and group-selection tests pass; cmvr_es builds with -j8.
2026-11-12 11:59:07 +08:00
bbcc716195 style(dexhand): expand OYMotion adapter lifecycle and SDK calls
Expand compact control flow, declarations and SDK calls for readability without changing the hand command mapping or behavior. Validated by rebuilding cmvr_es with -j8 on device 206.
2026-11-12 11:59:06 +08:00
23 changed files with 406 additions and 195 deletions

View File

@ -118,6 +118,7 @@ git rev-parse HEAD # 记录本次 userspace 和内核模块共同使用的提
--sysconfdir=$IGH_ETHERCAT_ROOT/etc \
--with-systemdsystemunitdir=$IGH_ETHERCAT_ROOT/lib/systemd/system \
--enable-generic \
--enable-igc \
--with-linux-dir=/lib/modules/$(uname -r)/build
make -j$(nproc) all modules
@ -158,7 +159,7 @@ sudo scripts/ethercat/start_ethercat.sh eno1
$IGH_ETHERCAT_ROOT/etc/ethercat.conf
```
脚本会把该网卡的 MAC 写到 `MASTER0_DEVICE`,使用 `DEVICE_MODULES="generic"`,把网卡从 NetworkManager 断开,并通过 `ethercatctl -c` 启动 IgH master。
脚本会把该网卡的 MAC 写到 `MASTER0_DEVICE`,使用 `DEVICE_MODULES="igc"`,把网卡从 NetworkManager 断开,并通过 `ethercatctl -c` 启动 IgH master。
查看状态:

View File

@ -1,5 +1,6 @@
arm {
robot_arms {
# Junction Port 1: 7 axes, EtherCAT positions 1-7.
id: "right_tchl_arm"
motor {
@ -159,6 +160,7 @@ arm {
}
robot_arms {
# Junction Port 2: 7 axes, EtherCAT positions 8-14.
id: "left_tchl_arm"
motor {

View File

@ -38,6 +38,7 @@ motor {
sync_monitor_period_ms: 1000
}
# Junction position 0: Port 1 -> right arm J1-J7 (positions 1-7).
slaves { motor_id: 1 alias: 0 position: 1 }
slaves { motor_id: 2 alias: 0 position: 2 }
slaves { motor_id: 3 alias: 0 position: 3 }
@ -45,6 +46,7 @@ motor {
slaves { motor_id: 5 alias: 0 position: 5 }
slaves { motor_id: 6 alias: 0 position: 6 }
slaves { motor_id: 7 alias: 0 position: 7 }
# Junction Port 2 -> left arm J1-J7 (positions 8-14).
slaves { motor_id: 8 alias: 0 position: 8 }
slaves { motor_id: 9 alias: 0 position: 9 }
slaves { motor_id: 10 alias: 0 position: 10 }

View File

@ -44,6 +44,18 @@ quic_edge {
# }
# }
platforms {
id: "platform_199"
server_host: "192.168.0.199"
server_port: 4433
enable_media: false
tls {
ca_file: "certs/cmvr-quic-ca.crt"
server_name: "192.168.0.199"
allow_insecure: false
}
}
reconnect {
initial_delay_ms: 500
maximum_delay_ms: 30000

View File

@ -13,76 +13,85 @@
namespace cmvr::device {
class OYMotionAP001 : public AbstractDexHand {
public:
static constexpr size_t MOTOR_COUNT = 6;
explicit OYMotionAP001(const config::OYMotionAP001Config& config);
~OYMotionAP001() override;
std::string typeName() const override { return type_name_; }
bool init() override;
bool start() override;
bool stop() override;
Status state() const override;
std::string lastError() const override;
void getState(DexHandState& state) override;
void setAngles(const std::vector<int>& values) override;
void setPositions(const std::vector<int>& values) override;
void setVelocities(const std::vector<int>& values) override;
void setForce(const std::vector<int>& values) override;
void setTactilePollingRegions(const std::vector<TactileRegionKey>& regions) override;
bool acquireSensorDataStream() override;
void releaseSensorDataStream() override;
std::vector<TactileRegionData> getSensorData() override;
TactileRegionData getSensorData(FingerType finger, TactileRegion region) override;
ResultantForce getResultantForce(FingerType finger, TactileRegion region) override;
ForceNewtons getResultantForceNewtons(FingerType finger, TactileRegion region) override;
class OYMotionAP001 : public AbstractDexHand {
public:
static constexpr size_t MOTOR_COUNT = 6;
protected:
OYMotionAP001(std::string id, std::string can_interface,
uint32_t hand_id, uint32_t master_id,
uint32_t command_timeout_ms, std::string type_name);
explicit OYMotionAP001(const config::OYMotionAP001Config& config);
~OYMotionAP001() override;
private:
static uint32_t sdkMillis();
static void sdkDelay(uint32_t milliseconds);
static void sdkSend(uint8_t address, uint8_t* data, uint8_t size, void* context);
static void sdkReceive(void* context);
static OYMotionAP001* fromSdkContext(void* context);
bool openSocket();
bool checkSdkResult(uint8_t result, uint8_t remote_error, const char* operation);
void startTactilePolling();
void stopTactilePolling();
void tactilePollingLoop();
void setFault(const std::string& message);
void setState(Status state);
std::array<uint16_t, MOTOR_COUNT> normalizedPositions(const std::vector<int>& values, int maximum) const;
std::string typeName() const override { return type_name_; }
bool init() override;
bool start() override;
bool stop() override;
std::string type_name_{"OYMotionAP001"};
std::string can_interface_;
uint32_t command_timeout_ms_{255};
uint8_t hand_id_{2};
uint8_t master_id_{1};
int socket_fd_{-1};
void* sdk_context_{nullptr};
mutable std::mutex state_mutex_;
mutable std::mutex io_mutex_;
Status lifecycle_state_{Status::CREATED};
std::string last_error_;
std::array<uint8_t, MOTOR_COUNT> speeds_{{255, 255, 255, 255, 255, 255}};
std::array<uint16_t, MOTOR_COUNT> current_positions_{};
// OHand SDK reports the physical joint angle in degrees * 100.
std::array<int16_t, MOTOR_COUNT> current_angles_{};
std::vector<TactileRegionKey> tactile_regions_;
static constexpr size_t INDEX_TACTILE_COUNT = 60;
std::atomic<bool> tactile_running_{false};
std::mutex tactile_subscription_mutex_;
size_t tactile_subscriber_count_{0};
std::thread tactile_thread_;
mutable std::mutex tactile_mutex_;
std::array<TactilePoint, INDEX_TACTILE_COUNT> index_tactile_{};
bool index_tactile_valid_{false};
uint64_t index_tactile_sequence_{0};
};
Status state() const override;
std::string lastError() const override;
void getState(DexHandState& state) override;
void setAngles(const std::vector<int>& values) override;
void setPositions(const std::vector<int>& values) override;
void setVelocities(const std::vector<int>& values) override;
void setForce(const std::vector<int>& values) override;
void setTactilePollingRegions(const std::vector<TactileRegionKey>& regions) override;
bool acquireSensorDataStream() override;
void releaseSensorDataStream() override;
std::vector<TactileRegionData> getSensorData() override;
TactileRegionData getSensorData(FingerType finger, TactileRegion region) override;
ResultantForce getResultantForce(FingerType finger, TactileRegion region) override;
ForceNewtons getResultantForceNewtons(FingerType finger, TactileRegion region) override;
protected:
OYMotionAP001(std::string id, std::string can_interface,
uint32_t hand_id, uint32_t master_id,
uint32_t command_timeout_ms, std::string type_name);
private:
static uint32_t sdkMillis();
static void sdkDelay(uint32_t milliseconds);
static void sdkSend(uint8_t address, uint8_t* data, uint8_t size, void* context);
static void sdkReceive(void* context);
static OYMotionAP001* fromSdkContext(void* context);
bool openSocket();
bool checkSdkResult(uint8_t result, uint8_t remote_error, const char* operation);
void startTactilePolling();
void stopTactilePolling();
void tactilePollingLoop();
void setFault(const std::string& message);
void setState(Status state);
std::array<uint16_t, MOTOR_COUNT> normalizedPositions(const std::vector<int>& values, int maximum) const;
std::string type_name_{"OYMotionAP001"};
std::string can_interface_;
uint32_t command_timeout_ms_{255};
uint8_t hand_id_{2};
uint8_t master_id_{1};
int socket_fd_{-1};
void* sdk_context_{nullptr};
mutable std::mutex state_mutex_;
mutable std::mutex io_mutex_;
Status lifecycle_state_{Status::CREATED};
std::string last_error_;
std::array<uint8_t, MOTOR_COUNT> speeds_{{255, 255, 255, 255, 255, 255}};
std::array<uint16_t, MOTOR_COUNT> current_positions_{};
// OHand SDK reports the physical joint angle in degrees * 100.
std::array<int16_t, MOTOR_COUNT> current_angles_{};
std::vector<TactileRegionKey> tactile_regions_;
static constexpr size_t INDEX_TACTILE_COUNT = 60;
std::atomic<bool> tactile_running_{false};
std::mutex tactile_subscription_mutex_;
size_t tactile_subscriber_count_{0};
std::thread tactile_thread_;
mutable std::mutex tactile_mutex_;
std::array<TactilePoint, INDEX_TACTILE_COUNT> index_tactile_{};
bool index_tactile_valid_{false};
uint64_t index_tactile_sequence_{0};
};
} // namespace cmvr::device
#endif
#endif // CMVR_ES_OYMOTION_AP001_H

View File

@ -5,6 +5,7 @@
#include <cstring>
#include <stdexcept>
#include <thread>
#include <fcntl.h>
#include <linux/can.h>
#include <linux/can/raw.h>
@ -13,51 +14,52 @@
#include <sys/socket.h>
#include <unistd.h>
#include "OHandSerialAPI.h"
#include "common/base/logging/logger.h"
namespace cmvr::device {
namespace {
constexpr int kPositionInputMaximum = 2000;
constexpr int kAngleInputMaximum = 1000;
constexpr int kVelocityInputMaximum = 1000;
constexpr int kForceInputMaximum = 3000;
constexpr uint8_t kIndexTactileSensorId = 1;
constexpr int kIndexTactileRows = 12;
constexpr int kIndexTactileCols = 5;
constexpr auto kTactilePollingPeriod = std::chrono::microseconds(6667);
constexpr int kPositionInputMaximum = 2000;
constexpr int kAngleInputMaximum = 1000;
constexpr int kVelocityInputMaximum = 1000;
constexpr int kForceInputMaximum = 3000;
constexpr uint8_t kIndexTactileSensorId = 1;
constexpr int kIndexTactileRows = 12;
constexpr int kIndexTactileCols = 5;
constexpr auto kTactilePollingPeriod = std::chrono::microseconds(6667);
// cmvr-es exposes dex-hand joints as:
// pinky, ring, middle, index, thumb flexion, thumb rotation.
// OHand SDK uses:
// thumb flexion, index, middle, ring, pinky, thumb rotation.
constexpr std::array<size_t, OYMotionAP001::MOTOR_COUNT> kApiToSdkMotor{
4, 3, 2, 1, 0, 5};
// cmvr-es exposes dex-hand joints as:
// pinky, ring, middle, index, thumb flexion, thumb rotation.
// OHand SDK uses:
// thumb flexion, index, middle, ring, pinky, thumb rotation.
constexpr std::array<size_t, OYMotionAP001::MOTOR_COUNT> kApiToSdkMotor{
4, 3, 2, 1, 0, 5};
template <typename T>
std::array<T, OYMotionAP001::MOTOR_COUNT> apiToSdkMotorOrder(
const std::array<T, OYMotionAP001::MOTOR_COUNT>& api_values) {
std::array<T, OYMotionAP001::MOTOR_COUNT> sdk_values{};
for (size_t api_index = 0; api_index < kApiToSdkMotor.size(); ++api_index) {
sdk_values[kApiToSdkMotor[api_index]] = api_values[api_index];
template <typename T>
std::array<T, OYMotionAP001::MOTOR_COUNT> apiToSdkMotorOrder(
const std::array<T, OYMotionAP001::MOTOR_COUNT>& api_values) {
std::array<T, OYMotionAP001::MOTOR_COUNT> sdk_values{};
for (size_t api_index = 0; api_index < kApiToSdkMotor.size(); ++api_index) {
sdk_values[kApiToSdkMotor[api_index]] = api_values[api_index];
}
return sdk_values;
}
return sdk_values;
}
template <typename T>
std::array<T, OYMotionAP001::MOTOR_COUNT> sdkToApiMotorOrder(
const std::array<T, OYMotionAP001::MOTOR_COUNT>& sdk_values) {
std::array<T, OYMotionAP001::MOTOR_COUNT> api_values{};
for (size_t api_index = 0; api_index < kApiToSdkMotor.size(); ++api_index) {
api_values[api_index] = sdk_values[kApiToSdkMotor[api_index]];
template <typename T>
std::array<T, OYMotionAP001::MOTOR_COUNT> sdkToApiMotorOrder(
const std::array<T, OYMotionAP001::MOTOR_COUNT>& sdk_values) {
std::array<T, OYMotionAP001::MOTOR_COUNT> api_values{};
for (size_t api_index = 0; api_index < kApiToSdkMotor.size(); ++api_index) {
api_values[api_index] = sdk_values[kApiToSdkMotor[api_index]];
}
return api_values;
}
return api_values;
}
struct TactileSnapshot {
std::array<AbstractDexHand::TactilePoint,
static_cast<size_t>(kIndexTactileRows * kIndexTactileCols)>
values{};
};
struct TactileSnapshot {
std::array<AbstractDexHand::TactilePoint,
static_cast<size_t>(kIndexTactileRows * kIndexTactileCols)>
values{};
};
}
OYMotionAP001::OYMotionAP001(const config::OYMotionAP001Config& config)
@ -72,21 +74,37 @@ OYMotionAP001::OYMotionAP001(std::string id, std::string can_interface,
can_interface_(can_interface.empty() ? "can0" : std::move(can_interface)),
command_timeout_ms_(command_timeout_ms == 0 ? 255 : command_timeout_ms) {
id_ = std::move(id);
if (hand_id > 0 && hand_id <= 0xff) hand_id_ = static_cast<uint8_t>(hand_id);
if (master_id > 0 && master_id <= 0xff) master_id_ = static_cast<uint8_t>(master_id);
if (hand_id > 0 && hand_id <= 0xff) {
hand_id_ = static_cast<uint8_t>(hand_id);
}
if (master_id > 0 && master_id <= 0xff) {
master_id_ = static_cast<uint8_t>(master_id);
}
}
OYMotionAP001::~OYMotionAP001() {
stop();
}
OYMotionAP001::~OYMotionAP001() { stop(); }
uint32_t OYMotionAP001::sdkMillis() {
static const auto start = std::chrono::steady_clock::now();
return static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count());
return static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start).count());
}
void OYMotionAP001::sdkDelay(uint32_t milliseconds) {
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}
OYMotionAP001* OYMotionAP001::fromSdkContext(void* context) {
return context ? *static_cast<OYMotionAP001**>(context) : nullptr;
}
void OYMotionAP001::sdkDelay(uint32_t milliseconds) { std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); }
OYMotionAP001* OYMotionAP001::fromSdkContext(void* context) { return context ? *static_cast<OYMotionAP001**>(context) : nullptr; }
void OYMotionAP001::sdkSend(uint8_t address, uint8_t* data, uint8_t size, void* context) {
auto* self = fromSdkContext(context);
if (!self || self->socket_fd_ < 0) return;
if (!self || self->socket_fd_ < 0) {
return;
}
for (uint8_t offset = 0; offset < size; offset += CAN_MAX_DLEN) {
can_frame frame{};
frame.can_id = address;
@ -101,49 +119,80 @@ void OYMotionAP001::sdkSend(uint8_t address, uint8_t* data, uint8_t size, void*
void OYMotionAP001::sdkReceive(void* context) {
auto* self = fromSdkContext(context);
if (!self || self->socket_fd_ < 0) return;
if (!self || self->socket_fd_ < 0) {
return;
}
can_frame frame{};
while (read(self->socket_fd_, &frame, sizeof(frame)) == sizeof(frame)) {
if ((frame.can_id & CAN_EFF_FLAG) || (frame.can_id & CAN_RTR_FLAG)) continue;
for (uint8_t i = 0; i < frame.can_dlc; ++i) HAND_OnData(context, frame.data[i]);
if ((frame.can_id & CAN_EFF_FLAG) || (frame.can_id & CAN_RTR_FLAG)) {
continue;
}
for (uint8_t i = 0; i < frame.can_dlc; ++i) {
HAND_OnData(context, frame.data[i]);
}
}
}
bool OYMotionAP001::openSocket() {
socket_fd_ = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (socket_fd_ < 0) return false;
if (socket_fd_ < 0) {
return false;
}
ifreq request{};
std::strncpy(request.ifr_name, can_interface_.c_str(), IFNAMSIZ - 1);
if (ioctl(socket_fd_, SIOCGIFINDEX, &request) < 0) return false;
if (ioctl(socket_fd_, SIOCGIFINDEX, &request) < 0) {
return false;
}
sockaddr_can address{};
address.can_family = AF_CAN;
address.can_ifindex = request.ifr_ifindex;
if (bind(socket_fd_, reinterpret_cast<sockaddr*>(&address), sizeof(address)) < 0) return false;
if (bind(socket_fd_, reinterpret_cast<sockaddr*>(&address), sizeof(address)) < 0) {
return false;
}
const int flags = fcntl(socket_fd_, F_GETFL, 0);
return flags >= 0 && fcntl(socket_fd_, F_SETFL, flags | O_NONBLOCK) == 0;
}
bool OYMotionAP001::init() {
std::lock_guard<std::mutex> io_lock(io_mutex_);
if (sdk_context_) return true;
if (!openSocket()) { setFault("[OYMotionAP001] cannot open " + can_interface_ + "; ensure it is UP at 1 Mbps"); return false; }
if (sdk_context_) {
return true;
}
if (!openSocket()) {
setFault("[OYMotionAP001] cannot open " + can_interface_ + "; ensure it is UP at 1 Mbps");
return false;
}
HAND_SetTimerFunction(sdkMillis, sdkDelay);
sdk_context_ = HAND_CreateContext(this, HAND_PROTOCOL_UART, master_id_, sdkSend, sdkReceive);
if (!sdk_context_) { setFault("[OYMotionAP001] HAND_CreateContext failed"); return false; }
if (!sdk_context_) {
setFault("[OYMotionAP001] HAND_CreateContext failed");
return false;
}
HAND_SetCommandTimeOut(sdk_context_, static_cast<uint16_t>(
std::min<uint32_t>(command_timeout_ms_, 65535)));
uint8_t major = 0, minor = 0, remote_error = 0;
if (!checkSdkResult(HAND_GetProtocolVersion(sdk_context_, hand_id_, &major, &minor, &remote_error), remote_error, "HAND_GetProtocolVersion")) return false;
CMVR_LOG(INFO) << "[OYMotionAP001] connected: id=" << id_ << ", interface=" << can_interface_ << ", hand_id=" << static_cast<int>(hand_id_) << ", protocol=" << static_cast<int>(major) << "." << static_cast<int>(minor);
if (!checkSdkResult(
HAND_GetProtocolVersion(
sdk_context_, hand_id_, &major, &minor, &remote_error),
remote_error, "HAND_GetProtocolVersion")) {
return false;
}
CMVR_LOG(INFO) << "[OYMotionAP001] connected: id=" << id_
<< ", interface=" << can_interface_
<< ", hand_id=" << static_cast<int>(hand_id_)
<< ", protocol=" << static_cast<int>(major) << "." << static_cast<int>(minor);
setState(Status::INITIALIZED);
return true;
}
bool OYMotionAP001::start() {
if (!init()) return false;
if (!init()) {
return false;
}
setState(Status::STREAMING);
return true;
}
bool OYMotionAP001::stop() {
{
std::lock_guard<std::mutex> subscription_lock(
@ -152,13 +201,29 @@ bool OYMotionAP001::stop() {
stopTactilePolling();
}
std::lock_guard<std::mutex> io_lock(io_mutex_);
if (sdk_context_) { HAND_FreeContext(sdk_context_); sdk_context_ = nullptr; }
if (socket_fd_ >= 0) { close(socket_fd_); socket_fd_ = -1; }
if (state() != Status::FAULT) setState(Status::STOPPED);
if (sdk_context_) {
HAND_FreeContext(sdk_context_);
sdk_context_ = nullptr;
}
if (socket_fd_ >= 0) {
close(socket_fd_);
socket_fd_ = -1;
}
if (state() != Status::FAULT) {
setState(Status::STOPPED);
}
return true;
}
AbstractDexHand::Status OYMotionAP001::state() const { std::lock_guard<std::mutex> lock(state_mutex_); return lifecycle_state_; }
std::string OYMotionAP001::lastError() const { std::lock_guard<std::mutex> lock(state_mutex_); return last_error_; }
AbstractDexHand::Status OYMotionAP001::state() const {
std::lock_guard<std::mutex> lock(state_mutex_);
return lifecycle_state_;
}
std::string OYMotionAP001::lastError() const {
std::lock_guard<std::mutex> lock(state_mutex_);
return last_error_;
}
void OYMotionAP001::getState(DexHandState& output) {
DexHandState next{};
@ -211,14 +276,22 @@ void OYMotionAP001::getState(DexHandState& output) {
next.hands[i].speed = static_cast<int>(speeds_[i]) * kVelocityInputMaximum / 255;
}
const auto error = lastError();
if (!error.empty()) next.hands[0].error_message.push_back(error);
if (!error.empty()) {
next.hands[0].error_message.push_back(error);
}
output = std::move(next);
}
std::array<uint16_t, OYMotionAP001::MOTOR_COUNT> OYMotionAP001::normalizedPositions(const std::vector<int>& values, int maximum) const {
if (values.size() != MOTOR_COUNT) throw std::invalid_argument("OYMotionAP001 expects exactly 6 values");
std::array<uint16_t, OYMotionAP001::MOTOR_COUNT> OYMotionAP001::normalizedPositions(
const std::vector<int>& values, int maximum) const {
if (values.size() != MOTOR_COUNT) {
throw std::invalid_argument("OYMotionAP001 expects exactly 6 values");
}
std::array<uint16_t, MOTOR_COUNT> output{};
for (size_t i = 0; i < MOTOR_COUNT; ++i) output[i] = static_cast<uint16_t>(static_cast<uint32_t>(std::clamp(values[i], 0, maximum)) * 65535U / maximum);
for (size_t i = 0; i < MOTOR_COUNT; ++i) {
output[i] = static_cast<uint16_t>(
static_cast<uint32_t>(std::clamp(values[i], 0, maximum)) * 65535U / maximum);
}
return output;
}
@ -228,23 +301,41 @@ void OYMotionAP001::setPositions(const std::vector<int>& values) {
std::lock_guard<std::mutex> lock(io_mutex_);
auto speeds = apiToSdkMotorOrder(speeds_);
uint8_t remote_error = 0;
checkSdkResult(HAND_SetFingerPosAll(sdk_context_, hand_id_, positions.data(), speeds.data(), MOTOR_COUNT, &remote_error), remote_error, "HAND_SetFingerPosAll");
checkSdkResult(
HAND_SetFingerPosAll(
sdk_context_, hand_id_, positions.data(), speeds.data(),
MOTOR_COUNT, &remote_error),
remote_error, "HAND_SetFingerPosAll");
}
void OYMotionAP001::setAngles(const std::vector<int>& values) {
auto positions = apiToSdkMotorOrder(
normalizedPositions(values, kAngleInputMaximum));
std::lock_guard<std::mutex> lock(io_mutex_);
auto speeds = apiToSdkMotorOrder(speeds_);
uint8_t remote_error = 0;
checkSdkResult(HAND_SetFingerPosAll(sdk_context_, hand_id_, positions.data(), speeds.data(), MOTOR_COUNT, &remote_error), remote_error, "HAND_SetFingerPosAll(angle normalization)");
checkSdkResult(
HAND_SetFingerPosAll(
sdk_context_, hand_id_, positions.data(), speeds.data(),
MOTOR_COUNT, &remote_error),
remote_error, "HAND_SetFingerPosAll(angle normalization)");
}
void OYMotionAP001::setVelocities(const std::vector<int>& values) {
if (values.size() != MOTOR_COUNT) throw std::invalid_argument("OYMotionAP001 expects exactly 6 velocities");
if (values.size() != MOTOR_COUNT) {
throw std::invalid_argument("OYMotionAP001 expects exactly 6 velocities");
}
std::lock_guard<std::mutex> lock(io_mutex_);
for (size_t i = 0; i < MOTOR_COUNT; ++i) speeds_[i] = static_cast<uint8_t>(std::clamp(values[i], 0, kVelocityInputMaximum) * 255 / kVelocityInputMaximum);
for (size_t i = 0; i < MOTOR_COUNT; ++i) {
speeds_[i] = static_cast<uint8_t>(
std::clamp(values[i], 0, kVelocityInputMaximum) * 255 / kVelocityInputMaximum);
}
}
void OYMotionAP001::setForce(const std::vector<int>& values) {
if (values.size() != MOTOR_COUNT) throw std::invalid_argument("OYMotionAP001 expects exactly 6 force values");
if (values.size() != MOTOR_COUNT) {
throw std::invalid_argument("OYMotionAP001 expects exactly 6 force values");
}
std::lock_guard<std::mutex> lock(io_mutex_);
for (size_t api_index = 0; api_index < MOTOR_COUNT; ++api_index) {
uint8_t remote_error = 0;
@ -263,13 +354,17 @@ void OYMotionAP001::setForce(const std::vector<int>& values) {
void OYMotionAP001::setTactilePollingRegions(
const std::vector<TactileRegionKey>& regions) {
if (!hasTactileFeedback()) return;
if (!hasTactileFeedback()) {
return;
}
std::lock_guard<std::mutex> lock(io_mutex_);
tactile_regions_ = regions;
}
bool OYMotionAP001::acquireSensorDataStream() {
if (!hasTactileFeedback()) return false;
if (!hasTactileFeedback()) {
return false;
}
std::lock_guard<std::mutex> lock(tactile_subscription_mutex_);
++tactile_subscriber_count_;
if (tactile_subscriber_count_ == 1) {
@ -284,21 +379,31 @@ bool OYMotionAP001::acquireSensorDataStream() {
void OYMotionAP001::releaseSensorDataStream() {
std::lock_guard<std::mutex> lock(tactile_subscription_mutex_);
if (tactile_subscriber_count_ == 0) return;
if (tactile_subscriber_count_ == 0) {
return;
}
--tactile_subscriber_count_;
if (tactile_subscriber_count_ == 0) stopTactilePolling();
if (tactile_subscriber_count_ == 0) {
stopTactilePolling();
}
}
void OYMotionAP001::startTactilePolling() {
if (!hasTactileFeedback()) return;
if (!hasTactileFeedback()) {
return;
}
bool expected = false;
if (!tactile_running_.compare_exchange_strong(expected, true)) return;
if (!tactile_running_.compare_exchange_strong(expected, true)) {
return;
}
tactile_thread_ = std::thread(&OYMotionAP001::tactilePollingLoop, this);
}
void OYMotionAP001::stopTactilePolling() {
tactile_running_.store(false);
if (tactile_thread_.joinable()) tactile_thread_.join();
if (tactile_thread_.joinable()) {
tactile_thread_.join();
}
}
void OYMotionAP001::tactilePollingLoop() {
@ -357,11 +462,15 @@ void OYMotionAP001::tactilePollingLoop() {
}
std::vector<AbstractDexHand::TactileRegionData> OYMotionAP001::getSensorData() {
if (!hasTactileFeedback()) return {};
if (!hasTactileFeedback()) {
return {};
}
auto snapshot = std::make_shared<TactileSnapshot>();
{
std::lock_guard<std::mutex> lock(tactile_mutex_);
if (!index_tactile_valid_) return {};
if (!index_tactile_valid_) {
return {};
}
snapshot->values = index_tactile_;
}
return {TactileRegionData{
@ -373,7 +482,9 @@ std::vector<AbstractDexHand::TactileRegionData> OYMotionAP001::getSensorData() {
AbstractDexHand::TactileRegionData OYMotionAP001::getSensorData(
FingerType finger, TactileRegion region) {
if (finger != FingerType::INDEX || region != TactileRegion::PAD) return {};
if (finger != FingerType::INDEX || region != TactileRegion::PAD) {
return {};
}
auto regions = getSensorData();
return regions.empty() ? TactileRegionData{} : regions.front();
}
@ -381,7 +492,9 @@ AbstractDexHand::TactileRegionData OYMotionAP001::getSensorData(
AbstractDexHand::ResultantForce OYMotionAP001::getResultantForce(
FingerType finger, TactileRegion region) {
const auto tactile_data = getSensorData(finger, region);
if (!tactile_data.valid()) return {};
if (!tactile_data.valid()) {
return {};
}
ResultantForce resultant{};
for (int row = 0; row < tactile_data.view.rows; ++row) {
@ -402,10 +515,30 @@ AbstractDexHand::ForceNewtons OYMotionAP001::getResultantForceNewtons(
}
bool OYMotionAP001::checkSdkResult(uint8_t result, uint8_t remote_error, const char* operation) {
if (result == HAND_RESP_SUCCESS) return true;
setFault("[OYMotionAP001] " + std::string(operation) + " failed: result=" + std::to_string(result) + ", remote_error=" + std::to_string(remote_error));
if (result == HAND_RESP_SUCCESS) {
return true;
}
setFault("[OYMotionAP001] " + std::string(operation) + " failed: result="
+ std::to_string(result) + ", remote_error="
+ std::to_string(remote_error));
return false;
}
void OYMotionAP001::setFault(const std::string& message) { { std::lock_guard<std::mutex> lock(state_mutex_); lifecycle_state_ = Status::FAULT; last_error_ = message; } CMVR_LOG(ERROR) << message; }
void OYMotionAP001::setState(Status next) { std::lock_guard<std::mutex> lock(state_mutex_); lifecycle_state_ = next; if (next == Status::INITIALIZED || next == Status::STREAMING) last_error_.clear(); }
void OYMotionAP001::setFault(const std::string& message) {
{
std::lock_guard<std::mutex> lock(state_mutex_);
lifecycle_state_ = Status::FAULT;
last_error_ = message;
}
CMVR_LOG(ERROR) << message;
}
void OYMotionAP001::setState(Status next) {
std::lock_guard<std::mutex> lock(state_mutex_);
lifecycle_state_ = next;
if (next == Status::INITIALIZED || next == Status::STREAMING) {
last_error_.clear();
}
}
} // namespace cmvr::device

View File

@ -25,3 +25,5 @@ install(TARGETS motor_manager LIBRARY DESTINATION lib)
add_executable(motor_manager_shutdown_test src/motor_manager_shutdown_test.cpp)
target_link_libraries(motor_manager_shutdown_test PRIVATE
cmvr_es::device::motor_manager cmvr_es::device::lingkong_can_motor_driver cmvr_es::device::canbus gtest gtest_main pthread)
target_compile_definitions(motor_manager_shutdown_test PRIVATE CMVR_ES_SOURCE_DIR="${PROJECT_SOURCE_DIR}")

View File

@ -39,6 +39,10 @@ public:
DeviceKind kind() const noexcept override { return DeviceKind::MotorSystem; }
std::string typeName() const override { return "MotorManager"; }
// Existing group ID selects one group. A distinct MotorConfig.id selects
// all groups in that dedicated configuration as one motor system.
static std::vector<const config::MotorGroupConfig*> selectGroups(
const config::MotorConfig& config, const std::string& selected_id);
bool init() override;
bool stop() override;

View File

@ -6,6 +6,7 @@
#include <cstdint>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include "common/base/logging/logger.h"
@ -45,6 +46,31 @@ MotorManager::MotorManager(std::string id,
MotorManager::~MotorManager() = default;
std::vector<const config::MotorGroupConfig*> MotorManager::selectGroups(
const config::MotorConfig& cfg, const std::string& selected_id)
{
if (selected_id.empty()) return {};
std::vector<const config::MotorGroupConfig*> exact;
for (const auto& group : cfg.motor_groups())
if (group.id() == selected_id) exact.push_back(&group);
if (exact.size() > 1) return {};
if (exact.size() == 1) return exact; // Existing single-group semantics win.
if (cfg.id() != selected_id || cfg.motor_groups().empty()) return {};
std::vector<const config::MotorGroupConfig*> selected;
std::unordered_set<std::string> groups, joints;
std::unordered_set<int> ids;
for (const auto& group : cfg.motor_groups()) {
if (group.id().empty() || !groups.insert(group.id()).second ||
!group.has_motors() || group.motors().motors().empty()) return {};
for (const auto& motor : group.motors().motors()) {
if (motor.id() <= 0 || motor.joint_name().empty() ||
!ids.insert(motor.id()).second || !joints.insert(motor.joint_name()).second) return {};
}
selected.push_back(&group);
}
return selected;
}
bool MotorManager::init()
{
if (initialized_) {
@ -71,14 +97,15 @@ bool MotorManager::init()
motors_by_joint_.clear();
}
const auto selected_groups = selectGroups(cfg_, selected_group_id_);
if (selected_groups.empty()) {
CMVR_LOG(ERROR) << "[MotorManager] invalid or ambiguous motor group selection: " << selected_group_id_;
return false;
}
bool all_ok = true;
std::size_t selected_group_count = 0;
for (const auto& motor_group_cfg : cfg_.motor_groups()) {
for (const auto* selected_group : selected_groups) {
const auto& motor_group_cfg = *selected_group;
const auto& group_name = motor_group_cfg.id();
if (group_name != selected_group_id_) {
continue;
}
++selected_group_count;
if (!motor_group_cfg.has_motors()) {
CMVR_LOG(ERROR) << "[MotorManager] motor group missing motors: " << group_name;
all_ok = false;
@ -166,11 +193,6 @@ bool MotorManager::init()
bus_runtimes_.push_back(std::move(bus_runtime));
}
if (selected_group_count != 1) {
CMVR_LOG(ERROR) << "[MotorManager] selected motor group '" << selected_group_id_
<< "' must occur exactly once in config: " << id_;
all_ok = false;
}
if (!all_ok) {
for (auto& bus_runtime : bus_runtimes_) {

View File

@ -84,6 +84,43 @@ TEST(MotorManagerShutdown, LingkongDisabledOrMissingMotorNeverAbortsGroupStop) {
}
}
#include <google/protobuf/text_format.h>
#include <fstream>
#include <sstream>
namespace {
config::MotorConfig mixedConfig() {
config::MotorConfig cfg;cfg.set_id("mixed");
for(int part=0;part<2;++part){
auto* group=cfg.add_motor_groups();group->set_id(part==0?"old":"new");
group->set_protocol(part==0?config::MOTOR_PROTOCOL_LINGKONG_CAN:config::MOTOR_PROTOCOL_LINGKONG_CAN_V2_38);
group->set_bus_type(config::MOTOR_BUS_CAN);group->set_vendor(config::MOTOR_VENDOR_LINGKONG);group->mutable_can()->set_channel_id(3);
for(int id=(part==0?1:2);id<=(part==0?1:3);++id){auto* m=group->mutable_motors()->add_motors();m->set_id(id);m->set_joint_name("axis_"+std::to_string(id));}
}
return cfg;
}
TEST(MotorGroupSelection, MixedSystemSelectsBothProtocolsInOneManager){
auto cfg=mixedConfig();auto groups=MotorManager::selectGroups(cfg,"mixed");ASSERT_EQ(groups.size(),2);
EXPECT_EQ(groups[0]->protocol(),config::MOTOR_PROTOCOL_LINGKONG_CAN);EXPECT_EQ(groups[1]->protocol(),config::MOTOR_PROTOCOL_LINGKONG_CAN_V2_38);
EXPECT_EQ(groups[0]->motors().motors(0).id(),1);EXPECT_EQ(groups[1]->motors().motors_size(),2);
}
TEST(MotorGroupSelection, ExistingGroupSelectionStillLoadsOnlyItsOwnGroup){
auto cfg=mixedConfig();auto groups=MotorManager::selectGroups(cfg,"old");ASSERT_EQ(groups.size(),1);EXPECT_EQ(groups[0]->id(),"old");
cfg.set_id("old");groups=MotorManager::selectGroups(cfg,"old");ASSERT_EQ(groups.size(),1);EXPECT_EQ(groups[0]->id(),"old");
}
TEST(MotorGroupSelection, UnknownEmptyOrDuplicateGroupsAreRejected){
auto cfg=mixedConfig();EXPECT_TRUE(MotorManager::selectGroups(cfg,"missing").empty());EXPECT_TRUE(MotorManager::selectGroups(cfg,"").empty());
cfg.mutable_motor_groups(1)->set_id("old");EXPECT_TRUE(MotorManager::selectGroups(cfg,"old").empty());EXPECT_TRUE(MotorManager::selectGroups(cfg,"mixed").empty());
}
TEST(MotorGroupSelection, AggregateRejectsDuplicateMotorIdsOrJointNames){
auto cfg=mixedConfig();cfg.mutable_motor_groups(1)->mutable_motors()->mutable_motors(0)->set_id(1);EXPECT_TRUE(MotorManager::selectGroups(cfg,"mixed").empty());
cfg=mixedConfig();cfg.mutable_motor_groups(1)->mutable_motors()->mutable_motors(0)->set_joint_name("axis_1");EXPECT_TRUE(MotorManager::selectGroups(cfg,"mixed").empty());
}
TEST(MotorGroupSelection, NewDeploymentFileParsesAndHasRequestedProtocolsAndLimits){
std::ifstream input(std::string(CMVR_ES_SOURCE_DIR)+"/cmvr-es/config/devices/motor/lingkong_mixed_motors.pb.txt");ASSERT_TRUE(input.good());std::ostringstream text;text<<input.rdbuf();config::MotorRootConfig root;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(text.str(),&root));auto groups=MotorManager::selectGroups(root.motor(),"lingkong_mixed_motors");ASSERT_EQ(groups.size(),2);
for(auto* group:groups){EXPECT_EQ(group->can().channel_id(),3);for(const auto& motor:group->motors().motors()){EXPECT_DOUBLE_EQ(motor.limit_q_lb(),-3.14);EXPECT_DOUBLE_EQ(motor.limit_q_ub(),3.14);EXPECT_DOUBLE_EQ(motor.gear_ratio(),10);EXPECT_EQ(group->protocol(),motor.id()==1?config::MOTOR_PROTOCOL_LINGKONG_CAN:config::MOTOR_PROTOCOL_LINGKONG_CAN_V2_38);}}
}
}
#include <future>
#include <chrono>
namespace {

View File

@ -241,21 +241,8 @@ DeviceFactory::DeviceFactory()
return DeviceRecord{};
}
const config::MotorGroupConfig* selected_group = nullptr;
for (const auto& group_cfg : root_cfg.motor().motor_groups()) {
if (group_cfg.id() != entry.id()) {
continue;
}
if (selected_group != nullptr) {
CMVR_LOG(ERROR) << "[DeviceFactory]: Duplicate motor group id '"
<< entry.id() << "' in config: " << entry.config_file();
return DeviceRecord{};
}
selected_group = &group_cfg;
}
if (selected_group == nullptr) {
CMVR_LOG(ERROR) << "[DeviceFactory]: Motor group id '" << entry.id()
<< "' not found in config: " << entry.config_file();
if (MotorManager::selectGroups(root_cfg.motor(), entry.id()).empty()) {
CMVR_LOG(ERROR) << "[DeviceFactory]: Invalid motor group/system selection: " << entry.id();
return DeviceRecord{};
}
CMVR_LOG(INFO) << "[DeviceFactory]: Read device config success";

View File

@ -40,7 +40,7 @@
#------------------------------------------------------------------------------
ETHERCATCTL="/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/sbin/ethercatctl -c /home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/etc/sysconfig/ethercat"
ETHERCATCTL="/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/sbin/ethercatctl -c /home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/etc/sysconfig/ethercat"
#------------------------------------------------------------------------------

View File

@ -24,12 +24,12 @@
find_library(EtherCAT_LIBRARY
NAMES ethercat
PATHS /home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/lib
PATHS /home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/lib
)
find_path(EtherCAT_INCLUDE_DIR
NAMES ecrt.h
PATHS /home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/include
PATHS /home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/include
)
mark_as_advanced(EtherCAT_LIBRARY EtherCAT_INCLUDE_DIR)

View File

@ -8,7 +8,7 @@
dlname='libethercat.so.1'
# Names of this library.
library_names='libethercat.so.1.2.0 libethercat.so.1 libethercat.so'
library_names='libethercat.so.1.3.0 libethercat.so.1 libethercat.so'
# The name of the static archive.
old_library='libethercat.a'
@ -23,8 +23,8 @@ dependency_libs=''
weak_library_names=''
# Version information for libethercat.
current=3
age=2
current=4
age=3
revision=0
# Is this an already installed library?
@ -38,4 +38,4 @@ dlopen=''
dlpreopen=''
# Directory that this library needs to be installed in:
libdir='/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/lib'
libdir='/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/lib'

View File

@ -1 +1 @@
libethercat.so.1.2.0
libethercat.so.1.3.0

View File

@ -1 +1 @@
libethercat.so.1.2.0
libethercat.so.1.3.0

Binary file not shown.

View File

@ -21,10 +21,10 @@
# vim: tw=78 noexpandtab
#
prefix=/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0
prefix=/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0
exec_prefix=${prefix}
libdir=/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/lib
includedir=/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/include
libdir=/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/lib
includedir=/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/include
Name: libethercat
Description: Client support library for the EtherCAT Master

View File

@ -31,8 +31,8 @@ Description=EtherCAT Master Kernel Modules
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/sbin/ethercatctl start
ExecStop=/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/sbin/ethercatctl stop
ExecStart=/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/sbin/ethercatctl start
ExecStop=/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/sbin/ethercatctl stop
[Install]
WantedBy=multi-user.target

View File

@ -31,7 +31,7 @@ RMMOD="/sbin/rmmod"
MODINFO="/sbin/modinfo"
IP="/sbin/ip"
ETHERCAT="/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/bin/ethercat"
ETHERCAT="/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/bin/ethercat"
#------------------------------------------------------------------------------
@ -39,7 +39,7 @@ if [ "$1" = "-c" ]; then
ETHERCAT_CONFIG="$2"
COMMAND="$3"
else
ETHERCAT_CONFIG="/home/cmvr/projects/cmvr-es//dependency/x86/third_party/ethercat/v1.7.0/etc/ethercat.conf"
ETHERCAT_CONFIG="/home/cmvr/project/cmvr-es/dependency/x86/third_party/ethercat/v1.7.0/etc/ethercat.conf"
COMMAND="$1"
fi

View File

@ -16,7 +16,7 @@ Example:
sudo scripts/ethercat/start_ethercat.sh eno1 10 /dev/EtherCAT0
Environment:
DEVICE_MODULES=igc IgH device module list (234 default).
DEVICE_MODULES=igc IgH device module list (igc default).
IGH_ROOT=... Override bundled IgH install path.
ETHERCAT_CONF=... Override ethercatctl config path.
ETHERCAT_DEV=/dev/EtherCAT0 Override EtherCAT character device node.