LEGO MINDSTORMS PROGRAMMING CAMP

112
LEGO MINDSTORMS PROGRAMMING CAMP Robotics Programming 101 Camp Curriculum

Transcript of LEGO MINDSTORMS PROGRAMMING CAMP

Page 1: LEGO MINDSTORMS PROGRAMMING CAMP

LEGOMINDSTORMSPROGRAMMINGCAMP

RoboticsProgramming101CampCurriculum

Page 2: LEGO MINDSTORMS PROGRAMMING CAMP

ì Everydayofcamp,westartedwithashortvideoshowingFLLrobots,realrobotsorsomethingrelevanttotheday’slesson

ì Weswitchedbetweenusingpowerpoint slidesandshowingtheactualEV3softwareenvironment

ì Wecreatedsomeadd-onchallengesforstudentswhofinishedearly

ì Wecreatedanewprojecteachday

ì Wedidnotshowthechallengesolutionslideunlesssomeoneneededhelp.

InstructorNotes2

Page 3: LEGO MINDSTORMS PROGRAMMING CAMP

Whatisarobot?

Robotsaremachinesinfactories,playsoccer,areinresearchlabs,behavelikeanimals….

WewillusetheLEGOMINDSTORMS

3

Page 4: LEGO MINDSTORMS PROGRAMMING CAMP

LegoMindstorms Robots

YoucanplaceavideoofyourchoiceheretointroduceFLL/Legorobotics

4

Page 5: LEGO MINDSTORMS PROGRAMMING CAMP

ì Sharethelaptopandtherobot

ì Worktogetherinyourgroup

ì Helpeachotherfinisheachchallenge

ì Lifttherobotbythehandle

ì TakeTurns

ì Donotdrop,throw,pushontherobot– theyareveryexpensive

ì Treatyourcomputerwell– theyareexpensive

DosandDoNots thisweek5

Page 6: LEGO MINDSTORMS PROGRAMMING CAMP

ì LearnbasicEV3-GProgramming

ì MakeaMindstorms robotfollowyourinstructions

ì Learnhowtomakeyourrobotmovestraight,turn,repeatactions,choosebetweenactionsandfollowlines

ì HaveFUNwithrobotsandgetinspiredtolearnmore!!

Whatwillwedothisweek?6

Page 7: LEGO MINDSTORMS PROGRAMMING CAMP

ì WeusetheCyberBot NXTdesign– pleasenotethelocationofthecolorsensor

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

brickfaster.

ì Weaskedstudentstomakeanewprojectfileeachdayfortheirprograms.

InstructorNotes7

Page 8: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNEREV3PROGRAMMINGLesson:Introduction

TopicsCovered:NXTBasicsIntroductiontotheNXTBrickandEV3Software

Page 9: LEGO MINDSTORMS PROGRAMMING CAMP

LessonObjectives

1. LearnhowtheNXTbrickoperates

2. LearnaboutthemaincomponentsoftheEV3software

9

Page 10: LEGO MINDSTORMS PROGRAMMING CAMP

The“Brick”Buttons

ì 1=Left,Rightà Navigatemenus

ì 2=CenterButtonàSelectoptionsRunProgramTurnroboton

ì 3=BackàUndoStopProgramTurnrobotoff

1 1

3

2

10

Page 11: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 12: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 13: LEGO MINDSTORMS PROGRAMMING CAMP

EV3Software

CreateNewProject

OpenSavedProject

13

Page 14: LEGO MINDSTORMS PROGRAMMING CAMP

EV3Software:Savingyourproject

File..Save ProjectAs

Saveprojectondesktopas“CampPM1”

Eachprojectcancontainmultipleprograms.

Youwillcreateanewprojectforeachdaythisweek.

14

Page 15: LEGO MINDSTORMS PROGRAMMING CAMP

EV3Software:StartingAnewprogram

OpenedProjects

OpenedPrograms

CreateaProject

CreateaProgram

ProjectProperties

Listofprograms

15

Page 16: LEGO MINDSTORMS PROGRAMMING CAMP

EV3Software:Namingyourprogram

