【ROS2實體機械臂驅動】rokae xCoreSDK Python測試使用
文章目錄
- 前言
- 正文
- 配置環境
- 下載源碼
- 配置環境變量
- 測試運行
- 修改點說明
- 實際運行情況
- 參考
前言
本文用來記錄 xCoreSDK-Python的調用使用1。
正文
配置環境
配置開發環境,這里使用conda做python環境管理,安裝可以參考
anaconda、miniconda、conda的關系及miniconda安裝-CSDN博客2
初始化python環境,根據sdk的說明,依賴于python3.8環境
# 創建環境
$ conda create --name xCorePy python=3.8# 激活虛擬環境
$ conda activate xCorePy# 配置鏡像源加速
$ pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
下載源碼
這里一定要使用git lfs
下載源碼,內部有幾個so文件是使用lfs
保存的。
$ mkdir -p ~/workspace/rokae_projects
$ cd ~/workspace/rokae_projects
$ git lfs clone https://github.com/RokaeRobot/xCoreSDK-Python.git
$ cd xCoreSDK-Python
配置環境變量
$ export PYTHONPATH=/home/dev/workspace/rokae_projects/xCoreSDK-Python/rokae_SDK_linux_v0.1.6_py38/lib:$PYTHONPATH
測試運行
注意: 應將庫文件所在路徑添加至運行腳本可識別的路徑中。將腳本中的ip修改為連接機器人所設置的ip。
提醒: 到這一步發現robot.connectToRobot(ec)
沒有權限,需要找廠家要license,可以找買機械臂的代理,拿到后自行插入優盤導入即可。
# 運行 firstexample.py
$ cd ~/workspace/rokae_projects/xCoreSDK-Python/rokae_SDK_linux_v0.1.6_py38/example
$ python firstexample.py
修改點說明
from robot import *
from convert_tools import *
import timefrom math import pi # 增加導入pi定義def waitRobot(robot):running = Truewhile running:time.sleep(0.1)ec = {}st = robot.operationState(ec)if st == rokae.OperationState.idle.value or st == rokae.OperationState.unknown.value:running = Falsedef main():ip = "192.168.8.160" # 修改本地機械臂對應的IP. BingLee 2025.5.28ec = {}# 下述類根據實際機械臂情況自行修改. BingLee 2025.5.28## XMateRobot(ip) # 協作機械臂# xMateErProRobot(ip) # 協作7軸機型# StandardRobot(ip) # 連接工業6軸機型# PCB4Robot(ip) # 連接PCB4軸機型# PCB3Robot(ip) # 連接PCB3軸機型with StandardRobot(ip) as robot: # 這里修改為博主使用的工業臂. BingLee 2025.5.28# 連接機器人robot.connectToRobot(ec)# 設置機器人上下電狀態-上電robot.setPowerState(True, ec)# 查詢機器人狀態power = robot.powerState(ec)print("當前上下電狀態為:", power)time.sleep(2)# 設置機器人上下電狀態-下電robot.setPowerState(False, ec)power = robot.powerState(ec)print("當前上下電狀態為:", power)################################################ 2. 查詢信息 ########################################################## 獲取機器人的基本信息info = robot.robotInfo(ec)print("機器人軸數:", info["joint_num"], "機型:", info["type"], "控制器版本:", info["version"])# 獲取SDK版本print("SDK版本:", robot.sdkVersion(ec))# 獲取機器人的上下電狀態power = robot.powerState(ec)print("當前上下電狀態為:", power)# 獲取機器人的操作模式mode = robot.operateMode(ec)print("當前機器人的操作模式為:", mode)# 獲取機器人運行狀態state = robot.operationState(ec)print("當前機器人的運行狀態為:", state)######################################## 3. 獲取機器人當前位姿,軸角度,基坐標系等信息 ###################################### 獲取關節位置joint_pos = robot.jointPos(ec)print("當前關節位置:", joint_pos)# 獲取關節速度joint_vel = robot.jointVel(ec)print("當前關節速度:", joint_vel)# 獲取關節力矩joint_torque = robot.jointTorque(ec)print("當前關節力矩:", joint_torque)# 獲取法蘭位姿posture = robot.flangePos(ec)print("當前法蘭位姿:", posture)# 獲取基坐標系-----原model()類base = robot.baseFrame(ec)print("當前基坐標系:", base)# 獲取當前的工具坐標系toolset = robot.toolset(ec)print("當前的工具坐標系為:", toolset)# 設置新的坐標系# coor_new = {'end': {'rot': [0, 0, 0], 'trans': [0.0, 0.0, -0.01]}, 'load': {'cog': [0.0, 0.0, 0.0],# 'inertia': [0.0, 0.0, 0.0], 'mass': 0.0},# 'ref': {'rot': [0.0, -0.0, 0.0], 'trans': [0.0, 0.0, 0.0]}}# robot.setToolset(coor_new, ec)# 獲取當前的工具坐標系# toolset = robot.toolset(ec)# print("修改后的工具坐標系為:", toolset)# 獲取法蘭位姿posture_ = robot.flangePos(ec)print("修改坐標系后的法蘭位姿:", posture_)# zero = zeroToolset()# robot.setToolset(zero, ec)############################################## 4. 計算正解和逆解 ###################################################### 計算正解->輸入一個與當前機型軸數相同的List,返回一個當前位姿的listpoint = [10, 20, 30, 40, 50, 10]point = degree2rad(point)print(point)fk = robot.calcFK(point, ec)print("計算正解為:", fk)# 計算逆解->輸入一個位姿,返回一個軸角的list# pos = [0.5930779237738772, -0.060094684364914094, 0.4260427869095114, 3.110893947990362, 0.04429035357891989, -2.9729572573550245]# ik = robot.calcIK(pos, ec)# # ik = rad2degree(ik)# print("計算逆解為:", ik)############################################## 5. 查詢DO和DI ######################################################### 查詢端口1_0的DO值do = robot.getDO(1, 0, ec)print(message(ec))print("DO1_0當前的信號值為:", do)# 查詢端口1_0的DI值di = robot.getDI(0, 0, ec)print("DI1_0當前的信號值為:", di)# 將DO1_0的值設為falserobot.setDO(0, 0, False, ec)# 查詢端口1_0的DO值do = robot.getDO(0, 0, ec)print("DO0_0修改后信號值為:", do)robot.setDO(0, 0, True, ec)############################################## 6. 斷開連接再重連 ###################################################### 機器人斷開連接robot.disconnectFromRobot(ec)time.sleep(2)# 機器人再次連接robot.connectToRobot(ec)############################################## 7. 打開和關閉拖動 ####################################################### # 機器人下電,因機器人拖動模式自動上電# robot.setPowerState(False, ec)# # 將機器人操作模式設為手動# robot.setOperateMode(rokae.OperateMode.manual, ec)# # 開啟拖動# robot.enableDrag(rokae.DragParameter.Space.cartesianSpace.value, rokae.DragParameter.Type.freely.value, ec)# print("機器人狀態:", robot.operationState(ec))# time.sleep(2)# # 關閉拖動# robot.disableDrag(ec)# print("機器人狀態:", robot.operationState(ec))# print("非Drag模式下的上下電模式為:", robot.powerState(ec))# time.sleep(2)############################################## 8. 查詢工件/工具信息 #################################################### 查詢所有工具的信息# tool = robot.toolsInfo(ec)# print(tool)# for name in tool.keys():# print(name, "質量:", tool[str(name)]["load"]["mass"])# # 查詢所有工件的信息# wobj = robot.wobjsInfo(ec)# print("查詢工件名信息為:")# for name in wobj.keys():# print(name)############################################### 9. 運動指令 ########################################################robot.setOperateMode(rokae.OperateMode.automatic, ec)robot.setPowerState(True, ec)robot.moveReset(ec)# robot.setDefaultZone(100, ec)# robot.setDefaultSpeed(100, ec)# p0 = robot.flangePos(ec)# print(p0)# ############################################### 10. Move L 點位測試/ NB4 運動指令 ######################################################### 這里博主均改為使用 MoveAbsJCommand命令,數值使用弧度描述. BingLee 2025.5.28p1 = MoveAbsJCommand([0, -pi/4, 0, 0, pi/2, pi],1000, 0)# p1.offset = [0.1, 0, 0, 0, 0, 0]p2 = MoveAbsJCommand([pi/2, 0, pi/4, 0, 0, 0],1000, 0)# p2.offset = [0, 0, 0.01, 0, 0, 0]p3 = MoveAbsJCommand([-pi/2, -pi/4, 0, 0, pi/2, pi],1000, 0)p4 = MoveAbsJCommand([0, -pi/4, pi/4, 0, 0, 0],1000, 0)# p1.offset = [0.1, 0, 0, 0, 0, 0]p5 = MoveAbsJCommand([0, 0, 0, 0, pi/2, 0],500, 0)while True:cmd = input("please input"" 'm(start move)', 'p(pause)', 'c(continue)', 'q(break)', 'i(check)', 's(stop)','a(adjust)',""'r(reset)', d(drag), k(stop_drag) ")if cmd == 'm':print("start move")robot.executeCommand([p1, p2, p3, p4, p5], ec)robot.moveStart(ec)print(ec)elif cmd == 'p':print("suspend")robot.pause(ec)elif cmd == 'd':print("drag")robot.setOperateMode(rokae.OperateMode.manual, ec)robot.enableDrag(rokae.DragParameter.Space.jointSpace.value, rokae.DragParameter.Type.freely.value, ec)elif cmd == 'k':print("kill drag")robot.disableDrag(ec)elif cmd == 'c':print("continue move")robot.moveStart(ec)elif cmd == 'a':print("adjust speed percentage 0.5")robot.adjustSpeedOnline(0.1, ec)elif cmd == 'i':print("current pos id:", robot.getPointPos(ec))elif cmd == 'r':robot.moveReset(ec)elif cmd == 's':robot.stop(ec)else:print("stop")breakrobot.stop(ec)time.sleep(1)robot.setPowerState(False, ec)robot.disconnectFromRobot(ec)#
if __name__ == '__main__':main()
運行輸出結果:
$ python3 firstexample.py
當前上下電狀態為: 0
當前上下電狀態為: 1
機器人軸數: 6 機型: XB10s-R1206-3B 控制器版本: 2.3.2
SDK版本: 0.1.7(Beta)
當前上下電狀態為: 1
當前機器人的操作模式為: 0
當前機器人的運行狀態為: 0
當前關節位置: [-1.1489009000176548e-05, 1.4887235907275245e-06, 1.6342124870940784e-05, 3.834951969714103e-06, 1.5708015998538547, -5.086011055912068e-05]
當前關節速度: [0.00010657046279474108, -0.0, 0.000355366933557185, 0.0, -0.0, -0.0]
當前關節力矩: [0.0, -0.0, 0.0, 0.0, -0.0, -0.0]
當前法蘭位姿: [0.6140099673622327, -6.736065028031115e-06, 0.9639800086642404, 3.1415888174476487, -2.3400841886472892e-05, -3.141553282467999]
當前基坐標系: [0.0, 0.0, 0.0, 0.0, -0.0, 0.0]
當前的工具坐標系為: {'end': {'name': '', 'rot': [0.0, -0.0, 0.0], 'trans': [0.0, 0.0, 0.0]}, 'load': {'cog': [0.0, 0.0, 0.0], 'inertia': [0.0, 0.0, 0.0], 'mass': 0.0}, 'ref': {'name': '', 'rot': [0.0, -0.0, 0.0], 'trans': [0.0, 0.0, 0.0]}}
修改坐標系后的法蘭位姿: [0.6140099530347417, -6.7360648634224415e-06, 0.9639798350653594, 3.141588817432537, -2.369797142919661e-05, -3.1415532824679984]
[0.17453292222222222, 0.34906584444444444, 0.5235987666666666, 0.6981316888888889, 0.8726646111111112, 0.17453292222222222]
計算正解為: [0.6142222356255438, 0.14980394927718851, 0.47920904205188103, 2.749132677334611, -0.3447416415079533, 2.705140663241381]
操作成功完成
DO1_0當前的信號值為: False
DI1_0當前的信號值為: None
DO0_0修改后信號值為: None
please input 'm(start move)', 'p(pause)', 'c(continue)', 'q(break)', 'i(check)', 's(stop)','a(adjust)','r(reset)', d(drag), k(stop_drag) - m
輸入下述指令控制:
- m:啟動運動軌跡
- p:暫停
- c:繼續
- q:中斷程序
- i:輸出當前位置信息
- r:清理robot運動點寄存
- s:停止
這里博主常用的是m、p、c、q、i指令。
實際運行情況
參考
中間還涉及官方的其它庫,也一并附上345。
RokaeRobot/xCoreSDK-Python: Software development Python interfaces for Rokae robots all series ??
anaconda、miniconda、conda的關系及miniconda安裝-CSDN博客 ??
RokaeRobot/xCoreSDK-CPP: Software development C++ interfaces for Rokae robots all series, compatible with Linux and Windows. ??
RokaeRobot/xCoreSDK-CSharp: Software development C# interfaces for Rokae robots all series ??
RokaeRobot/xCoreSDK-Android: Software development interfaces for Rokae robots all series, supports Android platform ??