Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / scoutsim / GUI.py @ 9a4d8398

History | View | Annotate | Download (14.2 KB)

1 01f09975 Yuyang Guo
#!/usr/bin/python
2
3
# Behavior GUI
4
5
# shell subprocess libs
6
import subprocess
7
import shlex  #for converting string commands to tokens
8
9
# GUI libs
10
import wx
11
import wx.lib.intctrl
12
import wx.lib.agw.floatspin
13
14
# not sure why these 2 are here
15
import string
16
import signal
17
18
# ros imports for spawning and killing scout
19
import rospy
20
import roslib
21
roslib.load_manifest("scoutsim")
22
23
# scoutsim imports
24
from scoutsim.srv import *
25
26
from collections import defaultdict
27
28 9a4d8398 Yuyang Guo
29
class Behaviors(object):
30
    def __init__(self):
31
        print "Warning: You should not need to create an instance of Behaviors"
32
33
    # this takes advantage of the fact that our behavior list start at 1    
34
    behaviors = ["Pause", "CW Circle", "CCW Circle", "Odometry",
35
                          "Navigation Map", "Scheduler", "Warehouse",
36
                          "Line Follow", "WL Test"]
37
    @classmethod
38
    def getNumber(self, behaviorName):
39
        if (behaviorName in Behaviors.behaviors):
40
            return Behaviors.behaviors.index(behaviorName)
41
    
42
    @classmethod
43
    def getName(self, index):
44
        if (0 <= index < len(self.Behavior)):
45
            return Behaviors.behaviors[index]
46
        else:
47
            return -1
48
    
49
    @classmethod
50
    def getBehaviors(self):
51
        # "Pause" is not considered a behavior in GUI
52
        return Behaviors.behaviors[1:]
53
54 01f09975 Yuyang Guo
# each scout is represented by this class
55
class Scout(object):
56
    numOfScouts = 0 # class variable keeping track of scouts
57
    
58
    def __init__(self, x=0, y=0, theta=0, name=u"",
59
                       Scouts=None, oninit = False):
60
        Scout.numOfScouts += 1 # reverted if not successful
61
        if len(name) == 0:
62
            # automatically give name according to numOfScouts
63
            name = self.autoNameGen(Scouts)
64
        # set vars
65
        self.name = name
66
        self.behavior = "chilling"
67
        self.process =  None# used to keep track of all processes
68
                        # related to this scout  
69 9a4d8398 Yuyang Guo
        self.paused = False
70 01f09975 Yuyang Guo
        self.valid = False
71
        # these vars are right now just the original position
72
        # but when ODOMETRY is done, we could actually use them
73
        self.x, self.y = x, y 
74
        self.theta = theta
75
        
76
        # spawn the scout if it is not scout1 (automatically spawned)
77
        if (not oninit): 
78
            if (self.SpawnInSimulator()):
79
                # spawned correctly
80
                self.valid = True
81
            else:
82
                Scout.numOfScouts -= 1;
83
        
84
85
    def autoNameGen(self, Scouts):
86
        i = 1
87
        while (True):
88
            if not (("scout%d"%i) in Scouts):
89
                return "scout%d"%i
90
            i+=1
91
92
93
        # update the classvariable
94
        
95
96
    def SpawnInSimulator(self):
97
        try:
98
            # ros spawn routine
99
            rospy.wait_for_service('/spawn');
100
            service = rospy.ServiceProxy('/spawn', Spawn);
101
            response = service(self.x, self.y, self.theta, self.name)
102
            return True
103
        except rospy.ServiceException as Error:
104
            return False
105 9a4d8398 Yuyang Guo
   
106
    def terminateOldBehavior(self):
107
        if self.process != None:
108
            # now terminate the process
109
            self.process.terminate() #TODO: this kills the process
110
                                     #      but maybe not the rosnode
111
                                     #      change to "rosnode kill" instead
112
            # make sure old behavior is terminated before we run the new one
113
            self.process.wait()
114 01f09975 Yuyang Guo
115 9a4d8398 Yuyang Guo
            # the following lines causes lag and does not solve real issues
116
            # first unregister the node
117
            #cmd = "rosnode kill /%s_behavior"%self.name
118
            #temp = subprocess.Popen(cmd, shell=True)
119
            #temp.wait() 
120
            # this node loses contact but still is not cleaned up in rosnode
121
            # however, this should not affect usage at all.
122 01f09975 Yuyang Guo
        
123 9a4d8398 Yuyang Guo
    def changeBehavior(self, behavior):
124
        self.behavior = behavior
125
        if (behavior == "pause"):
126
            self.paused = True
127
        else:
128
            self.paused = False
129
        self.terminateOldBehavior()
130 01f09975 Yuyang Guo
        # do rosprocess calls for new behavior
131
        roscommand = shlex.split("rosrun libscout libscout %s %s"%
132 9a4d8398 Yuyang Guo
                            (self.name, Behaviors.getNumber(self.behavior)))
133
        self.process = subprocess.Popen(roscommand, shell=False)
134 01f09975 Yuyang Guo
135
    def killSelf(self):
136
        # terminate its current behavior
137 9a4d8398 Yuyang Guo
        self.terminateOldBehavior()
138
        
139 01f09975 Yuyang Guo
        # ros call to kill scout in simulator
140
        try:
141
            rospy.wait_for_service("/kill")
142
            service = rospy.ServiceProxy("/kill", Kill)
143
            response = service(self.name)
144
            Scout.numOfScouts -= 1
145
            self.valid = False
146
        except rospy.ServiceException, e:
147
            print "warning: kill scout unsuccessful" # TODO: change to alert
148
            self.valid = True
149
        # remove from the class
150
151
152
# a wrapper for wx Frame
153
class Window(wx.App):
154
    def __init__(self, title=""):
155
        super(Window, self).__init__()
156
        self.initUIFrame()
157
158
    def initUIFrame(self):
159
        self.GUIFrame = GUI(None, "hallo!")
160
161
162
# actual GUI frame
163
class GUI(wx.Frame):
164
    def __init__(self, parent, title):
165
        super(GUI, self).__init__(parent, title=title,
166
            style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER, size=(600, 600))
167
        
168
        # open up scoutsim race
169
        command = shlex.split("rosrun scoutsim scoutsim_node race")
170
        self.simWindowProcess = subprocess.Popen(command, shell=False)
171
        #rospy.wait_for_service('/spawn') #don't know why this is here
172
        
173
        # register call back for on close cleanup
174
        self.Bind(wx.EVT_CLOSE, self.onClose) 
175
176
177
        self.initData()
178
        self.initUI()
179
        self.Show(True)     
180
    
181
    # do clean up after user close the window
182
    def onClose(self, event):
183
        print "Cleaning up all Processes"
184
        
185
        # kill all the behavior processes
186
        for scout in self.scouts:
187
            process = self.scouts[scout].process
188
            if (process != None):
189
                process.terminate()
190
191
        # kill the scout sim window
192
        self.simWindowProcess.terminate()
193
        print "done"
194
        self.Destroy()
195
196
    def initData(self):
197
        self.scouts = {}
198 9a4d8398 Yuyang Guo
        #FIXME: these arguments are not right...fix them!!!
199 01f09975 Yuyang Guo
        self.scouts["scout1"] = Scout(x=0, y=0, theta=0, name="scout1",
200
                                      Scouts = self.scouts, oninit=True)
201
202
    # addButton callback
203
    def addScout(self, x_wx, y_wx, theta_wx, name_wx):
204
        # x, y, theta, name are given as wx Inputs
205
        x = x_wx.GetValue()
206
        y = y_wx.GetValue()
207
        theta = theta_wx.GetValue()
208
        name = name_wx.GetValue()
209
        newSc = Scout(x, y, theta, name, self.scouts)
210
        if (newSc.valid):
211
            # successful
212
            self.scouts[newSc.name] = newSc
213
            self.addScoutBox(newSc.name)
214
            # alert user spawn successful
215
            wx.MessageBox(u"Scout '%s' created successfully at (%d,%d,%d\u00B0)"
216
                            %(newSc.name, newSc.x, newSc.y, newSc.theta), 
217
                            "Scout Created", wx.OK | wx.ICON_INFORMATION)
218
219
        else:
220
            #failed to create scout
221
            wx.MessageBox("Scout Creation failed :( Check for duplicate name",
222
                            "Add Scout failed", wx.OK | wx.ICON_ERROR)
223
224
    def removeScout(self, name):
225
        sct = self.scouts[name]
226
        sct.killSelf()
227
        if (sct.valid == False):
228
            # successful, remove from the dictionary
229
            del self.scouts[name]
230 9a4d8398 Yuyang Guo
            # delete the refresh the display
231
            self.mainArea.Hide(self.sizer[name]) #required by wx before remove
232
            self.mainArea.Remove(self.sizer[name])
233 01f09975 Yuyang Guo
            del self.sizer[name]
234
            self.window.Layout()
235
            self.window.Refresh()
236
            self.window.Update()
237
        else:
238
            raise Exception
239
240 9a4d8398 Yuyang Guo
    # change UI display and invoke change Behavior in scout
241
    def changeBehavior(self, name, currBehaviorLabel, 
242
                        pauseButton, newBehavior):
243
        
244
        # to handle user pressing "Run" during pause
245
        if (newBehavior != "Pause" and self.scouts[name].paused == True):
246
            print "trying to do stuff!!!"
247
            self.correctPauseButton(name, 
248
                                        pauseButton, pauseToResume=True)
249
        
250
        currBehaviorLabel.SetLabel(" | Current Behavior: %s"%newBehavior)
251
        scout = self.scouts[name]
252
        scout.changeBehavior(newBehavior)
253
254
255
    def correctPauseButton(self, name, pauseButton, pauseToResume):
256
        if (pauseToResume):
257
            print "correcting button!"
258
            self.scouts[name].paused = False
259
            pauseButton.SetValue(False) # unpress it
260
            pauseButton.SetLabel("Pause")
261
        else:
262
            self.scouts[name].paused = True
263
            pauseButton.SetValue(True) # press it
264
            pauseButton.SetLabel("Resume")
265
266
    def buttonDown(button):
267
        return button.GetValue()
268
269
    def pauseResumeScout(self, name, pauseButton, currBehaviorLabel, dropMenu):
270
        if (pauseButton.GetValue() == True): # 
271
            # change behavior to Pause
272
            self.changeBehavior(name, currBehaviorLabel, pauseButton, "Pause")    
273
            # change button to "Resume" & label to "Pause"
274
            currBehaviorLabel.SetLabel("Pause")
275
            self.correctPauseButton(name, 
276
                                        pauseButton, pauseToResume=False)
277
        else:
278
            # resume
279
            # change behavior to whatever is in the drop down menu
280
            self.changeBehavior(name, currBehaviorLabel, pauseButton, 
281
                        dropMenu.GetStringSelection())
282
            self.correctPauseButton(name, 
283
                                        pauseButton, pauseToResume=True)
284
            
285
286 01f09975 Yuyang Guo
    ############################ UI stuff here ###########################
287
    
288
    def initUI(self):
289
        self.initAddScoutArea()
290
291
    # the labels and input boxes for adding a scout through GUI
292
    def initAddScoutArea(self):
293
        # all the layout stuff copied over: using grid layout
294
        # button callbacks are changed
295
        self.totalCols = 8
296
        self.window = wx.ScrolledWindow(self, style=wx.VSCROLL)
297
        self.mainArea = wx.GridSizer(cols=1)
298
        sizer = wx.FlexGridSizer(rows=4, cols=self.totalCols, hgap=5, vgap=5)
299
300
        # Labels
301
        blankText = wx.StaticText(self.window, label="")
302
        newScout = wx.StaticText(self.window, label="New Scout")
303
        newScoutName = wx.StaticText(self.window, label="Name:")
304
        startXTitle = wx.StaticText(self.window, label="X:")
305
        startYTitle = wx.StaticText(self.window, label="Y:")
306
        startThetaTitle = wx.StaticText(self.window, label="Rad:")
307
308
        # Inputs
309
        newScoutInput = wx.TextCtrl(self.window)
310
        startX = wx.lib.agw.floatspin.FloatSpin(self.window, min_val=0, digits=5)
311
        startY = wx.lib.agw.floatspin.FloatSpin(self.window, min_val=0, digits=5)
312
        startTheta = wx.lib.agw.floatspin.FloatSpin(self.window, min_val=0, digits=5)
313
        addButton = wx.Button(self.window, id=wx.ID_ADD)
314
315
        # Pretty Stuff
316
        hLine = wx.StaticLine(self.window, size=(600, 5))
317
        bottomHLine = wx.StaticLine(self.window, size=(600, 5))
318
        
319
        # Row 0: just the label add scout
320
        sizer.Add(newScout)
321
        for i in range(7):
322
            sizer.AddStretchSpacer(1)
323
        # Row 1: input(name), x coord, y coord, rad, 
324
        sizer.AddMany([newScoutName, (newScoutInput, 0, wx.EXPAND), startXTitle,
325
            startX, startYTitle, startY, startThetaTitle, startTheta])
326
        # Row 2: just the add button to the right
327
        for i in range(7):
328
            sizer.AddStretchSpacer(1)
329
        sizer.Add(addButton)
330
        # Row 3
331
        
332
        # Events
333
        addButton.Bind(wx.EVT_BUTTON, lambda event: self.addScout(
334
            startX, startY, startTheta, newScoutInput))
335
336
        sizer.AddGrowableCol(idx=1)
337
        self.mainArea.Add(sizer, proportion=1, flag=wx.ALL|wx.EXPAND, border=10)
338
        self.window.SetSizer(self.mainArea)
339
        self.sizer = defaultdict()
340
        self.window.Layout()
341
        
342
        # make the scout1's controller
343
        self.addScoutBox("scout1");
344
        
345
    # copied over from old GUI
346
    def addScoutBox(self, name):
347
        self.sizer[name] = wx.FlexGridSizer(rows=2, cols=5, hgap=5, vgap=5)
348
        # Labels
349 9a4d8398 Yuyang Guo
        scoutName = wx.StaticText(self.window, label="Scout: %s"%name)
350 01f09975 Yuyang Guo
        behaviorLabel = wx.StaticText(self.window, label="Behavior: ")
351
        currBehaviorLabel = wx.StaticText(self.window,
352
            label="  |  Current Behavior: Slacking off")
353
354
        # Inputs
355
        # drop down menue
356 9a4d8398 Yuyang Guo
        scoutChoices = wx.Choice(self.window, 
357
                                    choices=Behaviors.getBehaviors())
358 01f09975 Yuyang Guo
        #   buttons
359
        pauseButton = wx.ToggleButton(self.window, label="Pause")
360
        runButton = wx.Button(self.window, label="Run")
361
        killButton = wx.Button(self.window, label="Kill")
362
        teleopButton = wx.ToggleButton(self.window, label="Teleop")
363
364
        # row 0
365
        self.sizer[name].Add(scoutName)
366
        self.sizer[name].Add(currBehaviorLabel, wx.EXPAND | wx.ALIGN_RIGHT)
367
        self.sizer[name].AddStretchSpacer(1)
368
        self.sizer[name].AddStretchSpacer(1)
369
        self.sizer[name].Add(killButton, wx.ALIGN_RIGHT)
370
371
        # row 1
372
        self.sizer[name].Add(behaviorLabel)
373
        self.sizer[name].Add(scoutChoices, wx.EXPAND)
374
        self.sizer[name].Add(runButton)
375
        self.sizer[name].Add(pauseButton, wx.ALIGN_RIGHT)
376
        self.sizer[name].Add(teleopButton) 
377
        # Events
378
        killButton.Bind(wx.EVT_BUTTON, lambda event: self.removeScout(name))
379 9a4d8398 Yuyang Guo
        runButton.Bind(wx.EVT_BUTTON,
380
                lambda event: self.changeBehavior(name, currBehaviorLabel,
381
                    pauseButton, # needed to handle press "run" during pause
382
                    scoutChoices.GetStringSelection()))
383
        pauseButton.Bind(wx.EVT_TOGGLEBUTTON, 
384
            lambda event: self.pauseResumeScout(name, pauseButton,
385
                                        currBehaviorLabel, scoutChoices))
386 01f09975 Yuyang Guo
387
        self.mainArea.Add(self.sizer[name], proportion=1,
388
            flag=wx.ALL | wx.EXPAND, border=10)
389
        self.window.Layout()
390
        return True
391
392
393
394
if __name__ == '__main__':
395
    # open up GUI
396
    window = Window(title="Colony Scout Manager")
397
    window.MainLoop()