-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.py
66 lines (51 loc) · 1.74 KB
/
drive.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
import os
import sys
import numpy as np
import util
class Servo:
def __init__(self, pins):
self.pins = pins
self.min = 1000
self.mid = 1500
self.max = 2000
os.system('sudo servod --p1pins="{}"'.format(str(self.pins)[1:-1]))
def write(self, servo_num, us):
''' Write to a single servo in MicroSeconds mode. '''
us = np.clip(us, self.min, self.max)
os.system("echo {}={}us > /dev/servoblaster".format(servo_num, us))
def multi_write(self, vals):
''' Write to a all the servo in MicroSeconds mode. '''
for servo_num, us in enumerate(np.clip(vals, self.min, self.max)):
os.system("echo {}={}us > /dev/servoblaster".format(servo_num, us))
def center_all(self):
''' Writes all the servos to the midpoint value '''
self.multi_write(np.ones_like(self.pins) * self.mid)
def close(self):
os.system("sudo killall servod")
class Driver:
def __init__(self, m1_pin, m2_pin):
self.servo = Servo([m1_pin, m2_pin])
self.m = np.zeros(2)
self.motor_dirs = np.array([-1.,1.])
def set_servos(self):
self.servo.multi_write(self.m * self.motor_dirs * 500. + 1500.)
def move(self, speed, turn):
''' Move forward at `speed`, and rotate `turn`'''
self.m = np.clip(np.array([-turn, turn]) + speed, -1., 1.)
self.set_servos()
def dmotor(self, d):
''' Delta the motor speeds by the specified amounts '''
self.m = np.clip(self.m + d, -1., 1.)
self.set_servos()
def act(self, action):
self.dmotor(util.action_to_motor(action))
def stop(self):
self.move(0,0)
def close(self):
self.stop()
self.servo.close()
if __name__ == '__main__':
if len(sys.argv) < 3:
print('python -i drive.py <m1Pin> <m2Pin>')
sys.exit(1)
d = Driver(int(sys.argv[1]), int(sys.argv[2]))