Strange mavlink local_ned SITL behavior

In the following code a copter is directed to take off, go to a waypoint, then move two meters. It moves to the waypoint, but I am unclear why after the last local_ned statement the copter instead flies back home, whereas if I execute that statement alone once airborne it behaves as expected, ie, moves 2 meters.

from pymavlink import mavutil
import time


# Start a connection listening to a UDP port
vehicle = mavutil.mavlink_connection('udpin:localhost:14551')

# Wait for the first heartbeat 
vehicle.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (vehicle.target_system, vehicle.target_component))

# Arm the vehicle
vehicle.mav.command_long_send(vehicle.target_system, vehicle.target_component, mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, 1, 0, 0, 0, 0, 0, 0)
msg = vehicle.recv_match(type='COMMAND_ACK', blocking=True)
while msg.result != 0:
    print("Arming not ready")
    time.sleep(1)
    msg = vehicle.recv_match(type='COMMAND_ACK', blocking=True)
print(msg)

# Takeoff
vehicle.mav.command_long_send(vehicle.target_system, vehicle.target_component, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, 10)
msg = vehicle.recv_match(type='COMMAND_ACK', blocking=True)
while msg.result != 0:
    print("Takeoff not ready")
    time.sleep(1)
    msg = vehicle.recv_match(type='COMMAND_ACK', blocking=True)
print(msg)

time.sleep(5)

# Go to waypoint
vehicle.mav.send(mavutil.mavlink.MAVLink_set_position_target_global_int_message(10, vehicle.target_system, vehicle.target_component, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, int(0b110111111000), int(32.460904 * 10 ** 7), int(-111.209204 * 10 ** 7), 10, 0, 0, 0, 0, 0, 0, 0, 0))

time.sleep(5)

# Move a few meters
vehicle.mav.send(mavutil.mavlink.MAVLink_set_position_target_local_ned_message(10, vehicle.target_system, vehicle.target_component, mavutil.mavlink.MAV_FRAME_LOCAL_NED, int(0b110111111000), 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
while 1:
    msg = vehicle.recv_match( type='LOCAL_POSITION_NED', blocking=True)
    print(msg)

1 Like

you should make attention in wich frame you work, if you want to go forward 2 meters from the current position, you should use “MAV_FRAME_BODY_OFFSET_NED”