2021-01-06 20:51:50 -04:00
# include "AP_BattMonitor_Backend.h"
# include <AP_Logger/AP_Logger.h>
extern const AP_HAL : : HAL & hal ;
// Write BAT data packet(s)
void AP_BattMonitor_Backend : : Log_Write_BAT ( const uint8_t instance , const uint64_t time_us ) const
{
bool has_curr = has_current ( ) ;
2021-08-06 14:39:51 -03:00
uint8_t percent = - 1 ;
IGNORE_RETURN ( capacity_remaining_pct ( percent ) ) ;
2021-01-06 20:51:50 -04:00
2023-09-02 07:36:40 -03:00
float temperature ;
int16_t temperature_cd = 0 ;
if ( get_temperature ( temperature ) ) {
temperature_cd = temperature * 100.0 ;
}
2021-01-06 20:51:50 -04:00
const struct log_BAT pkt {
LOG_PACKET_HEADER_INIT ( LOG_BAT_MSG ) ,
time_us : time_us ,
instance : instance ,
voltage : _state . voltage ,
voltage_resting : _state . voltage_resting_estimate ,
current_amps : has_curr ? _state . current_amps : AP : : logger ( ) . quiet_nanf ( ) ,
current_total : has_curr ? _state . consumed_mah : AP : : logger ( ) . quiet_nanf ( ) ,
consumed_wh : has_curr ? _state . consumed_wh : AP : : logger ( ) . quiet_nanf ( ) ,
2023-09-02 07:36:40 -03:00
temperature : temperature_cd ,
2021-06-03 19:57:49 -03:00
resistance : _state . resistance ,
2021-08-06 14:39:51 -03:00
rem_percent : percent ,
2023-08-01 18:14:06 -03:00
health : _state . healthy
2021-01-06 20:51:50 -04:00
} ;
AP : : logger ( ) . WriteBlock ( & pkt , sizeof ( pkt ) ) ;
}
// Write BCL data packet if has_cell_voltages
void AP_BattMonitor_Backend : : Log_Write_BCL ( const uint8_t instance , const uint64_t time_us ) const
{
if ( ! has_cell_voltages ( ) ) {
return ;
}
2021-06-08 02:21:10 -03:00
2021-01-06 20:51:50 -04:00
struct log_BCL cell_pkt {
LOG_PACKET_HEADER_INIT ( LOG_BCL_MSG ) ,
time_us : time_us ,
instance : instance ,
voltage : _state . voltage
} ;
2021-06-08 02:21:10 -03:00
// we pack the entire BCL message - we must have at least that
// many supported cells or the loop below will over-read
2021-06-27 19:41:51 -03:00
static_assert ( ARRAY_SIZE ( _state . cell_voltages . cells ) > = ARRAY_SIZE ( cell_pkt . cell_voltages ) , " must have at least ARRAY_SIZE(log_BCL.cell_voltages) cells " ) ;
2021-06-08 02:21:10 -03:00
2020-09-23 05:15:37 -03:00
for ( uint8_t i = 0 ; i < ARRAY_SIZE ( cell_pkt . cell_voltages ) ; i + + ) {
cell_pkt . cell_voltages [ i ] = _state . cell_voltages . cells [ i ] + 1 ; // add 1mv
2021-01-06 20:51:50 -04:00
}
AP : : logger ( ) . WriteBlock ( & cell_pkt , sizeof ( cell_pkt ) ) ;
2021-06-08 02:21:10 -03:00
# if AP_BATT_MONITOR_CELLS_MAX > 12
2020-09-23 05:15:37 -03:00
if ( _state . cell_voltages . cells [ 12 ] ! = UINT16_MAX | | _state . cell_voltages . cells [ 13 ] ! = UINT16_MAX )
{
2021-06-08 02:21:10 -03:00
// @LoggerMessage: BCL2
// @Description: Battery cell voltage information
// @Field: TimeUS: Time since system startup
// @Field: Instance: battery instance number
// @Field: V13: thirteenth cell voltage
// @Field: V14: fourteenth cell voltage
2021-08-17 06:57:41 -03:00
AP : : logger ( ) . WriteStreaming (
2021-06-08 02:21:10 -03:00
" BCL2 " ,
" TimeUS,Instance,V13,V14 " ,
" s#vv " ,
" F-CC " ,
" QBHH " ,
time_us ,
instance ,
_state . cell_voltages . cells [ ARRAY_SIZE ( cell_pkt . cell_voltages ) + 0 ] + 1 , // add 1mv
_state . cell_voltages . cells [ ARRAY_SIZE ( cell_pkt . cell_voltages ) + 1 ] + 1 // add 1mv
) ;
2020-09-23 05:15:37 -03:00
}
2021-06-08 02:21:10 -03:00
# endif
2021-01-06 20:51:50 -04:00
}