Index: trunk/code/projects/colonet/utilities/robot_slave/robot_slave.c
===================================================================
--- trunk/code/projects/colonet/utilities/robot_slave/robot_slave.c	(revision 422)
+++ trunk/code/projects/colonet/utilities/robot_slave/robot_slave.c	(revision 423)
@@ -8,6 +8,7 @@
 #include <dragonfly_lib.h>
 #include <colonet_dragonfly.h>
 #include <wireless.h>
+#include <wl_token_ring.h>
 #include <serial.h>
 #include <stdio.h>
 
@@ -20,14 +21,20 @@
   wl_init();
   colonet_init();
 
-  
-  //orb_set_color(YELLOW);
-  orb_set_color(GREEN);
-  usb_puts("test!\n");
+  wl_token_ring_register();
 
-  while(1) {
-    wl_do();
+  if (wl_token_ring_join() == 0) {
+    orb_set_color(GREEN);
+    //usb_puts("test!\n");
+
+    while(1) {
+      wl_do();
+    }
+  } else {
+    orb_set_color(RED);
   }
-  
+
+  while(1);
+
   return 0;
 }
Index: trunk/code/projects/colonet/utilities/robot_slave/Makefile
===================================================================
--- trunk/code/projects/colonet/utilities/robot_slave/Makefile	(revision 422)
+++ trunk/code/projects/colonet/utilities/robot_slave/Makefile	(revision 423)
@@ -14,8 +14,8 @@
 USE_WIRELESS = 1
 
 # com1 = serial port. Use lpt1 to connect to parallel port.
-#AVRDUDE_PORT = /dev/ttyUSB1
-AVRDUDE_PORT = /dev/ttyUSB0
+AVRDUDE_PORT = /dev/ttyUSB1
+#AVRDUDE_PORT = /dev/ttyUSB0
 
 #
 #
@@ -121,7 +121,7 @@
 CINCS = -I$(COLONYROOT)/code/lib/include/libdragonfly
 CINCS += -L$(COLONYROOT)/code/lib/bin
 ifdef USE_WIRELESS
-	CINCS += -I$(COLONYROOT)/code/lib/include/libwireless -I$(COLONYROOT)/code/projects/colonet/lib -I$(COLONYROOT)/code/projects/colonet/lib/colonet_dragonfly
+	CINCS += -I$(COLONYROOT)/code/projects/libwireless/lib -I$(COLONYROOT)/code/projects/colonet/lib -I$(COLONYROOT)/code/projects/colonet/lib/colonet_dragonfly
 endif
 
 #---------------- Compiler Options ----------------
Index: trunk/code/projects/libwireless/lib/wl_token_ring.c
===================================================================
--- trunk/code/projects/libwireless/lib/wl_token_ring.c	(revision 422)
+++ trunk/code/projects/libwireless/lib/wl_token_ring.c	(revision 423)
@@ -70,7 +70,7 @@
 static void wl_token_ring_cleanup(void);
 
 /*Helper Functions*/
-static void wl_token_pass_token(void);
+static int wl_token_pass_token(void);
 static int get_token_distance(int robot1, int robot2);
 static void wl_token_get_token(void);
 
@@ -131,6 +131,34 @@
 		wl_token_ring_cleanup};
 
 /**
+ * Causes the robot to join an existing token ring, or create one
+ * if no token ring exists. The token ring uses global and robot to robot
+ * packets, and does not rely on any PAN.
+ **/
+int wl_token_ring_join()
+{
+	WL_DEBUG_PRINT("Joining the token ring.\r\n");
+
+	ringState = JOINING;
+	joinDelay = DEATH_DELAY * 2;
+	if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, NULL, 0, 0) != 0) {
+		return -1;
+	}
+
+	return 0;
+}
+
+/**
+ * Causes the robot to leave the token ring. The robot stops
+ * alerting others of its location, but continues storing the
+ * locations of other robots.
+ **/
+void wl_token_ring_leave()
+{
+	ringState = LEAVING;
+}
+
+/**
  * Initialize the token ring packet group and register it with the
  * wireless library. The robot will not join a token ring.
  **/
@@ -186,7 +214,7 @@
 /**
  * Called to cleanup the token ring packet group.
  **/
