Jump to content

Battery


Recommended Posts

What is the most proper way to calculate battery discharge ratio?

 

I am trying to add this here:

Load: 0.10, 0.50, 0.38 - Memory: 974Mb - Battery discharging: 30%

AXP209 gives me:

http://linux-sunxi.org/AXP209

 

- voltage

- charge / discharge current

 

Battery capacity should be the only parameter I want to set.

 

Is there any advanced calculation script or simply, but not so accurate, direct connection between voltage and percentage?

Link to comment
Share on other sites

Hi Igor, i´am not much of a coder, hardware is my thing.

 

I just wanted to point out as i geuss the perfectionist u are, that there are simple hardware add-ons, i am using to measure my solar panel charge/discharge in real time.

The sensor i am using comes with a 3 pin 2,54 inch connector and communicates very straight forward in mV to a/d channel.

U could pop it into the strip connector of the board, if we can find a 5volt + /- and an a/d channel besides another. (did'nt check yet)

 

I could elaborate more, if this is interesting for the community.

 

This is the 1 i´am talking about and costs about 2-3 euro's and is very accurate.

acs712.png

Link to comment
Share on other sites

Hi Igor, i´am not much of a coder, hardware is my thing.

 

I just wanted to point out as i geuss the perfectionist u are, that there are simple hardware add-ons, i am using to measure my solar panel charge/discharge in real time.

The sensor i am using comes with a 3 pin 2,54 inch connector and communicates very straight forward in mV to a/d channel.

U could pop it into the strip connector of the board, if we can find a 5volt + /- and an a/d channel besides another. (did'nt check yet)

 

I could elaborate more, if this is interesting for the community.

 

This is the 1 i´am talking about and costs about 2-3 euro's and is very accurate.

acs712.png

I think he wants to implement this for everybody.

(cat /sys/class/power_supply/battery/capacity) there i get the % from my battery.?

Link to comment
Share on other sites

This script was originaly for using acpi to get the values but should give you an idea on how to calculate what you need. Requires calc to be installed

#!/bin/bash
math() { calc -d "$@"|tr -d ~; }
cd /proc/acpi/battery/BAT0;
max=$(grep 'design capacity:' info|awk '{print $3}')
current=$(grep 'remaining capacity:' state|awk '{print $3}')
percent=$(math "($current / $max) * 100");
echo $(echo $percent|cut -d. -f1)%
Link to comment
Share on other sites

I came out with this: 

######################################################################################
#
# Battery info
#


i2cset -y -f 0 0x34 0x82 0xC3
# read power OPERATING MODE register @01h
POWER_OP_MODE=$(i2cget -y -f 0 0x34 0x01)
BAT_EXIST=$(($(($POWER_OP_MODE&0x20))/32))  # divide by 32 is like shifting rigth 5 times
CHARG_IND=$(($(($POWER_OP_MODE&0x40))/64))  # divide by 64 is like shifting rigth 6 times

########################################################################################
#read battery voltage   79h, 78h        0 mV -> 000h,   1.1 mV/bit      FFFh -> 4.5045 V
BAT_VOLT_LSB=$(i2cget -y -f 0 0x34 0x79)
BAT_VOLT_MSB=$(i2cget -y -f 0 0x34 0x78)

BAT_BIN=$(( $(($BAT_VOLT_MSB << 4)) | $(($(($BAT_VOLT_LSB & 0xF0)) >> 4)) ))
BAT_VOLT=$(echo "($BAT_BIN*1.1)"|bc)

# store maximum battery voltage to compare to
if [ -f "/etc/default/battery" ]; then
        source "/etc/default/battery"
else
        echo "MAX=$BAT_VOLT" > /etc/default/battery
	echo "MIN=3484" >> /etc/default/battery
fi

# integer is enough, cut down the decimals
MAX=${MAX%.*}
BAT_VOLT=${BAT_VOLT%.*}

# mainline kernel shows battery existence even if not exists. this is walkaround 
if [ "$BAT_VOLT" -ge "3200" ]; then


# if we have new max value, alter defaults
if [ "$BAT_VOLT" -gt "$MAX" ]; then 
MAX=$BAT_VOLT
sed -e 's/MAX=.*/MAX='$BAT_VOLT'/g' -i /etc/default/battery
fi

# if we have new min value, alter defaults
if [ "$BAT_VOLT" -lt "$MIN" ]; then
MIN=$BAT_VOLT
#sed -e 's/MIN=.*/MIN='$BAT_VOLT'/g' -i /etc/default/battery
fi

# calculate percentage
percent=$(echo "($BAT_VOLT-$MIN)*100/($MAX-$MIN)"|bc)

# colorize output under certain percentage
if [ $percent -le 15 ]
then
  color="31"
fi

if [ "$BAT_EXIST" == "1" ]; then
BATT=" - Batt: "
# dispay charging / percentage
if [ "$CHARG_IND" == "1" ]; then
BATT=$BATT"charging $percent%"
else
BATT=$BATT"\e["$color"m$percent%\x1B[0m"
fi
fi
fi
OUT="${OUT}${BATT}"


echo ""
echo -e ${OUT}
echo ""

Load: 1.64, 1.14, 1.00 - Board: 36.2°C - Ambient: 27.0°C - Drive: 43°C / 2.5Tb - Memory: 934Mb - Batt: 7%

 

Working on both kernels.

Edited by Igor
update
Link to comment
Share on other sites

I think that @Patcher has the right idea in that the only way to get an accurate measure of the battery charge is to integrate current in/out (+/-) of the battery over time (as confirmed by @KF5YFD's wikipedia link).  Fortunately the AXP209 includes a coulomb counter which should do the trick. I'm just getting started with Armbian on an Olimex Lime A20 myself, but Igor's script is a great starting point and example of communicating with the AXP209 via I2C. At some point when I have some time I might try to sort out a technique that uses the coulomb counter to track battery charge. If I do I'll be sure to post here. In any case I think the coulomb counter is the way to go.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines