43
Lego NXT Programming for FLL Competition A primer on sensors Jim Keller ([email protected]) University of Pennsylvania GRASP Lab 26 August, 2012

Lego NXT Programming for FLL Competition A primer on sensors Jim Keller ([email protected]) University of Pennsylvania GRASP Lab 26 August, 2012

Embed Size (px)

Citation preview

Page 1: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Lego NXT Programming for FLL Competition

A primer on sensors

Jim Keller ([email protected])University of Pennsylvania GRASP Lab

26 August, 2012

Page 2: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Agenda

• Overview of Robotics with Lego NXT• NXT Sensors

– Ultrasonic, touch, light, color, rotation

• Lunch• Integration of Sensors with other features

– Switches • Two position and multi-position

– Loops– Basic autonomous motion (line following basics)– Advanced autonomous motion (smooth line following)

Page 3: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Lego NXT – FLL Overview

• NXT is Lego’s answer to lowering the robotics threshold to the middle school level– Robust modular system

• Mechanical “technic” building elements

• Motors to enable motion• Sensors to enable limited

perception• programmable micro-

processor (the “brick”) to integrate sensors and motors into robots

Page 4: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Lego NXT – FLL Overview

• NXT is Lego’s answer to lowering the robotics threshold to the middle school level– Robust modular system

• Mechanical “technic” building elements

• Motors to enable motion• Sensors to enable limited

perception• programmable micro-

processor (the “brick”) to integrate sensors and motors into robots

Page 5: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Lego NXT – FLL Overview

• NXT is Lego’s answer to lowering the robotics threshold to the middle school level– Robust modular system

• Mechanical “technic” building elements

• Motors to enable motion• Sensors to enable limited

perception• programmable micro-

processor (the “brick”) to integrate sensors and motors into robots

Color

Rotation

Page 6: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Lego NXT – FLL Overview

• NXT is Lego’s answer to lowering the robotics threshold to the middle school level– Robust modular system

• Mechanical “technic” building elements

• Motors to enable motion• Sensors to enable limited

perception• programmable micro-

processor (the “brick”) to integrate sensors and motors into robots

Page 7: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Basic Robot Topology• A robot is a system

– System processor (the Lego “brick” in our case) repeats a loop of steps in a program so the robot can do something useful

UnaugmentedVehicle

ControlActuators

FeedbackSensors

CommandProcessing

ReferenceCommands

(if tele-operated;Not allowed in FLL)

SystemProcessor

Action/Response

InputDevice

Page 8: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Basics of NXT Robot Construction:

What we need to Facilitate Motion Commands

• Most Lego NXT robots use a differential drive to move– 2 independently controlled motors

• Both commanded in “positive” direction=move forward• Both commanded in “opposite” direction=move backward• Left motor forward/right motor backward=turn right• Left motor backward/right motor forward=turn left

– Robot can pivot about the point midway between the drive wheels (most reliable way to turn)

– An additional free wheel or support is needed to balance the robot• Castering free wheel should be at the rear of the robot and be free to

rotate

Page 9: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Some Critical Details to Keep in Mind When Building Your FLL Robot…

• Be sure you keep track of how to command the robot to drive forward or turn:

This is the Direction Lego calls Positive or Forward, so take note of how the motors are arranged on your robot because for some designs the motors will be turning backwards when the robot moves forward…not a big deal, you just have to know how to set up the motor commands…

If you use a light sensor to guide line following, make sure it is in front of (forward of) the drive wheels so you don’t have to worry about reversing the turning controls; mount the sensor so its light is about ¼ “ above the board

Page 10: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How to use the Sonar Sensor• Plug the sonar into one of the numbered ports• The sonar does not require any calibration• View the sonar output directly on the NXT display using the “View” option

• The sonar detects the closest object in its beam– Lego says it can measure distances up to 255 cm (> 8 ft.) with a precision of ± 3 cm.– Large objects with hard surfaces provide the best readings – The “beam” is about ±30⁰ wide but is not perfectly symmetric (right “eye” is the sender;

left is the receiver)

Page 11: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Touch Sensor

• Touch sensor has three states it can sense:– Pressed– Released– Bumped

• Can directly drive a switch or output a true-or-false output to another block

Page 12: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How to Use the Light Sensor (part 1)

• The light sensor is the only NXT sensor that can be calibrated by the user– What is calibration? It is a way of adjusting the way readings are

taken to account for conditions• Our eyes adjust from dark to bright environments on their own• If the Lego light sensor works well in a bright setting, it may not do very well in a dark

