From 823d423456922c2f53a0b4b74c1ca1ca19e61bd5 Mon Sep 17 00:00:00 2001
From: Matthias Grob <maetugr@gmail.com>
Date: Mon, 23 Oct 2017 17:06:01 +0200
Subject: [PATCH] Battery: refactor current integration to be easier
 understandable

---
 src/modules/systemlib/battery.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/modules/systemlib/battery.cpp b/src/modules/systemlib/battery.cpp
index 5cd5090ab7..3db0f2aeeb 100644
--- a/src/modules/systemlib/battery.cpp
+++ b/src/modules/systemlib/battery.cpp
@@ -156,9 +156,11 @@ Battery::sumDischarged(hrt_abstime timestamp, float current_a)
 		return;
 	}
 
-	// Ignore first update because we don't know dT.
+	// Ignore first update because we don't know dt.
 	if (_last_timestamp != 0) {
-		_discharged_mah += current_a * ((float)(timestamp - _last_timestamp)) / 1e3f / 3600.f;
+		const float dt = (timestamp - _last_timestamp) / 1e6;
+		// current[A] * 1000 = [mA]; dt[s] / 3600 = [h]
+		_discharged_mah += (current_a * 1e3f) * (dt / 3600.f);
 	}
 
 	_last_timestamp = timestamp;
-- 
GitLab