LEGO MINDSTORMS PROGRAMMING CAMP

Post on 31-Dec-2016

232 views 11 download

Transcript of LEGO MINDSTORMS PROGRAMMING CAMP

LEGOMINDSTORMSPROGRAMMINGCAMP

RoboticsProgramming101CampCurriculum

ì Everydayofcamp,westartedwithashortvideoshowingFLLrobots,realrobotsorsomethingrelevanttotheday’slesson

ì Weswitchedbetweenusingpowerpoint slidesandshowingtheactualEV3softwareenvironment

ì Wecreatedsomeadd-onchallengesforstudentswhofinishedearly

ì Wecreatedanewprojecteachday

ì Wedidnotshowthechallengesolutionslideunlesssomeoneneededhelp.

InstructorNotes2

Whatisarobot?

Robotsaremachinesinfactories,playsoccer,areinresearchlabs,behavelikeanimals….

WewillusetheLEGOMINDSTORMS

3

LegoMindstorms Robots

YoucanplaceavideoofyourchoiceheretointroduceFLL/Legorobotics

4

ì Sharethelaptopandtherobot

ì Worktogetherinyourgroup

ì Helpeachotherfinisheachchallenge

ì Lifttherobotbythehandle

ì TakeTurns

ì Donotdrop,throw,pushontherobot– theyareveryexpensive

ì Treatyourcomputerwell– theyareexpensive

DosandDoNots thisweek5

ì LearnbasicEV3-GProgramming

ì MakeaMindstorms robotfollowyourinstructions

ì Learnhowtomakeyourrobotmovestraight,turn,repeatactions,choosebetweenactionsandfollowlines

ì HaveFUNwithrobotsandgetinspiredtolearnmore!!

Whatwillwedothisweek?6

ì WeusetheCyberBot NXTdesign– pleasenotethelocationofthecolorsensor

ì WeusetheNXTbrickbutprogramwiththeEV3software.TheNXTbrickhaslessmemory.ì Youwillhavetodeletethefilesoften(attheendofeverysession)ì Studentswhocreateandusealotofsoundfileswillfillinthe

brickfaster.

ì Weaskedstudentstomakeanewprojectfileeachdayfortheirprograms.

InstructorNotes7

BEGINNEREV3PROGRAMMINGLesson:Introduction

TopicsCovered:NXTBasicsIntroductiontotheNXTBrickandEV3Software

LessonObjectives

1. LearnhowtheNXTbrickoperates

2. LearnaboutthemaincomponentsoftheEV3software

9

The“Brick”Buttons

ì 1=Left,Rightà Navigatemenus

ì 2=CenterButtonàSelectoptionsRunProgramTurnroboton

ì 3=BackàUndoStopProgramTurnrobotoff

1 1

3

2

10

The“Brick”Screen

Menus on Screen

1. My FilesFind programs you downloaded

2. Use left/right buttons to find other menus such as “View”

11

Ports,Sensors,motors

Ports1,2,3,4=Sensors

PortsA,B,C=Motors

DefaultsetupassumesRightMotorisinC,LeftMotor isinB

B

C

Defaultsetup Front

Motors:makeyourrobotmove

TouchSensor:Makeyourrobotdetectobjects

Colorsensor:Makeyourrobotdetectcolors

Ultrasonicsensor:Makeyourrobotmeasuredistances

12

EV3Software

CreateNewProject

OpenSavedProject

13

EV3Software:Savingyourproject

File..Save ProjectAs

Saveprojectondesktopas“CampPM1”

Eachprojectcancontainmultipleprograms.

Youwillcreateanewprojectforeachdaythisweek.

14

EV3Software:StartingAnewprogram

OpenedProjects

OpenedPrograms

CreateaProject

CreateaProgram

ProjectProperties

Listofprograms

15

EV3Software:Namingyourprogram

DoubleclickonProgram

Changenameofprogramto“MoveStraight”

16

PROJECTSVS.PROGRAMS

ì YouwillstartbycreatingaPROJECTwhichhasa.ev3extension.YouchangethenameofaprojectbyusingtheSaveProjectAsoptionfromthethe Filemenu.