-void wl_token_ring_cleanup()
+static void wl_token_ring_cleanup()
 {
 	sensor_matrix_destroy(sensorMatrix);
 }
@@ -212,8 +240,9 @@
 		}
 
 		// we may have been dropped from the ring when this is received
-		if (ringState == MEMBER)
+		if (ringState == MEMBER) {
 			wl_token_pass_token();
+		}
 	}
 
 	//we must start our own token ring, no one is responding to us
@@ -238,12 +267,17 @@
 		}
 	}
 
-	if (deathDelay >= 0)
+	if (deathDelay >= 0) {
 		deathDelay--;
-	if (joinDelay >= 0)
+	}
+
+	if (joinDelay >= 0) {
 		joinDelay--;
-	if (bom_on_count >= 0)
+	}
+
+	if (bom_on_count >= 0) {
 		bom_on_count++;
+	}
 }
 
 /**
@@ -296,33 +330,6 @@
 }
 
 /**
- * Causes the robot to join an existing token ring, or create one
- * if no token ring exists. The token ring uses global and robot to robot
- * packets, and does not rely on any PAN.
- **/
-int wl_token_ring_join()
-{
-	WL_DEBUG_PRINT("Joining the token ring.\r\n");
-	ringState = JOINING;
-	joinDelay = DEATH_DELAY * 2;
-	if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, NULL, 0, 0) != 0) {
-		return -1;
-	}
-
-	return 0;
-}
-
-/**
- * Causes the robot to leave the token ring. The robot stops
- * alerting others of its location, but continues storing the
- * locations of other robots.
- **/
-void wl_token_ring_leave()
-{
-	ringState = LEAVING;
-}
-
-/**
  * Returns the BOM reading robot source has for robot dest.
  *
  * @param source the robot that made the BOM reading
@@ -354,7 +361,111 @@
 	return wl_token_get_sensor_reading(wl_get_xbee_id(), dest);
 }
 
+
 /**
+ * Returns the number of robots in the token ring.
+ *
+ * @return the number of robots in the token ring
+ **/
+int wl_token_get_robots_in_ring(void)
+{
+	return sensor_matrix_get_joined(sensorMatrix);
+}
+
+/**
+ * Returns true if the specified robot is in the token ring, false
+ * otherwise.
+ *
+ * @param robot the robot to check for whether it is in the token ring
+ * @return nonzero if the robot is in the token ring, zero otherwise
+ **/
+int wl_token_is_robot_in_ring(int robot)
+{
+	return sensor_matrix_get_in_ring(sensorMatrix, robot);
+}
+
+/**
+ * Begins iterating through the robots in the token ring.
+ *
+ * @see wl_token_iterator_has_next, wl_token_iterator_next
+ **/
+void wl_token_iterator_begin(void)
+{
+	int i = 0;
+
+	while (!sensor_matrix_get_in_ring(sensorMatrix, i) && i < sensor_matrix_get_size(sensorMatrix)) {
+		i++;
+	}
+
+	if (i == sensor_matrix_get_size(sensorMatrix)) {
+		i = -1;
+	}
+
+	iteratorCount = i;
+}
+
+/**
+ * Returns true if there are more robots in the token ring
+ * to iterate through, and false otherwise.
+ *
+ * @return nonzero if there are more robots to iterate through,
+ * zero otherwise
+ *
+ * @see wl_token_iterator_begin, wl_token_iterator_next
+ **/
+int wl_token_iterator_has_next(void)
+{
+	return iteratorCount != -1;
+}
+
+/**
+ * Returns the next robot ID in the token ring.
+ *
+ * @return the next robot ID in the token ring, or -1 if none exists
+ *
+ * @see wl_token_iterator_begin, wl_token_iterator_has_next
+ **/
+int wl_token_iterator_next(void)
+{
+	int result = iteratorCount;
+	if (result < 0) {
+		return result;
+	}
+
+	iteratorCount++;
+	while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount)
+		&& iteratorCount < sensor_matrix_get_size(sensorMatrix)) {
+		iteratorCount++;
+	}
+
+	if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) {
+		iteratorCount = -1;
+	}
+
+	return result;
+}
+
+/**
+ * Returns the number of robots currently in the token ring.
+ *
+ * @return the number of robots in the token ring
+ **/
+int wl_token_get_num_robots(void)
+{
+	return sensor_matrix_get_joined(sensorMatrix);
+}
+
+/**
+ * Returns the number of robots in the sensor matrix.
+ *
+ * @return the number of robots in the sensor matrix
+ **/
+int wl_token_get_matrix_size(void)
+{
+	return sensor_matrix_get_size(sensorMatrix);
+}
+
+/**
  * This method is called when we receive a token pass packet.
  * @param source is the robot it came from
  * @param nextRobot is the robot the token was passed to
@@ -389,8 +500,9 @@
 	j = 0;
 	for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++)
 	{
-		if (i == source)
+		if (i == source) {
 			continue;
+		}
 
 		//set the sensor information we receive
 		if (j < sensorDataLength / 2 && sensorData[2 * j] == i)
@@ -446,8 +558,9 @@
 	deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY;
 
 	//we have the token
-	if (wl_token_next_robot == wl_get_xbee_id())
+	if (wl_token_next_robot == wl_get_xbee_id()) {
 		wl_token_get_token();
+	}
 }
 
 /**
@@ -459,7 +572,7 @@
  * @return the number of passes before the token is expected
  * to reach robot2 from robot1
  **/
