Program for Line-Following

Now that you know your QTI sensors are wired properly, it's time to build the line-following code. It is long, so let's build it a few sections at a time.  (You can also open the community project here, but you should still read what's happening below so you know what sections to enable, and how it works).

  • Save your project, then disable the repeat forever test code section with the Terminal print statements in it.
  • At the top of your project, add the blocks shown below to initialize your robot and also a set of variables.
  • Remember to choose ActivityBot or ActivityBot 360° to match the robot you have!

  • Keep the function QTIs section in place, and don't disable it. 

Instead of displaying the sensor states, we'll now use that 4-bit binary number to make navigation decisions.   The item variable plugs into a switch...case block with nine different case options, shown in the table.  The binary number determines the right and left variable values. Those variables will help adjust the robot drive values to keep your ActivityBot on the black line.

binary   left = right = behavior
1000 16 32 pivot left
1100 32 48 sharp left
1110 48 64 left
0100 64 64 slight left
0110 64 64 forward
0010 64 64 slight right
0111 64 48 right
0011 48 32 sharp right
0001 32 16 pivot right
  • Add a repeat forever block for the driving routine.
  • First thing inside the loop, add a run function "QTIs" block.
  • Then, add a switch...case block with nine cases. Use item for the variable.
  • Fill in each case using the binary values and the values for the left and right variables shown in the table above, and the screencapture below.

  • The switch...case statement has nine options, but a 4-bit binary number can go up to 16.  We'll need to add default code that will run if item is not equal to one of the defined nine cases.
  • In the switch...case block's default section, add the if...do...else if... code shown below. Comparing leftPrev and rightPrev gives a hint of where the line was on the last trip through the loop, so it can now choose an appropriate default behavior.

  • Add these next blocks below the switch...case block, but still inside the repeat forever loop.

Here, the values of left and right from the switch...case block factor into the driveRight and driveLeft variables that actually set each wheel's speed. By adjusting the initial value of scale at the top of the code. you can fine-tune the robot's performance.  Finally, just before the pause block, the values of left and right are saved in leftPrev and rightPrev before the loop repeats.

  • Save your project, and load the code to your robot's EEPROM.
  • Place your robot on its track and put the power switch in position 2. Your ActivityBot should follow the black line.

Fine Tuning Performance

There are several places you can make adjustments to the code to fine-tune your robot's line-following performance.  Use Save As to make copies of your project before experimenting with the adjustments.

  • Try adjusting the initial value of scale and reloading the code to see how it affects the robot's behavior.
  • Try adjusting the ticks per second in the robot set acceleration for speed block.

If you take a look at the values assigned to the left and right variables for each case, you can see a pattern.

  • Try adjusting the pattern of values for the left and right variables, such as starting at 0 instead of 16, or gradually increasing up to 96 instead of 64.