ì YouwillwritemanyPROGRAMS aspartofeachPROJECTfile.Youchangethenameofaprogramwithinaprojectbydoublingclickingontheprogram’stabandtypinginanewname.

ì Note:Ifthereisan*nexttotheprojectname,youhavemadechangestotheprojectandyouhavenotyetsavedyet.

ì Thereisa“x”nexttoprojectandprogramnames.Clickingonitsimplyclosesthefile(itdoesnotgetdeleted).

17

Clickonaprogramtabtobringuptheprogrammingcanvas.

EV3Software:Programmingscreen

� Connection status � Download programs ready to be run � Download/play programs instantly � Download a section of a program to

run � Intelligent EV3 Brick status: name and

battery level, etc. � Port status and sensor readings � Type of connection between the EV3

Brick and the computer (BT, Wi-Fi, or USB)

The Communication Pane

ProgrammingBlocksin6ColoredTabs

ProgrammingAreaorCanvas

BrickStatus&Downloading

18

EV3Blocks:ColoredTabs

ACTIONBLOCKSMove,Large&Medium

Motor, Display…

FLOWBLOCKSStart,Wait,Loop,Switch,

LoopInterrupt

SENSORBLOCKSBrickButtons,Gyro,Color,

Ultrasonic

DATAOPERATIONSVariables,Array,Logic,Math,Compare…

ADVANCEDBLOCKSDataLogging, Unregulated

Motor…

MYBLOCKSCustomBlocksyoucreate

1 2 3

4 5 6

1 2 3 4 5 6

19

AddingBlocksToYourProgramSteps to create a program

1. Click and hold block with left mouse button to drag it

2. Drop the programming block when grey box appears

3. Select / enter options

4. Click download to compile and load the program in the EV3 controller

25

STEP1:GreenBlockTab,Clickandholdanyblockanddragtoprogramming area

STEP2:DropnexttotheStartBlock(greenarrow)(Seeanimation)

20

EV3PROGRAMMINGLesson:MovingStraight

BeginnerLesson:MovingStraight

LessonObjectives

1. Learnhowtomakeyourrobotgoforwardandbackwards

2. LearnhowtousetheMoveSteeringblock

3. LearnhowtoreadsensorvaluesusingView

12

FINISH

START

B C

22

HowdoyouMoveStraight?Steps to create a program

1. Click and hold block with left mouse button to drag it

2. Drop the programming block when grey box appears

3. Select / enter options

4. Click download to compile and load the program in the EV3 controller

25

STEP1:GreenBlockTab,ClickandholdMoveSteeringanddragtoprogramming area

STEP2:DropnexttotheStartBlock(greenarrow)(Seeanimation)

23

CHALLENGE1:MoveStraight

STEP1:GreenBlockTab,ClickandholdMoveSteeringanddragtoprogramming area

STEP2:DropnexttotheStartBlock(greenarrow)

STEP3:ConnectUSBcabletoNXTandLaptop.

STEP4:Download toNXT

Step3

Step4

24

MoveSTEERINGBlock

Steering:Straightorturn Power/Speed Duration/Distance

Modeofoperation

Brake/Coast

B

C

25

NEGATIVE&POSITIVEPOWER:BACKWARD&FORWARD

NegativePower=Backwards

PositivePower=Forward

26

CHALLENGE2:MoveStraight(3SECONDS)

STEP1:GreenBlockTab,ClickandholdMoveSteeringanddragtoprogramming area

STEP2:DropnexttotheStartBlock(greenarrow)

STEP3:SelectOptions.Move“3Seconds”

STEP4:ConnectUSBcabletoNXTand Laptop.

STEP5:Download toNXT

Step3

Step4

Step5

27

MoveStraight:Secondsvs.degreesvs.rotations

ì CHALLENGE:Moveyourrobotforwardfromthestartlinetothefinishline(1)andbacktothestart(2).

ì TrymodeSECONDS,DEGREESorROTATIONSandadjustduration/distance

