Sei sulla pagina 1di 33

Programming Pi-Bot with MiniBloq

STEM Center USA

www.STEMCenterUSA.com 1
Contents
I. Installing miniBloq (windows) .............................................................................................. 4
II. Getting started with the Pi-Bot .............................................................................................. 5
III. Programming Pi-Bot using miniBloq .................................................................................... 8
IV. Making your robot move ..................................................................................................... 16
V. Ultrasonic Sensor ................................................................................................................. 19
VI. Line Following ..................................................................................................................... 24
VII. Reference ............................................................................................................................. 28

www.STEMCenterUSA.com 2
MiniBloq
About miniBloq
MiniBloq is one of the first graphics programming environment developed
for Arduino. MiniBloq provides an excellent environment for learning
Arduino C. The system displays C codes dynamically as the program is
developed using icons. Another important feature of miniBloq is that coding
can be done in C or Python in traditional text format. The ability to switch
between languages along with icon-based programming makes it a powerful
learning tool. Julin da Silva Gillig from Argentina created miniBloq. STEM
Center USA has partnered with Mr. da Silva and adopted miniBloq to work
with the STEM Board.

www.STEMCenterUSA.com 3
I. INSTALLING MINIBLOQ (WINDOWS)
To download miniBloq, go to the link: http://www.stemcenterusa.com/pi-bot/downloads-/
Please download the miniBlog installer file, miniBloq.v0.82.PiBot.Win.exe. Double click on this file
and it installs the miniBloq software and creates a shortcut on your desktop. You must have
administrative rights to install the software. To do that, right-click on the file and choose Run as
administrator from the contextual menu there, as shown in the image below.

Installation will create an icon for miniBloq on the desktop. Double click) miniBloq icon shown to
start the program.

www.STEMCenterUSA.com 4
II. GETTING STARTED WITH THE PI-BOT
MiniBloq supports the STEM Board (Arduino compatible). When you open miniBloq, Pi-Bot is
already selected for you. Make sure you connect your Pi-Bot using the USB cable to the computer
and wait for a few seconds. Then choose the appropriate com port from the pull down menu. You
will get a screen display which may look like as shown below:

The left hand side area shows the STEM board. The right area is meant for miniBloq graphics
programming. MiniBloq has the ability to dynamically generate the C code as you program using
icons. This is a great learning tool. In order to see the generated code, please select View from the
top menu and then choose Generated Code. Now the right area shows the C code generated
dynamically as you program using icons. Refer to the figure below.

www.STEMCenterUSA.com 5
Understanding STEM Board Layout and Pin connections
The default pin connections used in the sample programs uses the following configuration. If you
are a beginner, use default values as shown in the STEM Board diagram. The following diagram
shows all pin connections. Just familiarize yourself but do not wire them yet. We will do this in the
next section starting with a LED.

www.STEMCenterUSA.com 6
Motor wiring
Pin 3 connects to one of the two dc motors. Let us call this A motor (or motor0). AIN1 stands for
one of the leads (top) from A motor

Pin 5 connects to the second lead of A motor. AIN2 stands for second lead (bottom) from the from
A motor

Pin 6 connects to the second dc motor. Let us call this B motor (or motor1). BIN1 stands for one
of the leads (top) from B motor

Pin 11 connects to the second lead of B motor. BIN2 stands for second lead (bottom) from the
from B motor

Ultrasonic sensor wiring


Pin 7 connects to the Trigger pin of the ultrasonic sensor
Pin 8 connects to the Echo pin of the ultrasonic sensor

LED wiring
Pin 13 connects to the anode of an LED

Buzzer
Pin 12- one of the leads of the buzzer

Line sensor wiring


Pin D14 Right light sensor
Pin D15 Middle light sensor
Pin D16 Left light sensor

Servo motor (not included in basic Pi-Bot)


Pin 9 One of the servo motors (servo0)
Pin 10 The second servo (servo1)

IR Sensor
Pin 4 not included in basic Pi-Bot

www.STEMCenterUSA.com 7
III. PROGRAMMING PI-BOT USING MINIBLOQ
Programming in miniBloq is done using icons which can be selected from the action blocks menu and
putting them together like a flow chart in the graphics programming area. Each miniBloq program
begins with the Start icon (initBoard initializes the board) shown below.

