Python code can't get the position (the connection looks up)
Your Python code gets no position even though the connection looks established. The cause is a stream rate of zero. See how to turn on the position stream and confirm the data is arriving.
You run the code, the screen prints that the connection is established, and then latitude is empty, longitude is empty, altitude is empty. You wiggle the cable, change the baud rate, open the ground control station to check. Everything is right where it should be there.
In this situation the link isn't broken. The autopilot never produces that data.
The connected signal is one thing, the data is another
The autopilot does two separate jobs. The first is a signal at a fixed interval announcing that someone is on the end of the link.
The second is streaming message groups such as position, attitude, and satellite info. The first job is done under all conditions; the second isn't.
Your code only sees the first job and decides the connection is established. For the second job the autopilot has a stream table, and in that table every row is zero for multirotor vehicles.
A row at zero means that group is never generated. A message that isn't generated never goes out on the link, never reaches your code, and the attribute never fills. Waiting doesn't help either. Even if you wait until the next day, it won't come.
Four different states on the same connection
As you change what the code requests, you clearly see the difference on the same connection. The connection never drops and the vehicle never restarts. The only thing that changes is the rate of the position message.
Four states on the same connection: the connected signal is always there, position only when requested
Look at the first section in the figure: the connected signal is flowing, the position message is at zero. The moment the request is sent, the position message starts arriving over the same link.
The most common mistake
Setting the stream rate once and forgetting it. On a multirotor, the rate set by a stream request isn't persistent. When the autopilot is power cycled, the value goes back to zero. The code has to send the request again on every boot.
Example: on the first flight in the morning everything is fine. During a battery swap you power the drone off and on, your code still says "connected", but latitude and longitude stay empty. The stream request you sent in the morning was reset along with the autopilot. Every time your code establishes the connection, after a battery swap too, it needs to send the stream request again and see the first position message.
A working request
Sending the request isn't enough; you need to see that it actually took effect. The autopilot can report that it accepted the request and still not send a single message. The short snippet below sends the request, then waits for the message itself, and tries again if it doesn't arrive.
from pymavlink import mavutil
# Connect to the autopilot and wait for the first heartbeat
arac = mavutil.mavlink_connection("tcp:127.0.0.1:5760")
arac.wait_heartbeat()
print("connected:", arac.target_system, arac.target_component)
def konum_akisini_ac(arac, hz=5):
"""Requests a message interval for GLOBAL_POSITION_INT and verifies it actually streams."""
aralik_us = int(1_000_000 / hz)
for deneme in range(1, 6):
arac.mav.command_long_send(
arac.target_system, arac.target_component,
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL, 0,
mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT,
aralik_us, 0, 0, 0, 0, 0)
# The ACK alone is not enough: wait for the message itself
veri = arac.recv_match(type="GLOBAL_POSITION_INT", blocking=True, timeout=3)
if veri is not None:
print("stream opened, attempt:", deneme)
return True
print("no stream, requesting again:", deneme)
return False
if not konum_akisini_ac(arac):
raise SystemExit("Position message never arrived. Check the stream rate and channel option.")
for _ in range(5):
v = arac.recv_match(type="GLOBAL_POSITION_INT", blocking=True, timeout=3)
print("latitude %.7f longitude %.7f altitude above ground %.2f m" %
(v.lat / 1e7, v.lon / 1e7, v.relative_alt / 1000.0))The most important line here is the one that waits for the message itself instead of the acknowledgment. The acknowledgment only shows that the request was processed. You only know the stream has started from the first message that arrives.
If you see zeros in the first seconds after boot
Right after you turn on the stream, a second trap is waiting: messages start arriving, but the numbers inside can be zero. That doesn't mean the message isn't arriving. It means the autopilot hasn't settled its position origin yet.
If you run the same code right after boot and half a minute later, you get this:
# right after startup
connected: 1 0
stream opened, attempt: 1
latitude 0.0000000 longitude 0.0000000 altitude above ground 0.00 m
latitude 0.0000000 longitude 0.0000000 altitude above ground 0.00 m
latitude 0.0000000 longitude 0.0000000 altitude above ground 0.00 m
latitude 0.0000000 longitude 0.0000000 altitude above ground 0.00 m
latitude 0.0000000 longitude 0.0000000 altitude above ground 0.00 m
# half a minute later, same code
connected: 1 0
stream opened, attempt: 1
latitude 37.1590998 longitude 38.7969001 altitude above ground -0.01 m
latitude 37.1590998 longitude 38.7969001 altitude above ground -0.01 m
latitude 37.1590998 longitude 38.7969001 altitude above ground -0.01 m
latitude 37.1590998 longitude 38.7969001 altitude above ground -0.01 m
latitude 37.1590998 longitude 38.7969001 altitude above ground -0.01 mAn empty attribute and a zero value are not the same thing. In the first, there's no message at all; in the second, the message is there but its contents aren't filled yet. If you don't tell these two apart in your code, you'll be looking in the wrong place.
Error Messages and Their Meanings
dronekit.TimeoutError: wait_ready experienced a timeout after 30 seconds.
The code is waiting for the attributes to fill, and since the messages never arrive, the wait times out. This doesn't mean the link is broken. This line also appears when the stream rate is zero.
Link timeout, no heartbeat in last 5 seconds
This line is a warning, not an error. What's missing here is the connected signal itself, so the problem is in the link, not the stream rate. Check the cable, the baud, and the serial port protocol.
No ap_message for mavlink id (141)
The autopilot never produces the message you requested. Raising the stream rate changes nothing, because there is no stream to begin with. This line is only printed in simulation. On a board, the equivalent is the request being rejected.
What changes in the field
The code that runs the stream table is the same as the code running on the board. In the field, the difference shows up in the link: if the telemetry module is losing packets, the message is generated but doesn't reach you.
To tell the two cases apart, first confirm with a short cable that the autopilot is producing the message, then look into the range problem.
If you want to work through this topic end to end
Stream rates, attribute listeners, and all the autonomous commands are covered step by step in the Drone Control with Python from Scratch course. In the lesson you'll find the table from this article together with the decision chain, source code evidence, and log signature tables.
Was this article helpful?
Learn this topic from start to finish
Season plan: all six courses for 9 months, ₺790 a month. ₺390 a month for high school and university students, ₺351 the first month with your member code. You can stop in any month you like. Start the Season plan · I'm a student