Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Serial digital interface wikipedia , lookup

Rectiverter wikipedia , lookup

Analog-to-digital converter wikipedia , lookup

Microcontroller wikipedia , lookup

Opto-isolator wikipedia , lookup

UniPro protocol stack wikipedia , lookup

Charlieplexing wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Transcript
8. Robot Tasks - Single Platform
In this chapter we review algorithmic techniques for individual mobile robots.
Specific examples will be shown for the SPaRC-1 educational desktop robot but
the methods described are adaptable to any wheeled robotic platform with
differential drive.
8.1 SPaRC-1 Basic Configuration
Basic Software Library - The SPaRC-1 uses the BasicX-24 microcontroller,
programmable in Basic. Refer to the NetMedia Basic Express Language
Reference Manual for details on this version of the Basic programming language.
The NetMedia System Library provides details for all the functions and
procedures available on the BasicX-24 and BasicX-1 microcontrollers. A list of
these functions and procedures is provided below:
Math functions
Abs
ACos
ASin
Atn
Cos
Exp
Exp10
Fix
Log
Log10
Pow
Sin
Sqr
Tan
Absolute value
Arc cosine
Arc sine
Arc tangent
Cosine
Raises e to a specified power
Raises 10 to a specified power
Truncates a floating point value
Natural log
Log base 10
Raises an operand to a given power
Sine
Square root
Tangent
String functions
Asc
Chr
LCase
Len
Mid
Trim
UCase
Returns the ASCII code of a character
Converts a numeric value to a character
Converts string to lower case
Returns the length of a string
Copies a substring
Trims leading and trailing blanks from string
Converts string to upper case
Memory-related functions
BlockMove
FlipBits
GetBit
GetEEPROM
MemAddress
MemAddressU
PersistentPeek
PersistentPoke
PutBit
PutEEPROM
RAMpeek
RAMpoke
SerialNumber
Copies a block of data from one RAM location to another
Generates mirror image of bit pattern
Reads a single bit from a variable
Reads data from EEPROM
Returns the address of a variable or array
Returns the address of a variable or array
Reads a byte from EEPROM
Writes a byte to EEPROM
Writes a single bit to a variable
Writes data to EEPROM
Reads a byte from RAM
Writes a byte to RAM
Returns the version number of a BasicX chip
Queues
GetQueue
OpenQueue
PeekQueue
PutQueue
PutQueueStr
StatusQueue
Reads data from a queue
Defines an array as a queue
Looks at queue data without removing any data
Writes data to a queue
Writes a string to a queue
Determines if a queue has data available for reading
Tasking
CallTask
Starts a task
CPUsleep
Puts the processor in various low-power modes
Delay
Pauses task and allows other tasks to run
DelayUntilClockTick
Pauses task until the next tick of the real time clock
FirstTime
Determines if the program has been run since download
LockTask
Locks the task and discourages other tasks from running
OpenWatchdog Starts the watchdog timer
ResetProcessor Resets and reboots the processor
Semaphore
Coordinates the sharing of data between tasks
Sleep
Pauses task and allows other tasks to run
TaskIsLocked
Determine whether a task is locked
UnlockTask
Unlocks a task
WaitForInterrupt Allows a task to respond to a hardware interrupt
Watchdog
Resets the watchdog timer
Type conversions
CBool
CByte
CInt
CLng
CSng
CStr
CuInt
CuLng
FixB
FixI
FixL
FixUI
FixUL
Convert Byte to Boolean
Convert to Byte
Convert to Integer
Convert to Long
Convert to floating point (single)
Convert to string
Convert to UnsignedInteger
Convert to UnsignedLong
Truncates a floating point value, converts to Byte
Truncates a floating point value, converts to Integer
Truncates a floating point value, converts to Long
Truncates a floating point value, converts to UnsignedInteger
Truncates a floating point value, converts to UnsignedLong
Real time clock
GetDate
GetDayOfWeek
GetTime
GetTimestamp
PutDate
PutTime
PutTimestamp
Timer
Returns the date
Returns the day of week
Returns the time of day
Returns the date and time of day
Sets the date
Sets the time of day
Sets the date, day of week and time of day
Returns floating point seconds since midnight
Pin I/O
ADCtoCom1
Com1toDAC
CountTransitions
DACpin
FreqOut
GetADC
GetPin
InputCapture
OutputCapture
PlaySound
PulseIn
PulseOut
PutDAC
PutPin
RCtime
ShiftIn
ShiftOut
Streams data from ADC to serial port
Streams data from serial port to DAC
Counts the logic transitions on an input pin
Generates a pseudo-analog voltage at an output pin
Generates dual sinewaves on output pin
Returns analog voltage
Returns the logic level of an input pin
Records a pulse train on the input capture pin
Sends a pulse train to the output capture pin
Plays sound from sampled data stored in EEPROM
Measures pulse width on an input pin
Sends a pulse to an output pin
Generates a pseudo-analog voltage at an output pin
Configures a pin to 1 of 4 input or output states
Measures the time delay until a pin transition occurs
Shifts bits from an I/O pin into a byte variable
Shifts bits out of a byte variable to an I/O pin
Communications
Debug.Print
DefineCom3
Get1Wire
OpenCom
OpenSPI
Put1Wire
SPIcmd
X10cmd
Sends string to Com1 serial port
Defines parameters for serial I/O on any pin
Receives data bit using Dallas 1-Wire protocol
Opens an RS-232 serial port
Opens SPI communications
Transmits data bit using Dallas 1-Wire protocol
SPI communications
Transmits X-10 data
Processor Hardware - BasicX-24 microcontroller hardware configuration is
discussed in detail in the NetMedia BX-24 Hardware Reference Manual. The
BasicX-24 is programmable from a desktop PC through a serial interface using
the BasicX Development Environment. The major features of the processing
hardware listed below:
BX-24 Hardware - The BX-24 uses an Atmel AT90S8535 core processor
with 400 bytes of RAM and 32 KBytes of EEPROM. This system provides
16 I/O ports (8 of them with ADC), timers, UARTs, and an SPI peripheral
bus. The carriage also includes a voltage regulator and a red/green LED.
BasicX Operating System (BOS) - The BasicX Operating System supports
a multitasking environment and includes the speed BasicX execution
engine.
BasicX Development Environment - The BasicX Development
Environment runs under Windows 98/NT and includes an editor, a
compiler, debugging aids, and sample source code. This environment
permits the downloading of compiled Basic code and support a two-way
communication between the BasicX-24 and the desktop computer during
runtime.
BX-24 Technical Specifications - The following table is taken from the NetMedia
BX-24 Hardware Reference. This table lists the function and operational
characteristics of the BasicX-24 microcontroller.
I/O Lines
16 total; 8 digital plus 8 lines that can be ADC or digital
EEPROM for program
and data storage
RAM
On-board 32 KB EEPROM
Largest executable user program size is 32 KBytes
400 bytes
Analog to digital converter
8 channels of 10 bit ADC, can also be used as regular
digital (TTL level) I/O
6 k samples/s maximum
ADC sample rate
On-chip LEDs
Program execution speed
Has a 2-color surface mount LED (red/green), fully
user programmable, not counted as I/O line
60 microseconds per 16 bit integer add/subtract
Serial I/O speed
2400 baud to 460.8 Kbaud on Com1
300 baud to 19 200 baud on any I/O pin (Com3)
Operating voltage range
Min/Max
Current requirements
4.8 VDC to 15.0 VDC
I/O output source current
10 mA @ 5 V (I/O pin driven high)
I/O output sink current
20 mA @ 5 V (I/O pin pulled low)
Combined max current
load allowed across I/Os
I/O pull-up resistors
80 mA sink or source
Floating point math
Yes
On-chip multitasking
Yes
On-chip clock/calendar
Yes
Built-in SPI interface
Yes
PC prog. interface
Parallel or serial downloads
Package type
24 pin PDIP carrier board
Environmental specs
abs max ratings
Operating temperature: 0 C to +70 C
Storage temperature: -65 C to +150 C
20 mA plus I/O loads, if any
120 k maximum
BX-24 Pin Layout - The pinout configuration of the BasicX-24 is shown below.
This microcontroller and its supporting chip set are mounted on a small circuit
board matching the footprint of a 24-pin wide dual inline integrated circuit.
Additional access points are provided for the EEPROM Chip Select, SPI MOSI
and SCK, processor reset, output capture and the green and red LEDs.
The BasicX-24 is mounted on the SPaRC-1 mobile robot as shown in the figure
below. On the SPaRC we have set up pins 5-12 as logical output pins, capable
of controlling any TTL or CMOS compatible circuit such as a servo or H-bridge
DC motor controller. Pins 13-20 have been set up as analog/digital inputs for
sensors such as contact switches and photocells.
The BasicX-24 provides analog-to-digital conversion (ADC) on these eight pins.
All 16 I/O pins can be used for input or output as needed for a particular
application.
When oriented as shown above, the servo type outputs are on the left. The
leftmost column of pins are ground the next column is 4.5 volts (3 AA batteries)
and the pins in the next column are connected to BX-24 outputs 5 -12 thrugh
1K resistors. On the right side of the BX-24 are 5 columns of sockets. The
sockets in the column closest to the BX-24 are connected directly to I/O pins 1320. The next two columns are common pairs for additional attachments. The
next column is 9 volts (from the 9-volt battery) and the rightmost column is
ground. A small breadboard is also provided on the SPaRC-1 for circuit
prototyping and experimentation.
This breadboard is layed out in the
conventional manner with electrically common rows at the top and bottom and
groups of 5 common recepticles on either side of a central gutter.
Platform Basics - The SPaRC-1 robot uses two drive wheels (called differential
drive) as shown below. These wheels are driven by "hacked" servos as
discussed in Chapter 4.
SPaRC-1
Differential Drive
A wheel can be driven counter-clockwise by sending it a series of pulses with
puslewidth less than approximately 1.5 msec, and clockwise by series of pulses
with pulsewidth greater than 1.5 msec. We can use the BasicX-24 system call
PulseOut( ) to generate these pulses as shown in the sample code below.
for i=0 to 100
Call PulseOut(11,0.002,1)
Call PulseOut(12,0.001,1)
Call Delay(0.02)
next
The system call to Delay(0.02) separates the pulses with a 20 msec delay. The
parameters in system call PulseOut are pin number, pulse width (in seconds) and
the logical value of the pulse, respectively. Since the servos can be connected to
any I/O pin (5 through 12), your code may need to be modified. Also, notice that
to move forward or backward, one wheel must be turning clockwise while the
other is turning counter-clockwise. You can rotate your robot by rotating both
wheels in the same direction.
8.2 Tasks without Sensors
Most of the programming projects we will cover here involve the use of some
types of sensors. Occasionally we will need to move the robot platform or some
some manipulator without direct measurement of the robot itself or the
environment. Controlled movement without sensing is referred to as dead
reckoning. The following code segments and programs illustrate the basics of
dead reckoning for a mobile robot platform.
Straight Line Motion - We can move the platform forward in an approximately
straight line by driving the lefthand wheel counter-clockwise and the righthand
wheel clockwise by the same amount. This can be accomplished using a
for..loop similar to the code shown in the previous section. Unfortunately, not all
servos have the same rotational rate. Sometimes we need to compensate for
speed variations between servos. A simple approach is to provide one wheel
with a few extra pulses to compensate for a slower rotation rate. In the code
below the wheel driven from pin 12 is given an extra pulse every five passes
through the for..loop. You can vary the number of extra pulses by changing the
mod value.
for i=0 to 300
Call PulseOut(11,0.002,1)
Call PulseOut(12,0.001,1)
Call Delay(0.02)
if i mod 5 = 0 then
Call PulseOut(12,0.001,1)
Call Delay(0.02)
end if
next
Turning - When the SPaRC-1 needs to turn you can either turn one wheel or
both. The amount of rotation is determined by the loop count and whether one or
both wheels are turning.
When turning around an outside corner, the robot can move forward until the
center of the inside wheel (i.e. wheel closest to the obstruction) is past the
corner. Note that this will be the pivot point of the turn.
Alternatively, the robot can turn by rotating both wheels counterclockwise (or
clockwise for a left-hand turn). In this case the robot must move further past the
corner before the turn is made.
When turning at an inside corner the robot must start far enough from the edge of
the wall to permit the rear of the robot to clear the wall during the turn. This is an
important point to consider when applying contact sensors for detecting
obstacles.
Moving Slowly - One of the shortcomings often associated with using hacked
servos for drive motors is the difficulty in making them turn slowly. The version of
hacking used on the servos in the SPaRC-1 permits some control of motor
speed. In the SPaRC-1 hacked servos the potentiometer has been disengaged
from the gearbox and permamently fixed in a central position. This means that
servo signals with a pulsewidth near the center of the range (apx. 1.5 msec) will
produce a slower rotational speed. The servo electronics is designed to slow the
motor when it gets close to the indicated position to reduce overshoot or "ringing"
in the servo position as a function of time.
We can take advantage of this feature by sending the hacked servo signals near
the (1.5 msec pulsewidth).
Variations between servos require that we
experiment to find the exact value of the pulsewidth that stops the rototation.
Once this value is found we can send pulsewidths a few microseconds wider or
narrower than this value to drive the wheels slowly forward or reverse.
The source code shown below is a sample program to determine the pulsewidths
needed to move each drive wheel of the SPaRC-1 as a desired speed. The
particular values for x and y will be specific to your robot.
Public Sub
dim x as
dim y as
dim i as
Main()
single
single
integer
x=0.0015
y=0.00153
Call PutPin(26,0)
for i=1 to 1000
Call PulseOut(11,x,1)
Call PulseOut(12,y,1)
Call Delay(0.02)
next
Call PutPin(26,1)
End Sub
Dead Reckoning - We can estimate the distance traveled and the change in
heading (i.e. position and orientation) using dead reckoning. To do this we need
to collect some data.
To determine the straight line distance traveled by our robot running a for..loop
as shown below, choose some point on your robot (e.g. the front) and mark the
position on the floor or a table. Then run the for..loop for some value of K. Mark
the new position of the robot and measure the distance moved.
for i=0 to K
Call PulseOut(11,0.001,1)
Call PulseOut(12,0.002,1)
Call Delay(0.02)
next
Repeat this for several values of N and plot the results. Note that the call to
Delay(0.02) sets the amount of time until the next pulse. The total time between
pulses to one of the drive servos is the sum of the delay and the two pulse widths
(i.e. 0.023 secs or 23 msec). At these settings the servos are moving at
maximum speed for K.(0.023) seconds. Changing the amount of delay will affect
the time the servos run but not the speed. This is an important point for
Loop Count (K)
computing dead reckoning, since the distances moved will be a function of both
the loop count and the delay. The data set you collect can be used to create a
graph similar to the one shown below.
300
275
250
225
200
175
150
125
100
75
50
25
10
20
30
40
50
60
70
80
90
100
Distance (cm)
This graph shows that, except for a small loop count, the relationship between
loop count and distance traveled is fairly linear. We can develop a loop count vs.
distance traveled function to be used in dead reckoning. Assuming a linear
function of the form,
Y = mX + b (m=slope, b=y axis intercept)
Count = m.(Distance) + 0
In our application we will want to know the Count (K) needed to move a desired
distance. Therefore we let the distance traveled be the independent parameter
use to derive the count. We can compute the slope (m) by choosing a pair of
points on the graph that are well separated such as (11,25) and (95,225).
m = slope = rise/run = (225-25)/(95-11) = 2.381
Our function for count vs. distance is
Count = 2.381 (Distance)
As a test we compute the loop count needed to travel 50 cm to be 119.05, but
since we need an integer we would use 119. This compares well with our
experimentally derived value, so we are confident that this function is correct.
This graphical technique is rather limited and cannot be used when there is a
significant amount of noise (due to measurement error or system uncertainty) or
when the relationship between the dependent and independent variables is not
linear. A superior and more generally applicable technique is called method of
least squares.
Method of Least Squares - One of the simplest functional relationships we can
study is the first order polynomial or linear function:
y = f(x) = b + mx
If we make meaurements of a physical system (i.e. determine a set of (x,y)
values) there will be errors in these measurements. It is important to recognize
that there is a difference between the function f(x) and the sampled data set. We
need to establish a criterion for minimizing the difference between the chosen
function f(x) and the y components of the data set. Using this criterion we can
determine values for a the coefficients b and m that best fits the function to the
data. There is no absolute method for determining the best values for b and m
but we will argue for the use of a particular technique called the method of least
squares.
First we want to choose coefficients b and m to minimize the differeneces
between the actual y values and those calculated by the function f(x). That is,
minimize yi = yi - b - mxi for all i=1,..,n
We could simply add all the yi terms, but this would allow positive and negative
difference to cancel each other, possibly masking larger discrepancies between
f(x) and the measured values of y. Instead we can minimize the sum of the
squares of the differences,
minimize yi2
We can normalize each yi term by dividing it by the standard deviation of the yi
values,
2
 1

 y 

   i    
yi  b  mxi 2 
  
 2

2
This quantity is called the "chi-squared" value. It is also the exponent of the
normal probability density function. When we minimize this term we will simultaneously maximize the probability that the calculated yi will equal the measured
yi. The yi's are the measured values and the - b - mxi are the derived values.
The yi's are the measured values and the -b-mxi are the derived values. To find
the values of the coefficients that minimize this sum, we will compute the partial
derivatives of 2 with respect to b and m, and set them equal to zero.

 2   2
   2   yi  b  mxi   0
b
b 


 2   2

 
yi  b  mxi   0


m
m  2

These partial derivatives can be computed and used to obtain values for b and m
that minimize the mean squared error between the measured and computed
values of x and y. (Actually the error is assumed to be associated with the
dependent variable y).
m
n xi yi    xi  yi 


n  xi2   xi 
b
2
  yi   m xi 
n