-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathturn_testing.py
More file actions
259 lines (200 loc) · 8.09 KB
/
turn_testing.py
File metadata and controls
259 lines (200 loc) · 8.09 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
from time import sleep
from gps import *
import time
import RPi.GPIO as GPIO
import threading
import datetime
dt = datetime.datetime.today()
import magnetometer
import pygame, sys
from pygame.locals import *
pygame.init()
BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0, 255)
RED = (255, 0, 0)
SCREENWIDTH = 1000
SCREENHEIGHT = 600
WIDTHMIDPOINT = SCREENWIDTH/2
HEIGHTMIDPOINT = SCREENHEIGHT/2
windowSurface = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT), 0, 32)
windowSurface.fill(BLACK)
DISPLAYSURF = pygame.display.set_mode(((SCREENWIDTH, SCREENHEIGHT)))
fontObj = pygame.font.SysFont('Times New Roman', 16)
def writeText(text, position, color = WHITE):
global fontObj, DISPLAYSURF
txtSurf = fontObj.render(text, True, color)
txtRect = txtSurf.get_rect()
txtRect.topleft = (position)
DISPLAYSURF.blit(txtSurf, txtRect)
#########################################################
GPIO.setmode(GPIO.BOARD)
GPIO.setup(32, GPIO.OUT)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.output(38, GPIO.HIGH)
tolerancelat = .00003
tolerancelong = .00003
def goLeft():
GPIO.output(8, GPIO.HIGH)
#GPIO.output(32, GPIO.LOW)
#GPIO.output(36, GPIO.HIGH)
def goRight():
GPIO.output(12, GPIO.HIGH)
#GPIO.output(32, GPIO.HIGH)
#GPIO.output(36, GPIO.LOW)
def goStraight():
GPIO.output(10, GPIO.HIGH)
GPIO.output(32, GPIO.HIGH)
GPIO.output(36, GPIO.HIGH)
def goForwards():
#GPIO.output(38, GPIO.LOW)
filler = 0
def stop():
GPIO.output(38, GPIO.HIGH)
GPIO.output(10, GPIO.LOW)
GPIO.output(12, GPIO.LOW)
GPIO.output(8, GPIO.LOW)
######################################################
gpsd = gps(mode=WATCH_ENABLE|WATCH_NEWSTYLE)
used = False
def atTarget(pointA, pointB):
if abs(pointA[0]-pointB[0]) < tolerancelat and abs(pointA[1]-pointB[1]) < tolerancelong:
return True
else:
return False
def calculate_initial_compass_bearing(pointA, pointB):
"""
Calculates the bearing between two points.
The formulae used is the following:
θ = atan2(sin(Δlong).cos(lat2),
cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong))
:Parameters:
- `pointA: The tuple representing the latitude/longitude for the
first point. Latitude and longitude must be in decimal degrees
- `pointB: The tuple representing the latitude/longitude for the
second point. Latitude and longitude must be in decimal degrees
:Returns:
The bearing in degrees
:Returns Type:
float
"""
if (type(pointA) != tuple) or (type(pointB) != tuple):
raise TypeError("Only tuples are supported as arguments")
lat1 = math.radians(pointA[0])
lat2 = math.radians(pointB[0])
diffLong = math.radians(pointB[1] - pointA[1])
x = math.sin(diffLong) * math.cos(lat2)
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1)
* math.cos(lat2) * math.cos(diffLong))
initial_bearing = math.atan2(x, y)
# Now we have the initial bearing but math.atan2 return values
# from -180° to + 180° which is not what we want for a compass bearing
# The solution is to normalize the initial bearing as shown below
initial_bearing = math.degrees(initial_bearing)
compass_bearing = (initial_bearing +360) % 360
return compass_bearing
def steering():
global used, cur_long, cur_lat, heading, necessary_change, projected_bearing, position
if used!=True:
used = True
report = gpsd.next() #
if report['class'] == 'TPV':
cur_lat = getattr(report,'lat',0.0)
cur_long = getattr(report,'lon',0.0)
projected_bearing = calculate_initial_compass_bearing((cur_lat, cur_long), (lat1, long1))
heading = magnetometer.get_heading()
print(heading)
necessary_change = projected_bearing - heading
if necessary_change <= -180:
necessary_change += 360
elif necessary_change >= 180:
necessary_change -= 360
m = abs(necessary_change / 50);
if necessary_change > 5:
goRight()
position = "Right"
time.sleep(.2)
goStraight()
elif necessary_change < -5:
goLeft()
position = "Left"
time.sleep(.2)
goStraight()
else:
goStraight()
position = "Straight"
used = False
lat1 = 0.0 #47.34211
long1 = 0.0 #-122.32705
heading = 0.0
projected_bearing = 0.0
necessary_change = 0.0
position = "Straight"
report = gpsd.next()
while report['class'] != 'TPV':
report = gpsd.next()
if report['class'] == 'TPV':
#SWITCHED AROUND FOR TESTING PURPOSES
cur_long = float(getattr(report,'lon',0.0))
cur_lat = float(getattr(report,'lat',0.0))
c = open("coordinates-nautilus-jarvis.dat", "r")
coordinates_array=[]
coords = c.readline()
while coords:
coordinates_array.append(coords.split())
coords = c.readline()
num_of_coordinates = len(coordinates_array)
for y in range(0, num_of_coordinates):
both_coordinates = coordinates_array[y]
for z in range(0, y):
GPIO.output(8, GPIO.HIGH)
GPIO.output(10, GPIO.HIGH)
GPIO.output(12, GPIO.HIGH)
time.sleep(1)
GPIO.output(8, GPIO.LOW)
GPIO.output(10, GPIO.LOW)
GPIO.output(12, GPIO.LOW)
time.sleep(.5)
time.sleep(3)
#Switched these
lat1 = float(both_coordinates[1])
long1 = float(both_coordinates[0])
while atTarget((cur_lat, cur_long), (lat1, long1)) == False:
x = threading.Thread(target = steering, args = ())
x.start()
goForwards()
time.sleep(.1)
stop()
time.sleep(.2)
DISPLAYSURF.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit
pygame.draw.line(DISPLAYSURF, RED, (WIDTHMIDPOINT, 0), (WIDTHMIDPOINT, SCREENHEIGHT), 3)
pygame.draw.line(DISPLAYSURF, RED, (0, HEIGHTMIDPOINT), (WIDTHMIDPOINT, HEIGHTMIDPOINT), 3)
writeText("Variables: ", (WIDTHMIDPOINT + 10, 5))
writeText("GPS ", (WIDTHMIDPOINT + 20, 25))
writeText("Going to waypoint: " + str(y + 1), (WIDTHMIDPOINT + 20, 65))
writeText("Waypoint Latitude: " + str(lat1) + ", Waypoint Longitude" + str(long1), (WIDTHMIDPOINT + 20, 85))
writeText("Latitude: " + str(cur_lat) + ", Longitude: " + str(cur_long), (WIDTHMIDPOINT + 20, 45))
#writeText("Latitude Tolerance: " + str(tolerancelat) + ", Longitude Tolerance: " + str(tolerancelong), (WIDTHMIDPOINT + 20, 105))
writeText("Latitude Tolerance: .00003, Longitude Tolerance: .00003", (WIDTHMIDPOINT + 20, 105))
writeText("Lat from Waypoint: " + str(round(cur_lat - lat1, 8)), (WIDTHMIDPOINT + 20, 125))
writeText("Long from Waypoint: " + str(round(cur_long - long1, 8)), (WIDTHMIDPOINT + 20, 145))
writeText("AtTarget == " + str(atTarget((cur_lat, cur_long), (lat1, long1))), (WIDTHMIDPOINT + 20, 165))
writeText("Compass: ", (WIDTHMIDPOINT + 20, 205))
writeText("Current Heading: " + str(round(heading, 1)), (WIDTHMIDPOINT + 20, 225))
writeText("Desired Heading: " + str(round(projected_bearing, 1)), (WIDTHMIDPOINT + 20, 245))
writeText("Necessary Change: " + str(round(necessary_change, 1)), (WIDTHMIDPOINT + 20, 265))
writeText("Range of Straight: -5 < x < 5", (WIDTHMIDPOINT + 20, 285))
writeText("Car: ", (WIDTHMIDPOINT + 20, 325))
writeText("Turning Position: " + position, (WIDTHMIDPOINT + 20, 345))
#writeText("Forwards Interval: " + position, (WIDTHMIDPOINT + 20, 345))
#writeText("Turning Position: " + position, (WIDTHMIDPOINT + 20, 365))
pygame.display.update()