Ground and Obstacle Sensing (IR and Color Sensors)

Ground and Obstacle Sensing (IR and Color Sensors)

This page will help you get started with Marty's ground and obstacle sensors using the python library. We will be using the IR and/or color sensor add ons for these functions, so make sure you have set up your color sensors and IR sensors. You might have one of each or multiple of one type, so follow the instructions based on which add ons you have available.

Make sure you also have python installed and ready to go.

Get Connected

First you must always import Martypy and establish connection to your Marty. Follow the code below to connect your Marty.

import martypy

# USB connection
# change "COM1" on this next line to specify the USB port you want to use
# comment this line out to use WiFi
my_marty = martypy.Marty("USB")

# WiFi connection
# uncomment this and change the IP address to your Marty's to use WiFi
# my_marty = martypy.Marty("wifi", "192.168.0.5")

You should now be able to play with your sensors using the four functions below.

Is Your Marty On the Ground?

The function foot_on_ground tells you whether the sensor recognizes the chosen foot to be on a surface. It returns True if it is on the ground, False otherwise.

foot_on_ground(add_on_or_side):

  • add_on_or_side: This allows you to specify which add on or foot you would like the function to use. Click Here to learn how to pass in this parameter.

Try this function using the code below to check if your Marty's foot sensor is on the ground.

my_marty.foot_on_ground('RightIRFoot')  # Replace 'RightIRFoot' with your IR or color sensors name.

Checking if There's Something in Marty's Way

The function foot_obstacle_sensed tells you whether an obstacle is in front of the foot sensor. It returns True if an obstacle is sensed, and False otherwise.

foot_obstacle_sensed(add_on_or_side):

  • add_on_or_side: This allows you to specify which add on or foot you would like the function to use. Click Here to learn how to pass in this parameter.

Try this function using the code below to check if your Marty's foot has an obstacle blocking its way.

my_marty.foot_obstacle_sensed('RightIRFoot')  # Replace 'RightIRFoot' with your IR or color sensors name.

Getting the Raw Data

The functions get_obstacle_sensor_reading() and get_ground_sensor_reading() get the raw data from the IR or color sensor and return the wanted obstacle or ground sensor reading.

get_obstacle_sensor_reading(add_on_or_side):

  • add_on_or_side: This allows you to specify which add on or foot you would like the function to use. Click Here to learn how to pass in this parameter.

get_ground_sensor_reading(add_on_or_side):

  • add_on_or_side: This allows you to specify which add on or foot you would like the function to use. Click Here to learn how to pass in this parameter.

Try these functions using the code below to get the raw data from the obstacle and ground sensors.

my_marty.get_obstacle_sensor_reading('RightIRFoot')  # Replace 'RightIRFoot' with your IR or color sensors name.
my_marty.get_ground_sensor_reading('RightIRFoot')  # Replace 'RightIRFoot' with your IR or color sensors name.

Using the add_on_or_side Parameter

For the ground and obstacle sensing functions above, you must pass in an IR or color sensor that will perform the function. There are two ways to pass in the chosen sensor:

1. You can pass in a string of the name of your IR or color sensor. You can find the names of all your add ons by visiting the Scratch app and going to Configure –> ADD-ONS. Don't forget to disconnect the app after in order to connect with your Marty in python!

2. If your add ons are named correctly and connected to the correct feet, you can pass in a string 'left' or 'right' to refer to which foot you would like to use in the function. If your add ons are named correctly and seem to be in place, try checking if your left foot is on the ground using the code below!

my_marty.foot_on_ground('left')

Checking Your Sensor Names

In order to make sure that your add ons are named correctly, you can find the names of all your add ons by visiting the Scratch app and going to Configure –> ADD-ONS. Your add ons should be named based on what sensors you have available:

  • 1 IR sensor: Your IR sensor can be named either 'LeftIRFoot' or 'RightIRFoot'
  • 1 color sensor: Your color sensor can be named either 'LeftColorSensor' or 'RightColorSensor'
  • 1 color sensor 1 IR sensor: You can either have 'LeftColorSensor', 'RightIRFoot' OR 'LeftIRFoot', 'RightColorSensor'
  • 2 IR sensors: Your IR sensors should be named 'LeftIRFoot' and 'RightIRFoot'
  • 2 color sensors: Your color sensors should be named 'LeftColorSensor' and 'RightColorSensor'
Task Runner