HWS Robot Festival
Download
Report
Transcript HWS Robot Festival
Hardware Meets
Software
CPSC 120
Principles of Computer Science
February 15, 2012
Basic Stamp Editor: An Integrated
Development Environment (IDE)
Communication Channels
Notice we communicate with our BS2 through a
serial link. One character after another sent
along a wire. Examples are USB (Universal
Serial Bus) and old fashioned RS-232.
Bits in serial mode are encoded as voltage up
down, a pulse, during a short time interval.
This compares to parallel mode of having 8 or
more wires in a cable. Ex. Older printers.
ASCII code (American Standard Code for
Information Interchange)is used to encode
ASCII Code Chart
Integrated Circuits
Our CPU and RAM are manufactured as integrated circuits.
The metal tabs are called pins and allow communication
between the IC and connected circuits. The high/low or
Vdd/Vss values of certain pins can be set/controlled by our
programs! Pin voltage can be read by our programs and thus
allow our program to react accordingly. This is programmer
controlled input/output or programmed I/O.
Controlling Input/Output Pins
Input/output pins on the Basic Stamp 2 IC are brought
out to and are numbered on our BOE board for access
via the breadboard.
BS2 has 16 I/O pins available via BOE, labeled P0, P1,
P2, …, P14, P15, we can use to read/write values.
A TRUE/HIGH value on a pin means +5 volts (Vdd), a
FALSE/LOW value means 0 volts (Vss).
Using program control, we can turn an LED on/off if we
set up such a circuit on our breadboard connected to
BS2 correctly!
As we can read pin values, we can also build sensor
circuits on our breadboard so our robot will be smarter.
Creating a PBASIC program
to control PIN values
Here is our hardware arrangement with the resistors and
LEDs controlled from pins P12 and P13 on the BOE
A simple PBASIC program to blink an LED
'
'
'
'
'
Robotics with the Boe-Bot – HighLowLed.bs2
Turn the LED connected to P13 on/off once every second.
Programmer: John Doe
Date Created: 9/20/2007
Last modified: 2/15/2012
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "The LED connected to Pin 13 is blinking!"
DO
' Beginning of loop
HIGH 13
' Set pin 13 high
PAUSE 500
' Pause for 500 milliseconds
LOW 13
' Set pin 13 low
PAUSE 500
' Pause again for 500 milliseconds
LOOP
' End of loop
Controlling Servo Motors
Servo motors are electric motors controlled by
digital signals, perfect for use with
microcontrollers such as BS2
Servo motors have three attached wires: power
(+6V), ground, and control
Servo motors rotate left or right depending on
the width of a pulse on the control wire
The duration of a servo pulse is about 1.5
thousands of a second. 1.5 milliseconds!
We have library commands in our PBASIC
language to generate such very short pulses
Using two servos, we can drive a robot around!
Servo motors attached to the
Homework Board, our BOE-BOT
connection is similar but easier!
A PBASIC program to test centering of a servo
attached to BOE
' Robotics with the Boe-Bot - CenterServoP13.bs2
' This program sends 1.5 ms pulses to the servo connected to
' P13 for servo centering.
See BOE-BOT manual for details.
' Programmer: John Doe
' Date Created: 9/21/07
' Date Modified: 9/24/07
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
DO
PULSOUT 13, 750
' Send centering pulse to pin 13
PAUSE 20
' Wait 20 milliseconds
LOOP
Review of Common Prefixes from SI
Here are some standard prefix modifiers for multiples of number values:
Deca: means 10. Ex. A geometric decagon has 10 sides.
Kilo: means 1000 = 103. Ex. A kilometer is 1000 meters.
Mega: means 1,000,000 = 106. Ex. A megabye of RAM is (about) one
million bytes. [Actually it is 220 = 1,048,576 in binary]
Giga: means 1,000,000,000 = 109. Ex. A gigabyte of disk storage is
(about) one billion bytes of storage space. [230 in binary]
Other prefixes are used for fractions:
Deci: means 1/10. Ex. To decimate is to destroy 1/10, also decimal.
Centi: means 1/100 = 1 x 10-2. Ex. Centimeter = 1/100 of a meter.
Milli: means 1/1000 = 1 x 10-3. Ex. A millisecond is 1/1000 of a
second. Often denoted ms or msec.
Micro: means 1/1,000,000 = 1 x 10-6. Ex. A microsecond is 1 millionth
of a second. Often denoted sec. [Greek letter mu]
Applications to PBASIC
We have seen how the DEBUG command acts like a mathematical
function and takes arguments, the list of items to display. Ex.
DEBUG CR, “Hello”, CR, “there!”
The PAUSE command takes one argument which is the number of
milliseconds to simply wait, doing nothing.
PAUSE 500 ‘ Wait 1/2 second before continuing
The math: 1 ms x 500 = 500/1000 second = 1/2 second.
The PULSOUT command takes two arguments: A BS2 pin number
and interval to create an up/down signal on that pin. However, the
basic unit used is NOT milliseconds but 2 microseconds!
PULSOUT 12, 500 ‘ Create a pulse on pin 12 of 1 millisecond
The math: 2 s x 500 = 1000 s = 1 millisecond.
So, this turns out to be exactly the kind of extremely short on/off
interval we need to send to a servo motor to rotate in a particular
direction.
A PBASIC program to test centering of a servo
attached to BOE
' Robotics with the Boe-Bot - CenterServoP13.bs2
' This program sends 1.5 ms pulses to the servo connected to
' P13 for manual centering.
See BOE-BOT manual for details.
' Programmer: John Doe
' Date Created: 9/21/07
' Date Modified: 2/15/12
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
DO
PULSOUT 13, 750
' Send centering pulse to pin 13
PAUSE 20
' Wait 20 milliseconds
LOOP
Variables in PBASIC
Variables are simply named memory locations we can
use for arithmetic, control of loops, and storing sensor
values. We choose the variable names!
You can declare four different types of variables in
PBASIC:
Type
Stores This Range of Number Values
Bit
0 to 1
Nib
0 to 15
Byte
0 to 255
Word
0 to 65535 or -32768 to + 32767
We need to choose the appropriate variable type when we
plan on storing values in the variable
Variable Use and Assignments
Here are some variable declarations and assignments (with some errors).
Notice that PBASIC is not case-sensitive. You can use either upper or lower
case letters for your code. Some other languages do not allow this!
‘ Variable declarations begin here
Counter
VAR
WORD
ON_OFF
VAR
BIT
Letter
VAR
BYTE
‘ Some simple assignments begin here
LETTER = 32
Letter = -1
ON_OFF = 4
Counter = 25
Counter = Counter * 100 + LETTER
DO
Counter = Counter + 1
LOOP
Loops in PBASIC
We can use several different types of looping constructions in PBASIC
depending on what we need to do.
‘ Simple FOR…NEXT loop to drive a servo for about 1 second
counter
VAR
Word
FOR counter = 1 TO 46
‘ Iterate this loop 46 times
PULSOUT 13, 850 ‘ Drive servo attached to pin 13 forward, 1.7 ms
PAUSE 20
NEXT
‘ Do nothing for 20 ms
‘ Increment our counter variable, then top of loop
PULSOUT 13, 750
‘ Stop the servo by using the centering value
This code “loops” or iterates through the two commands in the body of of the
loop as the counter variable (started at 1) is automatically incremented by
one each time through. When counter reaches 46, we do one last pass
through the loop, then exit to go to the next PBASIC instruction.