-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
333 lines (288 loc) · 13.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#==============================IMPORTS================================
import tkinter
from tkinter import ttk, messagebox
import datetime
#import sv_ttk
import os
import json
#from PIL import ImageTk, Image
#=======================Importing Custom Modules=======================
import taskcreator
import databridge
#Initialising all required Tkinter functions
root = tkinter.Tk()
root.attributes('-fullscreen',True)
root.geometry("1920x1080")
root.title("Robotic Arm Software")
frame = tkinter.Frame(root)
frame.pack()
#Loading Assets and Initialising Variable
play_image = ImageTk.photoImage(Image.open("assets/play.png").resize((30,30)))
pause_image = ImageTk.photoImage(Image.open("assets/pause.png").resize((30,30)))
stop_image = ImageTk.photoImage(Image.open("assets/stop.png").resize((30,30)))
close_image = ImageTk.photoImage(Image.open("assets/close.png").resize((30,30)))
minim_image = ImageTk.photoImage(Image.open("assets/minimize.png").resize((30,30)))
robo_arm_image = ImageTk.photoImage(Image.open("assets/arm.png").resize((256,256)))
path = "./tasks"
progress = 0
state = "stopped"
last_update = datetime.datetime.now()
com_ports = databridge.getPorts()
run_selection_file = None
run_selection_item = None
select_arm = None
#=======================Defining Panes==========================================
#essental ops
button_frame = tkinter.Frame(root)
button_frame.pack(side="top")
#Left Pane
left_pane = tkinter.LabelFrame(frame, text="Options", padx=100)
left_pane.grid(row=0,column=0,padx=20,pady=20)
#Left Bottom Pane
left_bottom_pane = tkinter.LabelFrame(frame, text = "Task List", padx=50)
left_bottom_pane.grid(row=1,column=0,padx=20,pady=20)
#Right Pane
right_pane = tkinter.LabelFrame(frame, text="Arm Operations",pady=50)
right_pane.grid(row=0,column=2,padx=20,pady=20, rowspan=2)
#Middle Pane
middle_pane = tkinter.LabelFrame(frame, padx=50, bg="gray")
middle_pane.grid(row=0, column=1, padx=20, pady=20)
#Middle Bottom Pane
middle_bottom_pane = tkinter.LabelFrame(frame, text= "Movement Control", padx=50)
middle_bottom_pane.grid(row=1,column=1,padx=20,pady=20)
#=========================Essential Functions=====================================
#Task Handler Loader
def callhandler():
taskcreator.creaate_interface_angles(root)
def load_tasks():
files = [f for f in os.listdir(path) if f.endswith('.json')]
if Task_list_box.size()>0:
Task_list_box.delete(0,tkinter.END)
load_tasks()
else:
for filename in files:
with open(o.path.join(path,filename),'r') as f:
task = json.load(f)
task_name = task[0]["task name"]
Task_list_box.insert(tkinter.END, task_name)
def remove_task():
selected_file = Task_list_box.curselection()
if selected_file:
selected_item = [Task_list_box.get(index) for index in selected_file]
response = messagebox.askquestion("Confirmation",f"Are you sure you want to delete {selected_item[0]}", icon="warning",parent = root)
if response =="yes":
os.remove(os.path.join(path, selected_item[0]+".json"))
load_tasks()
else:
return
else:
messagebox.showwarning("Warning", "No Item Selected!")
def callviewer():
selected_file = Task_list_box.curselection()
if selected_file:
selected_item = [Task_list_box.get(index) for index in selected_file]
taskcreator.list_interface(root,selected_item[0])
else:
messagebox.showerror("Error","No task selected", parent=root)
def get_ports():
global com_ports
com_ports = databridge.gtPorts()
arm_combobox['values'] = com_ports
root.after(10000,get_ports)
def on_select_com(event):
selected_port = arm_combobox.get()
messagebox.showinfo("Info",f"{selected_port} selected!")
response = databridge.testConnection(selected_port)
if response == "True":
messagebox.showinfo("Info","Connection Successful!")
elif response != True or False:
messagebox.showerror("Error",response)
else:
messagebox.showeror("Error","Connection Failed")
def select_task():
global run_selection_file
global run_selection_item
run_selection_file = Task_list_box.curselection()
run_selection_item =[Task_list_box.get(index) for index in run_selection_file]
run_selection_item = run_selection_item[0]
run_selection_item = str(run_selection_item)
if run_selection_item:
messagebox.showinfo("Selected","file has been selected, you may run the task now", parent=root)
else:
messagebox.showerror("Error","No task selected",parent=root)
def run_task():
run_port = arm_combobox.get()
if run_port:
if run_selection_item is not None:
run_file = f"tasks\{run_selection_item}.json"
#play_button.invoke()
databridge.send_coordinates(run_file, run_port, root)
else:
messagebox.showerror("Error", "No file selected",parent=root)
else:
messagebox.showerror("Error","No port selected")
def getcurpos():
arm_no = select_arm
run_port = arm_combobox.get()
if arm_no is not None:
if run_port:
resp = databridge.retrv_cur_pos(arm_no,run_port)
resp_x = int(resp[:3])
resp_y = int(resp[3:6])
resp_z = int(resp[6:9])
mv_text_entry2.delete(0,"end")
mv_text_entry2.insert(0,"")
mv_text_entry2.insert(0,f"X: {resp_x}, Y: {resp_y}, Z: {resp_z}")
else:
messagebox.showerror("Error","No port selected!")
else:
messagebox.showerror("Error","No arm selected")
def on_entry_click(event, entry):
if entry.cget("fg") == "grey":
entry.delete(0,"end")
entry.inster(0,"")
entry.config(fg="white")
def on_focusout(event, entry, text):
if entry.get()=="":
entry.insert(0,text)
entry.config(fg="grey")
#==================================Begin Widgets====================
#Close minimixe util functions
button_frame = tkinter.LabelFrame(middle_pane)
button_frame.grid(row=1,column=1,columnspan=2)
close_app = ttk.Button(button_frame, image=close_image, command=root.destroy)
close_app.grid(row=0,column=0)
minimize_app = ttk.Button(button_frame, image=minim_image, command=root.iconify)
minimize_app.grid(row=0,column=1)
#Left Pane Widgets
style = ttk.Style()
style.configure('Custom.TButton', borderwidth=0, relief=0)
Task_opt =ttk.Label(left_pane, text="Task Options", padding=(20,20,20,20))
Task_opt.grid(row=0,column=0,padx=20,pady=20)
Task_opt.pack()
Load_task = ttk.Button(left_pane,text="Load Tasks", padding=(45,5), style="Custom.TButton", command=load_tasks)
Load_task.pack(pady=(10,0))
View_task = ttk.Button(left_pane, text="View Task", padding=(45,5), command=callviewer)
View_Task.pack(pady=(10,0))
Add_task.pack(pady=(10,0))
Remove_task =ttk.Button(left_pane, text="Remove Task", padding=(39,5), command=remove_task)
Remove_task.pack(pady=(10,10))
#Left Bottom Pane Widgets
Task_list = ttk.Label(left_bottom_pane, text="Task List", padding = (20,20,20,20))
Task_list.grid(row=0,column=0,padx=20,pady=20)
Task_list.pack()
Task_list_box = tkinter.Listbox(left_bottom_pane, justify="center", font=("Helvetica",12))
Task_list_box.pack(fill='x',expand='False')
Select_task = ttk.Button (left_bottom_pane, text="Select Task", padding=(20,10,20,10),width=10, command=select_task)
Select_task.pack(side=tkinter.LEFT, padx=(10,10),pady=10)
Run_task = ttk.Button(left_bottom_pane, text="Run Task", padding = (20,10,20,10), width=10, command=run_task)
Run_task.pack(side=tkinter.LEFT, padx=(10,10), pady=10)
Task_list.pack_propagate(0)
Select_task.pack_propagate(0)
Run_task.pack_propagate(0)
#Middle Pane Widgets
arm_images = tkinter.Label(middle_pane, image=robo_arm_image, bg="gray")
arm_images.grid(row=0,column=0)
arm_text = tkinter.Label(middle_pane,text="ROBOTIC \nARM \nSOFTWARE", font=("Helvetica", 24), bg="gray")
arm_text.grid(row=0,column=1)
#Middle Bottom Pane Widgets
Retrv_curr_pos = ttk.Button(middle_bottom_pane, text="Retrieve\nCurrent\nPosition", padding=(10,40), command=getcurpos)
Retrv_curr_pos.grid(row=0,column=0,rowspan=4,pady=(10,0),padx=(10,0),sticky ='E W')
joint_label = ttk.Label(middle_bottom_pane, text = "Joint Values")
joint_label.grid(row=0,column=1,pady=(10,0),padx=(10,0),sticky='W')
mv_to_joints =ttk.Button(middle_bottom_pane, text="Move to Joints", padding=(25,5))
mv_to_joints.grid(row=0,column=2,pady=(10,0),padx=(10,10),sticky='E')
mv_text_entry = tkinter.Entry(middle_bottom_ane, width=45,fg="grey")
mv_text_entry.insert(0, "Enter the joint values")
mv_text_entry.grid(row=1,column=1,pady=(5,0),padx=(10,10),columnspan=2, sticky='W')
cart_pos = ttk.Label(middle_bottom_pane, text="Cartesian Position")
cart_pos.grid(row=2,column=1,pady=(10,0),padx=(10,0),sticky = 'W')
mv_to_cart = ttk.Button(middle_bottom_pane, text ="Move to Postion", padding=(12,5))
mv_to_cart.grid(row=2,column=2, pady=(10,0),padx=(10,0),sticky='W')
mv_text_entry2 = tkinter.Entry(middle_bottom_pane, width=45, fg="grey")
mv_text_entry2.grid(row=3,column=1,pady=(5,10),padx=(10,10),columnspan=2, sticky='W')
time_label = ttk.Label(middle_bottom_pane, text="", padding=(20,20,20,20))
time_label.grid(row=4,column=0,padx=12, pady=12, columnspan=3)
mv_text_entry.bind("<FocusIn>",lambda event: on_entry_click(event,mv_text_entry))
mv_text_entry.bind("<FocusOut>",lambda event: on_focusout(event,mv_text_entry,"Enter the join values"))
mv_text_entry2.bind("<FocusIn>", lambda event: on_entry_click(even,mv_text_entry2))
mv_text_entry2.bind("<FocusOut>",lambda event: on_focusout(event,mv_text_entry2,"Enter the Cartesian Values"))
#========================UI Update Functions===========================================================================
#Arm Selections UI Updates
def select_arm1():
global select_arm
arm1_selector.configure(bg="green")
arm2_selector.configure(bg="gray")
select_arm = 1
def select_arm2():
global select_arm
arm1_selector.configure(bg="gray")
arm2_selector.configure(bg="green")
select_arm = 2
def send_one_move(run_ang):
run_port = arm_combobox.get()
amt = spinbox_degree.get()
print(amt)
if run_port:
#play_button.invoke()
databridge.move_by_one(run_ang,amt,run_port)
else:
messagebox.showerror("Error","No port selected")
#===============================Widgets Contd.===========================================================================
#Right Pane Widgets
arm_selector = tkinter.LabelFrame(right_pane, text="Arm Selection")
arm_selector.grid(row=0,column=0,padx=20,pady=20,columnspan=2)
com_selector = tkinter.Label(arm_selector,text="COM Port Selection")
com_selector.grid(row=1,column=0,padx=20,pady=20)
arm_combobox = ttk.Combobox(arm_selector, values=com_ports, state="readonly")
arm_combobox.grid(row=1,column=1,padx=20,pady=20)
arm1_selector = tkinter.Button(arm_selector,text="Select Arm1", command=select_arm1, bg="gray")
arm1_selector.grid(row=2,column=0,padx=20,pady=20)
arm2_selector = tkinter.Button(arm_selector,text="Select Arm2", command=select_arm2, bg="gray")
arm2_selector.grid(row=2,column=1,padx=20,pady=20)
arm_type_selector = tkinter.LabelFrame(right_pane,text="Select Movement Type", padx = 80)
arm_type_selector.grid(row3, column=0, padx=20, pady=20, columnspan=2)
spinbox_degree = tkinter.Spinbox(arm_type_selector, from_=10, to=90, width=10)
degree_label = tkinter.Label(arm_type_selector,text="Step Degrees (°): ")
degree_label.grid(row=5,column=0,padx=20,pady=20)
Spinbox_degree.grid(row=5,column=1,padx=20,pady=20)
#Six Button ==> -Tx -Ty -Tz -Rx -Ry -Rz +Tx +Ty +Tz +Rx +Ry +Rz
tx_button = ttk.Button(right_pane,text="-Tx", padding=(40,10), command = lambda: send_one_move("rs1"))
ty_button = ttk.Button(right_pane,text="-Ty", padding=(40,10), command = lambda: send_one_move("rs2"))
tz_button = ttk.Button(right_pane,text="-Tz", padding=(40,10), command = lambda: send_one_move("rs3"))
rx_button = ttk.Button(right_pane,text="-Rx", padding=(40,10), command = lambda: send_one_move("rs4"))
ry_button = ttk.Button(right_pane,text="-RY", padding=(40,10), command = lambda: send_one_move("rs5"))
rz_button = ttk.Button(right_pane,text="-Rz", padding=(40,10), command = lambda: send_one_move("rs6"))
tx_button.grid(row=6,column=0,padx=5,pady=5)
ty_button.grid(row=7,column=0,padx=5,pady=5)
tz_button.grid(row=8,column=0,padx=5,pady=5)
rx_button.grid(row=9,column=0,padx=5,pady=5)
ry_button.grid(row=10,column=0,padx=5,pady=5)
rz_button.grid(row=11,column=0,padx=5,pady=5)
tx_button = ttk.Button(right_pane,text="+Tx", padding=(40,10), command = lambda: send_one_move("s1"))
ty_button = ttk.Button(right_pane,text="+Ty", padding=(40,10), command = lambda: send_one_move("s2"))
tz_button = ttk.Button(right_pane,text="+Tz", padding=(40,10), command = lambda: send_one_move("s3"))
rx_button = ttk.Button(right_pane,text="+Rx", padding=(40,10), command = lambda: send_one_move("s4"))
ry_button = ttk.Button(right_pane,text="+RY", padding=(40,10), command = lambda: send_one_move("s5"))
rz_button = ttk.Button(right_pane,text="+Rz", padding=(40,10), command = lambda: send_one_move("s6"))
tx_button.grid(row=6,column=1,padx=5,pady=5)
ty_button.grid(row=7,column=1,padx=5,pady=5)
tz_button.grid(row=8,column=1,padx=5,pady=5)
rx_button.grid(row=9,column=1,padx=5,pady=5)
ry_button.grid(row=10,column=1,padx=5,pady=5)
rz_button.grid(row=11,column=1,padx=5,pady=5)
arm_combobox.bind("<<ComboboxSelected>>",on_select_com)
#================================Begin Functions================================
#Live Time Function
def update_time():
current_time = datetime.datetime.now()
time_str = current_time.strftime("%H:%M:%S")
date_str = current_time.strftime("%Y-%m -%D")
time_label.config(text="Current Time: " + time_str + "\nToday's Date: " + date_str)
root.after(1000, update_time)
#===============================Closing Function (Util)=========================================
get_ports()
#=================================End Functions (Tkinter)=======================================
sv_ttk.set_theme("dark")
update_time()
root.mainloop()