From 956ab85c201b865e36bcf42bf2b90baca405b620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergi=20Hern=C3=A1ndez?= <shernand@iri.upc.edu> Date: Wed, 11 May 2016 22:50:19 +0200 Subject: [PATCH] Solved a problem while reading the Robotis IR sensors. It seems they have to be enabled and disabled in orer to work properly. --- controllers/include/cm510_cfg.h | 2 +- controllers/src/adc.c | 72 ++++++++++++++++----------------- controllers/src/cm510.c | 2 +- motion/Makefile | 2 +- motion/src/examples/Makefile | 4 +- 5 files changed, 39 insertions(+), 43 deletions(-) diff --git a/controllers/include/cm510_cfg.h b/controllers/include/cm510_cfg.h index 88cca06..f8430c0 100644 --- a/controllers/include/cm510_cfg.h +++ b/controllers/include/cm510_cfg.h @@ -5,7 +5,7 @@ // controller configuration parameters #define ADC_MAX_NUM_SAMPLES 16 -#define ADC_SAMPLE_PERIOD_MS 10 +#define ADC_SAMPLE_PERIOD_MS 40 #define ADC_VOLTAGE_ALARM_TH 559 #define ADC_VOLTAGE_ALARM_WINDOW 20 #define ADC_VOLTAGE_ALARM_NOTE NOTE_LA diff --git a/controllers/src/adc.c b/controllers/src/adc.c index 28ad805..bc6d5c4 100644 --- a/controllers/src/adc.c +++ b/controllers/src/adc.c @@ -22,7 +22,7 @@ void adc_set_channel(uint8_t ch_id) ADMUX&=0xF8; ADMUX|=(ch_id&0x07); if(ch_id==0) - PORTA&=0xFC; + PORTA|=0xFC; else PORTA=~(0x80>>(ch_id-1));// enable the desired channel } @@ -33,7 +33,7 @@ void adc_set_sample_period(void) uint16_t compare_value; uint8_t i; - for(i=0;i<5;i++) + for(i=0;i<7;i++) { compare_value=(((F_CPU/1000)*ADC_SAMPLE_PERIOD_MS)/(prescaler_values[i]*NUM_ADC))-1; if(compare_value<256) @@ -75,49 +75,48 @@ void adc_loop(void) { TIFR2|=(1<<OCF2A)|(1<<OCF2B)|(1<<TOV2);// clear interrupt flag TCNT2=0x00;// start from 0 + // set the channel to sample + adc_set_channel(adc_current_ch); + _delay_us(12); // start a new conversion adc_start_conv(); - } - else - { - if(adc_is_conversion_done()) + // wait for the conversion to end + while(!adc_is_conversion_done()); + ADCSRA|=(1<<ADIF);// clear interrupt flag + data=ADC;//(ADCL | (ADCH<<8)); + adc_values[adc_current_ch]=data; + adc_ch_data[adc_current_ch][adc_current_sample]=data; + // compute the average for the current channel + data=0; + for(i=0;i<ADC_MAX_NUM_SAMPLES;i++) + data+=adc_ch_data[adc_current_ch][i]; + data=data/ADC_MAX_NUM_SAMPLES; + adc_avg_values[adc_current_ch]=data; + if(adc_current_ch==NUM_ADC-1)// last channel + adc_current_sample=(adc_current_sample+1)%ADC_MAX_NUM_SAMPLES; + if(adc_current_ch==ADC_VCC)// monitor the Voltage supply { - ADCSRA|=(1<<ADIF);// clear interrupt flag - data=(ADCL | (ADCH<<8)); - adc_values[adc_current_ch]=data; - adc_ch_data[adc_current_ch][adc_current_sample]=data; - // compute the average for the current channel - data=0; - for(i=0;i<ADC_MAX_NUM_SAMPLES;i++) - data+=adc_ch_data[adc_current_ch][i]; - data=data/ADC_MAX_NUM_SAMPLES; - adc_avg_values[adc_current_ch]=data; - if(adc_current_ch==NUM_ADC-1)// last channel - adc_current_sample=(adc_current_sample+1)%ADC_MAX_NUM_SAMPLES; - if(adc_current_ch==ADC_VCC)// monitor the Voltage supply +// voltage_mv=((uint32_t)data*(uint32_t)5000*(uint32_t)133)/((uint16_t)1024*(uint16_t)33); + // compare with hysteresis + if(adc_voltage_alarm==0x01) { -// voltage_mv=((uint32_t)data*(uint32_t)5000*(uint32_t)133)/((uint16_t)1024*(uint16_t)33); - // compare with hysteresis - if(adc_voltage_alarm==0x01) + if(data>=(ADC_VOLTAGE_ALARM_TH+ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V { - if(data>=(ADC_VOLTAGE_ALARM_TH+ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V - { - buzzer_stop_alarm(); - adc_voltage_alarm=0x00; - } + buzzer_stop_alarm(); + adc_voltage_alarm=0x00; } - else + } + else + { + if(data<(ADC_VOLTAGE_ALARM_TH-ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V { - if(data<(ADC_VOLTAGE_ALARM_TH-ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V - { - buzzer_start_alarm(NOTE_LA,30,30); - adc_voltage_alarm=0x01; - } + buzzer_start_alarm(NOTE_LA,30,30); + adc_voltage_alarm=0x01; } } - adc_current_ch=(adc_current_ch+1)%NUM_ADC; - adc_set_channel(adc_current_ch); } + // select next channel + adc_current_ch=(adc_current_ch+1)%NUM_ADC; } } @@ -155,9 +154,6 @@ void init_adc(void) TIMSK2=0x00;// interrupts not enabled TIFR2=0x07;// clear any pending interrupt adc_set_sample_period();// set the default sample period - - // enable the first ADC channel - adc_set_channel(adc_current_ch); } uint16_t get_adc_channel(adc_t channel_id) diff --git a/controllers/src/cm510.c b/controllers/src/cm510.c index dd68036..fbe21d1 100755 --- a/controllers/src/cm510.c +++ b/controllers/src/cm510.c @@ -42,7 +42,6 @@ void init_cm510(void) init_adc(); init_buzzer(); init_user_time(); - exp_board_init(EXP_BOARD_ID); } int16_t main(void) @@ -51,6 +50,7 @@ int16_t main(void) sei(); /* call the user initialization function */ manager_init(18); + exp_board_init(EXP_BOARD_ID); user_init(); // turn BAT_LED on to indicate the initialization is done turn_led_on(LED_BAT); diff --git a/motion/Makefile b/motion/Makefile index fe1e351..5b1a7ee 100755 --- a/motion/Makefile +++ b/motion/Makefile @@ -13,7 +13,7 @@ MMCU=atmega2561 INC_DIRS=-I./include/ -I$(DEV_DIR)include/ -I$(COMM_DIR)include/ -I$(CONT_DIR)include/ -LIBS=$(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a +LIBS=$(CONT_DIR)lib/libcontrollers.a $(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a CFLAGS=-mmcu=$(MMCU) -Wall -O3 -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes diff --git a/motion/src/examples/Makefile b/motion/src/examples/Makefile index 686382b..847e3b2 100644 --- a/motion/src/examples/Makefile +++ b/motion/src/examples/Makefile @@ -14,7 +14,7 @@ CC=avr-gcc OBJCOPY=avr-objcopy MMCU=atmega2561 -LIBS=$(CONT_DIR)lib/libcontrollers.a $(MAN_DIR)lib/libmotion_manager.a $(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a +LIBS=$(MAN_DIR)lib/libmotion_manager.a $(CONT_DIR)lib/libcontrollers.a $(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a INCLUDE_DIRS=-I$(DEV_DIR)include -I$(COMM_DIR)include -I$(CONT_DIR)include -I$(MAN_DIR)include @@ -48,7 +48,7 @@ motion_manager: $(MAKE) -C $(MAN_DIR) download: $(MAIN_OUT_HEX) - fw_downloader -d /dev/ttyUSB0 -f ./$(PROJECT).hex -p new + fw_downloader -d /dev/ttyUSB0 -f ./$(PROJECT).hex -p cm510 clean: -rm $(PROJECT).* -- GitLab