MBC Pathfinder 8-Way (Line8) × Pybricks

Read the Pathfinder in MicroPython in three steps

A Pybricks program always goes in this order. First import the library. Then create an object and give the sensor a name. Last, use that name to call a function. Follow the steps on the left, and the program on the right grows with you. Drag the black line under the sensor simulator to see what the program prints.

Your hub:
1

Import the library

The first thing your program does is tell Pybricks: "I want to use the Pathfinder library". Without this line, the hub does not know what MBC_LINE8 means, and you get a NameError.

Pick one library. Both work the same way. Only the import line is different.

  1. Download the library (save the file and keep its name): MBC_line8.py (only this sensor) or MBC_sensors.py (the all-in-one library). You picked MBC_line8.py.
  2. In Pybricks Code, press "Import a file" in the file list on the left. Import it into the same project.
  3. Do not rename the file, and do not put it in a sub-folder.
  4. Go back to your main program. Write the lines below at the very top (marked ① in the program on the right).
2

Create an object: name your sensor

A robot can have several sensors, so you "create an object". It tells the program which port the sensor is on, and gives the sensor a name. Every time you read a value, you use this name.

Which port is the sensor on?

Turn on multitasking?

The 3 in the program is the port number: SPIKE 1~6 = A~F. You can also write Port.C, but then you also need from pybricks.parameters import Port.

3

Use a function

You always write "line8.function_name()": the name, a dot, then the function. Click a function, and the program and output change to its example.

Sensor simulator (looking from behind the robot): drag the black line and watch the output on the right

The simulator is only a picture. The maths is simpler than the real thing, so a real sensor may be off by a channel or two. Light value 0 = black, 100 = white. A red ring = that channel sees the line (light value below 50).

An example for every function

16 functions. Each one has a short example you can put straight into a loop. Press "Try it" to jump to step 3. The program and output on the right change to that example.

Full examples

Simple line following (SPIKE, left motor A, right motor B)

        

!Only change the "things to change" part at the top. Robot wobbles left and right: make KP smaller. Robot cannot make the turn: make it bigger. Robot drives backwards: move Direction.COUNTERCLOCKWISE to the other motor.

Multitasking (advanced)

        

!Write True as the 2nd value when you create the object. Create the object outside run_task(). Put await in front of every function.

Use it with other MBC sensors

        

!Switch to the all-in-one library: change only the import line and import MBC_sensors.py. Do not import both libraries at the same time.

Good to know

  1. Unplugging does not stop the program. When the sensor cannot be read, you get the last value it read. If it has never been read, you get None. The computer screen prints one warning line. Plug it back in and it works again by itself. To check the cable, use sensor_ok(). Tick "Unplug the sensor" on the right to see this. If you unplug it while following a line, the robot keeps driving with the last position.
  2. Reading again within 10 ms is not slower. The library asks the sensor for the same data only once every 10 ms (the sensor updates every 10 ms anyway). So you can call several functions in a row.
  3. On EV3, changing to another data group takes about 0.5 s. Try to use one group in the same loop. pos100(), width(), edge_l(), edge_r() are one group (the most used for line following). bin_raw(), bitmap(), on_sensors(), junction(), junction_name() are one group. pos8() is a group on its own. ir_ch(), ir_calibration() are one group. Put device_id() and firmware_version() outside the loop. SPIKE does not have this problem.
  4. Direction: stand behind the robot and look forward. Line on the left = negative, line on the right = positive. Channel 1 is on the far left.
  5. It remembers the last side when the line is lost. When the line leaves the sensor completely, pos8() gives −8 or +8 and pos100() gives −100 or +100, depending on whether the line left on the left or the right. So you do not need extra code to find the line again: the robot turns back towards that side by itself. To check "I really cannot see a line", use width() == 0.