2020-01-05 09:33 PM
Hello.
I am estimating SOC using coulomb counting method.By coulomb counting method ,we have to do integration of discharge current.I got successfully discharge current in a variable but it shows one value at a time.I know,by using array we can store the data but don't know how to update it continuously.
The variable 'current0' contain data in my code as given below .Please give me any suggestion.
int main(void)
{
uint8_t TxData;
uint16_t Data;
uint16_t current0;
while (1)
{
HAL_I2C_Master_Transmit(&hi2c1,0x16, &TxData, 1, 100);
HAL_I2C_Master_Receive(&hi2c1, 0x17, &Data, 2, 100);
current0=65536-Data; /* variable 'current0' contain data.*/
}
}
2020-01-05 10:44 PM
You could have an array that holds enough values to cover your integration time. The array index could be increment to the size of the array, and then wrapped to zero.
You could use another variable as an accumulator/sum, subtract out the array element you are evicting, and replace with the new value and add that to the accumulator. This would be a sum of all items in the array.
2020-01-05 11:35 PM
@Community member Thanks for your quick response sir.You very well said in words but how to do it ,its my question. I am very new to this environment ,so i am expecting little bit detail from your side sir.
Thanks.
2020-01-05 11:53 PM
#define AVERAGING_SIZE 10
int averagingArray[AVERAGING_SIZE];
int averagingIdx = 0, averagingInitCnt = 0;
int averagingSum = 0; // assuming individual samples are smaller than (31-bit/AVERAGING_SIZE)
...
[when new sample arrives]
if (averagingInitCnt < AVERAGING_SIZE) {
averagingInitCnt++; // there are not enough samples yet, so just fill up the array/sum (below in common part) and keep counting
} else {
averagingSum -= averagingArray[averagingIdx]; // remove old value from the sum
}
averagingSum += sample; // add up the sample to the sum
averagingArray[averagingIdx] = sample; // store sample so that we can remove it from sum later when it "expires"
averagingIdx++; // increment index
if (averagingIdx >= AVERAGING_SIZE) averagingIdx = 0; // wrap around if index reaches end of array
...
2020-01-05 11:56 PM
@Community member Thanks for your reply ,i will try it.
2020-01-06 03:40 AM
You can maybe speed things up a bit by using a 2^N sized buffer and a uint8_t BufferCnt that is incremented after each sample so that it counts freely from 0 -> 0xff. Then, when using the count, mask it with a constant that selects the lowest N bits.
TempCnt = BufferCnt & (2^N - 1)
Also, if the rest of the code can handle the very first N values being low, simply initialize everything to 0.