setting. If we know we have to work in a dark room, we can CALIBRATE the sensor to account for this… The NXT brick will remember the “calibration” after we turn off the power.

• Caution: the VIEW utility does not let you see the calibrated output of the NXT Light sensor, so always use a display block to check out your calibration.

sensorlight

Page 13: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How to Use the Light Sensor (part 2)• Mount the sensor within ¼ to ⅜ inch

from the board– To minimize interference from

ambient light

• Select the “generate light” option in the function window

• Plug the sensor into port #1• Calibrate the light (see my program

cal_light_port1.rbt)– 100 = brightest light (reflection from a

white surface?)– 0 = darkest light (reflection from a

black surface or no reflection at all?)– Now “50” will correspond to the

sensor right over the division between light and dark…

Page 14: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How to Use the Light Sensor (part 3)To use it to follow a line:• Position sensor between the drive wheels AND slightly in front of them so we

can easily understand how to use its readings to steer (if you mount it behind the wheels then all the steering commands have to be reversed )

• Use the light sensor values to steer… if we see 50, then we want to go straight… less than 50 ↔ we can go one way… greater than 50 ↔ we can go the other way– We can even use the magnitude of the difference with respect to 50 to decide how hard to

steer…

• All my line followers have the line on the left but you can do it either way…

What the light sensor sees with respect to colors:

What we see what the sensor “sees”

Page 15: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Color Sensor

• Cannot be calibrated like light sensor• Can detect 6 colors• Has an illumination feature: red, green, or

blue– If you need to detect one of these, use

illumination (red objects reflect red light with the highest intensity and so forth…)

1 = Black 2 = Blue 3 = Green 4 = Yellow 5 = Red 6 = White

Page 16: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Rotation Sensor

• Built into motors– Sensor accumulates motion from the time when

robot is powered on• Can be reset as required to count from specific points

in the program

This is the Direction Lego calls Positive or Forward, so take note of how the motors are arranged on your robot because for some designs the motors will be turning backwards when the robot moves forward…not a big deal, you just have to know how to set up the motor commands…

Page 17: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Line Follower Boot CampWhat do we need?

• Moving and steering commands• Light calibration – So we can tell the robot how and when to steer

• Loop commands– We need to know when to start following and

when to stop

Page 18: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Basic Line Following…

• Set up the robot to have two options:– Turn left if the light sensor reading is greater than 50…

• Robot must be on the right side of the line

– Turn right if the light sensor reading is less than 50• Robot must be on the left side of the line (over a dark area)

• Robot is going to zigzag – all it “knows” to do is turn left or right…

Page 19: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Semi-smooth Line Following• Break the range of the light sensor reading into 5 “blocks”

– Decide how fast you want the robot to go when it is perfectly following (in my case, I chose 50%) : this is your “normal speed”

– If we are calibrated, we have the full range between 0 and 100• If the reading is less than 20, then turn hard right by reducing power to motor A (on right

side) a large amount and setting motor B (on the left side) to the normal speed• If the reading is between 20 and 40, then make a gradual right turn by reducing power to

motor A a small amount while keeping motor B at normal speed• If the reading is between 40 and 60, then we looking pretty good, so go straight ahead by

setting both motors to the normal speed• If the reading is between 60 and 80, then make a gradual left turn by reducing power to

motor B a small amount while keeping motor A at normal speed• If the reading is between 80 and 100, then turn hard left by reducing power to motor B a

large amount while keeping motor A at normal speed

• Now the robot has 5 flavors of steering, so the path is smoother

Page 20: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Proportional Line Following Concept• Recall the 2 and 5 state followers:

– 2 state: break the range of light sensor readings into 2 categories and use one to turn left and the other to turn right• Robot keeps alternating turns so the path zigzags

– 5 state: break the range of the light sensor readings into 5 categories so the turns can be encoded into 5 options• Turn hard left, turn medium left, go straight, turn medium right, or turn hard right• Robot still zigzags but not quite as much

• Suppose we continued to break up the light sensor readings into more and more blocks that are smaller and smaller– Instead of deciding ahead of time how to set these up, let’s actually

use the reading itself to decide how much to slow the motor on one side and speed up the one on the other…

Page 21: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Linear Line Followingthe smoothest

Page 22: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

The Smooth (linear) Line Follower

Turn on the lightTake a reading

Subtract 50 from the reading: • If we are on the light side of the line thenumber will be positive and bigger the fartherwe are away from the edge • If we are on the dark side of the line thenumber will be negative and more negative the farther we are away from the edge

We want to use this value to steer the robot:• set a normal speed• add this value to one motor to speed it up• subtract this value from the other to slow it down

