-
Notifications
You must be signed in to change notification settings - Fork 18
/
raspbian_for_robots_update.py
executable file
·401 lines (340 loc) · 15.5 KB
/
raspbian_for_robots_update.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import wx
import os
import sys
from datetime import datetime
import subprocess
try:
import auto_detect_robot
except:
pass
# This program runs various update programs for Raspbian for Robots, from Dexter Industries.
# See more about Dexter Industries at http://www.dexterindustries.com
'''
This program is started from the script /home/pi/di_update/Raspbian_For_Robots/update_master.sh
From this program you will be able to:
1. Update the OS. This should update the Raspbian operating system.
2. Update DI Software. This will update the Raspbian for Robots software, including the source and example files.
3. Update DI Hardware. This will update the firmware for the GrovePi and the GoPiGo.
Initially Authored: 10/1/2015
Last Update: 24/Oct/2019
'''
# Developer Notes and References:
# http://www.blog.pythonlibrary.org/2010/03/18/wxpython-putting-a-background-image-on-a-panel/
# ComboBoxes! http://wiki.wxpython.org/AnotherTutorial#wx.ComboBox
PIHOME="/home/pi"
DEXTER="Dexter"
SCRATCH="Scratch_GUI"
s = "/";
seq = (PIHOME, DEXTER,"lib",DEXTER,SCRATCH) # This is sequence of strings.
SCRATCH_PATH = s.join( seq )+"/"
robots = {}
robotbitmap = None
ICON_PATH="/home/pi/di_update/Raspbian_For_Robots/update_gui_elements/"
# Writes debug to file "error_log"
def write_debug(in_string):
# In in time logging.
#print in_string
write_string = str(datetime.now()) + " - " + in_string + "\n"
error_file = open('error_log', 'a') # File: Error logging
error_file.write(write_string)
error_file.close()
def detect():
try:
detected_robots=auto_detect_robot.autodetect()
print (detected_robots)
# handling it this way to cover the cases of
# multiple robot detection
if detected_robots.find("GoPiGo3") != -1:
write_state("GoPiGo3")
elif detected_robots.find("GoPiGo") != -1 and detected_robots.find("3") == -1:
write_state("GoPiGo")
elif detected_robots.find("BrickPi3")==0:
write_state("BrickPi3")
elif detected_robots.find("GrovePi")==0:
write_state("GrovePi")
else:
write_state('dex')
except Exception as e:
write_state("dex")
print(e)
def write_state(in_string):
try:
selected_robot = open(ICON_PATH+'selected_state', 'w') # File: selected state
if(' ' in in_string):
in_string = "dex"
selected_robot.write(in_string)
selected_robot.close()
except:
pass
print ("write_state: {}".format(in_string))
def read_state():
try:
selected_robot = open(ICON_PATH+'selected_state', 'r') # File: selected state
in_string = selected_robot.read()
selected_robot.close()
except:
in_string = "dex"
print ("read_state: {}".format(in_string))
return in_string
# This code is to allow customers to choose which robot they want to upgrade
# def write_robots_to_update():
# try:
# robots_2_update_file = open('/home/pi/di_update/Raspbian_For_Robots/update_gui_elements/robots_2_update','w')
# for robot in robots:
# if robots[robot].GetValue():
# robots_2_update_file.write(robot+'\n')
# robots_2_update_file.close()
# except:
# pass
def send_bash_command(bashCommand):
# print bashCommand
write_debug(bashCommand)
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE) #, stderr=subprocess.PIPE)
# print process
output = process.communicate()[0]
# print output
return output
def send_bash_command_in_background(bashCommand):
# Fire off a bash command and forget about it.
write_debug(bashCommand)
process = subprocess.Popen(bashCommand.split())
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
global robots
global update_firmware_static
global robotbitmap
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
# self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
self.SetBackgroundColour(wx.WHITE)
self.frame = parent
Sizer = wx.BoxSizer(wx.VERTICAL) # outside sizer. Contains 3 horizontal sizers
topSizer = wx.BoxSizer(wx.HORIZONTAL) # top logo
hSizer = wx.BoxSizer(wx.HORIZONTAL) # main
bottomSizer = wx.BoxSizer(wx.HORIZONTAL) # static text
exitSizer = wx.BoxSizer(wx.HORIZONTAL)
# hSizer contains 2 vertical sizer
vSizerLeft = wx.BoxSizer(wx.VERTICAL)
vSizerRight = wx.BoxSizer(wx.VERTICAL) # for robot image
# TOP SIZER WITH LOGO
logoSizer = wx.BoxSizer(wx.VERTICAL)
bmp = wx.Bitmap(ICON_PATH+"dexter_industries_logo.png",type=wx.BITMAP_TYPE_PNG)
logo=wx.StaticBitmap(self,bitmap=bmp)
logoSizer.Add(logo,0,wx.RIGHT|wx.LEFT|wx.EXPAND)
topSizer.Add(logoSizer)
# LEFT SIZER
# Update DI Software
update_software_label = wx.StaticText(self, label="Libraries and Projects:")
update_software = wx.Button(self, label="Update Dexter Software")
update_software.Bind(wx.EVT_BUTTON, self.update_software)
#-------------------------------------------------------------------
# Update Firmware
update_firmware_label = wx.StaticText(self, label="Firmware:")
update_firmware = wx.Button(self, label="Update Robot")
update_firmware.Bind(wx.EVT_BUTTON, self.update_firmware)
update_firmware.Bind(wx.EVT_ENTER_WINDOW, self.hovertxt_on)
update_firmware.Bind(wx.EVT_LEAVE_WINDOW, self.hovertxt_off)
# Drop Boxes
controls = ['Choose your robot', 'GoPiGo3', 'GoPiGo', 'GrovePi', 'BrickPi3'] # Options for drop down.
# Select Platform.
folder = read_state()
if folder in controls[1:]: # skip 'Choose your robot'
print("setting drop down to {}".format(folder))
robotDrop = wx.ComboBox(self, -1, str(folder), choices=controls, style=wx.CB_READONLY) # Drop down setup
else:
print ('default drop down')
robotDrop = wx.ComboBox(self, -1, "Choose your Robot", choices=controls, style=wx.CB_READONLY) # Drop down setup
robotDrop.Bind(wx.EVT_COMBOBOX, self.robotDrop) # Binds drop down.
#robotDrop.Bind(wx.EVT_ENTER_WINDOW, self.hovertxt_on)
#robotDrop.Bind(wx.EVT_LEAVE_WINDOW, self.hovertxt_off)
vSizerLeft.AddSpacer(5)
vSizerLeft.Add(update_software_label)
vSizerLeft.Add(update_software, 0, wx.EXPAND)
vSizerLeft.AddSpacer(45)
vSizerLeft.Add(update_firmware_label)
vSizerLeft.Add(update_firmware, 0, wx.EXPAND)
vSizerLeft.Add(robotDrop)
vSizerLeft.AddSpacer(10)
# RIGHT SIZER
icon_sizer = wx.BoxSizer(wx.VERTICAL)
robot = read_state()+".png"
print(robot)
bmp = wx.Bitmap(ICON_PATH+robot, type=wx.BITMAP_TYPE_PNG)
robotbitmap=wx.StaticBitmap(self, bitmap=bmp)
icon_sizer.Add(robotbitmap, 1, wx.RIGHT| wx.LEFT| wx.EXPAND| wx.ALIGN_TOP | wx.ALIGN_RIGHT)
vSizerRight.Add(icon_sizer, 0, wx.ALIGN_RIGHT)
# BOTTOM SIZER: static text plus exit button
update_firmware_static = wx.StaticText(self, -1,
"Use this to update the robot firmware. This only needs to be done occasionally! If you have questions, please ask on our forums!")
bottomSizer.AddSpacer(20)
bottomSizer.Add(update_firmware_static, 1, wx.EXPAND | wx.RESERVE_SPACE_EVEN_IF_HIDDEN)
bottomSizer.AddSpacer(5)
# Exit
exit_button = wx.Button(self, label="Exit")
exit_button.Bind(wx.EVT_BUTTON, self.onClose)
exitSizer.Add(exit_button, 0, wx.RIGHT | wx.ALIGN_RIGHT)
exitSizer.AddSpacer(20)
hSizer.AddSpacer(30)
hSizer.Add(vSizerLeft,0,wx.EXPAND)
hSizer.AddSpacer(50)
hSizer.Add(vSizerRight,0,wx.EXPAND)
hSizer.AddSpacer(30)
Sizer.AddSpacer(5)
Sizer.Add(topSizer,1,wx.EXPAND)
Sizer.Add(hSizer,1, wx.EXPAND)
Sizer.Add(bottomSizer, 1, wx.EXPAND)
Sizer.Add(exitSizer, 0, wx.ALIGN_RIGHT)
Sizer.AddSpacer(20)
self.SetSizer(Sizer)
update_firmware_static.Hide()
# RobotDrop
# This is the function called whenever the drop down box is called.
def robotDrop(self, event):
global robotbitmap
write_debug("robotDrop Selected.")
controls = ['dex', 'GoPiGo3', 'GoPiGo', 'GrovePi', 'BrickPi3'] # Options for drop down.
value = event.GetSelection()
# print (controls[value])
# position = 0 # Position in the key list on file
write_state(controls[value]) # print value to file.
# Update Picture
robot = ICON_PATH+read_state()+".png"
# robot = read_state()+".png"
newrobotbitmap = wx.Bitmap(robot,type=wx.BITMAP_TYPE_PNG)
robotbitmap.SetBitmap(newrobotbitmap)
# Update the Operating System.
def update_raspbian(self, event):
write_debug("update_raspbian")
dlg = wx.MessageDialog(self, 'Operating System Update will start! Depending on your internet speed this could take a few hours. Please do not close the terminal window or restart the update.', 'Alert!', wx.OK|wx.CANCEL|wx.ICON_INFORMATION)
ran_dialog = False
if dlg.ShowModal() == wx.ID_OK:
start_command = "sudo sh /home/pi/di_update/Raspbian_For_Robots/update_os.sh"
send_bash_command_in_background(start_command)
print ("Start Operating System update!")
ran_dialog = True
else:
print ("Cancel Operating System Update!")
dlg.Destroy()
write_debug("Cancel Operating System Update.")
# Update the Software.
def update_software(self, event):
write_debug("Update Dexter Software")
# write_robots_to_update() # Taking out for now. This would update the write_robots_to_update file.
dlg = wx.MessageDialog(self, 'Software update will start. Please do not close the terminal window or restart the update.', 'Alert!', wx.OK|wx.CANCEL|wx.ICON_INFORMATION)
ran_dialog = False
if dlg.ShowModal() == wx.ID_OK:
start_command = "sudo bash /home/pi/di_update/Raspbian_For_Robots/upd_script/update_all.sh"
send_bash_command_in_background(start_command)
print "Start software update!"
ran_dialog = True
else:
print "Cancel Software Update!"
dlg.Destroy()
write_debug("Update Dexter Software Finished.")
def update_firmware(self, event):
ran_dialog = False # For the first loop of choices.
show_dialog = False # For the second loop of choices.
write_debug("Update Dexter firmware")
folder = read_state()
if folder == 'dex':
dlg = wx.MessageDialog(self, 'Use the dropdown to select the hardware to update.', 'Alert!', wx.OK|wx.CANCEL|wx.ICON_INFORMATION)
program = " "
show_dialog = False
elif folder == 'GoPiGo3':
program = "/home/pi/Dexter/GoPiGo3/Firmware/gopigo3_flash_firmware.sh"
dlg = wx.MessageDialog(self, 'We will begin the firmware update.', 'Firmware Update', wx.OK|wx.CANCEL|wx.ICON_INFORMATION)
show_dialog = True
elif folder == 'GoPiGo':
program = "/home/pi/di_update/Raspbian_For_Robots/upd_script/update_GoPiGo_Firmware.sh"
dlg = wx.MessageDialog(self, 'We will begin the firmware update.', 'Firmware Update', wx.OK|wx.CANCEL|wx.ICON_INFORMATION)
show_dialog = True
elif folder == 'GrovePi':
program = "/home/pi/di_update/Raspbian_For_Robots/upd_script/update_GrovePi_Firmware.sh"
dlg = wx.MessageDialog(self, 'We will begin the firmware update.', 'Firmware Update', wx.OK|wx.CANCEL|wx.ICON_INFORMATION)
show_dialog = True
elif folder == 'BrickPi3':
program = "/home/pi/Dexter/BrickPi3/Firmware/brickpi3samd_flash_firmware.sh"
dlg = wx.MessageDialog(self, 'We will begin the firmware update.', 'Firmware Update', wx.OK|wx.CANCEL|wx.ICON_INFORMATION)
show_dialog = True
ran_dialog = False
if ((dlg.ShowModal() == wx.ID_OK) and (show_dialog)):
if folder == 'GoPiGo':
# If we're doing a GoPiGo Firmware update, we NEED to prompt the user, ONE MORE TIME to disconnect the motors.
dlg2 = wx.MessageDialog(self, 'DISCONNECT THE MOTORS! Before firmware update, disconnect the motors from the GoPiGo or you risk damaging the hardware.', 'DISCONNECT MOTORS!', wx.OK|wx.ICON_EXCLAMATION)
dlg2.ShowModal()
dlg2.Destroy()
start_command = "sudo bash "+program
# send_bash_command_in_background(start_command)
print "Start Firmware test!" + str(folder)
print send_bash_command(start_command)
# send_bash_command(program)
ran_dialog = True
else:
print "Cancel Firmware Update!"
dlg.Destroy()
# Depending on what the user chose, we either cancel or complete.
if ran_dialog:
dlg = wx.MessageDialog(self, 'Firmware update is done!', 'Begin', wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
else:
dlg = wx.MessageDialog(self, 'Firmware update is canceled.', 'Canceled', wx.OK|wx.ICON_HAND)
dlg.ShowModal()
dlg.Destroy()
write_debug("Programming Started.")
def hovertxt_on(self,event):
#print("HOVERING")
update_firmware_static.Show()
event.Skip()
def hovertxt_off(self,event):
#print("HOVERING")
update_firmware_static.Hide()
event.Skip()
def onClose(self, event): # Close the entire program.
write_debug("Close Pressed.")
# dlg = wx.MessageDialog(self, 'The Pi will now restart. Please save all open files before pressing OK.', 'Alert!', wx.OK|wx.ICON_INFORMATION)
# dlg.ShowModal()
# dlg.Destroy()
dlg = wx.MessageDialog(self, 'You must reboot for changes to take effect. Reboot now?', 'Reboot', wx.OK|wx.CANCEL|wx.ICON_INFORMATION)
if dlg.ShowModal() == wx.ID_OK:
# Reboot
send_bash_command('sudo reboot')
else:
# Do nothing.
print "No reboot."
dlg.Destroy()
"""
"""
self.frame.Close()
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
# wx.ComboBox
wx.Icon(ICON_PATH+'favicon.ico', wx.BITMAP_TYPE_ICO)
wx.Log.SetVerbose(False)
wx.Frame.__init__(self, None, title="Dexter Industries Update",size=(500,550)) # Set the frame size
panel = MainPanel(self)
self.Center()
########################################################################
class Main(wx.App):
""""""
#----------------------------------------------------------------------
def __init__(self, redirect=False, filename=None):
"""Constructor"""
wx.App.__init__(self, redirect, filename)
dlg = MainFrame()
dlg.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
send_bash_command_in_background("xhost +")
write_debug(" # Program # started # !")
detect()
# reset_file() #Reset the file every time we turn this program on.
app = Main()
app.MainLoop()