LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > QTI Line Follower for cyber:bot

QTI Line Follower for cyber:bot

This tutorial shows you how to add an array of four QTI sensors to your cyber:bot. These sensors can differentiate between dark and light surfaces. With a Block program, your cyber:bot robot can use the QTI sensors to follow a black electrical tape line on a light background. This tutorial provides a basic program you can use on a simple track with the cyber:bot’s standard Continuous Rotation Servos. The same code example could also be used with the High Speed Continuous Rotation Servo.

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 MakeCode
    • Making micro:bit Programs
    • Add Extensions to Your micro:bit
      (From the Adding a Module to the micro:bit Filesystem to the end of the page. Make sure to watch the video too.)
  • Main Lessons:
    • Build Your cyber:bot
    • Navigation with the cyber:bot
    • Sound for the cyber:bot
    • Circuits on the cyber:bot
    • Touch Navigation for the cyber:bot

What’s Needed

  • A fully built and tested cyber:bot (#32700)
  • A QTI Line Follower AppKit (#28108)
  • A 3/4 inch black line on a white surface.

Track Options

  1. Print out this line-following tracks PDF on an inkjet or laser printer and copy paper (NOT with photo printing paper and ink); or
  2. Make a track on white poster board, foam core board, or a smooth, light floor with black electrical tape (the vinyl variety works well). This is the preferred option because it provides so much flexibility in configuration.

After You Finish

You will be ready to combine QTI line following with other sensors for more advanced projects which can be done individually or in the classroom. Some of the ideas include:

  • Upgrading the cyber:bot to use the High Speed Continuous Rotation Servos (#900-00025) to move twice as fast
  • Adding breaks in the line, ramps, or obstacles which may only be detected with the Ping))) Ultrasonic Sensor (#28015) or Ping))) Ultrasonic Laser (#28041)

QTI Installation and Wiring

Each QTI sensor is connected to a post that is mounted on the underside of the chassis. This positions the sensors right above the ground surface.

  • Assemble the QTI sensors and mount them to the underside of the cyber:bot chassis:

Each QTI sensor needs to connect to 5 V power, a cyber:bot I/O pin, and ground. For this, you will use the four 3-pin headers from the kit to build ports on the breadboard for each sensor.

  • Build the header circuits as shown in the schematic and wiring diagram below. The 3-pin headers and jumper wires are in your QTI Line Follower kit.

  • Plug a 3-pin extension cable into each QTI sensor. Match the color of the wires to the B R W (black, red, white) labels on the sensor.

  • If you don’t already have it in place, install your piezospeaker into the dedicated P22 spot on your board. You’ll need it for this project.

  • Connect the extension cable from each QTI sensor and to its header circuit on the breadboard. Match the color of the wires to the diagram below. Take your time – getting this correct is important!

Testing Your cyber:bot QTIs

Test the QTI Sensors Using Micro:bit’s LEDs

You will be adding a second module to your micro:bit project for the QTI

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 browser, go to makecode.microbit.org to open the micro:bit Block Editor.
  • Add three modules to the project: cyberbot.py, qti.py, and intbits.py.
    • Go to Add extensions 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 qti.py module to the project. It will be in the same folder with cyberbot.py. 
    • Repeat the steps one more time to add the qti.py module to the project. It will also be in the same folder with cyberbot.py. 

Project: QTI_Display_Detections

  • Set the name to QTI_Display_Detections, enter the project below.
    (See Save & Edit Projects and Flash Projects with MakeCode Editor.)
  • Click Download. 
    (See Flash Projects with MakeCode Editor.)

  • Set the cyber:bot board’s PWR switch to position 2.
  • Position your robot over the black line on your track. The QTI sensors directly over the line will cause the corresponding column of micro:bit LEDs to illuminate.
  • Move the robot around slightly to test each QTI sensor Place each QTI sensor over black and then white to verify that the LEDs turn on and off, as shown in the video below.

If the sensors don’t see the line, try the following adjustments:

  • Double-check all of your wiring connections to make sure the sensors are properly connected to power, ground, and each I/O pin.
  • Try replacing the jumper wire connecting to 5 V with a 220 ohm resistor. This makes the QTIs less bright, which might be overwhelming with a shiny line or shiny paper.
  • Try removing the nylon spacers from one of the sensor’s posts. If this improves performance, remove it from the rest of the posts.

IMPORTANT: Do not continue until all four QTIs are working properly as shown above. This might take some troubleshooting of errors made while connecting the QTIs as well as some adjusting of the spacers. It will also be important that the 5×5 LED display jumps by one column when the black stripe is moved from one QTI to the next. If it instead skips back and forth, it might mean that the QTI cables are swapped -not plugged into the correct QTIs. Set the cyber:bot board’s PWR switch to 0 if you adjust the circuit.

  • Set the PWR switch to 0 until you are ready to rerun this project, or until you are ready to run the next project.

How It Works – Display QTI States with LEDs

This statement copies line detection information for all four QTI sensors into a single int variable:

The qti read start (Pin7) end (Pin4) call returns the states of QTI sensors connected to sockets P7 through P4. These states are stored as binary 1 and 0 digits in the int variable named pattern. If a digit is 1, it indicates a black or non-reflective surface. If it’s 0, it indicates a white or reflective surface. 

Suppose the two middle QTI sensors are over a black line so that they are both 1’s. The pattern variable’s value would be 0b0110.

After storing the QTI states, a loop uses the qti module’s check_bit method to successively check binary digits in pattern and store them in the z variable. The for x in range(0, 4) loop starts at the rightmost binary digit in pattern and works its way left by increasing the x variable’s value with each repetition: 0, 1, 2, 3. As an argument in the check_bit method, x selects which binary digit in pattern to return, and that binary 1/0 return value gets stored in the z int variable.

With the example 0b0110, this is how z evaluates at each loop repetition:

A nested loop turns all micro:bit LEDs in certain columns on or off to indicate which QTIs are over a black or white surface:

The third argument in plot x (x) y (y) brightness (z * 255) sets the brightness (from 0 to 9). If z is 0, then the brightness is z * 9 = 0 (off). If z is 1 then the brightness of z * 9 = 9 (max brightness). Instead of just one LED, the for y in range (0,5) loop makes plot set all LEDs in a given x column to on or off in response to a z value of 1 or 0. For more info on display, try the examples in the LED Matrix tutorial.

Block Program for Line Following

Basic Line Following with cyber:bot

How many possible combinations of QTI sensor states exist from pattern? The values could range from 0b0000 (0) to 0b1111 (8+4+2+1=15) for a total of 16 different number combinations!

Is it likely that pattern would be 0b1001 where the two outside QTI sensors are seeing black? Not likely since they’re 1-½” apart and electrical tape is less than ¾” wide. Rather than check for every possible 16 cases, choose the ones which are most likely. This saves memory and creates a faster-running program. The example program has eight possibilities shown and described in the following 8 images. 

0b1000, Sharp left turn

0b1100, Medium left turn

0b0100, Gentle left turn

0b0110, Straight ahead full speed

0b0010, Gentle right turn

0b0011, Medium right turn

0b0001, Sharp right turn

0b0000, Backup, turn, rotate, make a sound – or do the same command previously executed?

The cyber:bot Forward and Backward tutorial showed how the right motor must turn the opposite direction for the robot to go forward, using negative values. For example:

Note that the example QTI_Follow_Line code uses all positive values. How is that possible? At the end of the program, the wR values are made opposite, allowing the use of all positive numbers as arguments.

This makes it a little bit easier to come up with drive speed values in the example program. The values work well with a gentle, curvy line formed with electrical tape. The combinations of motor speeds you use will vary based on the line you create. 

Project: QTI_Follow_Line

  • If it’s not still open, use the micro:bit Makecode Editor to Open QTI_Display_Detections.
  • Set the name to QTI_Follow_Line, update the project so that it matches the one below.
  • Click Download. 
  • Set the PWR switch to 2, and test the cyber:bot on your track.
  • Depending on the difficulty of the track, you might need to slow down or speed up some of the project’s maneuvers. There is also plenty of room for customizations to improve the cyber:bot robot’s QTI roaming performance on various tracks.
  • Remember to set the PWR switch to 0 when you are not running the cyber:bot. If you are done for the day, also unplug the battery pack barrel plug from the cyber:bot board’s barrel jack.

High-speed Line Following with cyber:bot

If you are competing in a line following contest, you can speed up your cyber:bot two different ways.

  1. First, consider moving the power jumper from Vdd to Vin.
  2. Next, you could upgrade your cyber:bot with the Parallax Feedback 360° High Speed Servos.

DISCUSSION FORUMS | PARALLAX INC. STORE

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


Source URL:https://learn.parallax.com/courses/qti-line-follower-for-cyberbot_makecode/
Links