-
Notifications
You must be signed in to change notification settings - Fork 0
/
compass.py
314 lines (270 loc) · 9.54 KB
/
compass.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
'''
@author Marcopolo Gil
'''
import os
import sys
sys.path.append('/usr/local/lib/python3.4/site-packages/')
import threading
import time
import math
import numpy as np
import cv2
import random
import pathfindingv2 as pathfinding
import socket
from scipy import misc
from scipy.ndimage import rotate
import lib.variables as var
import lib.motors as motors
import lib.imu as imu
import Jetson.dbscan_contours as dbscan
#Navigation class
MAP_WIDTH = 400;
MAP_HEIGHT = 400;
BOUY_RADIOUS = 6;
LIDAR_RADIOUS = 1;
BOAT_HEIGHT = 58;
BOAT_WIDTH = 34;
BOAT_X1 = int(MAP_WIDTH/2 - BOAT_WIDTH/2);
BOAT_Y1 = int(MAP_HEIGHT/2 - BOAT_HEIGHT/2);
BOAT_X2 = int(MAP_WIDTH/2 + BOAT_WIDTH/2);
BOAT_Y2 = int(MAP_HEIGHT/2 + BOAT_HEIGHT/2);
LIDAR_COORD_X = 200;
LIDAR_COORD_Y = int(200 - BOAT_HEIGHT / 2);
runProgram = True;
capture = None;
emptyMap = None;
routeMap = None;
boatMap = None;
lidar_ready = False;
start_time = time.time();
destiny = {'degree': 0, 'distance': 0};
destinyCoords = [0,0];
routePoints = [];
lidarObstacles = [];
orientationDegree = 0;
pixelsGoal = [0,0];
class LidarSocketThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self);
self.threadID = threadID;
self.name = name;
def run(self):
global lidarObstacles;
s = socket.socket();
s.bind(("localhost", 8895));
s.listen(1);
sc, addr = s.accept();
while runProgram:
message = sc.recv(2000);
if message == "quit":
break
strMeasures = message.decode('utf-8');
arrMeasures = strMeasures.split(";");
if(len(arrMeasures) > 0):
lidarObstacles = arrMeasures;
lidarObstacles.pop();
sc.close();
s.close();
print("End thread Socket");
class MapThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self);
self.threadID = threadID;
self.name = name;
def run(self):
global routeMap, orientationDegree;
emptyMap = self.new_map(MAP_WIDTH, MAP_HEIGHT);
routeMap = emptyMap.copy();
while runProgram:
routeMap = emptyMap.copy();
'''
'Set lidar obstacles in the map
'''
for measure in lidarObstacles:
data = measure.split(",");
degree = int(data[0]);
if( (degree > 0 and degree < 90) or degree > 270 and degree < 360):
pixelX = LIDAR_COORD_X + int (math.cos(math.radians(degree - 90)) * float(data[1]) / 25);
pixelY = LIDAR_COORD_Y + int (math.sin(math.radians(degree - 90)) * float(data[1]) / 25);
#print(pixelX, pixelY);
cv2.circle(routeMap, (pixelX, pixelY), int(BOUY_RADIOUS + BOAT_WIDTH * 0.8), (255, 255 , 255), -1, 8);
cv2.circle(routeMap, (pixelX, pixelY), BOUY_RADIOUS, (0, 0, 255), -1, 8);
'''
'Set camera obstacles in the map
'''
frame = capture.read();
#cv2.imshow('cam', frame[1]);
cv2.waitKey(5);
values = dbscan.get_obstacles(frame[1],'yg', False);
camObstacles = values[1];
for obstacle in camObstacles:
#print("cam obstacles");
pixelX = LIDAR_COORD_X + int (math.cos(math.radians(obstacle[1] - 90)) * float(obstacle[0]) / 25);
pixelY = LIDAR_COORD_Y + int (math.sin(math.radians(obstacle[1] - 90)) * float(obstacle[0]) / 25);
cv2.circle(routeMap, (pixelX, pixelY), int(BOUY_RADIOUS + BOAT_WIDTH * 0.8), (255, 255 , 255), -1, 8);
cv2.circle(routeMap, (pixelX, pixelY), BOUY_RADIOUS, (0, 0, 255), -1, 8);
pass;
#print("destiny", destiny);
print(destiny);
#locate destiny pixels if is less than 10 meters.
if(destiny['distance'] < 10):
destinyPixelX = LIDAR_COORD_X + int (math.cos(math.radians(destiny['degree'] - 90)) * float(destiny['distance']) / 25);
destinyPixelY = LIDAR_COORD_Y + int (math.sin(math.radians(destiny['degree'] - 90)) * float(destiny['distance']) / 25);
destinyPixel = [destinyPixelY, destinyPixelX];
#locate destiny by orientation.
else:
#locate destiny in top border.
if(math.fabs(destiny['degree']) < 45):
destinyDistanceY = 200;
destinyPixelY = 0;
destinyDistanceX = destinyDistanceY / math.tan(math.radians(destiny['degree'] + 90));
#print("destinyDistanceX ", destinyDistanceX);
destinyPixelX = int(MAP_WIDTH/2 + destinyDistanceX);
#print("destinyPixelX ", destinyPixelX);
destinyPixel = [destinyPixelY, destinyPixelX];
#locate destiny in right border
elif(destiny['degree'] < -45 and destiny['degree'] > -135):
destinyDistanceX = 200;
destinyPixelX = 399;
destinyDistanceY = math.tan(math.radians(destiny['degree'] + 90)) * destinyDistanceX;
destinyPixelY = int(MAP_WIDTH/2 - destinyDistanceY);
destinyPixel = [destinyPixelY, destinyPixelX];
#locate destiny in left border
elif(destiny['degree'] > 45 and destiny['degree'] < 135):
destinyDistanceX = 200;
destinyPixelX = 0;
destinyDistanceY = math.tan(math.radians(destiny['degree'] + 90)) * destinyDistanceX;
destinyPixelY = int(MAP_WIDTH/2 + destinyDistanceY);
destinyPixel = [destinyPixelY, destinyPixelX];
#locate destiny in bottom border
elif(math.fabs(destiny['degree']) > 135):
destinyDistanceY = 200;
destinyPixelY = 399;
destinyDistanceX = destinyDistanceY / math.tan(math.radians(destiny['degree'] + 90));
destinyPixelX = int(MAP_WIDTH/2 + destinyDistanceX);
destinyPixel = [destinyPixelY, destinyPixelX];
print("destiny pixel: ", destinyPixel);
cv2.imshow('Route', routeMap);
#cv2.imwrite('route_test.png',routeMap)
#Todo: check if destiny is inside obstacle;
routePoints = pathfinding.a_star([int(MAP_WIDTH/2), int(MAP_HEIGHT/2)], destinyPixel, routeMap);
routeLength = len(routePoints);
for point in routePoints:
routeMap[point[0]][point[1]] = [0, 0, 255];
pass;
if(routeLength > 40):
pixelX = routePoints[-40][0];
pixelY = routePoints[-40][1];
orientation = math.atan2(MAP_HEIGHT / 2 - pixelY, MAP_WIDTH / 2 - pixelX);
orientationDegree = math.degrees(orientation);
else:
orientationDegree = 0;
#print("orientation degree mapa: ", orientationDegree);
self.add_boat(routeMap);
cv2.imshow('Route', routeMap);
print("End thread Map");
def new_map(self, rows, cols):
mapa = np.full((rows, cols, 3),0, dtype = np.uint8);
return mapa;
def add_boat(self, mapa):
cv2.circle(mapa, (LIDAR_COORD_X, LIDAR_COORD_Y), LIDAR_RADIOUS, (255,255,255), -1, 8);
cv2.rectangle(mapa,(BOAT_X1, BOAT_Y1),(BOAT_X2, BOAT_Y2), (0,255,0), 1, 8);
class NavigationThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self);
self.threadID = threadID;
self.name = name;
def run(self):
global orientationDegree, destinyCoords;
destinyCoords = [25.649529, -100.290430];
self.go_to_destiny(25.649529, -100.290430);
def go_to_destiny(self, latitude2, longitud2):
global destiny, runProgram;
destiny = imu.get_degrees_and_distance_to_gps_coords(latitude2, longitud2);
orientationDegree = destiny['degree'];
lastOrientationDegree = orientationDegree;
turn_degrees_needed = orientationDegree;
turn_degrees_accum = 0;
#clean angle;
imu.get_delta_theta();
print("destiny degrees", destiny['degree']);
print("destiny distance", destiny['distance']);
#Condition distance more than 2 meters.
while destiny['distance'] > 2 and runProgram:
#print("orientation degrees", orientationDegree);
if(lastOrientationDegree != orientationDegree):
turn_degrees_needed = orientationDegree;
turn_degrees_accum = 0;
#clean angle;
imu.get_delta_theta();
lastOrientationDegree = orientationDegree;
#If same direction, keep route
#while math.fabs(turn_degrees_needed) > 10:
imu_angle = imu.get_delta_theta()['z']%360;
if(imu_angle > 180):
imu_angle = imu_angle -360;
#print("grados imu: ", imu_angle);
turn_degrees_accum += imu_angle;
#print("grados acc: ", turn_degrees_accum);
turn_degrees_needed = (orientationDegree + turn_degrees_accum)%360;
if(turn_degrees_needed > 180):
turn_degrees_needed = turn_degrees_needed - 360;
print("grados a voltear: ", turn_degrees_needed);
if(math.fabs(turn_degrees_needed) < 5):
print("Tengo un margen menor a 5 grados");
else:
#girar
if(turn_degrees_needed > 0):
print("Going to move left")
#motors.move_right(100);
#motors.move_left(0);
else:
print("Going to move right")
#motors.move_left(100);
#motors.move_right(0);
#ir derecho;
#recorrer 2 metros
destiny = imu.get_degrees_and_distance_to_gps_coords(latitude2, longitud2);
runProgram = cv2.waitKey(1) != 27;
print("End thread Navigation");
class TestThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self);
self.threadID = threadID;
self.name = name;
def run(self):
print("hola")
while True:
print(imu.get_magnetic_measurments());
print(imu.get_yaw_orientation());
time.sleep(.5)
pass;
def init():
global capture;
imu.init();
capture = cv2.VideoCapture(1);
if(capture.isOpened() == False):
print("No hay cámara");
return -1;
else:
print("cámara encendida");
'''
' Inicio del programa
'''
init();
# Create new threads
#thread1 = LidarSocketThread(1, "LidarSocketThread");
#thread2 = MapThread(2, "MapThread");
#thread3 = NavigationThread(3, "NavigationThread");
thread4 = TestThread(4, "TestThread");
# Start new Threads
#thread1.start();
#thread2.start();
#thread3.start();
thread4.start();
#thread1.join();
#thread2.join();
#thread3.join();
thread4.join();
print ("Exiting Main Thread");