diff --git a/conf/modules/ezcurrent.xml b/conf/modules/ezcurrent.xml new file mode 100644 index 0000000000..dbaf90acb0 --- /dev/null +++ b/conf/modules/ezcurrent.xml @@ -0,0 +1,26 @@ + + + + + + + EzOSD Current sensor (I2C) + + + +
+ +
+ + + + + + + + + +
+ diff --git a/sw/airborne/firmwares/fixedwing/main_fbw.c b/sw/airborne/firmwares/fixedwing/main_fbw.c index a901fdfdd1..986331c7e4 100644 --- a/sw/airborne/firmwares/fixedwing/main_fbw.c +++ b/sw/airborne/firmwares/fixedwing/main_fbw.c @@ -64,7 +64,9 @@ void init_fbw( void ) { mcu_init(); +#ifndef DISABLE_ELECTRICAL electrical_init(); +#endif #ifdef ACTUATORS actuators_init(); @@ -213,7 +215,9 @@ void handle_periodic_tasks_fbw(void) { if (sys_time_check_and_ack_timer(fbw_periodic_tid)) periodic_task_fbw(); +#ifndef DISABLE_ELECTRICAL if (sys_time_check_and_ack_timer(electrical_tid)) electrical_periodic(); +#endif } diff --git a/sw/airborne/modules/sensors/ezcurrent.c b/sw/airborne/modules/sensors/ezcurrent.c new file mode 100644 index 0000000000..1aeac5e81d --- /dev/null +++ b/sw/airborne/modules/sensors/ezcurrent.c @@ -0,0 +1,77 @@ +/* + * Driver for the EzOSD Current sensor. + * + * Notes: + * Connect directly to I2C1 port. + * + * Copyright (C) 2012 Gerard Toonstra + * + * 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. + * + */ +#include "sensors/ezcurrent.h" +#include "estimator.h" +#include "mcu_periph/i2c.h" +#include "mcu_periph/uart.h" +#include "messages.h" +#include "subsystems/datalink/downlink.h" +#include "subsystems/electrical.h" +#include + +#define EZCURRENT_ADDR 0xEF + +#ifndef ezcurrent_I2C_DEV +#define ezcurrent_I2C_DEV i2c1 +#endif + +#ifndef DOWNLINK_DEVICE +#define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE +#endif + +struct i2c_transaction ezcurrent_i2c_trans; + +void ezcurrent_init( void ) { + electrical.vsupply = 0; + electrical.current = 0; + + ezcurrent_i2c_trans.status = I2CTransDone; + ezcurrent_i2c_trans.slave_addr = EZCURRENT_ADDR; +} + +void ezcurrent_read_periodic( void ) { +#ifndef SITL + if (ezcurrent_i2c_trans.status == I2CTransDone) { + I2CReceive(ezcurrent_I2C_DEV, ezcurrent_i2c_trans, ezcurrent_i2c_trans.slave_addr, 10); + } +#endif //SITL +} + +void ezcurrent_read_event( void ) { + if (ezcurrent_i2c_trans.status == I2CTransSuccess) { + // Get electrical information from buffer + electrical.vsupply = ((uint8_t)( (((ezcurrent_i2c_trans.buf[3]) << 8) + (ezcurrent_i2c_trans.buf[2])) * 0.01f) ); + electrical.current = ((int32_t)(ezcurrent_i2c_trans.buf[9]) << 8) + (int32_t)(ezcurrent_i2c_trans.buf[8]); + electrical.consumed = ((int32_t)(ezcurrent_i2c_trans.buf[7]) << 8) + (int32_t)(ezcurrent_i2c_trans.buf[6]); + // Transaction has been read + ezcurrent_i2c_trans.status = I2CTransDone; + } else if ( ezcurrent_i2c_trans.status == I2CTransFailed ) { + ezcurrent_i2c_trans.status = I2CTransDone; + // ezcurrent_i2c_trans.slave_addr++; + } +} + diff --git a/sw/airborne/modules/sensors/ezcurrent.h b/sw/airborne/modules/sensors/ezcurrent.h new file mode 100644 index 0000000000..1951a57aeb --- /dev/null +++ b/sw/airborne/modules/sensors/ezcurrent.h @@ -0,0 +1,46 @@ +/* + * Driver for the EzOSD Current sensor. + * + * Notes: + * Connect directly to I2C1 port. + * + * Sensor module wire assignments: + * Red wire: 5V + * Black wire: Ground + * DAT: SDA + * CLK: SCL + * + * Copyright (C) 2012 Gerard Toonstra + * + * 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. + * + */ + +#ifndef EZCURRENT_H +#define EZCURRENT_H + +#include "std.h" +#include "mcu_periph/i2c.h" + +extern struct i2c_transaction ezcurrent_i2c_trans; + +extern void ezcurrent_init( void ); +extern void ezcurrent_read_periodic( void ); +extern void ezcurrent_read_event( void ); + +#endif // EZCURRENT_H diff --git a/sw/airborne/subsystems/electrical.h b/sw/airborne/subsystems/electrical.h index 9980257d28..b093e83b10 100644 --- a/sw/airborne/subsystems/electrical.h +++ b/sw/airborne/subsystems/electrical.h @@ -7,6 +7,7 @@ struct Electrical { uint8_t vsupply; /* supply in decivolts */ int32_t current; /* current in milliamps */ + int32_t consumed; /* consumption in mAh */ };