Softivation

I write a parameter from Python, but Pixhawk doesn't take the value

You write parameters from Python and the Pixhawk doesn't take the value. Find the names that changed in 4.7, integer truncation, and the sysid error with code that reads the autopilot's reply.

Furkan IŞIKAY 6 min read

Your Python code on the Raspberry Pi writes the mission speed or the return altitude to the Pixhawk, and the program finishes without errors. You connect Mission Planner and look at the parameter: either the old value is still there, or there's a number you never entered.

Your code having sent something doesn't mean the autopilot received it. The autopilot either didn't hear the packet, didn't recognize the name, or changed the number to fit its own type and saved that. You can tell which one it was from the reply it gives each time.

How the autopilot answers a write request

Once a parameter write packet reaches the autopilot, it goes through three checks in order. Follow the figure below from left to right: first whether the packet is addressed to this vehicle, then whether the name is in the list, and finally the type of the parameter.

The path a PARAM_SET packet follows in the autopilot

The path a PARAM_SET packet follows in the autopilot: 8 boxes, 7 connectionsPARAM_SETnoyesnoyesINT8/16/32REAL32Your Python codeparam_yaz()does sysid match?no replypacket ignoredis the name in the list?PARAM_ERRORDOES_NOT_EXIST (4.7); 4.6:no reply at allwhat type is it?PARAM_VALUE, different value1.6 → 1.0 (truncated); 300 →127.0 (INT8 limit)PARAM_VALUE, same valueWP_SPD 5 → 5.0out-of-range accepted too: WP_SPD500 → 500.0
Read as text
  1. Your Python code param_yaz() then (PARAM_SET) does sysid match?
  2. does sysid match? then (no) no reply, packet ignored
  3. does sysid match? then (yes) is the name in the list?
  4. is the name in the list? then (no) PARAM_ERROR DOES_NOT_EXIST (4.7); 4.6: no reply at all
  5. is the name in the list? then (yes) what type is it?
  6. what type is it? then (INT8/16/32) PARAM_VALUE, different value 1.6 → 1.0 (truncated); 300 → 127.0 (INT8 limit)
  7. what type is it? then (REAL32) PARAM_VALUE, same value WP_SPD 5 → 5.0 out-of-range accepted too: WP_SPD 500 → 500.0
In the gray and red branches the value doesn't change. In the blue branch, compare the number that comes back with the one you wanted.

The gray branch is the hardest to notice. If the target system ID in the packet doesn't match the vehicle's, the autopilot ignores the packet and sends nothing. If you changed the vehicle's ID, or if there are two vehicles on the same link, check this first.

In the red branch, the name isn't in the list. ArduCopter 4.7 answers with a PARAM_ERROR packet in this case, while 4.6 and earlier stay silent and your code times out waiting for a reply.

In the blue branch the value is saved, but if you wrote a decimal or out-of-range number to a parameter that holds an integer, the value that comes back is different. Also look at the box at the bottom right: for decimal parameters there is no range check at write time, and whatever you send is saved.

If you've moved to Copter 4.7, the old names are rejected

ArduCopter 4.7 changed both the name and the unit of a group of parameters. WP_SPD (m/s) replaced WPNAV_SPEED (cm/s), RTL_ALT_M (m) replaced RTL_ALT (cm), and MAV_SYSID replaced SYSID_THISMAV.

On the first boot after an update, the autopilot moves the saved values to the new names itself and converts their units. But it can't touch the names in your Python code. The autopilot rejects every write you send with an old name. To find the new name in Mission Planner, see the article Why WPNAV_SPEED doesn't show up in Mission Planner.

The unit changed too. If you write 500 to WP_SPD out of old habit, the autopilot doesn't object and saves 500 m/s. Even though the parameter list says the range is 0.10 to 20 m/s, the autopilot doesn't check this range during a write.

What happens when you write a decimal to an integer parameter

Option parameters like FS_THR_ENABLE hold integers. This parameter is of type INT8 and takes values from −128 to 127: if you send 1.6, 1 is saved, and if you send 300, 127 is saved.

Some libraries copy an integer into the bytes of the float field as is, while ArduPilot reads the field as a number. If you copy the integer 1 as bytes, the autopilot saves 0. For FS_THR_ENABLE, 0 means the throttle failsafe is off.

