「完结撒花」使用intel realsense D435i深度相机获取信息,DOBOT MG400机械手实现动态实时抓取,并做了个GUI界面(python实现)

GUI界面如下,丑是丑了点,但很好用,嘿嘿。 

GUI部分

1.GUI界面可以实现的功能

  • 暂停・继续相机画面
  • 显示彩色RGB图像・深度图像・开运算结果图・进行图像处理后的图片(右侧)
  • 调节opencv轮廓查找框选面积范围
  • 调节图像区域
  • 给dobot机械手使能
  • 机械手的移动,复位,夹爪开关,z轴上下移动,自动运行和停止

2.GUI部分代码

①布局

1.菜单栏f1

"""
菜单栏 f1
"""
f1 = Frame(root)
info_width = 1280
info_height = 30
canvas_menu = Canvas(f1, width=info_width, height=info_height)
canvas_menu.pack(side=LEFT, padx=1, pady=1)
f1.pack()

虽然目前菜单栏还没东西也没功能,但留了一条备用

2.功能区域f2

"""
功能区域 f2
"""
f2 = Frame(root)
# info_width = 1280
# info_height = 434
frame_fun = Frame(f2, width=880, height=324)
frame_screenshot = Frame(f2, width=400, height=324)
frame_fun.pack(side=LEFT, padx=1, pady=1)
frame_screenshot.pack(side=LEFT, padx=1, pady=1)
f2.pack()

按钮们

btn_pause = Button(frame_fun, text="CAMERA PAUSE", width=10, height=1, command=pause_camera)
btn_pause.place(x=10, y=10)
btn_dobot_pause = Button(frame_fun, text="ロボット起動", width=10, height=1, command=dobot_enable)
btn_dobot_pause.place(x=110, y=10)
btn_dobot_move = Button(frame_fun, text="目標位置へ移動", width=10, height=1, command=dobot_move)
btn_dobot_move.place(x=210, y=10)
btn_dobot_move_home = Button(frame_fun, text="原点", width=10, height=1, command=dobot_move_home)
btn_dobot_move_home.place(x=310, y=10)
btn_dobot_nail_on = Button(frame_fun, text="チャック開", width=10, height=1, command=dobot_nail_on)
btn_dobot_nail_on.place(x=410, y=10)
btn_dobot_nail_off = Button(frame_fun, text="チャック閉", width=10, height=1, command=dobot_nail_off)
btn_dobot_nail_off.place(x=510, y=10)
btn_dobot_move_down = Button(frame_fun, text="down", width=10, height=1, command=dobot_move_down)
btn_dobot_move_down.place(x=610, y=10)
btn_dobot_move_up = Button(frame_fun, text="up", width=10, height=1, command=dobot_move_up)
btn_dobot_move_up.place(x=710, y=10)
btn_dobot_automove = Button(frame_fun, text="自動運転", width=10, height=1, command=dobot_automove)
btn_dobot_automove.place(x=610, y=60)
btn_dobot_automove_stop = Button(frame_fun, text="自動運転停止", width=10, height=1, command=dobot_automove_stop)
btn_dobot_automove_stop.place(x=710, y=60)

检出面积滑块们

# 检出面积滑块们
var_min = IntVar()
scl_area_min = Scale(frame_fun, orient=HORIZONTAL, length=200, width=10, from_=1000, to=5000, label='最小検出サイズ (1500)', tickinterval=1, resolution=50, variable=var_min)
area_min = 1500
var_min.set(1500)
scl_area_min.bind('<ButtonRelease-1>', get_var_min)
scl_area_min.place(x=10, y=60)

var_max = IntVar()
scl_area_max = Scale(frame_fun, orient=HORIZONTAL, length=200, width=10, from_=1000, to=5000, label='最大検出サイズ (5000)', tickinterval=1, resolution=50, variable=var_max)
area_max = 5000
var_max.set(5000)
scl_area_max.bind('<ButtonRelease-1>', get_var_max)
scl_area_max.place(x=230, y=60)

裁剪面积滑块们

# 裁剪面积滑块们
var_sliced_left = IntVar()
scl_sliced_left = Scale(frame_fun, orient=HORIZONTAL, length=200, width=10, from_=0, to=640, label='sliced_left (500)', tickinterval=1, resolution=10, variable=var_sliced_left)
sliced_left = 540
var_sliced_left.set(540)
scl_sliced_left.bind('<ButtonRelease-1>', get_sliced_left)
scl_sliced_left.place(x=10, y=140)

var_sliced_right = IntVar()
scl_sliced_right = Scale(frame_fun, orient=HORIZONTAL, length=200, width=10, from_=640, to=1280, label='sliced_right (930)', tickinterval=1, resolution=10, variable=var_sliced_right)
sliced_right = 960
var_sliced_right.set(960)
scl_sliced_right.bind('<ButtonRelease-1>', get_sliced_right)
scl_sliced_right.place(x=230, y=140)

