LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > cyber:bot Roaming with the Ping)))

cyber:bot Roaming with the Ping)))

This project demonstrates how a cyber:bot can roam around and avoid objects on the right, front and left of the chassis using the Ping))) Ultrasonic Sensor and Mounting Bracket Kit. A Ping))) Ultrasonic Sensor mounted on a servo can measure distances at any interval across a 180 degree radius. This technique can be used to travel down a narrow hallway or maze, staying centered in the corridor. You may also program the cyber:bot to never get stuck since it can easily detect obstacles on the sides.
Cyber:bots are made to be modified! What if your cyber:bot could be outfitted with wheel traction devices to travel on snow, ice, and pine needles? We hot-glued some screws to the perimeter of the wheel to test this concept. The video and short instructions are the last page of this tutorial.

Before You Start

This project assumes you already have some experience with the cyber:bot tutorial series.  At a minimum, work through these first:

  • Prerequisite(s):
    • Get Started with micro:bit and Python
    • Writing micro:bit Programs
    • Add Modules to Your micro:bit
      (From the Adding a Module to the micro:bit Filesystem heading to the end of the page.  Make sure to watch the video too.)
  • Main Lessons:
    • Build Your cyber:bot
    • Navigation with the cyber:bot
    • Touch Navigation for the cyber:bot
    • Sound for the cyber:bot

What’s Needed

  • (1) – Fully assembled and tested cyber:bot (#32700)
  • (1) – PING))) Ultrasonic Sensor + Mounting Bracket, (#910-28015a)
  • (1) Optional wheel traction devices of your own concept

Any hardware (screws, locknuts, etc) we use either comes from our cyber:bot kit, the Ping))) Ultrasonic Sensor + Mounting Bracket kit, or can be easily found at any local hardware store. We typically use #4-40 screw sizes.

 

Build the Ping))) Bracket and Circuit

The Build

  • Build and test your cyber:bot, following all tutorials up to Navigation with the cyber:bot
  • Attach the Ping))) sensor and Mounting Bracket Kit to your cyber:bot, following the directions that come with the kit.
    • The Ping))) Sensor plugs into servo header pin 16 on the cyber:bot board.
    • Mounting bracket standard servo plugs into servo header 17 on the cyber:bot board.
  • Center the Ping))) Mounting bracket on the servo horn.
  • Attach the Ping))) Mounting Bracket to the servo horn.
  • Move it back and forth and verify that the approximately 180 degrees of movement occurs evenly in front of the robot (the servo has mechanical limits)
  • Reposition it as necessary before tightening the black screw.
  • Next, you’ll need to check the servo wires to make sure they are free of binding.

It’s a good idea to add a “strain relief” with a zip tie or small wire to each end of the cable. One could secure the Ping)))’s cable to the header block and another may be used to keep the cable pointed upward so it doesn’t interfere with components on the breadboard. Make sure the cable doesn’t rub against anything when the servo moves.  

 

How the Ping))) Ultrasonic Sensor Works

The Ping))) Ultrasonic Distance Sensor lets your cyber:bot detect obstacles by measuring the distance to them.

Much like a bat, the Ping))) sensor emits an ultrasonic chirp, then listens for the chirp’s echo. The time between these two events is used to calculate the distance to an object. While most objects will return an echo, be aware that size, material, and angle all make a difference, as shown in the image below.  

The Ping))) sensor measures the echo return time in terms of microseconds. Then, it uses the fact that sound travels at 0.03448 cm/µs at room temperature. That’s 3.448 hundredths of a centimeter per millionth of a second at a temperature of (22.2 ºC). Just as a car travels distance = speed x time, so does sound, with an equation of s = ct, where s is the distance, c is the speed of sound and t is the time.

Did you know that the speed of sound in air actually changes with air temperature?  This Learn reference article explains more about why this happens. Temperature extremes can actually make a slight, but notable, difference in Ping))) sensor measurements.

 

Sub-system Ping))) and Servo Testing

Program the micro:bit to Use the Ping))) Ultrasonic Sensor

This project has several subsystems that we’ll test independently before we program to work together. First, we need to check the Ping))) Ultrasonic Sensor to see that it is wired properly and produces accurate measurements.

Hardware Setup

  • Set the cyber:bot board’s power (PWR) switch to Position 0.
  • Make sure the battery holder is loaded with 5 AA batteries.
  • Make sure the battery holder’s barrel plug is firmly plugged into the cyber:bot board’s barrel jack. 
  • Connect your micro:bit module to your computer with a USB cable.

