variables, arrays, and the setpositiontarget...
TRANSCRIPT
Variables, Arrays, and the setPositionTarget Function
2
Goals
Inthistutorial,youwillusetheZRIDE(IntegratedDevelopmentEnvironment)to:
– Createanewproject– Createanewvariable– Createanarray– LearnaboutaSPHEREScontrolandsetPosi'onTargetfuncGon– Compileyourcode(checkitforerrors)– Simulate(runthecodeinasimulaGon)
(x, y, z)
3
Log In
• GototheZeroRoboGcswebsite:www.zeroroboGcs.mit.edu
• Logintoyouraccountwithyour
emailandpassword
4
Create a New Project
• Selectlightblue“ZRIDE”SPHERESiconontopribbon
• Select“NewProject”• Enter
– ProjectName• Type:Project1
- Select“TextEditor”- Select“FreeMode”
• Click“NewProject”
5
Text Editor IDE
• TheTextEditorversionoftheZRIDEisshownhere
• Onthenextpages,youwill:– Reviewwhatyouknowaboutvariables– Createanewvariable
6
Declaring Variables
• Avariableisacontainerthatholdsasinglepieceofacertaintypeofdata.
• Beforeyouuseavariableinyourprogramyoumust“make”itfirst.Todothis,youmusttellthecomputer:
o ThetypeofinformaGonthevariablewillhold(forexample,anumber)
o Thenameofthevariable—likealabelonthecontainersoyoucanfinditanduseit(forexample,Y)
• Thisiscalleddeclaringthevariable.
Y
Y=0;
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
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
9
Variable Names Quiz
WhichofthesevariablenamesareOK?– Y– 3posiGon– five– float– PosiGon_3– _PosiGon3– p%
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%)
11
Declaring a New Variable
• Declareavariable(called“Y”)tosettheposiGonoftheSPHERESsatellite
1. Declarewhattypeofvariablethevariablewillbe.Thetwotypesyouknowsofarareintandfloat.
2. Putaspaceandthentypethenameofthevariable,followedbyasemicolon.
Format:typename; Forexample:intY;
• PutthedeclaraGonstatementatthebeginningofyourcode,wherethetemplatesaystodeclarevariablessharedbetweenfuncGons(don’tworryaboutfuncGonsyet)
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.
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)
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:
15
Array Declaration Question
Supposethenumbersbelowrepresentthetestscoresinaclass.{99,95,82,90,76,91,93,85,100,65}
Howwouldyoudeclareanarraytoholdthescores?(WhatarethethreethingsyoumustincludeinthedeclaraGon?)Whatwouldbethenamesandvaluesofthevariablesinthearray?
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
17
Declare a New Array
• Startyourprogrambydeclaringanarraytoholdx,y,zposiGoncoordinatesfortheSPHERESsatellite
• Gotothespaceabovevoidinit()andvoidloop(),rightbelowtheintegerYthatwedeclaredearlier.Remember,anynewvariablesandarrayswewanttodeclarewillbedeclaredinthisarea.
• ThearrayconsistsofthreefloaGng-pointnumbers,andwewanttonameitposiGon.InsertthedeclaraGonline:
floatposi1on[3];
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;
19
setPositionTarget
• TheSPHEREScontrolfuncGon“setPosi1onTarget”allowsyoutomovethesatellitetoatargetposiGon
• Thetargetpointisinputasanarrayof
threefloatsthatrepresentitsx,y,zcoordinatesinmeters.Thefirstthreeelementsofanarraywillcorrespondtothex,y,andzcoordinates
• WhenaposiGoniscommanded,the
satellitewillfirethrusterstomovetothetargetpoint,thenstop
+x
+y
+z
(x, y, z)
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);
21
Compile and Simulate
• Nowlet’sseeyourprograminacGon!
• Click“Simulate”• TheSimulaGon
windowwillopen:• Change“Maximum
Time”se`ngto60• Click“Simulate”• a“Running”
windowpopupwhilethesimulaGonisbeingconstructed
22
Simulate (cont.)
• Whencomplete:– ThelogwillopenwithasimulaGon
succeededorfailedmessage.– Clickon“ViewResults”
– ClickthePlaybu_on.ThesatellitesshouldappearandtheblueSPHERESshouldmovetocoordinates(2.0,0.0,0.0),justasyoutolditto.
Your Name
23
Review
CongratulaGons!• YouhavesuccessfullycreatedandrunaprogramintheZRIDE.• YouhaveusedanarraytoprogramaSPHERESsatellite.• Youprogrammedthesatellitetomovetoapointin3dimensions!
(0,0.5,0)
z
y
x
(2.0,0,0)