Why Pixhawk lands when you send a target from Python
If the drone lands when you send a target from Python, the altitude frame is the first place to look. If you send 20 meters relative to sea level, the Pixhawk descends to the ground.
The drone is waiting in the air in GUIDED mode. Your Python code on the Raspberry Pi sends a target 50 meters ahead at 20 meters altitude. Instead of moving forward, the drone slowly descends and touches down before reaching the target.
The 20 in the code is correct. The problem is what the autopilot reads that 20 relative to.
Why the drone descends
The altitude in a position target doesn't travel alone; it carries a frame number with it. Frame 6 means relative to home: 20 is 20 meters above the takeoff point. Frame 5 means relative to sea level: 20 is 20 meters above sea level.
If the field is 500 meters above sea level, a 20-meter target sent with frame 5 ends up 480 meters below the ground. ArduPilot accepts this target without objection and starts taking the drone toward it in a straight line.
The same 20, two different frames
Look at the point where the red line touches the ground in the figure. The target is 50 meters ahead but 480 meters down, so almost the entire path is vertical. The drone descends at the descent speed limit of 1.5 meters per second, moves only 2 meters horizontally and hits the ground.
An example from a TEKNOFEST swarm mission: imagine code that reads the leader drone's position and sends it to the follower as a target. If the code takes the height from the field relative to home (20 m) and sends it with frame 5, the follower lands. If it sends the same number with frame 6, the follower stays at the same height as the leader.
Why the autopilot doesn't warn you
This position message has no acknowledgment. Whether ArduPilot accepts the target or ignores it in a mode other than GUIDED, it doesn't write a single line to the ground station.
The only feedback you're left with is the target message the autopilot broadcasts. That message always gives the altitude relative to sea level. If your code subtracts the home altitude from this number, you see where the target sits relative to home.
The most common mistake is mixing up the two heights in the drone's position message. In GLOBAL_POSITION_INT, the alt field is relative to sea level and the relative_alt field is relative to home. Choose the frame based on which field you took the number from: 5 for alt, 6 for relative_alt.
The fix: choose the frame, read the target back
The code below sends the target with frame 6, that is, relative to home. It then reads the target the autopilot accepted and prints how many meters relative to home it works out to.
import time
from pymavlink import mavutil
BAGLANTI = "tcp:127.0.0.1:5762" # On a Raspberry Pi: "/dev/serial0"
IRTIFA_M = 20 # metres relative to the takeoff point (home)
KUZEY_M = 50 # how many metres north of the current position the target is
baglanti = mavutil.mavlink_connection(BAGLANTI, baud=57600)
baglanti.wait_heartbeat()
baglanti.mav.request_data_stream_send(
baglanti.target_system, baglanti.target_component,
mavutil.mavlink.MAV_DATA_STREAM_ALL, 4, 1)
konum = baglanti.recv_match(type="GLOBAL_POSITION_INT", blocking=True)
ev_amsl = (konum.alt - konum.relative_alt) / 1000.0 # home altitude above sea level
hedef_lat = konum.lat / 1e7 + KUZEY_M / 111111.0
hedef_lon = konum.lon / 1e7
baglanti.mav.set_position_target_global_int_send(
0, baglanti.target_system, baglanti.target_component,
mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # altitude relative to HOME
0b0000111111111000, # position only
int(hedef_lat * 1e7), int(hedef_lon * 1e7), IRTIFA_M,
0, 0, 0, 0, 0, 0, 0, 0)
# Read back the target the autopilot accepted. This message is always relative to sea level.
hedef = None
bitis = time.time() + 5
while time.time() < bitis:
m = baglanti.recv_match(type="POSITION_TARGET_GLOBAL_INT", blocking=True, timeout=1)
if m and abs(m.lat_int / 1e7 - hedef_lat) < 1e-5:
hedef = m
break
if hedef is None:
print("Target not returned: is the vehicle in GUIDED mode?")
else:
eve_gore = hedef.alt - ev_amsl
print(f"Home (sea level): {ev_amsl:.1f} m")
print(f"Target (sea level): {hedef.alt:.1f} m -> relative to home {eve_gore:.1f} m")
if eve_gore < 2:
print("WARNING: target is at or below ground, check the altitude frame")On a Raspberry Pi, set the BAGLANTI line to "/dev/serial0" and match the baud value to the speed of the Pixhawk port the Pi is connected to. The drone must be in GUIDED mode before you run the code. In any other mode, the autopilot doesn't process the target at all.
What you'll see
If you run the code as is, with frame 6, the target lands 20 meters above home and the drone flies 50 meters north at that height:
Home (sea level): 500.1 m
Target (sea level): 520.1 m -> relative to home 20.0 mIf you write MAV_FRAME_GLOBAL_INT instead of MAV_FRAME_GLOBAL_RELATIVE_ALT_INT on the same line, the target falls below the ground, the code warns you, and the drone starts to descend:
Home (sea level): 500.1 m
Target (sea level): 20.0 m -> relative to home -480.1 m
WARNING: target is at or below ground, check the altitude frameThe number on the home line is your field's elevation. You'll see your own figure instead of 500. What matters is that the relative-to-home value on the second line matches the height you want.
Error Messages
ArduPilot doesn't print a warning for the wrong frame. The following are real messages you may run into around the same problem.
Mode change to GUIDED failed: requires position
The autopilot has no position estimate, and the vehicle didn't switch to GUIDED. In this case the target you send is never processed, and the code prints "Hedef geri gelmedi". First wait for the GPS to get a 3D fix.
SET_POSITION_TARGET_GLOBAL_INT invalid coordinate frame 0
PX4 prints this line. In this message PX4 accepts only frames 5, 6 and 11. It rejects frames 0, 3 and 10. ArduPilot accepts both.
Expecting location to be LocationGlobal or LocationGlobalRelative.
DroneKit's simple_goto function gives this error if it gets anything other than these two classes. If you pass a LocationGlobal, the library subtracts the home altitude and sends it relative to home. If you write LocationGlobal(enlem, boylam, 20), the target still ends up below the ground.
Finish the code in the course
Complete code that checks whether the target will fall below the ground before sending it, compares it with the autopilot's response and switches the drone to LOITER if they don't match, along with the decision order ArduPilot follows when processing this message, are in the Drone Control with Python from Scratch course.
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