94
1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

1 ©2006, 2007, 2013 INSciTE

FLL Programming 101EV3 on NXT

September 2013

Version 1.2a

Page 2: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

2 ©2006, 2007, 2013 INSciTE

Legal Stuff

© 2006 INSciTE in agreement with, and permission from FIRST and the LEGO Group. This document is developed by INSciTE and is not an official FLL document from FIRST and the LEGO Group. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike License. To view a copy of this license, visit

http://creativecommons.org/licenses/by-nc-sa/2.0/

or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

LEGO, ROBOLAB, and MINDSTORMS are trademarks of the LEGO Group used here with special permission. FIRST LEGO League is a trademark owned by FIRST (For Inspiration and Recognition of Science and Technology) and the LEGO Group used here with special permission. INSciTE is a trademark of Innovations in Science and Technology Education.

INSciTE

PO Box 41221

Plymouth, MN 55441

www.hightechkids.org

Page 3: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

3 ©2006, 2007, 2013 INSciTE

Creative Commons License

High Tech Kids is committed to making the best possible training material. Since HTK has such a dynamic and talented global community, the best training material and processes, will naturally come from a team effort.

Professionally, the open source software movement has shown that far flung software developers can cooperate to create robust and widely used software. The open source process is a model High Tech Kids wants to emulate for much of the material we develop. The open source software license is a key enabler in this process. That is why we have chosen to make this work available via a Creative Commons license. Your usage rights are summarized below, but please check the complete license at: http://creativecommons.org/licenses/by-nc-sa/2.0/.

Page 4: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

4 ©2006, 2007, 2013 INSciTE

Credits

This presentation was developed by Doug Frevert. It is based

on the work of Fred Rose. The accompanying labs were

originally done in RCX Code by Joel Stone and converted to

ROBOLAB by Doug Frevert. A portion of the material is taken

from “Building LEGO Robots for FIRST LEGO League” by

Dean Hystad. Amy Harris defined the 10 programming steps.

Eric Engstrom, Jen Reichow, and Ted Cochran reviewed

ongoing drafts. Eric taught the first class and helped modify

the content accordingly.

Page 5: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

5 ©2006, 2007, 2013 INSciTE

Computer Programming 101

ObjectiveLearn to program robots.

FLL, NXT, EV3

Structure TheoryExamples specific to EV3Hands-on

This class isAn approach to programming

This class is notExhaustive reference on EV3

Page 6: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

6 ©2006, 2007, 2013 INSciTE

Class Agenda

Computer Basics

The Programming Environment

Moving and TurningLab #1

MyBlocks and LoopsLab #2

SensorsLab #3

Advanced Topics

Problem Solving

Page 7: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

7 ©2006, 2007, 2013 INSciTE

Computer Basics

Page 8: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

8 ©2006, 2007, 2013 INSciTE

The Computer (Generic)

The processor executes commands.Memory stores program and data.Inputs tell the computer about the world.Outputs tell the world about the computer.

Memory

Processor

Inputs Outputs

Page 9: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

9 ©2006, 2007, 2013 INSciTE

Processor: 32 bit ARM Atmel AT91SAM256 running at 50 Mhz

Memory: 64K Static RAM, 256K Flash

3 Motor Outputs(A, B, C)

4 Sensor Inputs (1, 2, 3, 4)

Go Back button

Right button

On/Select button

LCD display

USB communications port

NXT

Left button

Page 10: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

10 ©2006, 2007, 2013 INSciTE

Firmware Loaded?

Firmware must be loaded onto an NXT so that the NXT can understand your programs.

Only required to be loaded To install a new firmware release To restore lost firmware (was a problem for RCX).

Tick .. Tick ...No firmware Firmware loaded.

Page 11: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

11 ©2006, 2007, 2013 INSciTE

Writing a Computer Program

Specify the taskInputs to be supplied

Outputs to be produced

Devise an algorithm

Express that algorithm in a computer language

From: Introduction to Pascal, Welsh and Elder

Page 12: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

12 ©2006, 2007, 2013 INSciTE

Running a computer program (NXT)

Write program on PC

(EV3)

Downloadto the NXT

NXT executes

commands

Program conv. to bytecodes (text

description)

Bytecodes converted to ARM (NXT) machine code

commands

Move X to register 001 1000111...

SetPower(A,3)

Page 13: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

13 ©2006, 2007, 2013 INSciTE

EV3The Programming Environment

Page 14: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

14 ©2006, 2007, 2013 INSciTE

EV3 Opening Workspace

Project Screen

Download and Run

Content Editor

Name the Program

Program Blocks

Page 15: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