DoubleclickonProgram

Changenameofprogramto“MoveStraight”

16

Page 17: LEGO MINDSTORMS PROGRAMMING CAMP

PROJECTSVS.PROGRAMS

ì YouwillstartbycreatingaPROJECTwhichhasa.ev3extension.YouchangethenameofaprojectbyusingtheSaveProjectAsoptionfromthethe Filemenu.

ì YouwillwritemanyPROGRAMS aspartofeachPROJECTfile.Youchangethenameofaprogramwithinaprojectbydoublingclickingontheprogram’stabandtypinginanewname.

ì Note:Ifthereisan*nexttotheprojectname,youhavemadechangestotheprojectandyouhavenotyetsavedyet.

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

17

Page 18: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 19: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 20: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 21: LEGO MINDSTORMS PROGRAMMING CAMP

EV3PROGRAMMINGLesson:MovingStraight

BeginnerLesson:MovingStraight

Page 22: LEGO MINDSTORMS PROGRAMMING CAMP

LessonObjectives

1. Learnhowtomakeyourrobotgoforwardandbackwards

2. LearnhowtousetheMoveSteeringblock

3. LearnhowtoreadsensorvaluesusingView

12

FINISH

START

B C

22

Page 23: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 24: LEGO MINDSTORMS PROGRAMMING CAMP

CHALLENGE1:MoveStraight

STEP1:GreenBlockTab,ClickandholdMoveSteeringanddragtoprogramming area

STEP2:DropnexttotheStartBlock(greenarrow)

STEP3:ConnectUSBcabletoNXTandLaptop.

STEP4:Download toNXT

Step3

Step4

24

Page 25: LEGO MINDSTORMS PROGRAMMING CAMP

MoveSTEERINGBlock

Steering:Straightorturn Power/Speed Duration/Distance

Modeofoperation

Brake/Coast

B

C

25

Page 26: LEGO MINDSTORMS PROGRAMMING CAMP

NEGATIVE&POSITIVEPOWER:BACKWARD&FORWARD

NegativePower=Backwards

PositivePower=Forward

26

Page 27: LEGO MINDSTORMS PROGRAMMING CAMP

CHALLENGE2:MoveStraight(3SECONDS)

STEP1:GreenBlockTab,ClickandholdMoveSteeringanddragtoprogramming area

STEP2:DropnexttotheStartBlock(greenarrow)

STEP3:SelectOptions.Move“3Seconds”

STEP4:ConnectUSBcabletoNXTand Laptop.

STEP5:Download toNXT

Step3

Step4

Step5

27

Page 28: LEGO MINDSTORMS PROGRAMMING CAMP

MoveStraight:Secondsvs.degreesvs.rotations

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

ì TrymodeSECONDS,DEGREESorROTATIONSandadjustduration/distance

ì Trydifferentspeeds

12

FINISH

START

B C

28

Page 29: LEGO MINDSTORMS PROGRAMMING CAMP

MOVESTRAIGHTdiscussion

Didyouguessandcheckalot?Yes. Programmingwithseconds,rotationsanddegreesusingguessandchecktakesalotoftimeandeffort.

Didchangingthespeedmatter?Yes.Whenyoumoveinsecondsyourspeedwillmatter.

Doyouthinkthewheelsizewillmatter?Why?Wheelsizeaffectsdegrees/rotations.

Doyouthisthebatterylevelwillmatter?Why?Whenyoumoveinseconds,batterylevelschangethepower.

29

Page 30: LEGO MINDSTORMS PROGRAMMING CAMP

CHALLENGESOLUTION

• Thereisabetterway(gotonextslide)tosolvethischallenge

Thiswould bechangedfordegreesorseconds

30

Page 31: LEGO MINDSTORMS PROGRAMMING CAMP

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

SOLUTION:UseView

Browsefor‘Motordegrees’option

31

Page 32: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNEREV3PROGRAMMINGLesson:Turning

Beginner Lesson: Turning

Page 33: LEGO MINDSTORMS PROGRAMMING CAMP

FirstBase

SecondBase

