LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1...

45
1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch Sensor Color Sensor Motor Gears Axle Straight Beam Angled Beam Cable

Transcript of LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1...

Page 1: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

1

LEGO Mindstorms Class: Lesson 1

Some Important LEGO Mindstorm Parts

Brick Ultrasonic Sensor Light Sensor

Touch Sensor Color Sensor

Motor Gears

AxleStraight Beam Angled Beam Cable

Page 2: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

2

The NXT-G Programming Environment

Page 3: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

3

Write a simple program to make the brick play a sound

1. Connect the USB cable between the brick and the computer.2. Select the Sound brick from the Action Blocks, and select a Sound file to play.

The 'Play Sound' Program The Sound Block Settings

3. Select "Save" from the file menu, and save your program with the name "Play Sound".4. Press the "Download and Run" button. Did it play?5. Unplug your brick from the computer. See if you can find the program named "Play Sound" on the

brick and play it directly from the brick.6. Congratulations! You just wrote and ran your first NXT-G program!

Change the "Play Sound" program to repeat itself 3 times using a loop

1. Select the "Loop" brick from the Flow Blocks and put it after the "Sound" block2. Put the "Sound" block from before inside the loop, and set the loop to repeat 3 times.

The Sound block inside a Loop The Loop Block settings

3. Download and play the"Play Sound" program. Did it do what you expected?

Page 4: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

4

Building a Door Alarm:

The parts you'll need:

Step 1: Attach connectors to the arms:

Step 2: Attach the arms together, and connect them to the ultrasonic sensor and the brick:

Page 5: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

5

Step 3: Connect a wire from the ultrasonic sensor to port 4 on the brick.

The Door Alarm Program:

Pseudocode:

1) Repeat forevera) Wait for Ultrasonic Sensor to detect an object closer than 16 inches

i) When an object is detected repeat the following 3 times(1) Make a sound

2) Return to 1)

Write the Program:

1. Start with a Loop (from the Common Blocks List). Set the Loop to run "Forever"

The Loop Block The Loop Block Settings

Page 6: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

6

2. Put a Wait Block (from the Common Blocks List) inside the loop. Set the control to "Sensor" and select"Ultrasonic Sensor" from the Sensor list. Make sure the Port is set to "4" (where the Ultrasonic Sensoris plugged in), and the Distance is set to "< 16 Inches".

A Wait Block inside the Loop Block

The Wait block Settings

3. Add a loop after the Wait block, and set the control to "Count" and the "Count" to "3".

A loop inside the main loop The inner Loop settings

4. Put a sound block inside the inner loop and choose a sound to play when the alarm triggers

The finished Door Alarm program

Page 7: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

7

5. Save the program with the name "Door Alarm". Download and test your program. Did it work? Checkwith your instructors if you have any problems.

If you finish the door alarm try this!

(1) Pretty Tricky: Can you change the program so that the alarm makes a different sound if the object itdetects is closer than 6 inches? Hint: You will have to use a "Switch" block to perform different actions if theobject is closer than 6 inches, or further than 6 inches. Think carefully about where to put the Switch block,and if some of the existing program needs to go inside the Switch block.

(2) Even Trickier: Can you change the program so that the screen on the brick displays the distance detected?This will require a block that will convert the distance (a number) to text (letters) to be displayed on the screenand a block to display the text. It also requires the use of data wires to carry the output data from theultrasonic sensor to the converter block, and from the converter block to the display block. Don't worry if youcan't do this one yet, we'll be covering data wires later in the class.

(see your instructors for help with the answers)

Page 8: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

1

LEGO Mindstorms Class: Lesson 2

