From 432eb1ee25d0d459d0120daa9bc59f8828d13d63 Mon Sep 17 00:00:00 2001
From: Sergi Hernandez Juan <shernand@iri.upc.edu>
Date: Fri, 29 Apr 2016 14:18:17 +0200
Subject: [PATCH] Solved a bug in the gyro calibration because the ADC's were
 not updated while averaging the gyro data.

---
 controllers/src/examples/Makefile |  2 +-
 controllers/src/examples/main.c   |  4 ++--
 motion/src/balance.c              | 12 +++++++++++-
 motion/src/examples/Makefile      |  2 +-
 4 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/controllers/src/examples/Makefile b/controllers/src/examples/Makefile
index db83592..7c70888 100644
--- a/controllers/src/examples/Makefile
+++ b/controllers/src/examples/Makefile
@@ -36,7 +36,7 @@ $(PROJECT).elf: $(OBJS)
 	$(CC) -c $(CFLAGS) $(INCLUDE_DIRS) -o $@ $<
 
 download: $(MAIN_OUT_HEX)
-	fw_downloader -d /dev/ttyUSB0 -f ./src/examples/comm_ex.hex -p new
+	fw_downloader -d /dev/ttyUSB0 -f $(PROJECT).hex -p new
 
 clean:
 	-rm $(PROJECT).*
diff --git a/controllers/src/examples/main.c b/controllers/src/examples/main.c
index fa69239..a1d6216 100755
--- a/controllers/src/examples/main.c
+++ b/controllers/src/examples/main.c
@@ -29,8 +29,8 @@ void user_loop(void)
 {
   if(user_time_is_period_done())
   {
-    printf("Gyro X: %d\n",get_adc_avg_channel(ADC_PORT_3));
-    printf("Gyro Y: %d\n",get_adc_avg_channel(ADC_PORT_4));
+    printf("Gyro X: %d\n",get_adc_channel(ADC_PORT_3));
+    printf("Gyro Y: %d\n",get_adc_channel(ADC_PORT_4));
   }
   if(is_button_pressed(BTN_START))
     turn_led_on(LED_AUX);
diff --git a/motion/src/balance.c b/motion/src/balance.c
index 9caf2a9..dd52906 100644
--- a/motion/src/balance.c
+++ b/motion/src/balance.c
@@ -6,11 +6,15 @@
 #include "balance.h"
 #include "adc.h"
 #include "buzzer.h"
+#include "user_time.h"
 
 // external functions
 extern void buzzer_start_alarm(note_t note,uint16_t on_time_100ms,uint16_t off_time_100ms);
 extern void buzzer_stop_alarm(void);
 
+extern void adc_loop(void);
+extern void user_time_loop(void);
+
 // private variables
 uint16_t balance_x_gyro_center;
 uint16_t balance_y_gyro_center;
@@ -98,14 +102,20 @@ uint8_t balance_calibrate_gyro(void)
 
   balance_x_gyro_center=0;
   balance_y_gyro_center=0;
+  user_time_set_period(50);
   for(i=0;i<BALANCE_GYRO_CAL_NUM_SAMPLES;i++)
   {
-    _delay_ms(50);
+    while(!user_time_is_period_done())
+    {
+      user_time_loop();
+      adc_loop();
+    }
     x_gyro_values[i]=get_adc_channel(BALANCE_GYRO_X_CHANNEL); 
     y_gyro_values[i]=get_adc_channel(BALANCE_GYRO_Y_CHANNEL); 
     x_gyro_average+=x_gyro_values[i];
     y_gyro_average+=y_gyro_values[i];
   }  
+  user_time_stop();
   x_gyro_average/=(float)BALANCE_GYRO_CAL_NUM_SAMPLES;
   y_gyro_average/=(float)BALANCE_GYRO_CAL_NUM_SAMPLES;
   // compute the standard deviation
diff --git a/motion/src/examples/Makefile b/motion/src/examples/Makefile
index 4dacfd5..686382b 100644
--- a/motion/src/examples/Makefile
+++ b/motion/src/examples/Makefile
@@ -2,7 +2,7 @@ PROJECT=manager_ex
 ########################################################
 # afegir tots els fitxers que s'han de compilar aquí
 ########################################################
-SOURCES=walk_straight.c
+SOURCES=get_up.c#walk_straight.c
 
 OBJS=$(SOURCES:.c=.o)
 SRC_DIR=./src/
-- 
GitLab