hay.
I want to make a robot that will follow a line and count junctions. So, I got 3 IR sensors. 2 Of them are used to follow the internal side of a line. The third is placed far from the other 2 sensors and it counts the junctions.
Here are my problems:
- when the left line following sensor is out of the line (then the robot should turn a little to the right) and at the same time the junction counting sensor is at a junction, the robot turns fast and sharply to the right (almost 180 deg).
- As you will see in the code, there is a delay function when the sensor counts the junctions. I think that that delay is interrupting with the line following routine and makes it slower when the orangutan counts the junction and following the line at the same time.
The code uses the orangutan lib.
//include the hedear files we will need
#include "device.h"
#include <avr/io.h>
#include <util/delay.h>
#include "pwm.h"
#include "lcd.h"
#include "buzzer.h"
#include "servo.h"
// Past here the library has been loaded, and the rest is up to you.
// Delay for N seconds
void delay_sec(uint8_t sec)
{
unsigned int cycles;
// Delay 25ms at a time (38.4ms is the most we can delay with a
// 20MHz processor, unfortunately. See the delay.h include file
// for more info.)
//BMK delay_sec
for(cycles = 0; cycles < (sec * 8); cycles ++)
{
_delay_ms(25);
}
}
void sensors_init(void)
//this function defines and activates the sensors. there are 3 IR sensors and 3 IR LEDs
//I allso added a pullup resistor for each sensor.
{ //define 2 I/O pins for the leds
DDRC |= (1<<PC1) | (1<<PC2);
//define IR sensors as input
DDRC &=~ (1<<PC5) | (1<<PC4) | (1<<PC0);
//make pullup resistors to the IR sensors
PORTC |= (1<<PC5) | (1<<PC4) | (1<<PC0);
}
//the main function
int main(void)
{
//initiate the lcd's functions
lcd_init();
//initiate the motor pwm control
pwm_init();
//we will not use the servo just yet
//servo_init();
//initiate the IR sensors
sensors_init();
//variables that we will use to set the robot location
unsigned int x=0, xORy=1;
//xORy is a boolean, 1 = robot on x, 0 = robot on y
//set the crusor on the LCD to the first charectre in the first line
lcd_gotoxy(0,0);
//write X= on the lcd. this will let us know what is the current robot location on
//the X axell
lcd_string("X=");
//set the crusor of the LCD to the first charectre at the second line
lcd_gotoxy(0,1);
//same as the X but with Y.
lcd_string("Y=");
//endless loop
for(;;)
{
//positioning rutine
//X
//is the robot moving on X or on Y?
//BMK moving on x
if (xORy == 1){
//the robot will count 14 lines on the X
if (x < 14)
{
//BMK count x
//the sensor that count the lines
if ((PINC & (1<<PC5)) > 0)
{
x = x + 1;
lcd_gotoxy(4,0);
lcd_int(x);
delay_sec(1);
}
//this ELSE belonges to the first IF in this rutine. it means that
//the robot finished counting 4 lines, and now he has to stop
//and turn to Y.
}
if (x < 14){
//line following rutine
//for now we will use only 1 IR sensor to track the line. the
//robot will allways go to the right and whem he gets out
//of the line he will go back to the line by going left.
//so, is the sensor on the line IS on the line? or is it off of the line?
//BMK line traking rutine
pwm_a(50);
pwm_b(50);
if ((PINC & (1<<PC0)) == 0){
//left sensor
pwm_a(50);
pwm_b(0);
PORTC |= (1<<PC2);
}
else
{
PORTC &=~ (1<<PC2);
}
if ((PINC & (1<<PC4)) == 0){
//right sensor
pwm_a(0);
pwm_b(50);
PORTC |= (1<<PC1);
}
else
{
PORTC &=~ (1<<PC1);
}
}
else
{
pwm_a(0);
pwm_b(0);
}
}
else{
pwm_a(0);
pwm_b(0);
}
}
}
Arbel