Activity 1: Light up an LED


Make sure your Pi-Bot is assembled. All we need is the assembly of the chassis, gearbox, wheels,
caster, STEM board and the mini breadboard. No wiring is necessary at this time. Refer to the section,
Building Your Basic Pi-Bot, of the manual. You can access the manual from the following link:
http://www.stemcenterusa.com/pi-bot/downloads-/
You can also follow the video tutorial for Building Your Basic Pi-Bot from the following link:
http://www.stemcenterusa.com/pi-bot/tutorials-/

In order to light up an LED, we are going to use pin 13. Take any regular LED. Let us say, we will use
the Green LED. The Green wire indicates positive and black wire indicates negative. You will have to
use a resistor in order to connect an LED. Use the small breadboard and complete the following
wiring. A 5-band 330 ohm resistor (Orange-Orange-Black-Black-Brown) included in your kit is
shown below.

www.STEMCenterUSA.com 8
Below is a schematic diagram of your wiring.

Flow Diagram

Once you have wired your LED, we can write the program. The program begins with the initBoard
block. This block is already placed in the programming area as the initial start block.

Our goal is to light up an LED which is connected to Pin 13. On the STEM Board, digital pins can be
set to LOW (0 V) or HIGH (5V). To define an LED connected to pin 13, we can use the I/O Pin block
(two horizontal arrows pointing in opposite direction). Click on the I/O pin block and it will
automatically attach itself under the initBoard block as shown below.

www.STEMCenterUSA.com 9
Notice the RED arrow pointing left. Keep an eye for this red arrow as you will see this option in
various blocks on the right side. Clicking on the red arrow opens a configuration window which has
numerous parameters to choose from. Some of the options include true, false, greater than, less
than, etc. to choose from the pop-up menu. The following diagram choose the option available when
you click on the red arrow.

Click on the RED arrow and choose True as we want to light up an LED. Choosing True will set the
pin to high. To complete this step, choose appropriate digital pin form the pull down menu. Let us
choose Pin 13. So pin 13 is now high. To make it low, we have to set the parameter to false. Your
program should look like this.

www.STEMCenterUSA.com 10
Thats it. Congratulations! You have completed your first miniBloq program. Choose Run
from the component menu or from the tool bar, and watch your LED lighting up.

Exercise 1: In the last activity, we lighted up one LED. Can you light up two LEDS. Dont forget to
include a dropping resistor in your circuit.

Activity 2: Blink an LED


Now that you know how to connect an LED and write a simple program to light it up using miniBloq,
let us now take another step and make the LED to blink. To make an LED blink, we have to effectively
switch it on and off at specified intervals. In order to accomplish this, we will use a While loop block
(recycling arrows) and wait block. Start a new program and click on the while loop block and your
program should look like as shown below:

Anything placed between the recycling arrows will be repeated while the condition is true. To select
the condition, click on the red arrow and choose true as you have done before and your program
will now look like as follows:

Our goal is to blink the LED connected to Pin 13. We will use the I/O pin block as before and the
resulting diagram should look as follow:

www.STEMCenterUSA.com 11
Select the appropriate pin (in this case, we will choose D_13LED) from the pull down menu and
choose True condition. Your final program will now look like as shown below:

Now let us add a wait command. Wait block is described by a clock icon. Choose this icon. Since the
wait command is about time, the red arrow now looks for time in milliseconds (1000 milliseconds =
1 second). Click on the left arrow and choose # to specify time and type in 400. This means we want
the LED to be on for .4 seconds (400 milliseconds).

www.STEMCenterUSA.com 12
To blink an LED, we have to switch it on and off. Now that the LED is on, let us use another the I/O
pin block to turn it off. If you run this program, the LED will still lights up but there is no blinking. It
is because we switch off the LED momentarily but it turns on very quickly because of the loop. To
make sure it is off for a time that we can observe, we will use another wait block and set it to 0.4
seconds again. The final program is shown below:

