0

我对 Python 上的 Dronekit 有疑问

我的项目关于从 Android 应用程序接收 GPS 位置并启动无人机飞到那个位置,一切正常,但问题是无人机可以起飞,但无人机没有到达那个位置(测试 10 次以上只工作 1 次)

这是我的代码(我认为问题是 GlobalRelative)

 # Import DroneKit-Python
from dronekit import connect, VehicleMode, LocationGlobalRelative
import time
import urllib2
import simplejson
import json
import requests


#Wait for json data
response = urllib2.urlopen("http://localhost:3000/posts")
data = simplejson.load(response)
print(data)
json_string = data[0]["Information"]

gps = json.loads(json_string)
x=gps['lat']
y=gps['lon']
r = requests.delete('http://localhost:3000/posts/1')
# Connect to the Vehicle.
print("Connecting")
vehicle = connect('com4', wait_ready=False, baud=57600)#; vehicle.wait_ready(True, timeout=300)
print("Connected")
# Get some vehicle attributes (state)
print "Get some vehicle attribute values:"
print " GPS: %s" % vehicle.gps_0
print " Battery: %s" % vehicle.battery
print " Last Heartbeat: %s" % vehicle.last_heartbeat
print " Is Armable?: %s" % vehicle.is_armable
print " System status: %s" % vehicle.system_status.state
print " Mode: %s" % vehicle.mode.name    # settable

# Takeoff Function
def arm_and_takeoff(tgt_altitude):
    print("Arming motors")

#   while not vehicle.is_armable:
#       time.sleep(1)

    vehicle.mode = VehicleMode("GUIDED")
    vehicle.armed = True

    print("Takeoff")
    vehicle.simple_takeoff(tgt_altitude)

    # wait reach tgt_altitude
    while True:
        altitude = vehicle.location.global_relative_frame.alt

        if altitude >= tgt_altitude -1:
            print("Altitude Reached")
            break

        time.sleep(1)

# Main
arm_and_takeoff(10)

# Speed
vehicle.airspeed = 7

# Go to wp
wp1 = LocationGlobalRelative(x, y, 10)

# Close vehicle object before exiting script

vehicle.mode = VehicleMode("RTL")
vehicle.close()


print("Completed")

或者,如果我无法解决这个问题,我想使用 MissionPlanner(我对其进行了测试,它可以工作)但我想等待来自电话的 GPS 定位,然后启动任务(每件事都必须自动)我不知道如何绕过 MissionPlanner

4

1 回答 1

0

该行:wp1 = LocationGlobalRelative(x, y, 10)只为 wp1 变量分配位置坐标。您可以使用vehicle.simple_goto(wp1). simple_goto 是dronekit 中的内置函数,用于命令车辆到特定坐标,您可以在此处阅读更多信息http://python.dronekit.io/automodule.html?highlight=simple_goto#dronekit.Vehicle.simple_goto

于 2018-08-31T05:56:32.603 回答