LessonObjectives

1. Makearobotbaseballplayer

2. Learnhowtomaketherobotturn

StartandEndposition

B C

BC

B C

33

Page 34: LEGO MINDSTORMS PROGRAMMING CAMP

PIVOTVs.SPINTurns

180DegreePivotTurn

180DegreeSpinTurn

Noticewheretherobotendsinbothpicturesaftera180degreeturn.

IntheSpinTurn, therobotmovesalotlessandthatmakesSpinTurnsaregreatfortightpositions.Spinturnstendtobeabitfasterbutalsoalittlelessaccurate.

Sowhenyouneedtomaketurns,youshoulddecidewhichturnisbestforyou!

B

CStartPosition EndPosition

MotorsBandCMove

B

CMotorBMoves

StartPosition

EndPositionB

C

B

C

34

Page 35: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 36: LEGO MINDSTORMS PROGRAMMING CAMP

ì Turnsaremeasuredindegrees

MeasuringTurns

90270

180

0/360

135

45

225

31530

15

60

105

75

120

150165

210195

240

300

330345

255

285

36

Page 37: LEGO MINDSTORMS PROGRAMMING CAMP

MakingaPivotturnfor90Degrees

Programyourrobottoturn90degrees....Doestherobotactuallyturn90degreesifyoujustpick90degreesfordistance?

B

C

BC

?

Ans.NO!Thismakesthewheelturn90degrees.Thecorrectsolution onnextpage

37

Page 38: LEGO MINDSTORMS PROGRAMMING CAMP

Howdoyoumaketherobotturn90degrees?

ì Ans.Tryusingtheportviewtomeasuretheturnandtheninputthecorrectnumberofdegrees.

Motordegrees

38

Page 39: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge1ì Yourrobotisabaseballplayerwho

hastoruntoallthebasesandgobacktohomeplate.

ì Canyouprogramyourrobottomoveforwardandthenturnleft?

ì Useasquareboxortape

Challenge2ì Yourrobotbaseballplayermustrun

tosecondbase,turnaroundandcomebacktofirst.

ì Gostraight.Turn180degreesandreturntothesamespot.

TURNINGCHALLENGES

StartandEndposition

FirstBase

B C

SecondBase

39

Page 40: LEGO MINDSTORMS PROGRAMMING CAMP

ClassDiscussionGuide

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

ì Whatsituationswouldoneworkbetterthantheother?Spinturnsarebetterfortightturns(placeswherethereisnotenoughspace)andyoustayclosertoyouroriginalposition.

40

Page 41: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge1ì Youprobablyuseda

combinationofmovesteeringtogostraightanddopivotturnstogoaroundthebox.

Challenge2ì Youprobablyusedaspin

turnbecauseitisbetterfortighterturnsandgetsyouclosertothestartingpoint!

CHALLENGESOLUTIONS

StartandEndposition

FirstBase

B C

SecondBase

41

Page 42: LEGO MINDSTORMS PROGRAMMING CAMP

ì Whatdotheseprogramsdo?

ì Whatarethenamesoftheseblocks

Review42

Page 43: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNEREV3PROGRAMMINGLesson:Displaytothescreen

By:DroidsRobotics

Beginner Lesson: Display Blocks

Page 44: LEGO MINDSTORMS PROGRAMMING CAMP

LESSONOBJECTIVES

1. Maketherobotdisplaysomefunfaceswhilemoving!

2. LearntousetheDisplayBlocktodisplaytextandimages

3. UnderstandwhytheDisplayBlockcanbeusefulinprogramming

44

Page 45: LEGO MINDSTORMS PROGRAMMING CAMP

DisplayBlock

• TheDisplayBlocktoshowinformationandpicturesonthescreen

• Youcancontrolthelocationandsizeoftext

• Youcanusethissameblocktodisplaysensorreadingsandinstructions.

• LocatedinGreenTab

Display!!!!!

45

Page 46: LEGO MINDSTORMS PROGRAMMING CAMP

MoreonDisplayBlocks

ì Twomodestodisplay

