1
1
# Piper 机械臂 SDK使用说明
2
2
3
+ ![ ubuntu] ( https://img.shields.io/badge/Ubuntu-20.04-orange.svg )
4
+
5
+ Test:
6
+
7
+ | PYTHON | STATE|
8
+ | ---| ---|
9
+ | ![ python3.8] ( https://img.shields.io/badge/Python-3.8-blue.svg ) | ![ Pass] ( https://img.shields.io/badge/Pass-blue.svg ) |
10
+
11
+ 本SDK用于接收can数据帧,然后处理为自定义数据类型,没有包括数据偏移帧
12
+
13
+ 具体的接口函数的详细说明请看[ Interface README] ( ./asserts/INTERFACE.MD )
14
+
15
+ 协议解析部分说明[ Protocol README] ( ./asserts/PROTOCOL_V1.MD )
16
+
17
+ 消息部分说明[ Msgs README] ( ./asserts/MSGS.MD )
18
+
3
19
## 安装方法
4
20
5
21
### 安装依赖
8
24
pip3 install python-can
9
25
```
10
26
27
+ ``` shell
28
+ pip3 install piper_sdk
29
+ ```
30
+
31
+ ### 安装can工具
32
+
33
+ ``` shell
34
+ sudo apt update && sudo apt install can-utils ethtool
35
+ ```
36
+
37
+ 这两个工具用来配置can模块
38
+
11
39
## 快速使用
12
40
13
41
### 使能can模块
14
42
15
- ** can模块设定脚本` can_config.sh ` **
16
-
17
43
首先需要设置好shell脚本参数
18
44
19
45
#### 单条机械臂
20
46
21
- 在` can_config.sh ` 中,` EXPECTED_CAN_COUNT ` 参数设置为` 1 ` ,因为一条机械臂使用一个can模块
22
-
23
47
##### pc只插入一个usb转can模块
24
48
49
+ - ** 此处使用` can_activate.sh ` 脚本**
50
+
25
51
直接执行
26
52
27
53
``` bash
28
- bash can_config .sh can0 1000000
54
+ bash can_activate .sh can0 1000000
29
55
```
30
56
31
57
##### pc插入多个usb转can模块
32
58
59
+ - ** 此处使用` can_activate.sh ` 脚本**
60
+
33
61
拔掉所有can模块
34
62
35
- 只将连接到机械臂的can模块插入PC,执行` sudo ethtool -i can0 | grep bus ` ,并记录下 ` bus-info ` 的数值例如 ` 1-2:1.0 `
63
+ 只将连接到机械臂的can模块插入PC,执行
36
64
37
- ps:** 一般第一个插入的can模块会默认是can0,如果没有查询到can可以使用` ip link show ` 来显示所有can,然后先后插入查看can编号**
65
+ ``` shell
66
+ sudo ethtool -i can0 | grep bus
67
+ ```
68
+
69
+ 并记录下` bus-info ` 的数值例如` 1-2:1.0 `
70
+
71
+ ps:** 一般第一个插入的can模块会默认是can0,如果没有查询到can可以使用` bash find_all_can_port.sh ` 来查看刚才usb地址对应的can名称**
38
72
39
73
假设上面的操作记录的` bus-info ` 数值为` 1-2:1.0 `
40
74
41
75
然后执行,查看can设备是否激活成功
42
76
43
77
``` bash
44
- bash can_config .sh can_piper 1000000 " 1-2:1.0"
78
+ bash can_activate .sh can_piper 1000000 " 1-2:1.0"
45
79
```
46
80
47
81
ps:** 此处的意思是,1-2:1.0硬件编码的usb端口插入的can设备,名字被重命名为can_piper,波特率为1000000,并激活**
@@ -52,25 +86,112 @@ ps:**此处的意思是,1-2:1.0硬件编码的usb端口插入的can设备,
52
86
53
87
如果是四条机械臂,也就是两对主从机械臂
54
88
89
+ - ** 此处使用` can_config.sh ` 脚本**
90
+
55
91
在` can_config.sh ` 中,` EXPECTED_CAN_COUNT ` 参数一般设置为` 2 ` ,因为四条机械臂使用两个can模块
56
92
57
- 然后将两个can模块中的其中一个(一般第一个插入左臂所在的模块)单独插入PC,执行` sudo ethtool -i can0 | grep bus ` ,并记录下` bus-info ` 的数值例如` 1-2:1.0 `
93
+ 然后将两个can模块中的其中一个(一般第一个插入左臂所在的模块)单独插入PC,执行
94
+
95
+ ``` shell
96
+ sudo ethtool -i can0 | grep bus
97
+ ```
98
+
99
+ 并记录下` bus-info ` 的数值例如` 1-2:1.0 `
58
100
59
- 接着插入下一个can模块,注意不可以与上次can模块插入的usb口相同,然后执行` sudo ethtool -i can1 | grep bus `
101
+ 接着插入下一个can模块,注意不可以与上次can模块插入的usb口相同,然后执行
60
102
61
- ps:** 一般第一个插入的can模块会默认是can0,第二个为can1,如果没有查询到can可以使用` ip link show ` 来显示所有can,然后先后插入查看can编号**
103
+ ``` shell
104
+ sudo ethtool -i can1 | grep bus
105
+ ```
106
+
107
+ ps:** 一般第一个插入的can模块会默认是can0,第二个为can1,如果没有查询到can可以使用` bash find_all_can_port.sh ` 来查看刚才usb地址对应的can名称**
62
108
63
109
假设上面的操作记录的` bus-info ` 数值分别为` 1-2:1.0 ` 、` 1-4:1.0 `
64
110
65
- 则将下面的` USB_PORTS["1-9:1.0"]="can_left:1000000" ` 中的中括号内部的双引号内部的参数换为` 1-2:1.0 ` ,另一个同理
111
+ 则将下面的` USB_PORTS["1-9:1.0"]="can_left:1000000" ` 的中括号内部的双引号内部的参数换为` 1-2:1.0 `
112
+
113
+ 另一个同理
114
+
66
115
` USB_PORTS["1-5:1.0"]="can_right:1000000" ` -> ` USB_PORTS["1-4:1.0"]="can_right:1000000" `
67
116
68
117
ps:** 此处的意思是,1-2:1.0硬件编码的usb端口插入的can设备,名字被重命名为can_left,波特率为1000000,并激活**
69
118
70
- 然后执行` bash can_config.sh ` ,查看两个can设备是不是激活成功
119
+ 然后执行` bash can_config.sh ` ,查看终端输出是否激活成功
71
120
72
121
然后执行` ifconfig ` 查看是不是有` can_left ` 和` can_right ` ,如果有则can模块设置成功
73
122
123
+ ## 读取机械臂消息
124
+
125
+ ``` python
126
+ # !/usr/bin/env python3
127
+ # -*-coding:utf8-*-
128
+ # 读取机械臂消息并打印,需要先安装piper_sdk
129
+ from typing import (
130
+ Optional,
131
+ )
132
+ from piper_sdk import *
133
+
134
+ if __name__ == " __main__" :
135
+ # 实例化piper接口类
136
+ piper = C_PiperInterface(" can0" )
137
+ # 开启can设备连接
138
+ piper.ConnectPort()
139
+ while True :
140
+ import time
141
+ # 打印机械臂关节角度和夹爪消息
142
+ print (piper.GetArmJointGripperMsgs())
143
+ time.sleep(0.005 )
144
+ pass
145
+ ```
146
+
147
+ ## 控制机械臂运动
148
+
149
+ ``` python
150
+ # !/usr/bin/env python3
151
+ # -*-coding:utf8-*-
152
+ from typing import (
153
+ Optional,
154
+ )
155
+ from piper_sdk import *
156
+
157
+ if __name__ == " __main__" :
158
+ # 实例化piper接口类
159
+ piper = C_PiperInterface(" can0" )
160
+ # 开启can设备连接
161
+ piper.ConnectPort()
162
+ # 使能机械臂所有电机
163
+ piper.EnableArm(7 )
164
+ # 夹爪控制,发送位置0,扭矩1N,控制码为0x01,不设置当前为零点
165
+ piper.GripperCtrl(0 ,1000 ,0x 01 , 0 )
166
+ factor = 57324.840764 # 1000*180/3.14
167
+ position = [0 ,0 ,0 ,0 ,0 ,0 ,0 ]
168
+ count = 0
169
+ while True :
170
+ import time
171
+ count = count + 1
172
+ if (count == 0 ):
173
+ position = [0 ,0 ,0 ,0 ,0 ,0 ,0 ]
174
+ elif (count == 500 ):
175
+ position = [0 ,0 ,0 ,0 ,0 ,0 ,0.005 ]
176
+ elif (count == 1000 ):
177
+ count = 0
178
+ joint_0 = round (position[0 ]* factor)
179
+ joint_1 = round (position[1 ]* factor)
180
+ joint_2 = round (position[2 ]* factor)
181
+ joint_3 = round (position[3 ]* factor)
182
+ joint_4 = round (position[4 ]* factor)
183
+ joint_5 = round (position[5 ]* factor)
184
+ joint_6 = round (position[6 ]* 1000 * 1000 )
185
+ piper.MotionCtrl_2(0x 01 , 0x 01 , 50 )
186
+ piper.JointCtrl(joint_0, joint_1, joint_2, joint_3, joint_4, joint_5)
187
+ piper.GripperCtrl(abs (joint_6), 1000 , 0x 01 , 0 )
188
+ piper.MotionCtrl_2(0x 01 , 0x 01 , 50 )
189
+ time.sleep(0.005 )
190
+ pass
191
+ ```
192
+
74
193
## 注意事项
75
194
76
195
- 需要先激活can设备,并且设置正确的波特率,才可以读取机械臂消息或者控制机械臂
196
+ - C_PiperInterface 接口类在实例化时可传入激活的can路由名称,这个名称可以通过` ifconfig ` 得到
197
+ - 有时执行can发送,终端反馈` Message NOT sent ` ,是can模块没有成功连接设备,先检查模块与机械臂的连接状态,然后将机械臂断电后上电,再尝试发送
0 commit comments