Variables, Arrays, and the setPositionTarget...

23
Variables, Arrays, and the setPositionTarget Function

Transcript of Variables, Arrays, and the setPositionTarget...

Page 1: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

Variables, Arrays, and the setPositionTarget Function

Page 2: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

2

Goals

Inthistutorial,youwillusetheZRIDE(IntegratedDevelopmentEnvironment)to:

–  Createanewproject–  Createanewvariable–  Createanarray–  LearnaboutaSPHEREScontrolandsetPosi'onTargetfuncGon–  Compileyourcode(checkitforerrors)–  Simulate(runthecodeinasimulaGon)

(x, y, z)

Page 3: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

3

Log In

•  GototheZeroRoboGcswebsite:www.zeroroboGcs.mit.edu

•  Logintoyouraccountwithyour

emailandpassword

Page 4: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

4

Create a New Project

•  Selectlightblue“ZRIDE”SPHERESiconontopribbon

•  Select“NewProject”•  Enter

–  ProjectName•  Type:Project1

-  Select“TextEditor”-  Select“FreeMode”

•  Click“NewProject”

Page 5: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

5

Text Editor IDE

•  TheTextEditorversionoftheZRIDEisshownhere

•  Onthenextpages,youwill:–  Reviewwhatyouknowaboutvariables–  Createanewvariable

Page 6: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

6

Declaring Variables

•  Avariableisacontainerthatholdsasinglepieceofacertaintypeofdata.

•  Beforeyouuseavariableinyourprogramyoumust“make”itfirst.Todothis,youmusttellthecomputer:

o  ThetypeofinformaGonthevariablewillhold(forexample,anumber)

o  Thenameofthevariable—likealabelonthecontainersoyoucanfinditanduseit(forexample,Y)

•  Thisiscalleddeclaringthevariable.

Y

Y=0;

Page 7: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

7

Variable Types