Example: loading parameters from a Pi on competition morning

Your team has code from last season: before takeoff, it sets the mission speed to 8 m/s and the return altitude to 30 m from the Pi. The values in the code are still under the old names: WPNAV_SPEED 800, RTL_ALT 3000.

You updated the Pixhawk to 4.7. The code sends both writes, prints "sent" to the screen, and finishes. Because the autopilot rejects both names, the drone flies with the values saved before the update. Code that reads the reply would print the same two rejections to the terminal before takeoff.

Code that verifies what you wrote against the reply

The function below sends the parameter, waits for the PARAM_VALUE or PARAM_ERROR packet that comes back for the same name, and compares the returned value with the one you wanted. Change the connection line to match your own link: for the Pi's serial port, something like mavutil.mavlink_connection('/dev/serial0', baud=57600), and the baud value must match the serial port setting on the Pixhawk.

Pythonparam_yaz.py
import os, time
os.environ['MAVLINK20'] = '1'   # PARAM_ERROR exists only in MAVLink 2
from pymavlink import mavutil

def param_yaz(m, ad, deger, bekle=3):
    m.mav.param_set_send(m.target_system, m.target_component, ad.encode(), float(deger),
                         mavutil.mavlink.MAV_PARAM_TYPE_REAL32)
    son = time.time() + bekle
    while time.time() < son:
        r = m.recv_match(type=['PARAM_VALUE', 'PARAM_ERROR'], blocking=True, timeout=0.5)
        if r is None or r.param_id != ad:
            continue
        if r.get_type() == 'PARAM_ERROR':
            return f"{ad}: autopilot rejected it, error code {r.error}"
        if abs(r.param_value - deger) > 1e-4:
            return f"{ad}: you wrote {deger}, autopilot saved {r.param_value}"
        return f"{ad} = {r.param_value} saved"
    return f"{ad}: no reply (wrong name or sysid mismatch)"

m = mavutil.mavlink_connection('tcp:127.0.0.1:5760', source_system=255)
m.wait_heartbeat()
print(param_yaz(m, 'WPNAV_SPEED', 500))
print(param_yaz(m, 'WP_SPD', 5))
print(param_yaz(m, 'FS_THR_ENABLE', 1.6))

If you run the code, you'll see three lines:

Output
WPNAV_SPEED: autopilot rejected it, error code 1
WP_SPD = 5.0 saved
FS_THR_ENABLE: you wrote 1.6, autopilot saved 1.0

The first line is the red branch: error code 1 says the name doesn't exist in this firmware; replace the name with its new equivalent and unit. In the second line, the value was saved as is. The third line is the blue branch: look up the parameter's type in the list and send an integer.

If you only see "no reply received" in place of the three lines, check the target number. The code takes this number from the first HEARTBEAT packet it receives. If there is another vehicle on the link, print the m.target_system value and compare it with the vehicle's MAV_SYSID value.

If the reply is correct but the drone still flies with the old setting

Some parameters only take effect when the autopilot is rebooted. In the parameter list, these have the note "Reboot required after change" under them, and FRAME_CLASS is in this group. The value is saved, but the old setting stays in effect until you restart the autopilot.

If the value disappears when the board is powered off and on, the problem may be in storage. The autopilot does the permanent save in the background and sends PARAM_VALUE after that save, so don't cut the power before the reply arrives.

Error Messages

MAV_PARAM_ERROR_DOES_NOT_EXIST

The name you sent doesn't exist in this firmware. Search for the name in the parameter list. If you've moved to 4.7, look at the new name and its unit.

Param write denied

The parameter is read-only or locked for internal use. The autopilot sends the old value again alongside this error and the value doesn't change.

MAV_PARAM_ERROR_VALUE_OUT_OF_RANGE

The value you sent is NaN or infinite. The result of an empty read or a division by zero in your code is probably being written.

Which course includes this topic

If you want to learn from scratch how to send commands and parameters from a companion computer to the Pixhawk and read the replies, see the Drone Control with Python from Scratch course. The course version includes code that converts old names along with their units, the trace of a write in the flight log, and the PX4 difference.

Was this article helpful?

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