forked from robotstreamer/robotstreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tank_interface.py
199 lines (169 loc) · 5.51 KB
/
tank_interface.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
import robot_util
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
from Adafruit_MotorHAT.Adafruit_PWM_Servo_Driver import PWM
import time
import atexit
mh = Adafruit_MotorHAT(addr=0x6F)
pwm = PWM(0x6F)
#These are the on times that get sent to the pwm module to tell the servo how
#far to rotate. Duty cycle is onTime/4095. Set these to limit the range of
#motion to something sensible. Be sure the pan tilt doesn't bottom out or you
#can damage the servo.
panMinOnTime=125
panMaxOnTime=625
tiltMinOnTime=125
tiltMaxOnTime=575
#global variables to keep track of current percentage of tilt and pan
panPercentage=50.0
tiltPercentage=50.0
#Sets how big of a step each button press gives in percentage.
tiltIncrement=5
panIncrement=10.0/3.0
#Sets the duty cycle for the motors while moving. speed/255 is the duty cycle.
straightSpeed=255
turnSpeed=255
#Sets how long the motors turn on in seconds for movements.
straightDelay=0.4
turnDelay=0.1
movementSystemActive=False
def setTilt(percentage):
onTime=int((tiltMaxOnTime-tiltMinOnTime)*(percentage/100.0)+tiltMinOnTime)
if onTime > tiltMaxOnTime:
onTime=tiltMaxOnTime
elif onTime < tiltMinOnTime:
onTime=tiltMinOnTime
print("setTilt(",percentage,")")
print("ontime=", onTime)
pwm.setPWM(14, 0, onTime)
def setPan(percentage):
onTime=int((panMaxOnTime-panMinOnTime)*(percentage/100.0)+panMinOnTime)
if onTime > panMaxOnTime:
onTime=panMaxOnTime
elif onTime < panMinOnTime:
onTime=panMinOnTime
print("setPan(",percentage,")")
print("ontime=", onTime)
pwm.setPWM(15, 0, onTime)
def turnRight():
leftMotor.setSpeed(turnSpeed)
rightMotor.setSpeed(turnSpeed)
leftMotor.run(Adafruit_MotorHAT.BACKWARD)
rightMotor.run(Adafruit_MotorHAT.BACKWARD)
def turnLeft():
leftMotor.setSpeed(turnSpeed)
rightMotor.setSpeed(turnSpeed)
leftMotor.run(Adafruit_MotorHAT.FORWARD)
rightMotor.run(Adafruit_MotorHAT.FORWARD)
def goForward():
leftMotor.setSpeed(straightSpeed)
rightMotor.setSpeed(straightSpeed)
leftMotor.run(Adafruit_MotorHAT.BACKWARD)
rightMotor.run(Adafruit_MotorHAT.FORWARD)
def goBackward():
leftMotor.setSpeed(straightSpeed)
rightMotor.setSpeed(straightSpeed)
leftMotor.run(Adafruit_MotorHAT.FORWARD)
rightMotor.run(Adafruit_MotorHAT.BACKWARD)
#Turns off motors and the PWM
def motorhatShutdown():
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)
pwm.setPWM(14, 0, 0)
pwm.setPWM(15, 0, 0)
#Turns off only the motors
def releaseMotors():
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 init():
global leftMotor
global rightMotor
global panPercentage
global tiltPercentage
atexit.register(motorhatShutdown)
leftMotor = mh.getMotor(1)
rightMotor = mh.getMotor(2)
pwm.setPWMFreq(60)
setPan(50.0)
setTilt(50.0)
panPercentage=50.0
tiltPercentage=50.0
def handleCommand(command, keyPosition, price=0):
global movementSystemActive
global tiltPercentage
global panPercentage
print("\n\n")
if keyPosition != "down":
return
robot_util.handleSoundCommand(command, keyPosition, price)
if command == 'F':
if movementSystemActive:
print("skip")
else:
print("onforward")
movementSystemActive=True
goForward()
time.sleep(straightDelay)
releaseMotors()
movementSystemActive=False
if command == 'B':
if movementSystemActive:
print("skip")
else:
print("onback")
movementSystemActive=True
goBackward()
time.sleep(straightDelay)
releaseMotors()
movementSystemActive=False
if command == 'L':
if movementSystemActive:
print("skip")
else:
print("onleft")
movementSystemActive=True
turnLeft()
time.sleep(turnDelay)
releaseMotors()
movementSystemActive=False
if command == 'R':
if movementSystemActive:
print("skip")
else:
print("onright")
movementSystemActive=True
turnRight()
time.sleep(turnDelay)
releaseMotors()
movementSystemActive=False
#The m in front of these events differentiates them from the v4l2 commands
#because it is a mechanical pan and tilt. It is possible to have a robot
#that responds to v4l2 pan and tilt and mechanical pan and tilt events.
if command == 'mpan-':
print("onmpan-")
panPercentage+=panIncrement
if panPercentage > 100.0:
panPercentage=100.0
setPan(panPercentage)
if command == 'mpan+':
print("onmpan+")
panPercentage-=panIncrement
if panPercentage < 0.0:
panPercentage=0.0
setPan(panPercentage)
if command == 'mtilt-':
print("onmtilt-")
tiltPercentage+=tiltIncrement
if tiltPercentage > 100.0:
tiltPercentage=100.0
setTilt(tiltPercentage)
if command == 'mtilt+':
print("onmtilt+")
tiltPercentage-=tiltIncrement
if tiltPercentage < 0.0:
tiltPercentage=0.0
setTilt(tiltPercentage)