@@ -25,29 +25,32 @@ def __init__(self, config):
25
25
26
26
self .hit_obstacle = False
27
27
self .crash = 0
28
-
28
+ self .timeout = self .config ['timeout' ]
29
+ self .bounding_box = np .reshape (np .array (
30
+ self .config ['bounding_box' ], dtype = float ), (3 ,2 )).T
31
+
29
32
self ._initSubscribers (config ['topics' ])
30
33
self ._initPublishers (config ['topics' ])
31
34
32
35
33
36
def _initSubscribers (self , config ):
34
37
self .state_sub = rospy .Subscriber (
35
38
"/%s/%s" % (config ['quad_name' ], config ['state' ]),
36
- QuadState ,
39
+ QuadState ,
37
40
self .callbackState ,
38
41
queue_size = 1 ,
39
42
tcp_nodelay = True )
40
-
43
+
41
44
self .obstacle_sub = rospy .Subscriber (
42
45
"/%s/%s" % (config ['quad_name' ], config ['obstacles' ]),
43
- ObstacleArray ,
46
+ ObstacleArray ,
44
47
self .callbackObstacles ,
45
48
queue_size = 1 ,
46
49
tcp_nodelay = True )
47
50
48
51
self .start_sub = rospy .Subscriber (
49
52
"/%s/%s" % (config ['quad_name' ], config ['start' ]),
50
- Empty ,
53
+ Empty ,
51
54
self .callbackStart ,
52
55
queue_size = 1 ,
53
56
tcp_nodelay = True )
@@ -70,12 +73,11 @@ def callbackState(self, msg):
70
73
if not self .is_active :
71
74
return
72
75
73
- self .pos .append (np .array (
74
- [msg .header .stamp .to_sec (),
75
- msg .pose .position .x ,
76
- msg .pose .position .y ,
77
- msg .pose .position .z ]
78
- ))
76
+ pos = np .array ([msg .header .stamp .to_sec (),
77
+ msg .pose .position .x ,
78
+ msg .pose .position .y ,
79
+ msg .pose .position .z ])
80
+ self .pos .append (pos )
79
81
80
82
pos_x = msg .pose .position .x
81
83
bin_x = int (max (min (np .floor (pos_x ),self .xmax ),0 ))
@@ -86,38 +88,76 @@ def callbackState(self, msg):
86
88
self .is_active = False
87
89
self .publishFinish ()
88
90
91
+ if rospy .get_time () - self .time_array [0 ] > self .timeout :
92
+ self .abortRun ()
93
+
94
+ outside = ((pos [1 :] > self .bounding_box [1 ,:])
95
+ | (pos [1 :] < self .bounding_box [0 ,:])).any (axis = - 1 )
96
+ if (outside == True ).any ():
97
+ self .abortRun ()
98
+
99
+
89
100
90
101
def callbackStart (self , msg ):
91
- self .is_active = True
102
+ if not self .is_active :
103
+ self .is_active = True
92
104
self .time_array [0 ] = rospy .get_rostime ().to_sec ()
93
105
94
106
95
107
def callbackObstacles (self , msg ):
96
108
if not self .is_active :
97
109
return
98
-
110
+
99
111
obs = msg .obstacles [0 ]
100
- dist = np .linalg .norm (np .array ([obs .position .x ,
101
- obs .position .y ,
112
+ dist = np .linalg .norm (np .array ([obs .position .x ,
113
+ obs .position .y ,
102
114
obs .position .z ]))
103
115
margin = dist - obs .scale / 2
104
116
self .dist .append ([msg .header .stamp .to_sec (), margin ])
105
117
if margin < 0 :
106
118
if not self .hit_obstacle :
107
119
self .crash += 1
120
+ print ("Crashed" )
108
121
self .hit_obstacle = True
109
122
else :
110
123
self .hit_obstacle = False
111
124
112
125
126
+ def abortRun (self ):
127
+ print ("You did not reach the goal!" )
128
+ summary = {}
129
+ summary ['Success' ] = False
130
+ with open ("summary.yaml" , "w" ) as f :
131
+ if os .getenv ('ROLLOUT_NAME' ) is not None :
132
+ tmp = {}
133
+ tmp [os .getenv ('ROLLOUT_NAME' )] = summary
134
+ yaml .safe_dump (tmp , f )
135
+ else :
136
+ yaml .safe_dump (summary , f )
137
+ rospy .signal_shutdown ("Completed Evaluation" )
138
+
139
+
113
140
def printSummary (self ):
114
141
ttf = self .time_array [- 1 ] - self .time_array [0 ]
142
+ summary = {}
143
+ summary ['Success' ] = True if self .crash == 0 else False
115
144
print ("You reached the goal in %5.3f seconds" % ttf )
145
+ summary ['time_to_finish' ] = ttf
116
146
print ("Your intermediate times are:" )
117
147
print_distance = 10
118
- for i in range (print_distance , self .xmax - print_distance + 1 , print_distance ):
148
+ summary ['segment_times' ] = {}
149
+ for i in range (print_distance , self .xmax + 1 , print_distance ):
119
150
print (" %2i: %5.3fs " % (i ,self .time_array [i ] - self .time_array [0 ]))
151
+ summary ['segment_times' ]["%i" % i ] = self .time_array [i ] - self .time_array [0 ]
120
152
print ("You hit %i obstacles" % self .crash )
153
+ summary ['number_crashes' ] = self .crash
154
+ with open ("summary.yaml" , "w" ) as f :
155
+ if os .getenv ('ROLLOUT_NAME' ) is not None :
156
+ tmp = {}
157
+ tmp [os .getenv ('ROLLOUT_NAME' )] = summary
158
+ yaml .safe_dump (tmp , f )
159
+ else :
160
+ yaml .safe_dump (summary , f )
121
161
122
162
if not self .config ['plots' ]:
123
163
return
@@ -131,11 +171,13 @@ def printSummary(self):
131
171
dt = np .array (self .time_array )
132
172
y = 1 / (dt [1 :]- dt [0 :- 1 ])
133
173
plot (xs = x , ys = y , color = True )
134
-
174
+
135
175
print ("Here is a plot of the distance to the closest obstacles" )
136
176
dist = np .array (self .dist )
137
177
plot (xs = dist [:,0 ]- self .time_array [0 ], ys = dist [:,1 ], color = True )
138
178
179
+ rospy .signal_shutdown ("Completed Evaluation" )
180
+
139
181
140
182
141
183
if __name__ == "__main__" :
0 commit comments