Using Olympe enums types#

In addition to the ARSDK messages, Olympe also provides Python Enum and Bitfield types in the olympe.enums.<feature_name>[.<class_name>] modules.

Most of the time, you shouldn’t really need to import an enum type into your script because enum types are implicitly constructed from a string when you create a message object, so the following examples are roughly equivalent:

 1import olympe
 2import os
 3from olympe.messages.ardrone3.PilotingState import FlyingStateChanged
 4from olympe.enums.ardrone3.PilotingState import FlyingStateChanged_State as FlyingState
 5
 6olympe.log.update_config({"loggers": {"olympe": {"level": "WARNING"}}})
 7
 8DRONE_IP = os.environ.get("DRONE_IP", "10.202.0.1")
 9
10
11def test_enums():
12    drone = olympe.Drone(DRONE_IP)
13    drone.connect()
14    assert drone(FlyingStateChanged(state="landed")).wait(5).success()
15    # is equivalent to
16    assert drone(FlyingStateChanged(state=FlyingState.landed)).wait(5).success()
17    drone.disconnect()
18
19
20if __name__ == "__main__":
21    test_enums()

Olympe enum types should behave very much like Python 3 enum.Enum.

Bitfields types are associated to each ARSDK enums types and are occasionally used by ARSDK messages. A Bitfield type can hold any combination of its associated Enum type values.

Bitfield example:

 1import olympe
 2import os
 3from olympe.messages.ardrone3.PilotingState import FlyingStateChanged
 4from olympe.enums.ardrone3.PilotingState import FlyingStateChanged_State as FlyingState
 5
 6olympe.log.update_config({"loggers": {"olympe": {"level": "WARNING"}}})
 7
 8DRONE_IP = os.environ.get("DRONE_IP", "10.202.0.1")
 9
10
11def test_bitfield():
12    drone = olympe.Drone(DRONE_IP)
13    drone.connect()
14
15    flying_states = FlyingState._bitfield_type_("takingoff|hovering|flying")
16
17    if drone.get_state(FlyingStateChanged)["state"] in flying_states:
18        assert False, "The drone should not be flying"
19    else:
20        print("The drone is not in flight")
21    drone.disconnect()
22
23
24if __name__ == "__main__":
25    test_bitfield()

Additional usage examples are available in the unit tests of olympe.arsdkng.enums.