Congratulations! You have completed your blink program using miniBloq. Run the program
and you will see your LED will blink. Dont forget to save your program. To save your program,
go to file menu and choose Save All option.

www.STEMCenterUSA.com 13
Note: Inserting blocks
If you want to insert a block between two existing blocks, use your mouse to select the block in your
programming space where you want to insert new block to go below. Then click on the action block
on the right side of your window. This will insert the action block into your programming space,
below the originally clicked block. If you made a mistake, you can drag and drop the block anywhere
within the program.

C Program
Notice on the right hand side, as you select icons and create your graphics program, C codes are
dynamically generated. Take a look at the code if you want to learn C programming language, or you
can close this area by selecting the Generated Code option from the View menu option.

Activity 3: Blinking two LEDs


Let us write a program to control two LEDs. Each one comes one after the other. In order to do this,
you will need to use another Pin. We will use Pin 12. Dont forget to use dropping resistors. You
can write a new program or modify the existing program. Below is the sample code for blinking two
LEDs. In this example, each LED is lighted for 1 second.

www.STEMCenterUSA.com 14
Exercise 1: Traffic light
Write a program to simulate a traffic light using 3 LEDs.

www.STEMCenterUSA.com 15
IV. MAKING YOUR ROBOT MOVE
Wiring the motors
Motor wiring is explained in the section, Making Your Pi-Bot Move in the manual, which can be
downloaded from the following link:
http://www.stemcenterusa.com/pi-bot/downloads-/

However, please note that the there is a minor change in the wiring. Pins 3 and 5 are swapped
in miniBloq. If you do not make this change your robot will spin instead of going forward. The
wiring diagram is shown below.

Activity 1: Motor Test


Let us write a program to make the robot move forward for two seconds, wait for a second and then
backup for one second.

MiniBloq has a motor icon (two gears) to represent motors. Pi-Bot uses two dc motors. In miniBloq,
motor connected to pin 3 & 5 is named as motor 0 (also A motor as noted in the STEM Board pin
configuration). Motor connected to pin 6 & 11 is named as motor 1 (also B motor as noted in the

www.STEMCenterUSA.com 16
STEM Board pin configuration). Below is the sample program for making the Pi-Bot move. Notice
the motor block and how the values are added to define the power level in the motor. Motor power
values range from -100 to +100 in miniBloq. This means, for reverse, you have to enter the power
level with a negative sign. To specify negative value choose the modifier, -(X).

This program runs only once. Your robot goes forward and backward just once and then stops. If
you want your robot keep doing this again and again, you would have all this code inside a while
loop block.

www.STEMCenterUSA.com 17
Note: If the motor is not running, check the wiring properly. You might have swapped the
connections.

Exercise 1: Motion control


Is the robot going in a straight line? Run your robot at a moderate speed for a few seconds. Do a few
trials. See if your robot is leaning to one side and if so adjust the power for each motor. Can you
make your robot go in a straight line?

Exercise 2: Square
Write a program to make the robot go in a square.

www.STEMCenterUSA.com 18
V. ULTRASONIC SENSOR
Assemble your ultrasonic sensor using the section, Distance Sensor in the accompanying manual.
Wire your sensor exactly as mentioned. Trigger Pin of your sensor goes to STEM Board digital pin 7
and the echo pin of your sensor goes to STEM Board pin 8.

Activity 1: Ultrasonic Sensor Test


Write a program to test the ultrasonic sensor. We will use a terminal block to display the reading of
an ultrasonic sensor on the computer monitor.

To display we use the terminal option. To show the terminal, please go to the menu view->Terminal
or press Alt + T. However, you will have to use the Open button to see the results. See the figure
below for a view of the terminal.

Once we have the serial monitor block selected, to get the readings from the ultrasonic sensor, we
will define the input to serial monitor as number and that the numbers (readings) come from the
ultrasonic sensor. Make sure you choose the correct ultrasonic sensor (HC-SR4). Sample code is
given below:

www.STEMCenterUSA.com 19
Obstacle Avoidance Using Ultrasonic Sensor
Once you tested the ultrasonic sensor and the motors, you are now ready to program your robot to
move around while not hitting any objects. In order to prevent accidents, you must constantly
watch your ultrasonic sensor reading and take appropriate action when the object is too close.

