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.
This commit is contained in:
parent
403c194f74
commit
bbcc716195
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user