Este programa usa un sensor para poder registrar dos distancias y asi calcular la veclodidad deṕenidendo de el tiempo en que tarde en recorrer las dos medidas 



import time
import botbook_gpio as gpio
from turtle import *

t=Turtle()
a=Turtle()

colormode(255)

c=Screen()
c.bgcolor("lightgreen")
screen=t.getscreen()
setup(300,300,0,0)
t.speed(10)
t.hideturtle()
t.color("black")
t.pensize(720)
t.penup()
t.goto(0,-600)
t.pendown()
t.goto(0,600)

t.color("gray")
t.pensize(700)
t.penup()
t.goto(0,-600)
t.pendown()
t.goto(0,600)

t.color("black")
t.fillcolor("lightgray")
t.begin_fill()

t.pensize(10)
t.penup()
t.goto(0,-100)
t.pendown()
t.goto(-300,-100)
t.goto(-300,200)
t.goto(300,200)
t.goto(300,-100)
t.goto(0,-100)

t.end_fill()

t.pensize(30)
t.penup()
t.goto(-600,-300)
t.pendown()
t.goto(-600,210)
t.penup()
t.goto(-600,210)
t.pendown()
t.goto(600,210)
t.goto(600,-300)


t.color("black")
t.fillcolor("black")
t.begin_fill()

t.pensize(10)
t.penup()
t.goto(0,-50)
t.pendown()
t.goto(-250,-50)
t.goto(-250,150)
t.goto(250,150)
t.goto(250,-50)
t.goto(0,-50)

t.end_fill()

t.color("red")
t.penup()
t.goto(0,50)
t.pendown()



def readDistanceCm():
triggerPin = 22 # <1>
echoPin = 27

v=(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)

t = gpio.pulseInHigh(echoPin) # s

d = t*v
d = d/2
return d*100 # cm



try:
while True:
tiempo=0
while True:
d=readDistanceCm()
print( "Distance is %.2f cm" % d)

if d<=40:
tiempo=tiempo + 0.5
if d<=15:
break
velocidad=(40-15)//tiempo
print( "Velocidad es  %.2f cm/s" % velocidad)
time.sleep(1)

a.color("black")
a.fillcolor("black")
a.begin_fill()
a.pensize(10)
a.penup()
a.goto(0,-50)
a.pendown()
a.goto(-250,-50)
a.goto(-250,150)
a.goto(250,150)
a.goto(250,-50)
a.goto(0,-50)
a.end_fill()


t.penup()
t.goto(-120,10)
t.color("red")
vel1=int(velocidad)
vel=str(vel1)
t.write("" +vel+" cm/s",font=("BERTRAM LET",50,"bold italic"))
t.undo()


except KeyboardInterrupt:
GPIO.cleanup()
screen.exitonclick()




Comentarios

Entradas más populares de este blog

AREAS Y VOLUMENES DE FIGURAS EN PYTHON

Programas Unidad 3