-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibrate.py
executable file
·133 lines (106 loc) · 4.73 KB
/
calibrate.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
#!/usr/bin/env python3
"""
Scripts to drive a donkey 2 car
Usage:
manage.py (drive)
Options:
-h --help Show this screen.
"""
import os
import time
from docopt import docopt
import donkeycar as dk
#import parts
from donkeycar.parts.controller import LocalWebController, \
JoystickController, WebFpv
from donkeycar.parts.throttle_filter import ThrottleFilter
from donkeycar.parts import pins
from donkeycar.utils import *
from socket import gethostname
def drive(cfg ):
'''
Construct a working robotic vehicle from many parts.
Each part runs as a job in the Vehicle loop, calling either
it's run or run_threaded method depending on the constructor flag `threaded`.
All parts are updated one after another at the framerate given in
cfg.DRIVE_LOOP_HZ assuming each part finishes processing in a timely manner.
Parts may have named outputs and inputs. The framework handles passing named outputs
to parts requesting the same named input.
'''
#Initialize car
V = dk.vehicle.Vehicle()
ctr = LocalWebController(port=cfg.WEB_CONTROL_PORT)
V.add(ctr,
inputs=['cam/image_array', 'tub/num_records'],
outputs=['angle', 'throttle', 'user/mode', 'recording'],
threaded=True)
#this throttle filter will allow one tap back for esc reverse
th_filter = ThrottleFilter()
V.add(th_filter, inputs=['throttle'], outputs=['throttle'])
drive_train = None
#Drive train setup
if cfg.DONKEY_GYM or cfg.DRIVE_TRAIN_TYPE == "MOCK":
pass
elif cfg.DRIVE_TRAIN_TYPE == "PWM_STEERING_THROTTLE":
#
# drivetrain for RC car with servo and ESC.
# using a PwmPin for steering (servo)
# and as second PwmPin for throttle (ESC)
#
from donkeycar.parts.actuator import PWMSteering, PWMThrottle, PulseController
dt = cfg.PWM_STEERING_THROTTLE
steering_controller = PulseController(
pwm_pin=pins.pwm_pin_by_id(dt["PWM_STEERING_PIN"]),
pwm_scale=dt["PWM_STEERING_SCALE"],
pwm_inverted=dt["PWM_STEERING_INVERTED"])
steering = PWMSteering(controller=steering_controller,
left_pulse=dt["STEERING_LEFT_PWM"],
right_pulse=dt["STEERING_RIGHT_PWM"])
throttle_controller = PulseController(
pwm_pin=pins.pwm_pin_by_id(dt["PWM_THROTTLE_PIN"]),
pwm_scale=dt["PWM_THROTTLE_SCALE"],
pwm_inverted=dt['PWM_THROTTLE_INVERTED'])
throttle = PWMThrottle(controller=throttle_controller,
max_pulse=dt['THROTTLE_FORWARD_PWM'],
zero_pulse=dt['THROTTLE_STOPPED_PWM'],
min_pulse=dt['THROTTLE_REVERSE_PWM'])
drive_train = dict()
drive_train['steering'] = steering
drive_train['throttle'] = throttle
V.add(steering, inputs=['angle'], threaded=True)
V.add(throttle, inputs=['throttle'], threaded=True)
elif cfg.DRIVE_TRAIN_TYPE == "I2C_SERVO":
from donkeycar.parts.actuator import PCA9685, PWMSteering, PWMThrottle
steering_controller = PCA9685(cfg.STEERING_CHANNEL,
cfg.PCA9685_I2C_ADDR,
busnum=cfg.PCA9685_I2C_BUSNUM)
steering = PWMSteering(controller=steering_controller,
left_pulse=cfg.STEERING_LEFT_PWM,
right_pulse=cfg.STEERING_RIGHT_PWM)
throttle_controller = PCA9685(cfg.THROTTLE_CHANNEL,
cfg.PCA9685_I2C_ADDR,
busnum=cfg.PCA9685_I2C_BUSNUM)
throttle = PWMThrottle(controller=throttle_controller,
max_pulse=cfg.THROTTLE_FORWARD_PWM,
zero_pulse=cfg.THROTTLE_STOPPED_PWM,
min_pulse=cfg.THROTTLE_REVERSE_PWM)
drive_train = dict()
drive_train['steering'] = steering
drive_train['throttle'] = throttle
V.add(steering, inputs=['angle'], threaded=True)
V.add(throttle, inputs=['throttle'], threaded=True)
elif cfg.DRIVE_TRAIN_TYPE == "MM1":
from donkeycar.parts.robohat import RoboHATDriver
drive_train = RoboHATDriver(cfg)
V.add(drive_train, inputs=['angle', 'throttle'])
# TODO: monkeypatching is bad!!!
ctr.drive_train = drive_train
ctr.drive_train_type = cfg.DRIVE_TRAIN_TYPE
print(f"Go to http://{gethostname()}.local:{ctr.port}/calibrate to "
f"calibrate ")
V.start(rate_hz=cfg.DRIVE_LOOP_HZ, max_loop_count=cfg.MAX_LOOPS)
if __name__ == '__main__':
args = docopt(__doc__)
cfg = dk.load_config()
if args['drive']:
drive(cfg)