10. Case 07: The Obstacle Avoidance Car#

10.1. Introduction#

Make Cutebot drive forward and turn automatically when it meets an obstacle.

10.2. Hardware Installation#

Insert the ultrasonic sensor into the connector of Sonar. **Note: When you install the ultrasonic sensor, you cannot plug it into the IIC interface. If you plug it into the IIC interface, the Cutebot won’t work and the ultrasonic sensor heats up. ** ../../_images/pico-cutebot-case-07-01.png

10.3. Programming Preparation#

Please refer to: Preparing Programming Environment

10.4. Sample code#

from cutebot import *
import time

# Create a sample for Cutebot category
cutebot = Cutebot()    

# While true, keep reading the returned value detected from the ultrasonic sound sensor. 
# When there is no obstacle in front of the Cutebot, the Cutebot smart car moves forward at 50% speed
# When there is an obstacle in front of the Cutebot smart car, set the left wheel speed to 50% and the right wheel speed to -50%
while True:
    distance = cutebot.get_distance(Unit.cm)
    if distance > 3 and distance < 20:
        cutebot.set_speed(50,-50)
        time.sleep(0.5)
    else:
        cutebot.set_speed(50,50)

Code details#

  1. Import the modules that we need for the program: cutebot module contains classes and functions that operate on the Cutebot smart car, and time module contains classes and functions that operate on time.

from cutebot import *
import time
  1. Create a sample for Cutebot category.

cutebot = Cutebot()
  1. While true, keep reading the returned value detected from the ultrasonic sound sensor. When there is no obstacle in front of the Cutebot, the Cutebot smart car moves forward at 50% speed.

    When there is an obstacle in front of the Cutebot smart car, set the left wheel speed to 50% and the right wheel speed to -50%.

while True:
    distance = cutebot.get_distance(Unit.cm)
    if distance > 3 and distance < 20:
        cutebot.set_speed(50,-50)
        time.sleep(0.5)
    else:
        cutebot.set_speed(50,50)

10.5. Results#

After turning on the power, the Cutebot travels on a random trajectory and automatically lights up the turn signal according to the direction the smart car is traveling.

10.6. Exploration#

When the speed of the left and right wheels is the same, how can we program the two headlights of the Cutebot smart car to light up at the same time?