Sensors

From EG1004 Lab Manual
Revision as of 21:04, 22 July 2009 by Admin (talk | contribs)
Jump to: navigation, search

1 OBJECTIVES

The experimental objective of this lab is to design a Mindstorms program that uses light, touch and ultrasonic sensors to stop a motor when a red ball is detected.

We will learn to use light, touch, ultrasonic and rotation sensors so that we can use these tools in our semester-long design projects.

2 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.

3 YOUR ASSIGNMENT

  • A Zip file including all the programs needs to be submitted to the EG1004 Web site. 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.

4 MATERIALS AND EQUIPMENT

  • Lab PC
  • Mindstorms and LabVIEW Software
  • USB Cable
  • NXT Unit
  • Multiple NXT Sensor Array
  • DAC Board
  • Thermocouple

5 PROCEDURE

The program is broken up into three main parts. In Part A you will use the touch sensors to start, stop and reverse the direction of the motor. In Part B you will add the ultrasonic sensor to the program and have it reverse the direction of the motor when it is greater than 20cm or less than 8cm from the wall. In Part C you will add the light sensor to the program in order to stop the motor when it detects a red ball. Your TA will provide you with access to a testing assembly. You can see pictures of the testing assembly in Figures 1-3. Remember to test after each part.

Part A:

In this part of the exercise you first create a program that causes Motor A to move when the touch sensor connected to Port 1 is pushed and stop when the touch sensor connected to Port 2 is pushed. Also if the touch sensor connected to Port 1 is pushed while the motor is running then the motor must flip direction. There is a motor connected to Port B, and this motor will control the speed of Motor A, using the motor's rotation sensor. The following steps will help you build part of this program.

TASK 1: STORE VARIABLE

  1. Open MindStorms 1.1. Create a new program.
  2. To use the rotation sensor to control the speed of the motor, we will use the Rotation Sensor Block to read the value of the Rotation Sensor on Port B.
    Lab sensors 1.jpg
  3.  

  4. Next we will have to convert the value of the Rotation Sensor to a number value that the motor can use to determine speed. We will use the Math Block.
    Lab sensors 2.jpg
  5.  

  6. Change the block to Division. You can alter the settings of a block in the Configuration Panel located in the bottom left corner of the window.
    Lab sensors 3.jpg
  7.  

  8. To take the value of the Rotation Sensor, use the drop down menu of the Rotation Sensor block. Also do this for the Math Block.
  9. Lab sensors 5.jpg

  10. Next, highlight the degrees on the Rotation Sensor Block and wire it to "A" on the Math Block.
  11. Lab sensors 4.jpg

    This takes the value of the Rotation Sensor and uses that as input A on the Math Block.

  12. In the Math Block type "2" into the "B" box. This will convert the degrees of the Rotation Sensor to a percentage that the motor value can read.  We would ideally like to use 1.8 (i.e Divide by 180 degrees, then Multiply by 100%), but MindStorms 1.1 can only recognize whole numbers, not decimals.
  13.  

  14. Since we will be using this value further along in the program, we want to store this value as a variable. Use a Variable Block and string it next to the Math Bock.
    Lab sensors 6.jpg
  15.  

  16. Change the Variable Block settings to "Write" and "Number 1".  Using the drop down menu in the Variable Block, wire the "#" output from the Math Block to the "#" input on the Variable Block. The string of code should look like this:
    Lab sensors 7.jpg
  17.  

  18. We want to constantly check the value of the Rotation Sensor and change the speed accordingly. To do this we want to use a Loop. Bring out a Loop, select all the code that was written and place it inside the loop. The final line of code should look like this:
    Lab sensors 8.jpg
  19. TASK 2: USE VARIABLE

  20. In a separate task we want to write the code to start our program. To create another task line hold the "Shift Key" and click and drag the beam from  the existing white beam on the left side of the screen. Double click, to confirm the length and location of the new beam.
    Lab sensors 9.jpg
  21.  

  22. Create a new line under the first line of code written.
  23. We want our program to start when the Touch Sensor on Port 2 is bumped. Use a Wait Block.
    Lab sensors 10.jpg
  24.  

  25. Its default is a Wait For Touch. Change the settings accordingly to Port 2 and Bumped.
  26. Next, we want to use the stored Variable. Since we will be using the Motor on Port A, we must account for both direction and speed of the motor. We will use another Variable block to determine direction. Place this after the Wait for touch block.
    Lab sensors 11.jpg
  27.  

  28. This will be set to Logic 1 and "Write". Logic dictates that True will be Forward and False will be Reverse.  The Logic Variable Block must be set to True in order for the motor to go forward when the program starts.
  29. In order to easily debug the program when completed, and to have a visual of when the button is pressed, insert a Display Block. Then use the settings in the control panel in the bottom left of the screen to set the Display Block to print a Smiley Face to the display.
    Lab sensors 45.jpg
  30.  

  31. Now we want to use the Stored  Variable in conjunction with a Variable Logic Block to control the Motor A.  Place another Variable block set to Number 1 and "Read". This will read the value from the Stored Variable 1. Then place a Variable Block set to Logic 1 and "Read". This will read the value from the previous Variable Logic  Block.  Lastly place a Motor A Block. Your task so far should look like this:
  32. Lab sensors 46.jpg

  33. Wire the Variable and Logic Blocks to Motor A using their respective drop down menus.  Remember, you want to control Direction and Power of Motor A.
  34. Lab sensors 13.jpg

  35. Finally, you want to constantly control the speed of Motor A. Insert a Loop into the string of code and place inside of it the two Variable Blocks and the Motor A block. Your final code for this task should look like this:
  36. Lab sensors 48.jpg

    TASK 3: MOTOR FLIP DIRECTION

  37. The next task we want to accomplish is to be able to switch the direction of Motor A if the Touch Sensor on Port 1 is pressed.  First, create a new task line as described in Task 2.
  38. Since we want the program to differentiate between when the Touch Sensor is pressed from when it is not pressed, we will use a Switch Block.
    Lab sensors 15.jpg
  39.  

  40. Place the Switch Block on the new task line. Its default is set to Touch Sensor.
  41. The Top and Bottom sections of the Switch Block correspond to whether the Touch Sensor is pressed or not pressed, respectively. Since we want nothing to happen when the Touch Sensor is not pressed, leave the Bottom section empty.
  42. As in step number 11, add another Display Block in order to display a warning image. This is necessary in order to confirm that the button has been pushed and reacted properly.
  43. In the Top Section, we will first place a Variable Block set to Logic 1 and "Read". This will read the value from the Variable Logic Block we created in Step 10.
  44. Since the direction of the Motor corresponds to the True or False value stated in the Variable Logic Block from Task 2, it is then possible to use a Logic Block to change its value.
    Lab sensors 16.jpg
  45.  

  46. The Logic Block allows you to use operations such as AND, OR & NOT in the Configuration Panel in the lower left corner of the window. Now place a Logic Block set to "NOT" in the code string.
  47. To change the value of the Variable Logic Block from its value of True to False, wire the "Value" of the Variable Logic Block to "A" in the drop down menu of the Logic Block. Next we want to place another Variable Block set to Logic 1 and "Write". Now wire the "Result" of the Logic Block to the "Value" of this Variable Logic Block via the drop down menus. We are basically saying that if the Value of Logic 1 is TRUE, then "NOT TRUE" gives a result of FALSE.  Your code should look like this:
  48. Lab sensors 49.jpg

  49. Next we want to place a Wait Block changed to Time.
    Lab sensors 18.jpg
  50.  

  51. Have it Wait for 1 second.
  52. After the Wait for 1 second. Place another Display Block with the display setting to reset. This will clear the screen and allow for other objects to be printed to the screen later on in the program.
  53. Lastly, we want to constantly check if the touch sensor is being pressed, so place the entire Switch Block within a Loop Block. The Wait Block is necessary due to the fact that when the Touch Sensor is pressed we want the program to Wait a certain amount of time so the Motor can change direction before the Loop rechecks its direction. Your final code for this task should look like:
  54. Lab sensors 19.jpg

    TASK 4: STOP MOTOR A AFTER TOUCH SENSOR 2 IS PRESSED

  55. The last task simply ends the program when Touch Sensor 2 is pressed. First, create a new task string.
  56. Place two Wait Blocks in sequence. Set them to Touch Sensor and "Bumped." The first Wait Block is to account for the Start of the program, as indicated in Step 9. The second Wait Block will be used to determine when to end the program.
  57. Next insert another Sad Face Display Block in order to double check when the program is about to stop. To make sure that the program does not stop before the display is shown, add in a one second wait block.
  58. Next, place a Stop Block.
    Lab sensors 20.jpg
  59.  

    This Block will stop the whole program.

  60. Your final code for this task should look like:
  61. Lab sensors 51.jpg

    If you followed each step correctly, your Final Code for Part A should resemble:

    Lab sensors 52.jpg

  62. Test your Program.  After a TA approves that it works correctly, Save it and move onto Part B.

