Following a Line Example
Last Modified: 2006-11-07
find:

basket

Acroname Robotics PDF webpage version Following a Line Example PDF

Related
Products

Product image for Brainstem GP 1.0 Module
Brainstem GP 1.0 Module

Contents

Introduction

A common task for a robot is following a pre-determined route.  One way to do this is to make the robot follow a line.  Here at Acroname, we have a floor with green tiles and black tiles.  The green tiles are in the middle of the hallway and the black tiles are on the edges by the walls.  The border between the green and black tiles makes a convenient path for a robot to follow. 

A common way to detect color variations is to illuminate a surface and measure the light reflected back.  Differently colored surfaces reflect different amounts of light.  The color sensor used for this project illuminates the floor with a white LED and takes readings of reflected light with a photoresistor.  We took readings over green tiles and black tiles and used the average as a decision threshold.  Using this value, a robot can make a simple comparison to determine what color tile it is on and follow the border between the green and black tiles. 

The logic for following the border between the green and black tiles is very similar to wall-hugging.  If the robot sees green, it tries to steer right.  If the robot sees black, it tries to steer left.  Because this task is so similar to wall-hugging, the aMo_HugIter function found in the aMo.tea library file works well for implementing the green-black edge-following.  This function only works for a robot controlled by two modified servos. 

Source Code - Configurating Servo Motors

Modified servos on a robot must be individually configured for use with the "aMo.tea" library routines.  Once configured, identical position settings will make the servos go at nearly identical speeds in the same direction.  The following batch file will initialize the servo settings prior to the configuration procedure.  It executes cmdSRV_CFG , cmdSRV_LMT , and cmdSRV_ABS commands. 

/* filename: moinit.bag */ 2 31 0 0 /* cmdSRV_CFG, ServoID=0, SRVCFG=0 */ 2 31 1 0 /* cmdSRV_CFG, ServoID=1, SRVCFG=0 */ 2 32 0 0 40 /* cmdSRV_LMT, ServoID=0, POS0=0, POSRES=40 */ 2 32 1 0 40 /* cmdSRV_LMT, ServoID=1, POS0=0, POSRES=40 */ 2 33 0 60 /* cmdSRV_ABS, ServoID=0, ABSPOS=60 */ 2 33 1 60 /* cmdSRV_ABS, ServoID=1, ABSPOS=60 */

The following procedure is used to configure the servo motors for this example.  Copy the "moinit.bag" file listed above to your aUser directory.  The batch file for this example uses servos 0 and 1.  If you are using different servos, change the index parameters for "ServoID" in the batch file.  Apply power to the robot and connect it to the host computer.  Run the Console and enter the following command:

batch "moinit.bag"

Close the Console then run GP and select the servo tab.  Open the "Config" dialog for the right servo and click the "Enable" box.  Slide the offset slider all the way to the right.  If the servo is going backwards, click the "Reverse" box.  Slide the offset slider back to the left until the servo stops moving.  If you can't find a position where the servo stops moving, make a tiny adjustment to the range slider and try again with the offset slider.  Repeat the previous step for the left servo.  Return to the servo dialog and hit the "Commit Settings to EEPROM" button. 

Note

See the aMo_Init reference page for additional details on configuring two modified servos for use with the "aMo.tea" library routines.  The BrainStem Modified Servo Example also discusses modified servos. 

The new servo settings may be viewed by executing the following commands from the Console.  The commands execute the cmdSRV_CFG and cmdSRV_LMT commands that responds with replies from the BrainStem.  The replies to these commands contain the new, fine tuned settings.  They will be displayed in hexadecimal format.  As with the "moinit.bag" file, if you are using different servos, remember you will need to change the servo index parameters (ServoID). 

/* Where AMO_SRVL=0 and AMO_SRVR=1 */ 2 31 0 /* cmdSRV_CFG; reply is ServoID w/ SRVCFG=AMO_CFGL byte value */ 2 31 1 /* cmdSRV_CFG; reply is ServoID w/ SRVCFG=AMO_CFGR byte value */ 2 32 0 /* cmdSRV_LMT; reply is ServoID, POS0=AMO_POS0L and POSRES=AMO_POSRESL */ 2 32 1 /* cmdSRV_LMT; reply is ServoID, POS0=AMO_POS0R and POSRES=AMO_POSRESR */

Source Code - TEA Program

The source code consists of two TEA files.  The first file has the values for configuring the servos and some parameters for tuning the line-following behavior.  The "aMo_Init" routine initializes the drive servos with the servo settings.  Calling "aMo_Init" is not necessary if the settings are already saved to the EEPROM, but keeping these values in a file makes it easy to reconfigure the servos if the saved settings are ever changed.  Use the batch file above to retrieve the settings for your robot.  The comments in the batch file tell which settings are retrieved by each command. 

/* filename: followdef.tea */ /* modified copy of aMo_Def.tea library file */ #define AMO_SRVL 0 /* index of left servo */ #define AMO_SRVR 1 /* index of right servo */ #define AMO_CFGL 0xC0 /* invert and enable left servo */ #define AMO_CFGR 0x80 /* enable right servo */ #define AMO_POS0L 0x13 /* left servo position 0 offset */ #define AMO_POSRESL 0x20 /* left servo positioning resolution */ #define AMO_POS0R 0x24 /* right servo position 0 offset */ #define AMO_POSRESR 0x22 /* right servo positioning resolution */ /* WALL HUG PARAMETERS */ /* increase gain for quicker response */ /* decrease minv to turn more sharply when no wall is present */ /* decrease maxv to slow down wall-hug */ /* minv should probably not be more than 0.5*maxv */ #define AMO_WHUGGAIN 4 #define AMO_WHUGMAXV 60 #define AMO_WHUGMINV 10

The second file has the line-following logic.  It includes the first file.  A loop polls a start button to determine when it has changed state.  Once the switch has been pushed, the robot starts following the black-green border. 

/* filename: follow.tea */ /* line-following program */ #include "followdef.tea" #include <aCore.tea> #include <aMo.tea> #include <aDig.tea> #include <aA2D.tea> #define DLED 0 /* floor illuminator */ #define DBUTTON 4 /* start button */ #define AFRONT 1 /* forward-looking GP2D12 input */ #define ALIGHT 4 /* floor reflection sensor */ #define THRESH 125 /* black-green threshold */ /* (determined experimentally) */ void main() { int f; int nflag; aMo_Init(); aDig_Config(DLED, ADIG_OUTPUT); aDig_Config(DBUTTON, ADIG_INPUT); aDig_Write(DLED,1); /* wait for start button */ nflag=1; while (nflag) { nflag=aDig_ReadInt(DBUTTON); } /* border-follow */ while(1) { /* measure and scale light reflected from floor */ f=aA2D_ReadInt(ALIGHT); f=f/4; /* track edge between black and green tiles */ /* (acts just like wall-hugging) */ aMo_HugIter(f,THRESH,AMO_RWHUG); } }

Revision History:

  • 2002-01-22: Example Created.
 
 
voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy
© Copyright 1994-2010 Acroname, Inc., Boulder, Colorado. All rights reserved.