Build A Mindstorms Vehicle - 5 Minute Bot:(Copied from http://www.nxtprograms.com/five_minute_bot/steps.html)

We will build a 3-wheeled robot vehicle with two motorized wheels, and an extra wheel that can pivot, calleda "caster". It is possible to build a robot with 4 or 6 wheels, or with treads, like a tank, but the 3-wheeledrobot is the easiest to program, so that's what we'll use. A robot with a caster generally runs best on verysmooth, flat surfaces, so that the caster can glide easily. For rougher surfaces, 4 wheels or treads may workbetter. Follow the steps below to build the robot vehicle.

Step 1: Attaching Wheels to the Motors

Page 9: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

2

Step 2: Attaching Angled Beams to the Motors

Step 3: Adding Wheels and Motors to the Bot

Page 10: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

3

Step 4: The Attachment Bar

Page 11: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

4

Step 5: The Caster

Page 12: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

5

Step 6 - The Wiring: Use two medium length (35 cm) wires to connect the motors to ports B and C on the NXT. Makesure that the motor on each side is connected to the port on that side.

That's it! You made a robotic vehicle!

Page 13: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

6

Making the Robot Move:

Move Block vs. Motor Block

To make the motors turn, we can either use the Move Block or the Motor Block.

There are several differences between the two blocks, but the biggest is that the Move block can control one,two, or three motors at one time, while the Motor block can only control one motor at a time. The Motorblock allows you to control the motor in more detail, but for driving, you usually want to have at least twomotors running two wheels at the same time. This is why we'll stick with the Move block to drive our robot.

The Move Block

The Move block controls one, two or three motors simultaneously.

Look at the settings for the move block. Let's figure out what each one does:

Port: This tells the Move block which of the A, B and C ports are connected to motors to control Direction: This determines whether the wheels turn forwards or backwards. Steering: Depending on where the slider is, this gives more power to one motor or the other (you

specify which of the two motors to use). Power: This specifies how much power overall to send to the motors. More power makes the motors

turn faster, less power turns them slower Duration: This determines how many turns the motors make, or how long in seconds they run for. The

duration can be specified in rotations, degrees, seconds or unlimited (run forever). Next Action: Brake means the motor will stop after it is finished. Coast means it will keep turning,

though without any additional power. We will almost always use Brake.

Q: What happens to the vehicle if the motor on the left side runs faster than the motor on the right side(assume both motors are turning forwards)? A:_______________________________________________

Page 14: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

7

Moving in a Straight Line

Let's figure out how far the car will go when we input certain data to the Move Block. Create a simple program in NXT-G, consisting of a single Move Block. Save it in your folder on the S drive with the name "Move Test".

Use this program to answer some of the questions below. You may need a calculator:

Turning:

The robot will always turn when its wheels spin at different rates. In most cases, it's hard to predict where itwill go when both wheels are turning at different rates, so we'll focus on two kinds of turns that we canpredict: Pivoting in Place, and Turning with One Motor. You can control which kind of turn you make with thesteering setting. The following steering values give you the following different kinds of turns:

Steering Value Steering Results100 Pivot in place to the right50 Turn to the right using one motor0 Go straight-50 Turn to the left using one motor-100 Pivot in place to the left

Pivoting in Place

If you turn both motorized wheels in opposite directions at the same power (speed),the vehicle will pivot around a point halfway between the two powered wheels, as

1. What is the circumference (distance around) of a tire on your robot (in inches)? _____________ inches2. How far do you think the car will go (in inches) when the wheel turns one time? ______________ inches3. Try it. How far did it go? _____________ inches4. How far do you think the car will go (in inches) when the wheel turns 5 times? _____________ inches5. Try it 3 times and compare your results: How far did it go?

______________ inches _______________ inches ______________ inches6. How accurate do you think the Move block is? __________________________7. If you want the robot to travel 20 inches, how many turns should the wheel make? __________ turns.

Try it. Did it work? ________________8. How many degrees correspond to one rotation of the wheel? ____________ degrees9. How far will the car go if the wheels turn 720 degrees? _______________ inches10. Try it. How far did the car go for at 720 degree rotation? _______________ inches

Page 15: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

8

shown in the picture below. The wheels travel around a circle whose diameter is the length of the axle.

The tricky part is to figure out how many rotations the wheels need to complete a single turn. Let's do thecalculation, and then compare it to real life by programming our vehicle. You will probably need a calculator:

Let's program our Move Block to test our answer. Slide the Steering slider all the way to the right to specify aright turn about the center of the vehicle. Put the number of rotations from answer 3 above into the Durationsetting to specify how far around the vehicle will turn.

Turning with One Motor

If you give power to only one motorized wheel, while holding the other one still,the vehicle will pivot around the unpowered wheel. Let's do a calculation like wedid above. Remember, in this case, the diameter of the circle the car makes istwo times the length of the axle.

Let's program our Move Block to test our answer. Slide the Steering slider to value 50 to specify a turn to theright using one motor. Put the number of rotations from answer 6 above into the Duration setting to specifyhow far around the vehicle will turn.

1. What is the distance across the circle the car makes when it turns? ____________________ inches2. The circumference (distance around) the turning circle is about 3.14 x diameter =_______ ___________ inches3. How many rotations does the tire have to make to travel the circumference of the circle? The formula for the

answer is: (Circumference of turning circle) ÷ (circumference of tire) = ________________________rotations

Download and run your program. Did it work? Check with your instructor if you're not sure of the answer.

4. What is the diameter (distance across) the circle the car makes when it turns? ____________________ inches5. The circumference (distance around) the turning circle is about 3.14 x diameter =_______ ___________ inches6. How many rotations does the tire have to make to travel the circumference of the circle? The formula for the

answer is: (Circumference of Circle) ÷ (Circumference of Tire) = ___________________________ rotations

Download and run your program. Did it work? Check with your instructor if you're not sure of the answer.

Page 16: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

9

Commenting your program:

Now that you know how to go straight and turn, you can combine a series of move blocks in a row to make your robotfollow a certain path. When you have a lot of Move blocks in a row that look similar, it is a good idea to placecomments near each move block so that you know what it's doing. That way if you have to make any changes, it's easy

to find the right place. You can add comments to your program using the comment tool at the top of the NXTwindow. An example of a commented program is below:

Challenges for You

(1) Can you write a program to make the Robot travel in a 12 inch square, using only 2 move blocks?

(2) Your instructor has set up a maze lined by blocks. Can you program your robot to navigate the maze withoutknocking over any blocks? Note: Before you start programming, measure the distances in the maze to predict whereyou want your robot to go. You will probably have to adjust your program afterwards, but you will have a good startingpoint.

(3) Your instructor has set up a "parking garage" made of blocks. Can you drive your vehicle from the starting point topark in the garage, without knocking over any of the blocks?

Page 17: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

1

LEGO Mindstorms Class: Lesson 3

Why Use Sensors?

1: Keep the Robot on Track:

In Lesson 2 we saw that it can be difficult to get the robot to navigate around its environment by just telling it where itneeds to go. Part of the problem is that small variations in its path can become really big errors once the robot hastravelled a long distance. For example, if you are trying to get your robot to move straight ahead, and it is angled offpath by just a little, it may only be a small amount off course after a few inches, but will be much further off course afterit has travelled a few feet. You can see this in the picture below.

1) Starting off course by a small amount can grow to a big error in a short while.