Software Setup

  • In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
  • Add two modules to the project: cyberbot.py, qti.py, and intbits.py.
    • Go to Add modules to your micro:bit.
    • Skip the Quick Start section, and instead start at the section titled Adding a Module to micro:bit Filesystem. 
    • Watch the video and then add the cyberbot.py module to the project by following the instructions from there to the end of the page. 
    • Next, follow the same steps to add the ping.py module to the project.  It will be in the same folder with cyberbot.py. 

Script: ping_test_with_serial_monitor

  • Make sure that you have completed the Hardware Setup and Software Setup tasks.
  • Set the project’s name to ping_test_with_serial_monitor, enter the script below, and then click Save.
    (See Save & Edit Scripts and Flash Scripts with Python Editor.)
  • Click Send to micro:bit.  
    (See Flash Scripts with Python Editor.)
# ping_test_with_serial_monitor

from cyberbot import *
from ping import *

bot(22).tone(2000, 300)

while True:
    distance = ping(16).distance('cm')
    print('Distance:', distance, 'cm')
    sleep(750)

The Ping))) sensor has a range of approximately 3 cm to 3 m in a controlled environment with both the sensor and a single target elevated well above the floor.  For these tests, we’ll work on a range of 10 cm to 1 m, which is more realistic for a cyber:bot roaming close to the floor with multiple objects nearby.  

  • Set the cyber:bot board’s PWR switch to position 2.
  • Click Show serial to open the serial monitor.
    (See Use the Serial Monitor.)
  • Place an object in front of the Ping))) sensor as shown.  Start at 10 cm, and try a few distances like 10, 20, 30, 40, and 50 cm.  Then try 1 m.
  • Verify that the serial monitor displays distances that match the actual object distances within a few cm.
  • Set the cyber:bot board’s PWR switch to position 0.

Theremin

Once the Ping))) is working you could easily add code for the piezospeaker and create a theremin. A theremin is an electronic musical instrument where the tone is changed by the movement of your hand between two antennas. You will substitute the Ping))) Ultrasonic Sensor for the antennas.

  • Change the project’s name from ping_test_with_serial_monitor to ping_theremin. 
  • Enter the script below, and then click Save.
  • Click Send to micro:bit.  
  • Set the cyber:bot board’s PWR switch to 2.
  • Vary the distance of an object (or your hand) from the Ping))) to create theremin-like sound effects.
  • Set the cyber:bot board’s PWR switch to 0.
# ping_theremin

from cyberbot import *
from ping import *

bot(22).tone(2000, 300)

while True:
    distance = ping(16).distance('cm')
    # print('Distance:', distance, 'cm')
    bot(22).tone((distance*50),50)

Program the micro:bit to Control the Ping))) Bracket’s Servo

The next subsystem to test is the Ping))) bracket’s servo. The Parallax Standard Servo is able to sweep 180 degrees, positioning the Ping))) at near-exact angles along the way so the micro:bit can take distance measurements on the sides and front of the cyber:bot.

  • Change the project’s name from ping_theremin to ping_servo_test. 
  • Enter the script below, and then click Save.
  • Click Send to micro:bit.  
# ping_servo_test

from cyberbot import *
from ping import *

# angles for L/R sweep scan
theta = [0, 30, 60, 90, 120, 150, 180, 165, 135, 105, 75, 45, 15]
# Offset from CW limit to actual 0 degree theta is 12 degrees
offset = 12   

index = 0
bot(22).tone(2000, 300)

while True:
    for index in range(0, 13):
        bot(17).servo_angle (theta[index] + offset)
        angle = theta[index] - 90
        display.scroll (angle)

Tests and Adjustments

The ping_servo_Test script programs the micro:bit to position the bracket’s servo through the entire range of motion, testing only the servo motor. If you find that your servo turns beyond the range shown in the picture below you will follow some instructions in this section to adjust it.

  • Set the cyber:bot board’s PWR switch to 2.
  • Compare the range of motion your servo points the ping against the diagram below.
  • If you find that your servo turns beyond the range shown in the picture:
    • Set the cyber:bot board’s PWR switch to 0 when the micro:bit starts to display 0.
    • This is the point where the script thinks the Ping))) should be pointing straight ahead.  
    • Remove the single screw that holds servo horn to the servo’s output spline.
    • Pull the Ping))) upwards and off the Ping))) Mounting Bracket.
    • Position the Ping))) so that it is pointing straight ahead, and press it back onto the servo spline.
    • Replace the screw that holds the servo horn to the servo.

 

