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. This is what you will need to do:

  • Add the ping.py module to your cyber:bot as shown in the add modules to your micro:bot tutorial. It is part of the cyberbot library.
  • Type in the Ping_Test_with_Plotter.py code:
# Ping_Test_with_Plotter.py

from cyberbot import *
from ping import *

bot(22).tone(2000, 300)

while True:
    distance = ping(16).distance('cm')
    print ((distance,))
    sleep(25)
  • Put the cyber:bot power switch in position 2 so the Ping))) Ultrasonic Sensor is powered.
  • Connect the cyber:bot to your computer with USB cable.
  • Flash the code on the micro:bit (and continue to keep the cyber:bot connected to the computer).
  • Click on the “Plotter” button and you should see a graph as shown in the following video.

 

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.

  • Try this code for fun and wave your hand in front of the Ping))) Ultrasonic Sensor:
# Ping_Theremin.py

from cyberbot import *
from ping import *

bot(22).tone(2000, 300)

while True:
    distance = ping(16).distance('cm')
    print ((distance,))
    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.

The Ping_Servo_Test.py code 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 above to look behind the cyber:bot, remove the screw which holds the Ping))) Mounting Bracket in place and reattach.

  • Run the following code.
  • If necessary, test and repeat until the servo moves the same amount right and left. The micro:bit LED display will show the angle.
# Ping_Servo_Test.py

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)

The Ping_Servo_Test.py 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.  

Calculating angles for PING))) roaming for the cyber:bot.