Ground and Obstacle Sensing (IR and Color Sensors)

Use MartyPy to check whether your Marty V2 is on a surface, detect obstacles in front of its feet, and read the raw sensor values.

These functions use the IR foot sensor and/or colour sensor. You may have one of each, or more than one of the same type; use the examples that match the sensors fitted to your Marty.

Before you begin:

Get Connected

Import martypy and connect to your robot. Choose USB or WiFi:

  • USB: run the code as shown. MartyPy can automatically find a single connected Marty.
  • WiFi: comment out the USB line, uncomment the WiFi line, and replace 192.168.0.5 with your Marty's IP address.
import martypy

# Connect by USB.
my_marty = martypy.Marty("USB")

# Alternatively, connect by WiFi using your Marty's IP address.
# my_marty = martypy.Marty("wifi", "192.168.0.5")

All four functions below take an add_on_or_side argument to choose the sensor. You can use its name, or 'left' or 'right' when the sensors are named for the correct feet. See Using the add_on_or_side Parameter for details.

In the Python shell, replace 'RightIRFoot' in the examples with the name of your IR or colour sensor.

Is Your Marty On the Ground?

foot_on_ground(add_on_or_side) checks whether the chosen foot sensor detects a surface beneath it. It returns True when the ground is detected, and False otherwise.

my_marty.foot_on_ground('RightIRFoot')

Checking if There's Something in Marty's Way

foot_obstacle_sensed(add_on_or_side) checks whether the chosen foot sensor detects an obstacle in front of it. It returns True when an obstacle is detected, and False otherwise.

my_marty.foot_obstacle_sensed('RightIRFoot')

Getting the Raw Data

Use these functions to read the raw values from the chosen IR or colour sensor:

Function Returns
get_obstacle_sensor_reading(add_on_or_side) Raw obstacle sensor reading.
get_ground_sensor_reading(add_on_or_side) Raw ground sensor reading.
my_marty.get_obstacle_sensor_reading('RightIRFoot')
my_marty.get_ground_sensor_reading('RightIRFoot')

Using the add_on_or_side Parameter

There are two ways to choose the sensor:

  1. Use its name, such as 'RightIRFoot' or 'LeftColorSensor'. In the Robotical app, open Robot settings → Add-ons to check the names of your sensors. Use the name exactly as shown. Disconnect Marty from the app before connecting through Python.
  2. Use a side: pass 'left' or 'right' when the sensors are named correctly and attached to the corresponding feet.

For example, check whether the left foot is on the ground:

my_marty.foot_on_ground('left')

Checking Your Sensor Names

In the Robotical app, open Robot settings → Add-ons to check or change a sensor's name. Choose names that match both the sensor type and the foot where it is fitted:

Sensors fitted Names to use
One IR sensor LeftIRFoot or RightIRFoot, according to the foot.
One colour sensor LeftColorSensor or RightColorSensor, according to the foot.
One colour sensor and one IR sensor LeftColorSensor and RightIRFoot, or LeftIRFoot and RightColorSensor.
Two IR sensors LeftIRFoot and RightIRFoot.
Two colour sensors LeftColorSensor and RightColorSensor.
Task Runner