I’ve discussed driving a stepper motor and ultrasonic distance sensor in previous posts. In this one, I’ll bring it all together and outline the Scratch based ultrasonic room scanner.
The main scratch program is fairly simple in operation, but does have a couple of tricky bits.
The main loop is entered when an icon (sprite in Scratch land) is clicked. The code is shown here.
This routine just counts the angle and while it’s below 260 units (180 degrees) it steps the motor, then takes a measurement. I’ve used broadcast and wait to call subroutines. Stepright is covered in an earlier blog entry, plot is shown below.
This routine is attached to a very small black dot sprite. Make sure the dot is in the centre of the sprite or the output looks really odd – took a while to figure out what was going on with that. So, this routine places the sprite in the centre of the canvas, at 0,0. It then sets the sprite rotation to match the angle of the stepper motor. The weird calculation there changes the 260 steps from the motor to the matching 180 degrees for the image, then subtracts 90 degrees so the left of the stepper range is left on the image. The 1 second wait is because the movement sometimes causes the sensor to misbehave. After calling measure with a broadcast and wait, the sprite moves forwards by the measured distance. As the sprite has already been rotated to the direction of the stepper, this moves from the centre to the position of the detected object. Stamp leaves an image of the sprite in it’s current position so the sprite can be re-used for the next measurement.
The output isn’t brilliant.
It does generally look about right though. There’s a TV to the left and the fireplace would appear just to the left of centre, a bit further back than the tv. Once it’s passed half way, there are a lot of random objects such as tables. I think these give unreliable echoes so only sometimes show up (like the 2 to 3 oclock area) The almost solid line around the 12 to 3 oclock angles is the maximum distance measurement. I guess I could suppress these to give a nicer image but didn’t think of that at the time.
Another problem with this is that the sensor has a 10 degree field of vision, everything is spread across at least that distance.
I think the image would also look better if the dots were translucent so more dots in the same place would give a darker area.
If I ever come back to this and make any improvements I’ll blog them here but I have a habit of losing interest and moving on, so don’t hold your breath.
One Reply to “Scratch Ultrasound Room Scanner”
Cheers, great stuff, I enjoying.