commit beea48aaab7b027978a3703b75fe90f29e6ddc2f
Author: Tom Mullins <tmullins@andrew.cmu.edu>
Date:   Sun Apr 15 14:15:06 2012 -0400

    Tested forklift code and fixed some things (a few weeks ago)

diff --git a/forklift/code/Makefile b/forklift/code/Makefile
index 0b7da2d..e159564 100644
--- a/forklift/code/Makefile
+++ b/forklift/code/Makefile
@@ -1,14 +1,17 @@
+SRC = forklift.c analog.c twi.c motor.c
+HDR = analog.h twi.h motor.h
+
 forklift: forklift.hex
 	@
 
 %.hex: %.elf
 	avr-objcopy -j .text -j .data -O ihex $< $@
 
-forklift.elf: forklift.c analog.c analog.h twi.c twi.h
-	avr-gcc -mmcu=atmega168 -DF_CPU=8000000L forklift.c analog.c twi.c -o forklift.elf -Os
+forklift.elf: $(SRC) $(HDR)
+	avr-gcc -mmcu=atmega168 -DF_CPU=8000000L $(SRC) -o forklift.elf -Os
 
 download: forklift.hex
-	avrdude -c avrispmkII -p m328p forklift.hex
+	avrdude -c avrispmkII -p m128 -P usb -B 1 -U flash:w:forklift.hex
 
 clean:
 	rm -f forklift.elf forklift.hex
diff --git a/forklift/code/analog.c b/forklift/code/analog.c
index 87687da..c959bec 100644
--- a/forklift/code/analog.c
+++ b/forklift/code/analog.c
@@ -3,10 +3,6 @@
 
 int line_threshold = 150;
 
-int line_read(int which);
-void line_update(int* values);
-int line_locate(int* values);
-
 void analog_init(void)
 {
   // ADMUX register
@@ -58,14 +54,14 @@ int line_read(int which)
   return analog_read(ADC_LINE);
 }
 
-void line_update(int* values)
+void line_update(char* values)
 {
   int i;
   for(i = 0; i<5; i++)
-    values[i] = line_read(i) < line_threshold ? LWHITE : LBLACK;
+    values[i] = line_read(i) < line_threshold ? LBLACK : LWHITE;
 }
 
-int line_locate(int* values)
+int line_locate(char* values)
 {
   int i;
   int wsum = 0;
@@ -85,7 +81,7 @@ int line_locate(int* values)
 
 int line_read_pos(void)
 {
-  int values[5];
+  char values[5];
   line_update(values);
   return line_locate(values);
 }
diff --git a/forklift/code/analog.h b/forklift/code/analog.h
index c1744c2..5dcc286 100644
--- a/forklift/code/analog.h
+++ b/forklift/code/analog.h
@@ -16,7 +16,11 @@
 #define ADC_LINE   1
 
 void analog_init(void);
-int analog_read(int which);
+int analog_read(int which);
+
+void line_update(char* values);
+int line_locate(char* values);
+int line_read(int which);
 int line_read_pos(void);
 void line_set_threshold_high(uint8_t threshhold);
 void line_set_threshold_low(uint8_t threshhold);
diff --git a/forklift/code/forklift.c b/forklift/code/forklift.c
index 039159e..c2ff5d3 100644
--- a/forklift/code/forklift.c
+++ b/forklift/code/forklift.c
@@ -27,9 +27,11 @@
 #define FORKLIFT_HEIGHT_SETPOINT   3 // r/w
 #define FORKLIFT_LINE_POS          4
 #define FORKLIFT_LINE_THRESH_HIGH  5 // r/w
-#define FORKLIFT_LINE_THRESH_LOW   6 // r/w
+#define FORKLIFT_LINE_THRESH_LOW   6 // r/w
+#define FORKLIFT_LINE_VALS_START   7
+#define FORKLIFT_LINE_VALS_END    12 // non-inclusive
 
-#define FORKLIFT_DATA_LEN          7
+#define FORKLIFT_DATA_LEN         12
 
 
 #define PROP(X,Y,Z)                (((X)*(Y))/(Z))
@@ -42,7 +44,12 @@ uint8_t internal_data[] = {
   0,
   0,
   0,
-  150
+  150, // default line threshold
+  0,
+  0,
+  0,
+  0,
+  0
 };
 int error;
 int i_term;
@@ -84,15 +91,8 @@ void slave_tx(void)
   internal_index++;
   if (internal_index >= FORKLIFT_DATA_LEN)
     internal_index = 0;
-}
-
-void update_height(void)
-//Call to update the forklift height point. Should be called each loop
-{
-	internal_data[FORKLIFT_HEIGHT] = analog_read(ADC_HEIGHT);
 }
 
-
 int clamp(int min, int max, int val)
 {
 	if(val>max) return max;
@@ -105,11 +105,12 @@ SIGNAL(TIMER0_OVF_vect)
 	error = (int)internal_data[FORKLIFT_HEIGHT]
 						-(int)internal_data[FORKLIFT_HEIGHT_SETPOINT];
 	i_term = i_term +  error/10;
-	set_motor(clamp(-127,127, i_term/10 + (error*2)/5));
+	//set_motor(clamp(-127,127, i_term/10 + (error*2)/5));
 }
 
 int main()
 {
+  int i;
   error = 0;
   i_term = 0;
   init_int0();
@@ -122,12 +123,10 @@ int main()
   motor_init();
   while (1)
   {
-  	/*error = PROP((int)internal_data[FORKLIFT_HEIGHT]
-						-(int)internal_data[FORKLIFT_HEIGHT_SETPOINT],2,5);
-		set_motor(clamp(-127,127,error));*/
-    // do motor stuff
-	internal_data[FORKLIFT_LINE_POS] = (analog_read(ADC_LINE)>>2);//line_read_pos();
-    internal_data[FORKLIFT_HEIGHT] = (analog_read(ADC_HEIGHT)>>2);
+    for (i = 0; i < 5; i++)
+  	  internal_data[i + FORKLIFT_LINE_VALS_START] = line_read(i) >> 2;
+	internal_data[FORKLIFT_LINE_POS] = line_read_pos();
+    internal_data[FORKLIFT_HEIGHT] = analog_read(ADC_HEIGHT) >> 2;
   }
   return 0;
 }
diff --git a/forklift/code/twi.c b/forklift/code/twi.c
index 1923f56..ad6a0c9 100644
--- a/forklift/code/twi.c
+++ b/forklift/code/twi.c
@@ -464,5 +464,4 @@ SIGNAL(TWI_vect)
       twi_stop();
       break;
   }
-}
-
+}