Thetwovariabletypesyouwillusemosto[enare:•  Integers(int)

•  Awholenumber,posiGveornegaGve,includingthenumber0.

•  IntegersareNOTallowedtohavedecimals

•  FloaGng-PointNumbers(float)•  Anumber,eitherposiGveornegaGve,thathasatleast1digita[erthedecimal.

•  Floatsallowforgreaterprecision•  Floatsshouldendwithftoshowthattheyaresingle-precisionfloatvalues(thetypeusedbySPHERES)

•  A_empGngtoputthewrongtypeofdataintoavariable(forexample,pu`ngafloatvalueintoavariabledeclaredasanint)willcauseanerror.

ints:0,1,2…-1,-2,-3…17,100

floats:1.1f,2.0f,-5.111111f,3.69f

Page 8: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

8

Naming Variables

RulesfornamingvariablesinC++–  Useonlyle_ers,numbers,andunderscores_–  DonotusespacesorpunctuaGonsymbols–  Beginthenamewithale_er,notanumber(1,2,3)orunderscore_

–  Donotmaketwovariableswiththesamename,eveniftheyhavedifferenttypes– C++iscase-sensi1ve,socapitalizaGonma_ers:myVariableisnotthesameasMYvAriabLe

–  DonotgiveavariableanamethatalreadymeanssomethingelseinC++,like“int”or“switch”—youwilllearnmoreofthesekeywordslater

Page 9: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

9

Variable Names Quiz

WhichofthesevariablenamesareOK?–  Y–  3posiGon–  five–  float–  PosiGon_3–  _PosiGon3–  p%

Page 10: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

10

Naming Variables

Answers:– Y–Good– 3posiGon–Bad(startswithanumber)– five–Good– float–Bad(C++keyword–specifically,atypeofvariable)– PosiGon_3–Good– _PosiGon3–Bad(startswithanunderscore)– p%–Bad(illegalsymbol%)

Page 11: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

11

Declaring a New Variable

•  Declareavariable(called“Y”)tosettheposiGonoftheSPHERESsatellite

1.  Declarewhattypeofvariablethevariablewillbe.Thetwotypesyouknowsofarareintandfloat.

2.  Putaspaceandthentypethenameofthevariable,followedbyasemicolon.

Format:typename; Forexample:intY;

•  PutthedeclaraGonstatementatthebeginningofyourcode,wherethetemplatesaystodeclarevariablessharedbetweenfuncGons(don’tworryaboutfuncGonsyet)

Page 12: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

12

Assign a Value to Your Variable

Nowthatwe’vecreatedthevariableY,weneedtoactuallyputavalueintoit.•  AssignavalueinthesecGonvoidinit()betweenthecurlybrackets{}.Thecode

herewillrunonceatthestartofthegame.•  Typeinthevariablename(withoutthetype),followedbyanequalssign.•  Typethedesiredvalueandendthelinewithasemicolon.

Forexample: Y=0;•  C++ignoreswhitespace(spaces,tabs,andlinebreaks)between“words.”Notethatthepictureshowsthislineindentedbyseveralspaces.UsingwhitespacecanmakethecodeeasiertoreadbuthasnoeffectonitsfuncGon.

Page 13: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

13

Arrays

•  Anarrayisalistofdataofthesametype.

•  Examples:{1.2,3.0,-2.5}Thisisanarrayof3floats(couldbeanx,y,zcoordinatepoint)

{99,95,82,90,76,91,93,85,100,65}Thisisanarrayof10integers(couldbeasetoftestscores)

Page 14: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

14

Declaring Arrays in C

•  Whenyoudeclareanarray,youareactuallydeclaringalotofvariablesatonce.

•  Anarrayisdeclaredbyassigning:-  variabletype(e.g.int)-  thearray’sname(e.g.myArray)-  numberofvariables(e.g.4)in[square

brackets]-  Endthelinewithasemicolon

•  Thevariablesinthearrayarenamedwiththenameofthearrayplusanumberinsquarebrackets.Thenumberingstartswith0.

•  Forexample,inthearrayattherightthemembersarefourintvariablescalledmyArray[0],myArray[1],myArray[2],andmyArray[3].Thevalueofeachvariableisdifferent.

myArray[0]=5myArray[1]=-7myArray[2]=18myArray[3]=100

intmyArray[4];

Member variables:

Array declaration:

Page 15: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

15

Array Declaration Question

Supposethenumbersbelowrepresentthetestscoresinaclass.{99,95,82,90,76,91,93,85,100,65}

Howwouldyoudeclareanarraytoholdthescores?(WhatarethethreethingsyoumustincludeinthedeclaraGon?)Whatwouldbethenamesandvaluesofthevariablesinthearray?

Page 16: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

16

Answer

Thedataconsistsoftenintegers,sowewanttenintvariables.DeclaraGon: intclassScores[10];Members: classScores[0]=99

classScores[1]=95 classScores[2]=82 classScores[3]=90 classScores[4]=76 classScores[5]=91 classScores[6]=93 classScores[7]=85 classScores[8]=100 classScores[9]=65

Page 17: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

17

Declare a New Array

•  Startyourprogrambydeclaringanarraytoholdx,y,zposiGoncoordinatesfortheSPHERESsatellite

•  Gotothespaceabovevoidinit()andvoidloop(),rightbelowtheintegerYthatwedeclaredearlier.Remember,anynewvariablesandarrayswewanttodeclarewillbedeclaredinthisarea.

•  ThearrayconsistsofthreefloaGng-pointnumbers,andwewanttonameitposiGon.InsertthedeclaraGonline:

floatposi1on[3];

Page 18: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

18

Assign Values to Your Array

•  Nowweassignvaluestothearrayinthevoidinit()area,justasbefore.•  Youmustassignavaluetoeachvariableinthearray.Thenameofeacharray

memberisthenameofthearrayplusthemember’snumberinsquarebrackets.•  Let’sassignthevalues2.0tothefirstelement(xcoordinate),0.0tothesecond

(y),and0.0tothethird(z.)Type:posi1on[0]=2.0f;posi1on[1]=0.0f;posi1on[2]=0.0f;

Page 19: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

19

setPositionTarget

•  TheSPHEREScontrolfuncGon“setPosi1onTarget”allowsyoutomovethesatellitetoatargetposiGon

•  Thetargetpointisinputasanarrayof

threefloatsthatrepresentitsx,y,zcoordinatesinmeters.Thefirstthreeelementsofanarraywillcorrespondtothex,y,andzcoordinates

•  WhenaposiGoniscommanded,the

satellitewillfirethrusterstomovetothetargetpoint,thenstop

+x

+y

+z

(x, y, z)

Page 20: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

20

Add setPositionTarget Function

•  Add this command inside void loop(). This section contains the main ZR program, which runs once per second.

•  Whenever you want to use one of the SPHERES controls functions, you must put api. before the name of the function.

•  In order to tell the SPHERES which coordinates to move to, append the array name in parenthesis.

•  End the line with a semicolon. The line should look like this:

api.setPositionTarget(position);

Page 21: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

21

Compile and Simulate

•  Nowlet’sseeyourprograminacGon!

•  Click“Simulate”•  TheSimulaGon

windowwillopen:•  Change“Maximum

Time”se`ngto60•  Click“Simulate”•  a“Running”

windowpopupwhilethesimulaGonisbeingconstructed

Page 22: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

22

Simulate (cont.)

•  Whencomplete:–  ThelogwillopenwithasimulaGon

succeededorfailedmessage.–  Clickon“ViewResults”

–  ClickthePlaybu_on.ThesatellitesshouldappearandtheblueSPHERESshouldmovetocoordinates(2.0,0.0,0.0),justasyoutolditto.

Your Name

Page 23: Variables, Arrays, and the setPositionTarget Functionstatic.zerorobotics.mit.edu/docs/tutorials/VariablesandArrays.pdfDeclaring Arrays in C • When you declare an array, you are actually

23

Review

CongratulaGons!•  YouhavesuccessfullycreatedandrunaprogramintheZRIDE.•  YouhaveusedanarraytoprogramaSPHERESsatellite.•  Youprogrammedthesatellitetomovetoapointin3dimensions!

(0,0.5,0)

z

y

x

(2.0,0,0)