-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMode.py
147 lines (129 loc) · 5.33 KB
/
Mode.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
import threading
import time
import Config
import DirectionBoatRotation
import DirectionNone
import DirectionTruckRandom
import DirectionTruckRotation
import Event
import HeightHeli
import HeightNone
import Input
import ResetNonConst
import ResetOutOfBound
import ResetSlowSpeed
import SaveAvoid
import SaveNone
import SpeedBoatTarget
import SpeedBoatThrottle
import SpeedNone
import SpeedTruckMax
import SpeedTruckRandom
import SpeedTruckTarget
import SpeedTruckThrottle
import TargetAvoid
import TargetNone
import TargetRandomAngle
import TargetRandomWaypoint
import TargetWaypoint
import TargetWaypointReverse
class Mode:
def __init__(self):
self.target = []
if 'target' in Config.config_json:
all_target = Config.config_json['target'].split(',')
for t in all_target:
if t == 'waypoint':
self.target.append(TargetWaypoint.TargetWaypoint())
elif t == 'waypoint_reverse':
self.target.append(TargetWaypointReverse.TargetWaypointReverse())
elif t == 'avoid':
self.target.append(TargetAvoid.TargetAvoid())
elif t == 'random_waypoint':
self.target.append(TargetRandomWaypoint.TargetRandomWaypoint())
elif t == 'random_angle':
self.target.append(TargetRandomAngle.TargetRandomAngle())
else:
self.target = [TargetNone.TargetNone()]
self.direction = DirectionNone.DirectionNone()
if 'direction' in Config.config_json:
if Config.config_json['direction'] == 'truck_rotation':
self.direction = DirectionTruckRotation.DirectionTruckRotation()
elif Config.config_json['direction'] == 'truck_random':
self.direction = DirectionTruckRandom.DirectionTruckRandom()
elif Config.config_json['direction'] == 'boat_rotation':
self.direction = DirectionBoatRotation.DirectionBoatRotation()
self.speed = SpeedNone.SpeedNone()
if 'speed' in Config.config_json:
if Config.config_json['speed'] == 'truck_random':
self.speed = SpeedTruckRandom.SpeedTruckRandom()
elif Config.config_json['speed'] == 'truck_target':
self.speed = SpeedTruckTarget.SpeedTruckTarget()
elif Config.config_json['speed'] == 'truck_max':
self.speed = SpeedTruckMax.SpeedTruckMax()
elif Config.config_json['speed'] == 'truck_throttle':
self.speed = SpeedTruckThrottle.SpeedTruckThrottle()
elif Config.config_json['speed'] == 'boat_target':
self.speed = SpeedBoatTarget.SpeedBoatTarget()
elif Config.config_json['speed'] == 'boat_throttle':
self.speed = SpeedBoatThrottle.SpeedBoatThrottle()
self.height = HeightNone.HeightNone()
if 'height' in Config.config_json:
if Config.config_json['height'] == 'heli':
self.height = HeightHeli.HeightHeli()
self.reset = []
if 'reset' in Config.config_json:
all_reset = Config.config_json['reset'].split(',')
for r in all_reset:
if r == 'slow':
self.reset.append(ResetSlowSpeed.ResetSlowSpeed())
if r == 'non_const':
self.reset.append(ResetNonConst.ResetNonConst())
if r == 'out_of_bound':
self.reset.append(ResetOutOfBound.ResetOutOfBound())
self.save = SaveNone.SaveNone()
if 'save' in Config.config_json:
if Config.config_json['save'] == 'avoid':
self.save = SaveAvoid.SaveAvoid()
def run(self):
while True:
Event.wait()
if Input.is_reset_forced() is False:
position = Input.get_position()
rotation = Input.get_rotation()
speed_ms = Input.get_speed_norm()
rot_diff = 0.0
target_speed_ms = 0.0
go_up = False
for t in self.target:
rot_diff, target_speed_ms, go_up = t.run(position, rotation, speed_ms, rot_diff, target_speed_ms, go_up)
self.direction.run(rot_diff)
self.speed.run(speed_ms, target_speed_ms)
self.height.run(go_up)
is_reset = False
reset_position = None
for r in self.reset:
is_reset, reset_position = r.run(position, speed_ms)
if is_reset is True:
break
if is_reset is True:
for t in self.target:
t.reset()
self.direction.reset()
self.speed.reset()
self.height.reset()
for r in self.reset:
r.reset()
self.save.reset(reset_position)
Input.reset()
else: # force reset
for t in self.target:
t.reset()
self.direction.reset()
self.speed.reset()
self.height.reset()
for r in self.reset:
r.reset()
# no save on forced reset
#self.save.reset(reset_position)
Input.reset()