var_sliced_up = IntVar()
scl_sliced_up = Scale(frame_fun, orient=HORIZONTAL, length=200, width=10, from_=0, to=360, label='sliced_up (150)', tickinterval=1, resolution=10, variable=var_sliced_up)
sliced_up = 220
var_sliced_up.set(220)
scl_sliced_up.bind('<ButtonRelease-1>', get_sliced_up)
scl_sliced_up.place(x=450, y=140)

var_sliced_down = IntVar()
scl_sliced_down = Scale(frame_fun, orient=HORIZONTAL, length=200, width=10, from_=360, to=720, label='sliced_down (570)', tickinterval=1, resolution=10, variable=var_sliced_down)
sliced_down = 570
var_sliced_down.set(570)
scl_sliced_down.bind('<ButtonRelease-1>', get_sliced_down)
scl_sliced_down.place(x=670, y=140)

log日志部分

# log日志
message_dobot = Text(frame_fun, height=1, cursor='arrow', width=25)
message_dobot.insert("insert", "wait for dobot")
message_dobot.config(fg='red')
message_dobot.place(x=10, y=280)
message_log = Text(frame_fun, height=1, cursor='arrow', width=25)
message_log.place(x=10, y=300)

3.下方相机画面部分

"""
相机区域 f3
"""
f3 = Frame(root)
frame_width = 427
frame_height = 366
rgb_frame = Frame(f3, width=frame_width, height=frame_height)
depth_frame = Frame(f3, width=frame_width, height=frame_height)
opening_frame = Frame(f3, width=frame_width, height=frame_height)
rgb_frame.pack(side=LEFT, padx=1, pady=1)
depth_frame.pack(side=LEFT, padx=1, pady=1)
opening_frame.pack(side=LEFT, padx=1, pady=1)
f3.pack()

rgb_info = Label(rgb_frame, text="LIVE", font=("游明朝", 10))
rgb_info.place(x=5, y=0)
canvas_rgb = Canvas(rgb_frame)
canvas_rgb.place(x=25, y=50)
depth_info = Label(depth_frame, text="DEPTH", font=("游明朝", 10))
depth_info.place(x=5, y=0)
canvas_depth = Canvas(depth_frame)
canvas_depth.place(x=25, y=50)
opening_info = Label(opening_frame, text="OPENING", font=("游明朝", 10))
opening_info.place(x=5, y=0)
canvas_opening = Canvas(opening_frame)
canvas_opening.place(x=25, y=50)

②按钮绑定的函数们

  • 暂停・继续画面  def pause_camera():

 这部分比较简单,实现声明一个realsense相机的类,把类里的暂停状态改一下就可以

class State:
    def __init__(self, *args, **kwargs):
        self.WIN_NAME = 'RealSense'
        self.paused = False

state = State()
def pause_camera():
    state.paused ^= True
  • 获取滑块值 以获取左侧裁剪区域值为例 def get_sliced_left(event):
def get_sliced_left(event):
    global sliced_left
    sliced_left = int(var_sliced_left.get())
  •  机器人使能 def dobot_enable():

使用tcp-ip地址连接机器人,创建多线程对象,由于机械臂收到信息会立刻返回,在循环最后面需要向30003发送Sync()指令做一个阻塞,等待前面执行完成再反馈0{}Sync(),否则会出现程序停止机械臂还在运作这样的情况。 

def dobot_enable():
    global lj29999
    global lj30003
    lj29999 = LJ29999("192.168.1.6", 29999)  # 创建实例对象
    lj30003 = LJ30003("192.168.1.6", 30003)
    # # 根据自己处理器自行判断使用线程还是进程
    thread = Thread(target=FK,  # 新建一个线程做数据刷新
                    daemon=True  # 设置新线程为daemon线程
                    )
    thread.start()
    lj29999.send(" EnableRobot()")
    print("机器人使能中...")
    message_dobot.delete(1.0, END)
    message_dobot.insert("insert", "机器人使能中...")
    message_dobot.config(fg='black')
def FK():
    fk30005 = feedback("192.168.1.6", 30005)  # 处理器性能较弱可以使用30005反馈
    while True:
        time.sleep(0.021)  # 略大于反馈时间即可
        fk30005.read()
        # print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        # print((fk30005.basics[1]))  # 打印轴实时位置

③下方视频区域

message_log.delete(1.0, END)
message_log.insert("insert", "live中...")
image_to_arm, arm_to_image = coordinate_transform()
img_color, img_depth, color_intrin, intr_matrix, color_intrin_coeffs, aligned_depth_frame, depth_mapped_image = get_aligned_images()
target_rgb, target_depth, target_opening = camera()
canvas_rgb.create_image(0, 0, anchor='nw', image=target_rgb)
canvas_depth.create_image(0, 0, anchor='nw', image=target_depth)
canvas_opening.create_image(0, 0, anchor='nw', image=target_opening)

root.update()
root.after(1)

视频实现时获取相机各个画面后,创建图片在画布上,实时刷新,已到达视频效果。

完整程序文件改日上传~!

猜你喜欢

转载自blog.csdn.net/weixin_49828565/article/details/127507680