-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadarLib.py
95 lines (76 loc) · 2.71 KB
/
RadarLib.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
# Bibliotheken einbinden
import RPi.GPIO as GPIO
import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import math
from io import BytesIO
class Radar:
def __init__(self, motors):
# GPIO Modus (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
# GPIO Pins zuweisen
self.GPIO_TRIGGER = 20
self.GPIO_ECHO = 21
# Richtung der GPIO-Pins festlegen (IN / OUT)
GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(self.GPIO_ECHO, GPIO.IN)
self.motors = motors
def distanz(self):
# setze Trigger auf HIGH
GPIO.output(self.GPIO_TRIGGER, True)
# setze Trigger nach 0.01ms aus LOW
time.sleep(0.00001)
GPIO.output(self.GPIO_TRIGGER, False)
StartZeit = time.time()
StopZeit = time.time()
# speichere Startzeit
while GPIO.input(self.GPIO_ECHO) == 0:
StartZeit = time.time()
# speichere Ankunftszeit
while GPIO.input(self.GPIO_ECHO) == 1:
StopZeit = time.time()
# Zeit Differenz zwischen Start und Ankunft
TimeElapsed = StopZeit - StartZeit
# mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
# und durch 2 teilen, da hin und zurueck
distanz = (TimeElapsed * 34300) / 2
return distanz
def scan(self):
contour=[[],[]]
#Styling
color = 'lime'
plt.rcParams['text.color'] = color
plt.rcParams['axes.labelcolor'] = color
plt.rcParams['xtick.color'] = color
plt.rcParams['ytick.color'] = color
plt.rcParams['axes.edgecolor'] = 'gray'
plt.rcParams['figure.figsize'] = 5, 5
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
# 360deg are 404 steps at speed of 0.027
for i in range(101):
print("Radar-scanning... ")
contour[0].append(math.radians(i*3.564356436)) #phi Phase
contour[1].append(self.distanz()) #r Betrag
#move
for k in range(4):
self.motors.step(1, -1)
time.sleep(0.027)
self.motors.off()
contour[0].append(contour[0][0])
contour[1].append(contour[1][0])
ax.plot(contour[0], contour[1], color='lime', linewidth=3)
#Styling
ax.set_rmax(math.ceil(max(contour[1])/50.0)*50)
ax.grid(True)
ax.set_theta_zero_location("N")
ax.set_title("Radarscan der Umgebung [cm]", va='bottom')
ax.set_facecolor('black')
fig.patch.set_facecolor('black')
#Make Plot ready to send
scan = BytesIO()
fig.savefig(scan, format='png', facecolor='black')
scan.seek(0)
return scan, contour