mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-29 14:52:12 +01:00
First version of battery module
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@882 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
c498611c60
commit
6ef11148b5
118
flight/OpenPilot/Modules/Battery/battery.c
Normal file
118
flight/OpenPilot/Modules/Battery/battery.c
Normal file
@ -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);
|
||||
}
|
||||
}
|
34
flight/OpenPilot/Modules/Battery/inc/battery.h
Normal file
34
flight/OpenPilot/Modules/Battery/inc/battery.h
Normal file
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user