This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtesting_cli.py
216 lines (161 loc) · 5.95 KB
/
testing_cli.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
"""Provides a continuous command line prompt for testing drone capabilities."""
import argparse
import threading
from flight.drone.drone_controller import DroneController
from flight import constants as c
PROMPT_FOR_COMMAND = '> '
def main():
parser = create_parser()
args = parser.parse_args()
# Make the controller object
controller = DroneController(is_simulation=args.sim)
# Make a thread whose target is a command line interface
input_thread = threading.Thread(
target=input_loop, args=(controller,))
input_thread.daemon = True
input_thread.start()
controller.run()
def create_parser():
"""Returns a configured argument parser."""
parser = argparse.ArgumentParser(description='Test flight commands.')
parser.add_argument('--sim',
dest='sim',
action='store_true',
default=False,
help='run simulator compatible flight code')
return parser
class ExitRequested(Exception):
"""Raised when the input loop should stop."""
pass
class Command(object):
"""A command line argument that can be translated into a drone command.
Attributes
----------
_controller : DroneController
Interface to adding a command to the drone.
"""
def __init__(self, controller):
"""Initialize the given command.
Notes
-----
Subclasses must assign a list to self._expected_order or else this
class will break. The list contains the types (in expected order) that
the given command takes.
"""
self._controller = controller
def matches(self, *args):
"""Validates the command line arguments.
Notes
-----
This function must be called before calling __call__, because it
populates the _parameters.
"""
if len(args) != len(self._expected_order):
raise TypeError('Expected {} arguments, got {}.'.format(
len(self._expected_order), len(args)))
for param, cast in zip(args, self._expected_order):
self._parameters.append(cast(param))
def __call__(self, *args):
"""Adds the command as a task on the drone controller."""
# implement command here:
pass
class HoverCommand(Command):
def __init__(self, controller):
super(HoverCommand, self).__init__(controller)
self._expected_order = [float, float, get_priority]
self._parameters = []
def __call__(self, *args):
self._controller.add_hover_task(*self._parameters)
class MoveCommand(Command):
def __init__(self, controller):
super(MoveCommand, self).__init__(controller)
self._expected_order = [get_direction, float, get_priority]
self._parameters = []
def __call__(self, *args):
self._controller.add_linear_movement_task(*self._parameters)
class TakeoffCommand(Command):
def __init__(self, controller):
super(TakeoffCommand, self).__init__(controller)
self._expected_order = [float]
self._parameters = []
def __call__(self, *args):
self._controller.add_takeoff_task(*self._parameters)
class LandCommand(Command):
def __init__(self, controller):
super(LandCommand, self).__init__(controller)
self._expected_order = [get_priority]
self._parameters = []
def __call__(self, *args):
self._controller.add_land_task(*self._parameters)
class ExitCommand(Command):
def __init__(self, controller):
super(ExitCommand, self).__init__(controller)
self._expected_order = []
def __call__(self, *args):
self._controller.add_exit_task(c.Priorities.HIGH)
raise ExitRequested
class YawCommand(Command):
def __init__(self, controller):
super(YawCommand, self).__init__(controller)
self._expected_order = [float, get_priority]
self._parameters = []
def __call__(self, *args):
self._controller.add_yaw_task(*self._parameters)
NAME_TO_COMMAND = {
'hover': HoverCommand,
'move': MoveCommand,
'takeoff': TakeoffCommand,
'land': LandCommand,
'exit': ExitCommand,
'yaw' : YawCommand
}
def input_loop(controller):
"""Simple command line interface to drone controller.
exit: lands and terminates program
land: land <priority>
hover: hover <duration> <priority>
takeoff: takeoff <altitude>
move: move <FOWARD,BACKWARD,LEFT,RIGHT> <duration> <priority>
Notes
-----
Priority is one of "high", "med", or "low".
"""
while True:
try:
args = raw_input(PROMPT_FOR_COMMAND).lower().split()
if not len(args):
continue
command = args[0]
if command in NAME_TO_COMMAND:
command_instance = NAME_TO_COMMAND[command](controller)
command_instance.matches(*args[1:])
command_instance()
else:
raise ValueError('Unknown command: {}'.format(command))
except Exception as e:
if isinstance(e, ExitRequested):
print('Closing session.')
return
print('{}{}: {}'.format(PROMPT_FOR_COMMAND, type(e).__name__, e))
def get_priority(priority):
"""Gets priority level from a string."""
key = priority.upper()
if hasattr(c.Priorities, key):
converted_form = getattr(c.Priorities, key)
else:
raise TypeError(
'Expected value for type {}, got {}.'.format(
type(c.Priorities).__name__, priority))
return converted_form
def get_direction(direction):
"""Gets direction from a string."""
key = direction.upper()
if hasattr(c.Directions, key):
converted_form = getattr(c.Directions, key)
else:
raise TypeError(
'Expected value for type {}, got {}.'.format(
type(c.Directions).__name__, direction))
return converted_form
if __name__ == '__main__':
main()