The Ping_Servo_Test code example creates a list of values named theta, which represent the position the micro:bit will use to control the servo. The value of 0 degrees is far right and 180 degrees is far left. However, to make the next program easier to read and useful for navigation, we will create a variable named angle which calculates the straight-ahead value as 0 degrees, far right as -90 degrees and far left as 90 degrees from this line of code:

angle = theta[index] - 90

The angle value is simply 90 degrees less than the servo position controlled by the micro:bit.  The image shows how this adjustment makes it so that an angle of 0 degrees is straight ahead.  Anything on the left is a positive angle, and anything on the right is negative.  This comes in handy for navigation programming because your code can check if the object’s angle is greater or less than zero to decide which direction to turn.  

cyber:bot Ping))) Roaming Program

With the Ping))) and servo subsystems working correctly, we can combine them with motor control to make the cyber:bot roam. This program has several places where you can easily customize it for better performance in your own environment.

  • If ping_servo_test is not already open, open it with the micro:bit Python Editor.  
  • Change the project’s name to ping_servo_roaming. 
  • Enter the script below, and then click Save.
  • Click Send to micro:bit.  
  • Disconnect the USB cable from the cyber:bot robot’s micro:bit and put the cyber:bot on the floor.
  • Set the cyber:bot board’s PWR switch in position 2.
  • The cyber:bot should drive around, sweeping its Ping))) sensor from side to side, and turn away from obstacles.
# ping_servo_roaming

from cyberbot import *
from ping import *

# angles for L/R sweep scan
theta = [0, 30, 60, 90, 120, 150, 180, 165, 135, 105, 75, 45, 15]
# Offset from CW limit to actual 0 degree theta is 12 degrees
offset = 12

speed = 0
distance = 300
wL = 64
wR = 64
index = 0

bot(22).tone(2000, 300)

def turn_away(index):
    angle = theta[index] - 90

    if angle > 0:
        wL = 64
        wR = -64
    else:
        wL = -64
        wR = 64

    bot(18).servo_speed(wL)
    bot(19).servo_speed(-wR)    
    sleep(500)

while True:
    bot(17).servo_angle ( theta[index] + offset )
    sleep(150)
    distance = ping(16).distance('cm')

    #print("theta = %d, distance = %d" % (theta[index], distance))

    if distance >= 25:
        wL = 64
        wR = 64
    else:
        turn_away(index)

    bot(18).servo_speed(wL)
    bot(19).servo_speed(-wR)    

    index += 1
    if index > 12:
        index = 0
        #print("\r\r")

Try This – Improve your Code

You can easily modify the Ping_Servo_Roaming.py code for different behaviors based on your environment. For example, the turn_away(index) definition simply tests whether the close object is on the left or right of the robot:

def turn_away(index):
    angle = theta[index] - 90
    #print("angle = %d" % angle)

    if angle > 0:
        wL = 64
        wR = -64
    else:
        wL = -64
        wR = 64

More angle conditions could be evaluated by adding multiple elif statements and appropriate motor speeds. For example, if the object is on the left, it may only be necessary to make a slight correction with a right turn, rather than pivoting in place as shown above. An object detected in front of the cyber:bot may mean turn around, rather than turn away. These are all customizations you can make.  

Also, consider the distance at which objects are considered “close” together with the cyber:bot’s speed:

    if distance >= 25:
        wL = 64
        wR = 64
    else:
        turn_away(index)

The detection distance and speed need to work together. Driving fast with a short detection distance is a sure way to hit the wall!

Try different combinations of wL, wR, and threshold values for distance.

 

cyber:bot Wheels Off-road Modification

The cyber:bot wheels are available as pairs from the Parallax store. Since narrow wheels won’t work on dirt, snow or ice I made some modifications to them. Using hot glue and 1-inch long screws, I created a traction control system. This modification can easily be done within an hour.

The instructions for creating a pair of traction control wheels are:

  • Remove the wheels from the cyber:bot (or get another pair) by loosening the small black servo spline screw and pulling the wheel off the servo.
  • Remove the black O-ring from the wheel perimeter.
  • Hot glue screws into the slots around the wheel.
  • Attach the wheels to the cyber:bot using the small black servo spline screws.

The tests worked well! The cyber:bot had more flotation and traction on the snow though the wheels picked up ice and dirt along the way. A four-wheel, skid-steer robot would be more effective with these wheels. See the video highlights below!

Warning! Try this at your own risk! Always wear eye protection when working with hot glue guns. If you are trying snowy conditions, make sure your cyber:bot body and electrical components don’t get wet.

 

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/cyberbot-roaming-with-the-ping/
Links