For a robot to truly be considered a robot, it must be able to sense and affect its environment. That’s why sensors are so crucial to robotics, and understanding how to use them to make a robot smart is vital for any budding roboticist. I recently launched a Kickstarter and we’re actually giving out free sensors with every kit we sell, so check it out here before it ends!

In this Skill Builder I will take you through a robot operation called sense > think > act. It’s an operation that is widely used in robotics and is an excellent place to start understanding how to program a robot. Let’s dive in…

Choosing a sensor

2

There are as many sensors available as there are physical things to sense, but let’s use the HC-SR04 Ultrasonic Distance sensor because it’s cheap, simple, and widely used in robotics (it’s also got a very convenient Arduino library).

Defining the Robot Model

This sensor can detect distances, so let’s start with a simple robot that avoids collisions. This robot can move forwards and backwards and turn on the spot in either direction. On the front of the robot is the distance sensor. Now that we’ve defined our robot, let’s start looking at how to make it behave.

3

What is Sense, Think, Act?

Sense, Think, Act is a decision loop that can be used to solve many robotic problems, and it is remarkably simple. In this case the robot must sense if there are any obstacles in front of it. It must then think whether it can move forward, or whether it should turn or reverse, after which it will act on that decision. This same logic can apply to any robot, with any sensor for nearly any behaviour.

4To actually turn this into code that we can write to control the robot, we need to be more specific. Let’s create a simple behaviour for the robot as follows…

  1. The robot senses if any obstacle is less than 3cm in front.
  2. If there is no obstacle, it moves forward.
  3. If there is an obstacle, it moves backward.

5

This would be very easy to translate into code, however you might realise that this would result in the robot getting “stuck” oscillating when it reached a wall. It would move forward until it detected the wall, then move backwards until it didn’t, then forwards again, and so on. Let’s modify the behaviour in the following way.

  1. The robot senses if any obstacle is less than 3cm in front.
  2. If there is no obstacle, it moves forward.
  3. If there is an obstacle it turns left, then back to step 1.

This will continue until eventually the robot finds a direction that has no wall within 3cm.

6

Now we have a sense, think, act model that could actually work in helping our robot avoid obstacles. It’s not a particularly sophisticated model and we could add lots of complexity to improve obstacle avoidance.

Even with a simple robot, very complex sense, think, act models can be designed to create very smart behaviours. This is the essence of robotics — the smart’s in the software!

Our current behaviour is a bit inefficient because the robot has to turn left three times, just to turn right. Let’s modify our behaviour in the following way, as a fourth step to follow the three steps above:

4. If there is an obstacle it turns left, then back to step 1.

  1. The robot senses if any obstacle is less than 3cm in front.
  2. If there is no obstacle, it moves forward.
  3. If there is an obstacle it turns left and senses again.
  4. If there is no obstacle, it moves forward and the loop resets.
  5. If there is an obstacle, it turns to the right and senses again.
  6. If there is no obstacle, it moves forward and the loop resets.
  7. If there is an obstacle, the robot keeps turning right until there is no obstacle.

7

Now our robot will check both directions to see if there is a wall, meaning the robot will move more efficiently. Notice how even simple behaviour is starting to get more complex to illustrate, however if we think of this behaviour as a series of Sense, Think, Act loops, then it becomes much easier to understand!

And there you have it. We’ve taken a very simple case of Sense, Think, Act behaviour and fleshed it out into a working obstacle avoidance program. It would be a very simple task to translate that behaviour into code and begin playing around with robotics! And of course you can add more distance sensors, and even different types of sensors to make the behaviour even more sophisticated. Just remember to break it down into Sense, Think, Act and you’ll be able to handle it. And of course if you’re looking for a robot kit that includes tonnes of free sensors go check out our Kickstarter!