Thursday, December 23, 2010

The Project - Location Recognition

A second part of this project I worked on a few days ago and a bit today. I'm planning to put down a long tape line around parts of the house. The robot will follow that line using one light sensor. Also, I'm planning to put in smaller lines as markers at four points next to the first line. The first point will have one little line, the second two, the third three and the fourth four. The robot will hopefully use its second light sensor to count the small lines and find out where it is.

To do this, I have the robot basically check to see the light value of its second light sensor is near the already saved "on line" variable. When it sees a line, it waits until it does not see the line, adds 1 to a variable called "line_count" and then moves on. It then runs 600 checks for more lines, waiting one thousandth of a second between each line. This leads to 6 seconds all together. If it sees a line, waits until it does not see the line and adds 1 to line_count. If it does not see a line, then it waits a thousandth of a second and tries again. At the end of the loop, it saves sets another variable "location" to the same value as the line_count variable. Here is the code I used:

task get_location ()
{
line_count=0;
while (true)
{
if ((Sensor(S2) < (online + 20)) && (Sensor(S2) > (online - 20)))
{
until (Sensor(S2) > (online + 30))
{
}
PlayTone(600, 200);
line_count=0;
line_count++;
repeat (600)
{
if ((Sensor(S2) < (online + 20)) && (Sensor(S2) > (online - 20)))
{
until (Sensor(S2) > (online + 30))
{
}
PlayTone(600, 200);
line_count++;
Wait(10);
}
else
{
Wait(10);
}
}
}
location=line_count;
}
}

It translates the location variable to readable locations on the NXT screen through this piece of code running in the main task:

// display robot location
TextOut(0, LCD_LINE4, "Location");
if (location == 0)
{
TextOut(0, LCD_LINE5, "Unknown");
}
if (location == 1)
{
TextOut(0, LCD_LINE5, "Kitchen");
}
if (location == 2)
{
TextOut(0, LCD_LINE5, "To Entryway");
}
if (location == 3)
{
TextOut(0, LCD_LINE5, "Entryway");
}
if (location == 4)
{
TextOut(0, LCD_LINE5, "To Kitchen");
}

I'm planning to add a fifth instance so that if it counts more than 4 lines, it puts out a line of text saying "error."

This project may be nearing completion (depending on what else I decide to implement). Stayed tuned.