Skip to main content
Computer Vision allows your robots to understand their environment. For the competition, this is used to locate markers. It will give you information about the type of the marker, the distance/angle to the marker, etc.

Python

To look for markers call see():
markers is a Python list of “marker objects”, which each look like the following:
Full reference of the properties are further below but some useful properties are:

Codes

You do not need to use the marker ids themselves for your calculations. Use marker.typemarker.owning_team and marker.target_type instead to find out the information you need (see above).
Every april tag has a code:
  • April tags 0-31 will be used for cubes. They won’t all be used in each round.
  • April tags 100+ will around the arena on the walls. See the rules for specifics on where around the rules they will be placed
  • You do not need to use the marker numbers, and can instead use marker.type and marker.owning_team

Blockly

Blocks for vision can be found in the Vision section.

Example

After reading the motors documentation you should be able to create a function which moves your robot by a number of meters as well as turn. We can then use this to write some code where a robot:
  • Looks for a marker
  • If it can see a marker:
    • Turn so that it is facing the marker
    • Drive the distance to the marker
  • If there is no marker in sight turn a bit and look again, maybe there is a marker out of view.

The Marker object

Details about the markers can be accessed using the following syntax:
Marker object contains information about a detected marker. It has the following attributes:
Axes are all defined relative to the camera not your robot. Since we have no way to know how you’ve mounted your camera. You may need to account for this.
R.zone will tell you the team you are currently assigned to. This will be TEAM.RED during testing, but may be a different team during each competition round
You can use MARKER_TYPE, and TEAM from robot, for example…

The Camera object

An interface to the camera is provided in case you want to do additional computer vision.

Changing the resolution

By default the camera takes pictures at a resolution of 640x480px. You can change this by setting the res parameter.
You must use one of the following resolutions:
  • (640, 480) (default)
  • (1296, 736)
  • (1296, 976)
  • (1920, 1088)
  • (1920, 1440)
Using a higher resolution will increase the amount of time it takes to process the image, but you may be able to see more. Using a smaller resolution will be faster, but markers further away may stop being visible.

Get data straight from the camera

If you wish to do your own computer vision you can capture frames directly from the camera using robot.camera.capture().

Using USB cameras

The built-in Pi Camera inside your brain should be great for your robot, however if you would like to use your own USB Camera (perhaps you want to put a camera somewhere else on your robot), you can! USB cameras can have slightly different functionality than the built-in Pi Camera, so they’ll need some fine tuning before you can use them. The basic steps outlined below should get you up and running. Please turn your robot off before plugging in your USB Camera of choice. To use a USB camera you will need to initialize the Robot with something which inherits from robot.vision.Camera. Then just call R.see() as you would normally.

Setting the resolution

You may now wish to change the resolution of your camera, this can be done the same as before with R.camera.res = (width,height).
Some resolutions may not work with your USB camera, as different cameras support different resolutions. Check your camera’s documentation. If you try and use a resolution that your camera doesn’t support, you will get an error that will state the closest resolution to the value you attempted to use. Try changing your resolution to the value that the error message suggests!
For example, to set a USB Camera’s resolution to 800x600:

Calibrating the camera

You will then need to calibrate your USB camera as the distance that it reports will not be accurate. You can do this by changing the value in the R.camera.focal_lengths dictionary up or down. By default, the robot will use the focal lengths for a “Logitech C270” camera, it’s unlikely this is your camera - so see the steps below on how to calibrate it.
Remember that focal lengths vary for different resolutions. You will need to run the calibration code below to find the focal length for each resolution you intend to use with your USB camera.
  • Place a marker exactly 1m away from the camera (measure this distance). Make sure there are no other markers in sight of the camera.
  • Copy and paste the following code into your editor. Please set the resolution value to a resolution you wish to use.
It’s worth noting that this is only an average value. The code above will output a focal length value which you should use before setting R.camera.res or R.see(). You can also copy the line of code it produces and paste that into your code to set the focal lengths. For example, if your focal length was 123 at the resolution of 800x600, you should use the following lines of code, in the same order: