DroneKit(四)——无人机协同

2023-05-16

# -*-coding:utf8 -*-
from dronekit import connect, VehicleMode, LocationGlobalRelative, APIException
import time
#import exceptions
import math
import argparse
import serial                   #串口
import threading

opponent_state = 0
this_vehicle_state = 0
def re_message(): #接收消息
    global opponent_state
    while 1:
        data=s.readline() #一次接收1024字节1            s.recv    s为和数传进行连接
        postion=data.decode("utf8",'ignore')
        postion.strip() #方法用于移除字符串头尾指定的字符(默认为空格或换行符)
        b=postion.split('  ') #分隔
        #打印信息
        opponent_state=int(b[0])#1号机的this_vehicle_state
        opponent_gps=b[1]
        opponent_velocity=b[2]
        print ("Zhao vehicle state: %s" % opponent_state)
        print ("Zhao vehicle GPS: %s" % opponent_gps)
        print ("Zhao vehicle velocity: %s" % opponent_velocity)

def send_message():
    global this_vehicle_state
    while 1:
        time.sleep(1)
        print ("B battery: %s" % vehicle.battery)
        print("B vehicle state: %s" % this_vehicle_state)
        global_frame=vehicle.location.global_frame
        velocity=vehicle.velocity
        print("B vehicle state: %s" % vehicle.velocity)
        data=str(this_vehicle_state) + "  " + str(global_frame) + "  " + str(velocity) + "\n"
        s.write(data.encode()) #写进s中

def arm_and_takeoff(targetHeight):  #飞机初始化起飞
    #1、飞机自检
    while vehicle.is_armable!=True:  
        print("Waiting for vehicle to become aramable")
        time.sleep(1)
    print("Vehicle is now armable")
    #2、确定飞机模式
    vehicle.mode = VehicleMode("GUIDED")
 
    while vehicle.mode!="GUIDED":
        print("Waiting for drone to enter GUIDED flight mode")
        time.sleep(1)
    print("Vehicle now in GUIDED MODE. Have fun!!")
    #3、飞机解锁
    vehicle.armed = True
#    while vehicle.armed==False:
#        print("Waiting for drone to become armed")
    time.sleep(3)
#    print("Look out! Virtual props are spinning!")
    #4、飞行
    vehicle.simple_takeoff(targetHeight) ##meters
    #5、打印信息
    while True:
        print("Current Altitude:", vehicle.location.global_relative_frame.alt)
        if vehicle.location.global_relative_frame.alt>=.95*targetHeight:
            break
        time.sleep(1)
    print("Target altitude reached!!")
    return None
 
 
def get_distance_meters(targetLocation, currentLocation):#获取定点距离
    dLat = targetLocation.lat - currentLocation.lat
    dLon = targetLocation.lon - currentLocation.lon
 
    return math.sqrt((dLon*dLon)+(dLat*dLat))*1.113195e5
 
def goto(targetLocation):
    #到定点
    distanceToTargetLocation = get_distance_meters(targetLocation, vehicle.location.global_relative_frame)
 
    vehicle.simple_goto(targetLocation)
    
    while vehicle.mode.name == "GUIDED":
        currentDistance = get_distance_meters(targetLocation,vehicle.location.global_relative_frame)
        if currentDistance < distanceToTargetLocation*.01:
            print("Reached target waypoint")
            time.sleep(2)
            break
        time.sleep(1)
    return None

if __name__=='__main__':
    #1、连接
    vehicle = connect('/dev/ttyACM0', wait_ready=True, baud=57600)          #jetson通过串口与飞控连接
    print("connect ......     .....            ....     ")
    print("connect ......     .....            ....     ")
    print("connect ......     .....            ....     ")
    s=serial.Serial('/dev/ttyTHS0',115200,8,'N',1)                          #jetson通过串口和数传连接
    print("connect the ttl ...... .......")
    print("connect the ttl ...... .......")
    print("connect the ttl ...... ......")
    #2、发送接收线程
    re = threading.Thread(target=re_message)
    re.start()
    se = threading.Thread(target=send_message)
    se.start()
    #3、设置点位
    wp = LocationGlobalRelative(32.6874439, 119.5353102, 10)  #创建目标位置 纬度 经度 海拔

    while 1:
        if opponent_state == 1 : #第一台飞机发指令
            arm_and_takeoff(10) #起飞
            goto(wp)   #到定点
            #2号到达指定点后this_vehicle_state=1   给1号发送到opponent_state,1号机降落
            this_vehicle_state = 1
            vehicle.mode = VehicleMode("LAND")#降落
            print("LAND mode send to ardupilot........")
            while vehicle.mode != 'LAND':
                print("Waiting for drone to enter LAND mode")
                time.sleep(1)
            print("Vehicle in LAND mode")
            break
#while 1:
#    if opponent_high > 8 :
#        arm_and_takeoff(10)
#    if vehicle.location.global_relative_frame.alt > 8 :
#        if opponent_high < 4 :
#            vehicle.mode = VehicleMode("LAND")
#            while vehicle.mode != 'LAND':
#                print("Waiting for drone to enter LAND mode")
#                time.sleep(1)
##            print("Vehicle in LAND mode")

from dronekit import connect, VehicleMode, LocationGlobalRelative, APIException
import time
import socket
import math
import argparse
import serial
import threading

opponent_state = 0
this_vehicle_state = 0
def re_message():
    global opponent_state
    while True:
        data=s.readline()
        postion=data.decode("utf8",'ignore')
        postion.strip()
        b=postion.split('  ')
        opponent_state=int(b[0])#2号机的this_vehicle_state
        opponent_gps=b[1]
        opponent_velocity=b[2]
        print ("B vehicle state: %s" % opponent_state)
        print ("B vehicle GPS: %s" % opponent_gps)
        print ("B vehicle velocity: %s" % opponent_velocity)

def send_message():
    global this_vehicle_state
    while True:
        time.sleep(1)
        print("Zhao Local Location: %s" % vehicle.location.local_frame)
        print("Zhao Attitude: %s" % vehicle.attitude)
        print("Zhao Battery: %s" % vehicle.battery)
        print("Zhao vehicle state: %s" % this_vehicle_state)
        global_frame=vehicle.location.global_frame
        velocity=vehicle.velocity
        data=str(this_vehicle_state) + "  " + str(global_frame) + "  " + str(velocity) + "\n"
        s.write(data.encode())

def arm_and_takeoff(targetHeight):
    while vehicle.is_armable!=True:
        print("Waiting for vehicle to become aramable")
        time.sleep(1)
    print("Vehicle is now armable")
 
    vehicle.mode = VehicleMode("GUIDED")
 
    while vehicle.mode!="GUIDED":
        print("Waiting for drone to enter GUIDED flight mode")
        time.sleep(1)
    print("Vehicle now in GUIDED MODE. Have fun!!")
 
    vehicle.armed = True
#    while vehicle.armed==False:
#        print("Waiting for drone to become armed")
    time.sleep(3)
#    print("Look out! Virtual props are spinning!")
 
    vehicle.simple_takeoff(targetHeight) ##meters
 
    while True:
        print("Current Altitude:", vehicle.location.global_relative_frame.alt)
        if vehicle.location.global_relative_frame.alt>=.95*targetHeight:
            break
        time.sleep(1)
    print("Target altitude reached!!")
    return None
 
 
def get_distance_meters(targetLocation, currentLocation):
    dLat = targetLocation.lat - currentLocation.lat
    dLon = targetLocation.lon - currentLocation.lon
 
    return math.sqrt((dLon*dLon)+(dLat*dLat))*1.113195e5
 
def goto(targetLocation):
    distanceToTargetLocation = get_distance_meters(targetLocation, vehicle.location.global_relative_frame)
 
    vehicle.simple_goto(targetLocation)
 
    while vehicle.mode.name == "GUIDED":
        currentDistance = get_distance_meters(targetLocation,vehicle.location.global_relative_frame)
        if currentDistance < distanceToTargetLocation*.01:
            print("Reached target waypoint")
            time.sleep(2)
            break
        time.sleep(1)
    return None

