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"
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Configuration
|
|
|
|
//
|
2011-02-19 19:56:52 +01:00
|
|
|
#define SAMPLE_PERIOD_MS 500
|
2010-12-28 06:46:14 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
//#define ENABLE_DEBUG_MSG
|
|
|
|
|
|
|
|
#ifdef ENABLE_DEBUG_MSG
|
2011-02-19 19:56:52 +01:00
|
|
|
#define DEBUG_PORT PIOS_COM_GPS
|
2010-09-27 09:28:34 +02:00
|
|
|
#define DEBUG_MSG(format, ...) PIOS_COM_SendFormattedString(DEBUG_PORT, format, ## __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DEBUG_MSG(format, ...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
|
|
|
|
// 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-05-15 07:38:11 +02:00
|
|
|
FlightBatteryStateInitialize();
|
|
|
|
FlightBatterySettingsInitialize();
|
2011-06-18 18:59:02 +02:00
|
|
|
|
2011-02-19 19:56:52 +01:00
|
|
|
static UAVObjEvent ev;
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2011-03-19 19:45:40 +01:00
|
|
|
FlightBatterySettingsData batterySettings;
|
2011-02-24 06:05:17 +01:00
|
|
|
static float dT = SAMPLE_PERIOD_MS / 1000;
|
2011-02-19 19:56:52 +01:00
|
|
|
float energyRemaining;
|
|
|
|
|
2011-03-19 19:45:40 +01:00
|
|
|
FlightBatterySettingsGet(&batterySettings);
|
2011-02-19 19:56:52 +01:00
|
|
|
|
|
|
|
//calculate the battery parameters
|
2012-05-15 10:08:18 +02:00
|
|
|
flightBatteryData.Voltage = ((float)PIOS_ADC_PinGet(1)) * batterySettings.SensorCalibrations[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONS_VOLTAGEFACTOR]; //in Volts
|
|
|
|
flightBatteryData.Current = ((float)PIOS_ADC_PinGet(0)) * batterySettings.SensorCalibrations[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONS_CURRENTFACTOR]; //in Amps
|
2012-05-15 07:38:11 +02:00
|
|
|
|
2011-02-19 19:56:52 +01:00
|
|
|
flightBatteryData.ConsumedEnergy += (flightBatteryData.Current * 1000.0 * dT / 3600.0) ;//in mAh
|
|
|
|
|
|
|
|
if (flightBatteryData.Current > flightBatteryData.PeakCurrent)flightBatteryData.PeakCurrent = flightBatteryData.Current; //in Amps
|
|
|
|
flightBatteryData.AvgCurrent=(flightBatteryData.AvgCurrent*0.8)+(flightBatteryData.Current*0.2); //in Amps
|
|
|
|
|
|
|
|
//sanity checks
|
|
|
|
if (flightBatteryData.AvgCurrent<0)flightBatteryData.AvgCurrent=0.0;
|
|
|
|
if (flightBatteryData.PeakCurrent<0)flightBatteryData.PeakCurrent=0.0;
|
|
|
|
if (flightBatteryData.ConsumedEnergy<0)flightBatteryData.ConsumedEnergy=0.0;
|
|
|
|
|
2011-03-19 19:45:40 +01:00
|
|
|
energyRemaining = batterySettings.Capacity - flightBatteryData.ConsumedEnergy; // in mAh
|
2011-02-19 19:56:52 +01:00
|
|
|
flightBatteryData.EstimatedFlightTime = ((energyRemaining / (flightBatteryData.AvgCurrent*1000.0))*3600.0);//in Sec
|
|
|
|
|
|
|
|
//generate alarms where needed...
|
|
|
|
if ((flightBatteryData.Voltage<=0)&&(flightBatteryData.Current<=0))
|
|
|
|
{
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_BATTERY, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_FLIGHTTIME, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (flightBatteryData.EstimatedFlightTime < 30) AlarmsSet(SYSTEMALARMS_ALARM_FLIGHTTIME, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
else if (flightBatteryData.EstimatedFlightTime < 60) AlarmsSet(SYSTEMALARMS_ALARM_FLIGHTTIME, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
else AlarmsClear(SYSTEMALARMS_ALARM_FLIGHTTIME);
|
|
|
|
|
|
|
|
// FIXME: should make the battery voltage detection dependent on battery type.
|
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);
|
2011-02-19 19:56:52 +01:00
|
|
|
else AlarmsClear(SYSTEMALARMS_ALARM_BATTERY);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2011-02-19 19:56:52 +01:00
|
|
|
|
|
|
|
FlightBatteryStateSet(&flightBatteryData);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|