We can fix some of this inaccuracy by having the robot get information about its surroundings via sensors. This allowsthe robot to find out where it is by "seeing" what is around it, figure out if it is on the right path, and make corrections ifneeded.

2: Allow the Robot to interact with users and its environment

Sometimes you want the Robot to respond to a person or thing that it meets, or to give you information about theoutside world. A robot will only know about its environment if it can sense it in some way. With the right sensors, arobot can tell if it has come across a dangerous object or what the conditions around it are like (for example how hot orcold it is). For example, sensors allowed the Mars Rover, a robot that landed on the surface of Mars, to drive around thesurface of the planet, without falling into a deep hole, or running into a big rock. Sensors also allowed the robot todetect and communicate the conditions on Mars back to scientists on Earth.

Page 18: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

2

The Touch Sensor and Bumper:

Today's lesson focuses on the touch sensor. It is a simple device that can detect whether someone or somethingtouches, releases or "bumps" the orange button on its front. The button on the touch sensor is rather small, however,and it can be hard to press unless your robot is lined up perfectly with its target.

To make the sensor easier to use, we will build and attach a "bumper" to it. The bumper simply consists of a bar whichwill trigger the touch sensor when any part of it is pushed. That way an object doesn't have to be touching the orangebutton directly to trigger the touch sensor.

Programming with the Touch Sensor:

The touch sensor can detect three different conditions. It can notice if it has been:

[1] pressed (the orange button has been pushed inwards)[2] released (the orange has moved from being pushed in to its original position)[3] bumped (the orange button was pushed and released in a short period of time)

There are two main ways to program with the touch sensor. The Touch Sensor block, checks in with the sensor, andwaits to see if it has been triggered.

4) The Touch Sensor Block

The Wait for Touch Sensor will not do anything until the action specified (pressed, released or bumped) happens.

5) The Wait for Touch Sensor Block

