Index: trunk/code/projects/linefollowing/lineFollow.h
===================================================================
--- trunk/code/projects/linefollowing/lineFollow.h	(revision 1930)
+++ trunk/code/projects/linefollowing/lineFollow.h	(revision 1931)
@@ -1,93 +1,112 @@
 #include <dragonfly_lib.h>
 
- #ifndef _LINEFOLLOW_H_
- #define _LINEFOLLOW_H_
+#ifndef _LINEFOLLOW_H_
+#define _LINEFOLLOW_H_
 
 #define LWHITE			0
 #define LGREY			1
 #define LBLACK	 		2
 #define CENTER			3
-#define NOLINE			-42
 #define LINELOST		-1
 
 #define NOBARCODE 		-2
-#define INTERSECTION 		-25
-#define FULL_LINE 		-26
+#define INTERSECTION 	-25
 
+#define NOLINE			-50
+#define FULL_LINE 		-51
 
-
-/* 	lineFollow_init
-	Must call before lineFollow
-	Turns analog loop off
-*/ 
+/**
+ * @brief Initializes line following.
+ * 
+ * Must be called before line following will work.
+ * Turns the analog loop off.
+ */
 void lineFollow_init(void);
 
-/*	lineFollow
-	Must call lineFollow first
-	Must be called inside a loop
-*/
+/**	lineFollow
+ *	Must call lineFollow_init first
+ *	Must be called inside a loop
+ */
 int lineFollow(int speed);
 
-/*	turnLeft turnRight mergeLeft mergeRight
-	Must be called inside a loop
-	returns 0 when complete
-*/
+/**	turnLeft turnRight mergeLeft mergeRight
+ *	Must be called inside a loop
+ *	returns 0 when complete
+ */
 int turnLeft(void);
 int turnRight(void);
 int mergeLeft(void);
 int mergeRight(void);
 
-/*	updateLine
-	Reads in the analog values
-	Fills the given array with WHITE
-	or BLACK representing the line
-*/
+/**
+ * @brief Updates the values stored in the array to white or black based on
+ * current sensor readings.
+ *
+ * @param values The array of five integers to be updated. 
+ */
 void updateLine(int* values); 
 
-/*	lineLocate
-	Finds the location of the line
-	Outputs positive for right side
-	Negative for left, or NOLINE if a line is not found
-*/
+/**
+ * @brief Returns an index of the middle of the line based on line readings.
+ *
+ * Two special return values are possible:
+ *   NOLINE if none of the sensors holds a black value, and
+ *   FULL_LINE if all of the sensors see black.
+ *
+ * Otherwise, returns a value from -4 (farthest left) to 4 (farthest right), with
+ * 0 the line being centered in the middle.
+ *
+ * @param colors The array of 5 readings from the line sensor.  Must be either
+ *    LWHITE or LBLACK.
+ * @return Either a special value or an index from -4 to 4.
+ *
+ */
 int lineLocate(int* colors);
 
-/*	updatebarCode
-	Reads in and processes
-	bar code data
-*/
+/**	updatebarCode
+ *	Reads in and processes
+ *	bar code data
+ */
 void updateBarCode(void);
 
-/*	getBarCode
-	returns a bar code, if
-	available, otherwise NOBARCODE
-*/
+/**
+ * @brief Gets the completed value read by the barcode reader, or NOBARCODE.
+ *
+ * Returns a bar code if available (if at the end of a barcode) and resets the
+ * barcodePosition to 0. Otherwise, return NOBARCODE.  *
+ * @return The value of the barcode if a complete barcode, else NOBARCODE.
+ */
 int getBarCode(void);
 
-/*	min max
-	returns the minimum/maximum of two values
-*/
+
+//! A simple function to return the minimum of two integers.
 int min(int x, int y);
+//! A simple function to return the maximum of two integers.
 int max(int x, int y);
 
-/*	motorLeft
-	Commands the left motor
-	Cannot be used to stop
-	0-126 are backward
-	127-255 are forward
-*/
+/** @todo Alex: I hate these functions, but I'm keeping them so code will still work. But we should delete them sometime. */
+
+/**	motorLeft
+ *	Commands the left motor
+ *	Cannot be used to stop
+ *	0-126 are backward
+ *	127-255 are forward
+ */
 void motorLeft(int speed);
 
