From 58530ac42ab04c2a304c791b452c308bf82ca983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20S=C3=A1nchez=20de=20Le=C3=B3n=20Peque?= Date: Tue, 17 Apr 2018 20:10:29 +0200 Subject: [PATCH 1/3] Remove TIM1 interruptions and configuration setup --- src/setup.c | 39 --------------------------------------- src/target/detection.c | 14 -------------- 2 files changed, 53 deletions(-) diff --git a/src/setup.c b/src/setup.c index 540d89bd..02ca7245 100644 --- a/src/setup.c +++ b/src/setup.c @@ -63,11 +63,9 @@ static void setup_clock(void) * * - Systick priority to 1 with SCB. * - USART3 with priority 2 with NVIC. - * - TIM1_UP with priority 0. * * Interruptions enabled: * - * - TIM1 Update interrupt. * - USART3 interrupt. * * @note The priority levels are assigned on steps of 16 because the processor @@ -77,11 +75,9 @@ static void setup_clock(void) */ static void setup_exceptions(void) { - nvic_set_priority(NVIC_TIM1_UP_IRQ, 0); nvic_set_priority(NVIC_SYSTICK_IRQ, PRIORITY_FACTOR * 1); nvic_set_priority(NVIC_USART3_IRQ, PRIORITY_FACTOR * 2); - nvic_enable_irq(NVIC_TIM1_UP_IRQ); nvic_enable_irq(NVIC_USART3_IRQ); } @@ -406,40 +402,6 @@ static void setup_adc2(void) start_adc(ADC2); } -/** - * @brief TIM1 setup. - * - * The TIM1 generates an update event interruption that invokes the - * function tim1_up_isr. - * - * - Set TIM1 default values. - * - Configure the base time (no clock division ratio, no aligned mode, - * direction up). - * - Set clock division, prescaler and period parameters to get an update - * event with a frequency of 16 KHz. 16 interruptions by ms, 4 sensors with - * 4 states. - * - * \f$frequency = \frac{timerclock}{(preescaler + 1)(period + 1)}\f$ - * - * - Enable the TIM1. - * - Enable the interruption of type update event on the TIM1. - * - * @note The TIM1 is conected to the APB2 prescaler. - * - * @see Reference manual (RM0008) "Advanced-control timers" - */ -static void setup_timer1(void) -{ - rcc_periph_reset_pulse(RST_TIM1); - timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, - TIM_CR1_DIR_UP); - timer_set_clock_division(TIM1, 0x00); - timer_set_prescaler(TIM1, (rcc_apb2_frequency / 160000 - 1)); - timer_set_period(TIM1, 10 - 1); - timer_enable_counter(TIM1); - timer_enable_irq(TIM1, TIM_DIER_UIE); -} - /** * @brief Execute all setup functions. */ @@ -455,6 +417,5 @@ void setup(void) setup_pwm(); setup_mpu(); setup_systick(); - setup_timer1(); setup_adc1(); } diff --git a/src/target/detection.c b/src/target/detection.c index 4820452d..2d09b658 100644 --- a/src/target/detection.c +++ b/src/target/detection.c @@ -246,20 +246,6 @@ static void sm_emitter_adc(void) } } -/** - * @brief TIM1 interruption routine. - * - * - Manage the update event interruption flag. - * - Trigger state machine to manage sensors. - */ -void tim1_up_isr(void) -{ - if (timer_get_flag(TIM1, TIM_SR_UIF)) { - timer_clear_flag(TIM1, TIM_SR_UIF); - sm_emitter_adc(); - } -} - /** * @brief Get sensors values with emitter on and off. */ From 740c52a81ae7aeb489986b018106d8db29baf329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20S=C3=A1nchez=20de=20Le=C3=B3n=20Peque?= Date: Fri, 20 Apr 2018 00:45:08 +0200 Subject: [PATCH 2/3] Read sensors with systick and just before control --- src/setup.c | 1 - src/target/detection.c | 87 +++++++++++++++--------------------------- 2 files changed, 31 insertions(+), 57 deletions(-) diff --git a/src/setup.c b/src/setup.c index 02ca7245..9481b35f 100644 --- a/src/setup.c +++ b/src/setup.c @@ -417,5 +417,4 @@ void setup(void) setup_pwm(); setup_mpu(); setup_systick(); - setup_adc1(); } diff --git a/src/target/detection.c b/src/target/detection.c index 2d09b658..77474dd8 100644 --- a/src/target/detection.c +++ b/src/target/detection.c @@ -3,7 +3,6 @@ #define SIDE_WALL_DETECTION (CELL_DIMENSION * 0.90) #define FRONT_WALL_DETECTION (CELL_DIMENSION * 1.5) #define SIDE_CALIBRATION_READINGS 20 -#define SENSORS_SM_TICKS 4 #define LOG_CONVERSION_TABLE_STEP 4 #define LOG_CONVERSION_TABLE_SIZE (ADC_RESOLUTION / LOG_CONVERSION_TABLE_STEP) @@ -190,72 +189,47 @@ static void set_emitter_off(uint8_t emitter) } /** - * @brief State machine to manage the sensors activation and deactivation - * states and readings. - * - * In order to get accurate distance values, the phototransistor's output - * will be read with the infrared emitter sensors powered on and powered - * off. Besides, to avoid undesired interactions between different emitters and - * phototranistors, the reads will be done one by one. - * - * The battery voltage is also read on the state 1. - * - * - State 1 (first because the emitter is OFF on start): - * -# Save phototranistors sensors (ADC1) from emitter OFF and - * power ON the emitter. - * - State 2: - * -# Start the phototranistors sensors (ADC1) read. - * - State 3: - * -# Save phototranistors sensors (ADC1) from emitter ON and - * power OFF the emitter. - * - State 4: - * -# Start the phototranistors sensors (ADC1) read. + * @brief Get sensors values with emitter on and off. */ -static void sm_emitter_adc(void) +void get_sensors_raw(uint16_t *off, uint16_t *on) { - static uint8_t emitter_status = 1; - static uint8_t sensor_index = SENSOR_SIDE_LEFT_ID; + uint8_t i = 0; - switch (emitter_status) { - case 1: - sensors_off[sensor_index] = - adc_read_injected(ADC1, (sensor_index + 1)); - set_emitter_on(sensor_index); - emitter_status = 2; - break; - case 2: - adc_start_conversion_injected(ADC1); - emitter_status = 3; - break; - case 3: - sensors_on[sensor_index] = - adc_read_injected(ADC1, (sensor_index + 1)); - set_emitter_off(sensor_index); - emitter_status = 4; - break; - case 4: - adc_start_conversion_injected(ADC1); - emitter_status = 1; - if (sensor_index == (NUM_SENSOR - 1)) - sensor_index = 0; - else - sensor_index++; - break; - default: - break; + for (i = 0; i < NUM_SENSOR; i++) { + off[i] = sensors_off[i]; + on[i] = sensors_on[i]; } } /** - * @brief Get sensors values with emitter on and off. + * @brief Start and wait for complete injection of sensor readings. */ -void get_sensors_raw(uint16_t *off, uint16_t *on) +static void inject_readings(void) +{ + adc_start_conversion_injected(ADC1); + while (!(adc_eoc_injected(ADC1))) + ; + // Clear injected end of conversion + ADC_SR(ADC1) &= ~ADC_SR_JEOC; +} + +/** + * @brief Update the sensors raw readings. + */ +static void update_raw_readings(void) { uint8_t i = 0; + inject_readings(); + for (i = 0; i < NUM_SENSOR; i++) + sensors_off[i] = adc_read_injected(ADC1, (i + 1)); + for (i = 0; i < NUM_SENSOR; i++) { - off[i] = sensors_off[i]; - on[i] = sensors_on[i]; + set_emitter_on(i); + sleep_us(10); + inject_readings(); + sensors_on[i] = adc_read_injected(ADC1, (i + 1)); + set_emitter_off(i); } } @@ -291,6 +265,7 @@ void update_distance_readings(void) { uint8_t i = 0; + update_raw_readings(); for (i = 0; i < NUM_SENSOR; i++) { distance[i] = (sensors_calibration_a[i] / raw_log(sensors_on[i], sensors_off[i]) - @@ -433,7 +408,7 @@ void side_sensors_calibration(void) for (i = 0; i < SIDE_CALIBRATION_READINGS; i++) { left_temp += distance[SENSOR_SIDE_LEFT_ID]; right_temp += distance[SENSOR_SIDE_RIGHT_ID]; - sleep_ticks(SENSORS_SM_TICKS); + sleep_ticks(2); } calibration_factor[SENSOR_SIDE_LEFT_ID] += (left_temp / SIDE_CALIBRATION_READINGS) - MIDDLE_MAZE_DISTANCE; From 5e279ed11ff3c487045cf957117648df22e5a26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20S=C3=A1nchez=20de=20Le=C3=B3n=20Peque?= Date: Thu, 24 May 2018 01:54:40 +0200 Subject: [PATCH 3/3] More stable sensors readings --- src/target/detection.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/target/detection.c b/src/target/detection.c index 77474dd8..9150e4a5 100644 --- a/src/target/detection.c +++ b/src/target/detection.c @@ -220,9 +220,14 @@ static void update_raw_readings(void) { uint8_t i = 0; - inject_readings(); - for (i = 0; i < NUM_SENSOR; i++) + for (i = 0; i < NUM_SENSOR; i++) { + inject_readings(); sensors_off[i] = adc_read_injected(ADC1, (i + 1)); + } + for (i = 0; i < NUM_SENSOR; i++) { + inject_readings(); + sensors_off[i] = adc_read_injected(ADC1, (i + 1)); + } for (i = 0; i < NUM_SENSOR; i++) { set_emitter_on(i);