Skip to content

Commit 8125939

Browse files
Merge pull request #5 from linker-bot/dev
Add API interface can parameter
2 parents 2ffef88 + e48041e commit 8125939

8 files changed

Lines changed: 131 additions & 8 deletions

File tree

docs/API-Reference.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ LinkerHandApi(const LINKER_HAND &handModel, const HAND_TYPE &handType, const COM
7676
**返回值**
7777
7878
79+
```cpp
80+
LinkerHandApi(const LINKER_HAND &handModel, const HAND_TYPE &handType,
81+
const std::string canChannel, const int baudrate);
82+
```
83+
84+
**功能描述**
85+
创建并初始化 LinkerHand API 实例。
86+
87+
**参数说明**
88+
- `handModel` (LINKER_HAND): 机械手型号
89+
- 可选值: `LINKER_HAND::O6`, `LINKER_HAND::L6`, `LINKER_HAND::L7`, `LINKER_HAND::L10`, `LINKER_HAND::L20`, `LINKER_HAND::L21`, `LINKER_HAND::L25`
90+
- `handType` (HAND_TYPE): 机械手类型
91+
- `HAND_TYPE::LEFT` - 左手
92+
- `HAND_TYPE::RIGHT` - 右手
93+
- `canChannel` (std::string): can 通道名称
94+
- `baudrate` (int): can 波特率
95+
96+
**返回值**
97+
98+
7999
**使用示例**
80100
```cpp
81101
#include "LinkerHandApi.h"
@@ -91,6 +111,31 @@ int main() {
91111
}
92112
```
93113

114+
```cpp
115+
#include "LinkerHandApi.h"
116+
117+
int main() {
118+
119+
// 调用API接口
120+
LinkerHandApi hand(LINKER_HAND::O6, HAND_TYPE::LEFT, "can0", 115200);
121+
122+
// 获取版本信息
123+
std::cout << hand.getVersion() << std::endl;
124+
125+
// 四指握拳
126+
std::vector<uint8_t> fist_pose = {255, 255, 0, 0, 0, 0};
127+
hand.fingerMove(fist_pose);
128+
std::this_thread::sleep_for(std::chrono::seconds(1));
129+
130+
// 四指张开
131+
std::vector<uint8_t> open_pose = {255, 255, 255, 255, 255, 255};
132+
hand.fingerMove(open_pose);
133+
std::this_thread::sleep_for(std::chrono::seconds(1));
134+
135+
return 0;
136+
}
137+
```
138+
94139
**注意事项**
95140
- 构造函数会自动初始化通信接口
96141
- 确保在创建实例前,通信接口已正确配置(如 CAN 总线已启动、ModBus 端口已配置、EtherCAT 网络已初始化)

docs/FAQ.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ sudo modprobe can_raw
173173
sudo modprobe vcan # 虚拟 CAN(用于测试)
174174
175175
# 配置 CAN 接口
176-
sudo ip link set can0 type can bitrate 500000
176+
sudo ip link set can0 type can bitrate 1000000
177177
sudo ip link set can0 up
178178
179179
# 查看 CAN 接口状态

include/CanBusFactory.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
* @Author: liangshaoteng liangshaoteng2012@163.com
3+
* @Date: 2026-01-29 17:01:44
4+
* @LastEditors: liangshaoteng liangshaoteng2012@163.com
5+
* @LastEditTime: 2026-03-09 13:19:16
6+
* @FilePath: /linkerhand/include/CanBusFactory.h
7+
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
8+
*/
19
#ifndef LINKERHAND_CAN_BUS_FACTORY_H
210
#define LINKERHAND_CAN_BUS_FACTORY_H
311

@@ -34,10 +42,7 @@ namespace communication {
3442
return std::make_unique<linkerhand::communication::PCANBus>(channel, baudrate, linkerHand);
3543

3644
#else
37-
// Linux/Unix 平台
38-
if (interfaceOrChannel == "can0" || interfaceOrChannel == "can1") {
39-
return std::make_unique<linkerhand::communication::CanBus>(interfaceOrChannel, bitrate, linkerHand);
40-
}
45+
return std::make_unique<linkerhand::communication::CanBus>(interfaceOrChannel, bitrate, linkerHand);
4146

4247
#if USE_ETHERCAT
4348
else if (interfaceOrChannel == "ethercat") {

include/HandFactory.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#ifndef LINKERHAND_HAND_FACTORY_H
22
#define LINKERHAND_HAND_FACTORY_H
33

4+
#include <iostream>
5+
#include <string>
6+
#include <algorithm>
7+
#include <cctype>
8+
49
#include "IHand.h"
510
#include "LinkerHandL6.h"
611
#include "LinkerHandL7.h"
@@ -10,6 +15,8 @@
1015
#include "ModbusLinkerHandL10.h"
1116
#include "Common.h"
1217

18+
19+
1320
namespace linkerhand {
1421
namespace factory {
1522

@@ -100,6 +107,70 @@ class HandFactory {
100107

101108
return nullptr;
102109
}
110+
111+
112+
static std::unique_ptr<hand::IHand> createHand(LINKER_HAND type, uint32_t handId, const std::string canChannel, const int baudrate) {
113+
114+
if (handId != static_cast<uint32_t>(LEFT) &&
115+
handId != static_cast<uint32_t>(RIGHT))
116+
{
117+
throw std::invalid_argument("Unsupported hand type");
118+
}
119+
120+
if (containsIgnoreCase(canChannel, "can")) {
121+
switch (type) {
122+
case O6:
123+
return std::unique_ptr<hand::IHand>(std::make_unique<hand::L6Hand>(handId, canChannel, baudrate));
124+
break;
125+
case L6:
126+
return std::unique_ptr<hand::IHand>(std::make_unique<hand::L6Hand>(handId, canChannel, baudrate));
127+
break;
128+
case L7:
129+
return std::unique_ptr<hand::IHand>(std::make_unique<hand::L7Hand>(handId, canChannel, baudrate));
130+
break;
131+
case L10:
132+
return std::unique_ptr<hand::IHand>(std::make_unique<hand::L10Hand>(handId, canChannel, baudrate));
133+
break;
134+
case L20:
135+
return std::unique_ptr<hand::IHand>(std::make_unique<hand::L20Hand>(handId, canChannel, baudrate));
136+
break;
137+
case L21:
138+
return std::unique_ptr<hand::IHand>(std::make_unique<hand::L25Hand>(handId, canChannel, baudrate, 1));
139+
break;
140+
case L25:
141+
return std::unique_ptr<hand::IHand>(std::make_unique<hand::L25Hand>(handId, canChannel, baudrate, 0));
142+
break;
143+
default:
144+
throw std::invalid_argument("Unknown hand type");
145+
}
146+
} else if (canChannel == "modbus") {
147+
#if USE_RMAN
148+
switch (type) {
149+
case L10:
150+
return std::unique_ptr<hand::IHand>(std::make_unique<hand::ModbusL10Hand>(handId));
151+
default:
152+
throw std::invalid_argument("Unknown hand type");
153+
break;
154+
}
155+
#else
156+
throw std::runtime_error("ModBus support is disabled (USE_RMAN=0)");
157+
#endif
158+
}
159+
160+
return nullptr;
161+
}
162+
163+
private:
164+
165+
static std::string toLower(const std::string& str) {
166+
std::string result = str;
167+
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
168+
return result;
169+
}
170+
171+
static bool containsIgnoreCase(const std::string& str, const std::string& target) {
172+
return toLower(str).find(toLower(target)) != std::string::npos;
173+
}
103174
};
104175

105176
} // namespace factory

include/LinkerHandApi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class LinkerHandApi
2727
*/
2828
LinkerHandApi(const LINKER_HAND &handModel, const HAND_TYPE &handType,
2929
const COMM_TYPE commType = COMM_CAN_0);
30+
LinkerHandApi(const LINKER_HAND &handModel, const HAND_TYPE &handType,
31+
const std::string canChannel, const int baudrate);
3032
~LinkerHandApi();
3133

3234
// 设置关节位置
-11.9 KB
Binary file not shown.
-15 KB
Binary file not shown.

src/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
int main() {
44

55
// 调用API接口
6-
LinkerHandApi hand(LINKER_HAND::L10, HAND_TYPE::LEFT);
6+
LinkerHandApi hand(LINKER_HAND::O6, HAND_TYPE::LEFT, "can0", 115200);
77

88
// 获取版本信息
99
std::cout << hand.getVersion() << std::endl;
1010

1111
// 握拳
12-
std::vector<uint8_t> fist_pose = {101, 60, 0, 0, 0, 0, 255, 255, 255, 51};
12+
std::vector<uint8_t> fist_pose = {255, 255, 0, 0, 0, 0};
1313
hand.fingerMove(fist_pose);
1414
std::this_thread::sleep_for(std::chrono::seconds(1));
1515

1616
// 张开
17-
std::vector<uint8_t> open_pose = {255, 104, 255, 255, 255, 255, 255, 255, 255, 71};
17+
std::vector<uint8_t> open_pose = {255, 255, 255, 255, 255, 255};
1818
hand.fingerMove(open_pose);
1919
std::this_thread::sleep_for(std::chrono::seconds(1));
2020

0 commit comments

Comments
 (0)