MBC Ranger 8-Way (TOF8) × EV3 Pybricks

Read the Ranger 8-Way on the EV3 with MicroPython in three steps

MicroPython on the EV3 always goes in this order. First import. Then create an object and give the sensor a name. Last, use that name to read values. First pick Pybricks v4 or EV3 MicroPython v2 above. Then follow the steps on the left, and the program on the right grows with you. Drag the obstacle's direction and distance under the sensor simulator to see what the program prints.

Your software:
1

Import the library

The first thing your program does is tell Pybricks: "I want to use the Ranger 8-Way library". Without this line, the hub does not know what MBC_LASER8 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_laser8.py (only this sensor) or MBC_sensors.py (the all-in-one library). You picked MBC_laser8.py.
  2. Open code.pybricks.com in Chrome. Follow the steps on the site to put the Pybricks firmware on the EV3 (the release version already includes the EV3 firmware).
  3. Press "Import a file" in the file list on the left. Import the library into the same project.
  4. Do not rename the file, and do not put it in a sub-folder.
  5. 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 it a name. Every time you read a value, you use this name.

Which port is the sensor on?

Turn on multitasking?

The 1 in the program is the port number: EV3 1~4 = S1~S4. You can also write Port.S1, but then you also need from pybricks.parameters import Port.

3

Use a function

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

Simulated sensor (seen from above, robot facing up): drag the obstacle and watch the output on the right

The simulation is only a sketch: direction and distance come from a simple position-based estimate, so a real sensor may differ by a step or two. Red sector = that sensor sees something; grey sector = it reads 120 (nothing that way). Sensors 4 and 5 both face straight ahead.

An example for every function

Full examples


        

!


        

!


        

!

Good to know

  1. Unplugging works differently in the two programs. Pybricks v4 with the MBC library: when the sensor cannot be read, you get the last value it read (or None if it has never been read). The computer prints one warning line, and the program keeps running. Plug it back in and it works again by itself. To check the cable, use sensor_ok(). Careful: if it comes out while avoiding obstacles, the robot keeps driving with the last distance. EV3 MicroPython v2 with Ev3devSensor: you get an OSError and the program stops. Plug the sensor back in and run the program again. Tick "Unplug the sensor" on the right to see both.
  2. On the EV3, changing mode takes about 1.1 s. Try to use one mode in the same loop (the "Mode N" on each card). min_direction(), min_distance(), max_direction(), max_distance() are mode 2 (the most used for avoiding). distances(), ch() are mode 1. nearest_mm(), nearest_dir() are mode 5. front_mm() is mode 3.
  3. Check the unit. On the EV3 the sensor sends centimetres as "1 step = 1.2 cm" (0 to 100). The MBC library turns them back into cm (0 to 120, jumping by about 1 to 2 cm). When you read modes 0, 1 and 2 yourself with EV3 MicroPython v2, you get steps: cm = steps × 1.2. Modes 3 and 5 use millimetres.
  4. Direction: stand behind the robot and look forward: left is negative, right is positive. Laser 1 points straight left, lasers 4 and 5 point straight ahead, laser 8 points straight right. When something is seen, the direction is −7 to +7. ±8 = nothing around. So to check "is anything there?", see if the distance is at its maximum (120 cm, 100 steps, 1200 mm).
  5. What the EV3 cannot read: read_all(), is_ok(), init_status(), fault_status(), device_id() and firmware_version() are SPIKE only. Millimetres (modes 3 and 5) need sensor firmware v4.0 or later.