Hi,
I’m working on integrating my smart battery with a Pixhawk board and have encountered a challenge that I need some help with. I can successfully read the battery values using the SMBus Generic Monitor under Battery Monitor. However, I’m facing an issue with the current measurement and other parameters dependent on it, such as battery capacity.
My battery uses current scaling to measure currents exceeding 32 Amps. For this particular battery, the current scaling factor is set to 4. This means that if the battery supplies 100 Amps, it will be read as 25 Amps (100/4).
The Capacity Scalar function in the ardupilot firmware can be used to achieve this, but it is set to 1 by default, and using the Maxell library, the Capacity Scalar is hardcoded to 2.While I could manually set this to 4 to achieve the actual current measurement instead of the scaled value, I prefer not to hardcode this number. I may use different current scaling values for other batteries, and having to modify the code for each battery is not ideal.
There is a way to read the current scaling via SMBUS communication, which I’ve previously done on STM32 by using its HAL and via the Wire Library in the Arduino framework. The process involves writing a two-byte integer to the address 0x44, which is the ManufacturerBlockAccess() for my battery, and then reading the data returned from 0x44. The data returned is 34 bytes long, with the fourth byte containing the current scaling value.
I am using the following piece of code to extract the value for the moment
uint8_t data_to_Write[4] = { 0x44, 0x02,0xe8,0x4a};
uint8_t data_to_Read[34];
_dev->transfer(data_to_Write, sizeof(data_to_Write), nullptr, 0);
_dev->read_registers(0x44,data_to_Read,sizeof(data_to_Read));
if (data_to_Read[3] == 0)
{
return 1;
}
else
{
return data_to_Read[3];
}
The issue I’m facing is that while the board correctly reads the Current Scaling most of the time, it occasionally reads a random number. As a result, the “battery_usedmah” and “battery_remaining” (SoC of the battery) values fluctuate unexpectedly. Any idea what could be causing this?
Additionally, I would like to save these changes into a set of header and .cpp files and push the code to the main repository. If anyone could assist me with this, it would be greatly appreciated.
Thank you for your help.