2) The Touch Sensor 3) The Touch Sensorwith a Bumper

Page 19: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

3

Now that we know how it works, let's add a touch sensor to our 5 Minute Bot from the last lesson, and program it toperform some tasks…

Adding a Touch Sensor to "5-Minute Bot" to make "Bumper Bot":

We Start with 5-minute Bot from last time. (The motor wires have been removed to make it easier to see)

Attach the Touch Sensor to a Bumper:

Step 1:

Page 20: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

4

Step 2:

Page 21: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

5

Page 22: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

6

Attach rails to 5-minute Bot:

Page 23: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

7

Attach the Bumper to the 5-Minute Bot:

Page 24: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

8

Attach the Wires (Connect the Touch Sensor to Port 1):

You're done with Bumper Bot!

Page 25: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

9

Programming Exercises:

Exercise 1: Write a simple program to say "ouch" when the touch sensor is pressed and "hooray" when it is released.The pseudocode (program steps written in plain language) for this program looks like this:

Wait for touch sensor to get pressed. Say "ouch". Wait for touch sensor to get released. Say "hooray". Repeat from the beginning.

1) We will want our program to repeat itself so that it can say "ouch"/"hooray" over and over again. Most of the timewhen you repeat actions inside a program it's a good idea to use a loop.

2) Wait for the touch sensor to get pressed.

3) Make an "ouch" sound.

4) Now wait for the touch sensor to get released

Page 26: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

10

5) Say "hooray", and the loop will return you to the beginning of the cycle

Exercise 2: Write your own program to have the "Bumper Bot" drive until the bumper hits something, then stop, backup about 6 inches, turn right and start over again. The pseudocode would look something like this:

Start driving forward Wait until the bumper is touched Stop Back up about 6 inches Turn right Repeat from the beginning

Challenges for You

Challenge 1: See if you can get Bumper Bot drive up to a block, and stop when it touches it without knocking the blockover.

Challenge 2: Use the touch sensor to get Bumper Bot to knock a block off the edge of a low platform (your instructorswill set one up for you) without falling over the edge of the platform itself.

Challenge 3: (Advanced!): Have Bumper Bot travel to a wall, stop when it hits it and then travel backwards to the exactsame starting place. You need to use the rotation sensor and data wires to do this.

Page 27: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

1

LEGO Mindstorms Class: Lesson 4

The Light Sensor:

The light sensor allows a Mindstorms robot to "see" the difference between light and dark. It detectslight and dark surfaces by shining a light on the surface, and seeing how much of the light bounces back tothe detector. Light surfaces reflect more light, and dark surfaces reflect less light. You can also turn offthe light bulb in the light sensor, and then the sensor will detect the amount of light around it. We'll workwith reflected light, and use the light sensor attached to 5-Minute Bot to detect dark and light surfaces

that it drives over.

Because lighting conditions differ depending on where you are or what time of day it is, the light sensor needs to becalibrated every time you use it under different lighting conditions (for example if you move from a dark room to abrightly lit room, or take your robot outside). What looks to be one color in a dark room may look different in brightsunlight.

You have to tell the light sensor what you mean by "dark" and what you mean by "light". When you write a programthat checks the light sensor, the sensor returns a value between 0 and 100. 0 is the darkest and 100 is the lightest.When you calibrate the sensor, it asks you to show it the darkest color it will be detecting, and it sets that as the 0 value.Similarly, it asks to see the lightest color you will be detecting, and it sets that value to 100. We'll see how to calibratethe sensor later on.

Let's attach a light sensor to our robot.

Attaching the Light Sensor to the 5-minute Bot:

Take off the touch sensor from the last lesson so you are only left with the 5-minute Bot (below)

Page 28: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

2

You will need these parts:

Now we can assemble and attach the light sensor:

Page 29: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

3

Attach the light sensor to the underside of 5-minute-Bot

Connect the light sensor to Port 3 on the Brick. This is the default light sensor port.

That's it! You're ready to follow the light!

Calibrating the Light Sensor:

You can calibrate the light sensor directly from the NXT-G programming environment, or write your own program to doit. If you use the NXT programming environment directly, your brick needs to be plugged in to the computer forcalibration. Since we're using laptops, and can move them around easily, we'll use that method.

Page 30: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

4

Exercises:

Exercise 1: Calibrating the light sensor in the NXT-G programming environment

