Robot sensors
This text is remixed from http://wiki.roboteducation.org/CS110:Lab04. Errors are my own.
Your robot has sensors that senses different aspects of its environment. The image below shows two types of sensors that your robot has – proximity and light sensors. Notice that they’re on the opposite side of the robot as the camera so you will have to drive backward to use them!
The following is a list of external stimuli that the robot can sense and a description of how the robot senses them.
- Light: There are three light sensors on your robot. These are located in the three holes on the front of your robot. These sensors can detect the levels of brightness (or darkness). Your robot can use these sensors to detect variations in ambient light in a room.
- Proximity: At the front of the robot you will see two tiny lamps on each side of the robot. These are IR emitters. If the light that is emitted by these sensors get reflected by the presence of an obstacle, then the light will bounce back towards the robot and is captured by the IR sensor that is present in the tiny notch in the middle of the two IR emmiters (see figure above).
- Line: If you look at the front portion of the Scribbler’s chassis (away from the third wheel), you will notice two pairs of tiny holes. These are also IR emitters and receivers and can be used to detect lines on the floor.
Once you are familiar with your robot’s sensors, you can use them to create interesting creature-like behaviors for your Scribbler.
Enter the following command in the Python Shell:
>>> senses()
Scribbler Sensor Values:
- Light: The values being reported by the three light sensors are: 135 (left), 3716 (center), and 75 (right). In general, the lower the values, the brighter the light being sensed. This snapshot was taken when the center light sensor was covered by a finger.
- Line: In the presence of a line under the Scribbler (the line has to be aligned with the length of the robot) the Scribbler can detect a bright edge against a dark edge (that forms a line). The display shows the values of left and right sensors (both are 1). If the right sensor is on a dark area of an edge or a line and the left sensor is on a lighter area or edge, the sensor values would be (left=0, right=1). If the edge is reversed, the values would be (left=1, right=0).
- IR: The IR values displayed are also left and right (in the same orientation as the light). Their values will be either 1 or 0. If there is an obstacle detected, the value will be 0. It is 1 otherwise.
- Stall: The stall sensor detects that the robot is not moving even though the motors are. That is, it is stuck somewhere. Its values can be 0 or 1, 0 implies it is not stalled and 1 implies it is.
Unfortunately, some of the sensors are pretty flaky. Try to get the line sensor to get different values for left and right, for example. It doesn’t really happen, so it’s not very useful!
Obtaining Sensory Information
In addition to using the senses operation, you can use various functions in the Myro library to obtain these sensor values. These are listed below:
Light Sensors
- getLight(<POSITION>): This returns the current value in the <POSITION> light sensor. Your Scribbler has three light sensors (see picture above). <POSITION> can either be ‘left’, ‘center’, ‘right’ or one of the numbers 0, 1, 2. The positions 0, 1, and 2 correspond to the left, center, and right sensors. For example, keep your robot in the same position and try the following:
>>> getLight('left')
>>> getLight(0)
>>> getLight('center')
>>> getLight(1)
>>> getLight('right')
>>> getLight(2)
Proximity Sensors
- getIR(<POSITION>): This returns the values of the IR sensors. <POSITION> can be 0 or ‘left’ and 1 or ‘right’. Try the following:
>>> getIR('left')
>>> getIR('right')
>>> getIR(0)
>>> getIR(1)
Obstacle Sensor


The Fluke dongle has an additional set of obstacle sensors on it. These are
also IR sensors but behave very differently in terms of the kinds of values
they report. The following functions are available to obtain values of the
obstacle IR sensors:
- getObstacle() Returns a list containing the two values of all IR sensors
- getObstacle(<POSITION>) Returns the current value in the <POSITION> IR sensor. <POSITION> can either be one of ‘left’, ‘center’, or ‘right’ or one of the
numbers 0, 1, or 2. The positions 0, 1, and 2 correspond to the left, center, and
right sensors. Try the following:>>> getObstacle() >>> getObstacle('center') >>> getObstacle(1) >>> getObstacle('right') >>> getObstacle(2) >>> getObstacle('left') >>> getObstacle(0)
Stall Sensor
- getStall(): This returns the values of the internal stall sensors (propioceptors). Its value can be 0 (the robots is not stalled) or 1 (it is stalled and cannot continue to move). It gets set whenever the motors of the robot are trying to move but the robot is no longer moving. Try the following:
>>> getStall()
Line Sensors
- getLine(<POSITION>): This returns the values of the line sensors. <POSITION> can be 0 or ‘left’ and 1 or ‘right’. Try the following:
>>> getLine(0)
>>> getLine('left')
>>> getLine('right)
>>> getLine(1)
Now that you are familiar with your robot’s sensing capabilities, you can use this knowledge to make different decisions.
Making Decisions
When I had you look at and modify the light sensing program, one of the first lines in the sensing loop was
if rightSensor > 2000:
The first line above uses a conditional expression: rightSensor > 2000. You should read it as if it were asking the question (or testing the condition):
Is the value returned by the right light sensor greater than 2000?
These questions are called conditional expressions and their answers are either a yes or a no. In Python, a yes is equivalent to the value True and a no to the value False. The operator > is a way of asking if the thing on its left (in this case, the value returned by the right light sensor) is greater than to the value on its right (in this case 2000).
Relational operations are used for comparisons. For example: less than (<), less than or equal to(<=), greater than (>), greater than or equal to (>=), equal to (==) and not equal to (!=). Try the following to a get a sense for how these conditional expressions can be used:
>>> 4 > 5 >>> 5 <= 30 >>> 1 != 1 >>> 4 != 6 >>> 67 > 2 >>> (3+4) >= (2-1) >>> "old bag" != "new bag"
The if statement has the following structure:
if <CONDITION>: <do something> <do something> ...
That is, if the condition specified by <CONDITION> is True then whatever is specified in the body of the if statement is executed. If the condition is False, all the statements under the if command are skipped over.
You can create more complex conditional expressions using the logical operations (also called Boolean operations): and, or, and not. Try the following examples:
>>> (20 < 7) and (81 > 31) >>> not ( (20 < 7) and (81 > 31) ) >>> (2 > 3) or (3 > 4) >>> (1 > 7) or (3 > 2)
We can define the meaning of logical operators as follows:
- <expression-1> and <expression-2>: Such an expression will result in a value True only if both <expression-1> and <expression-2> are True. In all other cases (i.e. if either one or both of <expression-1> and <expression-2> are False) it results in a False.
- <expression-1> or <expression-2>: Such an expression will result in a value True if either <expression-1> or <expression-2> are True or if both are True. In all other cases (i.e. if both of <expression-1> and <expression-2> are False) it results in a False.
- not <expression>: Such an expression will result in a value True if <expression> is False or False if <expression> is True). I.e., it flips or complements the value of expression.
Beyond Simple Decisions
You can combine if-statements with the else-statements to make two-way decisions as follows:
if <condition>: <do something> else: <do something else>
That is, if the <condition> is true it will do the commands specified in <do something>. If, however, the <condition> is false, it will <do something else>. You can also extend the if-statement to help specify multiple options using the elif-statement as follows (notice the last else):
if <condition-1>: <command set 1> elif <condition-2>: <command set 2> elif <condition-3>: <command set 3> ... ... else: <last command set>





