-
Notifications
You must be signed in to change notification settings - Fork 0
/
okon_actions.py
280 lines (223 loc) · 11.6 KB
/
okon_actions.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Module with implementation of OKON actions using py_trees"""
import math
import sys
import time
import py_trees
from okon_sim_client import Okon
class Status:
SUCCESS = py_trees.common.Status.SUCCESS
FAILURE = py_trees.common.Status.FAILURE
RUNNING = py_trees.common.Status.RUNNING
INVALID = py_trees.common.Status.INVALID
class SetDepth(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "set depth", okon: Okon = None, depth: float = 0.6, delta: float = 0.05):
super().__init__(name)
self.okon = okon
self.depth = depth
self.delta = delta
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
self.okon.set_depth(self.depth)
new_status = Status.SUCCESS if self.okon.reachedTargetDepth(self.delta) else Status.RUNNING
if new_status == Status.SUCCESS:
self.feedback_message = "Target depth of {self.depth} m reached."
self.feedback_message = (
f"Current depth {self.okon.sens['baro'] / 1000 / 9.81:.3f}. Waiting for target depth of {self.depth} m."
)
self.logger.debug(f"{self.__class__.__name__}.update()[{self.status}->{new_status}][{self.feedback_message}]")
return new_status
def update_depth(self, new_depth):
self.depth = new_depth
def terminate(self, new_status):
"""Nothing to clean up in this example."""
self.logger.debug(f"{self.__class__.__name__}.terminate()[{self.status}->{new_status}]")
class SetVelocity(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "set velocity", okon: Okon = None, x: float = 0.0, y: float = 0.0, z: float = 0.0):
super().__init__(name)
self.okon = okon
self.x = x
self.y = y
self.z = z
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
self.okon.set_stable_vel(x=self.x, y=self.y, z=self.z)
new_status = Status.SUCCESS
if new_status == Status.SUCCESS:
self.feedback_message = f"Speed set as: Vx = {self.x:.3f} Vy = {self.y:.3f} Vz = {self.z:.3f}."
self.logger.debug(f"{self.__class__.__name__}.update()[{self.status}->{new_status}][{self.feedback_message}]")
return new_status
def terminate(self, new_status):
"""Nothing to clean up in this example."""
self.logger.debug(f"{self.__class__.__name__}.terminate()[{self.status}->{new_status}]")
class Rotate(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "rotate", okon: Okon = None, add_angle: float = 45.0, delta: float = 1.0):
super().__init__(name)
self.okon = okon
self.add_angle = add_angle
self.target_angle = self.okon.sens["imu"]["rot"]["y"] + self.add_angle
self.delta = delta
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.target_angle = self.okon.sens["imu"]["rot"]["y"] + self.add_angle
if self.target_angle < 0.0:
self.target_angle += 360.0
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
self.okon.set_stable_rot(y=self.target_angle)
new_status = Status.SUCCESS if self.okon.reachedTargetRotation(self.delta) else Status.RUNNING
if new_status == Status.SUCCESS:
self.feedback_message = f"Target rotation of {self.target_angle} degrees reached."
else:
self.feedback_message = f"Current rotation is {self.okon.sens['imu']['rot']['y']:.3f} degrees. Waiting for target rotation of {self.target_angle:.3f} degrees."
self.logger.debug(f"{self.__class__.__name__}.update()[{self.status}->{new_status}][{self.feedback_message}]")
return new_status
def update_add_angle(self, new_add_angle):
self.add_angle = new_add_angle
def terminate(self, new_status):
"""Nothing to clean up in this example."""
self.logger.debug(f"{self.__class__.__name__}.terminate()[{self.status}->{new_status}]")
class RotateDeltaYawAngle(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "rotate delta yaw angle", okon: Okon = None, delta: float = 1.0):
super().__init__(name)
self.okon = okon
self.delta = delta
self.blackboard = self.attach_blackboard_client()
self.blackboard.register_key(key="deltaYaw", access=py_trees.common.Access.READ)
self.target_angle = self.okon.sens["imu"]["rot"]["y"]
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.target_angle = self.okon.sens["imu"]["rot"]["y"] + self.blackboard.deltaYaw
if self.target_angle < 0.0:
self.target_angle += 360.0
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
self.okon.set_stable_rot(y=self.target_angle)
new_status = Status.SUCCESS if self.okon.reachedTargetRotation(self.delta) else Status.RUNNING
if new_status == Status.SUCCESS:
self.feedback_message = f"Target rotation of {self.target_angle} degrees reached."
else:
self.feedback_message = f"Current rotation is {self.okon.sens['imu']['rot']['y']:.3f} degrees. Waiting for target rotation of {self.target_angle:.3f} degrees."
self.logger.debug(f"{self.__class__.__name__}.update()[{self.status}->{new_status}][{self.feedback_message}]")
return new_status
def terminate(self, new_status):
"""Nothing to clean up in this example."""
self.logger.debug(f"{self.__class__.__name__}.terminate()[{self.status}->{new_status}]")
class TryDetectNTimes(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "try detect n times", okon: Okon = None, object: str = "gate", n: int = 3):
super().__init__(name)
self.okon = okon
self.object = object
self.n = n
self.counter = 1
self.blackboard = self.attach_blackboard_client()
self.blackboard.register_key(key="detection", access=py_trees.common.Access.WRITE)
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.counter = 1
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
detection = self.okon.get_detection(self.object)
if len(detection) > 0:
new_status = Status.SUCCESS
self.blackboard.detection = detection
elif self.counter == self.n:
new_status = Status.FAILURE
else:
new_status = Status.RUNNING
if new_status == Status.SUCCESS:
self.feedback_message = f"Object {self.object} detected in attempt number {self.counter}"
else:
self.feedback_message = f"Object {self.object} undetected in attempt number {self.counter}"
self.counter += 1
self.logger.debug(f"{self.__class__.__name__}.update()[{self.status}->{new_status}][{self.feedback_message}]")
return new_status
def terminate(self, new_status):
"""Nothing to clean up in this example."""
self.logger.debug(f"{self.__class__.__name__}.terminate()[{self.status}->{new_status}]")
class CalculateDeltaYaw(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "calculate delta yaw", okon: Okon = None):
super().__init__(name)
self.okon = okon
self.blackboard = self.attach_blackboard_client()
self.blackboard.register_key(key="detection", access=py_trees.common.Access.READ)
self.blackboard.register_key(key="deltaYaw", access=py_trees.common.Access.WRITE)
self.delta_yaw = 0
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
detection = self.blackboard.detection
if len(detection) > 0:
new_status = Status.SUCCESS
hfov = 60
gate = detection[0]
if gate["distance"] < 1:
self.delta_yaw = 0
else:
center = (gate["max"]["x"] + gate["min"]["x"]) / 2 * 2 - 1
camera_plane_x = 1.0 / math.tan(hfov / 2 / 180 * math.pi)
self.delta_yaw = math.atan(center / camera_plane_x) / math.pi * 180
self.blackboard.deltaYaw = self.delta_yaw
else:
new_status = Status.FAILURE
if new_status == Status.SUCCESS:
self.feedback_message = f"Delta Yaw was calculated and is equal to {self.delta_yaw}"
else:
self.feedback_message = "There were no objects in the detection parameter in blackboard."
self.logger.debug(f"{self.__class__.__name__}.update()[{self.status}->{new_status}][{self.feedback_message}]")
return new_status
def terminate(self, new_status):
"""Nothing to clean up in this example."""
self.logger.debug(f"{self.__class__.__name__}.terminate()[{self.status}->{new_status}]")
class IsGateFarEnough(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "calculate delta yaw", okon: Okon = None, max_distance: float = 1.5):
super().__init__(name)
self.okon = okon
self.max_distance = max_distance
self.blackboard = self.attach_blackboard_client()
self.blackboard.register_key(key="detection", access=py_trees.common.Access.READ)
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
gate = self.blackboard.detection[0]
new_status = Status.SUCCESS if gate["distance"] > self.max_distance else Status.FAILURE
if new_status == Status.SUCCESS:
self.feedback_message = f"Gate is in distance of {gate['distance']:.3f} m."
else:
self.feedback_message = f"Gate is closer than max distance set to {self.max_distance:.3f}m."
self.logger.debug(f"{self.__class__.__name__}.update()[{self.status}->{new_status}][{self.feedback_message}]")
return new_status
def terminate(self, new_status):
"""Nothing to clean up in this example."""
self.logger.debug(f"{self.__class__.__name__}.terminate()[{self.status}->{new_status}]")
class Wait(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "wait", okon: Okon = None, secs: float = 0.0):
super().__init__(name)
self.okon = okon
self.secs = secs
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
time.sleep(self.secs)
new_status = Status.SUCCESS
if new_status == Status.SUCCESS:
self.feedback_message = f"Robot waited for {self.secs} seconds."
self.logger.debug(f"{self.__class__.__name__}.update()[{self.status}->{new_status}][{self.feedback_message}]")
return new_status
def terminate(self, new_status):
"""Nothing to clean up in this example."""
self.logger.debug(f"{self.__class__.__name__}.terminate()[{self.status}->{new_status}]")
class Exit(py_trees.behaviour.Behaviour):
def __init__(self, name: str = "exit"):
super().__init__(name)
self.logger.debug(f"{self.__class__.__name__}.__init__()")
def initialise(self):
self.logger.debug(f"{self.__class__.__name__}.initialise()")
def update(self):
sys.exit()