-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultrason.py
88 lines (77 loc) · 3.14 KB
/
ultrason.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
#!/usr/bin/python
# -*- coding: cp1252 -*-
#+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|N|e|c|r|o|m|a|n|c|i|e|n|-+
#+-+-+-+-+-+-+-+-+-+-+-+-+-+
## ultrason.py
# Mesure deistance utilisant un capteur a ultrason
# Auteur : Lens Hunnel
# Date : 11/03/2014
# Importation des librairies Python requises
import time
import RPi.GPIO as GPIO
# Utilise les references GPIO BCM
# au lieu des numeros de pins
GPIO.setmode(GPIO.BCM)
# definition des GPIO que nous allons utiliser
GPIO_TRIGGER = 23
GPIO_ECHO = 24
class Ultrason(object):
def __init__(self):
self.valeur = list()
self.distance=-1;
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
print" mise à zero de trigger"
GPIO.output(GPIO_TRIGGER, False)
time.sleep(0.5)
def calculMoy(self,tab):
somme=0
moy = 0
for i in tab:
somme = somme + i
moy = somme/len(tab)
return moy
def supValmin(self,tab):
valeurMin=tab[0]
for i in tab:
if i<valeurMin:
valeurMin=i
tab.remove(valeurMin)
def supValmax(self,tab):
valeurMax=tab[0]
for i in tab:
if i>valeurMax:
valeurMax=i
tab.remove(valeurMax)
def getdistance(self):
print "Ultrasonic Measurement"
n = 0
while n < 10 :
#print "Envoie d'une impulsion de 10uS a la broche Trigger"
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
#print "Calcul de la longueur de l'impulsion de la broche Echo"
elapsed = stop-start
#print"""Distance pulse travelled in that time is time
#multiplié par la vitesse du son (cm/s)"""
self.distance = elapsed * 34000
#print "That was the distance there and back so halve the value"
self.distance = self.distance / 2
self.valeur.append(self.distance)
n=n+1
self.supValmin(self.valeur)
self.supValmax(self.valeur)
self.distance = self.calculMoy(self.valeur)
print "Distance : %.3f" % self.distance
# Reset GPIO settings
GPIO.cleanup()
return self.distance
if __name__ == "__main__":
print Ultrason().getdistance()