ì Pixelmode(Usefordisplayingimagesortext)ì Givesfinecontroloverplacementofimage/text

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

ì Clickontopleftofblocktopreview

46

Page 47: LEGO MINDSTORMS PROGRAMMING CAMP

DisplayingTextinGridMode

Step1:PickDisplayBlock

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

Step3:Usetheboxonthetoprighttoenterthetextyouwanttodisplay

TextGoesHere

47

Page 48: LEGO MINDSTORMS PROGRAMMING CAMP

DISPLAYBLOCK:CHALLENGE1

ì Canyouwriteaprogramtodisplaytextinthemiddleofthescreen?ì Display“HelloWorld”

ì Howdowegetthetexttostayonthescreenlonger?ì Needtomaketheprogram“wait”beforeexiting

48

Page 49: LEGO MINDSTORMS PROGRAMMING CAMP

ì Waitblocksmakeyourprogrampauseforsometime(oruntilsomethinghappens)beforemovingtothenextstep

ì Fornow,wearejustgoingtowaitfortime

WaitBlocks

ChooseTimeformode

Enter“3”forsecondstowait

49

Page 50: LEGO MINDSTORMS PROGRAMMING CAMP

DISPLAYBLOCK:CHALLENGE2

ì Canyouwriteaprogramtodisplaytextinthemiddleofthescreen?ì Display“HelloWorld”

ì Makethedisplayblockrunfor3seconds

50

Page 51: LEGO MINDSTORMS PROGRAMMING CAMP

DISPLAYBLOCK:CHALLENGE3

ì Canyouwriteaprogramtodisplaytextinthemiddleofthescreen?ì Display“HelloWorld”

ì Makethedisplayblockrunfor3seconds

ì Canyoualsomovewhiledoingthis?

51

Page 52: LEGO MINDSTORMS PROGRAMMING CAMP

AtipforMoveSteeringBlocksWithWaitBlocks

ì Leavingthemotor“on”and“off”

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

taskswhilemoving

52

Page 53: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge3solution53

Page 54: LEGO MINDSTORMS PROGRAMMING CAMP

DisplayinganimageinPIXELMode

Step1:PickDisplayBlock

Step2:Clickonthe“SelectMode”whichhasafolder iconandpick“image”

Step3:Usetheemptyboxonthetoprighttopicktheimageyouwanttodisplay

Note:theimageyouchoosemaynotshowupcorrectlyonaNXT

54

Page 55: LEGO MINDSTORMS PROGRAMMING CAMP

DISPLAYBLOCKCHALLENGE4

ì Canyoudisplayeyesonthescreenwhilemoving?Alternateeyeballsthatlookleftandright.• UsetheDisplayBlock,

MotorOnandWaitBlock

• Feelfreetohavefunwiththischallengeandmakeityours!

55

Page 56: LEGO MINDSTORMS PROGRAMMING CAMP

CHALLENGE4Solution

DisplayBlockMotorOn Wait

BlockMotorOff

56

Page 57: LEGO MINDSTORMS PROGRAMMING CAMP

DiscussionGuideupdate?

ì Whymightyouwanttoknowhowtousethedisplayblock?ì Youmightwanttoknowwhatthesensorvalueyourrobotis

seeingì Youmighthavetoprogramarobottostopwhentherobot

reachesaredlinebutitstopsbeforeì Doestherobotseethesamethingyousee?ì Youcandisplaythevalueonthescreenandcheck

ì It’sagreatdebuggingtool.YoucanlearnmoreaboutdebuggingcodeinoneofourIntermediatelessons.

57

Page 58: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNEREV3PROGRAMMING:LessonTouchSensor

Page 59: LEGO MINDSTORMS PROGRAMMING CAMP

LessonObjectives

1. Makearobotthatwanderstheroom– bumpsintowallsandturnsaround

2. LearnhowtousetheTouchSensor

3. LearnthedifferencebetweentheWaitForBlockandtheSensorBlocks

B

C

B

C

BC

59

Page 60: LEGO MINDSTORMS PROGRAMMING CAMP

Whatisasensor?