1) Plug the brick into your computer. Be sure it is turned on.2) Select "Calibrate Sensors" from the "Tools" menu in the NXT-G window.3) From the pop-up window (shown below), select light sensor, port 3, and click the "Calibrate" button.

4) Look at the screen on the brick. It is asking you to hold the light sensor over the darkest surface it will bescanning. The changing numbers you see are the "raw" unprocessed output from the light sensor, and theyrange from 0 to about 1000. Move the sensor around to surfaces with different lightness/darkness, and watchhow the numbers change. After you calibrate the sensor and use it in a program, the NXT-G programenvironment will convert the numbers to a value between 0 and 100.

5) Place the light sensor so that it is completely over a dark line on the paper your instructors have set up, thenpress "Enter" (the orange button).

6) The screen is now asking you to hold the light sensor over the lightest surface it will be scanning. Place the lightsensor over the white part of the paper your instructors have set up, then press "Enter".

7) Your light sensor is now calibrated. The brick will remember these calibration settings until the next time yourecalibrate it. You will only need to recalibrate if you are using the light sensor in a place with different lightingconditions.

Exercise 2: Write a program to display the final light sensor readings

Let's write a program to view the output from the calibrated light sensor. We're using some advanced concepts here,including data wires, which carry information from one NXT programming block to another. Data wires will let us takethe numbers that are output by the light sensor block, and use them as input to the display block. To input and outputdata to a NXT-G programming block, many blocks have what is called a data "hub" which clicks in and out of the datablock. You can click on the hub to make it slide in and out. Below is a picture of the Display Block with the data hubclosed, and opened. The ports on the data hub are for taking in and putting out different kinds of data, such asnumbers, text and images.

Page 31: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

5

A Display Block with the data hub closed:To open the Data Hub, click on the line atthe lower left of the Display Block.

A Display Block with the data hub open:Each port is for inputting and outputtinga different kind of information, such astext, start position, end position, etc.

You will need to know how to display written information on the NXT brick's screen. The screen can write almostanything you tell it to, but it only understands numbers in "text" format. Letters and words are automatically "text", butnumbers are not. To put the numerical (number) output from the light sensor into a form that the display block canunderstand, we will have to convert it to text. Create a program named "showlight.rbt" in the NXT-G programmingwindow (be sure to save it to the shared drive!), and follow the steps below to write it.

1) We will want to run this program repeatedly, so set up a forever loop:

2) Add a Light Sensor Block (from the Sensor Menu), and make sure that it reads Port 3

3) Add a Number to Text Block (from the Advanced Menu)

Page 32: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

6

4) Now move the cursor to the plug sticking out of the bottom of the Light Sensor Block. It will turn into a pictureof a little spool of wire. Click on the plug, and drag the cursor over to the plug to the left of the "#" plug at thebottom of the Number to Text Block. When you let go, you will create a yellow Data Wire connecting the outputof the light sensor block to the input of the Number to Text Block.

5) Add a Display Block, and click on the data hub to make sure it pops out.

6) Connect a data wire from the output of the Number to Text box to the capital letter "T" (for Text) on the DisplayBlock data hub.

Page 33: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

7

Set the Display block to display the input Text:

7) Add a Wait block, and set it to wait for 0.2 seconds before repeating

8) Now let's test out our program.o Run "showlight.rbt", and move the sensor around over different dark and light areas. Do you see the

readout change? When is the readout higher, and when is it lower? Is that what you expected?o Try rolling your robot over the dark lines on the paper that your instructors set up. Watch the values

change as the sensor starts to move over the line. Do the values make sense?

Page 34: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

8

Challenges:

Challenge 1: Write a program to make the robot drive until it detects a dark line and then stop. Use the paper that yourinstructors have provided, with electrical tape to make the dark lines.

Before you attempt this challenge, think about what it means for your robot to detect a dark line. Think about what thesensor readings will be as the sensor approaches the line. When the light sensor is over the white paper, the readingswill be pretty high. When the sensor is just a little bit over the black line, less light will get reflected, so the light sensorvalues will start to drop. The more of the sensor that is over the black line, the lower the light sensor values will be.

Hint 1: Use the Wait for Sensor Block to let your robot keep moving until the value of the Light Sensor has dropped pasta certain value. Think about what a reasonable value might be to indicate that the sensor is over the black line.

