Difference between revisions of "Sensors"

From EG1004 Lab Manual
Jump to: navigation, search
 
(49 intermediate revisions by the same user not shown)
Line 1: Line 1:
<h1 align=center>EG1004 Lab 7: Sensors</h1>
''This writeup is intended for use with the NXT. For the EV3 version, see [[Sensors (EV3)]].''
= Objectives =
The experimental objective of this lab is to design various Mindstorms programs that use rotation, light, touch, and ultrasonic sensors.


<h2>1 OBJECTIVES</h2>
These exercises will build a foundation of experience with sensors programming that can be used with the semester-long design project.


<p>The experimental objective of this lab is to design two programs.
= Overview =
The first is a RoboLAB program that uses light and touch
sensors to stop a motor when a yellow brick is detected. The second is a
LabVIEW program that uses a thermocouple to measure air temperature in the
heating and cooling program you designed in the <b><i>LabVIEW Lab</i></b>.</p>


<p>We will learn to use light, touch, and temperature sensors so that
== Mindstorms Program ==
we can use these tools in our semester-long design projects.</p>
Before we can learn to use sensors, we must become familiar with the NXT. Remember that the Mindstorms program is a compiler: a program used to make other programs. Just as a compiler is used when creating and debugging a C++ program, Mindstorms similarly compiles programs for the NXT. The Mindstorms interface is graphic; a text compiler like those used for C++ is not. However, the principle is very much the same. Actions are taken based upon circumstances set by the programmer.


<h2>2 OVERVIEW</h2>
The NXT houses all the programming instructions that control the movement of your robot. Once you have created your program in Mindstorms, it is uploaded to the NXT. The NXT then dictates the robot's motion. If your robot does not do what you intended, you must rewrite the program in Mindstorms and upload the corrected version to the NXT.


