Dead Reckoning and object avoidance - source

New user, first time poster, I received a Sparki for Christmas, and I’ve been hacking away for most of a week.

My first project is a mash-up of object avoidance, random walk, and dead reckoning navigation.

Github source:

Place Sparki on a flat floor surrounded by good echo-location obstacles (with New Year’s Eve behind us, I used liquor bottles). Switch to “On” and after boot, Sparki lights the LED green to indicate he is in “atHome” state.

Press “1” on the IR remote, and the LED turns blue to indicate Sparki is in “exploring” state. Sparki repeatedly chooses a random heading in a 140 degree arc ahead of him, evaluates it for obstacles, and if it’s clear travels at that heading for up to 40 cm.

At any time, press “2” on the IR remote - the LED turns red, and Sparki works to return back to the spot where he started by checking if there’s a clear path to his origin, and if not, selecting some random heading and distance to try again.

When Sparki succeeds in finding the origin of his exploration, his LED lights green and he stays put.

At any time, press “3” on the remote to stop Sparki and set his current location as his new “home” or origin.

Things I learned:
[ul][li]The ultrasonic distance sensor reads -1 pretty often, and the program works better when Sparki takes multiple readings in a 20 degree arc and selects the most conservative reading.[/li]
[li]The moveForward(float cm) command is unreliable and will occasionally result in Sparki running off on his path forever, or until he collides with something - I replaced this with stepForward(unsigned long steps) to solve this issue.[/li][/ul]

How to make it fail:
[ul][li]Run it on carpet[/li]
[li]Use obstacles that don’t reflect well, or reflect at an angle[/li]
[li]Use obstacles that are very narrow, extremely low, or don’t present at the height of Sparki’s sensors[/li][/ul]

Cylinders would appear to be the perfect obstacle for Sparki to detect, especially metal cans.

Wow, this is pretty impressive! This is probably the most comprehensive automatic explorer I’ve seen so far. Do you mind if we post a tutorial about it? Do you have any other write-ups?

Well thanks. I don’t mind at all. I don’t have any additional material yet, but my curiosity for what I can do with my Sparki is nowhere near satisfied. :wink:

I spent a little time today working on a flow chart to explain the loop behavior, and as I thought about it, I had some ideas for how to make the program behave more smoothly. At present, it pre-calculates it’s “x/y” position before each “leg” of its random walk is completed - that leaves it open to being disrupted a great deal by hitting an obstacle it missed in the preceding scan, or that was “introduced” after the scan (as my son delights in doing). There’s plenty of room for improvement.

I plan to continue updating the repository with more explanatory documentation, better comments in the source, and exploration of different ideas for calculating Sparki’s location closer to the motor step level instead of once for every “walk”. If there’s a particular area you’d like me to focus on that would be most helpful for adaptation of this code to a tutorial, I would be happy to address that first. I’m a serious supporter of technical education opportunities for children and adults, and I’m very impressed with what you’ve assembled. I would be happy to help in any way that you need.

Ideally there would be a write-up similar to how we do the lessons on our site. We have a style guide if you’d like some guidance: … sp=sharing

If not that’s okay too, you’ve done some great work already!

I’m on it. Might take a week or two. I’ll keep you apprised.

Version 0.4 is up: … s/tag/v0.4

It features a number of improvements:
[ul][li]User can select a “maxDistance” (in cm) using the IR remote that Sparki will roam from his origin. This establishes a virtual “fence” to keep Sparki from running over rugs, falling off cliffs, defecting to Cuba, etc.[/li]
[li]Sparki now checks the clearance in front of him AS HE IS MOVING! No more blind dash after a single clearance check, no more getting fooled by pranksters placing objects in his path.[/li]
[li]Useful readout of operating info on the LCD screen: Sparki displays the variables the user can change with the remote, including the maxDistance (see virtual fence above) and the number of steps he’s allowed to travel between sensor readings. Also displayed are Sparki’s X and Y coordinates from origin (in cm), Sparki’s heading in degrees, and the number of motor steps left in Sparki’s present move command.[/li][/ul]

All in all, this version has Sparki behaving more like a real autonomous robot than a frightened, slightly stupefied rodent (v0.1).