Home > AreaBot, My Robots, RobotC Code > AreaRover – Adding Wall Following with the Lego Ultrasonic Sensor

AreaRover – Adding Wall Following with the Lego Ultrasonic Sensor

Building on our behavior-based programming concepts

I thought I better start building more robots to supportTFR_20090624_0340 my articles on behavior-based programming.  The world loves examples and there are very few in this area, if any for the Mindstorms robots.  This robot is a natural progression from the BumperBot.  It is called AreaRover (clever names!).

AreaRover’s job is to bounce around a closed area and cover as much ground as possible in the least amount of time.  In order to accomplish this task, I started with the BumperBot, and added a Lego Ultrasonic sensor on the left side of the robot facing sideways.  I then added a new behavior that triggers if a wall is in range helping the robot to follow walls for a period of time.

Download Program: bbAreaRover.zip
See the Video:


The strategy is to allow the robot to cruise around and bounce off walls most of the time, but follow them when the opportunity arises.  This gives the robot a good chance at covering most of the floor.  The wall following behavior improves on BumperBots concept in a few ways.

  • Bouncing robots seem to hit wall, turn away and go on.  The area around the walls only gets touched at the impact point.  By following a wall, the robot skirts around walls covering this ground.
  • As the room (arena) becomes more complex (ie-furniture, doorways and obstacles are placed inside or the area is odd-shaped) the bouncing robot tends to spend time in the big, open areas, skipping difficult areas.  By adding wall following, the robot can traverse doors and enter small areas.  It can go around obstacles, and furniture to get in hard-to-reach places.


The WALL_FOLLOW behavior in this case hinges on the Lego Ultrasonic sensor.  I wanted to implement a servo (continuous feedback) behavior that uses a target distance (D) from the wall and provides steering values based on the difference between the measured (d) and target distance. steering=g(D-d) where g is a gain multiplier to give more scale to the steering values.  If we are closer than the target distance and we turn away from the wall.  if we are farther away, we turn toward the wall.

AreaRover-Behavior Diagram

AreaRover-Behavior Diagram

I ended up implementing a BANG-BANG controller that is a simple FINITE STATE MACHINE (FSM) with two states (START, FOLLOWING).  Bang-bang controllers are either on or off.  The reason for this decision was initial triggering.

  • I wanted to behavior to monitor the distance (d) until it was less then the target distance (D) before following a wall.
  • I also wanted to implement a timeout so the robot would only follow for a time before giving up on the behavior.  This is implemented with a constant reference timeout and a random factor that adds or subtracts time.

If you review the code, you will see the above.
I also implemented code to read the UL sensor value and smooth the results out and discard obviously incorrect readings to provide more meaningful output.


The above strategy does in fact work!  check out my new video for AreaRover.

Here are couple of other observations of interest.  I don’t see these as good or bad, but interesting emerged behavior as the robot interprets its surroundings and takes action.  Do you see these (and other) observations in the video?

A couple of things I learned from this experience are:
* Because the sensor only detects walls from the side, the robot needs to be traveling parallel to a wall in order to detect it.  In other words, there is a lower inherient chance to follow a wall under normal circumstances.

  • Because of the hierarchy of BUMP_ESCAPE, when AreaRover is following a wall into a corner and hits the wall ahead, BUMP_ESCAPE takes control and spins the robot away from the wall and back into cruise mode.
  • Wall following needs to be tuned to the physical robot.  If the turn is too tight, following a straight wall still works, but when going around a corner, the robot bumps the wall and escapes.  If the turn is too loose (bigger radius), the robot may not follow a wall very well.
  • The robot follows around outside corners very easily.  As the robot looses the wall around the corner, it arcs that direction to find it.
  • The robot does not follow inside corners at all.  The robot follows the wall into  an inside corner, bumps the wall ahead and escapes.
  • By adjusting the timeout parameters, you can effectively allow the robot to get bored after following a wall for a while.  This allows other behaviors to function and give the robot a better chance to cover a area of a room.
  • Sometimes I watched the robot thrash back and forth for a few seconds in a strange open space.  I wonder if the UL sensor is picking up stray reflections from the arena or if I maybe I missed something in the code.


  • Is there a way to follow an inside corner?  I think so…
  • How can we increase our odds of following a wall and smooth out the movements?Stay tuned.
  1. June 26, 2009 at 12:28 am

    I’m currently using your framework to create a ball-seeking and retrieving robot. Your framework makes it easy to add new ones and even exchange behaviours with other people since they’re kept in separate files. This makes collaboration much easier. I would love to see an example of a ballistic behaviour since I am going to need one of those very soon and would like to see how you’d go about setting it up.

  2. Lior Sabag
    January 16, 2013 at 9:47 pm

    Hello, I know this is quite old, but it looks like this code is exactly what I’ve been looking for. Is there anyway you could host it on another site/send it to me? (link doesn’t work)

    • Thom Roach
      January 17, 2013 at 6:16 am

      Sorry about the broken link. I moved all of the code to another service and did not do a great job fixing the articles.
      here is a page with all of my code correctly linked (I think). There are several samples of the area robot here.


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: