Reacting to Objects

Using distance sensor to teach our robot how to react to objects in his way.

Distance sensor is one of the most popular electronic components used in robotics. It’s highly useful and also a bit “magical”, because at first sight, you can’t tell how exactly it works.

The distance sensor that we use is the popular Arduino HC-SR04. It works based on ultrasound and echolocation. The sensor has two probes (that look like robot eyes in our case): one is a speaker that sends series of short “beeps”, the other is a microphone that records ultrasound signals bouncing from objects in front of the sensor.

czujnik_odleglosci

We can’t hear the sounds sent by the sensor, because their frequency is higher than the spectrum of human hearing. The “beeps” frequency is 40 kHz while people can hear sounds up to 20 kHz.

Because the speed of sound is constant (1235 km/h or 767 mph), the sensor can measure distance by measuring the time from sending a “beep” to receiving it back, bounced from an object. That’s how sonars on ships operate. A similar mechanism can be also observed in nature with bats, dolphins and other cetaceans.

Distance sensor will allow our robot to “see” what’s in front of him. We can use the sensor to program algorithms for reaction to signals from the distance sensor.

Preparations

  1. You will need the Rover.
  2. Connect the Rover with LOFI Blocks.

Programming

In order to read the distance measured by the distance sensor in LOFI Blocks, we will use the DISTANCE SENSOR block from ROBOT category.

LOFI_Robot_reacting_to_objects (1)

The block returns distance measured in centimeters, and if the sensor doesn’t sense any obstacle, the block returns value of 100. Before programming, let’s see if our distance sensor works properly. Put your hand close to the sensor, and if the sensor monitor reacts by moving the slider, that means the sensor works fine.

LOFI_Robot_reacting_to_objects

Excercise 1 – Move when you see something.

This program will instruct the robot to move forward when it sees something close enough, and stay in place when there’s nothing close. Let’s assume that we want our robot to react when there’s something closer than 20 cm. That means the measurement from the sensor needs to be less than 20.

LOFI_Robot_reacting_to_objects

And…

  1. If the reading from the sensor is less than 20, go forward.
  2. IF the reading from the sensor is more than 20, stay.

We will use a conditional instruction to translate all of the above into instructions:

LOFI_Robot_reacting_to_objects

Now if we play our program, the robot will move forward if we put our hand in front of him closer than 20 cm, otherwise, it will stop.

Excercise 2 – Move if you see something. Move back when you are too close.

Let’s upgrade our program, and instruct our robot to be able to get close to an object without running into it, and even move back when the distance is too close.

Breaking down the idea behind the program, the instructions will go like this:

  1. If you don’t see anything, stay in place.
  2. If you see something closer than 30 cm and more than 20 cm, go forward.
  3. If your distance is less than 20 cm and more than 10 cm, stop.
  4. If your distance is less than 10 cm, move back.

These double conditions can be programmed with a special block from the LOGICS category.

LOFI_Robot_reacting_to_objects

This block will return value TRUE only when both its conditions will be met.

LOFI_Robot_reacting_to_objects

Let’s go ahead and put the new instructions into our program.

LOFI_Robot_reacting_to_objects

So now our robot will behave like this:


Notice. If your robot doesn’t react, check if the power set for the motors isn’t too low. We recommend setting the power higher than 55.

As you can see, with the help of our distance sensor, we can write algorithms that make our robots behave similarly to living organisms. In the next lesson, we will upgrade our program even more and teach our robot to be more autonomous.