11#!/usr/bin/env python3
22import tkinter
3+ from tkinter import ttk
34import time
45import threading
56import os
7+ import serial
8+ import serial .tools .list_ports
69
710from pymycobot .mycobot import MyCobot
811from pymycobot import PI_PORT , PI_BAUD
@@ -19,69 +22,85 @@ def __init__(self):
1922 self .win .title ("树莓派版 Mycobot 测试工具" )
2023 self .win .geometry ("918x511+10+10" ) # 290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
2124
25+ self .port_label = tkinter .Label (self .win , text = "选择串口:" )
26+ self .port_label .grid (row = 0 )
27+ self .port_list = ttk .Combobox (
28+ self .win , width = 15 , postcommand = self .get_serial_port_list
29+ ) # #创建下拉菜单
30+ self .get_serial_port_list () # #给下拉菜单设定值
31+ self .port_list .current (0 )
32+ self .port_list .grid (row = 0 , column = 1 )
33+
34+ self .baud_label = tkinter .Label (self .win , text = "选择波特率:" )
35+ self .baud_label .grid (row = 1 )
36+ self .baud_list = ttk .Combobox (self .win , width = 15 )
37+ self .baud_list ["value" ] = ("115200" , "1000000" )
38+ self .baud_list .current (0 )
39+ self .baud_list .grid (row = 1 , column = 1 )
40+
2241 # Connect
2342 self .connect_label = tkinter .Label (self .win , text = "连接mycobot:" )
24- self .connect_label .grid (row = 0 )
43+ self .connect_label .grid (row = 2 )
2544 self .connect = tkinter .Button (self .win , text = "连接" , command = self .connect_mycobot )
2645 self .disconnect = tkinter .Button (
2746 self .win , text = "断开" , command = self .disconnect_mycobot
2847 )
29- self .connect .grid (row = 1 )
30- self .disconnect .grid (row = 1 , column = 1 )
48+ self .connect .grid (row = 3 )
49+ self .disconnect .grid (row = 3 , column = 1 )
3150
3251 # Check servo.
3352 self .check_label = tkinter .Label (self .win , text = "检测连接:" )
34- self .check_label .grid (row = 2 )
53+ self .check_label .grid (row = 4 )
3554 self .check_btn = tkinter .Button (
3655 self .win , text = "开始检测" , command = self .check_mycobot_servos
3756 )
38- self .check_btn .grid (row = 2 , column = 1 )
57+ self .check_btn .grid (row = 4 , column = 1 )
3958
4059 # Calibration.
4160 self .calibration_num = None
4261 self .calibration_label = tkinter .Label (self .win , text = "校准舵机:" )
43- self .calibration_label .grid (row = 3 )
62+ self .calibration_label .grid (row = 5 )
4463 self .calibration_btn = tkinter .Button (
4564 self .win , text = "开始校准" , command = self .calibration_mycobot
4665 )
47- self .calibration_btn .grid (row = 3 , column = 1 )
66+ self .calibration_btn .grid (row = 5 , column = 1 )
4867
4968 # LED.
5069 self .set_color_label = tkinter .Label (self .win , text = "测试Atom灯板:" )
51- self .set_color_label .grid (row = 4 , columnspan = 2 )
70+ self .set_color_label .grid (row = 6 , columnspan = 2 )
5271 self .color_red = tkinter .Button (
5372 self .win , text = "设置红色" , command = lambda : self .send_color ("red" )
5473 )
5574 self .color_green = tkinter .Button (
5675 self .win , text = "设置绿色" , command = lambda : self .send_color ("green" )
5776 )
58- self .color_red .grid (row = 5 )
59- self .color_green .grid (row = 5 , column = 1 )
77+ self .color_red .grid (row = 7 )
78+ self .color_green .grid (row = 7 , column = 1 )
6079
6180 # Aging test.
6281 self .aging_stop = False
6382 self .movement_label = tkinter .Label (self .win , text = "老化循环动作:" )
64- self .movement_label .grid (row = 6 )
83+ self .movement_label .grid (row = 8 )
6584 self .start_btn = tkinter .Button (
6685 self .win , text = "开始" , command = self .start_aging_test
6786 )
68- self .start_btn .grid (row = 7 )
87+ self .start_btn .grid (row = 9 )
6988 self .stop_btn = tkinter .Button (
7089 self .win , text = "停止" , command = self .stop_aging_test
7190 )
72- self .stop_btn .grid (row = 7 , column = 1 )
91+ self .stop_btn .grid (row = 9 , column = 1 )
7392
7493 # Release
7594 self .release_btn = tkinter .Button (
7695 self .win , text = "放松所有电机" , command = self .release_mycobot
7796 )
78- self .release_btn .grid (row = 8 )
97+ self .release_btn .grid (row = 10 )
7998
8099 # rectify
81100 self .rectify_btn = tkinter .Button (
82101 self .win , text = "校准pid" , command = self .rectify_mycobot
83102 )
84- self .rectify_btn .grid (row = 8 , column = 1 )
103+ self .rectify_btn .grid (row = 10 , column = 1 )
85104
86105 # Log output.
87106 self .log_label = tkinter .Label (self .win , text = "日志:" )
@@ -104,8 +123,19 @@ def run(self):
104123 # Connect method
105124 # ============================================================
106125 def connect_mycobot (self ):
126+ port = self .port_list .get ()
127+ if not port :
128+ self .write_log_to_Text ("请选择串口" )
129+ return
130+ baud = self .baud_list .get ()
131+ if not baud :
132+ self .write_log_to_Text ("请选择波特率" )
133+ return
134+ baud = int (baud )
135+
107136 try :
108- self .mycobot = MyCobot (PI_PORT , PI_BAUD )
137+ # self.mycobot = MyCobot(PI_PORT, PI_BAUD)
138+ self .mycobot = MyCobot (port , baud )
109139 # self.mycobot = MyCobot("/dev/cu.usbserial-0213245D", 115200)
110140 self .write_log_to_Text ("连接成功 !" )
111141 except Exception as e :
@@ -140,6 +170,9 @@ def release_mycobot(self):
140170 self .write_log_to_Text ("Release over." )
141171
142172 def check_mycobot_servos (self ):
173+ if not self .has_mycobot ():
174+ return
175+
143176 ping_commands = [
144177 [255 , 255 , 1 , 2 , 1 , 251 ],
145178 [255 , 255 , 2 , 2 , 1 , 250 ],
@@ -221,6 +254,9 @@ def stop_aging_test(self):
221254 self .write_log_to_Text ("结束老化测试失败 !!!" )
222255
223256 def rectify_mycobot (self ):
257+ if not self .has_mycobot ():
258+ return
259+
224260 for i in range (1 , 7 ):
225261 self .mycobot .set_servo_data (i , 24 , 0 )
226262 time .sleep (0.1 )
@@ -378,6 +414,14 @@ def _calibration_test(self):
378414 time .sleep (0.7 )
379415 self .write_log_to_Text ("测试校准结束." )
380416
417+ def get_serial_port_list (self ):
418+ plist = [
419+ str (x ).split (" - " )[0 ].strip () for x in serial .tools .list_ports .comports ()
420+ ]
421+ print (plist )
422+ self .port_list ["value" ] = plist
423+ return plist
424+
381425 def get_current_time (self ):
382426 """Get current time with format."""
383427 current_time = time .strftime ("%Y-%m-%d %H:%M:%S" , time .localtime (time .time ()))
0 commit comments