• AsensorletsanEV3programmeasureandcollectdataaboutissurroundings

• TheEV3sensorsinclude:ì Color– measurescoloranddarknessì Ultrasonic– measuresdistancetonearby

surfacesì Touch– measurescontactwithsurface

60

Page 61: LEGO MINDSTORMS PROGRAMMING CAMP

WHATISATOUCHSENSOR?

• TouchSensorcandetectwhenthesensor’sredbuttonhasbeenpressedorreleased

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

• Whenmightyouusethissensor?ì Usefulforprogramming“movinguntiltouchsensor

ispressed/released/bumpedӓ Youcanalsohaveyourprogramstartorstopwhen

atouchsensorispressed.

61

Page 62: LEGO MINDSTORMS PROGRAMMING CAMP

HowdoyouprogramwiththeTouchSensor?

YellowSensorTab:SensorBlocks

UsedtoReadandCompareSensorValues

OrangeFlowTab:WaitforBlock

Usedtowaitforasensorreading(ortime)

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

62

Page 63: LEGO MINDSTORMS PROGRAMMING CAMP

Reminder:AtipforMoveSteeringBlocksWithSensors

ì Leavingthemotor“on”and“off”

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

taskssuchasreadingasensorwhilemoving

63

Page 64: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 65: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge1Solution

ì Addscreenshot

65

Page 66: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 67: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge2Solution

ì AddScreenshot

67

Page 68: LEGO MINDSTORMS PROGRAMMING CAMP

DISCUSSION

ì WhydidyouuseMOTORONforthesechallenges?ì Youwanttoreadthesensorwhilethemotorison.

ì WhydoweusetheWAITFORBLOCKinthesechallenges?ì Weneedtoprogramtowaitforthecorrectreading

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

BUMPED=pressedandreleasedrecently

ì Whataresomesituationsyoumightwanttouseeachofthesefor?ì PRESSED=runningintoawall,BUMPED=tappedbyhand

RELEASED=nolongertouchingawall

68

Page 69: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNEREV3PROGRAMMINGLesson:ColorSensor

Page 70: LEGO MINDSTORMS PROGRAMMING CAMP

LessonObjectives

1. Maketherobotmoveuptothegreenlinewithoutdoinganymeasurement!

2. LearnhowtousetheColorSensor

3. LearnaboutCoastandBrake 12

FINISH

START

B C

70

Page 71: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 72: LEGO MINDSTORMS PROGRAMMING CAMP

COLORSENSORCHALLENGE

ì Howdoyoumaketherobotmoveuptoagreenlineusingthecolorsensor?

ì Step1:UseWaitForColor

ì Step2:UsethecolorsensorinCOLORMODE

ì Hint:YouwilluseMoveSteering(thinkaboutmotoronandoff)andWaitfor“Color”

72

Page 73: LEGO MINDSTORMS PROGRAMMING CAMP

ColorSensorChallengeSolution

FINISH

START

MoveSteeringSetto“OFF”withBRAKE

MoveSteeringSetto“ON”