Part B:

In this part of the program you will modify what you had created in Part A to include the ultrasonic sensor. In this portion of the exercise you will add another task to utilize the ultrasonic sensor connected to Port 3. In addition to its functionality from Part A, the motor must now reverse itself when the ultrasonic sensor on Port 3 detects a distance greater than 20cm or less than 8cm. The idea behind this is to keep the motor on the track at all times. Using what you have learned in Part A you can create this portion of the program on your own. Remember to save your VI. Once you are done, you may move on to Part C.

Part C:

In this part of the program you will modify what you have done in Parts A and B.You will add the light sensor to Port 4 and use input from that to stop the motor. When the light sensor connected to Port 4 detects the red ball, the motor will stop. You will need to determine what the light sensor value for the color red is. To do this, maneuver the red ball near the light sensor.On the NXT module, go to the View menu, Reflected Light, and choose Port 4.The percentage value displayed on the NXT module is the light sensor value for red. Use this to finish this final part of the program.

When this program is done the touch sensor on Port 1 should be able to start Motor A and also flip its direction.The touch sensor on Port 2 should be able to stop Motor A and end the program. The ultrasonic sensor on Port 3 should be able to reverse the direction of the motor when the distance of the motor is greater than 20cm or less than 8cm from the wall. And the light sensor on Port 4 should be able to stop Motor A and end the program when it sees the red ball. Also, the speed of Motor A is controlled by Motor B.

Lab sensors 23.jpg

Figure 1: Test Assembly, angled view

Lab sensors 24.jpg

Figure 2: Test Assembly, side view

Lab sensors 25.jpg

Figure 3: Test Assembly, top view

Testing

In order to test your program, ask your TA for the assembly, connect the NXT to the USB outlet and run the program you created.

Have all sketches and original data signed by your TA. Your lab work is now complete.

Please clean up your workstation. Return all unused materials to your TA.

Refer to section 3 Your Assignment for the instructions you need to prepare your assignments this week.

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.