One option is when the distance from an object is less than a predefined value, the robot should
back up and turn. In order to do that we will need to define two variables, the minimum distance
you want to maintain between the robot and the object, and the speed of the motors. Notice the
icons for creating new variables. Below is the sample code which shows how three variables are
defined. Motor speed is set to 75 and minimum distance is set to 15 cm.

The next step is to introduce the while loop as we want our robot to be wandering around without
stopping. We now want to get the readings from the ultrasonic sensor and compare the result with
the minimum distance we want to maintain. If the distance is greater than the minimum, then the
robot can move forward. If not go back for and then turn. If is a conditional statement and we will

www.STEMCenterUSA.com 20
use this statement now. If statement is defined by a block with a question mark. Since if statement
compares a condition, there are two parameters that need to be defined. One of them (the top one)
is the input from the ultrasonic sensor and the second is the minimum distance which is defined as
15 cm). See example below for the if statement inside the while loop.

Finally if the obstacle is far away (condition is true meaning the distance from the sensor to the
object is greater), then the robot must go forward. We can achieve this by making both motors to
run at the specified speed. These blocks are self-explanatory. If the not, then the corresponding
motor blocks below the false option. See the sample code with explanations below.

www.STEMCenterUSA.com 21
Study each part of this code and understand how it works. The complete code is given below. It is
broken into two parts for the purpose of display only. Second part goes right at the end of first part.
You can download this program from our website.

www.STEMCenterUSA.com 22
Part 1 Part 2

www.STEMCenterUSA.com 23
VI. LINE FOLLOWING
Line following is a very important and fun activity. Many real world system including
robots performing warehouse operations use line following activities. Speaking of
warehouse operations, it may be interesting to know that not too long ago, Amazon paid
almost a billion dollar for Kiva System, a company that makes robots for warehouse
operations. These Kiva robots are strong and silent. They replace the use of humans with
forklifts. These robots use pretty much all techniques our students learn and more: going in
a straight line, turning, machine vision, line following, obstacle avoidance, dead reckoning
and bar code reading. For a day in the life of Kiva Robot, take a look at the following video:
http://www.youtube.com/watch?v=6KRjuuEVEZs

There are several competitions focusing on line following. You can use one, two, three or
even more sensors for line following. In this lesson, we will be using three light sensors.

Line Tracking Sensor Assembly


Before we attempt to do line following, the light sensors should be assembled and tested.
Assembly should be done as shown in the section, Line Tracking of the manual, or as shown
below.

Connect five (5) 30cm wires to the line sensor array as follows:
VCC Red
L Yellow (Sensor on the left)
C Green (Sensor at the center)
R Blue (Sensor on the right)
GND Black

After connecting the wires to the pins, attach the line sensor to the two nylon (1) spacers
using two 4-40 x screws as shown in the diagram below:

www.STEMCenterUSA.com 24
Figure below show the view of the robot from the underside showing the line sensor
wiring.

Wiring the Light Sensors


Take the five wires from the sensor assembly through the hole and bring it to the top and
connect the 5 wires as follows:
Power (Red) to STEM Board 5V Power (on the breadboard)
R Blue to STEM Board Analog Pin A0 (noted as D14 on the diagram)
C Green to STEM Board Analog Pin A1 (noted as D15 on the diagram)
L Yellow to STEM Board Analog Pin A2 (noted as D16 on the diagram)
GND (black) to STEM Board Ground (on the breadboard)

Line Following - Test Program


Before we start writing a line following program, first let us test the sensors. Wire the
sensors as specified. Download and run the program. The line following program is self-
explanatory. In this program, there is one new block, called Boolean to Number block. The
block is shown in the figure below:

This block converts the Boolean data coming from the IO readings to a number (0 for false
and 1 for true) from light sensors so the readings can be stored in a numerical variable.

www.STEMCenterUSA.com 25
Part 1 Part 2

Line Following Program


