From bda06a1cc21008d119ef3ca95913113ee062fbc2 Mon Sep 17 00:00:00 2001 From: Jérémy Dufour Date: Sat, 16 May 2009 01:23:16 +0200 Subject: * digital/io/src: - always update sharp module every cycle, - sharp update now read only one value every cycle, filter it SHARP_RAW_FILTER times and only keep the smallest one. --- digital/io/src/main.c | 21 ++---------------- digital/io/src/sharp.c | 60 +++++++++++++++++++++++++++++++++----------------- digital/io/src/sharp.h | 3 +-- 3 files changed, 43 insertions(+), 41 deletions(-) (limited to 'digital/io/src') diff --git a/digital/io/src/main.c b/digital/io/src/main.c index ea200a73..76bc66b9 100644 --- a/digital/io/src/main.c +++ b/digital/io/src/main.c @@ -116,16 +116,6 @@ static uint8_t main_stats_asserv_, main_stats_asserv_cpt_; */ static uint8_t main_stats_timer_; -/** - * Update frequency of sharps. - */ -#define MAIN_SHARP_UPDATE_FREQ 5 - -/** - * Sharps frequency counter. - */ -uint8_t main_sharp_freq_counter_; - /** * Initialize the main and all its subsystems. */ @@ -246,15 +236,8 @@ main_loop (void) /* Update wait flag for move FSM */ if (main_move_wait_cycle) main_move_wait_cycle--; - /* Update sharp module if required and only every - * MAIN_SHARP_UPDATE_FREQ cycles */ - if (++main_sharp_freq_counter_ == MAIN_SHARP_UPDATE_FREQ) - { - /* Update sharps */ - sharp_update (0xff); - /* Reset counter */ - main_sharp_freq_counter_ = 0; - } + /* Update sharps */ + sharp_update (); /* Update FSM timeouts. */ FSM_HANDLE_TIMEOUT (&move_fsm); FSM_HANDLE_TIMEOUT (&top_fsm); diff --git a/digital/io/src/sharp.c b/digital/io/src/sharp.c index 2283937c..78c0d371 100644 --- a/digital/io/src/sharp.c +++ b/digital/io/src/sharp.c @@ -27,6 +27,7 @@ #include "sharp.h" #include "modules/adc/adc.h" /* ADC functions */ +#include "modules/utils/utils.h" #include "io.h" /** Sharp conversion table. @@ -40,6 +41,12 @@ uint16_t sharp_conv_table[SHARP_NB_ELEMENT_TABLE_CONV][2] = {100, 480}, {90, 500}, {80, 500}}; +/** + * Raw filter iteration. + * How many values should we get when reading a sharp? + */ +#define SHARP_RAW_FILTER 2 + /** * Cached array of raw sharp values. */ @@ -130,27 +137,40 @@ sharp_init (void) /* Update read data from sharps. */ void -sharp_update (uint8_t sharp_mask) +sharp_update (void) { - uint8_t compt; - - /* Go through the bit mask */ - for (compt = 0; compt < SHARP_NUMBER; compt++) - { - /* Check if the bit/sharp id is set */ - if (bit_is_set (sharp_mask, compt)) - { - /* Start the capture */ - adc_start (compt); - /* Wait until ADC mesure is finished */ - while (!adc_checkf ()) - ; - /* Store the value */ - sharp_values_[compt] = adc_read (); - /* Update interpreted cached value */ - sharp_cache_interpreted_[compt] = sharp_update_interpreted (compt); - } - } + static uint8_t sharp_filter_count = SHARP_RAW_FILTER; + static uint8_t sharp_id = 0; + static uint16_t min_value = 0xFFFF; + uint16_t current; + + /* Start the capture */ + adc_start (sharp_id); + /* Wait until ADC mesure is finished */ + while (!adc_checkf ()) + ; + /* Get the value */ + current = adc_read (); + /* Only keep the minimum. */ + min_value = UTILS_MIN (min_value, current); + /* Filter decrement. */ + sharp_filter_count--; + + /* Enough reading? */ + if (!sharp_filter_count) + { + /* Store value. */ + sharp_values_[sharp_id] = min_value; + /* Update interpreted cached value */ + sharp_cache_interpreted_[sharp_id] = sharp_update_interpreted + (sharp_id); + /* Next sharp. */ + sharp_id++; + sharp_id %= SHARP_NUMBER; + /* Reset. */ + min_value = 0xFFFF; + sharp_filter_count = SHARP_RAW_FILTER; + } } /* Get raw cached data from sharps. */ diff --git a/digital/io/src/sharp.h b/digital/io/src/sharp.h index 2a4f1c5d..4b0eb1de 100644 --- a/digital/io/src/sharp.h +++ b/digital/io/src/sharp.h @@ -89,9 +89,8 @@ void sharp_init (void); * Update read data from sharps. * This function is blocking. To get the value you have to use the get * function (@a sharp_get_raw). - * @param sharp_mask list of sharps (using a mask) to update. */ -void sharp_update (uint8_t sharp_mask); +void sharp_update (void); /** * Get raw cached data from sharps. -- cgit v1.2.3