如何使用python dronekit“向左/向右/向前/向后”?

时间:2016-09-26 05:22:41

标签: dronekit-python dronekit

我正在使用APM自动驾驶我的hexacopter并遵循this教程。通过查看this可用命令,我看不出如何命令无人机进入左/右/前进/后退?

有人可以帮我吗?

3 个答案:

答案 0 :(得分:2)

您需要创建vehicle.message_factory.set_position_target_local_ned_encode。 它需要一个mavutil.mavlink.MAV_FRAME_BODY_NED框架。 您可以将所需的x,y和/或z速度(以m / s为单位)添加到消息中。

from pymavlink import mavutil
from dronekit import connect, VehicleMode, LocationGlobalRelative
import time

def send_body_ned_velocity(velocity_x, velocity_y, velocity_z, duration=0):
    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_BODY_NED, # frame Needs to be MAV_FRAME_BODY_NED for forward/back left/right control.
        0b0000111111000111, # type_mask
        0, 0, 0, # x, y, z positions (not used)
        velocity_x, velocity_y, velocity_z, # m/s
        0, 0, 0, # x, y, z acceleration
        0, 0)
    for x in range(0,duration):
        vehicle.send_mavlink(msg)
        time.sleep(1)

connection_string = 'tcp:192.168.1.2:5760' # Edit to suit your needs.
takeoff_alt = 10
vehicle = connect(connection_string, wait_ready=True)
while not vehicle.is_armable:
    time.sleep(1)
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
while not vehicle.armed:
    print('Waiting for arming...')
    time.sleep(1)
vehicle.simple_takeoff(takeoff_alt) # Take off to target altitude
while True:
    print('Altitude: %d' %  self.vehicle.location.global_relative_frame.alt)
    if vehicle.location.global_relative_frame.alt >= takeoff_alt * 0.95:
        print('REACHED TARGET ALTITUDE')
        break
    time.sleep(1)

# This is the command to move the copter 5 m/s forward for 10 sec.
velocity_x = 0
velocity_y = 5
velocity_z = 0
duration = 10
send_body_ned_velocity(velocity_x, velocity_y, velocity_z, duration)

# backwards at 5 m/s for 10 sec.
velocity_x = 0
velocity_y = -5
velocity_z = 0
duration = 10
send_body_ned_velocity(velocity_x, velocity_y, velocity_z, duration)

vehicle.mode = VehicleMode("LAND")

玩得开心,当然,在编程和飞行无人机时要注意严格的安全防护装置。手动模式覆盖是必须的!

答案 1 :(得分:0)

Dronekit-python具有非常简单的API来命令本地帧中的无人机。根据我的个人经验,我很难理解使用这些命令让我的无人机在本地遵循一个形状,例如正方形或圆形。 另一种方法是使用FlytOS drone APIs。如果你看到这个样本python code on github,你可以看到指挥无人机离开x米然后前进y米等是多么容易。乔恩的回答确实正确显示了如何用dronekit来实现什么你正试图做,但另一个可能被复杂代码吓倒的初学者。

答案 2 :(得分:0)

我遇到了同样的问题,希望该链接有助于查找文章here的“通过指定车辆的速度分量进行控制”部分