-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmotorpitx.py
236 lines (212 loc) · 5.09 KB
/
motorpitx.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
#
# motorpitx v0.1 Python module for the MotorPiTX addon board for the Raspberry Pi
# Copyright (c) 2013 Jason Barnett <[email protected]>
#
# This module will allow you to control all the interfaces of the MotorPiTX
# addon board easily. This has been tested with a Raspberry Pi Model A with
# MotorPiTX v0.2 beta board. There is no reason why it shouldn't work with
# other configurations, if it doesn't, email me with as much information and
# let me know!
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# Servod is a ServoBlaster daemon by Richard Hirst <[email protected]>
# https://github.com/richardghirst/PiBits/tree/master/ServoBlaster
#
# Revisions
#
# 0.1 - Initial release (Functions are; blink, motor1, motor2, out1, out2, in1, in2, servo1, servo2, cleanup)
#
#
#
from time import sleep
import RPi.GPIO as GPIO
import os
from random import randint
print "Setting up pins"
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
READY = 7
MOTA1 = 9
MOTA2 = 10
MOTAE = 11
MOTB1 = 24
MOTB2 = 23
MOTBE = 25
OUT1 = 22
OUT2 = 17
if GPIO.RPI_REVISION == 1:
IN1 = 21
else:
IN1 = 27
#IN1 = 27
IN2 = 4
SERVO1 = 18
SERVO2 = 15
GPIO.setup(READY, GPIO.OUT)
GPIO.setup(MOTA1, GPIO.OUT)
GPIO.setup(MOTA2, GPIO.OUT)
GPIO.setup(MOTAE, GPIO.OUT)
GPIO.setup(MOTB1, GPIO.OUT)
GPIO.setup(MOTB2, GPIO.OUT)
GPIO.setup(MOTBE, GPIO.OUT)
GPIO.setup(OUT1, GPIO.OUT)
GPIO.setup(OUT2, GPIO.OUT)
GPIO.setup(IN1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(IN2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(SERVO1, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(SERVO2, GPIO.OUT, initial=GPIO.LOW)
MOTAPWM = GPIO.PWM(MOTAE,100)
MOTAPWM.start(0)
MOTBPWM = GPIO.PWM(MOTBE,100)
MOTBPWM.start(0)
OUT1PWM = GPIO.PWM(OUT1,100)
OUT1PWM.start(0)
OUT2PWM = GPIO.PWM(OUT2,100)
OUT2PWM.start(0)
## Blink the Ready light, perfect first test
def blink():
GPIO.output(READY, GPIO.LOW)
sleep(1)
GPIO.output(READY, GPIO.HIGH)
sleep(1)
def motor1(value):
Error = False
while True:
try:
value = int(value)
break
except:
Error = True
print "Motor 1: Please enter a number - " + value + " isn't valid"
break
while Error != True:
if -100 <= value <= 100:
break
else:
print "Motor 1: Number must be between -100 and 100 - '" + str(value) + "' isn't valid"
Error = True
break
if Error != True:
if value > 0:
#print "Forward"
GPIO.output(MOTA1, GPIO.HIGH)
GPIO.output(MOTA2, GPIO.LOW)
MOTAPWM.ChangeDutyCycle(value)
elif value < 0:
#print "Backwards"
GPIO.output(MOTA1, GPIO.LOW)
GPIO.output(MOTA2, GPIO.HIGH)
MOTAPWM.ChangeDutyCycle(abs(value))
else:
#print "Stopped"
GPIO.output(MOTA1, GPIO.LOW)
GPIO.output(MOTA2, GPIO.LOW)
MOTAPWM.ChangeDutyCycle(0)
def motor2(value):
Error = False
while True:
try:
value = int(value)
break
except:
Error = True
print "Motor 2: Please enter a number - " + value + " isn't valid"
break
while Error != True:
if -100 <= value <= 100:
break
else:
print "Motor 2: Number must be between -100 and 100 - '" + str(value) + "' isn't valid"
Error = True
break
if Error != True:
if value > 0:
#print "Forward"
GPIO.output(MOTB1, GPIO.HIGH)
GPIO.output(MOTB2, GPIO.LOW)
MOTBPWM.ChangeDutyCycle(value)
elif value < 0:
#print "Backwards"
GPIO.output(MOTB1, GPIO.LOW)
GPIO.output(MOTB2, GPIO.HIGH)
MOTBPWM.ChangeDutyCycle(abs(value))
else:
#print "Stopped"
GPIO.output(MOTB1, GPIO.LOW)
GPIO.output(MOTB2, GPIO.LOW)
MOTBPWM.ChangeDutyCycle(0)
def out1(value):
if value == True:
OUT1PWM.ChangeDutyCycle(100)
elif value == int(value):
OUT1PWM.ChangeDutyCycle(value)
else:
GPIO.output(OUT1, GPIO.LOW)
def out2(value):
if value == True:
OUT2PWM.ChangeDutyCycle(100)
elif value == int(value):
OUT2PWM.ChangeDutyCycle(value)
else:
GPIO.output(OUT2, GPIO.LOW)
def in1():
if GPIO.input(IN1):
return True
else:
return False
def in2():
if GPIO.input(IN2):
return True
else:
return False
def servo1(value):
Error = False
while True:
try:
value = int(value)
break
except:
Error = True
print "Servo 1: Please enter a number - " + value + " isn't valid"
break
while Error != True:
if 0 <= value <= 180:
break
else:
print "Servo 1: Number must be between 0 and 180 - '" + str(value) + "' isn't valid"
Error = True
break
if Error != True:
servo = "echo 0=%d > /dev/servoblaster" % value
os.system(servo)
sleep(0.2)
servo = "echo 0=0 > /dev/servoblaster"
os.system(servo)
def servo2(value):
Error = False
while True:
try:
value = int(value)
break
except:
Error = True
print "Servo 2: Please enter a number - " + value + " isn't valid"
break
while Error != True:
if 0 <= value <= 180:
break
else:
print "Servo 2: Number must be between 0 and 180 - '" + str(value) + "' isn't valid"
Error = True
break
if Error != True:
servo = "echo 1=%d > /dev/servoblaster" % value
os.system(servo)
sleep(0.2)
servo = "echo 1=0 > /dev/servoblaster"
os.system(servo)
def cleanup():
GPIO.cleanup()