diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 108a687..f20a0e6 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -76,6 +76,26 @@ LinkerHandApi(const LINKER_HAND &handModel, const HAND_TYPE &handType, const COM **返回值** 无 +```cpp +LinkerHandApi(const LINKER_HAND &handModel, const HAND_TYPE &handType, + const std::string canChannel, const int baudrate); +``` + +**功能描述** +创建并初始化 LinkerHand API 实例。 + +**参数说明** +- `handModel` (LINKER_HAND): 机械手型号 + - 可选值: `LINKER_HAND::O6`, `LINKER_HAND::L6`, `LINKER_HAND::L7`, `LINKER_HAND::L10`, `LINKER_HAND::L20`, `LINKER_HAND::L21`, `LINKER_HAND::L25` +- `handType` (HAND_TYPE): 机械手类型 + - `HAND_TYPE::LEFT` - 左手 + - `HAND_TYPE::RIGHT` - 右手 +- `canChannel` (std::string): can 通道名称 +- `baudrate` (int): can 波特率 + +**返回值** +无 + **使用示例** ```cpp #include "LinkerHandApi.h" @@ -91,6 +111,31 @@ int main() { } ``` +```cpp +#include "LinkerHandApi.h" + +int main() { + + // 调用API接口 + LinkerHandApi hand(LINKER_HAND::O6, HAND_TYPE::LEFT, "can0", 115200); + + // 获取版本信息 + std::cout << hand.getVersion() << std::endl; + + // 四指握拳 + std::vector fist_pose = {255, 255, 0, 0, 0, 0}; + hand.fingerMove(fist_pose); + std::this_thread::sleep_for(std::chrono::seconds(1)); + + // 四指张开 + std::vector open_pose = {255, 255, 255, 255, 255, 255}; + hand.fingerMove(open_pose); + std::this_thread::sleep_for(std::chrono::seconds(1)); + + return 0; +} +``` + **注意事项** - 构造函数会自动初始化通信接口 - 确保在创建实例前,通信接口已正确配置(如 CAN 总线已启动、ModBus 端口已配置、EtherCAT 网络已初始化) diff --git a/docs/FAQ.md b/docs/FAQ.md index d38a1e1..f8784e9 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -173,7 +173,7 @@ sudo modprobe can_raw sudo modprobe vcan # 虚拟 CAN(用于测试) # 配置 CAN 接口 -sudo ip link set can0 type can bitrate 500000 +sudo ip link set can0 type can bitrate 1000000 sudo ip link set can0 up # 查看 CAN 接口状态 diff --git a/include/CanBusFactory.h b/include/CanBusFactory.h index e24f3cd..d511616 100644 --- a/include/CanBusFactory.h +++ b/include/CanBusFactory.h @@ -1,3 +1,11 @@ +/* + * @Author: liangshaoteng liangshaoteng2012@163.com + * @Date: 2026-01-29 17:01:44 + * @LastEditors: liangshaoteng liangshaoteng2012@163.com + * @LastEditTime: 2026-03-09 13:19:16 + * @FilePath: /linkerhand/include/CanBusFactory.h + * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE + */ #ifndef LINKERHAND_CAN_BUS_FACTORY_H #define LINKERHAND_CAN_BUS_FACTORY_H @@ -34,10 +42,7 @@ namespace communication { return std::make_unique(channel, baudrate, linkerHand); #else - // Linux/Unix 平台 - if (interfaceOrChannel == "can0" || interfaceOrChannel == "can1") { - return std::make_unique(interfaceOrChannel, bitrate, linkerHand); - } + return std::make_unique(interfaceOrChannel, bitrate, linkerHand); #if USE_ETHERCAT else if (interfaceOrChannel == "ethercat") { diff --git a/include/HandFactory.h b/include/HandFactory.h index 41566a8..d60238a 100644 --- a/include/HandFactory.h +++ b/include/HandFactory.h @@ -1,6 +1,11 @@ #ifndef LINKERHAND_HAND_FACTORY_H #define LINKERHAND_HAND_FACTORY_H +#include +#include +#include +#include + #include "IHand.h" #include "LinkerHandL6.h" #include "LinkerHandL7.h" @@ -10,6 +15,8 @@ #include "ModbusLinkerHandL10.h" #include "Common.h" + + namespace linkerhand { namespace factory { @@ -100,6 +107,70 @@ class HandFactory { return nullptr; } + + + static std::unique_ptr createHand(LINKER_HAND type, uint32_t handId, const std::string canChannel, const int baudrate) { + + if (handId != static_cast(LEFT) && + handId != static_cast(RIGHT)) + { + throw std::invalid_argument("Unsupported hand type"); + } + + if (containsIgnoreCase(canChannel, "can")) { + switch (type) { + case O6: + return std::unique_ptr(std::make_unique(handId, canChannel, baudrate)); + break; + case L6: + return std::unique_ptr(std::make_unique(handId, canChannel, baudrate)); + break; + case L7: + return std::unique_ptr(std::make_unique(handId, canChannel, baudrate)); + break; + case L10: + return std::unique_ptr(std::make_unique(handId, canChannel, baudrate)); + break; + case L20: + return std::unique_ptr(std::make_unique(handId, canChannel, baudrate)); + break; + case L21: + return std::unique_ptr(std::make_unique(handId, canChannel, baudrate, 1)); + break; + case L25: + return std::unique_ptr(std::make_unique(handId, canChannel, baudrate, 0)); + break; + default: + throw std::invalid_argument("Unknown hand type"); + } + } else if (canChannel == "modbus") { + #if USE_RMAN + switch (type) { + case L10: + return std::unique_ptr(std::make_unique(handId)); + default: + throw std::invalid_argument("Unknown hand type"); + break; + } + #else + throw std::runtime_error("ModBus support is disabled (USE_RMAN=0)"); + #endif + } + + return nullptr; + } + +private: + + static std::string toLower(const std::string& str) { + std::string result = str; + std::transform(result.begin(), result.end(), result.begin(), ::tolower); + return result; + } + + static bool containsIgnoreCase(const std::string& str, const std::string& target) { + return toLower(str).find(toLower(target)) != std::string::npos; + } }; } // namespace factory diff --git a/include/LinkerHandApi.h b/include/LinkerHandApi.h index 5aef66c..c0a1180 100644 --- a/include/LinkerHandApi.h +++ b/include/LinkerHandApi.h @@ -27,6 +27,8 @@ class LinkerHandApi */ LinkerHandApi(const LINKER_HAND &handModel, const HAND_TYPE &handType, const COMM_TYPE commType = COMM_CAN_0); + LinkerHandApi(const LINKER_HAND &handModel, const HAND_TYPE &handType, + const std::string canChannel, const int baudrate); ~LinkerHandApi(); // 设置关节位置 diff --git a/lib/aarch64/liblinkerhand_cpp_sdk.so.1.5.8 b/lib/aarch64/liblinkerhand_cpp_sdk.so.1.5.8 index dfc5605..e9175bd 100755 Binary files a/lib/aarch64/liblinkerhand_cpp_sdk.so.1.5.8 and b/lib/aarch64/liblinkerhand_cpp_sdk.so.1.5.8 differ diff --git a/lib/x86_64/liblinkerhand_cpp_sdk.so.1.5.8 b/lib/x86_64/liblinkerhand_cpp_sdk.so.1.5.8 index 97c9e5c..7a41752 100755 Binary files a/lib/x86_64/liblinkerhand_cpp_sdk.so.1.5.8 and b/lib/x86_64/liblinkerhand_cpp_sdk.so.1.5.8 differ diff --git a/src/main.cpp b/src/main.cpp index b90399c..74bbb05 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,18 +3,18 @@ int main() { // 调用API接口 - LinkerHandApi hand(LINKER_HAND::L10, HAND_TYPE::LEFT); + LinkerHandApi hand(LINKER_HAND::O6, HAND_TYPE::LEFT, "can0", 115200); // 获取版本信息 std::cout << hand.getVersion() << std::endl; // 握拳 - std::vector fist_pose = {101, 60, 0, 0, 0, 0, 255, 255, 255, 51}; + std::vector fist_pose = {255, 255, 0, 0, 0, 0}; hand.fingerMove(fist_pose); std::this_thread::sleep_for(std::chrono::seconds(1)); // 张开 - std::vector open_pose = {255, 104, 255, 255, 255, 255, 255, 255, 255, 71}; + std::vector open_pose = {255, 255, 255, 255, 255, 255}; hand.fingerMove(open_pose); std::this_thread::sleep_for(std::chrono::seconds(1));