WaituntilColor isGreen(#3)

73

Page 74: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNEREV3PROGRAMMINGLesson:Loops

By:DroidsRobotics

Page 75: LEGO MINDSTORMS PROGRAMMING CAMP

LessonObjectives

1. Canweimproveourbaseballrobotbyjustprogrammingittogotothe“next”baseandrepeatingthisaction?

2. Learnhowtorepeatanaction

3. LearnhowtouseLoopBlocks

75

Page 76: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 77: LEGO MINDSTORMS PROGRAMMING CAMP

ì 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

Page 78: LEGO MINDSTORMS PROGRAMMING CAMP

LOOPCHALLENGE

ì Challenge:Writeaprogramtogoaroundbasesuntiltouched.YoumustuseaLOOP

78

Page 79: LEGO MINDSTORMS PROGRAMMING CAMP

LoopCHALLENGESolution79

Page 80: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNEREV3PROGRAMMINGLesson:Switches

By:DroidsRobotics

Page 81: LEGO MINDSTORMS PROGRAMMING CAMP

LessonObjectives

1. Gettherobottoshowahappyfacewhenyoupressitsbuttonandasadfacewhenyoudon’t

2. Learnhowtomakeyourrobotdecidewhattodooutofdifferentchoices

3. LearnhowtouseaSwitchBlock

81

Page 82: LEGO MINDSTORMS PROGRAMMING CAMP

SwitchBlocks

ì Askingtherobotaquestionanddoingsomethingdifferentbasedontheanswerì Example:Doestherobot

seealine?Ornot?

ì BasicallyaYES/NOQUESTION

ì Switchblocksarefoundintheorange/flowtab Thequestionbeing

asked: isthetouchsensorpressed

Runthiscodeiftheanswerisno

Runthiscodeiftheanswerisyes

82

Page 83: LEGO MINDSTORMS PROGRAMMING CAMP

SwitchBlockCHALLENGE1

ì Challenge:Writeaprogramthatchangesthedisplaybasedonifthetouchsensorispressedornotpressed.

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

ì Hint:Youwillneedtousethedisplayblock,loopsandswitchblocks!

83

Page 84: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge1SOLUTION

DisplayBlock

DisplayBlock

WaitBlock

Loop

TouchSensorSelected

84

Page 85: LEGO MINDSTORMS PROGRAMMING CAMP

SwitchBlockChallenge2

ì Canyouwriteaprogramthatdisplaybigeyeballsifyoutouchitonceandasadfaceifyoutouchitasecondtimeandtogglesbackandforth.

85

Page 86: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge2solution86

Page 87: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNEREV3PROGRAMMINGLesson:UltrasonicSensor

By:DroidsRobotics

Page 88: LEGO MINDSTORMS PROGRAMMING CAMP

LESSONOBJECTIVES

1. Makearobotthatfollowsyouaroundlikeadog

2. LearnabouttheUltrasonicSensor

3. LearnhowtouseWaitUntilUltrasonicBlock

4. LearnthedifferencebetweentheWaitUntilUltrasonicBlockandtheUltrasonicBlock

88

Page 89: LEGO MINDSTORMS PROGRAMMING CAMP

ULTRASONIC• Anultrasonicsensormeasuresdistance.

• Youuseitwhenyouneedtomakesureyouareacertaindistanceawayfromatarget.

• Thedistancecanbemeasuredininchesorcentimeters.

• Toreadtheultrasonicsensor,youusetheUltrasonicBlock.Tousetheultrasonictodoanactionuntiladistance,youuse“WaitUntil”

WaitforUltrasonicReadUltrasonic

VS.

89

Page 90: LEGO MINDSTORMS PROGRAMMING CAMP

UltrasonicChallenge1

ì Challenge:Maketherobotmoveuntilitis5inchesawayfromthewall.

ì Step1:Makeanewprogram

ì Step2:Setmoveto“on”

ì Step3:SetwaitblocktousetheUltrasonic

ì Step4:Setmoveblockto“off”

90

Page 91: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge1solution

91

Page 92: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge2:DogFollower

ì Iftherobotiscloserthan5inchesawayfromyourhandmovebackward,otherwisemoveforward.

ì Step1:Dragaloopfromtheorangetab

ì Step2:Dragswitchinside loop

ì Step3:SetswitchtoUltrasonic

ì Step4:SetmovesteeringblocktoONandplaceinTRUE

ì Step4:SetmovesteeringblocktoOFFandplaceinFALSE

92

Page 93: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge2Solution

93

Page 94: LEGO MINDSTORMS PROGRAMMING CAMP

BEGINNERPROGRAMMINGLesson:BasicLineFollower

Page 95: LEGO MINDSTORMS PROGRAMMING CAMP

LESSONOBJECTIVES

1. Learnhowtomakearobotfollowaline

2. LearnhowtogetarobottofollowalineusingColorModeontheEV3ColorSensor

3. Learnhowtofollowalineuntilasensorisactivated

4. Learnhowtofollowalineforaparticulardistance

5. Learnhowtocombinesensors,loopsandswitches

95

Page 96: LEGO MINDSTORMS PROGRAMMING CAMP

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

power)

