2015-07-23 06:16:37 -03:00
|
|
|
/*
|
|
|
|
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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#include "AP_BattMonitor_config.h"
|
2015-07-23 06:16:37 -03:00
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#if AP_BATTERY_BEBOP_ENABLED
|
2015-07-23 06:16:37 -03:00
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_HAL_Linux/RCOutput_Bebop.h>
|
2016-06-06 19:14:35 -03:00
|
|
|
#include <AP_HAL_Linux/RCOutput_Disco.h>
|
2015-07-23 06:16:37 -03:00
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#include "AP_BattMonitor_Bebop.h"
|
|
|
|
|
2015-07-23 06:16:37 -03:00
|
|
|
#define BATTERY_VOLTAGE_COMPENSATION_LANDED (0.2f)
|
|
|
|
|
2016-07-29 16:14:02 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
|
|
|
using namespace Linux;
|
|
|
|
|
2015-07-23 06:16:37 -03:00
|
|
|
/* polynomial compensation coefficients */
|
|
|
|
static const float bat_comp_polynomial_coeffs[5] = {
|
|
|
|
-1.2471059149657287e-16f,
|
|
|
|
3.2072883440944087e-12f,
|
|
|
|
-3.3012241016211356e-08f,
|
|
|
|
1.4612693130825659e-04f,
|
|
|
|
-1.9236755589522961e-01f
|
|
|
|
};
|
|
|
|
|
|
|
|
/* battery percent lookup table */
|
|
|
|
static const struct {
|
|
|
|
float voltage;
|
|
|
|
float percent;
|
2016-09-21 00:48:53 -03:00
|
|
|
} bat_lut[] = {
|
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
|
|
|
|
{9.5, 0},
|
|
|
|
{11.04, 5},
|
|
|
|
{11.11, 10},
|
|
|
|
{11.21, 15},
|
|
|
|
{11.3, 25},
|
|
|
|
{11.4, 45},
|
|
|
|
{11.6, 55},
|
|
|
|
{11.9, 79},
|
|
|
|
{12.02, 84},
|
|
|
|
{12.11, 88},
|
|
|
|
{12.19, 91},
|
|
|
|
{12.26, 94},
|
|
|
|
{12.35, 96},
|
|
|
|
{12.45, 98},
|
2016-09-21 04:37:32 -03:00
|
|
|
{12.5, 100}
|
2016-09-21 00:48:53 -03:00
|
|
|
#else
|
|
|
|
// bebop
|
2015-07-23 06:16:37 -03:00
|
|
|
{10.50f, 0.0f},
|
|
|
|
{10.741699f, 2.6063901f},
|
|
|
|
{10.835779f, 5.1693798f},
|
|
|
|
{10.867705f, 7.7323696f},
|
|
|
|
{10.900651f, 10.295359f},
|
|
|
|
{11.008754f, 20.547318f},
|
|
|
|
{11.148267f, 38.488246f},
|
|
|
|
{11.322504f, 53.866185f},
|
|
|
|
{11.505738f, 66.681133f},
|
|
|
|
{11.746556f, 79.496082f},
|
|
|
|
{12.110226f, 94.874021f},
|
|
|
|
{12.3f, 100.0f }
|
2016-09-21 00:48:53 -03:00
|
|
|
#endif
|
2015-07-23 06:16:37 -03:00
|
|
|
};
|
2016-09-21 00:48:53 -03:00
|
|
|
#define BATTERY_PERCENT_LUT_SIZE ARRAY_SIZE(bat_lut)
|
|
|
|
|
2015-07-23 06:16:37 -03:00
|
|
|
void AP_BattMonitor_Bebop::init(void)
|
|
|
|
{
|
|
|
|
_battery_voltage_max = bat_lut[BATTERY_PERCENT_LUT_SIZE - 1].voltage;
|
|
|
|
_prev_vbat_raw = bat_lut[BATTERY_PERCENT_LUT_SIZE - 1].voltage;
|
|
|
|
_prev_vbat = bat_lut[BATTERY_PERCENT_LUT_SIZE - 1].voltage;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AP_BattMonitor_Bebop::_filter_voltage(float vbat_raw)
|
|
|
|
{
|
|
|
|
static const float a[2] = {
|
|
|
|
1.0f, -9.9686333183343789e-01f
|
|
|
|
};
|
|
|
|
static const float b[2] = {
|
|
|
|
1.5683340832810533e-03f, 1.5683340832810533e-03f
|
|
|
|
};
|
|
|
|
float vbat;
|
|
|
|
static int only_once = 1;
|
|
|
|
|
|
|
|
/* on first time reset filter with first raw value */
|
|
|
|
if (only_once) {
|
|
|
|
vbat = vbat_raw;
|
|
|
|
_prev_vbat_raw = vbat_raw;
|
|
|
|
_prev_vbat = vbat_raw;
|
|
|
|
only_once = 0;
|
|
|
|
} else if (vbat_raw > 0.0f) {
|
|
|
|
/* 1st order fitler */
|
|
|
|
vbat = b[0] * vbat_raw +
|
|
|
|
b[1] * _prev_vbat_raw - a[1] * _prev_vbat;
|
|
|
|
_prev_vbat_raw = vbat_raw;
|
|
|
|
_prev_vbat = vbat;
|
|
|
|
} else {
|
|
|
|
vbat = _prev_vbat;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vbat;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AP_BattMonitor_Bebop::_compute_compensation(const uint16_t *rpm,
|
|
|
|
float vbat_raw)
|
|
|
|
{
|
|
|
|
float vbat, res;
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
vbat = vbat_raw;
|
|
|
|
for (i = 0; i < BEBOP_BLDC_MOTORS_NUM; i++) {
|
|
|
|
res = 0;
|
2015-07-23 06:14:35 -03:00
|
|
|
for (j = 0; j < ARRAY_SIZE(bat_comp_polynomial_coeffs); j++)
|
2015-07-23 06:16:37 -03:00
|
|
|
res = res * rpm[i] + bat_comp_polynomial_coeffs[j];
|
|
|
|
|
|
|
|
vbat -= res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vbat;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AP_BattMonitor_Bebop::_compute_battery_percentage(float vbat)
|
|
|
|
{
|
|
|
|
float percent = 0.0f;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (vbat <= bat_lut[0].voltage) {
|
|
|
|
percent = 0.0f;
|
|
|
|
} else if (vbat >= bat_lut[BATTERY_PERCENT_LUT_SIZE - 1].voltage) {
|
|
|
|
percent = 100.0f;
|
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
while (vbat >= bat_lut[i].voltage)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
percent += bat_lut[i - 1].percent +
|
|
|
|
(vbat - bat_lut[i - 1].voltage) *
|
|
|
|
(bat_lut[i].percent - bat_lut[i - 1].percent) /
|
|
|
|
(bat_lut[i].voltage - bat_lut[i - 1].voltage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_BattMonitor_Bebop::read(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint32_t tnow;
|
|
|
|
BebopBLDC_ObsData data;
|
2016-10-22 13:47:59 -03:00
|
|
|
float capacity, remaining, vbat, vbat_raw;
|
2015-07-23 06:16:37 -03:00
|
|
|
|
2016-06-06 19:14:35 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP
|
2015-10-20 18:21:51 -03:00
|
|
|
auto rcout = Linux::RCOutput_Bebop::from(hal.rcout);
|
2016-06-06 19:14:35 -03:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
|
|
|
|
auto rcout = Linux::RCOutput_Disco::from(hal.rcout);
|
|
|
|
#endif
|
2015-11-19 23:08:43 -04:00
|
|
|
tnow = AP_HAL::micros();
|
2015-07-23 06:16:37 -03:00
|
|
|
|
|
|
|
ret = rcout->read_obs_data(data);
|
|
|
|
if (ret < 0) {
|
|
|
|
_state.healthy = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get battery voltage observed by cypress */
|
2022-03-11 13:31:56 -04:00
|
|
|
vbat_raw = (float)data.batt_mv * 0.001f;
|
2015-07-23 06:16:37 -03:00
|
|
|
|
|
|
|
/* do not compute battery status on ramping or braking transition */
|
|
|
|
if (data.status == BEBOP_BLDC_STATUS_RAMPING ||
|
|
|
|
data.status == BEBOP_BLDC_STATUS_STOPPING)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* if motors are spinning compute polynomial compensation */
|
|
|
|
if (data.status == BEBOP_BLDC_STATUS_SPINNING_1 ||
|
|
|
|
data.status == BEBOP_BLDC_STATUS_SPINNING_2) {
|
|
|
|
vbat = _compute_compensation(data.rpm, vbat_raw);
|
|
|
|
/* otherwise compute constant compensation */
|
|
|
|
} else {
|
|
|
|
vbat = vbat_raw - BATTERY_VOLTAGE_COMPENSATION_LANDED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* filter raw value */
|
|
|
|
vbat = _filter_voltage(vbat);
|
|
|
|
|
|
|
|
/* ensure battery voltage/percent will not grow up during use */
|
|
|
|
if (vbat > _battery_voltage_max) {
|
|
|
|
vbat = _battery_voltage_max;
|
|
|
|
} else if (vbat < 0.0f) {
|
|
|
|
vbat = 0.0f;
|
|
|
|
_battery_voltage_max = 0.0f;
|
|
|
|
} else {
|
|
|
|
_battery_voltage_max = vbat;
|
|
|
|
}
|
|
|
|
|
2016-10-22 13:47:59 -03:00
|
|
|
/* compute remaining battery percent and get battery capacity */
|
2015-07-23 06:16:37 -03:00
|
|
|
remaining = _compute_battery_percentage(vbat);
|
2017-10-27 02:36:49 -03:00
|
|
|
capacity = (float) _params._pack_capacity;
|
2015-07-23 06:16:37 -03:00
|
|
|
|
|
|
|
/* fillup battery state */
|
|
|
|
_state.voltage = vbat;
|
|
|
|
_state.last_time_micros = tnow;
|
|
|
|
_state.healthy = true;
|
2022-03-11 13:31:56 -04:00
|
|
|
_state.consumed_mah = capacity - (remaining * capacity) * 0.01f;
|
2015-07-23 06:16:37 -03:00
|
|
|
}
|
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#endif // AP_BATTERY_BEBOP_ENABLED
|