15 ©2006, 2007, 2013 INSciTE

EV3 Work Space

Action, Flow Control, Sensor, Data Operations, Advanced, and My Blocks tabs

Add Program

Blocks here

Download and Run

Page 16: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

16 ©2006, 2007, 2013 INSciTE

EV3 to NXT

When connected by USB cable or Bluetooth Give your NXT a new nameCheck Battery voltageView available memory (in KiloBytes)Firmware version

Communications

View the NXT memory browser. See the next slide.

Page 17: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

17 ©2006, 2007, 2013 INSciTE

EV3 to NXT

Memory

Select, then delete Programs, Sounds, Graphics, and Unused files.

Can free up to 118Kb of Free Storage on the NXT.

Page 18: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

18 ©2006, 2007, 2013 INSciTE

EV3 Work Space

Comment

Pointer Tool

Grab Hand to move the

display

Page 19: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

19 ©2006, 2007, 2013 INSciTE

Tips and Tricks (1)

The NXT has memory to store many programsNXT automatically powers down.

Bluetooth CommunicationsNXT brick has bluetooth.

EV3 software to NXT via bluetooth is not supported.

Disable bluetooth during competition.

Page 20: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

20 ©2006, 2007, 2013 INSciTE

Tips and Tricks (2)

Direction of connecting wiresNXT wire connectors only fit one way. Can not

be rotated.

BatteriesAA

No worry about losing firmware.

NiMH rechargeable batteries work. NiCads don't.

Lithium rechargeables come with the FLL Mindstorm kits.

Avoid stalling the motors, it drains batteries.

Page 21: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

21 ©2006, 2007, 2013 INSciTE

Adding a Block to a Program

Click on a Block

Move cursor onto Program and drop it into place. EV3 will make room.

Change settings

Click a Block

Drag it here

Change the settings

Page 22: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

22 ©2006, 2007, 2013 INSciTE

Action Blocks

Action BlocksMove SoundDisplay

With many modifiersMove direction, steering, distance, motors used . . .Settings are visible without digging deeper.

Move Steering

Display

Sound

Page 23: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

23 ©2006, 2007, 2013 INSciTE

Flow Controls

Start Wait(1 sensor)

Loop Switch Loop Interrupt

Page 24: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

24 ©2006, 2007, 2013 INSciTE

Wait Block

Wait blockConvert Time to another sensor Port

Number

Touch, Release, or Touch

and Release

Page 25: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

25 ©2006, 2007, 2013 INSciTE

Motors

9 volt geared motorMaking the motors turn is the

output of your program. It makes your creation a robot!

Without load, motor shaft turns at about 150 rpm.

Servo sensitive to 1 degree.

With a typical robot, 3-4 hours on a set of batteries.

FLL allows up to 4 motors.

Page 26: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

26 ©2006, 2007, 2013 INSciTE

Motor Details

Motor can be set to different power settingsPower levels 0-100Power is adjusted by Pulse Width Modulation

Turning the power setting up higher essentially makes the shaft turn faster.

Page 27: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

27 ©2006, 2007, 2013 INSciTE

Using the Steering Block

Ports A, B, and/or C

On, Off,

Degrees, or

Rotation

SteeringSpin, Pivot, Arc, Straight

Power 0-100%Forward/Backward

Duration

Brake or Coast

Page 28: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

28 ©2006, 2007, 2013 INSciTE

Lab One

Task:Given rectangle ABCD.

Move from A to B to C to D.Extra Credit: A to C to D to A.

AB

CD

Page 29: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

29 ©2006, 2007, 2013 INSciTE

Lab 1 An Answer

Page 30: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

30 ©2006, 2007, 2013 INSciTE

Problem Solving

Page 31: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

31 ©2006, 2007, 2013 INSciTE

Generic Problem Solving Process

Define the problemBrainstorm solutionsEvaluate solutions Pick oneTry (implement) itEvaluate results

Express the solution as an algorithm, then convert it into a computer program.

Page 32: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

32 ©2006, 2007, 2013 INSciTE

Pseudocode

Pseudocode: Not software code, not English, but somewhere in between.

Count EggsAdd IngredientsMix and StrainDip, Fry, SprinkleServe

set counter to zerofor each egg

increment countercrack open into bowl

next

do counter timesadd ½ cup milkadd ¼ cup flour

end do

do counter/2 timesdo 4 times

dip 2 slices of breadfry, sprinkle, serve

end doend do

Page 33: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

33 ©2006, 2007, 2013 INSciTE

Flowcharts

Flowcharts: A graphic representation of logic.Convert from pseudocode.One step closer to software.

set counter to zerofor each egg

