Case 8 - Collision Avoidance Simulation with Ultrasonic Sensor

Case 8 - Collision Avoidance Simulation with Ultrasonic Sensor

 

Part 1   Objective

After you compile the program, you can control the motor to stop or start based on the distance detected by the ultrasonic sensor.

28.gif

Part 2   Preparation

  • MegaPi x 1

  • MegaPi RJ25 adapter x 1

  • Ultrasonic sensor x 1

  • RJ25 cable x 1

  • MegaPi encoder/DC motor driver x 2

  • Photoelectric encoder motor 185RPM with wheel × 2

  • Encoder motor cable x 2

  • USB cable (Type-B) x 1

  • Battery holder with 6 AA batteries x 1

  • Computer with network access x 1

Install the MegaPi RJ25 adapter and the MegaPi encoder/DC motor drivers on MegaPi, connect the ultrasonic sensor to MegaPi by using the RJ25 cable, connect the encoder motors to MegaPi by using the encoder motor cables, connect the battery holder to MegaPi, connect MegaPi to the computer by using the USB cable, open mBlock 5 (PC client, web version, or mobile app), click +add, select Ultimate 2.0 from Device Library, click Connect, and select the Live mode.

29.gif
30.gif

Part 3   Programming

Collision avoidance simulation with ultrasonic sensor: Use the if ( ) then ( ) else ( ) block for judgment and "the distance between the ultrasonic sensor and an object is less than 10cm" as the condition. When the distance between the ultrasonic sensor and an object is less than 10cm, the motors stop rotating, that is, the power of the two motors becomes 0. Otherwise, the motors run at 50% power. However, the two motors are installed in opposite directions, so you need to change the power of one motor to -50%.

31.gif

Part 4   Practice

Try to stop the motors when the distance between the ultrasonic sensor and an object is less than 10cm, and then change the rotation direction of the motors to simulate turning.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.