Friday, July 8, 2016

Ultrasonic distance and a Pi...

Looking around itself... well, at least like a bat might!



I had stuff lying around, partly from old robotics experiments, partly from gifts from friends/coworkers, and partly because I bought them to build this robot.

This sensor is one of the latter - spent literally pocket change to get a 4 pin SR04 ultrasonic range finder.  It pings a series of sounds at 40k and waits to hear them back, you measure time difference and do the math knowing speed of sound (at water level etc).

I've used something similar to this in the past (back around 1998!) in a robot, so know it can work quite well, however I know a lot more now than I did then.  Also, I'm not using a stamp micro controller as a brain any more... the RPi is a ridiculous improvement in computing power.

So yea, this sensor is meant for 5v use, the pi GPIO is 3.3volt, so I built a resistor voltage divider.  I went through my bag of resistors looking for suitable color codes, found 3 (two are in series to make the lower resistance needed), and voila, my 5v output echo pin from the SR04 now returns 3.3v (3.2, really) to the PI GPIO.

I wired it up but had to go to work, so I Puttied in from the office at lunch to see if I could get a measurement.

Initial code was in python, but I am growing rapidly to despise the language so instead used node.js to do this in the code I know best - JavaScript.

I can now loop (without holding up the cpu cycles, like in python....) over a call to the sensor and get the distance....



.... from the rear of the roomba.

Why the rear?  Because I just left it sitting on a tiny breadboard (with resistor network) pointing into my living room at home while the robot is parked on it's dock, facing the wall (under my low mantel).

I wanted something other than 3cm readings constantly, figured this would be more fun.  well, it's not.  But I do have something coming back from it:


I made a little ascii bar chart to help with visuals.

that runs 10 times per second right now (I set it for that frequency).

the most common distance around 390cm makes sense... should be that far to the couch across the room.

regular half distance measurements are interesting... maybe something small on floor it's hitting, maybe edge of the throw rug is actually being returned at times (it's a shag so has some height).  Who knows.

once in a while it doesn't find anything, and I set it to say ERR and the actual number in cm (which is infinite fallback in the sensor) if that happens.  Not sure why it happens, but it does.  maybe won't happen with more close up stuff.

Now I will have to "debounce" this output in the actual robot or it will behave erratically... erm, more erratically than necessary ;-)

if I run this multiple times per second and it sees something close once then it's gone again, the robot could ignore it possibly, or maybe slow down briefly to make sure it's done before speeding up.

If it gets no result back it should treat that as acceleration to full speed but not instant jump to full speed, ya know?  in case it's reading 10cm 10cm 10cm 3380cm, it shouldn't go 5% speed 5% speeed 5% speed 110% speed instantly LoL.

So yes, that is one point to think about in my next coding exercise - I must remember to accelerate smoothly up to a speed and down to a speed, not instantly, unless it sees an obstacle suddenly that lasts more than 1 test in a row.  2 in a row?  slow down FAST.  3 in a row?  STOP!!  or something like that.

So.... the next coding exercise is.....

To put the sensor on front and make this thing start to run around my house without hitting anything, choosing random directions (for now).

- needs to move forward if nothing in it's way.
- needs to turn one way or another about 90 degrees if something in it's way.
- needs to accelerate and decelerate smoothly respecting general ideas I pointed out above
- needs to read the bump sensor in the roomba and respect that as instant stop followed by normal turn and retest code of mine.

Basic autonomy.... This will be tonight/tomorrow morning!

No comments:

Post a Comment