How to Detect and Follow a Colored Object with the SCUTTLE Robot on Viam
NoteIn the Python code, you must add your robot’s address and secret, which are found on the CODE SAMPLE tab of the Viam app at https://app.viam.com in your web browser. Viam app pre-populates the CODE SAMPLE tab with the robot name, address, and secret:
CautionDo not share your robot secret or robot address publicly. Sharing this information compromises your system security by allowing unauthorized access to your computer.
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.
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.
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.
Notes on Color Detection Operation
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.
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.
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!
Python.py code Gist: https://gist.github.com/mestcihazal/e78e3b29c58aa301c9a197ada272e6a0 ↩︎
Python SDK installation: https://github.com/viamrobotics/viam-python-sdk#installation ↩︎