ì ProgrammingalinefolloweronanNXTbrickwithanEV3requiressomeadjustmentsì WefoundthatMoveSteeringwithanangledoesnotworkwith

theNXTforpivotturnsì Therefore,thecodeusesLargeMotorblocksinsteadforturning

InstructorNotes96

Page 97: LEGO MINDSTORMS PROGRAMMING CAMP

FOLLOWTHEMIDDLE?

ì Humanswanttofollowthelineinthemiddle.

ì Let’shavetherobotdothesamethingusingtheColorSensor

ì Whattypeofquestionscanweaskusingthissensorì Areyouonlineornot?

97

Page 98: LEGO MINDSTORMS PROGRAMMING CAMP

1. Ifweareonblack,keepgoingstraight

2. Ifweareonwhite,turnlefttogetbacktotheline

Seemstoworkfinehere…

98

Page 99: LEGO MINDSTORMS PROGRAMMING CAMP

1. Ifweareonblack,keepgoingstraight

2. Ifweareonwhite,turnlefttogetbacktotheline

OHNO…myrobotisrunningaway….

Whentherobotleavestheleftsideoftheline,theprogramnolongerworks!

99

Page 100: LEGO MINDSTORMS PROGRAMMING CAMP

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

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

SAMESIDEoftheline?ì Insteadofthemiddle,couldtherobotfollowthe“edge”?

ì Sonowtherobotwillfalloffonlythesameside.ì Wewillnowshowyouhowthisworks!

LineFollowing:ROBOTSTYLE100

Page 101: LEGO MINDSTORMS PROGRAMMING CAMP

ROBOTLINEFOLLOWINGHappensontheedges

Leftsidelinefollowing

RightsidelinefollowingTherobothasto

choosewhichwaytoturnwhenthecolorsensorseesadifferentcolor.

Theanswerdependsonwhatsideofthelineyou

arefollowing!

Ifonblack,turnleft.Ifonwhiteturnright.

Ifonblack,turnright.Ifonwhiteturnleft.

101

Page 102: LEGO MINDSTORMS PROGRAMMING CAMP

Startingtherobotonthecorrectside

✔ ✗✔

102

Page 103: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 104: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 105: LEGO MINDSTORMS PROGRAMMING CAMP

CHALLENGE1SOLUTION

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

105

Page 106: LEGO MINDSTORMS PROGRAMMING CAMP

Linefollowerchallenge2

Part1:Makealinefollowerthatstopswhenyoupressthetouchsensor

Part2:Makealinefollowerthatstopsafterittravelsaparticulardistance

106

Page 107: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge2SOLUTION:Sensor

107

Page 108: LEGO MINDSTORMS PROGRAMMING CAMP

Challenge2Solution:Particulardistance

108

Page 109: LEGO MINDSTORMS PROGRAMMING CAMP

DISCUSSIONGUIDE

ì Whyisitimportantfortherobottofollowthesamesideoftheline?

ì Therobotonlyknowstocheckifitisonorofftheline.

ì Thisisabasiclinefollower.Whataresomethingsthatwerenotgoodaboutthislinefollower?Doyouthinkthelinefollowercanbeimproved?

ì Itwigglesalot.SmootherlinefollowersaredescribedintheAdvancedlessons.

ì Whatsensormeasureshowfaryouhavetravelled?

ì TherotationsensorusedinChallenge2solutionmeasureshowmuchthewheelshaveturned.

ì Howwouldyouwritealinefollowerthatwillstopwhenitseesaline?Oranothercolor?

ì Changetheloopexitconditiontousethecolorsensor.

109

Page 110: LEGO MINDSTORMS PROGRAMMING CAMP

FinalChallenge

Page 111: LEGO MINDSTORMS PROGRAMMING CAMP

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

Page 112: LEGO MINDSTORMS PROGRAMMING CAMP

CREDITS

• ThistutorialwascreatedbySanjaySeshanandArvindSeshan

• Morelessonsareavailableatwww.ev3lessons.com

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

112