Las siguientes líneas de código dibujan y actúan como un
radar de velocidad en pequeña escala. El sensor lee dos distancias (una inicial
y una final) entre 50 y 10 cm, es a los 50 cm cuando lee la inicial y a menos
de 10 cuando obtiene la final, al mismo tiempo contara las décimas de segundo para
obtener el tiempo. Ya teniendo las dos variables necesarias para calcular una
velocidad solo realiza la operación y muestra el resultado en la interfaz.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import time
import botbook_gpio as gpio
from turtle import *
t=Turtle()
screen=t.getscreen()
setup(720,720,0,0)
t.speed(5)
t.penup()
screen.bgcolor("gray")
t.fillcolor("white")
t.begin_fill()
t.goto(200,-200)
t.pendown()
t.goto(200,200)
t.goto(-200,200)
t.goto(-200,-200)
t.goto(200,-200)
t.end_fill()
t.fillcolor("lightgray")
t.begin_fill()
t.penup()
t.goto(-180,180)
t.pendown()
t.goto(180,180)
t.goto(180,130)
t.goto(-180,130)
t.goto(-180,180)
t.end_fill()
t.penup()
t.hideturtle()
t.goto(-80,140)
t.write("VELOCIDAD",font=("Arial",20,"bold"))
t.goto(-50,-190)
t.write("CM/S",font=("Arial",30))
t.goto(-160,120)
t.fillcolor("black")
t.begin_fill()
t.pendown()
t.goto(160,120)
t.goto(160,-130)
t.goto(-160,-130)
t.goto(-160,120)
t.end_fill()
t.penup()
t.pencolor("Red")
t.goto(-110,-120)
def readDistanceCm():
triggerPin=22
echoPin=27
v1=(331.5+0.6*20)#m/s
gpio.mode(triggerPin,"out")
gpio.mode(echoPin,"in")
gpio.interruptMode(echoPin,"both")
gpio.write(triggerPin, gpio.LOW)
time.sleep(0.5)
gpio.write(triggerPin,gpio.HIGH)
time.sleep(1/1000.0/1000.0)
gpio.write(triggerPin,gpio.LOW)
t1=gpio.pulseInHigh(echoPin)#s
d=t1*v1
d=d/2
return d*100 #cm
try:
while(True):
v=0
t2=0
d1=readDistanceCm()
if d1<=50:
print("Distancia 1 es %.2f cm"%d1)
time.sleep(0.1)
t2+=0.1
d2=readDistanceCm()
if d2<=10:
print("Distancia 2 es %.2f cm"%d2)
D=d1-d2
v=D/t2
else:
continue
if t==0:
v=0.0
print("{}".format(v))
t.write("%.2f"%v,font=("Arial",80,"bold"))
t.undo()
except KeyboardInterrupt:
screen.exitonclick
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Comentarios
Publicar un comentario