diff --git a/flight/OpenPilot/Modules/Battery/battery.c b/flight/OpenPilot/Modules/Battery/battery.c new file mode 100644 index 000000000..f4f7779ec --- /dev/null +++ b/flight/OpenPilot/Modules/Battery/battery.c @@ -0,0 +1,118 @@ +/** + ****************************************************************************** + * + * @file attitude.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Module to read the attitude solution from the AHRS on a periodic basis. + * + * @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" + +// +// Configuration +// +#define DEBUG_PORT PIOS_COM_TELEM_RF +#define STACK_SIZE 1024 +#define TASK_PRIORITY (tskIDLE_PRIORITY + 3) +#define SAMPLE_PERIOD_MS 500 +//#define ENABLE_DEBUG_MSG + +#ifdef ENABLE_DEBUG_MSG + #define DEBUG_MSG(format, ...) PIOS_COM_SendFormattedString(DEBUG_PORT, format, ## __VA_ARGS__) +#else + #define DEBUG_MSG(format, ...) +#endif + + + +// Private types + +// Private variables +static xTaskHandle taskHandle; + +// Private functions +static void task(void* parameters); + +/** + * Initialise the module, called on startup + * \returns 0 on success or -1 if initialisation failed + */ +int32_t BatteryInitialize(void) +{ + // Start main task + xTaskCreate(task, (signed char*)"Battery", STACK_SIZE, NULL, TASK_PRIORITY, &taskHandle); + + return 0; +} + +/** + * Module thread, should not return. + */ +static void task(void* parameters) +{ + uint cnt = 0; + uint32_t mAs = 0; + portTickType lastSysTime; + FlightBatteryStateData flightBatteryData; + + PIOS_COM_ChangeBaud(DEBUG_PORT, 57600); + + DEBUG_MSG("Battery Started\n"); + + lastSysTime = xTaskGetTickCount(); + while(1) + { + // TODO: Compare with floating point calculations + + uint32_t mV, mA; + + mV = PIOS_ADC_PinGet(2)*1257/100; + mA = (PIOS_ADC_PinGet(1)-28)*4871/100; + mAs += mA * SAMPLE_PERIOD_MS / 1000; // FIXME: Use real time between samples + + DEBUG_MSG("%03d %06d => %06dmV %06d => %06dmA %06dmAh\n", cnt, PIOS_ADC_PinGet(2), mV, PIOS_ADC_PinGet(1), mA, mAs/3600); + cnt++; + + flightBatteryData.Voltage = mV; + flightBatteryData.Current = mA; + flightBatteryData.ConsumedEnergy = mAs / 3600; + FlightBatteryStateSet(&flightBatteryData); + + vTaskDelayUntil(&lastSysTime, SAMPLE_PERIOD_MS / portTICK_RATE_MS); + } +} diff --git a/flight/OpenPilot/Modules/Battery/inc/battery.h b/flight/OpenPilot/Modules/Battery/inc/battery.h new file mode 100644 index 000000000..33211d84e --- /dev/null +++ b/flight/OpenPilot/Modules/Battery/inc/battery.h @@ -0,0 +1,34 @@ +/** + ****************************************************************************** + * + * @file attitude.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Module to read the attitude solution from the AHRS on a periodic basis. + * + * @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 + */ +#ifndef BATTERY_H +#define BATTERY_H + +#include "openpilot.h" + +int32_t BatteryInitialize(void); + +#endif // BATTERY_H +