increment countercrack into bowl

next

Set Counter to 0

Increment Counter

More Eggs?

Crack Egg into Bowl

Yes

No

Page 34: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

34 ©2006, 2007, 2013 INSciTE

Debugging and Analysis

Split into pieces. Test each piece.Do little pieces at a time

For example, get the robot to where it needs to be first, then work on getting it to do something

Reuse pieces that work For example, you know how to turn 90°

Brainstorm a new solutionLook outside your box.

Ask for help.

Page 35: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

35 ©2006, 2007, 2013 INSciTE

Keep It Simple Strategies

KISS #1: Subroutines #2: Comments

#3: Loops

Page 36: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

36 ©2006, 2007, 2013 INSciTE

KISS #1: Subroutines

Wrap a complicated process into a neat and tidy package.

Once wrapped, just worry about the package.

In EV3, Subroutines are MyBlocksSelect from the MyBlocks tab.

It starts out

empty.

Page 37: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

37 ©2006, 2007, 2013 INSciTE

Subroutines: When to Use

To do the same thing from different places.Reuse

To divide a task into pieces.Modules

To hide complex details.

Page 38: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

38 ©2006, 2007, 2013 INSciTE

MyBlock Names

Useful and informativeClearSoccerField not Csf_amy_3a

12 characters visible on a MyBlock15 characters visible on the NXT

Suggest using “action + to + target”:Fwd2Wall or ForwardToWall or Forward_To_Wall

FwdDist

TurnRight

Name the task accomplished, not how it was done.FollowLine not FollowLine1LightSensor

Page 39: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

39 ©2006, 2007, 2013 INSciTE

MyBlock Creation 1

Click and drag a selection box around a set of

blocks.

Carefully select a section of working code.

Page 40: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

40 ©2006, 2007, 2013 INSciTE

MyBlock Creation 2

Click the My Block Builder

My Block Builder

Page 41: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

41 ©2006, 2007, 2013 INSciTE

MyBlock Creation 3

Name it.

Describe it.

Pick an icon.

Select a Parameter and “Parameter Setup”

Page 42: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

42 ©2006, 2007, 2013 INSciTE

MyBlock Creation 4

For each parameter

Name

Input/Output

Data Type

Default Value

Finish

Page 43: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

43 ©2006, 2007, 2013 INSciTE

MyBlock Creation 5

Rewiring may be required

Input parameter

needs wiring

Page 44: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

44 ©2006, 2007, 2013 INSciTE

MyBlock Creation 6

Only the spin duration can be set.

To add the new MyBlock to a program, select it from the MyBlock Tab.

Simple - can only set the

duration.

Page 45: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

45 ©2006, 2007, 2013 INSciTE

KISS #2: Comments

Explain the program.Programmers forget. Teams compete in FLL. More than one

person will be working on the program.

Click in the program, then click here and a comment box will appear. Click the

comment box and start typing.Drag borders to re-size the box.

Page 46: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

46 ©2006, 2007, 2013 INSciTE

Comment Use

Add who, when, how to use, assumptions taken, and expected results.

Page 47: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

47 ©2006, 2007, 2013 INSciTE

KISS #3: Loops

Loops are a control structure

In other programming languages: For … Next Do loop n timesDo ... Until Do it. Unless some test, do it again.

There are loops for

Forever

Every sensor (including time)

Logic

Count

Loop Block

Page 48: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

48 ©2006, 2007, 2013 INSciTE

Simple Loop

Convert thisto something simpler using a loop

Page 49: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

49 ©2006, 2007, 2013 INSciTE

Lab Two

Task:Make Forward and Right Turn MyBlocks.

Move around the rectangle twice, ending at A.

AB

CD

Page 50: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

50 ©2006, 2007, 2013 INSciTE

Lab 2 Answer

One possible answer.

Page 51: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

51 ©2006, 2007, 2013 INSciTE

Data Input

Sensors

Page 52: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

52 ©2006, 2007, 2013 INSciTE

Sensors

Allow your robot to detect the real world. Touch

Has your robot made contact with something?

LightIs the surface light or dark?

Sound (Microphone)

Ultrasound (Distance)

RotationEmbedded in the motors

TimeInternal sensor, keeps track of time

Battery Voltage

Page 53: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

53 ©2006, 2007, 2013 INSciTE

Sensor #1: Touch

To detect touching or bumping into something.

Good for detecting robot arm movements. The sensor activates when the arm moves far enough to push in the touch sensor.

Page 54: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

54 ©2006, 2007, 2013 INSciTE

Pressed, Released, Bumped

Bumped: pressed and released in any order.

Page 55: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

