Reputation: 13
I am trying to calculate the average battery length in minutes of a motor using the following equation: https://i.sstatic.net/7A6bu.jpg
I get the average speed by using the following code:
for (Location location : locationResult.getLocations()) {
int speed = (int) (location.getSpeed() * 2.2369);
float addedSpeed = avgMPH + speed;
avgMPH = addedSpeed / 2;
float math1 = 45 / avgMPH;
batteryView.setText("Battery: " + String.valueOf(math1 * 60) + " Minutes Remaining");
}
The issue is that every time the location loops it doubles my battery life each loop even though it is still at 1 miles per hour. it should not loop like this, help appreciated, thanks!
Upvotes: 1
Views: 319
Reputation: 474
I think you are calculating average speed wrong. You should calculate like this:
float addedSpeed = avgMPH * numSamples + speed;
avgMPH = addedSpeed / (numSamples + 1);
numSamples = numSamples + 1;
Not like this:
float addedSpeed = avgMPH + speed;
avgMPH = addedSpeed / 2;
For example, think you went 1 mph for 10 hours and 100 mph for a hour. Former will give you average 10 mph which is correct and latter will give you average 55.5 mph which is wrong.
Upvotes: 1