1

请帮助我了解如何在无人机 API 中更改高度。我找不到有关此的信息。即使在源代码中。

我有一个简单的项目,无人机必须做几个动作。准备好电机(当然要进行飞行前检查),起飞 10 米,等待 20 秒。并爬到20米。

那么我用什么函数来强制我的多旋翼在 20 米内爬升,或者在 5 米内下降呢?

请告诉我一个类似的用于 MAVProxy 的“cmd”命令。

4

1 回答 1

1

干得好:

def gotoGPS(location):
    currentLocation = vehicle.location
    targetDistance = get_distance_metres(currentLocation, location)
    gotoFunction(location)
    vehicle.flush()
    while not api.exit and vehicle.mode.name=="GUIDED": #Stop action if we are no longer in guided mode.
        remainingDistance=get_distance_metres(vehicle.location, location)
        if remainingDistance<=targetDistance*0.01: #Just below target, in case of undershoot.
            print "Reached target"
            break
        time.sleep(2) 


 newLoc = Location(vehicle.location.lat, vehicle.location.lon, vehicle.location.alt+2)
 gotoGPS(newLoc)

编辑:您可以找到以下所有代码:

from droneapi.lib import VehicleMode, Location
from pymavlink import mavutil
import time
import math

api = local_connect()
vehicle= api.get_vehicles()[0]

def arm_and_takeoff(aTargetAltitude):
    """
    Arm vehicle and fly to aTargetAltitude.
    """
    print "Basic pre-arm checks"
    # Don't let the user try to fly while autopilot is booting
    if vehicle.mode.name == "INITIALISING":
        print "Waiting for vehicle to initialise"
        time.sleep(1)
    while vehicle.gps_0.fix_type < 2:
        print "Waiting for GPS...:", vehicle.gps_0.fix_type
        time.sleep(1)

    print "Arming motors"
    # Copter should arm in GUIDED mode
    vehicle.mode    = VehicleMode("GUIDED")
    vehicle.armed   = True
    vehicle.flush()

    while not vehicle.armed and not api.exit:
        print " Waiting for arming..."
        time.sleep(1)

    print "Taking off!"
    vehicle.commands.takeoff(aTargetAltitude) # Take off to target altitude
    vehicle.flush()

    # Wait until the vehicle reaches a safe height before processing the goto (otherwise the command 
    #  after Vehicle.commands.takeoff will execute immediately).
    while not api.exit:
        print " Altitude: ", vehicle.location.alt
        if vehicle.location.alt>=aTargetAltitude*0.95: #Just below target, in case of undershoot.
            print "Reached target altitude"
            break
        time.sleep(1)

def get_distance_metres(aLocation1, aLocation2):
    dlat        = aLocation2.lat - aLocation1.lat
    dlong       = aLocation2.lon - aLocation1.lon
    return math.sqrt((dlat*dlat) + (dlong*dlong)) * 1.113195e5

def goto_position_target_global_int(aLocation):
    """
    Send SET_POSITION_TARGET_GLOBAL_INT command to request the vehicle fly to a specified location.

    For more information see: https://pixhawk.ethz.ch/mavlink/#SET_POSITION_TARGET_GLOBAL_INT

    See the above link for information on the type_mask (0=enable, 1=ignore). 
    At time of writing, acceleration and yaw bits are ignored.
    """
    msg = vehicle.message_factory.set_position_target_global_int_encode(
        0,       # time_boot_ms (not used)
        0, 0,    # target system, target component
        mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame      
        0b0000111111111000, # type_mask (only speeds enabled)
        aLocation.lat*1e7, # lat_int - X Position in WGS84 frame in 1e7 * meters
        aLocation.lon*1e7, # lon_int - Y Position in WGS84 frame in 1e7 * meters
        aLocation.alt, # alt - Altitude in meters in AMSL altitude, not WGS84 if absolute or relative, above terrain if GLOBAL_TERRAIN_ALT_INT
        0, # X velocity in NED frame in m/s
        0, # Y velocity in NED frame in m/s
        0, # Z velocity in NED frame in m/s
        0, 0, 0, # afx, afy, afz acceleration (not supported yet, ignored in GCS_Mavlink)
        0, 0)    # yaw, yaw_rate (not supported yet, ignored in GCS_Mavlink) 
    # send command to vehicle
    vehicle.send_mavlink(msg)
    vehicle.flush()



def gotoGPS(location):
    currentLocation = vehicle.location
    targetDistance = get_distance_metres(currentLocation, location)
    goto_position_target_global_int(location)
    vehicle.flush()
    while not api.exit and vehicle.mode.name=="GUIDED": #Stop action if we are no longer in guided mode.
        remainingDistance=get_distance_metres(vehicle.location, location)
        if remainingDistance<=targetDistance*0.05: #Just below target, in case of undershoot.
            print "Reached target"
            break
        time.sleep(2)

arm_and_takeoff(10)
time.sleep(2)
newLoc = Location (vehicle.location.lat, vehicle.location.lon, vehicle.location.alt+10)
gotoGPS(newLoc)
time.sleep(10)
vehicle.mode = VehicleMode("LAND")
vehicle.flush()

干杯!

尼古拉斯

于 2015-09-03T19:47:38.107 回答