Python DroneKit控制无人机起飞

from dronekit import connect, VehicleMode
import time

from dronekit import VehicleMode



# 串口连接字符串,根据实际串口设备和波特率修改
serial_connection_string = "/dev/ttyUSB0"
baud_rate = 921600

try:
    # 通过串口连接到无人机
    vehicle = connect(serial_connection_string, baud=baud_rate, wait_ready=True)
    
    # 打印无人机的状态
    print("无人机版本: %s" % vehicle.version)
    print("无人机模式: %s" % vehicle.mode.name)
    print("电池信息: %s" % vehicle.battery)

    # 等待无人机GPS锁定
    while not vehicle.gps_0:
        print("等待GPS锁定...")
        time.sleep(1)

    # 解锁无人机
    vehicle.armed = True
    print("无人机已解锁。")

    # 等待无人机解锁
    while not vehicle.armed:
        print("等待无人机解锁...")
        time.sleep(1)

    # 起飞
    # vehicle.mode = VehicleMode("GUIDED")
    # 设置模式为 GUIDED
    vehicle.mode = VehicleMode("OFFBOARD")
    # vehicle.airspeed = 6
    # for i in range(2):
    target_alt = 3
    vehicle.simple_takeoff(target_alt)  # 10表示起飞到10米的高度
    # time.sleep(3)
    # while True:
    #     print("当前高度: %s" % vehicle.location.global_relative_frame.alt)
    #     if vehicle.location.global_relative_frame.alt >= target_alt * 0.95:  # 设置目标高度为2.8米
    #         print("无人机已到达目标高度。")
    #         break
    #     else:
    #         vehicle.simple_takeoff(target_alt)  # 10表示起飞到10米的高度
    #         time.sleep(6)
    #     time.sleep(1)
    # time.sleep(4)
    print("无人机起飞中...")

except Exception as e:
    print("连接无人机失败: %s" % str(e))

finally:
    if vehicle:
        vehicle.close()
        print("无人机连接已关闭。")
















# from dronekit import connect
# vehicle = connect('COM4', wait_ready = True)

# # 飞控软件版本
# print ("Autopilot Firmware version: %s" % vehicle.version)
# # 全球定位信息(经纬度,高度相对于平均海平面)
# print ("Global Location: %s" % vehicle.location.global_frame)
# # 全球定位信息(经纬度,高度相对于home点)
# print ("Global Location (relative altitude): %s" % vehicle.location.global_relative_frame)
# # 相对home点的位置信息(向北、向东、向下);解锁之前返回None
# print ("Local Location: %s" % vehicle.location.local_frame)
# # 无人机朝向(欧拉角:roll,pitch,yaw,单位为rad,范围-π到+π)
# print ("Attitude: %s" % vehicle.attitude)
# # 三维速度(m/s)
# print ("Velocity: %s" % vehicle.velocity)
# # GPS信息
# print ("GPS: %s" % vehicle.gps_0)
# # 地速(m/s)
# print ("Groundspeed: %s" % vehicle.groundspeed)
# # 空速(m/s)
# print ("Airspeed: %s"% vehicle.airspeed)
# # 云台信息(得到的为当前目标的roll, pitch, yaw,而非测量值。单位为度)
# print ("Gimbal status: %s" % vehicle.gimbal)
# # 电池信息
# print ("Battery: %s" % vehicle.battery)
# # EKF(拓展卡曼滤波器)状态
# print ("EKF OK?: %s"% vehicle.ekf_ok)
# # 超声波或激光雷达传感器状态
# print ("Rangefinder: %s" % vehicle.rangefinder)
# # 无人机朝向(度)
# print ("Heading: %s" % vehicle.heading)
# # 是否可以解锁
# print ("Is Armable?: %s" % vehicle.is_armable)
# # 系统状态
# print ("System status: %s" % vehicle.system_status.state)
# # 当前飞行模式
# print ("Mode: %s" % vehicle.mode.name)
# # 解锁状态
# print ("Armed: %s" % vehicle.armed)




# from dronekit import connect
# #串口连接字符串,根据实际串口设备和波特率修改serial connection string="/dev/ttyUsBe"baud rate=57686
# try:
#     #通过串口连接到无人机
#     vehicle = connect('/dev/ttyUSB0', baud=921600, wait_ready=True)
#     #打印无人机的状态
#     print("无人机版本:%s"%vehicle.version)
#     #print("无人机位置:%s"% vehicle.location.global frame)
#     print("无人机模式:%s"%vehicle.mode.name)
    
#     # # 电池信息
#     print ("Battery: %s" % vehicle.battery)
# except Exception as e:
#     print("连接无人机失败:%s"%str(e))
# finally:
#     if vehicle:
#         vehicle.close()


# from dronekit import connect
# # from pymavlink import mavutil
# # import time, sys, argparse, math
 
 
# from dronekit import connect, Command, LocationGlobal
# from pymavlink import mavutil
# import time, sys, argparse, math

# # Connect to the Vehicle
# print("Connecting")
# vehicle = connect('/dev/ttyUSB0', wait_ready=True, baud=921600) # 
 
# # Display basic vehicle state
# print(" Type: %s" % vehicle._vehicle_type)
# print(" Armed: %s" % vehicle.armed)
# print(" System status: %s" % vehicle.system_status.state)
# print(" GPS: %s" % vehicle.gps_0)
# print(" Alt: %s" % vehicle.location.global_relative_frame.alt)

# vehicle.close()



# from dronekit import connect
# import time

# # 串口连接字符串,根据实际串口设备和波特率修改
# serial_connection_string = "/dev/ttyUSB0"
# baud_rate = 921600

# try:
#     # 通过串口连接到无人机
#     vehicle = connect(serial_connection_string, baud=baud_rate, wait_ready=True)
    
#     # 打印无人机的状态
#     print("无人机版本: %s" % vehicle.version)
#     print("无人机模式: %s" % vehicle.mode.name)
#     print("电池信息: %s" % vehicle.battery)

#     # 等待无人机GPS锁定
#     while not vehicle.gps_0:
#         print("等待GPS锁定...")
#         time.sleep(1)

#     # 解锁无人机
#     vehicle.armed = True
#     print("无人机已解锁。")

#     # 等待无人机解锁
#     while not vehicle.armed:
#         print("等待无人机解锁...")
#         time.sleep(1)

#     # 起飞
#     vehicle.mode = dronekit.VehicleMode("GUIDED")
#     vehicle.simple_takeoff(10)  # 10表示起飞到10米的高度
#     print("无人机起飞中...")

# except Exception as e:
#     print("连接无人机失败: %s" % str(e))

# finally:
#     if vehicle:
#         vehicle.close()
#         print("无人机连接已关闭。")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值