ì Trydifferentspeeds

12

FINISH

START

B C

28

MOVESTRAIGHTdiscussion

Didyouguessandcheckalot?Yes. Programmingwithseconds,rotationsanddegreesusingguessandchecktakesalotoftimeandeffort.

Didchangingthespeedmatter?Yes.Whenyoumoveinsecondsyourspeedwillmatter.

Doyouthinkthewheelsizewillmatter?Why?Wheelsizeaffectsdegrees/rotations.

Doyouthisthebatterylevelwillmatter?Why?Whenyoumoveinseconds,batterylevelschangethepower.

29

CHALLENGESOLUTION

• Thereisabetterway(gotonextslide)tosolvethischallenge

Thiswould bechangedfordegreesorseconds

30

ì Try“view”onbrick(instructionbelow)ì Moveyourrobotwithyourhandfromyourstartlinetoyourendlineì Readhowmanydegreesyourrobotmovedì UsethisnumberintheMoveSteeringBlocktomovethecorrectdistance.

SOLUTION:UseView

Browsefor‘Motordegrees’option

31

BEGINNEREV3PROGRAMMINGLesson:Turning

Beginner Lesson: Turning

FirstBase

SecondBase

LessonObjectives

1. Makearobotbaseballplayer

2. Learnhowtomaketherobotturn

StartandEndposition

B C

BC

B C

33

PIVOTVs.SPINTurns

180DegreePivotTurn

180DegreeSpinTurn

Noticewheretherobotendsinbothpicturesaftera180degreeturn.

IntheSpinTurn, therobotmovesalotlessandthatmakesSpinTurnsaregreatfortightpositions.Spinturnstendtobeabitfasterbutalsoalittlelessaccurate.

Sowhenyouneedtomaketurns,youshoulddecidewhichturnisbestforyou!

B

CStartPosition EndPosition

MotorsBandCMove

B

CMotorBMoves

StartPosition

EndPositionB

C

B

C

34

HowtoMakePivotandSpinturns

Move Steeringor LargeMotor

LargeMotorB LargeMotorC MoveSteeringSteering=100

MoveSteeringSteering=-100

PivotTurnRight PivotTurnLeft SpinTurnRight Spin TurnLeft

ChangeSteeringvaluehere

B

C

B

C

B

C

B

C

MoveSteeringBlock

LargeMotorBlock

35

ì Turnsaremeasuredindegrees

MeasuringTurns

90270

180

0/360

135

45

225

31530

15

60

105

75

120

150165

210195

240

300

330345

255

285

36

MakingaPivotturnfor90Degrees

Programyourrobottoturn90degrees....Doestherobotactuallyturn90degreesifyoujustpick90degreesfordistance?

B

C

BC

?

Ans.NO!Thismakesthewheelturn90degrees.Thecorrectsolution onnextpage

37

Howdoyoumaketherobotturn90degrees?

ì Ans.Tryusingtheportviewtomeasuretheturnandtheninputthecorrectnumberofdegrees.

Motordegrees

38

Challenge1ì Yourrobotisabaseballplayerwho

hastoruntoallthebasesandgobacktohomeplate.

ì Canyouprogramyourrobottomoveforwardandthenturnleft?

ì Useasquareboxortape

Challenge2ì Yourrobotbaseballplayermustrun

tosecondbase,turnaroundandcomebacktofirst.

ì Gostraight.Turn180degreesandreturntothesamespot.

TURNINGCHALLENGES

StartandEndposition

FirstBase

B C

SecondBase

39

ClassDiscussionGuide

ì DidyoutryPIVOTandSPINturns?Whatdidyoudiscover?PivotturnswerefineforChallenge1,butforChallenge2,ifweusedPivotturns,wewerefartherawayfromthebase.

ì Whatsituationswouldoneworkbetterthantheother?Spinturnsarebetterfortightturns(placeswherethereisnotenoughspace)andyoustayclosertoyouroriginalposition.

40

Challenge1ì Youprobablyuseda

combinationofmovesteeringtogostraightanddopivotturnstogoaroundthebox.

