# Import mavutil
from pymavlink import mavutil
import time

def wait_conn(master):
    msg = None
    while not msg:
        master.mav.ping_send(
            time.time(), # Unix time
            0, # Ping number
            0, # Request ping of all systems
            0 # Request ping of all components
        )
        msg = master.recv_match()
        time.sleep(0.5)


# Create the connection
master = mavutil.mavlink_connection('udpout:0.0.0.0:9000')
wait_conn(master)
# Wait a heartbeat before sending commands
def wait_heartbeat(m):
    print("Wait for PX4 Heartbeat")
    m.wait_heartbeat()
    print("Heartbeat from PX4 (system %u component %u)" % (m.target_system, m.target_system))

wait_heartbeat(master)

# Choose a mode
mode = 'STABILIZE'

# Check if mode is available
if mode not in master.mode_mapping():
    print('Unknown mode : {}'.format(mode))
    print('Try:', list(master.mode_mapping().keys()))
    exit(1)

# Get mode ID
mode_id = master.mode_mapping()[mode]
# Set new mode
# master.mav.command_long_send(
#    master.target_system, master.target_component,
#    mavutil.mavlink.MAV_CMD_DO_SET_MODE, 0,
#    0, mode_id, 0, 0, 0, 0, 0) or:
# master.set_mode(mode_id) or:
master.mav.set_mode_send(
    master.target_system,
    mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED,
    mode_id)

# Check ACK
ack = False
while not ack:
    # Wait for ACK command
    ack_msg = master.recv_match(type='COMMAND_ACK', blocking=True)
    ack_msg = ack_msg.to_dict()

    # Check if command in the same in `set_mode`
    if ack_msg['command'] != mavutil.mavlink.MAVLINK_MSG_ID_SET_MODE:
        continue

    # Print the ACK result !
    print(mavutil.mavlink.enums['MAV_RESULT'][ack_msg['result']].description)
    break

c5c4a9e2fc63154295ef9888a735e69bd8c88661
<mask token>


def wait_conn(master):
    msg = None
    while not msg:
        master.mav.ping_send(time.time(), 0, 0, 0)
        msg = master.recv_match()
        time.sleep(0.5)


<mask token>


def wait_heartbeat(m):
    print('Wait for PX4 Heartbeat')
    m.wait_heartbeat()
    print('Heartbeat from PX4 (system %u component %u)' % (m.target_system,
        m.target_system))


<mask token>

<mask token>


def wait_conn(master):
    msg = None
    while not msg:
        master.mav.ping_send(time.time(), 0, 0, 0)
        msg = master.recv_match()
        time.sleep(0.5)


<mask token>
wait_conn(master)


def wait_heartbeat(m):
    print('Wait for PX4 Heartbeat')
    m.wait_heartbeat()
    print('Heartbeat from PX4 (system %u component %u)' % (m.target_system,
        m.target_system))


wait_heartbeat(master)
<mask token>
if mode not in master.mode_mapping():
    print('Unknown mode : {}'.format(mode))
    print('Try:', list(master.mode_mapping().keys()))
    exit(1)
<mask token>
master.mav.set_mode_send(master.target_system, mavutil.mavlink.
    MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, mode_id)
<mask token>
while not ack:
    ack_msg = master.recv_match(type='COMMAND_ACK', blocking=True)
    ack_msg = ack_msg.to_dict()
    if ack_msg['command'] != mavutil.mavlink.MAVLINK_MSG_ID_SET_MODE:
        continue
    print(mavutil.mavlink.enums['MAV_RESULT'][ack_msg['result']].description)
    break

<mask token>


def wait_conn(master):
    msg = None
    while not msg:
        master.mav.ping_send(time.time(), 0, 0, 0)
        msg = master.recv_match()
        time.sleep(0.5)


master = mavutil.mavlink_connection('udpout:0.0.0.0:9000')
wait_conn(master)


def wait_heartbeat(m):
    print('Wait for PX4 Heartbeat')
    m.wait_heartbeat()
    print('Heartbeat from PX4 (system %u component %u)' % (m.target_system,
        m.target_system))


