Distance Sensor readings integration to YetiWeb.py
Forums:
Hello,
---project little rover running Zeroborg with added distance sensor----
I would like to add an ultra-sonic sensor for simple continuous readings of distance to obstacle.
The idea is to show the sensor readings from the sensor using the web server interface YetiWeb.py
I need guidance to implement this idea. What would be the most pythonic/effective way to go about it?
So far, I have tried to merge both stand alone python scripts in attachment unsuccessfully.
My attempt of creating a separate thread in YetiWeb.py to do the job fell flat as I cannot combine both my Threading script with Distance script into one working script.
Maybe using an already existing thread in YetiWeb.py is the solution... or not using any threads at all could do as well.
Really not sure what type of object I should use to be honest.
Would appreciate if I could be pointed into the right direction.
Thanks -Marc
piborg
Fri, 04/21/2017 - 17:33
Permalink
Threaded distance reading
I have had a look at your code and tried to combine the two into a single threaded script.
At the bottom it has a little loop which gets the reading and prints it out.
If that works fine it should be something that you can merge into the
YetiWeb.py
script :)Attached as a file below.
marc.eymard
Tue, 04/25/2017 - 13:21
Permalink
Thanks for having a go at it.
Thanks for having a go at it. Really appreciate since I am so unfamiliar with threading objects.
Good effort and that is much better than what I came up with scope-wise!
Unfortunately, there is still something wrong with, I believe, the scoping of variable distance.
I spent a little while tweaking the script but cannot make it run. I will have to read the documentation for further understanding of the Thread object.
The way I would approach it: variable distance should be an attribute of the Sensor object. Distance can be read be invoking a method to get() the changing distance of the sensor object. I would get rid of global distance and try to fully encapsulate distance within sensor/thread object.
what do you think?
piborg
Tue, 04/25/2017 - 15:25
Permalink
That sounds good
That would be a cleaner way of doing things, particularly if you are planning to add more sensors later :)
marc.eymard
Mon, 05/01/2017 - 12:27
Permalink
There you go! I went for a
There you go! I went for a thread base class, which is probably not the most robust/pythonesc implementation of all but it works (accessing an object's attribute directly is not recommended)
Note the GPIOZero library already provides supports for the ultrasonic sensor
marc.eymard
Tue, 05/02/2017 - 13:44
Permalink
Integrating Sensor object in HTML - yetiWeb.py
Hello - I am yet to integrate the continuous reading value of the sensor to the web page frame shape. I removed the motors percentage reading and would like to display a continuous distance-to-obstacle info instead.
From my understanding, some syntax along the lines of....
....should be added to the following part of yetiWeb.py:
Any help on getting the syntax corrected would be very much appreciated
Thanks,
Marc
piborg
Tue, 05/02/2017 - 16:42
Permalink
Sensor readings in yetiWeb.py HTML
You are basically correct with the syntax for the line, but the
"
symbols are not really needed:Getting the reading to keep refreshing is a little more difficult. First I would add a new section which returns the values only:
You can check this works by changing the page URL to end with
/distance
and check you get a single reading.The stream section does not need updating but the
/
section does. What we need to do is:refreshDistance
)iframe
to display the resultThis gives us something like:
Lines 21 to 23 are the new function, line 24 calls this function every 200 ms, and line 29 is the new display. Everything else is as the standard example.
marc.eymard
Thu, 05/04/2017 - 13:54
Permalink
html crash course
Thanks a lot for helping with the HTML. It is the very last hurdle in my project and I think all is now provided and explained to complete the distance reading integration to the frame.
I have had a go at it but couldn't avoid some issues on the main "/" address. The "/distance" directory worked fine and showed a single reading as expected.
This deserve more time to check and test. I need to put everything together again from a fresh yetiWeb.py file and test again to understand whether there is something missing or not.
Bear with me until I thoroughly take it from scratch integrating the sensor object + new frame refresh.
I will be back in touch next week.
Marc
marc.eymard
Mon, 06/19/2017 - 13:29
Permalink
Sensor Thread timing out
Hello there
The integration of the sensor reading thread works only for a very short while before stopping. I believe a watchdog to reset the thread whenever it times out is necessary to achieve continuous reading when integrated to yetiWeb.py
Could the already existing Watchdog class be used to take care of my sensor thread timing out?
Any other implementation suggestion is welcome.
Marc
piborg
Mon, 06/19/2017 - 14:23
Permalink
Thread timing out
It is possible that a copy of the current watchdog thread which does something else on a shorter time out might work.
The question is what is happening to cause the sensor thread to time out in the first place? If you can fix that then a watchdog is probably not needed.
Does the camera image also seem to stop, or is it just the readings?
marc.eymard
Tue, 06/20/2017 - 13:02
Permalink
The camera keeps running.
The camera keeps running. Only the distance reading is stopping. Agreed, I need to further test to understand the root cause. I shall report back once I made progress with debugging.
ps: I tried the GPIO Zero library sensor class. From what I could see, it is not working well at all for now.
piborg
Tue, 06/20/2017 - 14:34
Permalink
Interesting
That is interesting, it suggests the problem is more likely to be around the thread, rather than the HTML code. Let us know what you find out, more heads are better when it come to debugging these type of problems :)
I am a little surprised about the gpiozero DistanceSensor class as it looks like it should be written with at this type of use in mind from a quick glance.