Challenge2ì Youprobablyusedaspin

turnbecauseitisbetterfortighterturnsandgetsyouclosertothestartingpoint!

CHALLENGESOLUTIONS

StartandEndposition

FirstBase

B C

SecondBase

41

ì Whatdotheseprogramsdo?

ì Whatarethenamesoftheseblocks

Review42

BEGINNEREV3PROGRAMMINGLesson:Displaytothescreen

By:DroidsRobotics

Beginner Lesson: Display Blocks

LESSONOBJECTIVES

1. Maketherobotdisplaysomefunfaceswhilemoving!

2. LearntousetheDisplayBlocktodisplaytextandimages

3. UnderstandwhytheDisplayBlockcanbeusefulinprogramming

44

DisplayBlock

• TheDisplayBlocktoshowinformationandpicturesonthescreen

• Youcancontrolthelocationandsizeoftext

• Youcanusethissameblocktodisplaysensorreadingsandinstructions.

• LocatedinGreenTab

Display!!!!!

45

MoreonDisplayBlocks

ì Twomodestodisplay

ì Pixelmode(Usefordisplayingimagesortext)ì Givesfinecontroloverplacementofimage/text

ì Gridmode(Easiertouse,onlyworksfortextmode)ì Accuracyofasingle“line”

ì Clickontopleftofblocktopreview

46

DisplayingTextinGridMode

Step1:PickDisplayBlock

Step2:Clickon“SwitchModes”iconandhoverover“text”.Thenclickon“grid”.Theiconwillchangeintoasquarewithdots.

Step3:Usetheboxonthetoprighttoenterthetextyouwanttodisplay

TextGoesHere

47

DISPLAYBLOCK:CHALLENGE1

ì Canyouwriteaprogramtodisplaytextinthemiddleofthescreen?ì Display“HelloWorld”

ì Howdowegetthetexttostayonthescreenlonger?ì Needtomaketheprogram“wait”beforeexiting

48

ì Waitblocksmakeyourprogrampauseforsometime(oruntilsomethinghappens)beforemovingtothenextstep

ì Fornow,wearejustgoingtowaitfortime

WaitBlocks

ChooseTimeformode

Enter“3”forsecondstowait

49

DISPLAYBLOCK:CHALLENGE2

ì Canyouwriteaprogramtodisplaytextinthemiddleofthescreen?ì Display“HelloWorld”

ì Makethedisplayblockrunfor3seconds

50

DISPLAYBLOCK:CHALLENGE3

ì Canyouwriteaprogramtodisplaytextinthemiddleofthescreen?ì Display“HelloWorld”

ì Makethedisplayblockrunfor3seconds

ì Canyoualsomovewhiledoingthis?

51

AtipforMoveSteeringBlocksWithWaitBlocks

ì Leavingthemotor“on”and“off”

ì Whyusethe“on”insteadof“degrees”?ì Maywanttheprogramtodoother

taskswhilemoving

52

Challenge3solution53

DisplayinganimageinPIXELMode

Step1:PickDisplayBlock

Step2:Clickonthe“SelectMode”whichhasafolder iconandpick“image”

Step3:Usetheemptyboxonthetoprighttopicktheimageyouwanttodisplay

Note:theimageyouchoosemaynotshowupcorrectlyonaNXT

54

DISPLAYBLOCKCHALLENGE4

ì Canyoudisplayeyesonthescreenwhilemoving?Alternateeyeballsthatlookleftandright.• UsetheDisplayBlock,

MotorOnandWaitBlock

• Feelfreetohavefunwiththischallengeandmakeityours!

55

CHALLENGE4Solution

DisplayBlockMotorOn Wait

BlockMotorOff

56

DiscussionGuideupdate?

ì Whymightyouwanttoknowhowtousethedisplayblock?ì Youmightwanttoknowwhatthesensorvalueyourrobotis

seeingì Youmighthavetoprogramarobottostopwhentherobot

reachesaredlinebutitstopsbeforeì Doestherobotseethesamethingyousee?ì Youcandisplaythevalueonthescreenandcheck

ì It’sagreatdebuggingtool.YoucanlearnmoreaboutdebuggingcodeinoneofourIntermediatelessons.

57

BEGINNEREV3PROGRAMMING:LessonTouchSensor

LessonObjectives

1. Makearobotthatwanderstheroom– bumpsintowallsandturnsaround

2. LearnhowtousetheTouchSensor

3. LearnthedifferencebetweentheWaitForBlockandtheSensorBlocks

B

C

B

C

BC

59

Whatisasensor?

• AsensorletsanEV3programmeasureandcollectdataaboutissurroundings

• TheEV3sensorsinclude:ì Color– measurescoloranddarknessì Ultrasonic– measuresdistancetonearby

surfacesì Touch– measurescontactwithsurface

60

WHATISATOUCHSENSOR?

• TouchSensorcandetectwhenthesensor’sredbuttonhasbeenpressedorreleased

• Withthisinformation,youcanprogramanactionwhenthesensoris:• CurrentlyPressed• CurrentlyReleased• PressedandReleasedJustBefore(Bumped)

• Whenmightyouusethissensor?ì Usefulforprogramming“movinguntiltouchsensor

ispressed/released/bumpedӓ Youcanalsohaveyourprogramstartorstopwhen

atouchsensorispressed.

61

HowdoyouprogramwiththeTouchSensor?

YellowSensorTab:SensorBlocks

UsedtoReadandCompareSensorValues

OrangeFlowTab:WaitforBlock

Usedtowaitforasensorreading(ortime)

ThereisaTouchSensorBlockintheYellowTab,butthereisaWaitforTouchintheOrangeTab.Whatisthedifference!!????!

62

Reminder:AtipforMoveSteeringBlocksWithSensors

ì Leavingthemotor“on”and“off”

ì Whyusethe“on”insteadof“degrees”?ì Maywanttheprogramtodoother

taskssuchasreadingasensorwhilemoving

63

CHALLENGE1

ì Programyourrobottomovestraightuntilyoutapthesensorwithyourhand.

Touch Sensor � Detects pressed

� Detects released

� Detects bumped

� Improved design for easier build

solutions

� Auto ID

0=released1=pressed2=bumped

Hint:Youwillcombine:MoveSteering+WaitBlock

B

C

64

Challenge1Solution

ì Addscreenshot

65

CHALLENGE2

ì Programyourrobottomoveuntilithitstheedgeofawall.Thenbackupandturnright90degrees.

Touch Sensor � Detects pressed

� Detects released

� Detects bumped

� Improved design for easier build

solutions

� Auto ID

0=released1=pressed2=bumped

Hint:YouwillcombineMoveSteering+Turning+WaitBlock

B

C

66

Challenge2Solution

ì AddScreenshot

67

DISCUSSION

ì WhydidyouuseMOTORONforthesechallenges?ì Youwanttoreadthesensorwhilethemotorison.

ì WhydoweusetheWAITFORBLOCKinthesechallenges?ì Weneedtoprogramtowaitforthecorrectreading

ì WhatisthedifferencebetweenPRESSED,RELEASEDandBUMPED?ì PRESSED=pushedin,RELEASED=notpushed,

BUMPED=pressedandreleasedrecently

ì Whataresomesituationsyoumightwanttouseeachofthesefor?ì PRESSED=runningintoawall,BUMPED=tappedbyhand

RELEASED=nolongertouchingawall

68

BEGINNEREV3PROGRAMMINGLesson:ColorSensor

LessonObjectives

1. Maketherobotmoveuptothegreenlinewithoutdoinganymeasurement!

2. LearnhowtousetheColorSensor

3. LearnaboutCoastandBrake 12

FINISH

START

B C

70

Whatisthecolorsensor?

• Whatarethey?Sensorsthatdetecttheintensityoflightthatentersit

• Threemodes:Color,ReflectedLightIntensityandAmbientLightIntensityì ColorMode:Recognizes7colors(black,brown,blue,green,yellow,red,

white)andNoColorì ReflectedLight:Measurestheintensityofthelightreflectedbackfroma

