Drone stops in GUIDED mode: check the ArduPilot timeout
You send a velocity command in GUIDED mode, and the drone moves for a few seconds and then stops in the air. ArduPilot drops the command after three seconds. The cause and the one-line fix.
You run the code, the drone moves forward for two or three seconds, then hangs in the air. The connection hasn't dropped, the flight mode is still GUIDED, and the code is still running. There isn't a single line on the screen warning you.
When you run the same code again, the same thing happens: a short move, then a stop. This article explains where that stop comes from and which single line fixes the code.
The code is running, the vehicle stops
The autopilot is behaving as designed here. It doesn't treat the velocity command you sent as a permanent order. If a certain amount of time passes and no new command arrives, it resets the target on its own.
The reason is safety. If the companion computer crashes, the code errors out and exits, or the cable comes loose, the vehicle shouldn't keep flying toward the horizon at the last velocity it heard. The autopilot quietly slows down and waits.
The timer resets with every new command, so as long as the command stream continues, the vehicle keeps moving. When the stream stops and the timer runs out, the autopilot resets the velocity target, and the vehicle slows down and hangs in the air.
Meanwhile the flight mode stays GUIDED and no warning is printed on screen. The code still running doesn't change anything; what matters is when the last command arrived.
How long the autopilot holds the command
This duration isn't fixed; it depends on a parameter. On ArduCopter 4.7 its default is 3 seconds, and its adjustable range is 0.1 to 5 seconds.
See in the figure the difference between sending the same velocity command once and leaving it, and refreshing it every half second. In the top strip, look at when the commands are sent; in the curves below, look at what the velocity does.
The same velocity command: sent once vs. refreshed
On the red curve the vehicle almost reaches the target velocity, then comes down to zero within the acceleration limit. The drop isn't sudden, because when the autopilot resets the target it stops the vehicle as if braking.
On ArduCopter 4.7, a single command with a 5 m/s north target peaks at 4.79 m/s at second 3.26 and drops to zero. On the blue curve, the velocity holds steady for nine seconds.
Put the command in a loop
The fix is to refresh the command for as long as the movement continues, instead of sending it once. Keep the send interval well below the GUID_TIMEOUT value. Half a second is a comfortable choice and doesn't strain the telemetry link either.
The code below connects to SITL, sends the same velocity command every half second for twelve seconds, and prints the horizontal velocity it measures meanwhile. Run it while the vehicle is in GUIDED mode and in the air.
from pymavlink import mavutil
import time
TAZELEME = 0.5 # seconds · must stay below GUID_TIMEOUT
HEDEF_HIZ = 5.0 # m/s · northward
SURE = 12 # seconds · duration of the command stream
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, 10, 1)
TYPE_MASK_HIZ = 0b0000111111000111 # only the velocity fields are valid
def hiz_gonder(kuzey, dogu, asagi):
baglanti.mav.set_position_target_local_ned_send(
0, baglanti.target_system, baglanti.target_component,
mavutil.mavlink.MAV_FRAME_LOCAL_NED, TYPE_MASK_HIZ,
0, 0, 0, kuzey, dogu, asagi, 0, 0, 0, 0, 0)
baslangic = time.time()
son_gonderim = 0.0
while time.time() - baslangic < SURE:
simdi = time.time()
if simdi - son_gonderim >= TAZELEME:
hiz_gonder(HEDEF_HIZ, 0.0, 0.0)
son_gonderim = simdi
p = baglanti.recv_match(type="LOCAL_POSITION_NED", blocking=True, timeout=1)
if p:
hiz = (p.vx ** 2 + p.vy ** 2) ** 0.5
print(f"t+{simdi - baslangic:4.1f} s horizontal speed {hiz:5.2f} m/s")When you run it, you get output like this. The first seconds show the acceleration, the rest show a constant velocity:
t+ 0.0 s horizontal speed 0.00 m/s
t+ 1.0 s horizontal speed 1.96 m/s
t+ 2.0 s horizontal speed 4.32 m/s
t+ 3.0 s horizontal speed 4.83 m/s
t+ 4.0 s horizontal speed 5.02 m/s
t+ 5.0 s horizontal speed 5.09 m/s
t+ 6.0 s horizontal speed 5.06 m/s
t+ 7.0 s horizontal speed 5.02 m/s
t+ 8.0 s horizontal speed 5.01 m/s
t+ 9.0 s horizontal speed 5.02 m/s
t+10.0 s horizontal speed 5.02 m/s
t+11.0 s horizontal speed 4.99 m/s
t+12.0 s horizontal speed 4.99 m/sIf the velocity holds after the third second too, the refresh is working. If you run the same code without the refresh line, you'll see the red curve on your own screen.
Example: for the lock-on task in the Fighting UAV rotary wing category, the image processing code on the companion computer sends a velocity command once when it finds the target and goes back to processing the next frames. The vehicle moves toward the target for three seconds, then hangs, and the lock-on breaks before its time is up. Resend the command on every pass of the image processing loop, even if the target is in the same place. When you lose the target, stop sending, and the autopilot will stop the vehicle on its own after three seconds.
Error Messages and Their Meanings
Three lines you'll run into while searching for this topic, and what they actually mean.
Mode change to GUIDED failed: requires position
Switching to GUIDED mode was rejected. The autopilot has no position estimate it trusts. The GPS fix or the EKF state isn't in place yet. The code's velocity command never even gets its turn. Wait for the position estimate to settle first.
PreArm: Need Position Estimate
The counterpart of the previous one at the ARM step. The autopilot won't allow arming without knowing its position. If you're going to work in GUIDED mode, no movement command will have any effect until this line clears.
target not received last 3.0 secs, stopping
This warning is printed by ArduPilot's Rover firmware, and it reports exactly the stop described here. On the Copter side, no text is sent in the same situation; the vehicle stops silently. That's why you see the warning when you try the same code on Rover and not on Copter.
Which course covers this topic
When writing autonomous flight from a companion computer, you run into traps like this one by one. The course where we go through command refreshing, GUIDED mode commands, mission upload, and the MAVLink side in order is on the Drone Control with Python from Scratch page.
In the lesson version of the same topic, we follow the autopilot's decision order through the source code, extract the log signatures, and cover board-specific differences.
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