Softivation

Mission won't upload from Python to Pixhawk: check the request type

If the Python code on your Raspberry Pi hangs while uploading a mission to the Pixhawk, it means ArduPilot is requesting the items under the old request name and your code never receives that name.

Furkan IŞIKAY 8 min read

The Python code on your Raspberry Pi starts sending the route to the Pixhawk and freezes after the first item. There's no error message, just a blinking cursor. You can't blame the cable: the heartbeat is arriving and you can read parameters.

The code is waiting for the wrong message. Below you'll find which name the Pixhawk uses to request items, and corrected code that accepts both names.

The message the code is waiting for never arrives

Mission upload is a chain of question and answer. The code first says how many items it will send. The autopilot requests the items one by one starting from zero, the code answers each request with that item, and after the last item the autopilot returns a confirmation message.

The current MAVLink documentation says the autopilot should request items with MISSION_REQUEST_INT and treats the old MISSION_REQUEST message as deprecated. That's why most examples online wait only for the new name. ArduPilot 4.7.2, however, still requests items with the old name. In the source code, the function that sends the request calls the old message.

The Pixhawk repeats the same request, and the code receives none of them because it expects a different type

ArduPilot sends MISSION_REQUEST. Code that waits only for MISSION_REQUEST_INT receives no request for 8 seconds and the upload is canceled
The blue arrows are requests coming from the Pixhawk. Look at where the red bar ends at the 8th second: the upload was canceled there.

In the figure, the blue arrows are requests leaving the Pixhawk: all of them ask for item zero and repeat about one second apart. The red bar is your code. The requests reach it, but because its filter looks for a different name, your code receives none of them, and at the 8th second the autopilot cancels the upload.

In the stuck code, the recv_match call usually waits only for the MISSION_REQUEST_INT type and has no timeout. Even after the autopilot gives up, the code keeps waiting there.

A half-finished mission mixes with the old route

The real risk starts after the cancellation. As soon as the autopilot receives the new mission's item count, it truncates the old list to that length and overwrites items one by one as they arrive. If the upload stops halfway, the previous route remains in the items that weren't written.

Think of a TEKNOFEST rehearsal. Yesterday you uploaded a five-item route flown at 20 meters. Today you send a wider route of the same length at 30 meters from the Pi, and the upload drops at the second point. When you read it back, the takeoff item shows 30 meters, but the next three points are yesterday's coordinates and yesterday's 20 meters.

If your code doesn't check the confirmation, you won't notice this. When you switch to AUTO, the drone flies this mixed route. Don't switch to AUTO without seeing the confirmation message and reading the route back.

Sending everything back to back doesn't save you either

Another common attempt is to send the five items back to back without waiting for any requests. As long as no packet is lost, the autopilot accepts items in order. If a single packet drops on the telemetry radio, the autopilot treats the following items as out of order and discards them, and requests the missing item again every second. Because your code is no longer listening, the cancellation comes again at the 8th second.

The lasting fix is to drive the code by the requests: send whichever item the autopilot asks for, and accept both request names.

Fixed code

The code below answers both request types, keeps waiting on an out-of-order item warning, and reads the result of the final confirmation. Because it sends coordinates in integer form (MISSION_ITEM_INT), there's no decimal loss. After the confirmation, it reads the route back and compares each item with your list.

Python
import time
from pymavlink import mavutil

# For a serial link on a Raspberry Pi, e.g.: mavutil.mavlink_connection("/dev/serial0", baud=921600, source_system=255)
m = mavutil.mavlink_connection("tcp:127.0.0.1:5760", source_system=255)
m.wait_heartbeat()
print("Connected: sysid", m.target_system)

M = mavutil.mavlink
# (command, latitude, longitude, altitude). Row 0 is Home's slot; the autopilot fills it with its own position.
GOREV = [
    (M.MAV_CMD_NAV_WAYPOINT, 0, 0, 0),
    (M.MAV_CMD_NAV_TAKEOFF, 0, 0, 20),
    (M.MAV_CMD_NAV_WAYPOINT, 37.1600, 38.7969, 20),
    (M.MAV_CMD_NAV_WAYPOINT, 37.1600, 38.7985, 20),
    (M.MAV_CMD_NAV_WAYPOINT, 37.1591, 38.7985, 20),
]


def oge_gonder(seq):
    cmd, lat, lon, alt = GOREV[seq]
    frame = M.MAV_FRAME_GLOBAL if seq == 0 else M.MAV_FRAME_GLOBAL_RELATIVE_ALT
    m.mav.mission_item_int_send(
        m.target_system, m.target_component, seq, frame, cmd,
        0, 1, 0, 0, 0, 0,
        round(lat * 1e7), round(lon * 1e7), alt,
        M.MAV_MISSION_TYPE_MISSION)