lampthatemitsaredlight.(0=verydarkand100=verylight)ì AmbientLight:Measuresthestrengthofthelightthatentersthesensor

fromtheenvironment.(0=verydarkand100=verylight)

• USES:ì Moveuntilalineì Followaline

Color Sensors – reflected  light  mode…

• The COLOR SENSOR shines a light on the mat and reads the reflected brightness level (intensity) level, i.e. dark or bright, to help the robot recognize its position and through programming take action.

• Light intensity ranges from 0-100% • On a bright (white) area of the mat the light intensity value will be above 50% • On  darker  area  (blue,  black,  green,…),  the  light  intensity value will be below 50%

• To obtain more accurate reading, make sure the light sensor is close to the mat (less than ½ inch)

• Calibrate the sensor whenever light conditions change (see calibrate slide)

Black Violet

Dark Blue Dark Red / Brown

70%

50%

20%

50%

70%

40

WewilluseCOLORMODEinthisLesson

71

COLORSENSORCHALLENGE

ì Howdoyoumaketherobotmoveuptoagreenlineusingthecolorsensor?

ì Step1:UseWaitForColor

ì Step2:UsethecolorsensorinCOLORMODE

ì Hint:YouwilluseMoveSteering(thinkaboutmotoronandoff)andWaitfor“Color”

72

ColorSensorChallengeSolution

FINISH

START

MoveSteeringSetto“OFF”withBRAKE

MoveSteeringSetto“ON”

