使用RPC控制机械臂抓取物体
目标
控制机械臂和机械爪,将一个物体从一个位置抓取到另外一个位置
准备
步骤
引入需要使用的包
import grpc
import robot_pb2
import robot_pb2_grpc
# 三指机械爪
from robot_python_claw import DH3
创建RPC Client
channel = grpc.insecure_channel("192.168.1.167:50051")
stub = robot_pb2_grpc.RobotServiceStub(channel)
SSH连接到工控机,启动RPC Server
root@sia-ECS-1837:~# cd /root/
root@sia-ECS-1837:~# ls
robot_server
root@sia-ECS-1837:~# ./robot_server
...省略...
EtherCAT is not run, start simulation timer
#Control cycle =1 * Bus cycle#
create null grip!
[2020-08-18 23:24:10] init_robot_teach
[2020-08-18 23:24:10] set_robot_index
[2020-08-18 23:24:10] get_robot_num
[2020-08-18 23:24:10] get_robot_num
SIA initialize finish
initialize finish
Server listening on 0.0.0.0:50051
此时RPC Server已经启动,开始监听50051端口
打开RobotIDE,新建连接,配置RPC Server的IP和端口等信息 建立连接,连接RPC,上电 将机械臂示教到抓取位置 点击“保存示教关节点位置”,将此时机械臂的关节角度保存为j_1 切换到“数据”页面,此时可以看到我们保存的j_1数据点 切换回“示教”页面,示教保存其它关键位置 示教完成,得到关键点j_1、j_2、j_3、j_4 使用RPC获取我们示教的关节
joint_1 = stub.ReadRobjoint(robot_pb2.ReqString(reqstr="j_1"))
joint_2 = stub.ReadRobjoint(robot_pb2.ReqString(reqstr="j_2"))
joint_3 = stub.ReadRobjoint(robot_pb2.ReqString(reqstr="j_3"))
joint_4 = stub.ReadRobjoint(robot_pb2.ReqString(reqstr="j_4"))
实例化三指夹爪实例,设置夹爪加持力为50%,夹爪的三指成60°并张开至90%
dh3 = DH3()
dh3.set_force(50)
dh3.set_angle(60)
dh3.set_position(90)
自定义机械臂移动速度,然后从工控机读取转弯区域,工具和工件
speed = robot_pb2.HyySpeed(dof=6, per_flag=2, tcp_flag=0, per=[2] * 10, tcp=0.5, orl=0.5)
zone = stub.ReadZone(robot_pb2.ReqString(reqstr="z0"))
tool = stub.ReadTool(robot_pb2.ReqString(reqstr="tool0"))
wobj = stub.ReadWobj(robot_pb2.ReqString(reqstr="wobj0"))
首先让机械臂回到零点,调用RPC控制机械臂移动到第一个关键点,然后夹爪合至30%抓住物体
stub.TGetRobotNum(robot_pb2.ReqVoid())
stub.MoveA(robot_pb2.ReqMoveA(
rjoint=joint_1,
rspeed=speed,
rzone=zone,
rtool=tool,
wobj=wobj
))
dh3.set_position(30)
移动到第2、3、4个关键点
stub.MoveA(robot_pb2.ReqMoveA(
rjoint=joint_2,
rspeed=speed,
rzone=zone,
rtool=tool,
wobj=wobj
))
stub.MoveA(robot_pb2.ReqMoveA(
rjoint=joint_3,
rspeed=speed,
rzone=zone,
rtool=tool,
wobj=wobj
))
stub.MoveA(robot_pb2.ReqMoveA(
rjoint=joint_4,
rspeed=speed,
rzone=zone,
rtool=tool,
wobj=wobj
))
然后机械爪开合位置设置为90%,放下物体,然后回到零点
dh3.set_position(90)
stub.TRobotHome(robot_pb2.ReqVoid())
完整代码
import grpc
import robot_pb2
import robot_pb2_grpc
# 三指机械爪
from robot_python_claw import DH3
if __name__ == '__main__':
channel = grpc.insecure_channel("192.168.1.167:50051")
stub = robot_pb2_grpc.RobotServiceStub(channel)
joint_1 = stub.ReadRobjoint(robot_pb2.ReqString(reqstr="j_1"))
joint_2 = stub.ReadRobjoint(robot_pb2.ReqString(reqstr="j_2"))
joint_3 = stub.ReadRobjoint(robot_pb2.ReqString(reqstr="j_3"))
joint_4 = stub.ReadRobjoint(robot_pb2.ReqString(reqstr="j_4"))
dh3 = DH3()
dh3.set_force(50)
dh3.set_angle(60)
dh3.set_position(90)
speed = robot_pb2.HyySpeed(dof=6, per_flag=2, tcp_flag=0, per=[3] * 10, tcp=0.5, orl=0.5)
zone = stub.ReadZone(robot_pb2.ReqString(reqstr="z0"))
tool = stub.ReadTool(robot_pb2.ReqString(reqstr="tool0"))
wobj = stub.ReadWobj(robot_pb2.ReqString(reqstr="wobj0"))
stub.TGetRobotNum(robot_pb2.ReqVoid())
stub.MoveA(robot_pb2.ReqMoveA(
rjoint=joint_1,
rspeed=speed,
rzone=zone,
rtool=tool,
wobj=wobj
))
dh3.set_position(30)
stub.MoveA(robot_pb2.ReqMoveA(
rjoint=joint_2,
rspeed=speed,
rzone=zone,
rtool=tool,
wobj=wobj
))
stub.MoveA(robot_pb2.ReqMoveA(
rjoint=joint_3,
rspeed=speed,
rzone=zone,
rtool=tool,
wobj=wobj
))
stub.MoveA(robot_pb2.ReqMoveA(
rjoint=joint_4,
rspeed=speed,
rzone=zone,
rtool=tool,
wobj=wobj
))
dh3.set_position(90)
stub.TRobotHome(robot_pb2.ReqVoid())