From 1604d98406bcf99511d9fe13bd85e9bfdf38ec95 Mon Sep 17 00:00:00 2001 From: Martin Mueller Date: Mon, 23 May 2011 09:30:40 +0200 Subject: [PATCH] add University of Reading atmosphere charge sensor --- conf/messages.xml | 14 +- conf/modules/charge_sens.xml | 14 ++ .../non_ap/charge_sens/charge_sens.pde | 160 ++++++++++++++++++ sw/airborne/modules/meteo/charge_sens.c | 73 ++++++++ sw/airborne/modules/meteo/charge_sens.h | 11 ++ 5 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 conf/modules/charge_sens.xml create mode 100644 sw/airborne/firmwares/non_ap/charge_sens/charge_sens.pde create mode 100644 sw/airborne/modules/meteo/charge_sens.c create mode 100644 sw/airborne/modules/meteo/charge_sens.h diff --git a/conf/messages.xml b/conf/messages.xml index 692914de17..b4635cdc3c 100644 --- a/conf/messages.xml +++ b/conf/messages.xml @@ -779,7 +779,19 @@ - + + + + + + + + + + + + + diff --git a/conf/modules/charge_sens.xml b/conf/modules/charge_sens.xml new file mode 100644 index 0000000000..561b0132db --- /dev/null +++ b/conf/modules/charge_sens.xml @@ -0,0 +1,14 @@ + + + +
+ +
+ + + + + + +
+ diff --git a/sw/airborne/firmwares/non_ap/charge_sens/charge_sens.pde b/sw/airborne/firmwares/non_ap/charge_sens/charge_sens.pde new file mode 100644 index 0000000000..ee3b26fdab --- /dev/null +++ b/sw/airborne/firmwares/non_ap/charge_sens/charge_sens.pde @@ -0,0 +1,160 @@ +/* + * $Id$ + * + * Copyright (C) 2011 Martin Mueller + * + * This file is part of Paparazzi. + * + * Paparazzi is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * Paparazzi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with paparazzi; see the file COPYING. If not, write to + * the Free Software Foundation, 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +/* I2C interface for University of Reading charge sensor */ + +#define DEBUG 1 + +#include + + +/* in ms */ +#define SAMPLE_RATE 100 +/* x times samle rate */ +#define RESET_RATE 50 + +/* x times over-sampling */ +#define AVG_NB_SAMPLE 10 + +/* print every x values */ +#define LED_RATE 10 + +/* charge reset pin PB1, high active */ +#define RESET_PIN 9 +/* adc_in pin ADC0 */ +#define ADC_PIN 0 +/* green LED pin PB5 (on arduino pro mini) */ +#define LED_GR_PIN 13 + +#define CHARGE_SENS_I2C_ADDR 0x78 +#define RESET_ACTIVE 0x1000 + +struct adc_buf { + unsigned long sum; + unsigned short values[AVG_NB_SAMPLE]; + unsigned char head; +}; + +struct adc_buf buf; + +unsigned short reset_active_uart = 0; +unsigned short reset_active_i2c = 0; +unsigned long trig_next; +unsigned short adc_val; +unsigned char new_head; +int i, cnt = 0; +int i2c_cnt = 0; +int led_cnt = 0; +int reset_cnt = 0; + + +ISR(ADC_vect) { + adc_val = ADCL; + adc_val |= ADCH << 8; + new_head = buf.head + 1; + if (new_head >= AVG_NB_SAMPLE) + new_head=0; + buf.sum -= buf.values[new_head]; + buf.values[new_head] = adc_val; + buf.sum += adc_val; + buf.head = new_head; +} + +void read_i2c() { + unsigned short i2c; + i2c = (buf.sum / AVG_NB_SAMPLE) | reset_active_i2c; + reset_active_i2c = 0; + + Wire.send((uint8_t*)i2c, 2); +} + +void setup() { +#ifdef DEBUG + /* serial port */ + Serial.begin(38400); + pinMode(2,OUTPUT); + digitalWrite(2,HIGH); + Serial.println("charge sensor init"); +#endif + + /* I2C init */ + Wire.begin(CHARGE_SENS_I2C_ADDR >> 1); + Wire.onRequest(read_i2c); + + /* green LED init */ + digitalWrite(LED_GR_PIN, LOW); + pinMode(LED_GR_PIN, OUTPUT); + + /* reset pin init */ + digitalWrite(RESET_PIN, LOW); + pinMode(RESET_PIN, OUTPUT); + + /* init adc, default reference */ + ADCSRA |= (1<= trig_next) { + trig_next += SAMPLE_RATE / AVG_NB_SAMPLE; + + /* start conversion */ + ADCSRA |= 1 << ADSC; + + if (++cnt >= AVG_NB_SAMPLE) { + cnt = 0; + + if (++reset_cnt >= RESET_RATE) { + /* start reset */ + digitalWrite(RESET_PIN, HIGH); + reset_cnt = 0; + reset_active_uart = RESET_ACTIVE; + reset_active_i2c = RESET_ACTIVE; + } + else { + /* stop reset */ + digitalWrite(RESET_PIN, LOW); + } + +#ifdef DEBUG + Serial.print((buf.sum / AVG_NB_SAMPLE) | reset_active_uart); + Serial.print(" "); +#endif + if (++led_cnt >= LED_RATE) { + led_cnt = 0; + /* blink green LED */ + digitalWrite(LED_GR_PIN, HIGH); +#ifdef DEBUG + Serial.println(""); +#endif + } + else digitalWrite(LED_GR_PIN, LOW); + reset_active_uart = 0; + } + } +} + diff --git a/sw/airborne/modules/meteo/charge_sens.c b/sw/airborne/modules/meteo/charge_sens.c new file mode 100644 index 0000000000..91a1d2e720 --- /dev/null +++ b/sw/airborne/modules/meteo/charge_sens.c @@ -0,0 +1,73 @@ +/* + * $Id$ + * + * Copyright (C) 2011 Martin Mueller + * + * This file is part of paparazzi. + * + * paparazzi is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * paparazzi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with paparazzi; see the file COPYING. If not, write to + * the Free Software Foundation, 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + */ + +/** \file charge_sens.c + * \brief I2C interface for University of Reading charge sensor + * + */ + +#include "modules/meteo/charge_sens.h" +#include "mcu_periph/i2c.h" +#include "mcu_periph/uart.h" +#include "messages.h" +#include "downlink.h" + +#ifndef DOWNLINK_DEVICE +#define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE +#endif + +#ifndef CHARGE_SENS_DEV +#define CHARGE_SENS_DEV i2c0 +#endif + +#define CHARGE_SENS_I2C_ADDR 0x78 +#define CHARGE_NB 10 + +struct i2c_transaction charge_trans; +uint16_t charge[CHARGE_NB]; +int32_t charge_cnt; + +void charge_sens_init( void ) { + charge_cnt = 0; +} + +void charge_sens_periodic( void ) { + I2CReceive(CHARGE_SENS_DEV, charge_trans, CHARGE_SENS_I2C_ADDR, 2); +} + +void charge_sens_event( void ) { + if (charge_trans.status == I2CTransSuccess) { + /* read two byte atmosphere charge */ + charge[charge_cnt] = charge_trans.buf[1] << 8; + charge[charge_cnt] |= charge_trans.buf[0]; + charge_trans.status = I2CTransDone; + + if (++charge_cnt >= CHARGE_NB) { + DOWNLINK_SEND_ATMOSPHERE_CHARGE(DefaultChannel, + &charge[0], &charge[1], &charge[2], &charge[3], &charge[4], + &charge[5], &charge[6], &charge[7], &charge[8], &charge[9]); + charge_cnt = 0; + } + } +} diff --git a/sw/airborne/modules/meteo/charge_sens.h b/sw/airborne/modules/meteo/charge_sens.h new file mode 100644 index 0000000000..0e6ad15e74 --- /dev/null +++ b/sw/airborne/modules/meteo/charge_sens.h @@ -0,0 +1,11 @@ +#ifndef CHARGE_SENS_H +#define CHARGE_SENS_H + +#include "std.h" + +void charge_sens_init(void); +void charge_sens_periodic(void); +void charge_sens_event(void); + +#endif +