55 ©2006, 2007, 2013 INSciTE

Touch Sensor WaitFor Block

Pressed, released, or

bumped

Robot waits until sensor responds

Port

Page 56: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

56 ©2006, 2007, 2013 INSciTE

Touch Sensor Switch Block

Waiting for a touch sensor can be useful, but many times you want to do different things based on the current value.

Sensor ports 1, 2, 3, or 4

True

False

Page 57: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

57 ©2006, 2007, 2013 INSciTE

Loop contents always run at least once.

Touch Sensor Loop Commands

Loop until a touch sensor is pressed. Useful if the loop contains commands that may be repeated.

For instance, a routine that beeps until a bumper hits something.

Page 58: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

58 ©2006, 2007, 2013 INSciTE

Sensor #2: Light

Operates in "percent" mode 0 to 100

Higher number = more light. A lighter surface reflects more light.

Calibrate the sensor.

Light can be turned off.

Shines a red light.

Page 59: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

59 ©2006, 2007, 2013 INSciTE

Light Sensor Spectrum

Most sensitive to red/IR light.

Wavelength

Page 60: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

60 ©2006, 2007, 2013 INSciTE

Light Sensor Readings

Lowest likely reading 5%

Highest likely reading 100% (pointing at a light)

Readings also depend on the color of the surface

See “Building LEGO Robots for FIRST LEGO League” by Dean Hystad.

Sensitive to the distance between the sensor and the reflecting surface. Variations can make the readings unusable. Keep the sensor close to the surface, but not too close.

Shield the sensor from other light sources.

Page 61: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

61 ©2006, 2007, 2013 INSciTE

Light Sensor Readings

The light sensor averages its readings over roughly a circular area.

Cross a line too fast and you may miss the line.

Test and recalibrate on competition day.

Page 62: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

62 ©2006, 2007, 2013 INSciTE

Light Sensor WaitFor Block

Use WaitFor Blocks when watching only one sensor.

Reflected Light (not Ambient)

Page 63: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

63 ©2006, 2007, 2013 INSciTE

Light Sensor Switch Block

If darker than 50, turn motor B on. Stop C.Otherwise, turn motor C on. Stop B. What is this?

Outside Loop

Switch a.k.a. If/then/else

Page 64: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

64 ©2006, 2007, 2013 INSciTE

Calibrate Light Sensor

Move your robot over light and dark areas.Resets light sensor percentages.

Minimum (Black)

NXT can not use the EV3

Calibrate block!

Page 65: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

65 ©2006, 2007, 2013 INSciTE

Calibrate Light Sensor +

An example of writing calibration values to a file on the NXT.

Delete the existing file.

Write the max value.

Page 66: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

66 ©2006, 2007, 2013 INSciTE

Sensor #3: Rotation

Measures how far a rotating axle has turned. As the axle turns, a counter in the NXT is incremented or decremented.

360 counts per rotation.Each motor has an embedded rotation sensor.

Page 67: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

67 ©2006, 2007, 2013 INSciTE

Rotation Sensor Loop Block

Possible to make a rotation loop.Select any loop. Pick “Sensor” type and then

“Rotations.”

Page 68: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

68 ©2006, 2007, 2013 INSciTE

The rotation sensor also brings in the possibility of doing some real math!

We’ll leave that as an exercise for the reader!

Of course, trial and error also works.

Sources of error in calculation - dirt on surface, using a skid rather than a wheel, backlash (poor fitting gears).

Calculating Distance

Page 69: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

69 ©2006, 2007, 2013 INSciTE

More on Rotation Sensor

Rotation sensor counts forward and backwards.

Live updates (Bluetooth or USB connected).

Run

Select the Rotation Sensor

Live updates

Page 70: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

70 ©2006, 2007, 2013 INSciTE

Debugging and Analysis

Common problemsProgramming: reset the sensor to zero before

use.

Design: inadequate sensor resolution (trying to measure something very accurately, when the sensor is not that accurate).

Control: starting, stopping, turning too fast.

Variations in the initial conditions: not putting everything in the same place before pushing the run button.

Page 71: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

71 ©2006, 2007, 2013 INSciTE

Sensor #4: Timer

8 TimersTime in secondsGreater or Less than

testsResetTrigger Point

Page 72: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

72 ©2006, 2007, 2013 INSciTE

Other Sensors

Sound (microphone)Is the FLL competition too loud?

Ultrasonic (distance)Interference with other NXTs?

NXT ButtonsOne touch running of the next

program.

Received Messages

Page 73: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

73 ©2006, 2007, 2013 INSciTE

Keep it Simple Strategies

KISS #4: Variables #5: Parallel Sequence

