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 Absolute value
ACos Arc cosine
ASin Arc sine
Atn Arc tangent
Cos Cosine
Exp Raises e to a specified power
Exp10 Raises 10 to a specified power
Fix Truncates a floating point value
Log Natural log
Log10 Log base 10
Pow Raises an operand to a given power
Sin Sine
Sqr Square root
Tan Tangent
String functions
Asc Returns the ASCII code of a character
Chr Converts a numeric value to a character
LCase Converts string to lower case
Len Returns the length of a string
Mid Copies a substring
Trim Trims leading and trailing blanks from string
UCase Converts string to upper case
Memory-related functions
BlockMove Copies a block of data from one RAM location to another
FlipBits Generates mirror image of bit pattern
GetBit Reads a single bit from a variable
GetEEPROM Reads data from EEPROM
MemAddress Returns the address of a variable or array
MemAddressU Returns the address of a variable or array
PersistentPeek Reads a byte from EEPROM
PersistentPoke Writes a byte to EEPROM
PutBit Writes a single bit to a variable
PutEEPROM Writes data to EEPROM
RAMpeek Reads a byte from RAM
RAMpoke Writes a byte to RAM
SerialNumber Returns the version number of a BasicX chip
Queues
GetQueue Reads data from a queue
OpenQueue Defines an array as a queue
PeekQueue Looks at queue data without removing any data
PutQueue Writes data to a queue
PutQueueStr Writes a string to a queue
StatusQueue 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 Convert Byte to Boolean
CByte Convert to Byte
CInt Convert to Integer
CLng Convert to Long
CSng Convert to floating point (single)
CStr Convert to string
CuInt Convert to UnsignedInteger
CuLng Convert to UnsignedLong
FixB Truncates a floating point value, converts to Byte
FixI Truncates a floating point value, converts to Integer
FixL Truncates a floating point value, converts to Long
FixUI Truncates a floating point value, converts to UnsignedInteger
FixUL Truncates a floating point value, converts to UnsignedLong
Real time clock
GetDate Returns the date
GetDayOfWeek Returns the day of week
GetTime Returns the time of day
GetTimestamp Returns the date and time of day
PutDate Sets the date
PutTime Sets the time of day
PutTimestamp Sets the date, day of week and time of day
Timer Returns floating point seconds since midnight
Pin I/O
ADCtoCom1 Streams data from ADC to serial port
Com1toDAC Streams data from serial port to DAC
CountTransitions Counts the logic transitions on an input pin
DACpin Generates a pseudo-analog voltage at an output pin
FreqOut Generates dual sinewaves on output pin
GetADC Returns analog voltage
GetPin Returns the logic level of an input pin
InputCapture Records a pulse train on the input capture pin
OutputCapture Sends a pulse train to the output capture pin
PlaySound Plays sound from sampled data stored in EEPROM
PulseIn Measures pulse width on an input pin
PulseOut Sends a pulse to an output pin
PutDAC Generates a pseudo-analog voltage at an output pin
PutPin Configures a pin to 1 of 4 input or output states
RCtime Measures the time delay until a pin transition occurs
ShiftIn Shifts bits from an I/O pin into a byte variable
ShiftOut Shifts bits out of a byte variable to an I/O pin
Communications
Debug.Print Sends string to Com1 serial port
DefineCom3 Defines parameters for serial I/O on any pin
Get1Wire Receives data bit using Dallas 1-Wire protocol
OpenCom Opens an RS-232 serial port
OpenSPI Opens SPI communications
Put1Wire Transmits data bit using Dallas 1-Wire protocol
SPIcmd SPI communications
X10cmd 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 digitalEEPROM for program
and data storage / On-board 32 KB EEPROM
Largest executable user program size is 32 KBytes
RAM / 400 bytes
Analog to digital converter / 8 channels of 10 bit ADC, can also be used as regular digital (TTL level) I/O
ADC sample rate / 6 k samples/s maximum
On-chip LEDs / Has a 2-color surface mount LED (red/green), fully user programmable, not counted as I/O line
Program execution speed / 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 / 4.8 VDC to 15.0 VDC
Current requirements / 20 mA plus I/O loads, if any
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 / 80 mA sink or source
I/O pull-up resistors / 120 kW maximum
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
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 1KW 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 13-20. 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.