Hint 2: Make sure that the first Move Block in your program uses the unlimited setting, so that your program will checkthe light sensor while the robot is still driving. Otherwise, the robot will finish running the move block before it checksthe light sensor.

Challenge 2: Your instructors have created a "ring" out of dark electrical tape on white paper. The ring has a hole in it,large enough for your robot to drive through. Write a program that allows your robot to find its way out of the ringwithout crossing over a black line. Hint: When you encounter a black line, you will want the robot to stop, turn, andtravel in a different direction. So the pseudocode (description of your program in words) would look something like this:

1. Start driving forward.2. If you come across a black line, stop.3. Back up a few inches (you decide how many)4. Turn (you decide how far the robot should turn - try different values and see what works best!)5. Return to the start

Remember, each line in pseudocode corresponds to one instruction for the robot (and one programming "block" in NXT-G) Once you get the program working, experiment with different values for the turn. See which value works best forgetting your robot out of the "ring" the fastest.

Challenge 3: (Advanced!) Your instructor has provided paper with dark parallel lines. Write a program that lets therobot drive over two black lines, and stop on the third. There are many ways to solve this problem. See how creativeyou can be! Talk to your instructors and see if your solution matches theirs.

Page 35: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

1

LEGO Mindstorms Class: Lesson 5

Line Following with the Light Sensor:

In the last class we used the Light Sensor to detect when our robot encountered a dark line. In this class, we're going touse the Light Sensor to help our robots follow along a dark line. Specifically, we will follow the edge of the line.

Let's say we want our robot to follow a dark line on white paper. Remember, if we calibrated it, the light sensor willoutput a value from 0 to 100 depending on how much of the sensor is over the dark line. 0 means the sensor is totallyover the line. 100 means the sensor is only over the white paper. Imagine what the sensor would see if it moved fromleft to right over a dark line (see picture below):

Before we work with the line sensor, we'll need to calibrate it for the lighting conditions in the class today. Last weekyou loaded a program onto your robot called "Calibrate.rbt". Run it by holding the light sensor completely over theblack line, and pressing the orange button on the brick, then holding the robot over the white paper and pressing theorange button again. You can test the calibration by viewing the output of the light sensor in a Light Sensor Block in theNXT-G window. Ask your instructors for help if you don't remember how to do this.

Exercise 1: Write a line following program

We're going to tell our robot to follow the left edge of the line. In the picture above, we see that the sensor reading ishigh (> 50) if the sensor is mostly over the paper, and the sensor reading is low (< 50) if the sensor is mostly over theline. We can use this fact to keep the sensor lined up with the left edge of the line while the robot moves forward. Let'swrite some pseudocode that does this:

Get output from the sensor If the light sensor output is greater than ( > ) 50, we must be left of the line's edge

o Move forward and turn slightly to the right If the output is less than ( < ) 50, we must be right of the line's edge

o Move forward and turn slightly to the left Repeat from the beginning

Page 36: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

2

Now let's turn this pseudocode into an actual NXT-G program. Create a program called linefollow.rbt, and save it toyour folder.

1. Since the program repeats, we'll need a loop

2. Now notice we have an "If" statement in our pseudocode. This means we have to make a decision based on theoutput of the light sensor. We'll use the "Switch" block (from the Common menu) for this. Select the settingson the switch block to use the light sensor output, and make a decision based on whether it is greater than orless than 50.

Page 37: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

3

3. First fill in the top part of the switch statement. This is what will happen if the light sensor value is greater than50 (see that the "light symbol" looks bright). Remember that a value greater than 50 means that the robot ispositioned left of the line. We want to make the robot move forward, and slightly to the right to get closer tothe line. Instead of using a Move Block, we are going to use two Motor Blocks to do this. The motor blocks givethe robot a little more control over the turn, and make the movement smoother. Notice we have one motorblock for motor B, and one for Motor C. Since Motor B is on the left and Motor C is on the right, if motor Bspins faster (has more power) than Motor C, the robot will move to the right. Set the power for Motor B to 50and the power for Motor C to 10.

4. Now let's fill in the bottom part of the switch statement. This is what we do if the light sensor returns a valueless than 50. When the value is less than 50, the light sensor is mostly over the line. Since we want the robot tofollow the left edge of the line, we'll want it to move forward, and slightly to the left. In this case, we'll wantMotor C to have more power than Motor B. Add two motor blocks like we did in step 3, but set the power forMotor B to 10 and the power for Motor C to 50.