wait_heartbeat(master)
mode = 'STABILIZE'
if mode not in master.mode_mapping():
    print('Unknown mode : {}'.format(mode))
    print('Try:', list(master.mode_mapping().keys()))
    exit(1)
mode_id = master.mode_mapping()[mode]
master.mav.set_mode_send(master.target_system, mavutil.mavlink.
    MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, mode_id)
ack = False
while not ack:
    ack_msg = master.recv_match(type='COMMAND_ACK', blocking=True)
    ack_msg = ack_msg.to_dict()
    if ack_msg['command'] != mavutil.mavlink.MAVLINK_MSG_ID_SET_MODE:
        continue
    print(mavutil.mavlink.enums['MAV_RESULT'][ack_msg['result']].description)
    break

from pymavlink import mavutil
import time


def wait_conn(master):
    msg = None
    while not msg:
        master.mav.ping_send(time.time(), 0, 0, 0)
        msg = master.recv_match()
        time.sleep(0.5)


master = mavutil.mavlink_connection('udpout:0.0.0.0:9000')
wait_conn(master)


def wait_heartbeat(m):
    print('Wait for PX4 Heartbeat')
    m.wait_heartbeat()
    print('Heartbeat from PX4 (system %u component %u)' % (m.target_system,
        m.target_system))


wait_heartbeat(master)
mode = 'STABILIZE'
if mode not in master.mode_mapping():
    print('Unknown mode : {}'.format(mode))
    print('Try:', list(master.mode_mapping().keys()))
    exit(1)
mode_id = master.mode_mapping()[mode]
master.mav.set_mode_send(master.target_system, mavutil.mavlink.
    MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, mode_id)
ack = False
while not ack:
    ack_msg = master.recv_match(type='COMMAND_ACK', blocking=True)
    ack_msg = ack_msg.to_dict()
    if ack_msg['command'] != mavutil.mavlink.MAVLINK_MSG_ID_SET_MODE:
        continue
    print(mavutil.mavlink.enums['MAV_RESULT'][ack_msg['result']].description)
    break

# Import mavutil
from pymavlink import mavutil
import time

def wait_conn(master):
    msg = None
    while not msg:
        master.mav.ping_send(
            time.time(), # Unix time
            0, # Ping number
            0, # Request ping of all systems
            0 # Request ping of all components
        )
        msg = master.recv_match()
        time.sleep(0.5)


# Create the connection
master = mavutil.mavlink_connection('udpout:0.0.0.0:9000')
wait_conn(master)
# Wait a heartbeat before sending commands
def wait_heartbeat(m):
    print("Wait for PX4 Heartbeat")
    m.wait_heartbeat()
    print("Heartbeat from PX4 (system %u component %u)" % (m.target_system, m.target_system))

wait_heartbeat(master)

# Choose a mode
mode = 'STABILIZE'

# Check if mode is available
if mode not in master.mode_mapping():
    print('Unknown mode : {}'.format(mode))
    print('Try:', list(master.mode_mapping().keys()))
    exit(1)

# Get mode ID
mode_id = master.mode_mapping()[mode]
# Set new mode
# master.mav.command_long_send(
#    master.target_system, master.target_component,
#    mavutil.mavlink.MAV_CMD_DO_SET_MODE, 0,
#    0, mode_id, 0, 0, 0, 0, 0) or:
# master.set_mode(mode_id) or:
master.mav.set_mode_send(
    master.target_system,
    mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED,
    mode_id)

# Check ACK
ack = False
while not ack:
    # Wait for ACK command
    ack_msg = master.recv_match(type='COMMAND_ACK', blocking=True)
    ack_msg = ack_msg.to_dict()

    # Check if command in the same in `set_mode`
    if ack_msg['command'] != mavutil.mavlink.MAVLINK_MSG_ID_SET_MODE:
        continue

    # Print the ACK result !
    print(mavutil.mavlink.enums['MAV_RESULT'][ack_msg['result']].description)
    break
