Sandbox/Sensors
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 and LabVIEW Software
- USB Cable
- NXT Unit
- Multiple NXT Sensor Array
- DAC Board
- Thermocouple
Procedure
Rotation Sensor
- Hover over the Sensor sub-palette (indicated by the yellow block on the left side of the window) and select the rotation sensor block
- Place it on the sequence beam
- 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.
- 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
- Place the Number to Text block next to the Rotation Sensor block.
- Hover over the Action sub-palette (indicated by the light green block on the left side of the window) and select the Display block
- Place the Display block next to the Number to Text block.
- Configure the display block to show text.
- Expand the data hub by clicking near the bottom left corner of the Display block
- Wire the plugs to the blocks as shown below.
- Hover over the Flow sub-palette (indicated by the light orange block on the left side of the window) and select the Loop block
- Place the loop block to the left of the Rotation Sensor block
- Select all of the blocks to the right of the loop block and drag them into the loop block.
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.
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.
- Drag the beam to your desired destination and double click at that point to create the new beam
- Create the program previously made on the new beam
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
- Click on the loop block and change the control to sensor in the configuration menu.
- Change the sensor to Rotation Sensor.
- Set the loop conditions to greater than 3 rotations
Your completed program should look like the one on the image below.
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
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
- In the configuration panel, change the sensor to the Ultrasonic Sensor
- In the configuration panel, change the distance to 40
- 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.
- Place a display block into each of the two sequence beams in the switch block
- 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.
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.
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.
- Insert a switch block and have it set to the touch sensor
- 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
- Place the switch block into a loop block
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.
Line Following Code Using One Light Sensor
The code for following a line using one light sensor is shown below.
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.
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
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
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
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
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
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
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.
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.
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.
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
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.
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).
(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.
|