Page 74: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

74 ©2006, 2007, 2013 INSciTE

What’s a Variable?

A value that you can change during your program.This value is “variable”, hence the name.

For example, your program may store a light sensor reading in a variable called LightBright. Use that value later.

Use a meaningful name.Useful to pass values to MyBlocks

Page 75: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

75 ©2006, 2007, 2013 INSciTE

KISS #4: Variables

Add or Delete variables for the whole project on the “Variables” tab.

Variable data typeNumberTextLogic

Page 76: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

76 ©2006, 2007, 2013 INSciTE

Using a Variable

Pick Write, Numeric

Pick the variableBlock

Set the value

Page 77: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

77 ©2006, 2007, 2013 INSciTE

Using a Variable

Read Numeric

Pick the variable Wire the variable value

to a logic test

Page 78: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

78 ©2006, 2007, 2013 INSciTE

KISS #5: Parallel Sequences

Make two tasks that run independently.(Can you walk and chew gum?)

One task lifts the arm.The other task heads for home.

Page 79: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

79 ©2006, 2007, 2013 INSciTE

Lab Three

Task:Use a light sensor to detect the lines.

Move from A to B to C to D.Extra Credit: While moving, touch the touch sensor to

stop.

AB

CD

Page 80: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

80 ©2006, 2007, 2013 INSciTE

Lab 3 An Answer

Page 81: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

81 ©2006, 2007, 2013 INSciTE

Advanced Topics

Debugging ToolsSwitch between Cases

File OperationsBluetooth Communication Setup

Additional Resources

Page 82: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

82 ©2006, 2007, 2013 INSciTE

Debugging Tools

MusicUse music to identify sections of

code.One or two quick notes, a good

ear can hear the difference.

LCDWrite text, even graphics, to the

NXT LCD panel.

NXT Live updatesFrom your PC (or MAC), use

EV3 to view the values of variables and sensors.

Page 83: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

83 ©2006, 2007, 2013 INSciTE

Switch Between Cases

Must not use flat

view

One switch block with 3 tabs, one for each case. Click here to

add cases.

Text or number

type.

Page 84: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

84 ©2006, 2007, 2013 INSciTE

Rotation Sensor Reset Block

Motor and Move Blocks are servo controls.

Corrects momentum over runs

For several blocks in a series, the correction is based on a total rotation target.

Even applies across programs

Use the Rotation Sensor Block to reset each motor.

Page 85: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

85 ©2006, 2007, 2013 INSciTE

Arithmetic

Operation

Wire Input

WireOutput

Page 86: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

86 ©2006, 2007, 2013 INSciTE

Combining Structures

Outside Loop runs until the B motor runs 10.5 rotations.

Light Sensor Switch

runs inside.

Page 87: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

87 ©2006, 2007, 2013 INSciTE

Compare that line follower to this one:

BetterSimpler, proportional steering

WorseMore sensitive to proper calibration

Comparing Algorithms

Page 88: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

88 ©2006, 2007, 2013 INSciTE

Watching 3 Sensors

Logical ORs combine results into a single

True or False

Outside Loop (Logical)

Wait for any 1 of 3 sensors

Page 89: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

89 ©2006, 2007, 2013 INSciTE

Optimizing Code

Which is faster?more reliable?best?

Use the one that makes sense to you, the programmer.

Page 90: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

90 ©2006, 2007, 2013 INSciTE

File Operations

Read

Write

Close

Delete

Upload/Download

Delete Write Write Close

Page 91: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

91 ©2006, 2007, 2013 INSciTE

Bluetooth Communication Setup

On the NXT, select Bluetooth -> On/off -> ONBluetooth -> Visibility -> Visible

In EV3, select a NXT and click “Connect”

Enter a passkey, like “mysecret”and click “OK.”

On the NXT a screen will appear. Re-enter the same passkey. (Be quick, you've got 30 sec.)

After EV3 shows “connected”, on the NXT,Bluetooth -> Visibility -> Invisible

Sorry, but EV3

software to the NXT

brick does not

support Bluetooth.

Page 92: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

92 ©2006, 2007, 2013 INSciTE

Lab Four

Task:Move exactly one lap around an oval.

(Black 2cm line on white paper)

Page 93: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

93 ©2006, 2007, 2013 INSciTE

Lab 4 An Answer

Did you start by copying? Why not?

Is building a robot like taking a test?

Page 94: FLL Programming 101 NXT-G - dfrevert.github.io · 1 ©2006, 2007, 2013 INSciTE FLL Programming 101 EV3 on NXT September 2013 Version 1.2a

94 ©2006, 2007, 2013 INSciTE