<h3>RoboLAB Program</h3>
= Assignment =
A zip file including the following programs needs to be submitted to the [http://eg.poly.edu EG Website]. If you don't know how to make a zip file, read the page [[How to Compress Your Files]] in the ''Instructional Presentations'' section of this manual.
* [[#Task|Ultrasonic Sensor Task]]
* [[#Course_1|Light Sensor Task: Course 1]]
* [[#Course_2|Light Sensor Task: Course 2]]


<p>Before we can learn to use sensors, we must become familiar with
== Lab Report ==
the <b><i>RCX</i></b>.  Remember that the RoboLAB
There is no lab report for this lab.
program is a compiler: a program used to make other programs. Just as a
compiler is used when creating and debugging a C++ program, RoboLAB compiles
programs for the RCX. The RoboLAB interface is graphic; a text compiler like
those used for C++ is not. However, the principle is very much the same.
Actions are taken based upon circumstances set by the programmer. </p>


<p>The RCX houses all the programming instructions that control the
== Team PowerPoint Presentation ==
movement of your robot. Once you have created your program in RoboLAB, it is
There is no presentation for this lab.
uploaded to the RCX. The RCX then dictates the robot's motion. If your robot
does not do what you intended, you must rewrite the program in RoboLAB and
upload the corrected version to the RCX.</p>


<h3>LabVIEW Program</h3>
= Materials and Equipment =
* Lab PC
* Mindstorms Software
* USB Cable
* NXT Unit


<p>The thermocouple records temperatures that are transmitted through
= Procedure =
the DAC board. LabVIEW uses these values to operate the heating and cooling
== Rotation Sensor ==
system you designed in the <b><i>LabVIEW Lab</i></b>. To program your
# Hover over the Sensor sub-palette (indicated by the yellow block on the left side of the window) and select the rotation sensor block
heating and cooling system VI to gather information from an outside source
#: [[Image:lab_sensors_54.png|thumb|500px|center]]
(thermocouple), you must add an AI Sample
# Place it on the sequence beam
Channel. In order to wire this VI, you must right-click on
#: [[Image:lab_sensors_55.png|thumb|500px|center]]
the icon, select Visible Items, and then Terminals.</p>
# Select the rotation sensor block placed on the sequence beam and change the output to rotations. The configuration panel can be found on the bottom left corner of the window.
#: [[Image:lab_sensors_56.png|thumb|500px|center]]
# Hover over the Advanced sub-palette (indicated by the red block on the left side of the window) and select the Number to Text block
#: [[Image:lab_sensors_57.png|thumb|500px|center]]
# Place the Number to Text block next to the Rotation Sensor block.
#: [[Image:lab_sensors_58.png|thumb|500px|center]]
# Hover over the Action sub-palette (indicated by the light green block on the left side of the window) and select the Display block
#: [[Image:lab_sensors_59.png|thumb|500px|center]]
#Place the Display block next to the Number to Text block.
#: [[Image:lab_sensors_60.png|thumb|500px|center]]
# Configure the display block to show text.
#: [[Image:lab_sensors_61.png|thumb|500px|center]]
# Expand the data hub by clicking near the bottom left corner of the Display block
#: [[Image:lab_sensors_62.png|thumb|500px|center]]
#: [[Image:lab_sensors_63.png|thumb|500px|center]]
# Wire the plugs to the blocks as shown below.
#: [[Image:lab_sensors_64.png|thumb|500px|center]]
# Hover over the Flow sub-palette (indicated by the light orange block on the left side of the window) and select the Loop block
#: [[Image:lab_sensors_65.png|thumb|500px|center]]
# Place the loop block to the left of the Rotation Sensor block
#: [[Image:lab_sensors_66.png|thumb|500px|center]]
# Select all of the blocks to the right of the loop block and drag them into the loop block.
#: [[Image:lab_sensors_67.png|thumb|500px|center]]
With the program now completed, upload it to your NXT and run the program. Turn the motor that is connected to port A and look at the display. You should see numbers on the display that correspond to the number of times you've turned the motor.
=== Example for Rotation Sensor ===
Suppose you wrote a program that had your robot's motors move forward for 3 seconds. The procedure for this was covered in [[Product Evaluation and Quality Improvement]].
[[Image:lab_sensors_68.png|thumb|500px|center]]
Let's say you want to add the code that we previously made so that you can read the number of rotations that your robot made while it's moving. In order to do that, you'll need to create a new sequence beam.
# Start by clicking on the sequence beam below the starting point.
#: [[Image:lab_sensors_69.png|thumb|500px|center]]
# Drag the beam to your desired destination and double click at that point to create the new beam
#: [[Image:lab_sensors_70.png|thumb|500px|center]]
# Create the program previously made on the new beam
#: [[Image:lab_sensors_71.png|thumb|500px|center]]
=== Example #2 for Rotation Sensor ===
Suppose we wanted to create a program that displayed the number of rotations, but instead of having it loop forever, it would stop measuring the number of rotations once you've rotated it three times.
# Create the program shown below
#: [[Image:lab_sensors_72.png|thumb|500px|center]]
# Click on the loop block and change the control to sensor in the configuration menu.
#: [[Image:lab_sensors_73.png|thumb|500px|center]]
# Change the sensor to Rotation Sensor.
#: [[Image:lab_sensors_74.png|thumb|500px|center]]
# Set the loop conditions to greater than 3 rotations
#: [[Image:lab_sensors_75.png|thumb|500px|center]]
Your completed program should look like the one on the image below.
[[Image:lab_sensors_76.png|thumb|500px|center]]
When you run this program, you should be able to see the number of rotations on the NXT's display. If you make more than 3 rotations on the motor, the program should end.


<p>Developing advanced programming skills in both RoboLAB and LabVIEW
== Ultrasonic Sensor ==
will allow you to successfully complete your semester-long design project. This lab will
For this portion of the lab, you will learn how to use the ultrasonic sensor in order to determine the distance between the robot and an object.
help you acquire those skills.</p>
The code for obtaining a distance reading and displaying it on the NXT's screen is almost exactly the same as the code for the rotation sensor. The only difference is that instead of the rotation sensor block, you must use the ultrasonic sensor block
[[Image:lab_sensors_77.png|thumb|500px|center]]
=== Example for Ultrasonic Sensor ===
Suppose we wanted to write a program in which our NXT would show a happy face if the Ultrasonic Sensor detected a distance greater than 40 in, and show a sad face if the Ultrasonic Sensor detected a distance less than 40 in.
# To start off this program, we'll first need the switch block, which can found in the Flow sub-palette
#: [[Image:lab_sensors_78.png|thumb|500px|center]]
# In the configuration panel, change the sensor to the Ultrasonic Sensor
#: [[Image:lab_sensors_79.png|thumb|500px|center]]
# In the configuration panel, change the distance to 40
#: [[Image:lab_sensors_80.png|thumb|500px|center]]
#: If you look at the switch block, you'll notice two peculiar objects; a mountain and a flower. The mountain represents the case in which the sensor value is greater than 40, and the flower represents the case in which the sensor value is less than 40.
#: [[Image:lab_sensors_81.png|thumb|500px|center]]
# Place a display block into each of the two sequence beams in the switch block
#: [[Image:lab_sensors_82.png|thumb|500px|center]]
# Configure each display block so that it shows the correct image. Remember that the NXT should display happy face when the distance sensed by the Ultrasonic Sensor is greater than 40 in and show a sad face when the distance is less than 40 in.
# Place the entire switch block into a loop block.
#: [[Image:lab_sensors_83.png|thumb|500px|center]]
=== Example #2 for Ultrasonic Sensor ===
Now suppose we wanted to add an additional case to the program where the NXT would display a caution sign when the Ultrasonic Sensor senses a distance less than 40 in but greater than 30 in, and display a sad face when the distance sensed is less than 30. In this situation, one switch case won't be enough, as the program will only check to see if the value obtained is greater than or less than one specific number. To overcome this problem, we'll need an additional switch case inside of the switch case we already have.
[[Image:lab_sensors_84.png|thumb|500px|center]]
In this program, when the distanced sensed is greater than 40, it will display a happy face, just like before. When the distance sensed is less than 40, it will check to see if it's greater than or less than 30, and show the appropriate image based on that.
=== Task ===
Create a program in which your robot maintains a distance of 8 and 20 in from the wall. In other words, your robot should move away from the wall if the ultrasonic sensor senses a distance less than 8 in and moves towards the wall if the ultrasonic sensor senses a distance greater than 20 in.


<h2>3 YOUR ASSIGNMENT</h2>
== Touch Sensor ==
For this portion of the lab, you will learn how to use the touch sensor to control the actions of your robot based whether the touch sensor has been pressed or bumped.
=== Example for Touch Sensor ===
Suppose you wanted to create a program in which your robot would move when the touch sensor is pressed, and would stop when the touch sensor was released.
# Insert a switch block and have it set to the touch sensor
#: [[Image:lab_sensors_85.png|thumb|500px|center]]
# Insert a move block into each of the sequence beams. Configure one motor block to move continuously and set the other motor block to stop
#: [[Image:lab_sensors_86.png|thumb|500px|center]]
# Place the switch block into a loop block
#: [[Image:lab_sensors_87.png|thumb|500px|center]]


<h3>Individual Lab Report (one report per student),
== Light Sensor ==
Component Testing, and Progress Presentation.</h3>
For this portion of the lab, you will learn how to use the light sensor in order to determine the light intensity of the sensor's reflected light. You will also learn how to use the light sensor to follow a line.
The program for obtaining the light intensity and displaying it on the NXT's screen is the same as that of the rotation sensor and the ultrasonic sensor, except you need to use the light sensor block.


<p>Instead of a lab report his week, you will practice writing <b><i>computer documentation</i></b>.
[[Image:lab_sensors_88.png|thumb|500px|center]]
Being able to explain how to use software to other people is something you will need to
master to be successful in your career. Before you begin, you must read the section titled
<b><i>Writing Software Documentation</i></b> in the <b><i>Technical Communication</i></b>
section of this online manual.</p>


<ol>
=== Line Following Code Using One Light Sensor ===
<li>Your user, Natasha Gibson, is an electrical engineer who is designing automated systems. She is
The code for following a line using one light sensor is shown below.
an experienced computer user who has never tried LabVIEW. When she loads a software package for the
first time, she refers to the manual to help her get started. Your job is to write a set of instructions
to support Natasha as she learns to program a heating and cooling system VI to gather information from
an outside source using LabVIEW.</li>


<li>Prepare a one-paragraph <b><i>Introduction</i></b> that describes LabVIEW. You should talk about
[[Image:lab_sensors_89.png|thumb|500px|center]]
how it will make Natasha’s job easier. This will provide an incentive for her to learn it. You should
describe what LabVIEW is, what a Virtual Instrument (VI) is, and how it helps engineers in the task of
data acquisition.</li>


<li>Next, you will write <b><i>Procedures</i></b> for Natasha to follow. First, prepare a scenario.
This block of code alternates between the left and right motor of the robot, depending on whether the light sensor sees the line. For example, let us assume that motor A is the left motor and motor C is the right motor. Thus, when the light sensor sees the line, the right motor (motor C) stops while the left motor (motor A) starts turning. This causes the robot to turn away from the line while moving forward. Also, when the light sensor doesn't see the line, the left motor stops while the right motor starts turning. This causes the robot to turn towards the line while moving forwards.
This sets up the user to perform the task (e.g. LabVIEW allows you to design a program that simulates a
typical household lighting system). List the steps Natasha should follow to program a heating and cooling
system VI to gather information from an outside source. To make this easier, perform the task yourself and
take notes on each step. You may not copy the steps listed in this manual. Each numbered step must be a
complete sentence. Use a standard format. See <b><i>Writing Software Documentation</i></b> in the
<b><i>Technical Communication</i></b> section of this online manual for help in preparing this
assignment.</li>
</ol>


<h3>Component Testing</h3>
=== Line Following Code Using Two Light Sensors ===
The code for following a line using two light sensors is shown below.


<p>After making the PowerPoint presentation described in <b><i>Milestone 2</i></b>,
[[Image:lab_sensors_90.png|thumb|500px|center]]
<b><i>Component Testing for your specific project</i></b> will take place. Robot
models will be tested for functionality, and RoboLAB programs will be tested for
accuracy. Similarly, where applicable, the lighting system and heating and cooling
systems for your supermarket design must be fully functional. Your LabVIEW programs
will be tested for their accuracy.</p>


<h2>4 MATERIALS AND EQUIPMENT</h2>
This block of code alternates between turning the left and right motor of the robot, depending on which light sensor sees the line. First, let us assume that motor A is the left motor and motor C is the right motor. Also, we'll assume that light sensor 1 is the left light sensor and light sensor 2 is the right light sensor.  Thus, when the left light sensor sees the line, but the right line sensor doesn't, the left motor stops while the right motor starts moving. If the right light sensor sees the line, but the left light sensor doesn't see the line, the right motor stops while the left motor starts moving. If neither light sensor see the line, then both motors start turning.
For positioning, the robot should be placed such that the light sensors are in between the line.


<ul>
=== Tasks ===
<li>Lab PC</li>
==== Course 1 ====
[[Image:lab_sensors_91.png|thumb|500px|center]]


<li>RoboLAB and LabVIEW Software</li>
Create a program in which your robot travels from point A to point C in a clockwise manner, then rotates 180 degrees and finally returns to point A.
<li>USB Tower</li>
<li>RCX Unit</li>
<li>Light Sensor Array</li>
<li>DAC Board</li>
<li>Thermocouple</li>
</ul>


<h2>5 PROCEDURE</h2>
==== Course 2 ====
[[Image:lab_sensors_92.png|thumb|500px|center]]


<h3>Program 1: RoboLAB Sensors</h3>
Create a program in which your robot travels from point A to point H in a clockwise manner, rotates 180 degrees, and travels to point C.


<p>In this exercise, you must find a yellow brick out of a collection of  multi-colored
= Appendix: Advanced Mindstorms Reference =
bricks. The yellow brick has a larger light reflectivity than bricks of other
== Wait ==
colors. Thus, you can find the yellow brick by searching for the largest light
reflectivity among all the candidate bricks. You will be provided with a test
assembly that continually moves a light sensor, mounted on a motor, back and
forth along a short track. There is a touch sensor at each end of the track
that indicates that the end of the track has been reached. The light sensor,
the two touch sensors, and the motor are all connected to the RCX.</p>


<p>You must create a RoboLAB program for the test assembly. See Figures
<table>
1-3.  The test assembly has four main
<tr>
components: a motor, a light sensor, and two touch sensors. The objective of this assignment is to create
<td valign=top>[[Image:Lab_sensors_30.jpg]]</td>
a RoboLAB program that will move the light sensor side-to-side until it detects
<td valign=top>[[Image:Lab_sensors_26.jpg]]</td>
the yellow brick. When it does,  it will stop running.  To move the light sensor, you must run the
<td valign=top>[[Image:Lab_sensors_27.jpg]]</td>
motor. If you reverse the direction of
<td valign=top>[[Image:Lab_sensors_28.jpg]]</td>
the motor, the light sensor will move in the opposite direction. If the yellow brick is removed from the
<td valign=top>[[Image:Lab_sensors_29.jpg]]</td>
test block, then the light sensor must move all the way to one side until it hits the touch sensor, then
</tr>
reverse direction and move to the other side until it hits the other touch sensor. This process must
</table>
continue until the yellow brick is placed back on the test block and the light sensor detects it. The
program ends once the yellow brick is detected</p>


<p align=center>[[Image:lab_sensors_2.jpg]]</p>
The Wait Block allows your program to wait (pause) for a certain amount of time, or until a particular condition is met. The program will run up to the Wait command and then pause until the set period of time has elapsed or until the specific condition has been met. If a Wait command needs information from a sensor, the sensor in the Configuration Panel of the block located in the bottom left corner of the screen. This is so Mindstorms knows where to find it.


<p class=caption>Figure 1: Test Assembly, angled view</p>
== Switch ==
[[Image:Lab_sensors_31.jpg]]


<p align=center>[[Image:lab_sensors_4.jpg]]</p>
The Switch Block command is in the Flow menu. It will split the path of the program based on a condition you set. For instance, the program will perform one of two tasks depending on the value it receives from a sensor. Sensor switches must be specified in the Configuration Panel so Mindstorms knows where to look for sensor input.


<p class=caption>Figure 2: Test Assembly, side view</p>
== Task Split ==
[[Image:Lab_sensors_32.jpg]]


<p align=center>[[Image:lab_sensors_6.jpg]]</p>
To create two independent tasks that run simultaneously, you must create a new beam to place blocks on.  As illustrated, begin on left side of the screen where the first line of code is written.  Hold the "Shift" key and your "pointer" will change to a spool of "wire." Drag the spool to your desired destination and double click to confirm the new beam.


<p class=caption>Figure 3: Test Assembly, top view</p>
== Loops ==
[[Image:Lab_sensors_33.jpg]]


<ol>
The Loop block is under the Flow menu.  You can set the loop to constantly check for certain conditions based on what criteria is selected in the Configuration Panel.  Loops can depend on Time, Count, Sensor or Logic Values. Loops can also be set to "Forever." This will continue to Loop the code inside of it until the program ends.
<li>Before we begin, we need to determine the light percentage that allows the sensor to
  detect the yellow brick.</li>


<li>Make sure
== Touch Sensors ==
  the RCX is turned on, the USB Tower is plugged in, and the RCX is within range
There are touch sensor blocks that use the Wait, Loop, and Switch structures.
  of the USB Tower.</li>


<li>Pull down the Project menu, select COM Port, and choose USB1. Click the check mark.</li>
<table>
<tr>
<td valign=top>[[Image:Lab_sensors_30.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_34.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_35.jpg]]</td>
</tr>
</table>


<li>Pull down the Project menu, select Interrogate RCX.</li>
The touch sensor Wait Block commands will pause the motor(s) until the touch sensor is pressed, released or bumped, depending on its configuration.


<li>Within a few seconds, the RCX Present indicator will turn green and the battery power meter will begin
The touch sensor Loop executes until the sensor is pressed, released or bumped, depending on its configuration.
to read. Notice the small gray buttons labeled 1, 2, and 3 on top of the picture of the RCX. These buttons
represent the <b><i>inputs </i></b>on the RCX.</li>


<p><b>Note: </b><i>If a window opens when you launch RoboLAB with </i>Install Extras <i>highlighted,
The touch sensor Switch takes one input and produces two outputs, splitting the path into two possibilities. The first possibility is the result when the touch sensor is pushed and the second is the result when the touch sensor is released.
you must click on the </i>Install Extras <i>button and </i><b><i>restart </i></b><i>RoboLAB. If
</i>Remove Extras <i>is highlighted, click the check box and continue.</i></p>


== Light Sensors ==
There are light sensor blocks that use the Wait, Loop, and Switch structures.


<li>Click on the 1 button. A gray box will appear. Click on the box and set the two menus to Reflection
<table>
and Percent.</li>
<tr>
<td valign=top>[[Image:Lab_sensors_26.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_37.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_36.jpg]]</td>
</tr>
</table>


<li>Input 1 (Sensor 1) is now set up to receive data from a light sensor. Plug a light sensor
The light sensor Wait Block will pause the motor(s) when the measured value of reflected light is greater than or less than a constant value, depending on its configuration.
onto Input 1 on the RCX. The sensor returns a percentage between <b><i>1 </i></b>and <b><i>100</i></b>.
Put the sensor in front of the yellow brick. When the number stabilizes, this percentage
is the amount of reflected light given off by the yellow brick. Your screen will look like Figure 4.</li>


<p align=center>[[Image:lab_sensors_7.gif]]</p>
The light sensor Loop executes until the measured value of reflected light is greater than or less than a constant value, depending on its configuration.


<p class=caption>Figure 4: Interrogate RCX window</p>
The light sensor Switch takes one input and produces two outputs, splitting the path into two possibilities. The first possibility is the result when the value of reflected light is greater than a constant value and the second is the result when the value is less than the constant.


<li>It is a good idea to test the sensor. To do this, shine the light sensor at
== Ultrasonic Sensors ==
different colors of Lego blocks. Note the light percentage returned and the factors that affect the reading,
There are ultrasonic sensor blocks that use the Wait, Loop, and Switch structures.
e.g., glossiness, distance, and color.</li>


<li>Now you will program the light and touch sensors. For the purposes of this lab, program the
<table>
<b><i>motor</i></b> to Port A, Input 1 is the <b><i>light sensor</i></b>, Input 2 is <b><i>touch sensor
<tr>
2</i></b>, and Input 3 is <b><i>touch sensor 3</i></b>.</li>
<td valign=top>[[Image:Lab_sensors_28.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_39.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_38.jpg]]</td>
</tr>
</table>


<li>Open the Functions palette and write a program that will use each of the touch sensors to help the
The ultrasonic sensor Wait Block will pause the motor(s) when the distance measured is greater than or less than a constant value, depending on its configuration.
light sensor detect the yellow brick. In order to do this, you will need to be familiar with the tools
on the Functions palette, shown in Figure 5.</li>


</ol>
The ultrasonic sensor Loop executes until the distance measured is greater than or less than a constant value, depending on its configuration.


<p align=center>[[Image:lab_sensors_8.gif]]</p>
The ultrasonic sensor Switch takes one input and produces two outputs, splitting the path into two possibilities. The first possibility is the result when the distance measured is greater than the constant value and the second is the result when the value is less than the constant.


<p class=caption>Figure 5: The Functions palette</p>
== Display block ==
<table>
<tr>
<td valign=top>[[Image:Lab_sensors_53.jpg]]</td>
</tr>
</table>


<h4>Wait For</h4>
The Display Block is used to alter the display of the NXT Brick.  You can choose either pre-installed images or print your own variables by wiring sensors or other variable containers to the block.


<p>[[Image:lab_sensors_9.gif]]</p>
== Timers ==
There are Timer blocks that use the Wait, Loop, and Switch structures.


<p>The Wait For icon allows your program to wait (pause) for a certain amount of
Timer structures are linked and used like the sensors are used; however, instead of getting values from sensors, they hold values in timer containers. Timers count the time elapsed from when they are reset.
time, or until a particular condition is met. The program will run up to the Wait For command and then pause until the
set period of time has elapsed or until the specific condition has been met. If
a Wait For command
needs information from a sensor, the sensor must be wired to a <b><i>port modifier </i></b>so RoboLAB knows where to find it.</p>


<h4>Forks and Splits</h4>
<table>
<tr>
<td valign=top>[[Image:Lab_sensors_29.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_40.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_41.jpg]]</td>
</tr>
</table>


<p>[[Image:lab_sensors_10.gif]] [[Image:lab_sensors_11.gif]]</p>
The Timer  Wait Block will pause the program as it waits for the timer value to match the input value.


<p>The Fork Split command is in the Structures menu. It will split the path of the program based on a condition
The Timer Loop executes a selection of code while the timer is less than or greater than a certain value.
you set. For instance, the program will perform one of two tasks depending on
the value it receives from the light sensor. Every Fork Split in the program <b><i>must </i></b>be accompanied by a Fork
Merge. This combination of a Fork
Split and a Fork Merge works like a pair of parenthesis. Every open parenthesis must be
closed. Sensor Forks must be connected to a <b><i>port modifier </i></b>so
RoboLAB knows where to look for sensor input.</p>


<h4>Task Split</h4>
The Timer Switch works the same as the light, touch and ultrasonic sensor switches. It compares the value of the timer to a set value and pauses the program until the timer equals the value it is being compared to.


<p>The Task Split command is also located in the Structures
== Variables ==
menu. It allows you to create two separate tasks that
Variables can be used to store Number, Logic or Text Values. Variables can "Written" (1) or "Read" (2).
will run simultaneously. When implemented, your original program becomes two
separate independent tasks. Each task must end with its own red stop light.</p>


<h4>Jumps and Lands</h4>
<table>
<tr>
<td valign=top>[[Image:Lab_sensors_42.jpg]]</td>
<td valign=top>[[Image:Lab_sensors_43.jpg]]</td>
</tr>
<tr><td align=center>(1)</td><td align=center>(2)</td></tr>
</table>


<p>[[Image:lab_sensors_12.gif]] [[Image:lab_sensors_13.gif]]</p>
In order to use variables correctly you should first define a variable by either "writing" it or by storing an input value from a sensor. You can then use the Variable by "reading" it. Both methods are used in Part A of this lab.


<p>The Jumps menu is found in the Structures
{{Laboratory Experiments}}
menu. When the program reaches a Jump command, it skips to wherever you have placed a Land command. Note that
there are <b><i>six </i></b>different colored Jump and Land commands. So, the red Jump will always correspond with
the red Land, and so on. You can use a Jump and Land with a Fork Split to make a <b><i>loop</i></b>.</p>
 
<h4>Loops</h4>
 
<p>[[Image:lab_sensors_14.gif]]</p>
 
<p>The Loops menu is under the Structures menu. Hover your cursor over each of the
icons to learn what they do. All loops must be used in conjunction with a Loop End command.</p>
 
<h4>Touch Sensors</h4>
 
<p>There are touch sensor structures that use the Wait For, Loop, and Fork structures.</p>
 
<p>[[Image:lab_sensors_15.gif]] [[Image:lab_sensors_16.gif]]</p>
 
<p>The touch sensor Wait For commands will pause the motor(s) until
the touch sensor is released or pushed, depending on the direction of the
arrow.</p>
 
<p>[[Image:lab_sensors_17.gif]]</p>
 
<p>The touch sensor Fork takes one input and produces two outputs, splitting the path into
two possibilities. The first possibility is the result when the touch sensor is
released and the second is the result when the touch sensor is pushed.</p>
 
<p>[[Image:lab_sensors_18.gif]] [[Image:lab_sensors_19.gif]]</p>
 
<p>The touch sensor Loop executes when the sensor is pushed or released, depending on the
direction of the arrow.</p>
 
<h4>Light Sensors</h4>
 
<p>[[Image:lab_sensors_20.gif]]</p>
 
<p>A light sensor Fork works in the same way as a touch sensor Fork, except there is an extra
connection that is attached to a <b><i>numeric constant</i></b>. The RCX
decides if the light sensor is returning a value higher or lower than the
constant, and chooses a program path based on that information.</p>
 
<p>[[Image:lab_sensors_21.gif]] [[Image:lab_sensors_22.gif]]</p>
<p>The light sensor Loop works just like the touch sensor Loop. Depending on the
value the sensor returns, the loop will either continue executing the code
inside the loop, or it will move on to the rest of the program.</p>
 
<h4>Timers</h4>
 
<p>[[Image:lab_sensors_23.gif]]</p>
 
<p>Timer structures are linked and used like the sensors are used; however, instead of getting
values from sensors, they hold values in timer containers. Timers count the time elapsed from
when they are reset.</p>
 
<p>Before using a timer, it  must be reset to <i>0</i>. This is done using the Zero Timer command
in the Reset menu. This command takes two inputs and gives one output.</p>
 
<p>[[Image:lab_sensors_24.gif]]</p>
 
<p>The Timer Modifier and value can be found in the same menu as the port and motor modifiers. The
timer modifier indicates which timer a structure is referring to. The value of the timer can be used
for comparison in a timer loop or a timer fork by using the timer value command.</p>
 
<p>[[Image:lab_sensors_25.gif]] [[Image:lab_sensors_26.gif]]</p>
 
<p>The Timer Loop executes a selection of code while the timer is less than or
greater than a certain value. The structure has four connections: one input for
the flow of the program, one output for the flow of the program, one to specify
which timer to look at, and one to specify what value the timer is being compared
to.</p>
 
<p>[[Image:lab_sensors_27.gif]]</p>
 
<p>The Wait For Timer will pause the program as it waits for the timer value
to match the input value. This structure has four connections: one input for
the flow of the program, one output for the flow of the program, one to specify
which timer to look at, and one to specify what value the timer is being
compared to.</p>
 
<p>[[Image:lab_sensors_28.gif]]</p>
 
<p>The Timer Fork works the same as the Light Sensor Fork and the Touch Sensor Fork.</p>
 
<p>It compares the value of the timer to a set value and pauses the
program until the timer equals the value it is being compared to. The structure
has five connections: one input for the flow of the program, one output for the
flow of the program if the timer is greater than the compared value, one output
for the flow of the program if the timer is less than the compared value, one
to specify which timer to look at, and one to specify what value the timer is
being compared to.</p>
 
<h4>Other Structures</h4>
 
<p>[[Image:lab_sensors_29.gif]]</p>
 
<p>The Flip Direction function will change the direction of any motor attached to it.</p>
 
<p>It has two inputs and one output. The output and the first input control the flow of
the program. The other input is the <b><i>port modifier</i></b> which indicates which motor's
direction should be changed.</p>
 
<p>[[Image:lab_sensors_30.gif]] [[Image:lab_sensors_31.gif]] [[Image:lab_sensors_32.gif]]</p>
 
<p>Containers are structures that can be used like Timers, because they hold values and can be
reset. They can be compared to loops and forks, and work like variables in C++, because they
can be added to and set to be equal to specific values. This structure should be experimented
with independently.</p>
 
<p>Remember, RoboLAB will tell you the name of each icon if you hover your cursor over it. When
you are finished, your TA must <b>test</b> your program.</p>
 
<p><b>Note:</b><i> To receive data from an input for a touch sensor, set the two menus to
 
</i>Switch <i>and </i>Boolean <i>and follow the same steps.</i></p>
 
 
<h3>Program 2: LabVIEW Temperature Indicator System</h3>
 
<ol>
<li>Open the heating and cooling program you designed in the <b><i>LabVIEW Lab</i></b>. Switch the case to
the <b><i>automatic </i></b>case.</li>
 
<li>From the Functions palette select All Functions, NI measurements, and Data Acquisition, then
select Analog Input, and then AI Sample Channel.</li>
 
<li>Drag and drop the AI Sample Channel node into the automatic case on the back panel of your existing
program.</li>
 
<li>Right-click on the AI Sample Channel node and select Visible Items, then Terminals.</li>
 
<li>Right-click on the AI Sample Channel node and click the Select Type menu. Select Scaled Value.</li>
 
<li>Wire the AI Sample Channel node as shown in Figure 6.</li>
</ol>
 
<p align=center><b><i>[[Image:lab_sensors_33.jpg]]</i></b></p>
 
<p class=caption>Figure 6:  AI Sample Channel node.</p>
 
<h4>Wiring the Sample Channel node</h4>
 
<ol>
<li>Device ID (<b><i>integer)</i></b>: the DAC board is recognized by the computer as <b><i>1</i></b>.</li>
 
<li>Channel (<b><i>string)</i></b>: for our purposes we will always set this to <b><i>thermocouple</i></b>.</li>
 
<li>Upper Limit (<b><i>real number)</i></b>: for our purposes we will always set this to <b><i>+5</i></b>.</li>
 
<li>Lower Limit (<b><i>real number)</i></b>: for our purposes we will always set this to <b><i>-5</i></b>.</li>
 
<li>Output.</li>
 
<li>Change the thermometer to an Indicator by right clicking on it and selecting Change to Indicator.</li>
<li>Connect the output from the Sample Channel to the thermometer.</li>
</ol>
 
<h4>Testing Your Design</h4>
<ol>
<li>Connect the <b><font color=#aaaa00>yellow</font></b> wire of the thermocouple to pin <b><i>3 </i></b>on the DAC
board.</li>
 
<li>Connect the <b><font color=#ee8866>orange</font></b> wire of the thermocouple to pin <b><i>4 </i></b>on the DAC
board.</li>
 
<li>Run the program <b><i>continuously</i></b>.</li>
 
<li>Hold the thermocouple between your fingers to make the temperature rise. Confirm that the indicator on your
program rises.</li>
 
<li>Place thermocouple in a cup of cold water to make the temperature fall. Confirm that the indicator on your
program falls.</li>
</ol>
 
<p>Have all sketches and original data signed by your TA. Your lab work is now complete.</p>
 
<p>Please clean up your workstation. Return all unused materials to your TA.</p>
 
<p>Refer to section <b><i>3 Your Assignment</i></b> for the instructions you need to prepare your assignments this week.</p>
 
 
[[Main_Page | Return to Table of Contents]]

Latest revision as of 00:28, 5 March 2014

This writeup is intended for use with the NXT. For the EV3 version, see Sensors (EV3).

Objectives

The experimental objective of this lab is to design various Mindstorms programs that use rotation, light, touch, and ultrasonic sensors.

These exercises will build a foundation of experience with sensors programming that can be used with the semester-long design project.

Overview

Mindstorms Program

Before we can learn to use sensors, we must become familiar with the NXT. Remember that the Mindstorms program is a compiler: a program used to make other programs. Just as a compiler is used when creating and debugging a C++ program, Mindstorms similarly compiles programs for the NXT. The Mindstorms interface is graphic; a text compiler like those used for C++ is not. However, the principle is very much the same. Actions are taken based upon circumstances set by the programmer.

The NXT houses all the programming instructions that control the movement of your robot. Once you have created your program in Mindstorms, it is uploaded to the NXT. The NXT then dictates the robot's motion. If your robot does not do what you intended, you must rewrite the program in Mindstorms and upload the corrected version to the NXT.

Assignment

A zip file including the following programs needs to be submitted to the EG Website. If you don't know how to make a zip file, read the page How to Compress Your Files in the Instructional Presentations section of this manual.

Lab Report

There is no lab report for this lab.

Team PowerPoint Presentation

There is no presentation for this lab.

Materials and Equipment

  • Lab PC
  • Mindstorms Software
  • USB Cable
  • NXT Unit

Procedure

Rotation Sensor

  1. Hover over the Sensor sub-palette (indicated by the yellow block on the left side of the window) and select the rotation sensor block
    Lab sensors 54.png
  2. Place it on the sequence beam
    Lab sensors 55.png
  3. Select the rotation sensor block placed on the sequence beam and change the output to rotations. The configuration panel can be found on the bottom left corner of the window.
    Lab sensors 56.png
  4. Hover over the Advanced sub-palette (indicated by the red block on the left side of the window) and select the Number to Text block
    Lab sensors 57.png
  5. Place the Number to Text block next to the Rotation Sensor block.
    Lab sensors 58.png
  6. Hover over the Action sub-palette (indicated by the light green block on the left side of the window) and select the Display block
    Lab sensors 59.png
  7. Place the Display block next to the Number to Text block.
    Lab sensors 60.png
  8. Configure the display block to show text.
    Lab sensors 61.png
  9. Expand the data hub by clicking near the bottom left corner of the Display block
    Lab sensors 62.png
    Lab sensors 63.png
  10. Wire the plugs to the blocks as shown below.
    Lab sensors 64.png
  11. Hover over the Flow sub-palette (indicated by the light orange block on the left side of the window) and select the Loop block
    Lab sensors 65.png
  12. Place the loop block to the left of the Rotation Sensor block
    Lab sensors 66.png
  13. Select all of the blocks to the right of the loop block and drag them into the loop block.
    Lab sensors 67.png

With the program now completed, upload it to your NXT and run the program. Turn the motor that is connected to port A and look at the display. You should see numbers on the display that correspond to the number of times you've turned the motor.

Example for Rotation Sensor

Suppose you wrote a program that had your robot's motors move forward for 3 seconds. The procedure for this was covered in Product Evaluation and Quality Improvement.

Lab sensors 68.png

Let's say you want to add the code that we previously made so that you can read the number of rotations that your robot made while it's moving. In order to do that, you'll need to create a new sequence beam.

  1. Start by clicking on the sequence beam below the starting point.
    Lab sensors 69.png
  2. Drag the beam to your desired destination and double click at that point to create the new beam
    Lab sensors 70.png
  3. Create the program previously made on the new beam
    Lab sensors 71.png

Example #2 for Rotation Sensor

Suppose we wanted to create a program that displayed the number of rotations, but instead of having it loop forever, it would stop measuring the number of rotations once you've rotated it three times.

  1. Create the program shown below
    Lab sensors 72.png
  2. Click on the loop block and change the control to sensor in the configuration menu.
    Lab sensors 73.png
  3. Change the sensor to Rotation Sensor.
    Lab sensors 74.png
  4. Set the loop conditions to greater than 3 rotations
    Lab sensors 75.png

Your completed program should look like the one on the image below.

Lab sensors 76.png

When you run this program, you should be able to see the number of rotations on the NXT's display. If you make more than 3 rotations on the motor, the program should end.

Ultrasonic Sensor

For this portion of the lab, you will learn how to use the ultrasonic sensor in order to determine the distance between the robot and an object. The code for obtaining a distance reading and displaying it on the NXT's screen is almost exactly the same as the code for the rotation sensor. The only difference is that instead of the rotation sensor block, you must use the ultrasonic sensor block

Lab sensors 77.png

Example for Ultrasonic Sensor

Suppose we wanted to write a program in which our NXT would show a happy face if the Ultrasonic Sensor detected a distance greater than 40 in, and show a sad face if the Ultrasonic Sensor detected a distance less than 40 in.

  1. To start off this program, we'll first need the switch block, which can found in the Flow sub-palette
    Lab sensors 78.png
  2. In the configuration panel, change the sensor to the Ultrasonic Sensor
    Lab sensors 79.png
  3. In the configuration panel, change the distance to 40
    Lab sensors 80.png
    If you look at the switch block, you'll notice two peculiar objects; a mountain and a flower. The mountain represents the case in which the sensor value is greater than 40, and the flower represents the case in which the sensor value is less than 40.
    Lab sensors 81.png
  4. Place a display block into each of the two sequence beams in the switch block
    Lab sensors 82.png
  5. Configure each display block so that it shows the correct image. Remember that the NXT should display happy face when the distance sensed by the Ultrasonic Sensor is greater than 40 in and show a sad face when the distance is less than 40 in.
  6. Place the entire switch block into a loop block.
    Lab sensors 83.png

Example #2 for Ultrasonic Sensor

Now suppose we wanted to add an additional case to the program where the NXT would display a caution sign when the Ultrasonic Sensor senses a distance less than 40 in but greater than 30 in, and display a sad face when the distance sensed is less than 30. In this situation, one switch case won't be enough, as the program will only check to see if the value obtained is greater than or less than one specific number. To overcome this problem, we'll need an additional switch case inside of the switch case we already have.

Lab sensors 84.png

In this program, when the distanced sensed is greater than 40, it will display a happy face, just like before. When the distance sensed is less than 40, it will check to see if it's greater than or less than 30, and show the appropriate image based on that.

Task

Create a program in which your robot maintains a distance of 8 and 20 in from the wall. In other words, your robot should move away from the wall if the ultrasonic sensor senses a distance less than 8 in and moves towards the wall if the ultrasonic sensor senses a distance greater than 20 in.

Touch Sensor

For this portion of the lab, you will learn how to use the touch sensor to control the actions of your robot based whether the touch sensor has been pressed or bumped.

Example for Touch Sensor

Suppose you wanted to create a program in which your robot would move when the touch sensor is pressed, and would stop when the touch sensor was released.

  1. Insert a switch block and have it set to the touch sensor
    Lab sensors 85.png
  2. Insert a move block into each of the sequence beams. Configure one motor block to move continuously and set the other motor block to stop
    Lab sensors 86.png
  3. Place the switch block into a loop block
    Lab sensors 87.png

Light Sensor

For this portion of the lab, you will learn how to use the light sensor in order to determine the light intensity of the sensor's reflected light. You will also learn how to use the light sensor to follow a line. The program for obtaining the light intensity and displaying it on the NXT's screen is the same as that of the rotation sensor and the ultrasonic sensor, except you need to use the light sensor block.

Lab sensors 88.png

Line Following Code Using One Light Sensor

The code for following a line using one light sensor is shown below.

Lab sensors 89.png

This block of code alternates between the left and right motor of the robot, depending on whether the light sensor sees the line. For example, let us assume that motor A is the left motor and motor C is the right motor. Thus, when the light sensor sees the line, the right motor (motor C) stops while the left motor (motor A) starts turning. This causes the robot to turn away from the line while moving forward. Also, when the light sensor doesn't see the line, the left motor stops while the right motor starts turning. This causes the robot to turn towards the line while moving forwards.

Line Following Code Using Two Light Sensors

The code for following a line using two light sensors is shown below.

Lab sensors 90.png

This block of code alternates between turning the left and right motor of the robot, depending on which light sensor sees the line. First, let us assume that motor A is the left motor and motor C is the right motor. Also, we'll assume that light sensor 1 is the left light sensor and light sensor 2 is the right light sensor. Thus, when the left light sensor sees the line, but the right line sensor doesn't, the left motor stops while the right motor starts moving. If the right light sensor sees the line, but the left light sensor doesn't see the line, the right motor stops while the left motor starts moving. If neither light sensor see the line, then both motors start turning. For positioning, the robot should be placed such that the light sensors are in between the line.

Tasks

Course 1

Lab sensors 91.png

Create a program in which your robot travels from point A to point C in a clockwise manner, then rotates 180 degrees and finally returns to point A.

Course 2

Lab sensors 92.png

Create a program in which your robot travels from point A to point H in a clockwise manner, rotates 180 degrees, and travels to point C.

Appendix: Advanced Mindstorms Reference

Wait

Lab sensors 30.jpg Lab sensors 26.jpg Lab sensors 27.jpg Lab sensors 28.jpg Lab sensors 29.jpg

The Wait Block allows your program to wait (pause) for a certain amount of time, or until a particular condition is met. The program will run up to the Wait command and then pause until the set period of time has elapsed or until the specific condition has been met. If a Wait command needs information from a sensor, the sensor in the Configuration Panel of the block located in the bottom left corner of the screen. This is so Mindstorms knows where to find it.

Switch

Lab sensors 31.jpg

The Switch Block command is in the Flow menu. It will split the path of the program based on a condition you set. For instance, the program will perform one of two tasks depending on the value it receives from a sensor. Sensor switches must be specified in the Configuration Panel so Mindstorms knows where to look for sensor input.

Task Split

Lab sensors 32.jpg

To create two independent tasks that run simultaneously, you must create a new beam to place blocks on.  As illustrated, begin on left side of the screen where the first line of code is written.  Hold the "Shift" key and your "pointer" will change to a spool of "wire." Drag the spool to your desired destination and double click to confirm the new beam.

Loops

Lab sensors 33.jpg

The Loop block is under the Flow menu.  You can set the loop to constantly check for certain conditions based on what criteria is selected in the Configuration Panel.  Loops can depend on Time, Count, Sensor or Logic Values. Loops can also be set to "Forever." This will continue to Loop the code inside of it until the program ends.

Touch Sensors

There are touch sensor blocks that use the Wait, Loop, and Switch structures.

Lab sensors 30.jpg Lab sensors 34.jpg Lab sensors 35.jpg

The touch sensor Wait Block commands will pause the motor(s) until the touch sensor is pressed, released or bumped, depending on its configuration.

The touch sensor Loop executes until the sensor is pressed, released or bumped, depending on its configuration.

The touch sensor Switch takes one input and produces two outputs, splitting the path into two possibilities. The first possibility is the result when the touch sensor is pushed and the second is the result when the touch sensor is released.

Light Sensors

There are light sensor blocks that use the Wait, Loop, and Switch structures.

Lab sensors 26.jpg Lab sensors 37.jpg Lab sensors 36.jpg

The light sensor Wait Block will pause the motor(s) when the measured value of reflected light is greater than or less than a constant value, depending on its configuration.

The light sensor Loop executes until the measured value of reflected light is greater than or less than a constant value, depending on its configuration.

The light sensor Switch takes one input and produces two outputs, splitting the path into two possibilities. The first possibility is the result when the value of reflected light is greater than a constant value and the second is the result when the value is less than the constant.

Ultrasonic Sensors

There are ultrasonic sensor blocks that use the Wait, Loop, and Switch structures.

Lab sensors 28.jpg Lab sensors 39.jpg Lab sensors 38.jpg

The ultrasonic sensor Wait Block will pause the motor(s) when the distance measured is greater than or less than a constant value, depending on its configuration.

The ultrasonic sensor Loop executes until the distance measured is greater than or less than a constant value, depending on its configuration.

The ultrasonic sensor Switch takes one input and produces two outputs, splitting the path into two possibilities. The first possibility is the result when the distance measured is greater than the constant value and the second is the result when the value is less than the constant.

Display block

Lab sensors 53.jpg

The Display Block is used to alter the display of the NXT Brick. You can choose either pre-installed images or print your own variables by wiring sensors or other variable containers to the block.

Timers

There are Timer blocks that use the Wait, Loop, and Switch structures.

Timer structures are linked and used like the sensors are used; however, instead of getting values from sensors, they hold values in timer containers. Timers count the time elapsed from when they are reset.

Lab sensors 29.jpg Lab sensors 40.jpg Lab sensors 41.jpg

The Timer  Wait Block will pause the program as it waits for the timer value to match the input value.

The Timer Loop executes a selection of code while the timer is less than or greater than a certain value.

The Timer Switch works the same as the light, touch and ultrasonic sensor switches. It compares the value of the timer to a set value and pauses the program until the timer equals the value it is being compared to.

Variables

Variables can be used to store Number, Logic or Text Values. Variables can "Written" (1) or "Read" (2).

Lab sensors 42.jpg Lab sensors 43.jpg
(1)(2)

In order to use variables correctly you should first define a variable by either "writing" it or by storing an input value from a sensor. You can then use the Variable by "reading" it. Both methods are used in Part A of this lab.