That’s the basic concept but if we simply do this, the robot may steer too sharply, so let’s divide the difference by a number to reduce the steering sharpness (I used 3 but you can experiment…)

Page 23: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Calturnloop.rbt

Page 24: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Some Other Design Considerations• Try not to make the robot too long, especially if you use

wheels and not treads– The robot will pivot about the point between the wheels so the rest

of the robot will swing through an arc that could be wide enough to knock over game pieces that shouldn’t be touched if the robot is long (see slides 22-25)

• I am a fan of wheels and not treads (however, if your students like treads, stick with them)– Treads can be good for traction. The robot pivots about the center

of the treads when turning BUT treads tend to skid a little more than wheels so they can be less accurate when executing “dead reckoning” navigation• “dead reckoning” is where we tell the robot to move in one direction for a

given distance, then turn through a specified angle and then drive for another specified distance and so on… Lego calls this a MOVE command.

Page 25: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How to Configure Move Commands• Robot will move forward/backward a distance

of *Dw for each full rotation:– For every wheel rotation the distance the robot will

move = the circumference of the wheel– Since there is 360o in a circle, it will move *Dw /360

for each degree of rotation• My robot has wheels that are 2.2047 inches (56 mm)

in diameter so: *2.2047 inches = 6.93 inches (175.93 mm) per rotation or 0.0192 inches (0.489 mm) per degree

• Note: what we really want is the number of degrees to turn the wheel for each inch of robot movement, so that would be the reciprocal or 1/ 0.0192 or about 52 degrees per inch.

WheelDiameter (Dw)

PivotPoint

Diameter of Pivot Circle (Dp)

The wheel that comes as part of the standard NXT set is 56mm (2.2 inch) in diameter which results in a circumference of approximately 176mm (6.9 inch).

Page 26: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How to Program a Move Command…• Open the NXT program on your computer…

Select “Start New Program”-or- use the pull-down menu on the upper left to select File > New -or- use CNTL+N

Page 27: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How to Program a Move Command…• Open the NXT program on your computer…

Select “Start New Program”-or- use the pull-down menu on the upper left to select File > New -or- use CNTL+N

• Each icon block represents a command task• When a task is complete, the program continues to the next block• To write a program, just drag the icons you want to use and place them in order. The program automatically inserts links between them.

Page 28: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How to Configure Move Commands(another look)

• Robot will move forward/backward a distance of *Dw for each full rotation:

• So, my robot wheel commands must be:– In English units: 360 degrees/6.93 inch or about 52

degrees per inch. That is my scale factor to move forwards/backwards

– In metric units we would have 360 degrees/176 mm or about 2.05 degrees per mm (20.5 degrees per cm).

• So, if you want the robot to move 20 inches multiply 20 by this scale factor to get 1040 degrees. Now, if we command my robot to rotate its wheels 1040 degrees, it will move 20 inches…:

WheelDiameter (Dw)

PivotPoint

Diameter of Pivot Circle (Dp)

Forward Direction

Drive WheelCastering Wheel

Page 29: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How Pivoting Turns Look…(90° to right)

• Note: robot does not pivot about its midpoint, it pivots about the midpoint between the wheels

PivotPoint

Wheels move on this circle when pivoting

Initial orientation

Forward Direction

Diameter of Pivot Circle (Dp)

Page 30: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How Turns Look…(90° to right)• Note: The more offset the

wheels are from the center of the robot, the more clearance it needs to turn

PivotPoint

Wheels move on this circle when pivoting

Orientation after 90 turn to right

Forward D

irection

Note: castering wheel gets swiveled when the robot pivots.

Page 31: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How Turns Look…(90° to right)Putting both together…

PivotPoint

Wheels move on this circle when pivoting

Robot needs this much clearance when pivoting!Initial orientation

Orientation after 90 turn to right

Forward Direction

Forward D

irection

Page 32: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

How To Compute Turn Commands• Note: the pivot circle on which the wheels

move is specific to your robot design, not its wheels but we must use wheel commands to turn/pivot:– The pivot circle has a circumference of *Dp

• To turn the robot through a full circle we must move the wheels a distance = the circumference of the pivot circle, so we have to equate the circumference of the wheels to the circumference of the pivot circle:

• **Dw = *Dp

• = number of wheels rotations to make robot turn a in a full circle = one rotation

• Note this is also the same number of degrees of wheel rotation required per degree of robot rotation

• = Dp/ Dw ~ 1.87 degrees/degree for my robotPivotPoint

Pivot circle

Page 33: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Real World = Ideal World!!!• In order for the preceding computations to work, the