-int get_token_distance(int robot1, int robot2)
+static int get_token_distance(int robot1, int robot2)
 {
 	int curr = robot1 + 1;
 	int count = 1;
@@ -479,7 +592,7 @@
 /**
  * Passes the token to the next robot in the token ring.
  **/
-void wl_token_pass_token()
+static int wl_token_pass_token()
 {
 	char nextRobot;
 	int i = wl_get_xbee_id() + 1;
@@ -487,13 +600,16 @@
 	{
 		while (1)
 		{
-			if (i == sensor_matrix_get_size(sensorMatrix))
+			if (i == sensor_matrix_get_size(sensorMatrix)) {
 				i = 0;
+			}
+
 			if (sensor_matrix_get_in_ring(sensorMatrix, i))
 			{
 				nextRobot = (char)i;
 				break;
 			}
+
 			i++;
 		}
 	}
@@ -513,12 +629,13 @@
 	{
 		WL_DEBUG_PRINT_INT(packetSize);
 		WL_DEBUG_PRINT("Out of memory - pass token.\r\n");
-		return;
+		free(buf);
+		return -1;
 	}
 	buf[0] = nextRobot;
 
 	int j = 0;
-	for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++)
+	for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) {
 		if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id())
 		{
 			buf[2*j + 1] = i;
@@ -525,16 +642,21 @@
 			buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i);
 			j++;
 		}
+	}
 
 	WL_DEBUG_PRINT("Passing the token to robot ");
 	WL_DEBUG_PRINT_INT(buf[0]);
 	WL_DEBUG_PRINT(".\r\n");
-	wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS,
-		buf, packetSize, 3);
+	if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, buf, packetSize, 3) != 0) {
+		free(buf);
+		return -1;
+	}
 
 	wl_token_next_robot = nextRobot;
 	deathDelay = DEATH_DELAY;
 	free(buf);
+
+	return 0;
 }
 
 /**
@@ -544,7 +666,7 @@
  *
  * @param source the robot whose BOM is on
  **/
-void wl_token_bom_on_receive(int source)
+static void wl_token_bom_on_receive(int source)
 {
 	WL_DEBUG_PRINT("Robot ");
 	WL_DEBUG_PRINT_INT(source);
@@ -563,13 +685,12 @@
  *
  * If there is a pending request for the token, this is processed first.
  **/
-void wl_token_get_token()
+static void wl_token_get_token()
 {
 	WL_DEBUG_PRINT("We have the token.\r\n");
 	if (ringState == ACCEPTED)
 	{
-		sensor_matrix_set_in_ring(sensorMatrix,
-			wl_get_xbee_id(), 1);
+		sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
 		WL_DEBUG_PRINT("Now a member of the token ring.\r\n");
 		ringState = MEMBER;
 		joinDelay = -1;
@@ -586,8 +707,7 @@
 	}
 
 	WL_DEBUG_PRINT("Our BOM has been flashed.\r\n");
-	wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON,
-		NULL, 0, 0);
+	wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, NULL, 0, 0);
 
 	bom_on_function();
 	#ifdef ROBOT
@@ -612,7 +732,7 @@
  *
  * @param source the robot who requested to join
  **/
-void wl_token_join_receive(int source)
+static void wl_token_join_receive(int source)
 {
 	WL_DEBUG_PRINT("Received joining request from robot ");
 	WL_DEBUG_PRINT_INT(source);
@@ -664,7 +784,7 @@
  *
  * @param source the robot who accepted us
  **/
-void wl_token_join_accept_receive(int source)
+static void wl_token_join_accept_receive(int source)
 {
 	WL_DEBUG_PRINT("Accepted into the token ring by robot ");
 	WL_DEBUG_PRINT_INT(source);
@@ -676,107 +796,3 @@
 	//add ourselves to the token ring
 	sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
 }
-
-/**
- * Returns the number of robots in the token ring.
- *
- * @return the number of robots in the token ring
- **/
-int wl_token_get_robots_in_ring(void)
-{
-	return sensor_matrix_get_joined(sensorMatrix);
-}
-
-/**
- * Returns true if the specified robot is in the token ring, false
- * otherwise.
- *
- * @param robot the robot to check for whether it is in the token ring
- * @return nonzero if the robot is in the token ring, zero otherwise
- **/
-int wl_token_is_robot_in_ring(int robot)
-{
-	return sensor_matrix_get_in_ring(sensorMatrix, robot);
-}
-
-/**
- * Begins iterating through the robots in the token ring.
- *
- * @see wl_token_iterator_has_next, wl_token_iterator_next
- **/
-void wl_token_iterator_begin(void)
-{
-	int i = 0;
-
-	while (!sensor_matrix_get_in_ring(sensorMatrix, i) && i < sensor_matrix_get_size(sensorMatrix)) {
-		i++;
-	}
-
-	if (i == sensor_matrix_get_size(sensorMatrix)) {
-		i = -1;
-	}
-
-	iteratorCount = i;
-}
-
-/**
- * Returns true if there are more robots in the token ring
- * to iterate through, and false otherwise.
- *
- * @return nonzero if there are more robots to iterate through,
- * zero otherwise
- *
- * @see wl_token_iterator_begin, wl_token_iterator_next
- **/
-int wl_token_iterator_has_next(void)
-{
-	return iteratorCount != -1;
-}
-
-/**
- * Returns the next robot ID in the token ring.
- *
- * @return the next robot ID in the token ring, or -1 if none exists
- *
- * @see wl_token_iterator_begin, wl_token_iterator_has_next
- **/
-int wl_token_iterator_next(void)
-{
-	int result = iteratorCount;
-	if (result < 0) {
-		return result;
-	}
-
-	iteratorCount++;
-	while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount)
-		&& iteratorCount < sensor_matrix_get_size(sensorMatrix)) {
-		iteratorCount++;
-	}
-
-	if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) {
-		iteratorCount = -1;
-	}
-
-	return result;
-}
-
-/**
- * Returns the number of robots currently in the token ring.
- *
- * @return the number of robots in the token ring
- **/
-int wl_token_get_num_robots(void)
-{
-	return sensor_matrix_get_joined(sensorMatrix);
-}
-
-/**
- * Returns the number of robots in the sensor matrix.
- *
- * @return the number of robots in the sensor matrix
- **/
-int wl_token_get_matrix_size(void)
-{
-	return sensor_matrix_get_size(sensorMatrix);
-}
-
Index: trunk/code/projects/libwireless/lib/wl_token_ring.h
===================================================================
--- trunk/code/projects/libwireless/lib/wl_token_ring.h	(revision 422)
+++ trunk/code/projects/libwireless/lib/wl_token_ring.h	(revision 423)
@@ -51,8 +51,8 @@
 /**@brief Unregister the token ring group with the wirelss library.**/
 void wl_token_ring_unregister(void);
 /**@brief Set the functions called to turn the bom on and off.**/
-void wl_token_ring_set_bom_functions(void (*on_function) (void), 
-		void (*off_function) (void), int (*max_bom_function) (void));
+void wl_token_ring_set_bom_functions(void (*on_function) (void), void (*off_function) (void),
+  int (*max_bom_function) (void));
 
 /**@brief Join the token ring **/
 int wl_token_ring_join(void);
