-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPiCar.py
74 lines (61 loc) · 2.06 KB
/
RPiCar.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
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
import xbox
import time
import atexit
# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT(addr=0x6f)
FrontLeft = mh.getMotor(1)
FrontRight = mh.getMotor(2)
RearLeft = mh.getMotor(3)
RearRight = mh.getMotor(4)
joy = xbox.Joystick()
Wheels = [FrontLeft,FrontRight,RearLeft,RearRight]
turnThreshold = 100 #Turn Sensitivity
GasThreshold = 50 #Gas Sensitivity
# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)
def stick2speed(y):
return int(255*y)
def moveMotor(speeds,turn):
i = 0
for w in Wheels:
spd = speeds[i]
# print spd
if spd < 0:
direction = Adafruit_MotorHAT.BACKWARD
else:
direction = Adafruit_MotorHAT.FORWARD
if turn == True:
direction = Adafruit_MotorHAT.FORWARD
w.run(direction)
w.setSpeed(abs(spd))
i=i+1
atexit.register(turnOffMotors)
# # set the speed to start, from 0 (off) to 255 (max speed)
# myMotor.setSpeed(150)
# myMotor.run(Adafruit_MotorHAT.FORWARD);
# # turn on motor
# myMotor.run(Adafruit_MotorHAT.RELEASE);
while not joy.Back():
lx, ly = joy.leftStick()
rx, ry = joy.rightStick()
speed = stick2speed(ly)
turn = stick2speed(rx)
# print "Left ", speed, "| Right ", turn, "\r"
if speed < GasThreshold and turn < turnThreshold: #if sticks are not moving , shut off motors
turnOffMotors()
if (abs(speed) > GasThreshold) and (turn < turnThreshold):
# print "Left ", speed, "| Right ", turn ,"\r"
moveMotor([speed,speed,speed,speed], False)
if abs(turn) > turnThreshold:
# print "Left ", speed, "| Right ", turn, "\r"
if turn > 0:
moveMotor([turn,0,turn,0],True)
else:
moveMotor([0,turn,0,turn],True)
time.sleep(0.1)
joy.close()