Why the Pixhawk payload release servo won't turn from Python
You send a servo command from a Raspberry Pi with Python, and the Pixhawk payload release servo won't turn. How to tell a rejected channel from the output that comes back, with code.
You've mounted the payload release mechanism and you're trying to open the servo from Python code on the Raspberry Pi. The code finishes without errors, telemetry is flowing, and Mission Planner looks connected. The servo doesn't move at all. Sometimes it turns for a moment and snaps back to its old position.
There are three possible causes: the autopilot may have rejected the command, it may have accepted it and handed control back to the transmitter, or the output may be correct but no power is reaching the servo. Instead of guessing which, you need to read the reply the autopilot gives.
How the Pixhawk answers the command
When you send a servo command, the Pixhawk returns an acknowledgment packet. If your code doesn't read this packet, you'll never find out that the command was rejected.
If the packet never arrives, the line from the Pi to the Pixhawk is broken. The first place to look is the Pi's TX pin. If "Rejected" comes back, the autopilot has assigned that channel to something else. If "Accepted" comes back but the servo doesn't turn, the problem is in the wiring or the power.
If the channel's function is assigned to something else, the command is rejected
ArduPilot assigns a function to each output channel and applies a servo command only to channels with certain functions: those with no function, those that pass the transmitter through as is, the spraying pump, and the gripper. A command sent to a pin assigned to a motor channel or a relay is rejected.
Look at the red frame in the figure below: on a quad, the first four main outputs are motors, and servo commands don't go to them. If you've plugged the servo into AUX OUT 1, the channel number you write in the command is the blue 9 above the column. Ignore the "1" printed on the board.
Where the payload servo plugs in, and which number it gets in the command
The yellow dashed frame is AUX 5 and 6. According to the ArduPilot docs, on some boards these two pins are assigned to a relay by default. Plugging the servo into one of the first four AUX outputs is less likely to surprise you. Also set the function of the channel you plugged into to 0. For AUX OUT 1, that means SERVO9_FUNCTION = 0.
If the servo turns for a moment and snaps back
This is where teams get stuck most. To be able to open the servo from the transmitter too, you set the channel to the "pass the transmitter through" function. The autopilot accepts the Python command, and the servo turns. The moment the switch or stick on the transmitter moves a little, the output goes back to the transmitter's value.
Here the autopilot looks at that channel's deadzone (RCn_DZ). The hatched band in the graph is this zone: the two sides of the transmitter value at the moment the command arrived. As long as the transmitter stays inside the band, the servo stays at 1900 from the command. The moment it crosses the edge of the band, the output jumps to the transmitter's value and stays there until a new command arrives.
The servo went to 1900 from the command; what happens as the transmitter input moves
The default deadzone is 0, meaning there is no band at all: even a one-microsecond wobble at the receiver pulls the servo back. If you drive the payload servo only from Python, set the function to 0. If you also need to drive it from the transmitter, widen the deadzone of that transmitter channel. If you enter RC10_DZ = 30, wobbles within ±30 µs won't move the servo.
For functions that pass the transmitter input through scaled (140-155), the value you want doesn't come out as is. With the default SERVOn_MIN/MAX (1100/1900), if you ask for 1900 you'll see 1820 at the output.
Example: a TEKNOFEST payload release mechanism
The most common setup for payload release is this: the servo is screwed under the body, and the payload's ring is slipped over the tip of its arm. While the arm is horizontal, the ring hangs on top; when the arm rotates down, the ring slides off the tip of the arm and the payload drops.
In this setup, two commands from Python are enough. Before takeoff you send the channel to 1100 to put the arm in the locked position, and when you're over the target you send 1900. Look at the two positions in the figure below. If the channel goes back to the transmitter because of the deadzone described above, the arm stops at the transmitter's value. The payload either drops early or never drops.
Example mission: the payload release mechanism works with two commands
Which direction the arm turns and how far depends on your mechanism. First, on the ground with no payload attached, send the two commands in sequence and see whether the arm actually releases the ring. If needed, change the 1100 and 1900 values in the command to suit your mechanism.
Code that sends the command and reads the reply
The code below sends the command, prints the autopilot's reply and the explanation text if there is one, then reads the servo output value. On the Raspberry Pi, change the connection line to the serial port.
from pymavlink import mavutil
BAGLANTI = "tcp:127.0.0.1:5760" # Example on a Raspberry Pi: "/dev/serial0", baud=921600
KANAL = 9 # Pixhawk AUX OUT 1 = servo 9
PWM = 1900
m = mavutil.mavlink_connection(BAGLANTI, baud=921600)
m.wait_heartbeat()
m.mav.command_long_send(m.target_system, m.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_SERVO, 0,
KANAL, PWM, 0, 0, 0, 0, 0)
sonuc = None
while True:
msg = m.recv_match(type=["COMMAND_ACK", "STATUSTEXT"], blocking=True,
timeout=1.5 if sonuc else 3)
if msg is None:
if sonuc is None:
print("No reply: the link may be one-way (TX/RX)")
break
if msg.get_type() == "STATUSTEXT":
print("Autopilot:", msg.text)
elif msg.command == mavutil.mavlink.MAV_CMD_DO_SET_SERVO:
sonuc = mavutil.mavlink.enums["MAV_RESULT"][msg.result].name
print("Sonuc:", sonuc)
m.mav.command_long_send(m.target_system, m.target_component,
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL, 0,
36, 100000, 0, 0, 0, 0, 0)
cikis = m.recv_match(type="SERVO_OUTPUT_RAW", blocking=True, timeout=3)
if cikis:
print("Servo %d output: %d us" % (KANAL, getattr(cikis, "servo%d_raw" % KANAL)))If you send it to AUX OUT 1 (channel 9), whose function is 0, you'll see this:
Sonuc: MAV_RESULT_ACCEPTED
Servo 9 output: 1900 usIf you set the channel to 1, meaning you send the command to a motor channel, you'll see this:
Sonuc: MAV_RESULT_FAILED
Autopilot: ServoRelayEvent: Channel 1 is already in use
Servo 1 output: 1000 usIf it was accepted but the servo is still dead
The ArduPilot docs are clear: the Pixhawk doesn't power the servo rail. Look at where the UBEC is plugged in in the first figure. If the rail isn't powered by a separate 5V source, the servo gets the signal but doesn't turn. Measure 5V between + and − on the AUX rail with a multimeter.
To power the rail, it's safer to use a separate UBEC instead of the ESC's 5V output. The BEC inside ESCs is often a linear regulator, and it heats up as the servo draws current under load. A switching UBEC delivers the same current while heating up much less. Plug the UBEC's output into the + and − pins of an empty column; the signal pin stays empty.
The second suspect is the safety switch. If the switch hasn't been pressed, the outputs' PWM stays off. There is a BRD_SAFETY_MASK parameter to leave specific channels enabled. If the value changes on Mission Planner's Servo Output screen but the servo doesn't twitch, the problem is in the power, the safety switch, or the wiring.
Error Messages
ServoRelayEvent: Channel 1 is already in use
The function of the channel you sent the command to doesn't allow servo commands: motor, GPIO, relay, or another feature. Send the command to an empty channel, or set that channel's SERVOx_FUNCTION value to 0.
MAV_RESULT_FAILED
If there's no explanation text alongside, the channel number is invalid. The most common case is sending channel 0: channels start at 1.
Which course includes this topic
If you want to learn from scratch how to send commands from a companion computer to the Pixhawk and read the reply, see the Drone Control with Python from Scratch course. The course version has the autopilot's decision order and finding the fault in the flight log step by step.
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