We will now discover how the line sensors will allow your Pi-Bot to detect and track a line.
In line following Pi-Bot determines its position relative to a black line with the use of light
sensors, located in the front. When the light sensor detects the color (black or white),
sensor outputs Boolean data that is converted to number (1 or 0). By using three light
sensors, your Pi-Bot will know whether it is on a black line (left and right sensors output
0 and the center sensor outputs 1), to the left of a black line, or to the right of a black
line. With this information, Pi-Bot will control the speed of each motor individually to try
and get back over the line. The diagram below show the output of three light sensors.

www.STEMCenterUSA.com 26
Download and run the following program:
Part 1 Part 2

www.STEMCenterUSA.com 27
VII. REFERENCE

Action Blocks
Image Name Description Type
Every miniBloq program starts with a start
Start none
block
Block will execute when some conditions is
While loop condition: Boolean
true
This is same as while block but will execute
While loop iteration count
specified number of times
If Conditional statement decision making condition: Boolean

Wait Wait for specified amount of time. value: number

Variable
Create and initialize a new variable value: number
(create)
variable This block allows to assign a value to the
value: number
(assign) selected variable.
This block allows to control the state of a
I/O pin value: Boolean
digital pin
This block allows to control an analog pin
Analog value: number
(PWM)
value: terminal method
Terminal block: It lets a program to send
Terminal (to print text or
numbers or text through the serial port
number)
Terminal
This blocks enables the terminal block to send
number value: number
a numerical value though the serial port.
method
Terminal text This blocks enables the terminal block to send
value: text
method a text though the serial port.
This block allows to emit sounds. The first note (Hz): number
Buzzer
parameter sets the frequency (note) and the
(setter)
second sets the duration. duration (ms): number
This block allows to control an electric motor
power (-100 to 100):
Motor (setter) connected to one of the controller's motor
number
outputs.
This block allows to set the position (in
ServoRC
degrees) of an R/C Servo connected to the angle: number
(setter)
board.

This block allows to add a comment to the


Comment none
program.

www.STEMCenterUSA.com 28
Allows to insert code directly in the program,
Insert Code allowing for text programming inside a none
graphical program

Modifier Blocks (numbers)


These modifiers are configuration parameters that use numbers. These are context sensitive options
that become available when possible.

Image Name Description Type

variable This block returns the value stored in the


none
(getter) selected variable.

This block returns the state of an electric


Motor
motor connected to one of the controller's none
(getter)
motor outputs.

This block returns the position (in degrees) of


ServoRC
an R/C servo connected to a controller's digital none
(getter)
pin.

This block returns the value of the selected


AnalogRead none
analog sensor input.

This block returns the length (in


microseconds) of a pulse in the selected digital
input. For example, if the first param is true,
value: Bool
the block will wait until the input takes that
PulseIn
value, and then will measure the time until the
timeOut (micro
input goes false again. The second param sets
seconds): number
the maximum time the block will wait to take
the measure.

This block returns the number of milliseconds


timestamp none
since the program began.

This block returns a pseudorandom number


random none
between 0 and 100.

buzzerNote This block returns the frequency belonging to


none
(constant) the selected note.

www.STEMCenterUSA.com 29
number
This block returns a constant number. none
(constant)

This block returns the pi constant with a


pi (constant) limited number of decimals none
(3.14159265358979323846).

