Softivation

Pixhawk altitude is wrong in Python (Mission Planner is right)

Is the Pixhawk altitude wrong in Python? The alt field in GLOBAL_POSITION_INT gives the altitude above sea level. For the altitude shown in Mission Planner, read relative_alt.

Furkan IŞIKAY 6 min read

Mission Planner shows the drone at 10 meters. At the same moment, your Python code on the Raspberry Pi says 510, or 510080 if you forgot to divide. Even while the drone is still on the ground, your code says "I've climbed to 10 meters" and moves on to the next step.

There are two separate altitudes in the same packet, and your code is reading the one Mission Planner doesn't show.

The code turns on the camera while the drone is on the ground

Think of a condition often set up in the Fighting UAV mission: start the camera recording when the drone passes 10 meters on takeoff. The code reads the position packet, divides the altitude by 1000 to convert it to meters, and turns on the camera if the result is greater than 10.

If the field elevation is 500 meters, this condition is true even while the drone is on the ground, because 500 is already greater than 10. Recording starts before the propellers spin, and the code moves on to the next step of the mission without waiting for takeoff.

There are two altitudes in the same packet

ArduPilot sends the position in the GLOBAL_POSITION_INT packet. The packet has two altitude fields, and both are in millimeters:

  • alt measures from sea level. On a field with an elevation of 500 meters, you read around 500080 while the drone is on the ground.
  • relative_alt measures from the takeoff point (home). This is the altitude Mission Planner shows on screen.

In the figure, notice that all three arrows end at the same drone but start from different lines. The red arrow carries the field elevation within it; the blue arrow carries only the height climbed since takeoff.

Same drone, three zero points

The three altitude fields for the same drone are measured from different zero points
Look at the line where each arrow starts: the red arrow measures from the sea, the blue arrow from the home point, and the dashed arrow from the EKF origin. The vertical axis isn't to scale.

The dashed arrow is a third source: z in the LOCAL_POSITION_NED packet. This axis counts downward as positive, so when the drone is 10 meters up, you read a negative value like −9.98. The altitude in the VFR_HUD packet also measures from sea level with the default setting.

A common mistake

Dividing the alt field by 1000 doesn't solve the problem. The unit goes back to meters, but the zero point is still sea level. Instead of 10, the code sees the field elevation plus 10 and reads 510.

The fix: read relative to the takeoff point

Base the condition on the home point. The code below prints both fields side by side, so you can see on your own screen which one matches Mission Planner:

Python
from pymavlink import mavutil

# Put the address you use to connect to the Pixhawk (serial port, UDP or TCP)
baglanti = mavutil.mavlink_connection('tcp:127.0.0.1:5760')
baglanti.wait_heartbeat()
baglanti.mav.request_data_stream_send(baglanti.target_system, baglanti.target_component,
                                      mavutil.mavlink.MAV_DATA_STREAM_POSITION, 5, 1)

for _ in range(3):
    m = baglanti.recv_match(type='GLOBAL_POSITION_INT', blocking=True)
    deniz = m.alt / 1000            # mm -> m, relative to sea level
    kalkis = m.relative_alt / 1000  # mm -> m, relative to the takeoff point (home)
    print(f"sea level: {deniz:.2f} m | relative to takeoff point: {kalkis:.2f} m")

Enter: use m.relative_alt / 1000 in the camera condition. You'll see: a value close to zero while the drone is on the ground, and the same number as Mission Planner's altitude while it's in the air. If not: check where the home point was set (below).

What you'll see

If you run the code while the drone is on the ground, you'll see this:

Output
sea level: 500.08 m | relative to takeoff point: 0.01 m
sea level: 500.08 m | relative to takeoff point: 0.01 m
sea level: 500.08 m | relative to takeoff point: 0.01 m

If you run the same code after the drone has climbed to 10 meters, you'll see this:

Output
sea level: 510.08 m | relative to takeoff point: 10.00 m
sea level: 510.08 m | relative to takeoff point: 10.00 m
sea level: 510.08 m | relative to takeoff point: 10.00 m

The 500 meters in the output is the field elevation. On your own field, the first column starts with that field's elevation, while the second column starts from zero on every field.

The home point is set at the moment of ARM

The zero of relative_alt is the home point. If the home point isn't locked, ArduCopter rewrites it to the drone's current position on every ARM (AP_Arming_Copter.cpp). That's why the value you read before ARM can drift from zero. Base the takeoff condition on the value you read after ARM.

Error Messages and Their Meanings

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

recv_match timed out and returned None instead of a packet. If you read without requesting the position stream, the packet never arrives. Check that the request_data_stream_send line in the code is in place, and whether the returned value is None.

Field Elevation Set: 500m

The autopilot set the field elevation to this number. With BARO_FIELD_ELV = 0 (the default), the number is the altitude of the EKF origin, and the alt field sits on top of this elevation. On your field, the number will be that field's elevation.

PreArm: Baro: GPS alt error

There's a difference greater than BARO_ALTERR_MAX (default 2000 m) between the sea-level altitude from the GPS and the altitude the barometer calculates. The rest of the message gives the difference in meters, and ARM permission doesn't come.

Build the flight step by step from Python

Connecting, attribute listeners, GUIDED commands and mission upload, in that order, in the Drone Control with Python from Scratch course. In the course lessons you'll also find when each altitude source is reset and how to tell them apart in the log.

Was this article helpful?

Free resourceArduPilot SITL setup guideTry your code in SITL before heading out to the field: setup and connection, start to finish.