How do I get my virtual drone to print out GPS coordinates with Olympe

I’m currently using this code

import olympe
from olympe.messages.ardrone3.Piloting import TakeOff, Landing, moveBy
from olympe.messages.ardrone3.PilotingState import (
    PositionChanged,
    AlertStateChanged,
    FlyingStateChanged,
    NavigateHomeStateChanged,
)

class FlightListener(olympe.EventListener):

    @olympe.listen_event(FlyingStateChanged() | AlertStateChanged() |
        NavigateHomeStateChanged())
    def onStateChanged(self, event, scheduler):
        print("HELLO WORLDS")
        print("{} = {}".format(event.message.name, event.args["state"]))

    @olympe.listen_event(FlyingStateChanged() | NavigateHomeStateChanged() | PositionChanged())
    def onPositionChanged(self, event, scheduler):
        position_data = {
   		"latitude": event.args["latitude"],
   		"longitude": event.args["longitude"],
	}
        print("HELLO WORLD")
        print("GPS position:", json.dumps(position_data, indent=2))



drone = olympe.Drone("10.202.0.1")
with FlightListener(drone):
    drone.connect()
    drone(
        FlyingStateChanged(state="hovering")
        | (TakeOff() & FlyingStateChanged(state="hovering"))
    ).wait()
    drone(moveBy(1, 0, 0, 0)).wait()
    drone(Landing()).wait()
    drone(FlyingStateChanged(state="landed")).wait()
    drone.disconnect()

but it says that the key for event.args is wrong, and doesn’t print the GPS coordinates.

Hi,

It seems that your listener “onPositionChanged” expectation is wrong:

    @olympe.listen_event(FlyingStateChanged() | NavigateHomeStateChanged() | PositionChanged())
    def onPositionChanged(self, event, scheduler):

It should probably be:

    @olympe.listen_event(PositionChanged(_policy='wait'))
    def onPositionChanged(self, event, scheduler):

instead so that your “onPositionChanged” callback is called whenever you receive a new PositionChanged event message from the drone.

This topic was automatically closed after 30 days. New replies are no longer allowed.