-
Notifications
You must be signed in to change notification settings - Fork 0
/
initio.py
319 lines (265 loc) · 9.54 KB
/
initio.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/python
#
# Python Module to externalise all Initio/PiRoCon specific hardware
#
# Created by Gareth Davies, Sep 2013
# Updated May 2014, Feb 2015
# Copyright 4tronix
#
# This code is in the public domain and may be freely copied and used
# No warranty is provided or implied
#
#======================================================================
#======================================================================
# General Functions
#
# init(). Initialises GPIO pins, switches motors off, etc
# cleanup(). Sets all motors off and sets GPIO to standard values
# version(). Returns 1. Invalid until after init() has been called
#======================================================================
#======================================================================
# Motor Functions
#
# stop(): Stops both motors
# forward(speed): Sets both motors to move forward at speed. 0 <= speed <= 100
# reverse(speed): Sets both motors to reverse at speed. 0 <= speed <= 100
# spinLeft(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
# spinRight(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
# turnForward(leftSpeed, rightSpeed): Moves forwards in an arc by setting different speeds. 0 <= leftSpeed,rightSpeed <= 100
# turnreverse(leftSpeed, rightSpeed): Moves backwards in an arc by setting different speeds. 0 <= leftSpeed,rightSpeed <= 100
#======================================================================
#======================================================================
# IR Sensor Functions
#
# irLeft(): Returns state of Left IR Obstacle sensor
# irRight(): Returns state of Right IR Obstacle sensor
# irAll(): Returns true if either of the Obstacle sensors are triggered
# irLeftLine(): Returns state of Left IR Line sensor
# irRightLine(): Returns state of Right IR Line sensor
#======================================================================
#======================================================================
# UltraSonic Functions
#
# getDistance(). Returns the distance in cm to the nearest reflecting object. 0 == no object
#======================================================================
#======================================================================
# Servo Functions
#
# startServos(). Initialises the servo background process
# stop Servos(). terminates the servo background process
# setServo(Servo, Degrees). Sets the servo to position in degrees -90 to +90
#======================================================================
# Import all necessary libraries
import RPi.GPIO as GPIO, sys, threading, time, os, subprocess
# Pins 24, 26 Right Motor
# Pins 19, 21 Left Motor
R1 = 33
R2 = 32
L1 = 36
L2 = 35
# Define obstacle sensors and line sensors
irFL = 7
irFR = 11
lineRight = 13
lineLeft = 12
# Define Sonar Pin (same pin for both Ping and Echo)
# Note that this can be either 8 or 23 on PiRoCon
sonar = 8
ServosActive = False
#======================================================================
# General Functions
#
# init(). Initialises GPIO pins, switches motors and LEDs Off, etc
def init():
global p, q, a, b
# Initialise the PWM device using the default address
#use physical pin numbering
GPIO.setmode(GPIO.BOARD)
#print GPIO.RPI_REVISION
#set up digital line detectors as inputs
GPIO.setup(lineRight, GPIO.IN) # Right line sensor
GPIO.setup(lineLeft, GPIO.IN) # Left line sensor
#Set up IR obstacle sensors as inputs
GPIO.setup(irFL, GPIO.IN) # Left obstacle sensor
GPIO.setup(irFR, GPIO.IN) # Right obstacle sensor
#use pwm on inputs so motors don't go too fast
GPIO.setup(L1, GPIO.OUT)
p = GPIO.PWM(L1, 20)
p.start(0)
GPIO.setup(L2, GPIO.OUT)
q = GPIO.PWM(L2, 20)
q.start(0)
GPIO.setup(R1, GPIO.OUT)
a = GPIO.PWM(R1, 20)
a.start(0)
GPIO.setup(R2, GPIO.OUT)
b = GPIO.PWM(R2, 20)
b.start(0)
startServos()
# cleanup(). Sets all motors off and sets GPIO to standard values
def cleanup():
stop()
stopServos()
GPIO.cleanup()
# version(). Returns 1. Invalid until after init() has been called
def version():
return 1
# End of General Functions
#======================================================================
#======================================================================
# Motor Functions
#
# stop(): Stops both motors
def stop():
p.ChangeDutyCycle(0)
q.ChangeDutyCycle(0)
a.ChangeDutyCycle(0)
b.ChangeDutyCycle(0)
# forward(speed): Sets both motors to move forward at speed. 0 <= speed <= 100
def forward(speed):
p.ChangeDutyCycle(speed)
q.ChangeDutyCycle(0)
a.ChangeDutyCycle(speed)
b.ChangeDutyCycle(0)
p.ChangeFrequency(speed + 5)
a.ChangeFrequency(speed + 5)
# reverse(speed): Sets both motors to reverse at speed. 0 <= speed <= 100
def reverse(speed):
p.ChangeDutyCycle(0)
q.ChangeDutyCycle(speed)
a.ChangeDutyCycle(0)
b.ChangeDutyCycle(speed)
q.ChangeFrequency(speed + 5)
b.ChangeFrequency(speed + 5)
# spinLeft(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
def spinLeft(speed):
p.ChangeDutyCycle(0)
q.ChangeDutyCycle(speed)
a.ChangeDutyCycle(speed)
b.ChangeDutyCycle(0)
q.ChangeFrequency(speed + 5)
a.ChangeFrequency(speed + 5)
# spinRight(speed): Sets motors to turn opposite directions at speed. 0 <= speed <= 100
def spinRight(speed):
p.ChangeDutyCycle(speed)
q.ChangeDutyCycle(0)
a.ChangeDutyCycle(0)
b.ChangeDutyCycle(speed)
p.ChangeFrequency(speed + 5)
b.ChangeFrequency(speed + 5)
# turnForward(leftSpeed, rightSpeed): Moves forwards in an arc by setting different speeds. 0 <= leftSpeed,rightSpeed <= 100
def turnForward(leftSpeed, rightSpeed):
p.ChangeDutyCycle(leftSpeed)
q.ChangeDutyCycle(0)
a.ChangeDutyCycle(rightSpeed)
b.ChangeDutyCycle(0)
p.ChangeFrequency(leftSpeed + 5)
a.ChangeFrequency(rightSpeed + 5)
# turnReverse(leftSpeed, rightSpeed): Moves backwards in an arc by setting different speeds. 0 <= leftSpeed,rightSpeed <= 100
def turnReverse(leftSpeed, rightSpeed):
p.ChangeDutyCycle(0)
q.ChangeDutyCycle(leftSpeed)
a.ChangeDutyCycle(0)
b.ChangeDutyCycle(rightSpeed)
q.ChangeFrequency(leftSpeed + 5)
b.ChangeFrequency(rightSpeed + 5)
# End of Motor Functions
#======================================================================
#======================================================================
# IR Sensor Functions
#
# irLeft(): Returns state of Left IR Obstacle sensor
def irLeft():
if GPIO.input(irFL)==0:
return True
else:
return False
# irRight(): Returns state of Right IR Obstacle sensor
def irRight():
if GPIO.input(irFR)==0:
return True
else:
return False
# irAll(): Returns true if any of the Obstacle sensors are triggered
def irAll():
if GPIO.input(irFL)==0 or GPIO.input(irFR)==0:
return True
else:
return False
# irLeftLine(): Returns state of Left IR Line sensor
def irLeftLine():
if GPIO.input(lineLeft)==0:
return True
else:
return False
# irRightLine(): Returns state of Right IR Line sensor
def irRightLine():
if GPIO.input(lineRight)==0:
return True
else:
return False
# End of IR Sensor Functions
#======================================================================
#======================================================================
# UltraSonic Functions
#
# getDistance(). Returns the distance in cm to the nearest reflecting object. 0 == no object
def getDistance():
GPIO.setup(sonar, GPIO.OUT)
# Send 10us pulse to trigger
GPIO.output(sonar, True)
time.sleep(0.00001)
GPIO.output(sonar, False)
start = time.time()
count=time.time()
GPIO.setup(sonar,GPIO.IN)
while GPIO.input(sonar)==0 and time.time()-count<0.1:
start = time.time()
count=time.time()
stop=count
while GPIO.input(sonar)==1 and time.time()-count<0.1:
stop = time.time()
# Calculate pulse length
elapsed = stop-start
# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34000
# That was the distance there and back so halve the value
distance = distance / 2
return distance
# End of UltraSonic Functions
#======================================================================
#======================================================================
# Servo Functions
# Pirocon/microcon use ServoD to control servos
def setServo(Servo, Degrees):
global ServosActive
#print "ServosActive:", ServosActive
#print "Setting servo"
if ServosActive == False:
startServos()
pinServod (Servo, Degrees) # for now, simply pass on the input values
def stopServos():
#print "Stopping servo"
stopServod()
def startServos():
#print "Starting servod as CPU =", CPU
startServod()
def startServod():
global ServosActive
#print "Starting servod. ServosActive:", ServosActive
SCRIPTPATH = os.path.split(os.path.realpath(__file__))[0]
#os.system("sudo pkill -f servod")
initString = SCRIPTPATH +'/servod --pcm --idle-timeout=20000 --p1pins="18,22" > /dev/null'
os.system(initString)
#print initString
ServosActive = True
def pinServod(pin, degrees):
#print pin, degrees
pinString = "echo " + str(pin) + "=" + str(50+ ((90 - degrees) * 200 / 180)) + " > /dev/servoblaster"
#print pinString
os.system(pinString)
def stopServod():
global ServosActive
os.system("sudo pkill -f servod")
ServosActive = False