measurements have to be very precise…– So it’s good to start there and then make corrections by measuring how

the robot performs using these programs:• Calmove.rbt is a NXT program to let you measure the distance the robot moves

for 2 full wheel rotations = 720 degrees (look at the program to see which two motors to use)– If you divide 720 by the distance you measure, you’ll have the scale factor for your robot– It moves at 50%, 75%, and 100% speeds with 2 second intervals between moves to let

you mark where it stops each time.» This lets you see how fast you can make your robot move without skidding.

• Calturn.rbt is a NXT program to let you measure how many degrees of wheel rotation it takes to turn your robot through 90o. It moves the robot in a square pattern so it should finish right where it started. It is set up for my robot but keep changing the number of degrees in the MOVE command on the right until the robot stops more or less exactly where it started (facing the same way too). – Record the number of degrees you need to turn 90o and then divide this number by

90 to get the degrees of wheel rotation you need per degree of robot rotation, so you’ll know how to set up for a unique turn. This is your turn scale factor (like if you want to turn 45°… multiply this turn scale factor by 45 and enter that in the MOVE command you use to turn).

Page 34: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Calmove.rbt

August 26, 2012 FLL Sensor Training 34

Page 35: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Basic Rules

35

• Your equipment (robot, attachments, and other accessories) must be made entirely of LEGO elements in original factory condition.– Exception 1: You may reference a paper list to keep track of robot

programs.– Exception 2: LEGO string and tubing may be cut to length.

• Maximum Number of Components*:– NXT Controller - 1– Motors - 3– Touch Sensors - 2– Light or Color Sensors - 2– Ultrasonic Sensor - 1– Lamp - 1

* Number of electronic components that can be brought to the tableAugust 26, 2012 FLL Sensor Training

Page 36: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Mission Design Tips

• Combine as many missions as possible into each program – Match duration is too short to keep moving back to base– NXT memory is too small to hold 17 programs!

• For each “starting position” you use in the base area, construct a “fixture” from other parts of the Lego kit to precisely align your robot each time you launch it– fixtures are not allowed to protrude beyond the baselines

but are perfectly legal inside… They will greatly improve robot performance from run to run.

36August 26, 2012 FLL Sensor Training

Page 37: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Example Ideas

• http://www.youtube.com/watch?v=qbhZZxRU2X4&feature=related

• http://www.youtube.com/watch?v=1KRM_svKOk4&NR=1

A line follower was very important in 2010 than before…– Basic: uses two states to move in a zigzag pattern– Semi-smooth: uses 5 states to smooth out the path a little)– Linear: converts light sensor into a continuous variable, so it has the

smoothest path (but is the most complex)

37August 26, 2012 FLL Sensor Training

Page 38: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

So let’s build a simple robot…… with the following features.

• Two wheel differential drive• Castering rear wheel so robot can turn• Some sensors for autonomous motion

– A light sensor to detect the boundary between light and dark colors– A sonar to detect distance from an obstacle

• See Damien Kee’s excellent resource site for detailed instructions and all sorts of resources:– http://www.damienkee.com/home/2011/8/20/domabot-classroom-robot-

design.html– The next few slides show details on the caster wheel construction only

• General Wiring Basics of All Lego Robots:– Sensors plug into ports 1 through 4– Motors plug into ports A through C

August 26, 2012 FLL Sensor Training 38

Page 39: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Details of the Castering Wheel… part 1

• Assemble these parts as illustrated.

Page 40: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Details of the Castering Wheel… part 2

• Next, add these parts…

Page 41: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Details of the Castering Wheel… part 3

• Then, add the wheels…

Page 42: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

Details of the Castering Wheel… part 4

• Next, construct the part that attaches it…

Page 43: Lego NXT Programming for FLL Competition A primer on sensors Jim Keller (jfkeller@seas.upenn.edu) University of Pennsylvania GRASP Lab 26 August, 2012

43

Some Online Support• http://www.Nxtprograms.com

– click on free project instructions for NXT or NXT2 depending on your hardware…

– Has all three line follower types…• http://www.domabotics.com/domabot2

– An overview of how to build a robot with similar content to the slides I presented here but may be easier for you to follow…

• http://www.ortop.org/Workshops/Workshops2011/WorkshopIII2011/ConstructopediaDesigns-for-FLL2010%20(3).pdf– An overview of many construction ideas for NXT designs

• http://www.ortop.org/NXT_Tutorial/– This site has great video-based tutorials on programming from the very simple

to the quite complex. It’s all in NXT-G which is what we must use for FLL…

August 26, 2012 FLL Sensor Training