That's the whole program! Your instructor has made a course out of paper and electrical tape. Try placing therobot near the left edge of the line and seeing if it can follow the course.

Page 38: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

4

Challenges:

Challenge 1: The need for speed!

Your instructors have set up a course mapped out in electrical tape. Time how long it takes your robot to complete thecourse. Can you think of ways to make it go faster without losing its way? Hint: Try completing Challenge 2 below forone method to make your robot a speedier line follower.

Challenge 2: Write a 3-state line following program

The line following program we just wrote works best following a line where the curves aren't too sharp. If you try to getthe robot to follow a sharply angled turn, it may lose the line entirely. The robot also moves pretty slowly, since it has tospend so much time turning left and right to find the edge of the line. Let's try a method to make the robot move fasterand more accurately.

Instead of telling the robot what to do based on whether it is left or right of the line (two different states), let's make itmore sensitive to its position. We will let the robot travel straight if it is close to the edge of the line, and only turn leftor right when it moves away from the edge it is trying to follow. Since it reacts differently to three different conditions(left of edge, right of edge, close to edge) this is called a 3-State Program. Here is the pseudocode we want to follow:

Get output from the sensor If the light sensor output is greater than ( > ) 60, we must be left of the line's edge

o Move forward and turn slightly to the right Otherwise the light sensor output must be less than (<) 60

o If the light sensor is less than 60 but greater than 40, we must be close to the edge Move straight forward

o Otherwise the light sensor is less than 40 and we are right of the line's edge Move forward and turn slightly to the left

Repeat from the beginning

The code may look tricky because it has an "if" statement inside another "if" statement, but if you think carefully, youwill see how to write the program. You will need to use more than one switch statement to get it to work. Ask yourinstructors if you need help.

Page 39: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

5

An example of a 3-state line following program.

Page 40: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

Lesson 6:

No formal lesson - just built two lines with electrical tape. Placed a ball on a ring at the end, and twoloops along the way. Challenged the kids to design a robot to collect all 3 objects and bring them backto base.

Page 41: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

LEGO Mindstorms Class: Lesson 8

Gears:

The LEGO Mindstorms Set comes with all sorts of gears, but up to this point we haven't explored how to use them.Today we're going to see how gears work, and how they can be useful in building robots and other devices. Below is apicture of some of the basic gears that come with the Mindstorms set, as well as a definition of some of the differentgear types that are included. The different shaped gears have different purposes, but we'll focus on the spur gears.

Some Different Gear Types

Spur Gears - Havestraight teeth, and theiraxles are parallel (linedup) with each other. Theyare the most commontype of gear.

Bevel Gears - Canchange the direction theaxle is rotating by anangle (usually 90degrees)

Rack and Pinion Gears -Combine the motion of around gear with a flat-toothed platform. This letsyou convert circular motionto straight-line motion.

Worm Gear - This gear consists ofa cylinder with a spiral groove(like a screw) that turns. Eachtime it turns around once, theround gear moves forward by onetooth. The cylinder can turn theround gear, but turning the roundgear does not move the cylinder.

LEGO Mindstorm Gears (spur gears are underlined in red)

Page 42: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

Question 1: In the pictures below, what types of gears are being used in the hand mixer and the electric mixer? Writethe answers in the spaces below the pictures:

Gear Type:______________________________ Gear Type________________________________

To make the Mindstorms robot move, motors provide power to turn the wheels. We use gears for three main reasons:

1. Changing the speed of the wheels2. Changing the power going to the wheels3. Change the direction the axles are turning

Let's see how that works. Gears have "Teeth" that mesh together, so when one gear turns, it makes the other gear turn,too. Notice that the gears turn in opposite directions. The driver gear is the one that provides the power, and thefollower gear gets turned by the driver. The gears in the first picture below are the same size, so each time the drivercompletes one turn, the follower completes one turn.

Two Gears Turn in Opposite Directions With Three Gears, the Driver and FollowerTurn in the Same Direction

Gearing down and Gearing Up:

When you use two gears of different sizes, you will find that they turn at different rates. The picture on the left belowbelow show a gear with 8 teeth turning a gear with 24 teeth. Because the small gear (the driver) goes around threetimes (8 x 3 = 24) for every one time the follower gear goes around, this means that a wheel attached to the followeraxle will go three times more slowly than the driver gear. Similarly in the picture on the right, for every time the drivergear turns once, the follower gear will go around three times. Therefore the follower axle is turning three times as fastas the driver axle.

Page 43: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

Gearing Down(8 Tooth Gear Drives a 24 Tooth Gear)

Gearing Up(24 Tooth Gear Drives an 8 Tooth Gear)

The follower axle turns more slowly than thedriver but has more power.

The follower axle turns more quickly than thedriver but has less power.

Making a LEGO Gear Chain:

You can take advantage of gear ratios to put together a set of gears where the follower goes much more quickly thanthe driver (or vice versa). An example of a gear chain is shown below. The because the big gears have 40 teeth, and thesmall gears have 8 teeth, the small gears turn around 5 times for every turn of the big gear (5 x 8 = 40). That means, ifwe are gearing up, each set of gears increases the speed of the axle by a factor of 5. Since we have geared up twice, wehave increased the speed by a factor of 5 x 5 = 25 times!

Picture 1: A gear train. Picture 2: The same gear train, built a different way.

As we said above, turning the rightmost crank in each picture, makes the leftmost crank turn 25 times faster. There is atrade-off for this increased speed, however. The faster crank will have a factor of 25 times less power.

Exercise 1: Build a gear chain similar to the pictures above, and see if you can feel the difference in power that you getwhen turning each handle. You can feel this with your hands by trying to hold one handle still while trying to turn theother. You will notice that one of the handles will be harder to turn than the other one.

Exercise 2: Can you build a gear chain so that the follower gear goes more than 100 times faster than the driver gear?Try it!

Page 44: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

Question 2: In the picture below, if gear A is turning clockwise, as shown, what direction are each of the other gearsgoing (clockwise or counter-clockwise)? Which gear is turning the slowest?

Gear A: Clockwise________________________

Gear B: ________________________________

Gear C: ________________________________

Gear D: ________________________________

Slowest Gear? ___________________________

Exercise3: Build a "Gear-Bot" This is an engineering challenge. Can you build a robot that uses gear ratios to increaseits speed? Your instructor has a geared robot that you can use as an example. How fast can you get your robot to go?

Page 45: LEGO Mindstorms Class: Lesson 1 › cms › lib02 › PA01001072...1 LEGO Mindstorms Class: Lesson 1 Some Important LEGO Mindstorm Parts Brick Ultrasonic Sensor Light Sensor Touch

LEGO Mindstorms Class: Tug-of-War Challenge!

This class we will build robots which will compete against each other in a game of Tug-of-War. Two robots will beconnected by a string with a paperclip on each end. When the referee says "start", the robots must wait (at least) 3seconds, then start pulling. The winner is the robot which is able to pull its opponent over the line that is initiallyhalfway between them.

Rules:

1. No part of the robot may extend past the paperclip attached to the string. This means that the string can't wraparound your robot. It must be attached to a part of your robot that is facing its opponent.

2. Robots must wait 3 seconds after the referee says "start" to start pulling. Put this delay in the program youwrite for your robot.

3. If any part of a robot comes off during the pulling, that robot has lost the match. Build a sturdy structure, soyour robot won't break.

4. The robot that pulls any part of its opponent over the midline is the winner.5. Robots must have wheels, and pull their opponents by turning their wheels (you can't make a very heavy brick

that the opponent has to drag)6. The total weight of the robot must be less than 1 Kilogram (about 2.2) pounds. Your instructor has a scale so

that you may check your robot's weight.

Strategies:

Weight - Heavier robots are harder to pull, but you must keep total weight under 1 Kilogram. Gears - Remember, gearing UP (connecting the drive axle gear to a larger gear, which is connected by an axle to

the wheel), will increase your power. Traction (multiple tires) - We don't have treads available, but adding tires may help increase traction (the ability

of your robot not to slip) on the ground. Angle of pull - You may find yourself at an advantage if you try to pull up on your opponents robot, causing it's

wheels to lift up off the ground. Of course, this means that you will need a tall robot, and you will need to find away to keep it from tipping over.

If you don't know where to start, ask your instructor for directions to building a simple robot car. You can makeadditions to your robot from there.