def gorev_yukle(bekleme=10):
    m.mav.mission_count_send(m.target_system, m.target_component,
                             len(GOREV), M.MAV_MISSION_TYPE_MISSION)
    son_istek = time.time()
    while time.time() - son_istek < bekleme:
        msg = m.recv_match(type=["MISSION_REQUEST", "MISSION_REQUEST_INT", "MISSION_ACK"],
                           blocking=True, timeout=1)
        if msg is None or msg.mission_type != M.MAV_MISSION_TYPE_MISSION:
            continue
        if msg.get_type() == "MISSION_ACK":
            if msg.type == M.MAV_MISSION_INVALID_SEQUENCE:
                continue          # out-of-order item; the autopilot will request the missing one again
            return msg.type
        print("Requested:", msg.get_type(), "seq", msg.seq)
        oge_gonder(msg.seq)
        son_istek = time.time()
    return None


def gorev_oku():
    m.mav.mission_request_list_send(m.target_system, m.target_component, M.MAV_MISSION_TYPE_MISSION)
    adet = m.recv_match(type="MISSION_COUNT", blocking=True, timeout=5).count
    satirlar = []
    for seq in range(adet):
        m.mav.mission_request_int_send(m.target_system, m.target_component, seq, M.MAV_MISSION_TYPE_MISSION)
        o = m.recv_match(type="MISSION_ITEM_INT", blocking=True, timeout=3)
        satirlar.append((o.seq, o.command, o.x, o.y, o.z))
    m.mav.mission_ack_send(m.target_system, m.target_component, M.MAV_MISSION_ACCEPTED, M.MAV_MISSION_TYPE_MISSION)
    return satirlar


sonuc = gorev_yukle()
if sonuc is None:
    print("Autopilot did not respond: check the link and the sysid")
elif sonuc != M.MAV_MISSION_ACCEPTED:
    print("Mission rejected:", M.enums["MAV_MISSION_RESULT"][sonuc].name)
else:
    print("Mission accepted")
    for seq, cmd, x, y, z in gorev_oku()[1:]:
        _, lat, lon, alt = GOREV[seq]
        tamam = (x, y) == (round(lat * 1e7), round(lon * 1e7)) and z == alt
        print("seq", seq, "command", cmd, "matches" if tamam else "DIFFERENT")

If you're connecting over a serial link on the Pi, change the connection line to your own port and speed as in the comment. Item zero of the list is Home's position. The autopilot fills it in with its own location, so the comparison starts from item 1.

What you'll see

When you run the code, the Pixhawk requests each item separately, the confirmation arrives, and the items read back match your list:

Output
Connected: sysid 1
Requested: MISSION_REQUEST seq 0
Requested: MISSION_REQUEST seq 1
Requested: MISSION_REQUEST seq 2
Requested: MISSION_REQUEST seq 3
Requested: MISSION_REQUEST seq 4
Mission accepted
seq 1 command 22 matches
seq 2 command 16 matches
seq 3 command 16 matches
seq 4 command 16 matches

Notice that the requests arrive under the name MISSION_REQUEST. The fix exists precisely to catch these lines. If you see DIFFERENT on a line, that item wasn't written or is left over from the old route; repeat the upload before switching to AUTO.

Error messages

Mission upload timeout

The autopilot didn't receive the item it was waiting for during 8 seconds and canceled the upload. This message goes only to the connection that uploads the mission. If Mission Planner is connected over a separate radio, it may not appear on its screen. If your code reads STATUSTEXT messages, you'll see it there.

got MISSION_ITEM; GCS should send MISSION_ITEM_INT

The code is sending items with the old decimal message. The upload is still accepted, but the coordinates are rounded to 32-bit floats. Send with mission_item_int_send, multiply latitude and longitude by 10,000,000 and round to an integer.

Flight plan received

Success message: the autopilot accepted the last item. At this point, the result of the confirmation that arrives in your code should be 0 (ACCEPTED).

More in the course

Mission upload with Python, GUIDED and AUTO commands, and control from a companion computer are covered step by step in the Drone Control with Python from Scratch course. The newly added lesson follows the same upload through the autopilot's source code and the flight log. The code that clears a half-finished mission and uploads again is there too.

Was this article helpful?

Free resourceTEKNOFEST UAV software readiness checklistCheck off your team's software readiness stage by stage on a printable list.