forked from SCKIMOSU/rascar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_dir.py
73 lines (61 loc) · 1.88 KB
/
car_dir.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
#!/usr/bin/env python
import PCA9685 as servo
import time # Import necessary modules
def Map(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def setup(busnum=None):
global leftPWM, rightPWM, homePWM, pwm
leftPWM = 400
homePWM = 450
rightPWM = 500
offset =0
try:
for line in open('config'):
if line[0:8] == 'offset =':
offset = int(line[9:-1])
except:
print 'config error'
leftPWM += offset
homePWM += offset
rightPWM += offset
if busnum == None:
pwm = servo.PWM() # Initialize the servo controller.
else:
pwm = servo.PWM(bus_number=busnum) # Initialize the servo controller.
pwm.frequency = 60
# ==========================================================================================
# Control the servo connected to channel 0 of the servo control board, so as to make the
# car turn left.
# ==========================================================================================
def turn_left():
global leftPWM
pwm.write(0, 0, leftPWM) # CH0
# ==========================================================================================
# Make the car turn right.
# ==========================================================================================
def turn_right():
global rightPWM
pwm.write(0, 0, rightPWM)
# ==========================================================================================
# Make the car turn back.
# ==========================================================================================
def turn(angle):
angle = Map(angle, 0, 255, leftPWM, rightPWM)
pwm.write(0, 0, angle)
def home():
global homePWM
pwm.write(0, 0, homePWM)
def calibrate(x):
pwm.write(0, 0, 450+x)
def test():
while True:
turn_left()
time.sleep(1)
home()
time.sleep(1)
turn_right()
time.sleep(1)
home()
if __name__ == '__main__':
setup()
home()