-
Notifications
You must be signed in to change notification settings - Fork 51
/
wheels.py
109 lines (80 loc) · 2.01 KB
/
wheels.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
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
import os
import time
import atexit
# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT(addr=0x60)
# 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)
atexit.register(turnOffMotors)
################################# DC motor test!
mFL = mh.getMotor(1)
mBL = mh.getMotor(2)
mBR = mh.getMotor(3)
mFR = mh.getMotor(4)
def wakeup(m):
# set the speed to start, from 0 (off) to 255 (max speed)
m.setSpeed(150)
m.run(Adafruit_MotorHAT.FORWARD);
# turn on motor
m.run(Adafruit_MotorHAT.RELEASE);
wakeup(mFL)
wakeup(mBL)
wakeup(mFR)
wakeup(mBL)
def spin(wheel, speed):
if (speed > 0):
wheel.run(Adafruit_MotorHAT.FORWARD)
wheel.setSpeed(speed)
elif (speed < 0):
wheel.run(Adafruit_MotorHAT.BACKWARD)
wheel.setSpeed(-speed)
else:
wheel.run(Adafruit_MotorHAT.RELEASE)
def spinMotor(motorId=1, speed=200, dur=-1):
m = mh.getMotor(motorId)
spin(m, speed)
if (dur >= 0):
time.sleep(dur)
stop()
def stop():
mFL.run(Adafruit_MotorHAT.RELEASE)
mFR.run(Adafruit_MotorHAT.RELEASE)
mBL.run(Adafruit_MotorHAT.RELEASE)
mBR.run(Adafruit_MotorHAT.RELEASE)
def forward(speed=200, dur=-1):
spin(mFR, speed)
spin(mFL, speed)
spin(mBR, speed)
spin(mBL, speed)
if (dur >= 0):
time.sleep(dur)
stop()
def backward(speed, dur=-1):
spin(mFR, -speed)
spin(mFL, -speed)
spin(mBR, -speed)
spin(mBL, -speed)
if (dur >- 0):
time.sleep(dur)
stop()
def left(speed, dur=-1):
spin(mFR, -speed)
spin(mFL, speed)
spin(mBR, -speed)
spin(mBL, speed)
if (dur >- 0):
time.sleep(dur)
stop()
def right(speed, dur=-1):
spin(mFR, speed)
spin(mFL, -speed)
spin(mBR, speed)
spin(mBL, -speed)
if (dur >- 0):
time.sleep(dur)
stop()