Path: blob/master/libraries/AP_BattMonitor/AP_BattMonitor_AD7091R5.cpp
9552 views
#include "AP_BattMonitor_AD7091R5.h"12/**3* @brief You can use it to Read Current and voltage of 1-3 batteries from a ADC extender IC over I2C.4* AD7091R5 is a ADC extender and we are using it to read current and voltage of multiple batteries.5* Examples of Pin combination:6* 1)Pin 50 = Voltage 51,52,53 = Current. For 3 battery combination Voltage will be same accross.7* 2)Pin 50,51 = Voltage and Current Battery 1 - Pin 52,53 = Voltage and Current Battery 28* Only the First instance of Battery Monitor will be reading the values from IC over I2C.9* Make sure you understand the method of calculation used in this driver before using it.10* e.g. using pin 1 on IC to read voltage of 2 batteries and pin 2 and 3 to read current from individual battery.11* Pin number represents 50 = pin 1, 51 = pin 2 and so on 52, 5312* BATT2_Monitor = 24 , BATT3_Monitor = 2413* BATT2_VOLT_PIN = 50 , BATT3_VOLT_PIN = 5014* BATT2_CURR_PIN = 51 , BATT3_CURR_PIN = 5215*16*17*/1819#if AP_BATTERY_AD7091R5_ENABLED2021#include <AP_HAL/AP_HAL.h>22#include <AP_Common/AP_Common.h>23#include <AP_Math/AP_Math.h>2425//macro defines26#define AD7091R5_I2C_ADDR 0x2F // A0 and A1 tied to GND27#define AD7091R5_I2C_BUS 028#define AD7091R5_RESET 0x0229#define AD7091R5_RESULT_ADDR 0x0030#define AD7091R5_CHAN_ADDR 0x0131#define AD7091R5_CONF_ADDR 0x0232#define AD7091R5_CH_ID(x) ((x >> 5) & 0x03)33#define AD7091R5_RES_MASK 0x0F34#define AD7091R5_REF 3.3f35#define AD7091R5_RESOLUTION (float)409636#define AD7091R5_PERIOD_USEC 10000037#define AD7091R5_BASE_PIN 50383940extern const AP_HAL::HAL& hal;41const AP_Param::GroupInfo AP_BattMonitor_AD7091R5::var_info[] = {4243// @Param: VOLT_PIN44// @DisplayName: Battery Voltage sensing pin on the AD7091R5 Ic45// @Description: Sets the analog input pin that should be used for voltage monitoring on AD7091R5.46// @Values: -1:Disabled47// @Range: -1 12748// @User: Standard49// @RebootRequired: True50AP_GROUPINFO("VOLT_PIN", 56, AP_BattMonitor_AD7091R5, _volt_pin, 0),5152// @Param: CURR_PIN53// @DisplayName: Battery Current sensing pin54// @Description: Sets the analog input pin that should be used for Current monitoring on AD7091R5.55// @Values: -1:Disabled56// @Range: -1 12757// @User: Standard58// @RebootRequired: True59AP_GROUPINFO("CURR_PIN", 57, AP_BattMonitor_AD7091R5, _curr_pin, 0),6061// @Param: VOLT_MULT62// @DisplayName: Voltage Multiplier63// @Description: Used to convert the voltage of the voltage sensing pin (@PREFIX@VOLT_PIN) to the actual battery's voltage (pin_voltage * VOLT_MULT).64// @User: Advanced65AP_GROUPINFO("VOLT_MULT", 58, AP_BattMonitor_AD7091R5, _volt_multiplier, 0),6667// @Param: AMP_PERVLT68// @DisplayName: Amps per volt69// @Description: Number of amps that a 1V reading on the current sensor corresponds to.70// @Units: A/V71// @User: Standard72AP_GROUPINFO("AMP_PERVLT", 59, AP_BattMonitor_AD7091R5, _curr_amp_per_volt, 0),7374// @Param: AMP_OFFSET75// @DisplayName: AMP offset76// @Description: Voltage offset at zero current on current sensor77// @Units: V78// @User: Standard79AP_GROUPINFO("AMP_OFFSET", 60, AP_BattMonitor_AD7091R5, _curr_amp_offset, 0),8081// @Param: VLT_OFFSET82// @DisplayName: Volage offset83// @Description: Voltage offset on voltage pin. This allows for an offset due to a diode. This voltage is subtracted before the scaling is applied84// @Units: V85// @User: Advanced86AP_GROUPINFO("VLT_OFFSET", 61, AP_BattMonitor_AD7091R5, _volt_offset, 0),8788// CHECK/UPDATE INDEX TABLE IN AP_BattMonitor_Backend.cpp WHEN CHANGING OR ADDING PARAMETERS8990AP_GROUPEND91};929394//Variable initialised to read from first instance.95AP_BattMonitor_AD7091R5::AnalogData AP_BattMonitor_AD7091R5::_analog_data[AD7091R5_NO_OF_CHANNELS];96bool AP_BattMonitor_AD7091R5::_first = true;97bool AP_BattMonitor_AD7091R5::_health = false;9899/**100* @brief Construct a new ap battmonitor ad7091r5::ap battmonitor ad7091r5 object101*102* @param mon103* @param mon_state104* @param params105*/106AP_BattMonitor_AD7091R5::AP_BattMonitor_AD7091R5(AP_BattMonitor &mon,107AP_BattMonitor::BattMonitor_State &mon_state,108AP_BattMonitor_Params ¶ms) :109AP_BattMonitor_Backend(mon, mon_state, params)110{111AP_Param::setup_object_defaults(this, var_info);112_state.var_info = var_info;113}114115/**116* @brief probe and initialize the sensor and register call back117*118*/119void AP_BattMonitor_AD7091R5::init()120{121// voltage and current pins from params and check if there are in range122if (_volt_pin.get() >= AD7091R5_BASE_PIN && _volt_pin.get() <= AD7091R5_BASE_PIN + AD7091R5_NO_OF_CHANNELS &&123_curr_pin.get() >= AD7091R5_BASE_PIN && _curr_pin.get() <= AD7091R5_BASE_PIN + AD7091R5_NO_OF_CHANNELS) {124volt_buff_pt = _volt_pin.get() - AD7091R5_BASE_PIN;125curr_buff_pt = _curr_pin.get() - AD7091R5_BASE_PIN;126}127else{128return; //pin values are out of range129}130131// only the first instance read the i2c device132if (_first) {133_first = false;134// probe i2c device135_dev = hal.i2c_mgr->get_device_ptr(AD7091R5_I2C_BUS, AD7091R5_I2C_ADDR);136137if (_dev) {138WITH_SEMAPHORE(_dev->get_semaphore());139_dev->set_retries(10); // lots of retries during probe140//Reset and config device141if (_initialize()) {142_dev->set_retries(2); // drop to 2 retries for runtime143_dev->register_periodic_callback(AD7091R5_PERIOD_USEC, FUNCTOR_BIND_MEMBER(&AP_BattMonitor_AD7091R5::_read_adc, void));144}145}146}147}148149/**150* @brief read - read the voltage and curren151*152*/153void AP_BattMonitor_AD7091R5::read()154{155156WITH_SEMAPHORE(sem);157//copy global health status to all instances158_state.healthy = _health;159160//return if system not healthy161if (!_state.healthy) {162return;163}164165//voltage conversion166_state.voltage = (_data_to_volt(_analog_data[volt_buff_pt].data) - _volt_offset) * _volt_multiplier;167168//current amps conversion169_state.current_amps = (_data_to_volt(_analog_data[curr_buff_pt].data) - _curr_amp_offset) * _curr_amp_per_volt;170171// calculate time since last current read172uint32_t tnow = AP_HAL::micros();173uint32_t dt_us = tnow - _state.last_time_micros;174175// update total current drawn since startup176update_consumed(_state, dt_us);177178// record time179_state.last_time_micros = tnow;180}181182/**183* @brief read all four channels and store the results184*185*/186void AP_BattMonitor_AD7091R5::_read_adc()187{188uint8_t data[AD7091R5_NO_OF_CHANNELS*2];189//reset and reconfigure IC if health status is not good.190if (!_state.healthy) {191_initialize();192}193//read value194bool ret = _dev->transfer(nullptr, 0, data, sizeof(data));195WITH_SEMAPHORE(sem);196if (ret) {197for (int i=0; i<AD7091R5_NO_OF_CHANNELS; i++) {198uint8_t chan = AD7091R5_CH_ID(data[2*i]);199_analog_data[chan].data = ((uint16_t)(data[2*i]&AD7091R5_RES_MASK)<<8) | data[2*i+1];200}201_health = true;202} else {203_health = false;204}205}206207/**208* @brief config the adc209*210* @return true211* @return false212*/213bool AP_BattMonitor_AD7091R5::_initialize()214{215//reset the device216uint8_t data[3] = {AD7091R5_CONF_ADDR, AD7091R5_CONF_CMD | AD7091R5_RESET, AD7091R5_CONF_PDOWN0};217218if(_dev->transfer(data, sizeof(data), nullptr, 0)){219//command mode, use external 3.3 reference, all channels enabled, set address pointer register to read the adc results220uint8_t data_2[6] = {AD7091R5_CONF_ADDR, AD7091R5_CONF_CMD, AD7091R5_CONF_PDOWN0, AD7091R5_CHAN_ADDR, AD7091R5_CHAN_ALL, AD7091R5_RESULT_ADDR};221return _dev->transfer(data_2, sizeof(data_2), nullptr, 0);222}223return false;224}225226/**227* @brief convert binary reading to volts228*229* @param data230* @return float231*/232float AP_BattMonitor_AD7091R5::_data_to_volt(uint32_t data)233{234return (AD7091R5_REF/AD7091R5_RESOLUTION)*data;235}236237#endif // AP_BATTERY_AD7091R5_ENABLED238239240