forked from Ofekw/SoftEng-306-Project-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateEntity.py
166 lines (143 loc) · 6.01 KB
/
generateEntity.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
#!/usr/bin/python
import subprocess
import os
import stat
import sys
import getopt
# Directory of where the temporary robot .py files will be written
directory = "./se306Project1/src/"
debug_text1 = " debug = Debugger(robot)"
debug_text2 = " debug.start()"
processes = []
def main(argv, config):
testing = False
debugging = False
try:
opts, args = getopt.getopt(argv,"td")
except getopt.GetoptError:
print 'test.py -d for debugger mode '
print 'test.py -t for debugger mode '
sys.exit(2)
for opt, arg in opts:
if opt == '-t':
testing = True
elif opt == '-d':
debugging = True
# List of the temporary robot files created
file_name = []
# Types of robots that the script reads the config file for
robot_type = ["Picker", "Carrier", "Visitor", "Worker", "Animal", "Tractor"]
# Loads the fields in the config file
# Creates or overwrites the myworld.world file
myworld = open('world/myworld.world','w')
# Reads and add the world template to the world file to be written
world_template = open('world/templates/myworld.template').read()
myworld.write(world_template)
total_robots = 0;
# Each robot type will start at a different y-position
for type in robot_type:
# Loads the corresponding robot template
string = open('world/templates/' + type + '.template').read()
if config.get(type.lower() + '.number') is None:
break
number = int(config.get(type.lower() + '.number'))
if number > 4: # Limit of 4 for each entity
number = 4
robot = ""
if type == "Carrier":
x_value = -50
else:
x_value = 45 - robot_type.index(type)*4
for i in range(0, number):
if type != "Picker":
if type == "Carrier":
x_value += 10
constructor_name = "Robot" + type
y = -35-(4*i)
else:
constructor_name = type
y = -35-(4*i)
robot += type.lower() + "( pose [ " + str(x_value) + " " + str(y) + " 0.000 90 ] name \"r" + str(total_robots) + "\")" + "\n"
if type != "Carrier":
constructor = " robot = " + constructor_name + "(\"" + type + str(i) + "\", " + str(total_robots) + ", " + str(x_value) + ", " + str(y) +", math.pi/2)"
else:
constructor = " robot = " + constructor_name + "(\"" + type + str(i) + "\", " + str(total_robots) + ", " + str(x_value) + ", " + str(y) +", math.pi/2," + config.get("capacity.number") + ")"
name = type + str(i) + ".py" # Name of the robot files
else:
output = spawnPickers(i, total_robots, file_name, config)
robot += output[0]
constructor = output[1]
name = output[2]
file_name.append(name)
temp = open(os.path.join(directory, name),'w')
if debugging:
constructor = constructor + "\n" + debug_text1 + "\n" + debug_text2
# Replaces "@@@" string in the template with the constructor
temp.write(string.replace("@@@", constructor))
temp.close()
# Gives the temporary robot file run permission
os.chmod(directory+name,stat.S_IRWXU)
total_robots += 1
robot = robot + "\n"
# Writes the robot models to the world file
myworld.write(robot)
myworld.close()
# This is the carrier queue creation
string = open('world/templates/Queue.template').read()
temp = open(os.path.join(directory, "Queue_0.py"),'w')
# Replaces "@@@" string in the template with the constructor
temp.write(string.replace("@@@", " carrier_queue = Carrier_Queue(" + config.get("capacity.number") + ")"))
temp.close()
# Gives the temporary robot file run permission
os.chmod(directory+"Queue_0.py",stat.S_IRWXU)
file_name.append("Queue_0.py")
if not(testing):
for name in file_name:
# Runs all the temporary robot files created
command = ["rosrun", "se306Project1", name]
processes.append(subprocess.Popen(command, shell=False))
return file_name
"""
@function
The function generate the constructor line for both the world file and the robot's run script with
the appropriate position between the orchard rows
"""
def spawnPickers(number, total_robots, file_name, config):
picker = ""
#Creates the robot model of each robot to be appended to the world file
picker ="picker( pose [ " + str(getPickerPosition(config, number)) + " -28 0.000 90 ] name \"r" + str(total_robots) + "\")" + "\n"
#The constructor of that robot type with the robot_id and the x y positions
constructor = " robot = " + "Robot" + "Picker" + "(\"" + "Picker" + str(number) + "\", " + str(total_robots) + ", " + str(getPickerPosition(config, number)) + ", -28, math.pi/2," + config.get("capacity.number") + ")"
name = "Picker" + str(number) + ".py" #Name of the robot files
return [picker, constructor, name]
"""
@function
The function calculates the picker x position which is in between the orchard rows
"""
def getPickerPosition(config, robot_number):
WORLD_WIDTH = 80
rows = int(config.get('orchard.number'))
#max number of orchards is 10
if rows > 10:
rows = 10
elif rows < 1:
rows = 1
#Set the width between the rows
width_between_rows = WORLD_WIDTH/(rows)
if robot_number == 0:
return (-WORLD_WIDTH/2 + width_between_rows/2) - 4
else:
return -WORLD_WIDTH/2 + width_between_rows*robot_number
"""
@function
The function delete all the temporary robot files and the myworld.world file and kills
the entity processes running
"""
def exit_process(file_name):
os.remove('./world/myworld.world')
for name in file_name:
os.remove(directory+name)
for process in processes:
process.kill()
if __name__ == "__main__":
main(sys.argv[1:])