-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredboard.py
More file actions
280 lines (203 loc) · 7.11 KB
/
Copy pathredboard.py
File metadata and controls
280 lines (203 loc) · 7.11 KB
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
#Python library for the Red Robotics 'HeadBoard' and 'SideBoard' Raspberry Pi add on robotics boards.
#Simple python commands for controlling motors, servos and Neopixels (WS2812B).
#Version 1.22 21/6/2018
# Author: Neil Lambeth. neil@redrobotics.co.uk @NeilRedRobotics
from __future__ import print_function # Make print work with python 2 & 3
print('Please wait while modules load...')
import time
import pigpio
from neopixels import *
# Setup GPIO
servo_0 = 20
servo_1 = 21
dira = 23
pwma = 18
dirb = 24
pwmb = 25
FWD = 1
BWD = 0
rMotor = 0
lMotor = 0
RM = 0
LM = 0
INPUT = 1
OUTPUT = 0
pi = pigpio.pi()
pi.set_mode(dira, pigpio.OUTPUT)
pi.set_mode(pwma, pigpio.OUTPUT)
pi.set_mode(dirb, pigpio.OUTPUT)
pi.set_mode(pwmb, pigpio.OUTPUT)
pi.write(dira, 0)
pi.write(dirb, 0)
pi.set_PWM_frequency(pwma, 100)
pi.set_PWM_frequency(pwmb, 100)
pi.set_mode(servo_0, pigpio.OUTPUT)
pi.set_mode(servo_1, pigpio.OUTPUT)
print("Redboard Library V1.22 loaded")
#-----------------------------------------------------
#GPIO
def output_pin(p):
pi.set_mode(p, pigpio.OUTPUT)
def input_pin(p):
pi.set_mode(p, pigpio.INPUT)
def pull_up(p):
pi.set_pull_up_down(p, pigpio.PUD_UP)
def pull_down(p):
pi.set_pull_up_down(p, pigpio.PUD_DOWN)
def setPin(p, state):
pi.write(p, state)
def readPin(p):
r = pi.read(p)
return r
#-----------------------------------------------------
def servo0(pos0):
if pos0 >= 0 and pos0 <91:
print ("servo0 =",pos0)
pos0 = 1500 - (pos0 * 11.1)
#print (pos0)
pi.set_servo_pulsewidth(servo_0, pos0)
elif pos0 < 0 and pos0 >-91:
print ("servo0 =",pos0)
pos0 = (abs(pos0) * 11.1) + 1500
#print (pos0)
pi.set_servo_pulsewidth(servo_0, pos0)
else:
print ("Out Of Range!")
#-----------------------------------------------------
def servo0_P(pos0):
if pos0 >499 and pos0 <2501:
print ("servo0 =",pos0)
pi.set_servo_pulsewidth(servo_0, pos0)
else:
print ("Out Of Range!")
#-----------------------------------------------------
def servo0_off():
pi.set_servo_pulsewidth(servo_0, 0)
print ("servo0 off")
#-----------------------------------------------------
def servo1(pos1):
if pos1 >= 0 and pos1 <91:
print ("servo1 =",pos1)
pos1 = 1500 - (pos1 * 11.1)
#print (pos1)
pi.set_servo_pulsewidth(servo_1, pos1)
elif pos1 < 0 and pos1 >-91:
print ("servo1 =",pos1)
pos1 = (abs(pos1) * 11.1) + 1500
#print (pos1)
pi.set_servo_pulsewidth(servo_1, pos1)
else:
print ("Out Of Range!")
#-----------------------------------------------------
def servo1_P(pos1):
if pos1 >499 and pos1 <2501:
print ("servo1 =",pos1)
pi.set_servo_pulsewidth(servo_1, pos1)
else:
print ("Out Of Range!")
#-----------------------------------------------------
def servo1_off():
pi.set_servo_pulsewidth(servo_1, 0)
print ("servo1 off")
#-----------------------------------------------------
def M1(lm):
if lm > 100: # Make sure the value sent to the motor is 100 or less
print("Out of range")
lm = 100
elif lm < -100: # Make sure the value sent to the motor is 100 or less
print("Out of range")
lm = -100
lMotor = lm * 2.55
# Set left motor direction
if lMotor > 0:
pi.write(dirb, FWD) # Go forwards
LM = lMotor
#print("Motor1 =",lm,"\r")
#print("Actual = ",LM)
elif lMotor < 0:
pi.write(dirb, BWD) # Go backwards
LM = abs(lMotor) # Make positive
#print("Motor1 =",lm,"\r")
#print("Actual = -",LM)
else:
#print("M1 Stop\r")
LM = 0 # Stop
pi.set_PWM_dutycycle(pwmb,LM)
#-----------------------------------------------------
def M1_8bit(lm):
if lm > 255: # Make sure the value sent to the motor is 255 or less
print("Out of range")
lm = 255
elif lm < -255: # Make sure the value sent to the motor is 255 or less
print("Out of range")
lm = -255
# Set left motor direction
if lm > 0:
pi.write(dirb, FWD) # Go forwards
LM = lm
#print("Motor1 =",lm,"\r")
#print("Actual = ",LM)
elif lm < 0:
pi.write(dirb, BWD) # Go backwards
LM = abs(lm) # Make positive
#print("Motor1 =",lm,"\r")
#print("Actual = -",LM)
else:
#print("M1 Stop\r")
LM = 0 # Stop
pi.set_PWM_dutycycle(pwmb,LM)
#-----------------------------------------------------
def M2(rm):
if rm > 100: # Make sure the value sent to the motor is 100 or less
print("Out of range")
rm = 100
elif rm < -100: # Make sure the value sent to the motor is 100 or less
print("Out of range")
rm = -100
rMotor = rm * 2.55
# Set right motor direction
if rMotor > 0:
pi.write(dira, FWD) # Go forwards
RM = rMotor
#print("Motor2 =",rm,"\r")
#print("Actual = ",RM)
elif rMotor < 0:
pi.write(dira, BWD) # Go backwards
RM = abs(rMotor) # Make positive
#print("Motor2 =",rm,"\r")
#print("Actual = -",RM)
else:
#print("M2 Stop\r")
RM = 0 # Stop
pi.set_PWM_dutycycle(pwma,RM)
#-----------------------------------------------------
def M2_8bit(rm):
if rm > 255: # Make sure the value sent to the motor is 255 or less
print("Out of range")
rm = 255
elif rm < -255: # Make sure the value sent to the motor is 255 or less
print("Out of range")
rm = -255
# Set left motor direction
if rm > 0:
pi.write(dira, FWD) # Go forwards
RM = rm
#print("Motor1 =",rm,"\r")
#print("Actual = ",RM)
elif rm < 0:
pi.write(dira, BWD) # Go backwards
pi.write(dira, BWD) # Go backwards
RM = abs(rm) # Make positive
#print("Motor1 =",rm,"\r")
#print("Actual = -",RM)
else:
#print("M1 Stop\r")
RM = 0 # Stop
pi.set_PWM_dutycycle(pwma,RM)
#-----------------------------------------------------
def Stop():
pi.set_PWM_dutycycle(pwma,0)
pi.set_PWM_dutycycle(pwmb,0)
pi.set_mode(pwma, pigpio.INPUT)
pi.set_mode(pwmb, pigpio.INPUT)
pi.stop()