Softivation

Pixhawk position arrives slowly in Python (the link is fine)

Pixhawk position arrives slowly in Python while the link looks solid. Measure the request rate, the read loop and radio load one at a time and find the cause in a minute.

Furkan IŞIKAY 5 min read

Your Python code connects to the Pixhawk and the heartbeat arrives steadily, but position either never arrives or arrives once per second. You send a velocity command, and the position on screen still shows where the drone was two seconds ago.

The cable and the link are fine, yet the data is slow. The slowness can come from three different places, and you can tell which one in a minute.

Why position arrives so rarely

ArduCopter doesn't send position on its own. Position starts flowing when a program such as Python or a ground station asks for it. If whoever asked requested a low rate, you get a low rate.

The second possibility is on the Python side. If your read loop spends half a second on other work on every pass, incoming messages pile up in the queue and you read an old position.

The third is the radio. If the telemetry radio reports that it's filling up, the autopilot slows the stream down itself. All three causes give the same symptom: you see an old or infrequent position on screen. So measure each one separately.

Tell which one it is in a minute

Count the position messages that arrive over ten seconds. The figure below shows which cause the number you count points to.

The number you count points to three different places

The number you count points to three different places: 7 boxes, 6 connectionsOver 10 seconds,count position messagesnothing arrivescounter 0nobody asked for itrequest the rate yourself~1 per secondless than you askedlow request orradio filling upat your ratebut position staleyour read loop is slowcompare the time with the local clock
Read as text
  1. Over 10 seconds, count position messages then nothing arrives counter 0
  2. nothing arrives counter 0 then nobody asked for it request the rate yourself
  3. Over 10 seconds, count position messages then ~1 per second less than you asked
  4. ~1 per second less than you asked then low request or radio filling up
  5. Over 10 seconds, count position messages then at your rate but position stale
  6. at your rate but position stale then your read loop is slow compare the time with the local clock
Where to look: the middle column. The value the counter shows takes you straight to the box on the right, so you don't need to check all three causes.

If nothing arrives, nobody asked for it. If it arrives about once per second, it was requested at a low rate or the radio is filling up.

If it arrives at the rate you asked for but the position is stale, the problem is in your loop. If the count is right and the position still lags behind, compare the message timestamps with the local clock.

The most common mistake

Don't assume the rate took effect when COMMAND_ACK says "accepted". The acknowledgment only reports that the command was received. Check the rate by counting the messages.

Request position at the rate you want, and count it

Request the position message rate directly, then count the actual rate. The code below counts messages for ten seconds and prints the measured rate.

Pythonkonum_hizi.py
import time
from pymavlink import mavutil

m = mavutil.mavlink_connection('tcp:127.0.0.1:5760')
m.wait_heartbeat()

# Request an interval for GLOBAL_POSITION_INT: 100000 microseconds = 10 messages per second
m.mav.command_long_send(
    m.target_system, m.target_component,
    mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL, 0,
    mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT,
    100000, 0, 0, 0, 0, 0)
print(m.recv_match(type='COMMAND_ACK', blocking=True, timeout=3))

# COMMAND_ACK does not confirm the rate: count the actual rate
sayac, t0 = 0, time.time()
while time.time() - t0 < 10:
    msg = m.recv_match(type='GLOBAL_POSITION_INT', blocking=True, timeout=2)
    if msg is None:
        print('position not arriving')
        break
    sayac += 1
print('measured rate:', sayac / 10, 'Hz')
print(msg.lat / 1e7, msg.lon / 1e7, msg.relative_alt / 1000)

If another program also requests a rate on the same connection, the interval is rewritten. So keep the count inside your own code.

Example: In a swarm rehearsal, your code requests 10 position messages per second and the counter reads 10 Hz. When your teammate connects to the vehicle from the ground station sharing the same link, the counter drops a few seconds later. That's the middle path in the figure. Nothing broke on the vehicle; another program wrote its own rate on the same connection. Because you keep the count inside your own code, you see the drop right away and request the rate again.

Read the output

If you run the code as is, requesting 10 messages per second, you get this:

Output
COMMAND_ACK {command : 511, result : 0, progress : 0, result_param2 : 0, target_system : 255, target_component : 0}
measured rate: 10.0 Hz
37.1590999 38.7969 0.0

The first line shows the command was received, the second shows the actual rate. The third line has latitude, longitude and height above ground.

If you change the rate you request, the second line changes roughly like this:

Rate you requestRate your code prints
no request0 Hz (no position)
1 Hz0.93 Hz
10 Hz10 Hz
20 Hz20 Hz

The request-handling logic runs inside the autopilot. Radio load, on the other hand, only shows up on a real link. Run the same counting code on your own radio link and measure the rate there too.

Error Messages

AttributeError: 'NoneType' object has no attribute 'lat'

The read timed out and recv_match returned None instead of a message. You get this error when position was never requested or the rate is very low. Check for None before using the message.

Requested rate for message ID 33 too fast. Increase SCHED_LOOP_RATE

The interval you requested is shorter than the autopilot's loop period. The autopilot rejects the command with DENIED and sends this text as STATUSTEXT. Lengthen the interval, for example start with 100000 microseconds.

EOF on TCP socket

The TCP connection was closed by the other side. If you restarted the program you connected to, such as SITL, restart the Python code too.

More on drone control with Python

We cover the decision chain, line-by-line evidence from the firmware source code and the log signatures in the Drone Control with Python from Scratch course. The course has a lesson on this topic.

Was this article helpful?

Free resourcePixhawk and ArduPilot error message glossaryThe 4 messages in this article and similar ones are in the glossary: what they mean, why they appear, what to check first.