Obstacle avoidance

The kit also comes with a distance sensor.
By attaching it to a robot, it can detect nearby obstacles and move to avoid them.

Attachment

Let's attach the distance sensor.
First, connect the cable to the two black eye-like distance sensors.
There are three red, black and yellow wires on the cable. Connect to obniz respectively.

Connect red to obniz number 7, black to number 8, and yellow to number 9 as shown in the picture below.

Next, attach the distance sensor on the body.

There are no attaching holes on the body.Since it is a light sensor, it can be attached anywhere with double-sided tape.

Attach double-sided tape to the distance sensor and install it on the body.For example like this.

It can measure the distance from this eye-like object to the object ahead.

Distance

The distance measured from the robot using this distance sensor can be known from this "measured distance (mm)" block.

This round block is the measured distance.
If there is something 10 cm ahead of the sensor, this block will be 100.

Obstacle avoidance

Let's use the distance sensor to run without hitting.

If there is nothing nearby (if the distance value is large), go ahead and
If not, try to rotate to the right.

This way, we can go straight, turn right when something approaches, and move forward when nothing is left.

we can move.

If we use a block.
As a condition, let's say "if the distance is greater than 200".

By doing this, you can see that there is nothing up to 200 mm in front of the sensor.
Advance the robot here.

In the photo above, the sensor was attached on the side where obniz is located.
It's the opposite of when you put your smartphone down.
Therefore, turning the motor with a negative value advances to the side where the sensor is located.

And if there is something near the sensor, rotate it.

Both of them will collide if they are too early, so let's move a little slowly.

The end result is a program like this.

When you run the program, it will move by itself using the sensor. Try blocking it with your hands or walls.

関連の質問