Project

General

Profile

Revision cfef1cdc

IDcfef1cdc01a6b56dab9d025b2489cb2fc5a7e33d
Parent 7f2ccb46
Child b333d404

Added by Yuyang Guo about 11 years ago

fixed the issue of behavior list not corresponding to the GUI list
of behaviors

View differences:

scout/libscout/generate_behavior_lists.py
66 66
                LAST_PATH_CODE : last_paths,
67 67
                BASE_NAME_CODE : base_names}
68 68

  
69
# Write all the replacements in R_FILES
70
for outfile_name in R_FILES.keys():
71
    with open(outfile_name, 'w') as outfile:
72
        with open(R_FILES[outfile_name], 'r') as infile:
73
            
74
            # Write an obvious header so no one will edit our nicely
75
            # generated file
76
            outfile.write(AUTOGEN_HEADER)
69
# also simultaneously write a txt file for the GUI to read from
70
with open("../scoutsim/behaviorList.txt", "w") as listFile:
71
    # Write all the replacements in R_FILES
72
    for outfile_name in sorted(R_FILES.keys()):
73
        with open(outfile_name, 'w') as outfile:
74
            with open(R_FILES[outfile_name], 'r') as infile:
75
                
76
                # Write an obvious header so no one will edit our nicely
77
                # generated file
78
                outfile.write(AUTOGEN_HEADER)
77 79

  
78
            for line in infile.readlines():
79
                # If the line needs replacing, don't write the orgininal
80
                # to the output file; rather, write many versions of the
81
                # line replaced with each replacement value.
82
                replaced = False
83
                for r_key in replacements:
84
                    if r_key in line:
85
                        replaced = True
86
                        for b in replacements[r_key]:
87
                            outfile.write(line.replace(r_key, b))
88
                # If it doesn't need replacing, just write the original
89
                if not replaced:
90
                    outfile.write(line)
80
                for line in infile.readlines():
81
                    # If the line needs replacing, don't write the orgininal
82
                    # to the output file; rather, write many versions of the
83
                    # line replaced with each replacement value.
84
                    replaced = False
85
                    for r_key in replacements:
86
                        if r_key in line:
87
                            replaced = True
88
                            for b in replacements[r_key]:
89
                                outfile.write(line.replace(r_key, b))
90
                                if (r_key == BASE_NAME_CODE):
91
                                    listFile.write(b+"\n")
92
                    # If it doesn't need replacing, just write the original
93
                    if not replaced:
94
                        outfile.write(line)
95

  
96
    
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