无人机编程Arducopter基本Python脚本通过任务计划

2024-05-19 17:02:48 发布

您现在位置:Python中文网/ 问答频道 /正文

有通过任务规划程序驾驶无人机的知识的人能在下面帮助我吗?在

我是无人机初学者。我正试图通过使用Python的任务规划器,对四旋翼机进行一个非常基本的导航,比如起飞、悬停和着陆。在

我在基础上有些困难。我一直在检查示例代码并编写一些脚本。在

我写了一个基本的脚本,只有起飞,悬停和着陆。我遇到了一些问题, -起飞方向相反 -起飞不是垂直直线的 -应控制高度。它不能超过一米。 -速度很快。我如何减少它?在

还有, 当启动马达时,我在示例代码中看到我们正在将偏航设置为最大值(2000)并将其返回空档(1500)。为什么? 以下是我对雷达散射截面的理解, RC1-翻滚 RC2-节距 RC3-油门 RC4-偏航 RC5-用于改变模式。在

有人能帮我解决这些问题吗?我在下面附上了我的示例代码。在

谢谢你!在

import time

takeoff_throttle = 1700

def setup_rc():
    print("Set up all RCs to neutral")
    for chan in range(1,9):
        Script.SendRC(chan, 1500, False)
    SendRC(3, Script.GetParam('RC3_MIN'), True)
    Script.Sleep(5000)
    print("Done")

    return True

def arm_motors():
    "Arming motors"
    print('APM: ARMING MOTORS')  
    print("Setting mode to Stabilize")
    Script.ChangeMode("STABILIZE")  # STABILIZE MODE
    Script.SendRC(5, 1750, True)
    print("Done")
    print('Setting Throttle to minimum')
    Script.SendRC(3,1000,True)
    print("Done")
    print('Set Yaw')
    Script.SendRC(4,2000,True)
    print("Done")
    print('Waiting for Arming Motors')
    cs.messages.Clear()
    Script.WaitFor('ARMING MOTORS',20000)       #30000(30sec)
    print("Done")
    print('Reset Yaw')
    Script.SendRC(4,1500,True)
    print("Done")

    return True

def wait_altitude(alt_min, alt_max, timeout=30):
    climb_rate = 0
    previous_alt = 0
'''wait for a given altitude range'''
    tstart = time.time()
    print("Waiting for altitude between %u and %u" % (alt_min, alt_max))
    while time.time() < tstart + timeout:
    #m = mav.recv_match(type='VFR_HUD', blocking=True)
        climb_rate =  cs.alt - previous_alt
        previous_alt = cs.alt
    #print("Wait Altitude: Cur:%u, min_alt:%u, climb_rate: %u" % (cs.alt, alt_min , climb_rate))
        if cs.alt >= alt_min and cs.alt <= alt_max:
           print("Attained altitude:%u"%(cs.alt))
           print("Altitude OK")
           return True
    print("Failed to attain altitude range")
    return False

def takeoff(alt_min=0.5):
   print("Entered takeoff")
   print("About to takeoff")
   failed_status = False

   print("Make sure we are in stabilise mode")
   Script.ChangeMode("STABILIZE")
   Script.SendRC(5, 1750, True)
   print("Done")
   print("Increase the throttle")
   Script.SendRC(3, takeoff_throttle, True)

   print("Processing altitude control")

   found_alt = False

   if (cs.alt < alt_min):
        found_alt = wait_altitude(alt_min, alt_min + 0.5)

#Now we have our altitude we want to hold it

if found_alt:
    Script.ChangeMode("AltHold")
    Script.SendRC(5, 1000, True)

    print("Set the throttle between 40-60% to hold altitude")
    Script.SendRC(3, 1450, True)

    print("Takeoff complete")
else:
    print("Takeoff failed to reach adequate height")
    print("Initiate error control procedure")

    failed_status = True

return failed_status

def hover(hover_throttle = 1500):
    print("Entering Hover mode")
    print("Setting hover throttle")
    Script.SendRC(3, hover_throttle, True)
    print("Done")
    print("Hovering for 3 seconds")
    Script.Sleep(3000)
    print("Hover Complete")


def land():
'''land the quad'''
    print("STARTING LANDING")
    print("Changing Mode to LAND")
    Script.ChangeMode("LAND") # land mode
    Script.SendRC(5, 1300, True)
    print("Done")
    Script.WaitFor('LAND', 5000)
    print("Entered Landing Mode")
    ret = wait_altitude(-5, 1)
    print("LANDING: ok= %s" % ret)
    print("Landing complete")
    return ret

def disarm_motors():
'''disarm motors'''
    print("Disarming motors")
    print("Change mode to stabilize")
    Script.ChangeMode("STABILIZE") # stabilize mode
    print("Done")
    Script.WaitFor('STABILIZE', 5000)
    Script.SendRC(3, 1000, True)
    Script.SendRC(4, 1000, True)
    Script.WaitFor('DISARMING MOTORS',15000)
    Script.SendRC(4, 1500, True)
    #mav.motors_disarmed_wait()
    print("MOTORS DISARMED OK")
    return True

'''Main'''
 setup_rc()
 while cs.lat == 0:
    print 'Waiting for GPS'
    Script.Sleep(1000)
 print 'Got GPS'
 arm_motors()
 takeoff()
 land()
 disarm_motors()
 setup_rc()

Tags: totruereturndefscriptcsminalt