72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
import queue
|
||
import threading
|
||
|
||
import Robot
|
||
import time
|
||
|
||
|
||
class FRRobot:
|
||
|
||
def __init__(self):
|
||
self.robotCTL = Robot.RPC('192.168.3.102')
|
||
self.is_action = False # 是否正在执行动作
|
||
self.speed = 100 # 速度百分比,[0~100]默认20
|
||
self.home = [] # 回零点
|
||
self.tool = 1 # 工具坐标系编号
|
||
self.user = 1 # 工件坐标系编号
|
||
# 100% 60mm/s; 50% 30mm/s; 20% 12mm/s
|
||
self.velocity = 60 # 速度(mm/s)
|
||
# 线程相关
|
||
self.command_queue = queue.Queue()
|
||
self.listener_thread = threading.Thread(target=self.process_commands)
|
||
self.listener_thread.daemon = True # 设置为守护线程,确保主程序退出时线程也会退出
|
||
self.listener_thread.start()
|
||
|
||
speed_error_code = self.robotCTL.SetSpeed(self.speed)
|
||
if speed_error_code != 0:
|
||
print("初始化全局速度错误:", speed_error_code)
|
||
|
||
# 将移动命令添加到队列中
|
||
def add_move_command(self, command):
|
||
self.command_queue.put(command)
|
||
|
||
def process_commands(self):
|
||
while True:
|
||
move_commands = self.command_queue.get()
|
||
if move_commands == "stop":
|
||
self.robotCTL.ImmStopJOG()
|
||
break # 通过发送一个特定的"stop"命令来停止线程
|
||
# axis, distance, direction = move_commands
|
||
print("接收到指令: ", move_commands)
|
||
for command in move_commands:
|
||
self.is_action = True
|
||
axis, distance = command
|
||
if axis == "x":
|
||
print("x移动距离: ", distance)
|
||
move_time = abs(distance) / self.velocity
|
||
direction = 1 if distance > 0 else 0
|
||
self.move_x(distance, direction)
|
||
print("x移动时间: ", move_time)
|
||
time.sleep(move_time)
|
||
self.robotCTL.ImmStopJOG()
|
||
elif axis == "y":
|
||
print("y移动距离: ", distance)
|
||
move_time = abs(distance) / self.velocity
|
||
direction = 1 if distance < 0 else 0
|
||
self.move_y(distance, direction)
|
||
print("y移动时间: ", move_time)
|
||
time.sleep(move_time)
|
||
self.robotCTL.ImmStopJOG()
|
||
|
||
self.is_action = False
|
||
self.command_queue.task_done()
|
||
|
||
def move_x(self, distance=30, direction=1):
|
||
# direction为0代表负方向,为1代表正方向
|
||
dis = int(distance)
|
||
self.robotCTL.StartJOG(ref=4, nb=1, dir=direction, max_dis=100, vel=20.0, acc=100.0)
|
||
|
||
def move_y(self, distance=30, direction=1):
|
||
dis = int(distance)
|
||
self.robotCTL.StartJOG(ref=4, nb=2, dir=direction, max_dis=100, vel=20.0, acc=100.0)
|