if __name__=='__main__':
    #1、连接飞控
    vehicle = connect('/dev/ttyACM0', wait_ready=True, baud=57600)
    print("connect ardupilot.....            ....     ")
    print("connect ardupilot.....            ....     ")
    print("connect ardupilot.....            ....     ")
    #2、连接数传
    s = serial.Serial('/dev/ttyTHS0',115200,8,'N',1)
    print("connect the ttl.....            ....     ")
    print("connect the ttl.....            ....     ")
    print("connect the ttl.....            ....     ")
    re = threading.Thread(target=re_message)
    re.start()
    se = threading.Thread(target=send_message)
    se.start()

    wp = LocationGlobalRelative(32.6874453, 119.5349604, 12)
    arm_and_takeoff(12)
    goto(wp)
    #到达指定点后this_vehicle_state=1   给2号发送到opponent_state,2号接收到后起飞
    this_vehicle_state = 1
    while 1:
        #2号机到达后传过来的指令
        if opponent_state == 1 :
            vehicle.mode = VehicleMode("LAND")
            while vehicle.mode != 'LAND':
                print("Waiting for drone to enter LAND mode")
            time.sleep(1)
            print("Vehicle in LAND mode")

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DroneKit(四)——无人机协同 的相关文章

  • PADS版本历史

    从加载文件的速度 xff0c 生成的文件大小 xff0c 画图的速度 xff0c 渲染的速度等多方面来说 xff0c 个人认为 PDAS 算是非常不错的一款软件 xff0c 值得学习和使用 大概把其光辉历史罗列一下 xff0c 作为一个回忆
  • 简述Z-Stack的基本工作原理与流程(OSAL操作系统)

    首先上图 xff0c 跟着图中的函数顺序来感受Z Stack的工作流程 xff1a Z Stack协议栈总的来说做了两件事 xff0c 系统的初始化和启动OSAL操作系统 系统初始化 xff1a 从main函数看 xff0c 首先是调用了o
  • 使用MaixPy IDE开发K210

    使用MaixPy IDE快速开发K210 距离我第一次使用MaixPy将近40天了 xff0c 大概花了26天 xff0c 完成了我的毕业设计并且进行了优化 后面我会花时间去和大家分享我的毕设 xff0c 也希望能得到各位码友的意见和建议
  • K210实现人脸识别(附代码解读)

    基于K210的人脸识别门禁 xff08 一 xff09 进入官网 xff08 首次登陆需要注册 xff09 获取人脸识别源码 https wiki sipeed com soft maixpy zh course ai image face
  • K210人脸识别+人脸信息存储

    在我的上一篇博客中已经介绍了如何使用K210实现基本的人脸识别功能 https blog csdn net HuangChen666 article details 113995079 spm 61 1001 2014 3001 5501
  • 旅行商问题--蚁群优化算法求解(matlab实现)

    今天给大家分享一下matlab实现蚁群优化算法 xff0c 解决旅行商问题 在上一篇博客中对蚁群优化算法做了较为详细的介绍 xff0c 有需要的小伙伴可以看一下 https blog csdn net HuangChen666 articl
  • 粒子群优化算法及MATLAB实现

    上一篇博客是关于蚁群优化算法的 xff0c 有兴趣的可以看下 https blog csdn net HuangChen666 article details 115913181 1 粒子群优化算法概述 2 粒子群优化算法求解 2 1 连续
  • A星(A*、A Star)路径规划算法详解(附MATLAB代码)

    首先看看运行效果 xff0c 分别有三种模式 xff0c 代码运行前需要通过鼠标点击设置起点和终点 第一种模式直接输出最短路径 第二种模式输出最短路径的生成过程 第三种模式输出最短路径的生成过程和详细探索的过程 代码获取 gitee链接 x
  • Ubuntu20.04+MAVROS+PX4+Gazebo保姆级安装教程

    Ubuntu20 04 43 MAVROS 43 PX4 43 Gazebo 安装PX4步骤安装MAVROS安装QGCPX4仿真 安装PX4步骤 从github上clone源码 span class token function git s
  • PX4+Offboard模式+代码控制无人机起飞(Gazebo)

    参考PX4自动驾驶用户指南 https docs px4 io main zh ros mavros offboard cpp html 我的另一篇博客写了 键盘控制PX4无人机飞行 PX4无人机 键盘控制飞行代码 可以先借鉴本篇博客 xf
  • 基于ESP32的小型四轴无人机

    粗糙版试飞成功 xff01 陀螺仪部分直接飞线飞了一个模块 xff0c 懒得焊了 不是很水平 xff0c 稳定性不是很好 因为滤波算法中加入的元素太少了 xff0c 目前也就MPU6050的输出数据加入了计算 xff0c 所以很多自稳定性飞
  • PX4无人机 - 键盘控制飞行代码

    PX4无人机 键盘控制飞行代码 仿真效果 实机效果 由于图片限制5M以内 xff0c 只能上传一小段了 xff0c 整段视频请点击链接 Pixhawk 6c 无人机 键盘控制无人机 Offboard模式 核心 xff1a 发布 mavros
  • 【FreeRTOS学习 - 消息队列学习】

    跟着韦东山老师FreeRTOS教学资料的学习记录 FreeRTOS全部项目代码链接 xff08 更新中 xff09 https gitee com chenshao777 free rtos study 本文章一共分为一下几个部分 1 创建
  • 【Linux多线程编程-自学记录】08.Linux多线程互斥量

    Linux多线程编程学习代码 xff08 代码已上传gitee xff0c 还请各位兄弟点个Star哦 xff01 xff09 https gitee com chenshao777 linux thread git 笔记 xff1a 1
  • 【Linux多线程编程-自学记录】09.Linux多线程之读写锁

    Linux多线程编程学习代码 xff08 代码已上传gitee xff0c 还请各位兄弟点个Star哦 xff01 xff09 https gitee com chenshao777 linux thread git 笔记 xff1a 1
  • 【Linux多线程编程-自学记录】10.条件变量

    Linux多线程编程学习代码 xff08 代码已上传gitee xff0c 还请各位兄弟点个Star哦 xff01 xff09 https gitee com chenshao777 linux thread git 笔记 xff1a 1
  • 树莓派4B安装Ros 2 Foxy踩坑记录

    1 通过树莓派官方提供的写卡工具raspberry pi imager选择Ubuntu 20 04 5 xff08 64 bit xff09 xff0c 因为我打算用一个8G的存储卡安装ros 2 xff0c Ubuntu 22 04的比较
  • 港科大vins-fusion初探

    SLAM新手 xff0c 欢迎讨论 关于vins fusion的博客 xff1a 1 初探 xff1a https blog csdn net huanghaihui 123 article details 86518880 2 vio主体
  • vins-fusion代码解读[一] vio主体

    SLAM新手 xff0c 欢迎讨论 港科大vins fusion代码解读 一 vins fusion与vins mono代码结构有很大相似性 这次先看看vins estimator节点内的内容 1 程序入口 xff1a 1 vins est
  • vins-fusion代码解读[二] 惯性视觉里程结果与GPS松耦合

    感谢 slam萌新 xff0c 本篇博客部分参考 xff1a https blog csdn net weixin 41843971 article details 86748719 欢迎讨论 惯性视觉里程结果与GPS松耦合 xff1a g

