2016-09-26 207 views

回答

0

您需要創建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") 

玩得開心,當然,在編程和飛行無人機時要注意嚴密的安全衛士。手動模式覆蓋是必須的!

0

Dronekit-python有非常簡單的API來命令本地框架中的無人機。從我的個人經驗來看,很難使用這些命令使我的無人機在本地跟隨形狀,例如正方形或圓形。 另一種方法是使用FlytOS drone APIs。如果你看到這個樣本python code on github,你可以看到命令無人機向左走x米然後向前走米是多麼容易。Jon的答案確實顯示瞭如何使用dronekit來實現你想要做的事情,但是另一個初學者可能會被複雜的代碼嚇倒。

相關問題