Distance Sensor

Use MartyPy to read the distance sensor add-on on your Marty V2.

Before you begin:

Connect to Marty

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")

Read the distance sensor

In the Python shell, run:

my_marty.get_distance_sensor()

For Marty V2, get_distance_sensor() returns an integer distance reading in millimetres (mm) for the closest detected object. It returns 0 if no distance sensor is found.

Task Runner