-/*	motorRight
-	Commands the right motor
-	Cannot be used to stop
-	0-126 are backward
-	127-255 are forward
-*/
+/**	motorRight
+ *	Commands the right motor
+ *	Cannot be used to stop
+ *	0-126 are backward
+ *	127-255 are forward
+ */
 void motorRight(int speed);
 
-/*	lost
-	Internal counter to detect if the line was lost
-*/
+/**	lost
+ *	Internal counter to detect if the line was lost
+ */
 int lost;
 
+int onLine(void);
+
 #endif
Index: trunk/code/projects/linefollowing/lineDrive.c
===================================================================
--- trunk/code/projects/linefollowing/lineDrive.c	(revision 1930)
+++ trunk/code/projects/linefollowing/lineDrive.c	(revision 1931)
@@ -146,16 +146,60 @@
 int turn(int type, int dir)
 {
 	if(stateLength!=0)return ERROR;
-	if(dir==IRIGHT){stateLength++; state[1]=IRIGHT; return NORMAL;}
-	if(dir==IUTURN){stateLength+=2;  state[1]=state[2]=ILEFT; return NORMAL;}
-	if(dir==ISTRAIGHT && type==SINGLE){stateLength++; state[1]=ISTRAIGHT; return NORMAL;}
-	if(dir==ISTRAIGHT){stateLength+=2; state[1]=state[2]=ISTRAIGHT; return NORMAL;}
-	//must be left turn
-	if(type==SINGLE){stateLength++; state[1]=ILEFT; return NORMAL;}
-	if(type==DOUBLE){stateLength+=3;state[1]=state[3]=ISTRAIGHT; state[2]=ILEFT; return NORMAL;}
-	if(type==ON_RAMP){stateLength+=2; state[1]=ILEFT; state[2]=ISTRAIGHT; return NORMAL;}
-	if(type==OFF_RAMP){stateLength+=2; state[1]=ISTRAIGHT; state[2]=ILEFT; return NORMAL;}
+	if(dir==IRIGHT)
+    {
+        stateLength++;
+        state[1]=IRIGHT;
+        return NORMAL;
+    }
+	if(dir==IUTURN)
+    {
+        stateLength+=2;
+        state[1]=state[2]=ILEFT;
+        return NORMAL;
+    }
+	if(dir==ISTRAIGHT && type==SINGLE)
+    {
+        stateLength++;
+        state[1]=ISTRAIGHT;
+        return NORMAL;
+    }
+	if(dir==ISTRAIGHT)
+    {
+        stateLength+=2;
+        state[1]=state[2]=ISTRAIGHT;
+        return NORMAL;
+    }
+	// At this point, must be left turn
+	if(type==SINGLE)
+    {
+        stateLength++;
+        state[1]=ILEFT;
+        return NORMAL;
+    }
+	if(type==DOUBLE_C || type==DOUBLE_T)
+    {
+        stateLength+=3;
+        state[1]=state[3]=ISTRAIGHT;
+        state[2]=ILEFT;
+        return NORMAL;
+    }
+	if(type==ON_RAMP)
+    {
+        stateLength+=2;
+        state[1]=ILEFT;
+        state[2]=ISTRAIGHT;
+        return NORMAL;
+    }
+	if(type==OFF_RAMP)
+    {
+        stateLength+=2;
+        state[1]=ISTRAIGHT;
+        state[2]=ILEFT;
+        return NORMAL;
+    }
 
 	//Should never get here
 	return ERROR;
 }
+
Index: trunk/code/projects/linefollowing/lineDrive.h
===================================================================
--- trunk/code/projects/linefollowing/lineDrive.h	(revision 1930)
+++ trunk/code/projects/linefollowing/lineDrive.h	(revision 1931)
@@ -3,11 +3,19 @@
 
 #include "lineFollow.h"
 
-#define DOUBLE		0
+/* Old definitions, delete as soon as possible
+#define DOUBLE   	0
 #define SINGLE		1
 #define ON_RAMP		2
 #define OFF_RAMP	3
+*/
 
+#define SINGLE      0
+#define ON_RAMP     1
+#define OFF_RAMP    2
+#define DOUBLE_C    3
+#define DOUBLE_T    4
+
 #define ISTRAIGHT	0
 #define ILEFT		1
 #define IRIGHT		2
Index: trunk/code/projects/linefollowing/lineFollow.c
===================================================================
--- trunk/code/projects/linefollowing/lineFollow.c	(revision 1930)
+++ trunk/code/projects/linefollowing/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){
