Get remaining battery charge of drone

I’m trying to write a program to get the percent of battery remaining on my drone (real drone not simulator). The battery.health event message seems to do what I want but everytime I call getState the output is always OrderedDict([('state_of_health', 100)]) even when I leave drone running and the battery level obviously should not be 100%. Is this the correct way to get the amount of battery charge left on the drone?

import math
import olympe
import time
from olympe.messages.battery import health, voltage, alert


drone = olympe.Drone("192.168.42.1", loglevel=1)
drone.connection()


# display current battery life every 5 seconds, however this always returns 
# 100% even when I leave drone running.
while True:
	print("Battery health before takeoff :", drone.get_state(health))
	time.sleep(5)

drone.disconnection()
1 Like

clashing languages and frameworks here but I think what you really want is the battery level, not its health.

/**
 * Retrieves the device's current battery charge level, as an integer percentage of full charge.
 *
 * @return current battery charge level
 */
@IntRange(from = 0, to = 100)
int getBatteryLevel();

/**
 * Tells whether the device is currently charging.
 *
 * @return {@code true} if the device is charging, {@code false} otherwise
 */
boolean isCharging();

/**
 * Retrieves the device's current battery state of health, as an integer percentage of full health.
 * <p>
 * Battery health may be unsupported depending on the drone model and/or firmware versions. <br>
 * Hence, clients of this API should call {@link OptionalInt#isAvailable() isAvailable} method on the returned
 * value to check whether it can be considered valid before use.
 *
 * @return current battery state of health
 *
 * @see OptionalInt
 */
@NonNull
OptionalInt getBatteryHealth();
2 Likes

Thanks for reply. You are giving example from groundsdk? For Olympe I see 3 message types
-voltage
-health
-alerts

I don’t see the getBatteryLevel message anyway to do this in Olympe?

If not I can live with low battery alert but I would prefer to get exact battery remaining…

Have you tried voltage?

Hi,

The battery percentage is available through the common.CommonState.BatteryStateChanged message.

import math
import olympe
import time
from olympe.messages.common.CommonState import BatteryStateChanged


drone = olympe.Drone("192.168.42.1", loglevel=1)
drone.connection()
# display current battery life every 5 seconds
while True:
	print("Battery percentage before takeoff :", drone.get_state(BatteryStateChanged)["percent"])
	time.sleep(5)

drone.disconnection()
2 Likes