Revision cfef1cdc scout/scoutsim/GUI.py

View differences:

scout/scoutsim/GUI.py
30 30

  
31 31
class Behaviors(object):
32 32
    def __init__(self):
33
        print "Warning: You should not need to create an instance of Behaviors"
34

  
33
        self.behaviors = []
34
        self.loadBehaviorList()
35 35
    # this takes advantage of the fact that our behavior list start at 1    
36
    behaviors = ["Pause", "CW Circle", "CCW Circle", "Odometry",
37
                          "Navigation Map", "Scheduler", "Warehouse",
38
                          "Line Follow", "WL Test", "Maze Solve"]
39
    @classmethod
40
    def getListFromDir(self):
41
        behaviors = []
42
        for path, dirname, fnames in os.walk('../libscout/src'):
43
            if 'src/behaviors' in path or 'src/test_behaviors' in path:
44
                path_parts = path.split('/')
45

  
46
                for f in sorted(fnames):
47
                    # The pause_scout behavior needs to go first!
48
                    if f.endswith('.h') and 'pause_scout' in f:
49
                        behaviors.insert(0, f.split('.')[0])
50
                    # Everything else goes in alphabetical order
51
                    elif f.endswith('.h'):
52
                        behaviors.append(f.split('.')[0])
53
        return behaviors
54

  
55
    @classmethod
36
    #behaviors = ["Pause", "CW Circle", "CCW Circle", "Odometry",
37
    #                      "Navigation Map", "Scheduler", "Warehouse",
38
    #                      "Line Follow", "WL Test", "Maze Solve"]
39
#    @classmethod
40
#    def getListFromDir(self):
41
#        behaviors = []
42
#        for path, dirname, fnames in sorted(os.walk('../libscout/src')):
43
#            if 'src/behaviors' in path or 'src/test_behaviors' in path:
44
#                print "The path is!!!!!!!!!!!!", path
45
#                path_parts = path.split('/')
46
#
47
#                for f in sorted(fnames):
48
#                    # The pause_scout behavior needs to go first!
49
#                    print f
50
#                    if f.endswith('.h') and 'pause_scout' in f:
51
#                        behaviors.insert(0, f.split('.')[0])
52
#                    # Everything else goes in alphabetical order
53
#                    elif f.endswith('.h'):
54
#                        behaviors.append(f.split('.')[0])
55
#        return behaviors
56

  
57
    
58
    def loadBehaviorList(self):
59
        filename = "behaviorList.txt"
60
        print "laoding behavior list!!!"
61
        with open(filename, "r") as f:
62
            for line in f.read().rstrip().split("\n"):
63
                self.behaviors+= [line]
64
    
56 65
    def getNumber(self, behaviorName):
57
        behaviors = self.getListFromDir()
58
        if (behaviorName in behaviors):
59
            return behaviors.index(behaviorName)
66
        if (behaviorName in self.behaviors):
67
            return self.behaviors.index(behaviorName)
60 68
    
61
    @classmethod
62 69
    def getName(self, index):
63
        behaviors = self.getListFromDir()
64 70
        if (0 <= index < len(self.Behavior)):
65
            return behaviors[index]
71
            return self.behaviors[index]
66 72
        else:
67 73
            return -1
68 74
    
69
    @classmethod
70 75
    def getBehaviors(self):
71 76
        # "Pause" is not considered a behavior in GUI
72
        behaviors = self.getListFromDir()
73
        return behaviors[1:]
77
        return self.behaviors[1:]
74 78

  
75 79
# each scout is represented by this class
76 80
class Scout(object):
77 81
    numOfScouts = 0 # class variable keeping track of scouts
78 82
    
79
    def __init__(self, x=0, y=0, theta=0, name=u"",
83
    def __init__(self, x=0, y=0, theta=0, name=u"", behaviors=None,
80 84
                       Scouts=None, oninit = False):
81 85
        Scout.numOfScouts += 1 # reverted if not successful
82 86
        if len(name) == 0:
......
94 98
        self.x, self.y = x, y 
95 99
        self.theta = theta
96 100
        
101
        self.behaviors = behaviors
102

  
97 103
        # spawn the scout if it is not scout1 (automatically spawned)
98 104
        if (not oninit): 
99 105
            if (self.SpawnInSimulator()):
......
150 156
        self.terminateOldBehavior()
151 157
        # do rosprocess calls for new behavior
152 158
        roscommand = ("rosrun libscout libscout %s %s"%
153
                            (Behaviors.getNumber(self.behavior), self.name))
159
                            (self.behaviors.getNumber(self.behavior), self.name))
154 160
        self.process = subprocess.Popen(roscommand, shell=True)
155 161

  
156 162
    def teleop(self):
......
243 249
    def initData(self):
244 250
        self.scouts = {}
245 251
        #FIXME: these arguments are not right...fix them!!!
252
        self.behaviors = Behaviors()
246 253
        self.scouts["scout1"] = Scout(x=0, y=0, theta=0, name="scout1",
254
                                      behaviors=self.behaviors,
247 255
                                      Scouts = self.scouts, oninit=True)
248

  
256
    
249 257
    # addButton callback
250 258
    def addScout(self, x_wx, y_wx, theta_wx, name_wx):
251 259
        # x, y, theta, name are given as wx Inputs
......
253 261
        y = y_wx.GetValue()
254 262
        theta = theta_wx.GetValue()
255 263
        name = name_wx.GetValue()
256
        newSc = Scout(x, y, theta, name, self.scouts)
264
        newSc = Scout(x, y, theta, name, self.scouts, self.behaviors)
257 265
        if (newSc.valid):
258 266
            # successful
259 267
            self.scouts[newSc.name] = newSc
......
424 432
        # Inputs
425 433
        # drop down menue
426 434
        scoutChoices = wx.Choice(self.window, 
427
                                    choices=Behaviors.getBehaviors())
435
                                    choices=self.behaviors.getBehaviors())
428 436
        #   buttons
429 437
        pauseButton = wx.ToggleButton(self.window, label="Pause")
430 438
        runButton = wx.Button(self.window, label="Run")

Also available in: Unified diff