encapsulation day 11
DESCRIPTION
Encapsulation Day 11. Computer Programming through Robotics CPST 410 Summer 2009. Course organization. Course home page (http://robolab.tulane.edu/CPST410/) Lab (Newcomb 442) will be open for practice with 3-4 Macs, but you can bring your own laptop and all robots. Encapsulation. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/1.jpg)
EncapsulationDay 11
Computer Programming through Robotics
CPST 410
Summer 2009
![Page 2: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/2.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
2
Course organization
Course home page (http://robolab.tulane.edu/CPST410/)
Lab (Newcomb 442) will be open for practice with 3-4 Macs, but you can bring your own laptop and all robots.
![Page 3: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/3.jpg)
Encapsulation
My Blocks vs. Functions
![Page 4: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/4.jpg)
My Blocks in NXT-GKelly §26 ‘My Block is your block’
Open the Mindstorms app.
![Page 5: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/5.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
5
A silly program
Tribot, do the following slowly (at 50% speed):Spin right for a rotation,and then show a happy face.Move forward for a second, and then beep.Spin left for a rotation,and then say “good job!”.
![Page 6: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/6.jpg)
SillyProgram.rbt
![Page 7: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/7.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
7
Analysis
Note that the program really has three parts,but the structure of the program does not
show this division.There is a way to encapsulate each part into
its own block, called a My Block.
![Page 8: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/8.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
8
My Blocks
Select the first part (the first three blocks). Go to the Edit menu of the Mindstorms NXT application
and choose “Make a New My Block”. Give the block an informative name like StartUp and a short
explanation. Click the ‘Next’ button and choose a picture for it, such as one of
the motor icons. Click ‘Finish’.
You have just created your first MY BLOCK. It should have occupied the place of the originals in your
program, as shown in the next slide.
![Page 9: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/9.jpg)
SillyProgram.rbt with StartUp
![Page 10: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/10.jpg)
Keep on going
Note that if you double click on StartUp, it opens up in a new piece of graph paper, as if it were a program itself.
MY BLOCKS are stored in the Custom palette (aquamarine equals sign).
Now turn the other two pairs of blocks into My Blocks.
![Page 11: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/11.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
11
SillyProgram.rbt with MY BLOCKS
![Page 12: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/12.jpg)
Functions in NXC
Close the Mindstorms app,
and open BricxCC.
![Page 13: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/13.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
13
SillyProgram.nxctask main (){ OnFwdSync(OUT_BC, 50, 100); Wait(1000); Off(OUT_BC); GraphicOut(0, 0, "faceopen.ric"); Wait(500);
RotateMotor(OUT_BC, 50, 360); PlayTone(440, 500); Wait(500);
OnFwdSync(OUT_BC, 50, -100); Wait(1000); Off(OUT_BC); PlayFile("Good Job.rso"); Wait(700);}
![Page 14: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/14.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
14
Functions = My Blocksp. 149ff
Syntax: insert ‘void’ from Programreturn_type name(argument_list){
“statements”}
If there is no return type, use ‘void’.Define a function before the main task.
![Page 15: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/15.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
15
SillyProgram.nxc with one function
void StartUp(){ OnFwdSync(OUT_BC, 50, 100); Wait(1000); Off(OUT_BC); GraphicOut(0, 0, "faceopen.ric"); Wait(500);}task main(){
StartUp();RotateMotor(OUT_BC, 50, 360);PlayTone(440, 500);Wait(500);OnFwdSync(OUT_BC, 50, -100);Wait(1000);Off(OUT_BC);PlayFile("Good Job.rso");Wait(700);
}
![Page 16: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/16.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
16
SillyProgram.nxc with 3 functions
void StartUp(){ OnFwdSync(OUT_BC, 50, 100);
Wait(1000);Off(OUT_BC);GraphicOut(0, 0, "faceopen.ric");Wait(500);
}void ImportantStuff(){ RotateMotor(OUT_BC, 50, 360);
PlayTone(440, 500);Wait(500);
}void ShutDown(){ OnFwdSync(OUT_BC, 50, -100);
Wait(1000);Off(OUT_BC);PlayFile("Good Job.rso");Wait(700);
}task main(){
StartUp();ImportantStuff();ShutDown();
}
![Page 17: Encapsulation Day 11](https://reader036.vdocuments.us/reader036/viewer/2022062305/56814eca550346895dbc674f/html5/thumbnails/17.jpg)
7/1/09 Harry Howard, CPST 410, Tulane University
17
Next time
Parallelism.