随机推荐

  • vins-fusion代码解读[四] 图像回环检测loop_fusion主体

    SLAM新手 xff0c 欢迎讨论 这篇主要讲loop fusion包的程序结构 xff0c loop fusion主要作用 xff1a 利用词袋模型进行图像的回环检测 在vinsmono中 xff0c 该程序包处于pose graph包内
  • 基于乐鑫开源ESP32四轴无人机项目分享

    上次说重新焊接一块的 xff0c 周末搞定了 xff0c 基本组装的也完成了 xff0c 上个图 试飞还是可以的 xff0c 因为没有光流和定高模块 xff0c 所以稳定性不是很好 xff0c 不过乐鑫预留了扩展模块的接口 xff0c 大家
  • vins-回环检测单独剥离运行

    由于前端如果单纯依靠视觉 43 imu作为里程计 效果经常不稳定 因此最近做项目的过程中 xff0c 将前端转化为以里程计 xff08 码盘编码器 xff09 来进行 xff0c 相对比较鲁棒 由于这个局部传感器有累计误差 xff0c 因此
  • apt 的 update 和 upgrade 命令的区别是什么?

    如果想让你的 Ubuntu 或者 Debian 系统保持更新 xff0c 要用 sudo apt update 和 sudo apt upgrade 命令组合 一些以前的教程也会提到 sudo apt get update 和 sudo a
  • Ubuntu 下 ROS 使用 serial 包进行无线串口通信

    1 查看本机当前USB 串口设备 查看当前已连接的 USB 设备 xff1a lsusb 查看电脑连接的USB 转串口的信息 xff1a dmesg grep ttyUSB 查看电脑连接的串口的信息 xff1a dmesg grep tty
  • 画PCB时,一些非常好的布线技巧

    布线是PCB设计过程中技巧最细 限定最高的 xff0c 即使布了十几年布线的工程师也往往觉得自己不会布线 xff0c 因为看到了形形色色的问题 xff0c 知道了这根线布了出去就会导致什么恶果 xff0c 所以 xff0c 就变的不知道怎么
  • 不能错过的4本Linux好书

    2010年大学毕业 xff0c 在Linux下编程已三年有余了 最近看论坛上不少朋友谈论看过的编程 xff08 Linux xff09 书籍 xff0c 我感触良多 回头想想 xff0c 当初那个一无所知 xff0c 而且老是爱问白痴问题的
  • 微策略2017年秋招线下笔试+技术面+在线测评+主管面总结

    1 前言 微策略可能在国内的知名度比较小 xff0c 它是一家总部在美国 xff0c 在杭州设立研发中心 xff0c 主要做智能商用软件的外企 更多的信息 xff0c 请自行搜索 我是17年10月份面试微策略 xff0c 然后拿到的开发 x
  • github,dockerhub下载文件

    1 打开github xff0c dockerhub代理页面 xff0c 见菜单点击可进入dockerhub代理页面 github下载代理 2 把需要下载的文件的url复制到输入框 3 常用的脚本命令 git clone git clone
  • C++ 开源程序库

    1 系统和网络编程库 xff1a ACE 除了ACE之外 xff0c 还有很多系统和网络编程方面的程序库 比如在线程库方面 xff0c 还有ZThread boost thread xff0c 如果放大到C C 43 43 领域 xff0c
  • 360笔试题2013

    编程题 传教士人数M xff0c 野人C xff0c M C xff0c 开始都在岸左边 xff0c 船只能载两人 xff0c 传教士和野人都会划船 xff0c 当然必须有人划船 两岸边保证野人人数不能大于传教士人数 把所有人都送过河 xf
  • 搜狗笔试题

    搜狗 xff1a 1 xff0c 有n n个正方形格子 xff0c 每个格子里有正数或者0 xff0c 从最左上角往最右下角走 xff0c 只能向下和向右走 一共走两次 xff0c 把所有经过的格子的数加起来 xff0c 求最大值 且两次如
  • [INS-20802] Oracle Net Configuration Assistant failed

    INS 20802 Oracle Net Configuration Assistant failed 在安装Oracle 11g R2时出现了该错误提示 以前安装的时候没有碰到过类似 的错误 原来是64bit和32bit系统兼容性的问题
  • 自定义注解,打造自己的框架-下篇

    2019 12 04 22 53 52 文章目录 结构声明注解声明注解处理器处理注解逻辑给使用者提供调用方法使用 该系列介绍自定义注解 xff0c 完成如下功能 64 BindView 代替 findViewById 64 ClickRes
  • 嵌入式STM32深入之RTOS编程

    RTOS编程 一 前言二 RTOS的概念 xff08 一 xff09 用人来类比单片机程序和RTOS1 1 我无法一心多用1 2 我可以一心多用 xff08 二 xff09 架构的概念 xff08 三 xff09 常见的嵌入式操作系统 xf
  • 坐标系变换

    slam 基础之机器人学中的坐标转换学习总结 rot z 90 ppipp1109的博客 CSDN博客 https www youtube com watch v 61 4Y1 y9DI Hw amp t 61 538s 1 平移和旋转 3
  • 如何安装inf文件

    方法1 运行RunDll32 advpack dll LaunchINFSection YOUINF inf DefaultInstall 方法2 修复右键安装 操作步骤如下 xff1a 打开我的电脑 xff0c 工具 菜单中的 文件夹选项
  • 无人机采集图像的相关知识

    1 xff0e 飞行任务规划 一般使用飞行任务规划软件进行飞行任务的设计 xff0c 软件可以自动计算相机覆盖和图像重叠情况 比如ArduPilot http ardupilot com 和UgCS http www ugcs com 是两
  • 无人机编程donekit及通讯(三)——仿真

    1 启动SITL 启动STL cd courseRoot apm ardupilot sim vehicle py v ArduCopter console map 飞机起飞降落 mode GUIDED arm throttle takeo
  • DroneKit(四)——无人机协同

    coding utf8 from dronekit import connect VehicleMode LocationGlobalRelative APIException import time import exceptions i