Friday, March 13, 2015

The Bot With Kaleidosope Eyes

My first foray in to experimenting with the ubiquitous HC-SR04 ping sensor is a silly piece of code that I like to refer to as "kaleidoscope eyes". It's actually quite impressive to see when you're looking at it in person. Unfortunately my smart phone doesn't agree. I was really hoping to take a video of this, but I couldn't get the LED's to show up on the screen no matter what I did for lighting. I had a few more options with the camera, but even still this was the best I could do:


So I guess use your imagination? Basically what happens is that the rainbow pattern in the eyes changes as distance detected by the ping sensor varies.

This is not practical for much, if anything. But it is a cool demo and will probably entertain my infant child for hours once she's old enough to sit upright on her own.

Here's the code:

https://github.com/meatheadmike/kaleidoscope_ping_sensor_demo

As you can see I set up a buffer of 20 distance readings and then average the result. This makes the pattern changes a lot smoother than without the buffer. It can get quite flickery without.

An interesting observation that I came across while playing with this... Occasionally when you get too close to the sensor it would miss a reading. This would cause the code to get stuck on a while loop. So I added a simple timeout as you can see here:

while GPIO.input(PING_ECHO) == 0 and signaloff-start < 0.1:

So if 0.1 seconds elapses, then we move along. This solved the problem for me. When I implement the ping sensor for my bump-and-go functionality on Robie, this won't be a concern (hopefully). I'm planning on using interrupt logic instead of a while loop. As always, I'll post my findings...

No comments:

Post a Comment