Transcript Document
The Robotics Toolbox
A year’s worth of learning in two hours.
A Quick Tutorial
• Rather than just a Show-and-Tell, let
me explain some of the basics of
computer and microcontroller
programming
• Very abbreviated tour of the book
• Let’s start by learning how the BX-24
outputs data…
“Computers never do what
you want them to do, only
what you tell them to do.”
Brian Patton
Vice President, Robodyssey Systems
The Motherboard
You will control the BX-24 by sending your computer code
through the Robodyssey Advanced MotherBoard, or RAMB.
A serial cable connects the motherboard to your PC.
Connect the wire to the Motherboard now.
Chapter 1: Creating a Program
• The program is written on the PC
in the BasicX language
• The code is saved as a simple text
file. (New programs start with a
blank page.)
• When the program is ready to run,
simply press one button to compile
the program into a language the
BX-24 can understand
• The compiled program is sent to
the BX-24 via a serial cable
Create a Project
Open the BasicX software:
Create a Project
Open the BasicX Editor:
Create a Project
Make a New Project:
Create a Project
Name the project and module “MyFirstApp”
Save the project in the Robots folder on Drive C.
Ready, Set, Code!
Enter your
code here!
Chapter 2: Printing to the PC
Let’s write a short program that will
display a simple message to the
computer screen. The text message
will be sent back to the PC via the
serial cable. Here’s the code:
Public Sub Main()
Debug.Print "Hello, my name is Chris."
End Sub
“Run” the Code
To make your program run on the BX-24, simply press the
F5 key on your keyboard.
Watch the window on the left side of your computer screen.
You should see your message.
Or do you?
Oops!
Increase the string length. From the menu:
Options >> Environment >> Max String Length = 60
Now, here’s my program’s output:
Run it again!
Press the Reset Button on the Motherboard to run the
program again.
Did the same message show up again?
More than Meets the Eye
Turn off the power to the motherboard.
Now turn it back on. Despite removing power to the BX-24,
the program still resides on the chip. Try that with a PC!
It is important to realize that it’s the BX-24, not the PC,
that is controlling the action!
To prove this, swap motherboards with the person next to
you and run their program, displaying their program’s
output on your PC.
Cool, huh?
Printing to an LCD
With a small LCD screen, we can print messages and data
without the need of a computer monitor. This allows us to
be mobile!
See www.basicxandrobotics.com/tutorials/LCD/ to learn how.
Printing Numbers
When computers output data to the screen, that data must
be text, not numbers. In computer-speak, we call text
strings, because it is a string of characters.
Your PC’s processor is big and powerful enough to convert
numbers to strings on the fly, but we have to tell the BX-24 to
perform this conversion explicitly. We do it with the CStr
procedure:
Public Sub Main()
Debug.Print "Hello, my name is Chris."
Debug.Print CStr(38)
End Sub
Run the program and see what happens.
Fancy Printing
We can put two or more strings together with the so-called
concatenation operator, &.
Like this:
Public Sub Main()
Debug.Print "Hello, my name is Chris."
Debug.Print "I am " & CStr(38) & " yrs."
End Sub
Run the program and see what happens.
A Mathematical Genius
Computers are calculating machines, so let’s do a little math.
How about:
2 2
Debug.Print CStr(2 + 2)
or
Debug.Print "2 + 2 = " & CStr(2 + 2)
but not
Debug.Print 2 + 2
Chapter 4: Some More Math
Don’t feel obligated to enter all this code.
3 4
6
12
25
2
8
3.0 * 4.0
Debug.Print CStr(3.0 * 4.0)
6.0 / 12.0
Debug.Print CStr(6.0 / 12.0)
Sqr(25.0)
Debug.Print CStr(Sqr(25.0))
2.0^8.0
Debug.Print CStr(2.0^8.0)
Compiler Assistance
Say you make a mistake. For example, say we wish to
calculate 25 and we enter the following, forgetting to put a
decimal after 25 like this:
Debug.Print CStr(Sqr(25))
The compiler will catch your mistake and give you some hints
on how to fix it:
Chapter 7: Even More Math
Don’t feel obligated to enter all this code.
e
6
log(100)
lne
cos
Exp(6.0)
Debug.Print CStr(Exp(6.0))
Log10(100.0)
Debug.Print CStr(Log10(100.0))
Log(Exp(1.0))
Debug.Print CStr(Log(Exp(1.0)))
Cos(3.14159)
Debug.Print CStr(Cos(3.14159))
Caveat Emptor
If you are in the market for a microcontroller or a robotics
system, make sure that the processor can perform floating
point math!
That is, if the microcontroller cannot manipulate decimal
numbers, you probably don’t want it!
Chapter 5: Using Variables
Just like in math class, we can create and utilize variables:
Dim x as Single
Dim y as Single
Dim Answer as Single
x = 5.0
y = -2.5
Answer = x * y
Debug.Print "Answer = " & CStr(Answer)
There are many kinds of data types. Some common ones
are Integer, Single, & Byte. See Chapter 5 for more details.
Making Math Relevant
Here’s one of many Challenge Problems in my book that
students find interesting. You’ll find it on page 64.
The monthly payment of long-term loans, such as home mortgages or car loans, is
calculated with the formula
where
i
Monthly_ Payment Loan _ Amount
n
1
1
i
• Loan_Amount is the cost of the home or car minus the down payment.
• i is the monthly interest rate. Since you are interested in calculating a monthly
payment, i is the annual percentage rate (APR) divided by 12. Say, for example
the APR is 7.3%, then i 0.073 0.006083.
12
• n is the payment period (in months). If you wish to finance your loan over a
period of 5 years, for instance, n 5 12 60.
65. Calculate the monthly payment amount on a $25,000 loan at 7.3% annual
interest over a period of 5 years. (Check your answers with one of the many
online loan calculators.)
Making Math Relevant
Here’s one way to answer this Challenge Problem:
Dim
Dim
Dim
Dim
Dim
LoanAmt as Single
n as Single
' Payment period in mo.
APR as Single
' Annual rate
i as Single
' Monthly rate
Payment as Single
LoanAmt = 25000.0
' $25,000 loan
n = 60.0
' 5-year payment
APR = 0.073
' APR = 7.3%
i = APR/12.0
' Calculte monthly rate
Payment = LoanAmt*(i/(1.0-(1.0+i)^(-n)))
Debug.Print "Payment = $" & CStr(Payment)
Chapter 3: Output Using PutPin
BasicX has a command named PutPin that can be used to
turn on and off external devices such as lights, LEDs,
motors, buzzers, etc.
All you have to do is tell the BX-24 which pin the device is
connected to and whether to turn it on or off.
You execute this command calling it with Call.
Onboard LEDs
The BX-24 has two built-in LEDs. One
red (pin 25), one green (pin 26).
Think about how to make a light blink.
What we take for granted must be
painstakingly programmed into the
computer line by line:
–
–
–
–
–
Turn on the light
Leave it on for some time
Turn off the light
Leave it off for some time
Repeat
Onboard LEDs
Call PutPin(25, 0)
Pin 25 is the red LED.
Pin 26 is the Green LED.
0 turns it ON
1 turns it OFF
This is one thing I don’t understand about BasicX. For the
internal LEDs, we turn them on with 0 and off with 1.
For all other devices such as external LEDs, buzzers, and
motors, we use 1 to turn them on and 0 to turn them off as
expected.
Too Fast For You?
Call
Call
Call
Call
PutPin(25,
PutPin(25,
PutPin(26,
PutPin(26,
0)
1)
0)
1)
Run this and see what you get.
Are you surprised?
'
'
'
'
Turn
Turn
Turn
Turn
On RED
Off RED
On Green
Off Green
Chapter 3: Delays
Sometimes, the computer performs its
tasks too quickly. We can use a Delay
command to slow it down.
Again, use Call:
Call Delay(0.1)
Call Delay(1.0)
Call Delay(10.0)
' Pauses for 0.1s
' Pauses for 1.0s
' Pauses for 10.0s
Back to the LEDs…
Call
Call
Call
Call
Call
Call
Call
PutPin(25, 0)
Delay(1.0)
PutPin(25, 1)
Delay(0.5)
PutPin(26, 0)
Delay(1.75)
PutPin(26, 1)
Now that’s better!
' Turn On RED
' Turn Off RED
' Turn On Green
' Turn Off Green
Chapter 11: BX-24 & RAMB Pins
As shown here, the BX-24 has 24 pins
coming out of its underside. Pins 1-4 are
used to communicate with the PC and pins
21-24 are used to power to BX-24. These
eight pins are therefore off-limits to us
programmers.
Robodyssey numbered the pins of their
motherboard according to industry
standards. That is, pin 0 on the RAMB
corresponds to pin 5 on the BX-24.
It seems confusing, but you get used to it.
Just remember to add 5 to the RAMB pin
number to get the corresponding BX-24 pin.
Chapter 11: RAMB Signal Pins
Each pin of the BX-24 corresponds to three
pins on the RAMB.
The pins closest to the BX-24 (i.e., the
innermost pins) are the signal pins. These
pins are directly connected to the pins of the
BX-24 and are known as the I/O pins, since
they can receive input voltages as well as
send output voltages.
S
S
Each of the 16 signal pins are capable of outputting a 5V signal.
Eight of the 16 pins can be used for analog-to-digital (A-to-D)
conversions. These are pins 13-20 on the BX-24 and pins 8-15 on
the RAMB.
Chapter 11: RAMB Power Pins
The middle pins on the RAMB are the
power pins. These are used to provide
power to motors and sensors plugged into
the RAMB.
The power pins on the right side of the
board (RAMB pins 8-15) receive their
voltage from a 5V regulator and therefore
provide a very steady 5-volts.
P
P
The power pins on the left side of the board (RAMB pins 0-7) can
either be powered from the 5V regulator or from the battery pack.
(The new RAMB II motherboard comes with a selectable jumper.) If
powered directly from the battery pack, the voltage will drop as the
batteries lose power.
Chapter 11: RAMB Ground Pins
The pins farthest from the BX-24 (i.e., the
outermost pins) are the ground pins.
These pins are connected to the electrical
ground of the RAMB circuitry.
When a sensor or servo is plugged into the
RAMB, the ground pins provide these
peripheral devices with the necessary
electrical ground.
G
G
One should never short the ground pins to the power pins! Doing
so will produce a huge electric current that may damage the
microcontroller, RAMB, and batteries.
Chapter 11: Color Conventions
Ground Wire
Darkest
Lightest
It is a common convention in electronics to wire electrical
components with wires of distinguishable colors. Usually, sensors
and servos are wired from light-to-dark – the wire of the lightest
color carries the signal, the darkest wire is the ground wire, and the
middle wire carries the power.
When connecting or disconnecting components, you should always
turn off the power to the RAMB.
Appendix D: Piezo Buzzer!
Attached to your motherboard is a
piezo buzzer. It is connected to pin 12
on BX-24, which is labeled pin 7 on
Robodyssey’s motherboard.
Note that the positive light-colored lead
wire is connected to the innermost
signal pin and the negative black lead
is connected to the outermost ground
pin.
Piezo Buzzer!
To turn on any external device, use a 1 to set the pin “high”.
Use a 0 (“low”) to turn it off.
Call
Call
Call
Call
Call
Call
Call
PutPin(12,
Delay(0.3)
PutPin(12,
Delay(0.5)
PutPin(12,
Delay(0.3)
PutPin(12,
1)
' Turn On buzzer
0)
' Turn Off buzzer
1)
' Turn On buzzer
0)
' Turn Off buzzer
External LEDs
External LED’s can also be controlled in the
same way with the BX-24 using the PutPin
command.
See www.basicxandrobotics.com to learn how to make
these little guys.
Loops
Many computer applications use
structures called “loops” to repeatedly
perform a task or tasks.
For-to-Next loops run for a finite number
of times.
Do-Loops can run forever.
Chapter 8: For-To-Next Loops
Dim j as Integer
Debug.Print "I can count fast!"
For j = 1 to 5
Debug.Print CStr(j)
Next
Multiple Beeps
For j =
Call
Call
Call
Call
Next
1 to 10
PutPin(12, 1)
Delay(0.1)
PutPin(12, 0)
Delay(0.1)
' Turn On buzzer
' Turn Off buzzer
Chapter 9: Do-Loops
Do
Debug.Print "I can't stop!!!"
Loop
Create Another Project
• Make a New Project
• Save it in the Robots folder on Drive C.
• Name the Project and the Module “Sensors”
Voltmeter
• Not only can the BX-24 output voltages,
it can read them, too.
• This is easy to do with the GetADC
command.
• The analog voltage from a battery, for
example, must be converted into a
digital signal before it can be read by
the computer.
• The BX-24 has eight built-in A-to-D converters! These
are pins 13-20 on the BX-24 (pins 8-15 on the RAMB).
For example, if we plug voltage probes into pin 13
(RAMB pin 8), we can print the voltage readings like so:
Debug.Print CStr(GetADC(13))
Understanding the Signal
• Digital signal 0 = 0V
• Digital Signal 1023 = 5V
(10-bit value)
• Therefore, a signal of 512 is
~2.5V.
• Why 1023?
– The BX-24 uses a 10-bit processor
so 210 = 1024, or 0 to 1023.
• If this 1.28V battery was
measured by the BX-24, what
digital signal would it read?
1023
1.28V
261.9 262
5V
Caveat Emptor
If you are in the market for a microcontroller or a robotics
system, make sure that the processor has a built-in analogto-digital (A-to-D) converter!
That is, if the microcontroller cannot read analog voltages
directly, you probably don’t want it!
Chapter 17: Light Sensor
• Ambient light can also be read
using GetADC and an inexpensive
photoresistor.
• The photoresistor must be plugged
into voltage divider board (VDB).
• Turn off the power to the RAMB
before connecting any cables to
the RAMB!
• The VDB must be plugged into one of the A-to-D pins. The
orientation of the cable is important: The darkest wire is
always positioned nearest the edge of the board. (Dark
means ground.)
A Little Light Reading
• As the light intensity hitting the
photoresistor increases, its
resistance drops and the
voltage to the BX-24 increases.
• Verify that this is so by plugging
your VDB into pin 14 (pin 9 on
the RAMB) and entering the
code below.
Public Sub Main()
Do
Debug.Print CStr(GetADC(14))
Call Delay(0.2)
Loop
End Sub
Light Meter Data
A night watchman was “observed” checking the classroom.
From Section 17.4 in BasicX and Robotics
Inverse Square Law
After calibrating our light sensor, we can verify the famous
inverse-square law of light to within 1.095%!
Lux vs Distance
4000
Intensity
(Lux)
3000
Lux = 73606x -1.9781
R2 = 0.9874
2000
1000
0
0
10
20
30
40
50
60
70
80
Distance
(cm)
See www.basicxandrobotics.com for more details.
Chapter 17: Temperature Sensor
• The ambient temperature be
determined using GetADC, an
inexpensive thermistor, and a
voltage divider board. Just remove
the photoresistor from the VDB and
pop in the thermistor!
• As the temperature increases, the
thermistor’s resistance increases
and the voltage to the BX-24
decreases.
• The temperature can be calibrated
and displayed in Fahrenheit, Celsius,
and Kelvin scales. (See Section 17.7 of my
book.)
Chapter 16: Infrared Range Finder
• The range (or distance) to any
object can be determined using
GetADC and an IR sensor. No
VDB is required here.
• A transmitter sends out an invisible
(to the human eye) beam of
infrared light.
• The amount of light picked up by
the receiver is an indication of the
distance to the object
• Section 16.9 in my book shows
how to convert the digital signal to
an actual range
• Have you seen these? Yes!
Two Readings!
You can read multiple sensors at the same time. With your
thermistor in pin 14, plug your IR sensor into pin 15.
(Remember to turn off the power to the RAMB!) Here’s
one way to display both readings:
Public Sub Main()
Do
Debug.Print CStr(GetADC(14))
Debug.Print "IR = " & CStr(GetADC(15))
Debug.Print
Call Delay(0.2)
Loop
End Sub
Can your IR sensor detect movement?
Flame Sensor
• Raw infrared light (heat) can be
measured and displayed using an
infrared transistor and the GetADC
command. No VDB is required.
• This sensor is also used in linefollowing robots and soccer-playing
robots.
• Plug your flame sensor into pin 16
(RAMB pin 11) and see what
light/heat sources you can detect.
See www.basicxandrobotics.com to learn how to make
these little guys.
Three Readings!
You may want to display all your sensor readings using
variables to help you:
Public Sub Main()
Dim IRValue as Integer
Dim FlameValue as Integer
Do
IRValue = GetADC(14)
FlameValue = GetADC(15)
Debug.Print CStr(GetADC(14))
Debug.Print "IR = " & CStr(IRValue)
Debug.Print "Flame = " & CStr(FlameValue)
Debug.Print
Call Delay(0.2)
Loop
End Sub
Create Another Project
• Make a New Project
• Save it in the Robots folder on Drive C.
• Name the Project and the Module “Servos”
Chapter 11 & 13: Servomotors
• Electric motors spin when a voltage is applied to it.
• Servomotors (or servos) are special motors commonly used
in R/C planes, cars, and boats. Roboticists also use them.
Servomotors
•
•
•
•
•
Servos are controlled with the PulseOut command.
A 1-ms pulse turns the servo clockwise
A 2-ms rotates it counterclockwise
A 20-ms delay is required after each pair of pulses.
The speed can be controlled with pulse width modulation
(PWM) but we’ll save that for another workshop.
Servos and PulseOut
The left servo is
connected to pin 5
Dim i as Integer
For i = 1 to 20
Call PulseOut(5, 0.002, 1)
Call PulseOut(6, 0.001, 1)
Call Delay(0.02)
Next
Always put a 20ms
delay between pulses!
Pulsewidth = 0.002s
(Counterclockwise)
Pulsewidth = 0.001s
(Clockwise)
The right servo is
connected to pin 6
Which direction will your Mouse robot move? Think about it
before running the program!
Playtime!
How far did your robot travel? In what direction did it travel?
Can you do the following:
• Make your robot travel twice as far.
• Turn about its center to the left. To the right.
• Turn 90°.
• Turn 360°.
• Navigate a predetermined path on your table.
• Program your robot to pick up my kids from school.
Appendix G: Grippers
• Grippers use servo motors to open
and close their jaws.
• Loops and the PulseOut command
are used to control its movement.
• To hold an object, the jaws must
continually be pulsed.
Gripper Code
Plug a gripper into BX-24 pin 7 (RAMB pin 7) and enter the
following code to make the jaws open and then close:
' Open the jaws:
For i = 1 to 20
Call PulseOut(7, 0.0011, 1)
Call Delay(0.02)
Next
' Close
For i =
Call
Call
Next
the jaws:
1 to 20
PulseOut(7, 0.00175, 1)
Delay(0.02)
See www.basicxandrobotics.com/tutorials/multitasking/index.html to learn how to
multitask with the BX-24!
Chapter 15: Computer Logic
The computer can be programmed to
think using the “If-Then” logic statement.
Here’s one way that we humans employ
logic statements to leave a room:
1. First, check to see if the door is open.
2. If the door is open, then walk right
through.
3. Else (computerese for “otherwise”), if the
door is unlocked, then turn the handle
and walk through.
4. Else, unlock the door with a key, turn the
handle, and walk through.
The computer
is only as smart
as the person
who
programmed it!
An If-Then Logic Statement
For i = 1 to 5
If (i <= 3) Then
Debug.Print "I am happy to be here!"
ElseIf (i = 4) Then
Debug.Print "BasicX rocks!"
Else
Debug.Print "Let's get started!"
End If
Next
Serious Playtime!
We’ll that’s it! The whirlwind tour is over. In a few minutes
you’ll have the opportunity to explore on your own and put
your newfound tools to use. Stay as long as you like and
have fun. We’ll be here to help.
If you want a challenge, program your Mouse to follow the
black line race course. You’ll need two IR proximity sensors.
Or program it to follow you around the room. It’s not as
difficult as it sounds.
Or use a gripper to grab objects.
Or make your robot move forward when a bright light hits the
photocell.
Or do some science. The sky’s the limit!
Thank you for coming!
If you are interested in having a robot loaned to you so
you can put the book to good use, please ask.
www.basicxandrobotics.com
[email protected]