-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.py
195 lines (149 loc) · 5.81 KB
/
manage.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
#!/usr/bin/env python3
"""
Scripts to drive a donkey 2 car and train a model for it.
Usage:
manage.py (drive) [--model=<model>] [--js] [--chaos]
manage.py (train) [--tub=<tub1,tub2,..tubn>] (--model=<model>) [--base_model=<base_model>] [--no_cache]
Options:
-h --help Show this screen.
--tub TUBPATHS List of paths to tubs. Comma separated. Use quotes to use wildcards. ie "~/tubs/*"
--chaos Add periodic random steering when manually driving
"""
import os
from docopt import docopt
import donkeycar as dk
from donkeypart_picamera import PiCamera
from donkeypart_keras_behavior_cloning import KerasLinear
from donkeypart_PCA9685_actuators import PCA9685, PWMSteering, PWMThrottle
from donkeypart_tub import TubGroup, TubWriter
from donkeypart_web_controller import LocalWebController
from donkeypart_common import Timestamp
from donkeypart_bluetooth_game_controller import BluetoothGameController
from donkeypart_sombrero import Sombrero
def drive(cfg, model_path=None, use_chaos=False):
"""
"""
V = dk.vehicle.Vehicle()
clock = Timestamp()
V.add(clock, outputs=['timestamp'])
cam = PiCamera(resolution=cfg.CAMERA_RESOLUTION)
V.add(cam, outputs=['cam/image_array'], threaded=True)
"""
ctr = LocalWebController(use_chaos=use_chaos)
V.add(ctr,
inputs=['cam/image_array'],
outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'],
threaded=True)
"""
# then replace your current controller with...
ctl = BluetoothGameController()
V.add(ctl,
inputs=['cam/image_array'],
outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'],
threaded=True)
# See if we should even run the pilot module.
# This is only needed because the part run_condition only accepts boolean
pilot_condition_part = MakeRunConditionBoolean()
V.add(pilot_condition_part,
inputs=['user/mode'],
outputs=['run_pilot'])
# Run the pilot if the mode is not user.
kl = KerasLinear()
if model_path:
kl.load(model_path)
V.add(kl,
inputs=['cam/image_array'],
outputs=['pilot/angle', 'pilot/throttle'],
run_condition='run_pilot')
state_controller = StateController()
V.add(state_controller,
inputs=['user/mode',
'user/angle', 'user/throttle',
'pilot/angle', 'pilot/throttle'],
outputs=['angle', 'throttle'])
sombrero = Sombrero(
steering_channel=cfg.STEERING_CHANNEL,
steering_left_pwm=cfg.STEERING_LEFT_PWM,
steering_right_pwm=cfg.STEERING_RIGHT_PWM,
throttle_channel=cfg.THROTTLE_CHANNEL,
throttle_forward_pwm=cfg.THROTTLE_FORWARD_PWM,
throttle_stop_pwm=cfg.THROTTLE_STOPPED_PWM,
throttle_reverse_pwm=cfg.THROTTLE_REVERSE_PWM
)
V.add(sombrero, inputs=['angle', 'throttle'])
# add tub to save data
inputs = ['cam/image_array', 'user/angle', 'user/throttle', 'user/mode', 'timestamp']
types = ['image_array', 'float', 'float', 'str', 'str']
# single tub
tub = TubWriter(path=cfg.TUB_PATH, inputs=inputs, types=types)
V.add(tub, inputs=inputs, run_condition='recording')
# run the vehicle
V.start(rate_hz=cfg.DRIVE_LOOP_HZ,
max_loop_count=cfg.MAX_LOOPS)
def train(cfg, tub_names, new_model_path, base_model_path=None):
"""
use the specified data in tub_names to train an artifical neural network
saves the output trained model as model_name
"""
X_keys = ['cam/image_array']
y_keys = ['user/angle', 'user/throttle']
new_model_path = os.path.expanduser(new_model_path)
kl = KerasLinear()
if base_model_path is not None:
base_model_path = os.path.expanduser(base_model_path)
kl.load(base_model_path)
print('tub_names', tub_names)
if not tub_names:
tub_names = os.path.join(cfg.DATA_PATH, '*')
tubgroup = TubGroup(tub_names)
train_gen, val_gen = tubgroup.get_train_val_gen(X_keys, y_keys,
batch_size=cfg.BATCH_SIZE,
train_frac=cfg.TRAIN_TEST_SPLIT)
total_records = len(tubgroup.df)
total_train = int(total_records * cfg.TRAIN_TEST_SPLIT)
total_val = total_records - total_train
print('train: %d, validation: %d' % (total_train, total_val))
steps_per_epoch = total_train // cfg.BATCH_SIZE
print('steps_per_epoch', steps_per_epoch)
kl.train(train_gen,
val_gen,
saved_model_path=new_model_path,
steps=steps_per_epoch,
train_split=cfg.TRAIN_TEST_SPLIT)
class MakeRunConditionBoolean:
def run(self, mode):
if mode == 'user':
return False
else:
return True
class StateController:
"""
Wraps a function into a donkey part.
"""
def __init__(self):
pass
def run(self, mode,
user_angle, user_throttle,
pilot_angle, pilot_throttle):
"""
Returns the angle, throttle and boolean if autopilot should be run.
The angle and throttles returned are the ones given to the steering and
throttle actuators.
"""
if mode == 'user':
return user_angle, user_throttle
elif mode == 'local_angle':
return pilot_angle, user_throttle
else:
return pilot_angle, pilot_throttle
if __name__ == '__main__':
args = docopt(__doc__)
cfg = dk.load_config()
if args['drive']:
drive(cfg, model_path=args['--model'], use_chaos=args['--chaos'])
elif args['train']:
tub = args['--tub']
new_model_path = args['--model']
base_model_path = args['--base_model']
cache = not args['--no_cache']
train(cfg, tub, new_model_path, base_model_path)