#!/usr/bin/env python # -*- coding: utf-8 -*- # Import DroneKit-Python from dronekit import connect, VehicleMode from pymavlink import mavutil import time ################### Connect to the UAV ################### connection_string = "tcp:192.168.8.1:5760" print "Connecting to vehicle on: %s ..." % (connection_string) vehicle = connect(connection_string, wait_ready=False) print "... connected" ########################################################## ################### Wait for local location ################### local_position_set = False # Create a message listener for home position fix @vehicle.on_message('LOCAL_POSITION_NED') def listener(self, name, local_position): global local_position_set if not local_position_set: local_position_set = True # wait for a home position lock while not local_position_set: print "Waiting for local position..." time.sleep(1) ########################################################## ################### Arm the vehicle ################### vehicle.mode = VehicleMode('POSCTL') time.sleep(1) print "New mode (%s) set." % vehicle.mode.name print "Arming the vehicle ..." vehicle.armed = True time.sleep(10) print "... done." ########################################################## ################### Home position in local frame ################### print "Home local position: %s." % vehicle.location.local_frame x = vehicle.location.local_frame.north y = vehicle.location.local_frame.east ################### Take off ################### type_mask = 0b110111000000 # only position and velocity bits are enabled type_mask = type_mask | 0x1000 # set the is_takeoff_sp bit vehicle.mode = VehicleMode('OFFBOARD') z = -0.5 msg = vehicle.message_factory.set_position_target_local_ned_encode( 0, # time_boot_ms (not used) 0, 0, # target system, target component mavutil.mavlink.MAV_FRAME_LOCAL_NED, # frame type_mask, # type_mask x, y, z, # x, y and z in m 0, 0, 0.1, # x, y, z velocity in m/s 0, 0, 0, # x, y, z acceleration 0, 0) # yaw, yaw_rate print "Taking off ..." while True : vehicle.send_mavlink(msg) vehicle.flush() if -vehicle.location.local_frame.down >= -z * 0.95: #Trigger just below target alt. print "Reached target altitude (%s)." % (-vehicle.location.local_frame.down) break time.sleep(0.1) ########################################################## ################### Loiter ################### type_mask = 0b110111111000 # only position and velocity bits are enabled type_mask = type_mask | 0x3000 # set the is_loiter_sp bit msg = vehicle.message_factory.set_position_target_local_ned_encode( 0, # time_boot_ms (not used) 0, 0, # target system, target component mavutil.mavlink.MAV_FRAME_LOCAL_NED, # frame type_mask, # type_mask x, y, z, # x, y and z in m 0, 0, 0, # x, y, z velocity in m/s (not used) 0, 0, 0, # x, y, z acceleration (not supported yet, ignored in GCS_Mavlink) 0, 0) # yaw, yaw_rate (not supported yet, ignored in GCS_Mavlink) print "Loitering for 5s ..." while True: vehicle.send_mavlink(msg) vehicle.flush() time.sleep(0.1) ##########################################################
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question