38 #include "flightbatterystate.h"
39 #include "flightbatterysettings.h"
40 #include "modulesettings.h"
45 #define STACK_SIZE_BYTES 624
46 #define TASK_PRIORITY PIOS_THREAD_PRIO_LOW
47 #define SAMPLE_PERIOD_MS 250
81 if (FlightBatterySettingsInitialize() == -1) {
85 #ifdef MODULE_Battery_BUILTIN
88 uint8_t module_state[MODULESETTINGS_ADMINSTATE_NUMELEM];
89 ModuleSettingsAdminStateGet(module_state);
90 if (module_state[MODULESETTINGS_ADMINSTATE_BATTERY] == MODULESETTINGS_ADMINSTATE_ENABLED) {
97 if (FlightBatteryStateInitialize() == -1) {
114 bool cells_calculated =
false;
115 int cells_holddown = 0;
118 FlightBatteryStateData flightBatteryData;
119 FlightBatterySettingsData batterySettings;
121 FlightBatteryStateGet(&flightBatteryData);
128 float energyRemaining;
132 FlightBatterySettingsGet(&batterySettings);
142 cells_calculated =
false;
145 bool adc_pin_invalid =
false;
146 bool adc_offset_invalid =
false;
151 float scaled_voltage = 0.0f;
154 if(adc_voltage < 0.0
f)
155 adc_pin_invalid =
true;
158 scaled_voltage = (adc_voltage * 1000.0f
159 / batterySettings.SensorCalibrationFactor[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONFACTOR_VOLTAGE])
160 + batterySettings.SensorCalibrationOffset[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONOFFSET_VOLTAGE];
164 if (scaled_voltage < 0.0
f) {
165 scaled_voltage = 0.0f;
166 adc_offset_invalid =
true;
167 }
else if (batterySettings.MaxCellVoltage > 0.0f && scaled_voltage > 2.5f) {
168 if (!cells_calculated) {
169 cells = ((scaled_voltage / batterySettings.MaxCellVoltage) + 0.9
f);
177 cells_calculated =
true;
178 flightBatteryData.DetectedCellCount = cells;
182 cells_calculated =
false;
185 if (!cells_calculated) {
186 cells = batterySettings.NbCells;
187 flightBatteryData.DetectedCellCount = 0;
191 flightBatteryData.Voltage = scaled_voltage;
194 if (flightBatteryData.Voltage < (batterySettings.CellVoltageThresholds[FLIGHTBATTERYSETTINGS_CELLVOLTAGETHRESHOLDS_ALARM] * cells))
195 AlarmsSet(SYSTEMALARMS_ALARM_BATTERY, SYSTEMALARMS_ALARM_CRITICAL);
196 else if (flightBatteryData.Voltage < (batterySettings.CellVoltageThresholds[FLIGHTBATTERYSETTINGS_CELLVOLTAGETHRESHOLDS_WARNING] * cells))
197 AlarmsSet(SYSTEMALARMS_ALARM_BATTERY, SYSTEMALARMS_ALARM_WARNING);
201 flightBatteryData.Voltage = 0;
207 float scaled_current = 0.0f;
210 if(adc_voltage < 0.0
f)
211 adc_pin_invalid =
true;
214 scaled_current = (adc_voltage * 1000.0f
215 / batterySettings.SensorCalibrationFactor[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONFACTOR_CURRENT])
216 + batterySettings.SensorCalibrationOffset[FLIGHTBATTERYSETTINGS_SENSORCALIBRATIONOFFSET_CURRENT];
220 if(scaled_current < 0.0f) {
221 scaled_current = 0.0f;
222 adc_offset_invalid =
true;
226 flightBatteryData.Current = scaled_current;
228 if (flightBatteryData.Current > flightBatteryData.PeakCurrent)
229 flightBatteryData.PeakCurrent = flightBatteryData.Current;
231 flightBatteryData.ConsumedEnergy += (flightBatteryData.Current * dT * 1000.0f / 3600.0f);
234 float alpha = 1.0f - dT / (dT + 2.0f);
235 flightBatteryData.AvgCurrent = alpha * flightBatteryData.AvgCurrent + (1 - alpha) * flightBatteryData.Current;
238 energyRemaining = batterySettings.Capacity - flightBatteryData.ConsumedEnergy;
243 alpha = 1.0f - dT / (dT + 12.0f);
250 flightBatteryData.EstimatedFlightTime = 9999;
253 if ((batterySettings.FlightTimeThresholds[FLIGHTBATTERYSETTINGS_FLIGHTTIMETHRESHOLDS_ALARM] > 0)
254 && (flightBatteryData.EstimatedFlightTime < batterySettings.FlightTimeThresholds[FLIGHTBATTERYSETTINGS_FLIGHTTIMETHRESHOLDS_ALARM]))
255 AlarmsSet(SYSTEMALARMS_ALARM_FLIGHTTIME, SYSTEMALARMS_ALARM_CRITICAL);
256 else if ((batterySettings.FlightTimeThresholds[FLIGHTBATTERYSETTINGS_FLIGHTTIMETHRESHOLDS_WARNING] > 0)
257 && (flightBatteryData.EstimatedFlightTime < batterySettings.FlightTimeThresholds[FLIGHTBATTERYSETTINGS_FLIGHTTIMETHRESHOLDS_WARNING]))
258 AlarmsSet(SYSTEMALARMS_ALARM_FLIGHTTIME, SYSTEMALARMS_ALARM_WARNING);
262 flightBatteryData.Current = 0;
266 AlarmsSet(SYSTEMALARMS_ALARM_ADC, SYSTEMALARMS_ALARM_CRITICAL);
267 else if(adc_offset_invalid)
268 AlarmsSet(SYSTEMALARMS_ALARM_ADC, SYSTEMALARMS_ALARM_WARNING);
270 AlarmsSet(SYSTEMALARMS_ALARM_ADC, SYSTEMALARMS_ALARM_OK);
272 AlarmsSet(SYSTEMALARMS_ALARM_ADC, SYSTEMALARMS_ALARM_UNINITIALISED);
274 FlightBatteryStateSet(&flightBatteryData);
uint32_t PIOS_Thread_Systime(void)
static int8_t currentADCPin
static int8_t voltageADCPin
void UAVObjCbSetFlag(const UAVObjEvent *objEv, void *ctx, void *obj, int len)
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
static int32_t BatteryStart(void)
#define MODULE_INITCALL(ifn, sfn)
static float avg_current_lpf_for_time
static bool module_enabled
static void batteryTask(void *parameters)
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
float PIOS_ADC_GetChannelVolt(uint32_t channel)
int32_t BatteryInitialize(void)
Includes PiOS and core architecture components.
int32_t AlarmsClear(SystemAlarmsAlarmElem alarm)
static struct pios_thread * batteryTaskHandle
static uint32_t lastSysTime
static bool battery_settings_updated
if(BaroAltitudeHandle()!=NULL)