WaituntilColor isGreen(#3)

73

BEGINNEREV3PROGRAMMINGLesson:Loops

By:DroidsRobotics

LessonObjectives

1. Canweimproveourbaseballrobotbyjustprogrammingittogotothe“next”baseandrepeatingthisaction?

2. Learnhowtorepeatanaction

3. LearnhowtouseLoopBlocks

75

RepeatinganAction

ì Howcanwemovearoundbasesusingthecommandswealreadyknow?ì (move+turn)+(move+turn)+(move+turn)

ì Loopsmakerepeatingataskmultipletimeseasy

The LOOP Block • Sometimes, there are actions that you want to repeat. The LOOP block allows you

to repeat those actions until an end condition is met (or becomes TRUE).

• Example: make the robot move around a box and return to its starting position • To move along the box sides, it takes 8 blocks as follows:

Warning: Deleting the LOOP block will also delete all the blocks within the loop. You can move the blocks out of the loop, then delete it.

Using the LOOP block, only

Repeat the loop 4 times

45

Isthereaneasierway? Hint:

76

ì Loopsmakerepeatingataskmultipletimeseasyì KEEPGOING….Forever,foraCount,Untiltouch(orsomething

else)

LoopsThe LOOP Block • Sometimes, there are actions that you want to repeat. The LOOP block allows you

to repeat those actions until an end condition is met (or becomes TRUE).

• Example: make the robot move around a box and return to its starting position • To move along the box sides, it takes 8 blocks as follows:

Warning: Deleting the LOOP block will also delete all the blocks within the loop. You can move the blocks out of the loop, then delete it.

Using the LOOP block, only

Repeat the loop 4 times

45

The LOOP Block • Sometimes, there are actions that you want to repeat. The LOOP block allows you

to repeat those actions until an end condition is met (or becomes TRUE).

• Example: make the robot move around a box and return to its starting position • To move along the box sides, it takes 8 blocks as follows:

Warning: Deleting the LOOP block will also delete all the blocks within the loop. You can move the blocks out of the loop, then delete it.

Using the LOOP block, only

Repeat the loop 4 times

45

77

LOOPCHALLENGE

ì Challenge:Writeaprogramtogoaroundbasesuntiltouched.YoumustuseaLOOP

78

LoopCHALLENGESolution79

BEGINNEREV3PROGRAMMINGLesson:Switches

By:DroidsRobotics

LessonObjectives

1. Gettherobottoshowahappyfacewhenyoupressitsbuttonandasadfacewhenyoudon’t

2. Learnhowtomakeyourrobotdecidewhattodooutofdifferentchoices

3. LearnhowtouseaSwitchBlock

81

SwitchBlocks

ì Askingtherobotaquestionanddoingsomethingdifferentbasedontheanswerì Example:Doestherobot

seealine?Ornot?

ì BasicallyaYES/NOQUESTION

ì Switchblocksarefoundintheorange/flowtab Thequestionbeing

asked: isthetouchsensorpressed

Runthiscodeiftheanswerisno

Runthiscodeiftheanswerisyes

82

SwitchBlockCHALLENGE1

ì Challenge:Writeaprogramthatchangesthedisplaybasedonifthetouchsensorispressedornotpressed.

ì Ifpressed,yourEV3ishappy!Displayasmileyface.Ifnotpressed,theEV3issad!Displayasadface.

ì Hint:Youwillneedtousethedisplayblock,loopsandswitchblocks!

83

Challenge1SOLUTION

DisplayBlock

DisplayBlock

WaitBlock

Loop

TouchSensorSelected

84

SwitchBlockChallenge2

ì Canyouwriteaprogramthatdisplaybigeyeballsifyoutouchitonceandasadfaceifyoutouchitasecondtimeandtogglesbackandforth.

85

Challenge2solution86

BEGINNEREV3PROGRAMMINGLesson:UltrasonicSensor

By:DroidsRobotics

LESSONOBJECTIVES

1. Makearobotthatfollowsyouaroundlikeadog

2. LearnabouttheUltrasonicSensor

3. LearnhowtouseWaitUntilUltrasonicBlock

4. LearnthedifferencebetweentheWaitUntilUltrasonicBlockandtheUltrasonicBlock

88

ULTRASONIC• Anultrasonicsensormeasuresdistance.

• Youuseitwhenyouneedtomakesureyouareacertaindistanceawayfromatarget.

• Thedistancecanbemeasuredininchesorcentimeters.

• Toreadtheultrasonicsensor,youusetheUltrasonicBlock.Tousetheultrasonictodoanactionuntiladistance,youuse“WaitUntil”

WaitforUltrasonicReadUltrasonic

VS.

89

UltrasonicChallenge1

ì Challenge:Maketherobotmoveuntilitis5inchesawayfromthewall.

ì Step1:Makeanewprogram

ì Step2:Setmoveto“on”

ì Step3:SetwaitblocktousetheUltrasonic

ì Step4:Setmoveblockto“off”

90

Challenge1solution

91

Challenge2:DogFollower

ì Iftherobotiscloserthan5inchesawayfromyourhandmovebackward,otherwisemoveforward.

ì Step1:Dragaloopfromtheorangetab

ì Step2:Dragswitchinside loop

ì Step3:SetswitchtoUltrasonic

ì Step4:SetmovesteeringblocktoONandplaceinTRUE

ì Step4:SetmovesteeringblocktoOFFandplaceinFALSE

92

Challenge2Solution

93

BEGINNERPROGRAMMINGLesson:BasicLineFollower

LESSONOBJECTIVES

1. Learnhowtomakearobotfollowaline

2. LearnhowtogetarobottofollowalineusingColorModeontheEV3ColorSensor

3. Learnhowtofollowalineuntilasensorisactivated

4. Learnhowtofollowalineforaparticulardistance

5. Learnhowtocombinesensors,loopsandswitches

95

ì WeusedCyberBot (seeEV3Lessons.comRobotDesignpage)ì CyberBot hascolorsensorsbehindthewheelì Therefore,studentswillhavetolinefollowbackwards(negative

power)

ì ProgrammingalinefolloweronanNXTbrickwithanEV3requiressomeadjustmentsì WefoundthatMoveSteeringwithanangledoesnotworkwith

theNXTforpivotturnsì Therefore,thecodeusesLargeMotorblocksinsteadforturning

InstructorNotes96

FOLLOWTHEMIDDLE?

ì Humanswanttofollowthelineinthemiddle.

ì Let’shavetherobotdothesamethingusingtheColorSensor

ì Whattypeofquestionscanweaskusingthissensorì Areyouonlineornot?

97

1. Ifweareonblack,keepgoingstraight

2. Ifweareonwhite,turnlefttogetbacktotheline

Seemstoworkfinehere…

98

1. Ifweareonblack,keepgoingstraight

2. Ifweareonwhite,turnlefttogetbacktotheline

OHNO…myrobotisrunningaway….

Whentherobotleavestheleftsideoftheline,theprogramnolongerworks!

99

ì WhycouldtheHumanfollowthemiddle?:ì Theycanseeahead.ì Theycanseethewholelineanditssurroundingsì Theyseebothsides andwhichsidetheyleft

ì Whycan’ttheRobotdothesamething?:ì Can’ttellrightorleftsideofthelineì Howdowemakesuretherobotalwaysveersoffonthe

SAMESIDEoftheline?ì Insteadofthemiddle,couldtherobotfollowthe“edge”?

ì Sonowtherobotwillfalloffonlythesameside.ì Wewillnowshowyouhowthisworks!

LineFollowing:ROBOTSTYLE100

ROBOTLINEFOLLOWINGHappensontheedges

Leftsidelinefollowing

RightsidelinefollowingTherobothasto

choosewhichwaytoturnwhenthecolorsensorseesadifferentcolor.

Theanswerdependsonwhatsideofthelineyou

arefollowing!

Ifonblack,turnleft.Ifonwhiteturnright.

Ifonblack,turnright.Ifonwhiteturnleft.

101

Startingtherobotonthecorrectside

✔ ✗✔

102

LineFollowerchallenge1

ì Step1:WriteaprogramthatfollowstheRIGHTedgeofaline.

1. Ifyoursensorseesblack,turnright

2. Ifyoursensorseeswhite,turnleft

3. Useloopsandswitches!

4. YouwillneedtouseLargeMotorblockin“ON”Mode.

5. Youwillneedtocontroleachmotor(BandC)separately.

ì Step2:Tryitoutondifferentlines. B C B C

103

LineFollowingchallengeSolution

Q. Does this program follow the Right or Left side of a line?A. The robot is following the Right Side of the line.

104

CHALLENGE1SOLUTION

Q. This line follower goes forever. How do we make this stop?A. Change the end condition on the loop.

105

Linefollowerchallenge2

Part1:Makealinefollowerthatstopswhenyoupressthetouchsensor

Part2:Makealinefollowerthatstopsafterittravelsaparticulardistance

106

Challenge2SOLUTION:Sensor

107

Challenge2Solution:Particulardistance

108

DISCUSSIONGUIDE

ì Whyisitimportantfortherobottofollowthesamesideoftheline?

ì Therobotonlyknowstocheckifitisonorofftheline.

ì Thisisabasiclinefollower.Whataresomethingsthatwerenotgoodaboutthislinefollower?Doyouthinkthelinefollowercanbeimproved?

ì Itwigglesalot.SmootherlinefollowersaredescribedintheAdvancedlessons.

ì Whatsensormeasureshowfaryouhavetravelled?

ì TherotationsensorusedinChallenge2solutionmeasureshowmuchthewheelshaveturned.

ì Howwouldyouwritealinefollowerthatwillstopwhenitseesaline?Oranothercolor?

ì Changetheloopexitconditiontousethecolorsensor.

109

FinalChallenge

UsingEV3LessonsTrainingMats

LineFollowerMat SensorMat

BasicMat

• Linefollower races• Tryyour roboton

different lines

• Blacklinefollower• Redlinefollower• Moveuntilacertain

color• Driveoverallthecolors

andhavetherobotnamethecolor

• Haverobotsfolloweachotheraroundthetrack,butstay5inchesaway

• Linefolloweruntilgreen• Linefollowblackthenwhite• Goaroundthebox• Startingreenboxandend

inredwithout knockingoverbox(placelightboxinblackrectangle)

111

CREDITS

• ThistutorialwascreatedbySanjaySeshanandArvindSeshan

• Morelessonsareavailableatwww.ev3lessons.com

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

112