PDA

View Full Version : Small Line Following Robot - AntBot



Sid723
08-23-2010, 12:37 AM
:veryhappy:Hello Robot enthusiasts. I have been away for a while (work work work). I am still working on the Lawnbot project, but decided to take a break on that one. I am currently working with my grandson on a line following robot project. I may need some help with this one, but for now I just wanted to show everyone what I have so far.

2055

I am using a PIC12F615 microcontroller as the brain. It is an 8 pin chip and this is good, because I wanted to make the bot small. It uses 4 AAA type batteries and two 6 volt Radio Shack motors. The line sensors are Infrared sensor LED and sensor pairs that I also picked up from Radio Shack. All of this fits into a small box, of course from Radio Shack.

This is the program I have so far:

// ************************************************** ********************
// *
// Title: "Test Program # 2 for AntBot-Line following Robot" *
// Creation Date: 8/23/2010 *
// Author: Isidro R. Murillo *
// Company: None *
// *
// ************************************************** ********************
// *
// Architecture: Baseline PIC Micro *
// Processor: PIC12F615 *
// Compiler: Hitech C Pro for the PIC10/12/16 MCU family *
// (Lite) Ver.9.65PL1 *
// *
// ************************************************** ********************
// *
// Files Required: None *
// *
// ************************************************** ********************
// *
// Description: *
// Test program 2 for small line following robot made with Radio *
// Shack box and motors and 2 infrared sensors to sense the line. *
// *
// ************************************************** ********************
// Hardware Notes: *
// This application has the PIC running at 4Mhz with internal MCLR *
// PWRT enabled on startup. Input sensors are set to interrupt on *
// change (IOC) when the line is sensed. *
// *
// ************************************************** ********************
// *
// Pin Assignments: *
// 2(GP5) - Input - Right Sensor - R_SEN *
// 3(GP4) - Input - Left Sensor - L_SEN *
// 7(GP0)- Output - Right Motor - L_MOT *
// 6(GP1)- Output - Left Motor - R_MOT *
// *
// ************************************************** ********************
// *
// Last Modification on: 8-23-2010 *
// *
// ************************************************** ********************

#include <htc.h>
// #include <pic.h>

// Oscillator at 4Mhz, Code unprotected, Power up Timer enabled,
// Watchdog timer disabled, Internal Oscillator w/pins set to IO and
// Brown out Disabled.
__CONFIG(OSC_4MHZ & UNPROTECT & MCLRDIS & PWRTEN & WDTDIS & INTIO & BORDIS);

// Sensor PORT assignments
#define R_SEN GP5
#define L_SEN GP4

// Output PORT assignments;
#define R_MOT GP0
#define L_MOT GP1

// TEMP VARIABLES
bit RTEMP, LTEMP;
unsigned int LO_SPD, NO_SPD, HI_SPD;
unsigned int LCNT, RCNT, count;
unsigned int R_SPD, L_SPD;

void strun(void) // Start run routine
{
R_SPD = NO_SPD; // Right and
L_SPD = NO_SPD; // Left speed to normal
RTEMP = R_SEN; // Set temporary left
LTEMP = L_SEN; // and right variables
R_MOT = 0x01; // Start your motors
L_MOT = 0x01;
while (1) // Main loop for run
{
count = 0x3400; // Total motor time period
LCNT = L_SPD; // Temporary register for LSpeed
RCNT = R_SPD; // Temporary register for RSpeed
R_MOT = 0x01; // Turn the motors back on
L_MOT = 0x01; // for the next on time duration
while (count--)
{
if (count == LCNT)
{ L_MOT = 0x00;}; // Left motor times out
if (count == RCNT)
{ R_MOT = 0x00;}; // Right motor times out

}
}
}

void main(void)
{
/* setup stuff */
OPTION = 0x00;
WPU = 0x30;
ANSEL = 0x00;
GPIO = 0x00;
TRISIO = 0x30;
IOC = 0x30; // Interrupt set for L/R sensor inputs
GPIE = 0x01;
GPIF = 0x00;
// Speed settings, total duration is 0x3400 approximately 200 milliseconds
// HI_SPD = 0x0001; // High speed
NO_SPD = 0x0010; // Normal speed
LO_SPD = 0x1A00; // Low speed
ei();
strun(); //
}


static void interrupt
isr(void) // Here be interrupt function
{

if (!R_SEN == RTEMP)// Did right sensor (GP5) change?
{
if (R_SEN) // Did right sensor see the line?
{
R_SPD = LO_SPD; // If so, set speed to low
}
else {R_SPD = NO_SPD; // Otherwise set speed to normal
}
RTEMP = R_SEN;
};
if(!L_SEN == LTEMP) // Did the left sensor (GP4) change?
{
if (L_SEN) // Did left sensor see the line?
{
L_SPD = LO_SPD; // If so, set speed to low.
if (R_SPD == LO_SPD) // Check if both see the line
{ R_MOT = 0x00; // then stop all motors
L_MOT = 0x00; // You are done!!
for(;;); // End of Program run
} // If finished, stop motors.
}
else {L_SPD = NO_SPD; // If not, set speed to normal
}
LTEMP = L_SEN;
};
GPIF = 0x00; // interrupt flag cleared
}

It does seem to change the speed of the motors when the appropriate photo sensor sees the line. The problem I have so far, is that the motors don't seem to have enough torque. :confused:

I may have to try different motors and see how that works out.