This block returns the e constant (or "Euler's


e (constant) number") with a limited number of decimals none
(2.7182818284590452354).
value1: number

value2: number
This block allows to add numbers, variables
Add
and other arithmetic expressions. Note: More operands
(values) can be added
with the add param
button.
value1: number
This block allows to substract numbers,
Substract
variables and other arithmetic expressions.
value2: number
value1: number

value2: number
This block allows to multiply numbers,
Multiply
variables and other arithmetic expressions. Note: More operands
(values) can be added
with the add param
button
This block allows to divide numbers, variables numerator: number
divide and other arithmetic expressions. It also lets
you to write numbers as fractions. denominator: number

negative
This block makes negative the number or the
(unary value: number
arithmetic expression at its left.
minus)

This block allows to raise the first numerical base: number


power parameter (base) to the exponent (or power)
given by the second numerical parameter. exponent: number

This block returns the absolute value of the


Abs value: number
number or the arithmetic expression at its left.

value1: number
modulo This block returns the remainder of the
(remainder) division between it's 2 parameters.
value2: number

www.STEMCenterUSA.com 30
This block returns the minimum of two value1: number
minimum numbers, variables or other arithmetic
expressions. value2: number

This block returns the maximum of two value1: number


maximum numbers, variables or other arithmetic
expressions. value2: number
x: number

fromLow: number
This block allows to map linearly a numerical
map value from a range of values (fromLow to fromHigh: number
fromHigh) to another range (toLow to toHigh).
toLow: number

toHigh: number
x: number
This block allows to limit a number, variable
or any other arithmetic expression to a value
constrain a: number
between a minimum (a) and a maximum (b).
b: number
This block converts a Boolean value (true or
bool to
false) into a numerical value (1 or 0, value: bool
number
respectively)
This block returns the sine of the angle given
sine by the number or the arithmetic expression at value: number
its left (in radians).
This block returns the cosine of the angle
cosine given by the number or the arithmetic value: number
expression at its left (in radians).

This block returns the tangent of the angle


tangent given by the number or the arithmetic value: number
expression at its left (in radians).

This block returns the angle (in radians)


arcsin whose sine is the number or expression on the value: number
left.

This block returns the angle (in radians)


arccosine whose cosine is the number or expression on value: number
the left.

This block returns the angle (in radians)


arctangent whose tangent is the number or expression on value: number
the left.

www.STEMCenterUSA.com 31
This block returns the number obtained from
IRRemote none
an infrared remote control sensor.

This block returns the distance (in cm)


HCSR sensor measured with an ultrasonic sensor like the none
HC-SR04, HC-SR05 or similar.
This block returns the distance (in cm)
measured with an ultrasonic sensor like
Ping none
Parallax's PING)))[TM], Seeedstudio
SEN136B5B or similar.
This block returns the approximate distance
IRRanger (10 (in cm) measured with a Sharp
none
to 80 cm) GP2Y0A21YK0F infrared ranger (10 to 80 cm
aprox.).
This block returns the approximate distance
IRRanger (20 (in cm) measured with a Sharp
none
to 150 cm) GP2Y0A02YK0F infrared ranger (20 to 150 cm
aprox.).

Modifier Blocks (Boolean)


These modifiers are configuration parameters that specify conditions (such as the state of a pin, true
or false, etc.) These are context sensitive options that become available when possible.

Image Name Description Type

I/O Pin This block returns the state of a controller's


none
(getter) digital pin.

true
This block always returns "true". none
(constant)

false
This block always returns "false". none
(constant)

value1: number
This block allows to compare numbers, and
equal
returns "true" if these are equal.
value2: number
value1: number
This block allows to compare numbers, and
not equal
returns "true" if these are not equal.
value2: number
This block allows to compare numbers (or
value1: number
arithmetic expressions), and returns "true" if
less than
the first parameter (value 1) is lower than the
value2: number
second one (value 2).

www.STEMCenterUSA.com 32
This block allows to compare numbers (or
value1: number
arithmetic expressions), and returns "true" if
greater than
the first parameter is greater than the second
value2: number
one.
This block allows to compare numbers (or
value1: number
less than or arithmetic expressions), and returns "true" if
equal the first parameter is lower or equal to the
value2: number
second one.
This block allows to compare numbers (or
value1: number
greater or arithmetic expressions), and returns "true" if
equal the first parameter is greater or equal to the
value2: number
second one.

This block allows to negate logic (Boolean)


not value: Bool
expressions.

value1: Bool

value2: Bool
This block makes a logical (Boolean) "AND"
and
and returns the result. Note: More operands
(values) can be added
with the add param
button
value1: Bool

value2: Bool
This block makes a logical (Boolean) "OR" and
or
returns the result. Note: More operands
(values) can be added
with the add param
button
value1: Bool
This block makes a logical (Boolean)
xor
"Exclusive-OR" (XOR) and returns the result.
value2: Bool

Modifier Blocks (text)


These modifiers are configuration parameters that use text. These are context sensitive options that
become available when possible.

Image Name Description Type


text
This block returns a constant text. none
(constant)

www.STEMCenterUSA.com 33

Potrebbero piacerti anche