How to Detect and Follow a Colored Object with the SCUTTLE Robot on Viam

Instructions for detecting and following a colored object with a SCUTTLE Robot on Viam software.


Demonstration Video Code GIST1


The prerequisite of this tutorial is to have a SCUTTLE rover which you can drive via a webUI. Please refer to Setting Up Tutorial For SCUTTLE with a Pi. if you have not already configured your SCUTTLE.

Now you should try to drive the SCUTTLE around following the color red. Perhaps you can start with a red sports ball to demo with.

Download the code to a directory on your computer that you can remember. Feel free to choose your own location, but as an example, we’ve chosen the Desktop. We highly suggest using a virtual python environment like Poetry2 or Miniconda3.

Then create an environment for Python by running the following on the terminal:

poetry new pysdk  # new poetry project
conda create -n pysdk python=3.9  # new (mini)conda environment

You can also name your environment as you wish, but please remember to keep it consistent. We named our environment pysdk, referring to the Viam Python SDK.

NOTE: If using (mini)conda, activate the environment by running the command:

conda activate pysdk

Poetry environments are implicitly activated.

Before you can run the code, you need to install the “viam” module. Follow the Python SDK installation4 guide to properly install the package.

NOTE: If using a python environment, ensure that the package is installed in the proper environment.

pip freeze | grep viam  # generic
poetry show | grep viam  # for poetry environments
conda list | grep viam  # for (mini)conda environments

You should see viam-sdk listed near the end.

Now you are ready to run the code!

Running the code

Navigate to the folder you saved the Python script into. From that folder, run in the terminal:


Be sure to replace “~/Desktop/” with the “/path/toYour/directory/” where the Python code was saved.

python ~/Desktop/

Notes on Color Detection Operation

Within getVisService(robot), a detector is configured with particular properties and subsequently added to the vision service. This particular detector is a “color” detector, which means the relevant parameters are “detect_color (hex string)”, “hue_tolerance_pct (float from 0 to 1)”, and “segment_size_px (integer).” Feel free to add new detectors with different parameters! To learn about all the different detectors and parameters, check out the Vision Service topic.

The leftOrRight() code splits the screen vertically into thirds (left, center, and right) and makes a determination about which third the object (red ball) is in. Within main(), this decides how the robot moves (as configured by the 4 given variables). Run the code as is before making changes to see how it affects the output!