commit 31f4a0323d38938bb40b2da834fa73fc15003faf
Author: Tom Mullins <tmullins@andrew.cmu.edu>
Date:   Fri Sep 7 15:01:16 2012 -0400

    Some fixes to BOM, which is now tested and it works

diff --git a/scout_avr/src/bom.cpp b/scout_avr/src/bom.cpp
index 9f37046..e43dc5b 100644
--- a/scout_avr/src/bom.cpp
+++ b/scout_avr/src/bom.cpp
@@ -35,9 +35,9 @@ static volatile char out_done;
 // rx global vars
 static uint8_t prev_bom_sig;
 static struct bom_rx_t {
-    uint8_t bits_rx;
+    uint8_t count;
     uint16_t bits;
-    int last_bit;
+    int last_time;
 
     uint8_t new_data;
     uint8_t address;
@@ -114,23 +114,22 @@ static void stop_data_signal() {
 }
 
 ISR(TIMER4_COMPA_vect) {
-  if (out_counter) {
-    if (out_high) {
-      stop_38kHz_signal();
-      if (out_msg & 1) {
+  if (out_high) {
+    stop_38kHz_signal();
+    if (out_counter) {
+      out_counter--;
+      if ((out_msg >> out_counter) & 1) {
         OCR4A = TIME_MICROS(1680, 64);
       } else {
         OCR4A = TIME_MICROS(680, 64);
       }
-      out_msg >>= 1;
-      out_counter--;
     } else {
-      start_38kHz_signal();
-      OCR4A = TIME_MICROS(320, 64);
+      stop_data_signal();
+      out_done = 1;
     }
   } else {
-    stop_data_signal();
-    out_done = 1;
+    start_38kHz_signal();
+    OCR4A = TIME_MICROS(320, 64);
   }
 }
 
@@ -154,7 +153,7 @@ void bom_send(char dir) {
     case BOM_RIGHT: out_pin_mask = _BV(BOM_EMIT2); break;
     case BOM_BACK:  out_pin_mask = _BV(BOM_EMIT3); break;
   }
-  out_counter = 17;
+  out_counter = 16;
   out_msg = sharp_msg_make(0x2A, bom_msg_make(robot_id, dir));
   out_done = 0;
   start_data_signal();
@@ -165,35 +164,40 @@ void bom_send(char dir) {
 
 static void recv_edge(char is_rising, struct bom_rx_t *rx) {
   if (is_rising) {
+    // TODO check 320us? or have 320us timeout on rising edge?
+  } else {
     // uses timer 1, assuming prescale 1/8
     // timer 1 is set up by range_init()
-    int now = TCNT1 * (F_CPU / 1000000) / 8;
-    int diff = now - rx->last_bit;
-    rx->last_bit = now;
-    rx->bits_rx++;
-    rx->bits <<= 1;
-    if (MIN_LOW_PW < diff && diff < MAX_LOW_PW) {
-      // 0 already in bits
-    } else if (MIN_HIGH_PW < diff && diff < MAX_HIGH_PW) {
-      // add 1 to bits
-      rx->bits |= 1;
-    } else {
-      // error, start from beginning
-      rx->bits_rx = 0;
-      rx->bits = 0;
-    }
-    if (rx->bits_rx == 16) {
-      // finished!
-      if ((rx->bits & 3) == 2) { // expansion and check bits
-        rx->data = (rx->bits >> 2) & 0xff;
-        rx->address = (rx->bits >> 10) & 0x1f;
-        rx->new_data = 1;
+    // this should be in units of microseconds
+    int now = TCNT1 * (F_CPU / 1000000 / 8);
+    
+    if (rx->count) {
+      int diff = now - rx->last_time;
+      rx->bits <<= 1;
+      if (MIN_LOW_PW < diff && diff < MAX_LOW_PW) {
+        // 0 already in bits
+      } else if (MIN_HIGH_PW < diff && diff < MAX_HIGH_PW) {
+        // add 1 to bits
+        rx->bits |= 1;
+      } else {
+        // error, start from beginning
+        rx->count = 0;
+        rx->bits = 0;
+      }
+      if (rx->count == 16) {
+        // finished!
+        //if ((rx->bits & 3) == 2) { // expansion and check bits
+          rx->data = (rx->bits >> 2) & 0xff;
+          rx->address = (rx->bits >> 10) & 0x1f;
+          rx->new_data = 1;
+        //}
+        rx->count = 0;
+        rx->bits = 0;
       }
-      rx->bits_rx = 0;
-      rx->bits = 0;
     }
-  } else {
-    // TODO check 320us? or have 320us timeout on rising edge?
+    
+    rx->count++;
+    rx->last_time = now;
   }
 }
 
diff --git a/scout_avr/src/bom.h b/scout_avr/src/bom.h
index 2233466..53a9eff 100644
--- a/scout_avr/src/bom.h
+++ b/scout_avr/src/bom.h
@@ -12,17 +12,17 @@ extern "C" {
 #define BOM_RIGHT 3
 
 // timing, in us, for read of valid bits
-#define MIN_LOW_PW 800
-#define MAX_LOW_PW 1200
-#define MIN_HIGH_PW 1800
-#define MAX_HIGH_PW 2200
+#define MIN_LOW_PW 600
+#define MAX_LOW_PW 1400
+#define MIN_HIGH_PW 1600
+#define MAX_HIGH_PW 2400
 
 // returned by bom_get if there is no new data since last call
 #define BOM_NO_DATA -1
 
 // i/o pins
 // if these are changed, remember to change bom_init and/or ISRs
-#define BOM_SIG PORTB
+#define BOM_SIG PINB
 #define BOM_SIG0 PB0
 #define BOM_SIG1 PB1
 #define BOM_SIG2 PB2
diff --git a/scout_avr/src/debug.h b/scout_avr/src/debug.h
new file mode 100644
index 0000000..1b51c26
--- /dev/null
+++ b/scout_avr/src/debug.h
@@ -0,0 +1,6 @@
+#ifndef _DEBUG_H
+#define _DEBUG_H
+
+void debug(const char *str);
+
+#endif
diff --git a/scout_avr/src/main.cpp b/scout_avr/src/main.cpp
index c22de51..2298610 100644
--- a/scout_avr/src/main.cpp
+++ b/scout_avr/src/main.cpp
@@ -5,6 +5,9 @@
 
 ros::Publisher *ptest_out;
 
+void debug(const char *str) {
+}
+
 void callback(const std_msgs::Int16& msg)
 {
   ptest_out->publish(&msg);
@@ -44,21 +47,32 @@ extern "C"
 #include "range.h"
 #include "bom.h"
 
+Atmega128rfa1 avr;
+
+void debug(const char *str) {
+  avr.puts(str);
+}
+
 int main()
 {
   char buf[20];
   int i;
+  char id = 0;
   unsigned long now, next = 0;
-  Atmega128rfa1 avr;
   avr.init();
   range_init();
   bom_init();
-  set_robot_id(0x2a);
+  avr.puts("Hello!\r\n");
   while (1)
   {
     now = avr.time();
     if (now > next) {
       next = now + 100;
+      id++;
+      if (id == 0x40) {
+        id = 0;
+      }
+      set_robot_id(id);
       /*utoa(range_get(0), buf, 10);
       avr.puts("Range: ");
       avr.puts(buf);
@@ -73,11 +87,14 @@ int main()
           itoa(i, buf, 10);
           avr.puts(buf);
           avr.puts(": ");
-          itoa(msg, buf, 10);
+          itoa(bom_msg_get_robot_id(msg), buf, 10);
+          avr.puts(buf);
+          avr.puts(" (");
+          itoa(bom_msg_get_dir(msg), buf, 10);
           avr.puts(buf);
+          avr.puts(")\r\n");
         }
       }
-      avr.puts("\r\n");
     }
   }
   return 0;
diff --git a/scout_avr/src/range.cpp b/scout_avr/src/range.cpp
index 9d56ebe..0a9e48b 100644
--- a/scout_avr/src/range.cpp
+++ b/scout_avr/src/range.cpp
@@ -33,6 +33,8 @@ struct range_t {
 static void on_edge(int which)
 {
   unsigned char int_high;
+  // TODO centralize timer 1
+  // TODO ensure this is in microseconds even for 16MHz
   unsigned int time = TCNT1;
   
   if (which)
