-
Notifications
You must be signed in to change notification settings - Fork 0
/
pibot_remocon.py
221 lines (194 loc) · 4.75 KB
/
pibot_remocon.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
import pygame,sys
from time import sleep
import spidev
import RPi.GPIO as GPIO
Delay1=0.0000000001
Delay2=0.0000001
def Forward():
spi.open(0,0)
spi.max_speed_hz=1000000
spi.xfer([0b01011001])
spi.close()
spi.open(0,1)
spi.max_speed_hz=1000000
spi.xfer([0b01011000])
spi.close()
GPIO.output(29,True)
GPIO.output(31,True)
sleep(Delay1)
GPIO.output(29,False)
GPIO.output(31,False)
def Back():
spi.open(0,0)
spi.max_speed_hz=1000000
spi.xfer([0b01011000])
spi.close()
spi.open(0,1)
spi.max_speed_hz=1000000
spi.xfer([0b01011001])
spi.close()
GPIO.output(29,True)
GPIO.output(31,True)
sleep(Delay1)
GPIO.output(29,False)
GPIO.output(31,False)
#sleep(0.000001)
def PivotTurnRight():
spi.open(0,0)
spi.max_speed_hz=1000000
spi.xfer([0b01011000])
spi.close()
spi.open(0,1)
spi.max_speed_hz=1000000
spi.xfer([0b01011000])
spi.close()
GPIO.output(29,True)
GPIO.output(31,True)
sleep(Delay1)
GPIO.output(29,False)
GPIO.output(31,False)
#sleep(0.000001)
def PivotTurnLeft():
spi.open(0,0)
spi.max_speed_hz=1000000
spi.xfer([0b01011001])
spi.close()
spi.open(0,1)
spi.max_speed_hz=1000000
spi.xfer([0b01011001])
spi.close()
GPIO.output(29,True)
GPIO.output(31,True)
sleep(Delay1)
GPIO.output(29,False)
GPIO.output(31,False)
#sleep(0.000001)
def TurnRight():
spi.open(0,1)
spi.max_speed_hz=1000000
spi.xfer([0b01011000])
spi.close()
GPIO.output(31,True)
sleep(Delay2)
GPIO.output(31,False)
#sleep(0.000001)
def TurnLeft():
spi.open(0,0)
spi.max_speed_hz=1000000
spi.xfer([0b01011001])
spi.close()
GPIO.output(29,True)
sleep(Delay2)
GPIO.output(29,False)
#sleep(0.000001)
def BackTurnRight():
spi.open(0,1)
spi.max_speed_hz=1000000
spi.xfer([0b01011001])
spi.close()
GPIO.output(31,True)
sleep(Delay2)
GPIO.output(31,False)
#sleep(0.000001)
def BackTurnLeft():
spi.open(0,0)
spi.max_speed_hz=1000000
spi.xfer([0b01011000])
spi.close()
GPIO.output(29,True)
sleep(Delay2)
GPIO.output(29,False)
#sleep(0.000001)
#################################################################
pygame.init()
window = pygame.display.set_mode((200, 200), 0, 32)
# how many joysticks connected to computer?
joystick_count = pygame.joystick.get_count()
print ("There is " + str(joystick_count) + " joystick/s")
if joystick_count == 0:
# if no joysticks, quit program safely
print ("Error, I did not find any joysticks")
pygame.quit()
sys.exit()
else:
# initialise joystick
joystick = pygame.joystick.Joystick(0)
joystick.init()
axes = joystick.get_numaxes()
buttons = joystick.get_numbuttons()
hats = joystick.get_numhats()
print ("There is " + str(axes) + " axes")
print ("There is " + str(buttons) + " button/s")
print ("There is " + str(hats) + " hat/s")
#GPIO Setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(29,GPIO.OUT)
GPIO.setup(31,GPIO.OUT)
#SPI Motor Driver L6470 Setup
spi=spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=10000
spi.xfer([0b11000000])
spi.xfer([0x16])
spi.xfer([7])
spi.xfer([0b01011000])
spi.close()
spi.open(0,1)
spi.max_speed_hz=10000
spi.xfer([0b11000000])
spi.xfer([0x16])
spi.xfer([7])
spi.xfer([0b01011000])
spi.close()
comid=0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if joystick.get_button(3):
spi.open(0,0)
spi.xfer([0b11000000])
spi.close()
spi.open(0,1)
spi.xfer([0b11000000])
spi.close()
GPIO.cleanup()
pygame.quit()
sys.exit()
joylr=joystick.get_axis(0)
joyfb=joystick.get_axis(1)
#print(joylr,joyfb)
if joyfb<-0.8 and -0.6<joylr<0.6:
comid+=1
print(comid,'Forward')
Forward()
elif joyfb>0.8 and -0.6<joylr<0.6:
comid+=1
print(comid,'Back')
Back()
elif -0.6<joyfb<0.6 and joylr>0.8:
comid+=1
print(comid,'PivotTurnRight')
PivotTurnRight()
elif -0.6<joyfb<0.6 and joylr<-0.8:
comid+=1
print(comid,'PivotTurnLeft')
PivotTurnLeft()
elif joyfb<-0.8 and joylr>0.8:
comid+=1
print(comid,'TurnRight')
TurnRight()
elif joyfb<-0.8 and joylr<-0.8:
comid+=1
print(comid,'TurnLeft')
TurnLeft()
elif joyfb>0.8 and joylr>0.8:
comid+=1
print(comid,'BackTurnRight')
BackTurnRight()
elif joyfb>0.8 and joylr<-0.8:
comid+=1
print(comid,'BackTurnLeft')
BackTurnLeft()
#sleep(0.1)