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.
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
Read as text
- Over 10 seconds, count position messages then nothing arrives counter 0
- nothing arrives counter 0 then nobody asked for it request the rate yourself
- Over 10 seconds, count position messages then ~1 per second less than you asked
- ~1 per second less than you asked then low request or radio filling up
- Over 10 seconds, count position messages then at your rate but position stale
- at your rate but position stale then your read loop is slow compare the time with the local clock
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.
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:
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.0The 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 request | Rate your code prints |
|---|---|
| no request | 0 Hz (no position) |
| 1 Hz | 0.93 Hz |
| 10 Hz | 10 Hz |
| 20 Hz | 20 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?
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