Raspberry Pi Asked by Rakesh Wadbudhe on February 8, 2021
I am using Ultrasonic sensor to get reading and am using python program for getting reading. And am using Raspberry pi for this.
when am running python program independently its giving me reading continuously because am calling get_ultra function of program in while loop.
But when am calling get_ultra function in python tornado websocket server using threading which call get_ultra function in while loop ,its stop giving reading after giving some readings.
Here is my ultrasonic.py file code:
#!/usr/bin/python
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
def get_ultra():
global componentconfig
GPIO_TRIGGER = 26
GPIO_ECHO = 26
GPIO.setwarnings(False)
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)
GPIO.output(GPIO_TRIGGER, False)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
GPIO.setup(GPIO_ECHO,GPIO.IN)
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
elapsed = stop - start
distance = elapsed * 34300
distance = distance / 2
distance1 = "%.2f" % distance
print "ultra sonic distance" , distance1
if __name__ == '__main__':
while 1:
get_ultra()
time.sleep(1)
here is my python tornado websocket server:
import time
import thread
import traceback
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import ultrasonic
def Ultrasonic(T_name,a):
while 1:
ultrasonic.get_ultra()
time.sleep(0.0038)
class ServerHandler(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print"Connection open"
def on_close(self):
print"Connection close"
def on_message(self,message):
print message
if message=="start":
t12=threading.Thread(target=Ultrasonic,args('ultrasonic', 0 ))
t12.setDaemon(True)
t12.start()
if __name__=="__main__":
tornado.options.parse_command_line()
app=tornado.web.Application(handlers=[(r"/",ServerHandler)])
server=tornado.httpserver.HTTPServer(app)
app.listen(9001)
tornado.ioloop.IOLoop.instance().start()
Note:-With “t12” thread am calling ten another thread which i have not mention here.I think may be memory leak problem is there .
This seems to be based on the same piece of depressingly bad code published on site after site. It was written by someone who has no desire to be a software engineer.
These ultrasonic rangers work by waiting for a greater than 10 µs trigger. They then send a series of high pitched chirps. They then raise the echo line (about 400 µs after the original trigger from memory) and wait for an echo to the chirp. When the echo is returned the echo line is lowered (some devices also lower the echo line after a timeout period).
I have indicated in the comments why and how the code may fail. I suggest you search online for better source code.
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
GPIO.setup(GPIO_ECHO,GPIO.IN)
# If a reschedule occurs or the object is very close
# the echo may already have been received in which case
# the following will loop continuously.
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
# if an object is not detected some devices do not
# lower the echo line in which case the following will
# loop continuously.
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
Answered by joan on February 8, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP