blocos command reference_pdf_78474

16
Blocos Programming Environment Manual Last Modified in 02/19/11 What is the Blocos Programming Environment Blocos is a iconic programing environment, where the user specifies a program with visual elements, or blocks, instead of writing a text code (“Blocos” means “blocks” in Portuguese). The Blocos software automatically translates each block into a statement in the Cricket Logo computer language. The user may even see the generated text program if he wishes so, although it is not necessary – it is expected that students when first introduced to Logo computer programming will find the visual representation of a computer program easier to learn than its textual version. Then, the Blocos software calls a Cricket Logo compiler which compiles the text program, generating a byte-code sequence representing the program, and this byte-code sequence is sent to a controller board to be executed. The Cricket Logo compiler is bundled together with the Blocos software. Blocos has been used successfully with the following controller boards: 1. Br-Gogo Board ( http://sourceforge.net/projects/br-gogo/ ) 2. Babuino Board ( http://babuinoproject.blogspot.com/ ). 3. Gogo Board (http://www.gogoboard.org) Blocos is written in Python, therefore it may be executed in Windows, Linux, and Mac. Through this document, this icon indicates that the following note is valid only when using a Babuino board

Upload: industrial-n6

Post on 03-Aug-2015

509 views

Category:

Technology


1 download

TRANSCRIPT

Blocos Programming Environment Manual

Last Modified in 02/19/11

What is the Blocos Programming Environment

Blocos is a iconic programing environment, where the user specifies a program with visual elements, or

blocks, instead of writing a text code (“Blocos” means “blocks” in Portuguese). The Blocos software

automatically translates each block into a statement in the Cricket Logo computer language.

The user may even see the generated text program if he wishes so, although it is not necessary – it is

expected that students when first introduced to Logo computer programming will find the visual

representation of a computer program easier to learn than its textual version.

Then, the Blocos software calls a Cricket Logo compiler which compiles the text program, generating a

byte-code sequence representing the program, and this byte-code sequence is sent to a controller board

to be executed. The Cricket Logo compiler is bundled together with the Blocos software.

Blocos has been used successfully with the following controller boards:

1. Br-Gogo Board ( http://sourceforge.net/projects/br-gogo/ )

2. Babuino Board ( http://babuinoproject.blogspot.com/ ).

3. Gogo Board (http://www.gogoboard.org)

Blocos is written in Python, therefore it may be executed in Windows, Linux, and Mac.

Through this document, this icon indicates that the following note is valid only when using a

Babuino board

The Blocos Main Window

Start Blocos in the usual manner on your PC. On Windows, double click the file Blocos.py in the

Blocos Folder.

Blocos Main

Window

This is the Blocos window that will appear in a few seconds after you start Blocos.

The different areas are labeled in blue. Descriptions of these follow.

A command window for Python (below) will also start on Windows 2K and XP, and

will fill with debug statements as you use Blocos. For the most part, you can ignore

these. Sometimes you may use this window to help debug problems if you find any.

Post first to the Babuino or Blocos forums.

Python Window

Blocos Main

Window

Blocks Pane Click any block in this pane and move your cursor to the Program Pane. The block

you clicked will appear under the cursor so you can position it and drop it by

clicking.

Blocks Tabs Click these tabs for different Menus of Blocks. Note Arrows at top and bottom of

the displayed Tabs. These will scroll the displayed Blocks Tabs.

Program Pane Blocks are assembled here to build a program. The Start block will always appear

and always starts a program. Blocks “click” together when they are brought close to

each other. The shapes of the Blocks give direct clues as to what Blocks fit together.

Descriptions of the Blocks follow.

Define Pane For certain blocks, radio buttons appear here to choose options and boxes appear in

which values are entered. These are shown in the Command Reference sections.

Menu Bar Mostly, the Menu entries work as you would expect. There is currently no Help – if

you need help beyond the Command Reference which follows, use the Babuino or

Blocos forums.

Use View to see the Logo code that your program generates. For the most part, this

is of little use unless you want to use Cricket Logo. Then you can cut and paste the

code from Blocos into Cricket Logo. (I'm not sure why you'd want to do this, but

it's possible.)

Use File to create new programs or to load existing ones. You may want to create a

directory to hold your Blocos programs. Blocos does not create a specific directory

for this purpose.

Blocos Main

Window

Download Click to download a program to the actual robot hardware board. All Blocks in the

Program Pane will be downloaded. So if you have blocks that are not part of a

program, delete them or they could confuse your robot.

Blocos Command Reference

Control (Motors)

On – Turns on one or more motors. Motor Selection Buttons appear in

the Define Pane and are described below.

Off – Turns off one or more motors. Motor Selection Buttons appear in

the Define Pane and are described below.

On for – Turns on one or more motors for a specified time. Motor

Selection Buttons appear in the Define Pane and are described below.

Reverse – Reverses the direction of rotation of one or more motors.

Motor Selection Buttons appear in the Define Pane and are described

below.

Control (Motors)

Brake – Stops one or more motors as rapidly as possible. Motor Selection

Buttons appear in the Define Pane and are described below.

Clockwise – Sets direction of one or more motors to Clockwise. If your

motor turns in the wrong direction, simply swap the wires on the

connectors.

Anti Clockwise - Sets direction of one or more motors to Anti, or

Counter, Clockwise. If your motor turns in the wrong direction, simply

swap the wires on the connectors.

Motor Selection Buttons – These are presented in the Define Pane when

a Motor Block is selected. Click the boxes to select the motor or motors

to be affected by the block.

Babuino currently supports only Motor A and Motor B.

Power – Set the power for one or more motors. Only values between 0

and 7 should be used. 0 is off and 7 (default) is maximum power. Pulse

Width Modulation is used to vary the speed of the motors. Undefined

behavior may result if values greater than 7 are used.

Position – Sets the angular position of a servo motor.

Currently not implemented on Babuino since servo motors are

not supported.

Flow

If – The test block is evaluated. If it is TRUE, the action blocks are

executed and the program flow continues. If the test block is FALSE, the

program continues without executing the action blocks.

If Else – The test block is evaluated. If it is TRUE, the action blocks on

the left side are executed and the program flow continues. If the test

block is FALSE, the action blocks on the right side are executed and the

program flow continues.

Loop – The action blocks are continually repeated.

Repeat - The action blocks are repeated for the number of times

specified.

Wait Until – Waits until the test block attached to it becomes TRUE, then

program execution continues.

Flow

If Statement Example – The test block is Switch One. If it is pressed, the

test block is TRUE and Motor A will be turned off. If Switch One is not

pressed, then the If statement will have no effect.

*** These examples all assume that switches are HI, or connect to

positive voltage (not ground), when pressed and are LO, or at ground,

when released. ***

If Else Statement Example – The test block is Not (Switch Two). That is,

the sense of Switch Two will be inverted. If Switch Two is pressed, the

test block is FALSE and Motor B will turn on and Motor A will turn off.

If Switch Two is not pressed, the test block is TRUE and Motor A will

turn on while Motor B will turn off. Execution continues with the

statement after the If Else.

Loop Statement Example – Motor A will turn on for 2 seconds (twenty

tenths of a second), then reverse. Since this is a loop, Motor A will run

for another 20 seconds and reverse again. This behavior will continue

forever (or until the batteries go dead). Most robotics programs use a

single loop.

Repeat Statement Example – Motor A turns on. After 2 seconds, it

reverses. Then it reverses again; and one more time (Three times total).

Execution continues with the statement after the Repeat. The number 3

specifies the number of repeats.

Wait Until Statement Example – Motor A turns on. It remains on until

Switch One is HI. Once the test block (Switch One) becomes TRUE,

execution continues and Motor A is turned Off.

Disposition (Positioning)

Positioning Block 1 – Used as a spacer to position

blocks in a program so they do not overlap. Has no

effect on program execution.

Disposition (Positioning)

Positioning Block 2 – Used as a spacer to position

blocks in a program so they do not overlap. Has no

effect on program execution.

Positioning Block 3 – Used as a spacer to position

blocks in a program so they do not overlap. Has no

effect on program execution.

Positioning Block Example – The If Else statement

turns on Motor A for 2 seconds if Switch 1 is HI.

Otherwise, Motor B is turned on for 40 seconds.

Because the number block for 20 overlaps the On For B

action block, it's difficult to read the program.

Positioning Block Example (cont.) - This program does

the same thing, but a Positioning Block 1 is used to

move the left hand action block down, while a

Positioning Block 2 moves the right hand action block

further to the right. The program is now much easier to

read.

Test Condition (Logical Comparison)

Switch – The voltage level of the selected switch is tested. If it is HI

(near the positive supply voltage, usually 5 volts), then the value is

TRUE. If it is LO ( near ground), then the value is FALSE.

Switch Choice Buttons – This block will appear in the Define Pane

when a switch is placed in the Program Pane. These radio buttons allow

the switch to be specified.

Babuino only supports Switch 1 through 4.

Not – The logical value of the item attached is inverted.

And – If the two items are both TRUE, then the value is TRUE. If not,

the value is FALSE.

Test Condition (Logical Comparison)

Or – If either of the two items is TRUE, then the value is TRUE. If

neither is TRUE, then the value is FALSE.

Exclusive Or (XOR) – If the logical value of the the items being

compared is not the same (one TRUE, the other FALSE), this

comparison will be TRUE. If they are both the same, then the value is

FALSE.

Comparison – Compares two numbers. The comparison may be equal,

greater than, or less than. Radio buttons in the Define Pane allow the

comparison to be specified. The logical value of the comparison (TRUE

or FALSE) is the value.

Comparison Radio Buttons – These appear in the Define Pane and allow

the type of comparison to be specified.

Example of a Comparison – The reading from Sensor 1 is compared to

100. If it is less than 100, the Comparison is TRUE. Otherwise it is

FALSE. See the description of Sensors and numbers in the next section.

Example of Conditional Combination – The values of Switch One and

Switch Two are read. If they are both HI (TRUE), then this conditional

is TRUE. If either is LO, then the value is FALSE.

Numbers (16 Bit Integers)

Number - The number entered will be used here. Numbers are positive

integers between 0 and 32,767.

This box appears in the Define Pane. The value of a number is entered in

the box. The ENTER key is used to enter the value and the value will

then appear in the number block you've just entered. Examples of the

use of the Number block have been shown above.

Random – A random number will be used here. A different value will be

used each time the program processes this block. Random is often used

with Modulo to get a random number in a certain range (described

below). The Random block will have a value between 0 and 32,767.

Sensor – The selected sensor will be read each time the program

processes this. Sensors will return a numeric value between 0 and 255

when they are read.

Numbers (16 Bit Integers)

Sensor Radio Buttons – This box will appear in the Define Pane when a

sensor block is inserted into the Program Pane. Click the radio button

beside the sensor you want to use.

Babuino only allows Sensor 1 through 4.

Add – Adds the two numbers.

Subtract – Subtracts the second number from the first.

Multiply - Multiplies the two numbers.

Divide – Divides the first number by the second.

Modulo – Returns the integer remainder from dividing the first number

by the second.

This example shows one use of Modulo. A random number between

zero and 10 results.

Variable - Create or use variable. When you place this block in the

Programming Pane, the following box appears in the Define Pane.

To enter a new variable name, be sure your cursor is in top box (for

naming), enter the name, and hit return. To use an existing variable,

click it's name from the drop down menu. Generally, it is not a good idea

to use a new variable without first using the Create and Set Block

described next.

Create and Set a Variable – Drag the “Variable” block (above) into the

named opening in this block. Using the box shown above, provide a

name to create a new variable, or select an existing variable name. Next

attach a number to the block to set the value. Besides assigning a value

to a new variable, values of existing variables may be changed in this

Numbers (16 Bit Integers)

manner. See the example below.

Example of setting and using variables – The variable named “Speed1”

is created and assigned a value of 3. The variable named “Speed2” is

created and assigned a value of 1. The Power of Motor A is set to the

value of variable Speed1 (in this case, 3).

Sixteen variables may be defined and used in a program.

Time

Time – Used to set a time delay in tenths of a second. The

following box will appear in the Define Pane.

This is the box that will appear in the Define Pane and allow a

Time value to be set.

Timer – This block represents the current value of the 16 bit timer.

The value rolls over at 65,535 or about every 65.535 seconds.

Time is in milliseconds with a four millisecond granularity.

Reset Timer – Causes the internal 16 bit timer to be reset to zero.

Allows more accurate time delays to be specified.

Wait For – Causes program execution to pause for the time

indicated. It should have a Time Block attached to it, although a

Number block can be used also.

Example of Timer Use – This example shows how Reset Timer

and Timer may be used for a time delay with more than 0.1 second

resolution. In this case, we turn on Motor A for 640 milliseconds,

then turn it off for 2 seconds, and repeat forever. Reset Timer starts

the timer at 0. When the timer reaches 640, the comparison is

TRUE and Motor A is turned off. Be sure the time delay you

specify is more than the time it takes to execute the blocks before

the comparison if you want it to be accurate. Avoid comparing for

“=” since there is some uncertainty in the Timer.

Others (Miscellaneous)

Beep – Causes the Beeper to sound for 50 milliseconds. There will be a

minimum pause of 50 milliseconds before another beep.

Others (Miscellaneous)

Comment – Used to insert comments into a program. Has no effect on

program execution.

Led ON – Turns on the User Led.

Led OFF – Turns off the User Led.

Show

Not implemented on Babuino.

Comment Example – This is an example of a comment block that can be

used in a program. It has no effect on a program and is not downloaded.

Procedures

New Procedure – Used to define a new procedure (also

called a function or subroutine).

End – Used to end a procedure definition

Procedures

When a new procedure is defined, a block with the

new name is created. Here is the Blocks Pane when the

Procedures Blocks Tab is chosen.

The purpose of defining a new procedure block is to

group a number of blocks that are used repetitively in

your program. This allows you to reuse the set of

blocks easily without having to re-enter them each

time. It also keeps your program shorter and clearer.

When the New Procedure block is selected and placed

into the Program Pane, this box will appear in the

Define Pane. Enter a name for your new procedure.

Here is how the Blocks Pane will appear after a new

procedure is named. A new block with the new name is

added. The new Procedure is defined in the Program

Pane.

There's very little room for names, so they're usually

very short. If many new procedures are defined, then

it's good to add comments with Comment Blocks to

explain what they each do.

Complete the definition of your new procedure by

adding blocks and ending with an End block.

Don't delete the new procedure from the program

window! It must remain.

Using a new Procedure – Once defined, the new

procedure may be used just like any other action block.

It may be used as often as you like in your main

program. Each time it is encountered, the blocks in

your procedure will be executed.

Programming with Blocos This section is tutorial in nature and explains how to use Blocos to create and modify programs.

Programming with Blocos

Starting Out Start Blocos as described above. The Main Window will appear. You build

your program by choosing blocks from the Blocks Pane and assembling

them in the Program Pane. The Start Block will always be the first block

in your program. Once you have assembled your program, you can save it

using File from the Menu Bar.

Use the Blocks Tabs to find the blocks you want to use. To add a block to

your program, click the block and move your cursor to the Program Pane.

The block will appear under your cursor. Drag it where you want it and

click again to drop it in place.

Simple Example – Here is a very simple example program. This program

simply turns on Motor A for 3 seconds and reverses it. Starting with this,

we'll learn how to change, delete, and move blocks.

Changing a Block - Step 1 – We'll change the time block from 30 to 40.

To change a block, begin by clicking on it. The block will turn gray and a

red arrowhead will appear beside it. If there is a box or radio buttons

associated with the selected block, it will appear in the Define Pane and

changes may be made. In this case, we use the box (not shown – see

example in Command Reference above) to enter 40 for the time value.

Press the ENTER key and the new value will appear in the gray block.

Changing a Block - Step 2 – To complete the change, click any where in

the Program Pane except on the gray block. If we click on another block,

it will turn gray and the red arrowhead will appear beside it. Clicking

anywhere else in the Program Pane will return all blocks to their normal

colors, turn off the red arrowhead, and clear the Define Pane. The block

we changed will now appear in its original color with the change we

made.

Programming with Blocos

Moving and Adding blocks - Step 1 – We'll add a block to our program to

make the motor start turning in the Anticlockwise (counterclockwise)

direction. Since we want that block to be first, we have to move the other

blocks to make room for it.

To move a block, click and drag it a small distance from its original

position. Any blocks attached below or to the right of the selected block

will move with it. In this example, we've moved the blocks below Start

away so we can insert the Anticlockwise block. we click the OnFor block.

That block and all the others below it will turn gray. We then dragged

them away from Start.

Moving and Adding blocks - Step 2 – We now click the Anticlockwise

block in the Blocks Pane and move our cursor to the Program Pane. The

Anticlockwise block appears under our cursor. We drag it below the Start

block and click to drop it in place.

Moving and Adding blocks - Step 3 – Now we click the OnFor Block

again and drag it below the Anticlockwise block. Move the cursor

anywhere in the Program Pane, off the blocks, and click again and our

block addition is complete.

Taking blocks apart and Deleting Blocks- Step 1 – Suppose we decide we

really don't want the Anticlockwise block after all and want to delete it.

Step 1 is to click the Anticlockwise block and drag it away from the Start

block. Notice that all the other blocks turn gray and move along with it.

If you don't move the blocks far enough from Start, they'll snap back and

reconnect. If that happens, just try again and move the blocks a little

farther.

If we press the DELETE key, all the blocks are deleted and we're left with

only the Start block. We can use Edit Undo from the Menu Bar to recover

our work. Proceed as follows to delete only the desired block.

Programming with Blocos

Taking blocks apart and Deleting Blocks- Step 2 - To isolate the

Anticlockwise block, first click anywhere in the Program Pane (except on

a block) to de-select all the blocks. Then click on the OnFor block and

drag it and all the blocks below it away from the Anticlockwise block.

Taking blocks apart and Deleting Blocks- Step 3 – Now click on the

Anticlockwise block to select it.

Taking blocks apart and Deleting Blocks- Step 4 – To complete our

change, press DELETE to delete the Anticlockwise block, click and drag

the OnFor block below the Start block, and click anywhere in the Progarm

Pane.

Besides learning to delete blocks, we've also learned how to take blocks in

a program apart so we can move or reorder them. We'll do this lots as we

build our Blocos programs for Babuino.

Although this might seem like a lot of work and clumsy at first, a little

practice and this becomes easy and intuitive.

Copy and Paste – Select a block or group of blocks in the manner

described above. Then use [CTRL]C to copy the block and [CTRL]V to

paste a copy into the Program Pane. This can save a lot of time navigating

menus.

Created February 24, 2010 by Jim Larson (“doctek”)

Thanks to Brett Nelson for his helpful review and suggestions.

Most of all, thanks to the Blocos and Babuino an Gogo Board teams for their wonderful creations!

If you have comments or corrections, please post to the Blocos or Babuino or Gogo Board Forums