Exemplo n.º 1
0
class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ur = None
        # self.whole_layout = None
        self.clear_button = None
        self.paint_button = None
        self.paint_widget = None
        self._init_sdk()
        self._init_window()
        self._init_widget()  # widget 在 layout 前面
        self._init_layout()

    def _init_sdk(self):
        self.ur = UR()
        self.ur.connect()

    def _init_window(self):
        self.setWindowTitle('robot paint self')
        self.setFixedSize(640, 480)

    def _init_layout(self):
        whole_layout = QVBoxLayout()
        top_layout = QHBoxLayout()
        self.setLayout(whole_layout)
        whole_layout.addLayout(top_layout)
        whole_layout.addWidget(self.paint_widget)
        top_layout.addWidget(self.clear_button)
        top_layout.addWidget(self.paint_button)

    def _init_widget(self):
        self.paint_widget = MyPaintWidget()
        self.clear_button = QPushButton('清理')
        self.paint_button = QPushButton('绘制')
        self.clear_button.clicked.connect(self.clear)
        self.paint_button.clicked.connect(self.paint)

    def clear(self):
        self.paint_widget.clear()

    def scale_data(self, x, y):
        """
        画板坐标映射到机器人坐标
        :param x: 画板 x 坐标
        :param y: 画板 y 坐标
        :return: 机器人 x,y 坐标
        """
        x = 0.3 - 0.6 * x / 640
        y = -0.5 + 0.4 * y / 480
        return x, y

    def paint(self):
        """
        调用机器人sdk
        :return:
        """
        # 使用move_j移动至放松位置
        self.ur.move_j([-84.56, -87.06, -89.02, -96.33, 90.87, 89.87])
        # PaintWidget(自定义)对象paint_widget 获取所有绘画点
        points = self.paint_widget.points[::10]  # 每隔十个点选取
        # 开启sdk轨迹
        self.ur.enable_trail()
        # 绘制这些点
        for point in points:  # points 字典吗?
            px = point['x']
            py = point['y']
            # 使用move_p 方式移动可以提高效率的同时精确画图
            x, y = self.scale_data(px, py)
            self.ur.move_p([x, y, 0.45422, 180, 0, 90])
# 放松姿态
ur.move_j([-84.56, -87.06, -89.02, -96.33, 90.87, 89.87])

# 定义四个点
pos = [0.3, 0, 0, 180, 0, 90]

ur.clear_points()
ur.disable_trail()

ur.show_point((0, 0, 0), 0.1, [0, 255, 0, 0])  # 原点
ur.show_point((0.3, 0, 0))

# 移动到第一个点
# ur.move_p(pos)
# 开启线路显示
ur.enable_trail()

pos_1 = [0, 0, 0, 0, 0, 0]
pos_2 = [0, 0, 0, 0, 0, 0]
pos_3 = [0, 0, 0, 0, 0, 0]
pos_4 = [0, 0, 0, 0, 0, 0]

alpha = 0.5
delta = 0.01


# pos_1[0] -= delta
# pos_1[1] += delta
#
# pos_2[0] += delta
# pos_2[1] += delta
Exemplo n.º 3
0
class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        # 机器人驱动
        self.ur = UR()
        # 连接
        self.ur.connect()
        # 设置标题
        self.setWindowTitle('机器人绘制')
        # 设置窗口大小
        self.setFixedSize(640, 480)
        # 创建整体布局
        wholeLayout = QVBoxLayout()
        # 设置布局
        self.setLayout(wholeLayout)
        # 创建上部布局
        topLayout = QHBoxLayout()
        # 添加布局到整体布局中
        wholeLayout.addLayout(topLayout)
        # 创建两个按钮
        clearBtn = QPushButton('清理')
        paintBtn = QPushButton('绘制')
        # 按钮添加到布局中
        topLayout.addWidget(clearBtn)
        topLayout.addWidget(paintBtn)
        # 创建自定义控件
        self.paintWidget = PaintWidget()
        # 添加到整体布局中
        wholeLayout.addWidget(self.paintWidget)
        # 设置按钮点击事件
        # 槽函数可以是普通函数 也可以是类的方法
        clearBtn.clicked.connect(self.clear)
        # 绘制事件
        paintBtn.clicked.connect(self.paint)

    def clear(self):
        '''
        清理
        :return:
        '''
        self.paintWidget.clear()

    def scaleData(self, x, y):
        '''
        对数据缩放处理
        :param cal:
        :return:
        '''
        x = 0.3 - 0.6 * x / 640
        y = -0.5 + 0.4 * y / 480
        return x, y

    def paint(self):
        '''
        调用机器人绘制
        :return:
        '''
        self.ur.disable_trail()
        self.ur.move_j([-84.56, -87.06, -89.02, -96.33, 90.87, 89.87])
        # 获取所有的点
        points = self.paintWidget.points[::10]
        self.ur.enable_trail()
        # 绘制点的数量
        # 遍历所有的点,移动过去
        for point in points:
            px = point['x']
            py = point['y']
            # print(px, py)
            # 移动 采用哪一种移动方式? movel movep
            # pose:[x,y,z,rx,ry,rz]
            # 缩放x和y
            x, y = self.scaleData(px, py)
            # print(x, y)
            print("(px, py)::({}, {}), (x, y)::({}, {})".format(px, py, x, y))
            # x y z 单位:m
            self.ur.move_p([x, y, 0.45422, 180, 0, 90])