2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
2011-01-03 02:23:04 +01:00
|
|
|
* @{
|
2010-09-27 09:28:34 +02:00
|
|
|
* @addtogroup BatteryModule Battery Module
|
|
|
|
* @brief Measures battery voltage and current
|
|
|
|
* Updates the FlightBatteryState object
|
2011-01-03 02:23:04 +01:00
|
|
|
* @{
|
2010-09-27 09:28:34 +02:00
|
|
|
*
|
|
|
|
* @file battery.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2011-01-17 05:02:37 +01:00
|
|
|
* @brief Module to read the battery Voltage and Current periodically and set alarms appropriately.
|
2010-09-27 09:28:34 +02:00
|
|
|
*
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output object: FlightBatteryState
|
|
|
|
*
|
|
|
|
* This module will periodically generate information on the battery state.
|
|
|
|
*
|
|
|
|
* UAVObjects are automatically generated by the UAVObjectGenerator from
|
|
|
|
* the object definition XML file.
|
|
|
|
*
|
|
|
|
* Modules have no API, all communication to other modules is done through UAVObjects.
|
|
|
|
* However modules may use the API exposed by shared libraries.
|
|
|
|
* See the OpenPilot wiki for more details.
|
|
|
|
* http://www.openpilot.org/OpenPilot_Application_Architecture
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
|
|
|
|
|
|
|
#include "flightbatterystate.h"
|
2011-03-19 19:45:40 +01:00
|
|
|
#include "flightbatterysettings.h"
|
2012-06-04 17:31:45 +02:00
|
|
|
#include "hwsettings.h"
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Configuration
|
|
|
|
//
|
2011-02-19 19:56:52 +01:00
|
|
|
#define SAMPLE_PERIOD_MS 500
|
2010-09-27 09:28:34 +02:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
2012-06-04 17:31:45 +02:00
|
|
|
static bool batteryEnabled = false;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2012-07-08 13:16:05 +02:00
|
|
|
//THESE COULD BE BETTER AS SOME KIND OF UNION OR STRUCT, BY WHICH 4 BITS ARE USED FOR EACH
|
|
|
|
//PIN VARIABLE, ONE OF WHICH INDICATES SIGN, AND THE OTHER 3 BITS INDICATE POSITION. THIS WILL
|
|
|
|
//WORK FOR QUITE SOMETIME, UNTIL MORE THAN 8 ADC ARE AVAILABLE. EVEN AT THIS POINT, THE STRUCTURE
|
|
|
|
//CAN SIMPLY BE MODIFIED TO SUPPORT 15 ADC PINS, BY USING ALL AVAILABLE BITS.
|
|
|
|
static int8_t voltageADCPin=-1; //ADC pin for voltage
|
|
|
|
static int8_t currentADCPin=-1; //ADC pin for current
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Private functions
|
2011-02-19 19:56:52 +01:00
|
|
|
static void onTimer(UAVObjEvent* ev);
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t BatteryInitialize(void)
|
|
|
|
{
|
2012-06-04 17:31:45 +02:00
|
|
|
|
2012-07-08 23:13:05 +02:00
|
|
|
|
2013-03-16 10:42:12 +01:00
|
|
|
#ifdef MODULE_Battery_BUILTIN
|
2012-06-04 17:31:45 +02:00
|
|
|
batteryEnabled = true;
|
|
|
|
#else
|
|
|
|
uint8_t optionalModules[HWSETTINGS_OPTIONALMODULES_NUMELEM];
|
|
|
|
|
|
|
|
HwSettingsOptionalModulesGet(optionalModules);
|
2012-07-08 13:16:05 +02:00
|
|
|
|
2012-07-08 23:13:05 +02:00
|
|
|
if ((optionalModules[HWSETTINGS_OPTIONALMODULES_BATTERY] == HWSETTINGS_OPTIONALMODULES_ENABLED))
|
|
|
|
batteryEnabled = true;
|
|
|
|
else
|
|
|
|
batteryEnabled = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint8_t adcRouting[HWSETTINGS_ADCROUTING_NUMELEM];
|
|
|
|
HwSettingsADCRoutingGet(adcRouting);
|
|
|
|
|
2012-07-08 21:46:59 +02:00
|
|
|
//Determine if the battery sensors are routed to ADC pins
|
2012-07-08 13:16:05 +02:00
|
|
|
for (int i=0; i < HWSETTINGS_ADCROUTING_NUMELEM; i++) {
|
2012-07-08 23:13:05 +02:00
|
|
|
if (adcRouting[i] == HWSETTINGS_ADCROUTING_BATTERYVOLTAGE) {
|
2012-07-08 13:16:05 +02:00
|
|
|
voltageADCPin = i;
|
|
|
|
}
|
2012-07-08 23:13:05 +02:00
|
|
|
if (adcRouting[i] == HWSETTINGS_ADCROUTING_BATTERYCURRENT) {
|
2012-07-08 13:16:05 +02:00
|
|
|
currentADCPin = i;
|
|
|
|
}
|
|
|
|
}
|
2012-07-08 23:13:05 +02:00
|
|
|
|
|
|
|
//Don't enable module if no ADC pins are routed to the sensors
|
|
|
|
if (voltageADCPin <0 && currentADCPin <0)
|
2012-06-04 17:31:45 +02:00
|
|
|
batteryEnabled = false;
|
|
|
|
|
2012-07-08 23:13:05 +02:00
|
|
|
//Start module
|
2012-06-04 17:31:45 +02:00
|
|
|
if (batteryEnabled) {
|
|
|
|
FlightBatteryStateInitialize();
|
|
|
|
FlightBatterySettingsInitialize();
|
2011-06-18 18:59:02 +02:00
|
|
|
|
2012-06-04 17:31:45 +02:00
|
|
|
static UAVObjEvent ev;
|
2011-02-19 19:56:52 +01:00
|
|
|
|
2012-06-04 17:31:45 +02:00
|
|
|
memset(&ev,0,sizeof(UAVObjEvent));
|
|
|
|
EventPeriodicCallbackCreate(&ev, onTimer, SAMPLE_PERIOD_MS / portTICK_RATE_MS);
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-15 07:38:11 +02:00
|
|
|
MODULE_INITCALL(BatteryInitialize, 0)
|
2012-06-16 02:43:34 +02:00
|
|
|
#define HAS_SENSOR(x) batterySettings.SensorType[x]==FLIGHTBATTERYSETTINGS_SENSORTYPE_ENABLED
|
2011-02-19 19:56:52 +01:00
|
|
|
static void onTimer(UAVObjEvent* ev)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2011-02-24 06:05:17 +01:00
|
|
|
static FlightBatteryStateData flightBatteryData;
|
2011-03-19 19:45:40 +01:00
|
|
|
FlightBatterySettingsData batterySettings;
|
2011-02-19 19:56:52 +01:00
|
|
|
|
2011-03-19 19:45:40 +01:00
|
|
|
FlightBatterySettingsGet(&batterySettings);
|
2011-02-19 19:56:52 +01:00
|
|
|
|
2012-10-30 05:08:43 +01:00
|
|
|
static float dT = SAMPLE_PERIOD_MS / 1000.0f;
|
2012-06-16 02:43:34 +02:00
|
|
|
float energyRemaining;
|
2011-02-19 19:56:52 +01:00
|
|
|
|
|
|
|
//calculate the battery parameters
|
2012-07-08 13:16:05 +02:00
|
|
|
if (voltageADCPin >=0) {
|
2012-07-08 14:26:08 +02:00
|
|
|
flightBatteryData.Voltage = ((float)PIOS_ADC_PinGet(voltageADCPin)) * batterySettings.SensorCalibrations[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONS_VOLTAGEFACTOR]; //in Volts
|
2012-07-08 13:16:05 +02:00
|
|
|
}
|
2012-07-08 23:13:05 +02:00
|
|
|
else {
|
|
|
|
flightBatteryData.Voltage=1234; //Dummy placeholder value. This is in case we get another source of battery current which is not from the ADC
|
|
|
|
}
|
|
|
|
|
2012-07-08 13:16:05 +02:00
|
|
|
if (currentADCPin >=0) {
|
|
|
|
flightBatteryData.Current = ((float)PIOS_ADC_PinGet(currentADCPin)) * batterySettings.SensorCalibrations[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONS_CURRENTFACTOR]; //in Amps
|
|
|
|
if (flightBatteryData.Current > flightBatteryData.PeakCurrent)
|
|
|
|
flightBatteryData.PeakCurrent = flightBatteryData.Current; //in Amps
|
|
|
|
}
|
|
|
|
else { //If there's no current measurement, we still need to assign one. Make it negative, so it can never trigger an alarm
|
|
|
|
flightBatteryData.Current=-0.1234f; //Dummy placeholder value. This is in case we get another source of battery current which is not from the ADC
|
|
|
|
}
|
|
|
|
|
|
|
|
flightBatteryData.ConsumedEnergy += (flightBatteryData.Current * dT * 1000.0f / 3600.0f) ;//in mAh
|
|
|
|
|
|
|
|
//Apply a 2 second rise time low-pass filter to average the current
|
|
|
|
float alpha = 1.0f-dT/(dT+2.0f);
|
|
|
|
flightBatteryData.AvgCurrent=alpha*flightBatteryData.AvgCurrent+(1-alpha)*flightBatteryData.Current; //in Amps
|
|
|
|
|
|
|
|
/*The motor could regenerate power. Or we could have solar cells.
|
|
|
|
In short, is there any likelihood of measuring negative current? If it's a bad current reading we want to check, then
|
|
|
|
it makes sense to saturate at max and min values, because a misreading could as easily be very large, as negative. The simple
|
|
|
|
sign check doesn't catch this.*/
|
|
|
|
// //sanity checks
|
|
|
|
// if (flightBatteryData.AvgCurrent<0) flightBatteryData.AvgCurrent=0.0f;
|
|
|
|
// if (flightBatteryData.PeakCurrent<0) flightBatteryData.PeakCurrent=0.0f;
|
|
|
|
// if (flightBatteryData.ConsumedEnergy<0) flightBatteryData.ConsumedEnergy=0.0f;
|
2011-02-19 19:56:52 +01:00
|
|
|
|
2011-03-19 19:45:40 +01:00
|
|
|
energyRemaining = batterySettings.Capacity - flightBatteryData.ConsumedEnergy; // in mAh
|
2012-07-08 23:13:05 +02:00
|
|
|
if (flightBatteryData.AvgCurrent > 0)
|
|
|
|
flightBatteryData.EstimatedFlightTime = (energyRemaining / (flightBatteryData.AvgCurrent*1000.0f))*3600.0f;//in Sec
|
|
|
|
else
|
|
|
|
flightBatteryData.EstimatedFlightTime = 9999;
|
2011-02-19 19:56:52 +01:00
|
|
|
|
|
|
|
//generate alarms where needed...
|
2012-07-08 13:16:05 +02:00
|
|
|
if ((flightBatteryData.Voltage<=0) && (flightBatteryData.Current<=0))
|
|
|
|
{
|
|
|
|
//FIXME: There's no guarantee that a floating ADC will give 0. So this
|
|
|
|
// check might fail, even when there's nothing attached.
|
2011-02-19 19:56:52 +01:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_BATTERY, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_FLIGHTTIME, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-08 13:16:05 +02:00
|
|
|
// FIXME: should make the timer alarms user configurable
|
|
|
|
if (flightBatteryData.EstimatedFlightTime < 30)
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_FLIGHTTIME, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
else if (flightBatteryData.EstimatedFlightTime < 120)
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_FLIGHTTIME, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
else
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_FLIGHTTIME);
|
|
|
|
|
|
|
|
// FIXME: should make the battery voltage detection dependent on battery type.
|
|
|
|
/*Not so sure. Some users will want to run their batteries harder than others, so it should be the user's choice. [KDS]*/
|
2011-03-19 19:45:40 +01:00
|
|
|
if (flightBatteryData.Voltage < batterySettings.VoltageThresholds[FLIGHTBATTERYSETTINGS_VOLTAGETHRESHOLDS_ALARM])
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_BATTERY, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
else if (flightBatteryData.Voltage < batterySettings.VoltageThresholds[FLIGHTBATTERYSETTINGS_VOLTAGETHRESHOLDS_WARNING])
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_BATTERY, SYSTEMALARMS_ALARM_WARNING);
|
2012-07-08 13:16:05 +02:00
|
|
|
else
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_BATTERY);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2012-06-16 02:43:34 +02:00
|
|
|
|
2011-02-19 19:56:52 +01:00
|
|
|
FlightBatteryStateSet(&flightBatteryData);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|