2020-10-20 19:41:31 -03:00
|
|
|
#include "SIM_BattMonitor_SMBus_Generic.h"
|
|
|
|
|
|
|
|
SITL::SIM_BattMonitor_SMBus_Generic::SIM_BattMonitor_SMBus_Generic() :
|
|
|
|
SIM_BattMonitor_SMBus()
|
|
|
|
{
|
2021-03-16 02:02:25 -03:00
|
|
|
|
|
|
|
const char *manufacturer_name = "sitl_smbus_generic";
|
|
|
|
set_block(SMBusBattDevReg::MANUFACTURE_NAME, manufacturer_name);
|
|
|
|
|
2020-10-20 19:41:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SITL::SIM_BattMonitor_SMBus_Generic::init()
|
|
|
|
{
|
|
|
|
switch (cellcount()) {
|
2021-06-08 02:22:42 -03:00
|
|
|
case 14:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell14", SMBusBattGenericDevReg::CELL14, SITL::I2CRegisters::RegMode::RDONLY);
|
2021-06-08 02:22:42 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 13:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell13", SMBusBattGenericDevReg::CELL13, SITL::I2CRegisters::RegMode::RDONLY);
|
2021-06-08 02:22:42 -03:00
|
|
|
FALLTHROUGH;
|
2020-10-20 19:41:31 -03:00
|
|
|
case 12:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell12", SMBusBattGenericDevReg::CELL12, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 11:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell11", SMBusBattGenericDevReg::CELL11, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 10:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell10", SMBusBattGenericDevReg::CELL10, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 9:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell9", SMBusBattGenericDevReg::CELL9, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 8:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell8", SMBusBattGenericDevReg::CELL8, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 7:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell7", SMBusBattGenericDevReg::CELL7, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 6:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell6", SMBusBattGenericDevReg::CELL6, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 5:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell5", SMBusBattGenericDevReg::CELL5, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 4:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell4", SMBusBattGenericDevReg::CELL4, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 3:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell3", SMBusBattGenericDevReg::CELL3, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 2:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell2", SMBusBattGenericDevReg::CELL2, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case 1:
|
2021-10-11 02:04:21 -03:00
|
|
|
add_register("Cell1", SMBusBattGenericDevReg::CELL1, SITL::I2CRegisters::RegMode::RDONLY);
|
2020-10-20 19:41:31 -03:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
AP_HAL::panic("Bad cellcount %u", cellcount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SITL::SIM_BattMonitor_SMBus_Generic::update(const class Aircraft &aircraft)
|
|
|
|
{
|
|
|
|
SIM_BattMonitor_SMBus::update(aircraft);
|
|
|
|
|
|
|
|
// pretend to have three cells connected
|
2021-06-08 02:22:42 -03:00
|
|
|
const float millivolts = aircraft.get_battery_voltage() * 1000.0f;
|
|
|
|
uint16_t value_even = -1;
|
|
|
|
uint16_t value_odd = -1;
|
|
|
|
const uint8_t _connected_cells = connected_cells();
|
|
|
|
if (millivolts > 0) {
|
|
|
|
const float volts_per_cell = millivolts/float(_connected_cells);
|
|
|
|
value_even = uint16_t(volts_per_cell - 100.0f);
|
|
|
|
value_odd = uint16_t(volts_per_cell + 100.0f);
|
|
|
|
}
|
|
|
|
switch (_connected_cells) {
|
|
|
|
case 14:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL14, value_even);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 13:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL13, value_odd);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 12:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL12, value_even);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 11:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL11, value_odd);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 10:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL10, value_even);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 9:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL9, value_odd);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 8:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL8, value_even);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 7:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL7, value_odd);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 6:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL6, value_even);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 5:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL5, value_odd);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 4:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL4, value_even);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 3:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL3, value_odd);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 2:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL2, value_even);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case 1:
|
|
|
|
set_register(SMBusBattGenericDevReg::CELL1, value_odd);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
AP_HAL::panic("Bad connected_cellcount %u", _connected_cells);
|
|
|
|
}
|
2020-10-20 19:41:31 -03:00
|
|
|
}
|