
|
ACTIVITIES
|
CYBER LAB SENSOR ACTIVITIES
|
|
Encoding sensors measure breaks in an infrared beam. Like its name suggests, the most common use of this sensor is for encoding. When used in conjunction with a rotating wheel or gear with holes, the sensor's beam will be broken and received turning it on and off. The software supports two encoders; encoder 0 is read from digital port 7; encoder 1 is read from digital port 8. The encoder software is accessed by the following functions: enable_encoder(n); disable_encoder(n); read_encoder(n); reset_encoder(n);. In all of these functions, n is the encoder number (either 0 or 1). The enable function turns the encoder on. Note that this should only be done once for each encoder-enabling an encoder multiple times without first disabling it can cause your system to crash. Once an encoder is enabled, the read function returns how many times the encoder sensor has turned on and off. The reset function changes the count to 0.
|
|
Figure 2. The infrared beam of light can pass through the slits of the wheel, but are stopped by the opaque sides.
![]()
Learning how to use the encoding sensors can be a valuable tool when participating in Botball or conducting experiments using robots. Once you become familiar with the sensor's structure and how it works, you will be able to take on several interesting applications. Objectives: In this activity, you will learn how to load code for the encoding sensors and how to perform a basic test on the sensors. Click here for a quick IC and NQC tutorial
Materials Needed: HandyBoard setup,
one digital encoding sensor, Interactive C software, one penny
or key
Procedure (a): Loading the digital sensor code
1. Connect your computer to the serial interface box with the
9 to 24 pin PC or Mac cable. Connect the serial port box to the
HandyBoard with the 4 pin telephone cable. Turn the HandyBoard
on.
2. Open up the application Interactive C (IC).
3. Under "File" in IC, open a new text editor
by choosing "New." Write or cut and paste the
sample program code below into the new IC text box:
|
void main() printf("Press START ...\n"); |
Save this file as encoder.c and then download the file to the HandyBoard. If your download was successful, turn the HandyBoard off and on again. The LCD screen should be displaying the message "Press START"
Procedure (b): Testing the Sensor Beam with an Opaque Object
4. Attach the encoder sensor to port 7
5. Press START
6. The last (right most) of the 9 zeros reflects the value of port 7.
7. Pass a penny or key between the emitter and detector on the sensor.
8. You should see port 7's reading change back and forth. Question: How
does the digital reading differ from the analog readings?
9. Press STOP when you are finished sampling the inputs.
![]()
Positioning the encoder and wheel is important for getting acurate sensor readings. In the first activity, you learned how to break the infrared beam with an opaque object. Objectives: In this activity, you will add a library function to the encoder computer code that will run a LEGO mini-motor. The motor will turn a small 6 hole LEGO pulley wheel. As the IR beam passes through the holes in the wheel, the sensor will register (1). As the IR beam is blocked by the wheel, the sensor will register (0).
Materials Needed: HandyBoard setup, one encoder (break sensor), Interactive C software, two 6 cm LEGO bricks, one 4.5 cm LEGO flat, one LEGO mini-motor (red), LEGO/HandyBoard connector, one LEGO pulley wheel, one 3 cm LEGO axle, one flash light
Procedure:
1. Open up the code for digital sensors and add the function fd(1); In
IC code, fd(1) means turn on (forward) the motor in port 1.
2. Save this file as encoder.c
3. Load this file
|
void main() printf("Press START For Encoder ...\n"); |
4. Attach the LEGO mini-motor to motor port 1. Attach the 3 cm
axle to the motor. Attach the pulley wheel to the axle.
5. Attach the encoder sensor to port 7.
6. Stack the 4.5 cm LEGO flat on top of the 6 cm brick. Tape the
encoder to the end of this structure.
7. Stack the mini-motor on the top, center of the other 6 cm brick.
8. Position the sensor structure so that the wheel passes through
the IR beam when the motor runs. Tape both the sensor and motor
structures to the table with masking tape.
9. Turn on the HandyBoard, press start. The motor starts to run. Adjust the
sensor so that the IR beam is being broken.
Questions:
10. Is port 7 flashing 0 to 1? If it is a constant 1, try adjusting the sensor
so the pulley holes line up better with the sensor. If the port is a constant
0, are you in a room with a great deal of light? Try turning off some lights
in the room or shield the sensor from the light.
11. When your sensor starts reading correctly, turn on the flash light and point
it directly at the sensor. What happens to the reading? Does light have a negative
impact upon the sensor readings? Why?
![]()
The encoder sensor allows the robot to know how far it has traveled and can help it figure out where it is. The Encoder sensor shines a beam of infrared light from the emitting side to the receiving side. When the beam is detected, an electrical signal is sent to the computer board.
If a wheel with holes is put between the receiver and the emitter, as the wheel turns, the beam will be broken between holes, and will shine through the holes. When the beam is broken, the beam never reaches the receiving side, and a signal is not sent to the computer. When the wheel turns, the signal goes on, off, on, off, on, off... The computer can count the number of breaks in the beam and the number of times the beam is received.
Figure 3. Encoder
showing emission and detection
Materials Needed: HandyBoard setup, one digital encoding sensor, one LEGO pulley wheel containing six holes
Procedure:
The first thing you need to do is load the encoder libraries.
To do this, type load encoders.lis in the IC text box.
This command will initiate the encoders and you will be able to
use the code below.
The robot can have up to two encoders. The first encoder is called
encoder 0 and the second one is called encoder 1. Encoder 0 connects
to port 7 and encoder 1 connects to port 8 on the HandyBoard.
These are the functions that are used to control the encoders.
Function Purpose
enable_encoder(number); Tells the computer to start counting
the times the IR beam is received and broken. This statement must
be present in the code.
read_encoder(number); Tells your program how many times
the IR beam is broken and received.
since you last used enable_encoder or reset_encoder.
reset_encoder(number); Resets the computer's count for
that encoder to 0.
(Starting counting from 0.)
Here's an example of how to use the encoder functions in a program:
Code: Sample code for counting the number of holes on encoder
0. Make sure that an encoder sensor is connected to port 7. This
code used all 3 encoder functions. Be sure and load encoders.lis
| void main() { enable_encoder(0); /*start counting holes on encoder 0 */ while(!stop_button()) { /* keep doing this until the stop button is pushed. (Actually, it really means don't stop if it's not pushed.) */ printf("%d holes\n", read_encoder(0)/2); /* display the number of holes counted (reading divided by 2)*/ if (start_button()) reset_encoder(0); /* if the start button is pushed, start counting at 0 */ } } |
![]()
So, now you can count the breaks in the IR beam, but how do the number of breaks in the IR beam tell you how far the robot has gone? Well, the wheel has six holes. For every six breaks in the IR beam counted, the wheel has gone around once. When the encoder wheel is put on the same axle as a big LEGO wheel, the encoder wheel turns with the big wheel. By counting he number of breaks in the IR beam, you can approximate distance. For the robot, distance is measured in the number of makes and breaks in the IR beam. Here is sample code for making the robot move forward 50 holes.
Figure 4. Showing
beam in relation to wheel hole position
Materials Needed: HandyBoard setup, one digital encoding sensor, one simple robot w/ pulley wheel
Procedure:
1. Connect the left motor on your robot to motor port (0) on
the HandyBoard
2. Attach the encoding sensor to your robot so that it can read
your robot's pulley wheel
3. Attach the encoding sensor to port 7 on the HandyBoard
4. Open up Interactive C and write or copy the encoding sensor
code below
5. Load the code and activate the program by turning the HandyBoard
off and back on again
6. Initiate the robot by pushing start
7. Change the variable command in while (read_encoder(0)<100);
to <50, <200, <300
8. Test your robot on a tile floor, a rugged surface, a grassy
surface. Use the command while (read_encoder(0)<200).
Now measure how far each robot traveled on each surface. Question:
Does the robot behave differently on each surface? How?
Code: Sample code for making the robot move a certain distance.
To use this code, make sure the left motor is connected to motor
port 0, and the right motor is connected to motor port 1. Encoder
0 should be connected to port 7 and should be set up in a way
so that it turns with the left wheel.
|
void main() |
|
|
|
|
|
|