Index: lineFollow.c
===================================================================
--- lineFollow.c	(revision 1930)
+++ lineFollow.c	(revision 1931)
@@ -1,5 +1,6 @@
 /**
  * @file lineFollow.c
+ * @defgroup lineFollwing Line Following
  *
  * Takes care of following a line. Running this program is done by calling the
  * init() function and then the lineFollow(speed) command.  However, direct use
@@ -6,12 +7,13 @@
  * of this class is discouraged as its behavior is used by lineDrive.c, which
  * extends this class to provide behavior functionality.
  *
- * @author Dan Jacobs
+ * @author Dan Jacobs and the Colony Project
  * @date 11-1-2010
  */
 
 #include "lineFollow.h"
 
+//! The number of bits expected in a barcode
 #define CODESIZE 5 
 
 int countHi = 0;
@@ -21,14 +23,15 @@
 int barCodePosition=0;
 
 int turnDistance=0;
+//! Counts the number of full line readings before we determine an intersection
 int intersectionFilter=0;
 int disableBarCode=0;
 
+//! Keeps track of where the encoder of one motor started, for use in turns.
+int encoderStart = -1;
+int encoderReset = 0;   // 0 if encoderStart has no value set
 
-/** 
- * Initializes line following. Must be called before other line-following
- * behavior will work.
- */
+
 void lineFollow_init()
 {
 	analog_init(0);
@@ -54,7 +57,7 @@
 	//not on line
 	if(position == NOLINE)
 	{
-		if(lost++>20)
+		if(lost++ > 20)
 		{
 			orb2_set_color(GREEN);
 			motors_off();
@@ -63,7 +66,7 @@
 	}
 	else if(position == FULL_LINE)
 	{
-		if(intersectionFilter++>4)
+		if(intersectionFilter++ > 4)
 		{
 			orb2_set_color(RED);
 			barCodePosition=0;
@@ -153,11 +156,11 @@
 /**
  * Turns left at a cross of two lines.  Assumes that we are at lines in a cross
  * pattern, and turns until it sets straight on the new line.
- * @return 1 if the turn was executed successfully, 0 otherwise.
+ * @return 0 if turn finishes otherwise return 1
  */
 int turnLeft()
 {
-        motor_l_set(BACKWARD, 200);
+        /*motor_l_set(BACKWARD, 200);
         motor_r_set(FORWARD, 200);
         int colors[5];
         updateLine(colors);
@@ -171,6 +174,32 @@
                          return 0;
                 }
         }
+        return 1;*/
+
+        motor_l_set(BACKWARD,200);
+        motor_r_set(FORWARD,200);
+        if(!encoderReset)
+        {
+            encoderStart = encoder_get_x(RIGHT);
+            encoderReset = 1;
+        }
+
+        if(encoder_get_x(RIGHT) < encoderStart)
+        {
+            encoderStart = 0;
+            // Temporary: display an "error message" in case of overflow.
+            // Using this for debugging, take it out soon!
+            motor_l_set(FORWARD,0);
+            motor_r_set(FORWARD,0);
+            orb_set_color(WHITE);
+            delay_ms(2000);
+        }
+
+        if(encoder_get_x(RIGHT) - encoderStart > 300)
+        {
+            encoderReset = 0;
+            return 0;
+        }
         return 1;
 }
 
@@ -179,7 +208,7 @@
 /**
  * Turns right at a cross of two lines.  Assumes that we are at lines in a cross
  * pattern, and turns until it sets straight on the new line.
- * @return 1 if the turn was executed successfully, 0 otherwise.
+ * @return 0 if the turn finishes otherwise return 1
  */
 int turnRight()
 {
@@ -202,13 +231,11 @@
 
 
 
-
 int getBarCode()
 {
 	if(barCodePosition!=CODESIZE) return NOBARCODE ;
 	int temp = 0;
-	int i;
-	for(i=0; i<CODESIZE; i++)
+	for(int i=0; i<CODESIZE; i++)
 		temp += (barCode[i] << i);
 	barCodePosition = 0;
 	return temp;
@@ -240,15 +267,14 @@
 		return NOLINE;	
 	if(count==5)
 		return FULL_LINE;
-	return (wsum/count)-4;
+	return (wsum/count)-4; // Subtract 4 to center the index around the center.
 }
 
 
 void updateBarCode()
 {
+	//! Note: currently only uses one of the barcode sensors.
 
-	//NOTE: currently only uses one of the barcode sensors.
-
 	//maps the sensors to the analog input ports
 	int ports[2] = {8,1};
 	int current[2];
@@ -284,9 +310,7 @@
 }
 
 
-//! A simple function to return the minimum of two integers.
 int min(int x, int y){return x>y ? y : x;}
-//! A simple function to return the maximum of two integers.
 int max(int x, int y){return x<y ? y : x;}
 
 void motorLeft(int speed){
