-
Notifications
You must be signed in to change notification settings - Fork 0
/
station-keeping-behaviour
executable file
·69 lines (55 loc) · 2.51 KB
/
station-keeping-behaviour
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
#!/usr/bin/env python
import time
import boatdclient
from boatdclient import Point, Bearing
from navigate import Navigator
points = boatdclient.get_current_waypoints()
minutes_in_box = 5
class StationKeepingBehaviour(Navigator):
def __init__(self):
super(StationKeepingBehaviour, self).__init__(enable_tacking=False)
self.center = (points[0] + points[1] + points[2] + points[3]) / 4
self.set_target(self.center)
self.edge_bearing_to_center = None
self.center_reached = False
self.line_crossed = False
self.enter_time = None
self.time_to_center = None
sorted_points = []
for point in points:
sorted_points.append((self.boat.position.distance_to(point), point))
self.nearest_line_points = sorted(sorted_points, key=lambda x: x[0])
self.nearest_line_points = [n[1] for n in self.nearest_line_points][0:2]
if self.boat.position.cross_track_distance(self.nearest_line_points[1], self.nearest_line_points[0]) < 0:
self.start_negative = True
else:
self.start_negative = False
def check_new_target(self):
# now inside box
if self.boat.position.distance_to(self.center) < 5: #account for this in timing?
if self.center_reached is False: # first time center reached
self.time_to_center = time.time() - self.enter_time
self.center_reached = True
# still outside box
if self.center_reached is False:
distance_to_edge = self.boat.position.cross_track_distance(\
self.nearest_line_points[1], self.nearest_line_points[0])
# if now in box
if (distance_to_edge <= 0 and self.start_negative is False)\
or (distance_to_edge >= 0 and self.start_negative is True):
print 'YOU HAVE CROSSED THE LINE ' * 20
self.line_crossed = True
self.enter_time = time.time()
self.bearing_edge_to_center = self.boat.position.bearing_to(self.center)
return None
# while in box
elif self.center_reached is True:
# wait for time
if time.time() < self.enter_time + (minutes_in_box * 60) - self.time_to_center:
return None
# leave box
else:
return self.bearing_edge_to_center
if __name__ == '__main__':
behaviour = StationKeepingBehaviour()
behaviour.run()