1.ardupilot平台

对于ardupilot平台 ,dronekit的python代码可以这样编写实现 上/下/前/后/左/右

from pymavlink import mavutil
from dronekit import connect, VehicleMode, LocationGlobalRelative
import timedef 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 componentmavutil.mavlink.MAV_FRAME_BODY_NED, # frame Needs to be MAV_FRAME_BODY_NED for forward/back left/right control.0b0000111111000111, # type_mask0, 0, 0, # x, y, z positions (not used)velocity_x, velocity_y, velocity_z, # m/s0, 0, 0, # x, y, z acceleration0, 0)for x in range(0,duration):vehicle.send_mavlink(msg)time.sleep(1)connection_string = '127.0.0.1:14540' # 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' %  vehicle.location.global_relative_frame.alt)if vehicle.location.global_relative_frame.alt >= takeoff_alt * 0.95:print('REACHED TARGET ALTITUDE')breaktime.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")

通过调用send_body_ned_velocity方法,可以实现不同方向的指定运动。

2. PX4平台

由于项目需要,我将官网给的dronekit控制px4示例代码做了修改,由c编写的程序通过socket连接dronekit的python程序,发送指令向上/下/东/西/南/北移动,python程序接收后会执行相应操作。

示例代码:

c程序:

//
//  main.cpp
//  connect_python
//
//  Created by Qiucheng LIN on 2020/1/8.
//  Copyright © 2020 Qiucheng LIN. All rights reserved.
//#include <iostream>
#include <unistd.h>//Linux系统下网络通讯的头文件集合
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>int main(int argc, const char * argv[]) {// insert code here...int client_sockfd;//客户端套接字struct sockaddr_in my_addr;   //服务器网络地址结构体socklen_t sin_size;memset(&my_addr,0,sizeof(my_addr)); //数据初始化--清零my_addr.sin_family=AF_INET; //设置为IP通信my_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//服务器IP地址--允许连接到所有本地地址上my_addr.sin_port=htons(7777); //服务器端口号/*创建服务器端套接字--IPv4协议,面向连接通信,TCP协议*/if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0){perror("socket error");return 1;}/*将套接字绑定到服务器的网络地址上*/if(bind(client_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0){perror("bind error");return 1;}/*监听连接请求--监听队列长度为5if(listen(server_sockfd,5)<0){perror("listen error");return 1;}; */sin_size=sizeof(struct sockaddr_in);sockaddr_in addrConServer;addrConServer.sin_family = AF_INET;addrConServer.sin_port = htons(8888);addrConServer.sin_addr.s_addr = inet_addr("127.0.0.1");if(connect(client_sockfd, (sockaddr *)&addrConServer, sin_size)<0){perror("connect error");return 1;}/*等待客户端连接请求到达if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0){perror("accept error");return 1;}*/// char buf[3]={'a','c','k'};char recvbuf[100];//printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));//send(client_sockfd, buf, sizeof(buf), 0);long n=recv(client_sockfd,recvbuf,sizeof(recvbuf),0);recvbuf[n]='\0';std::cout<<recvbuf<<" "<<n<<std::endl;char flag;printf("takeoff?(y/n)");std::cin>>flag;if(flag=='y'||flag=='Y')send(client_sockfd, &flag, sizeof(flag), 0);else return 0;char cmd;while(true){printf("input comander:");std::cin>>cmd;printf("cmd=%c\n",cmd);switch (cmd) {case '1':send(client_sockfd, &cmd, sizeof(cmd), 0);break;case '2':send(client_sockfd, &cmd, sizeof(cmd), 0);break;case '3':send(client_sockfd, &cmd, sizeof(cmd), 0);break;case '4':send(client_sockfd, &cmd, sizeof(cmd), 0);break;case '5':send(client_sockfd, &cmd, sizeof(cmd), 0);break;case '6':send(client_sockfd, &cmd, sizeof(cmd), 0);break;case '7':send(client_sockfd, &cmd, sizeof(cmd), 0);break;default:printf("invalid commander/n");break;}if(cmd==7) break;}close(client_sockfd);return 0;}

python程序

# Import DroneKit-Python
from dronekit import connect, Command, LocationGlobal
from pymavlink import mavutil
import time, sys, argparse, math, socket################################################################################################
# Settings
################################################################################################connection_string       = '127.0.0.1:14540'
MAV_MODE_AUTO   = 4
# https://github.com/PX4/Firmware/blob/master/Tools/mavlink_px4.py# Parse connection argument
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--connect", help="connection string")
args = parser.parse_args()if args.connect:connection_string = args.connect################################################################################################
# Init
################################################################################################# Connect to the Vehicle
print "Connecting"
vehicle = connect(connection_string, wait_ready=True)def PX4setMode(mavMode):vehicle._master.mav.command_long_send(vehicle._master.target_system, vehicle._master.target_component,mavutil.mavlink.MAV_CMD_DO_SET_MODE, 0,mavMode,0, 0, 0, 0, 0, 0)def get_location_offset_meters(original_location, dNorth, dEast, alt):"""Returns a LocationGlobal object containing the latitude/longitude `dNorth` and `dEast` metres from thespecified `original_location`. The returned Location adds the entered `alt` value to the altitude of the `original_location`.The function is useful when you want to move the vehicle around specifying locations relative tothe current vehicle position.The algorithm is relatively accurate over small distances (10m within 1km) except close to the poles.For more information see:http://gis.stackexchange.com/questions/2951/algorithm-for-offsetting-a-latitude-longitude-by-some-amount-of-meters"""earth_radius=6378137.0 #Radius of "spherical" earth#Coordinate offsets in radiansdLat = dNorth/earth_radiusdLon = dEast/(earth_radius*math.cos(math.pi*original_location.lat/180))#New position in decimal degreesnewlat = original_location.lat + (dLat * 180/math.pi)newlon = original_location.lon + (dLon * 180/math.pi)return LocationGlobal(newlat, newlon,original_location.alt+alt)################################################################################################
# Listeners
################################################################################################home_position_set = False#Create a message listener for home position fix
@vehicle.on_message('HOME_POSITION')
def listener(self, name, home_position):global home_position_sethome_position_set = True################################################################################################
# Start mission example
################################################################################################s = socket.socket()  #  create socket
host = '127.0.0.1'
port = 8888  # portnumber
s.bind((host,port))        s.listen(5)
c,addr = s.accept()# wait for a home position lock
while not home_position_set:print "Waiting for home position..."time.sleep(1)c.send("Already!".encode())# 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# Change to AUTO mode
PX4setMode(MAV_MODE_AUTO)
time.sleep(1)# Load commands
cmds = vehicle.commands
cmds.clear()home = vehicle.location.global_relative_frame"""
# move 10 meters north
wp = get_location_offset_meters(wp, 10, 0, 0);
cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)
cmds.add(cmd)# move 10 meters east
wp = get_location_offset_meters(wp, 0, 10, 0);
cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)
cmds.add(cmd)# move 10 meters south
wp = get_location_offset_meters(wp, -10, 0, 0);
cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)
cmds.add(cmd)# move 10 meters west
wp = get_location_offset_meters(wp, 0, -10, 0);
cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_WAYPOINT, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)
cmds.add(cmd)# land
wp = get_location_offset_meters(home, 0, 0, 10);
cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_LAND, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)
cmds.add(cmd)# Upload mission
cmds.upload()
time.sleep(2)
"""
print("wait for takeoff!")
c.recv(1).decode()
print("taking off now!")# Arm vehicle
vehicle.armed = True# takeoff to 10 meters
wp = get_location_offset_meters(home, 0, 0, 10);
cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)
cmds.add(cmd)
cmds.upload()
time.sleep(2)
vehicle.commands.next
vehicle.commands.nextwhile 1:print("alt= %f" %wp.alt)print("lot= %f" %wp.lon)print("lat= %f" %wp.lat)cmd_num=c.recv(1).decode()print("receive commander %c\n" %cmd_num)#cmds.clear()if cmd_num=='1':  #upwp = get_location_offset_meters(wp, 0, 0, 10)cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)elif cmd_num=='2':    #downwp = get_location_offset_meters(wp, 0, 0, -10)cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)elif cmd_num=='3':    #northwp = get_location_offset_meters(wp, 10, 0, 0)cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)elif cmd_num=='4':    #eastwp = get_location_offset_meters(wp, 0, 10, 0)cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)elif cmd_num=='5':    #southwp = get_location_offset_meters(wp, -10, 0, 0)cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)elif cmd_num=='6':    #westwp = get_location_offset_meters(wp, 0, -10, 0)cmd = Command(0,0,0, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 1, 0, 0, 0, 0, wp.lat, wp.lon, wp.alt)elif cmd_num=='7':breakcmds.add(cmd)cmds.upload()time.sleep(2)vehicle.commands.nextvehicle.commands.nextprint("moving now!")time.sleep(1)"""# monitor mission execution
nextwaypoint = vehicle.commands.next
print "next " nextwaypoint
while nextwaypoint < len(vehicle.commands):if vehicle.commands.next > nextwaypoint:display_seq = vehicle.commands.next+1print "Moving to waypoint %s" % display_seqnextwaypoint = vehicle.commands.nexttime.sleep(1)# wait for the vehicle to land
while vehicle.commands.next > 0:time.sleep(1)
"""# Disarm vehicle
vehicle.armed = False
time.sleep(1)# Close vehicle object before exiting script
vehicle.close()
time.sleep(1)

测试方式:

由于目前条件有限,我还只在模拟器上测试了一下,首先要运行 jmavsim仿真程序,然后运行dronekit python程序,最后运行c程序,通过在c程序中输入命令,操作仿真器中的无人机移动。

dronekit 控制飞控 上/下/前/后/左/右 或是 上/下/东/西/南/北相关推荐

  1. Html中控制文字的排版方向(左-右-上-下或者上-下-右-左)

    语法: writing-mode : lr-tb | tb-rl 参数:   lr-tb : 左-右,上-下 tb-rl : 上-下,右-左 测试Html代码如下 <!DOCTYPE html ...

  2. JavaScript 实现图片上传前本地预览

    JavaScript 实现图片上传前本地预览 图片上传前预览,应该算是一个普遍的需求,很多时候可能选中的图片并不是想要的那张,所以需要上传前预览下. JS(浏览器中)是一门特殊的语言,它没有直接读写磁 ...

  3. vue+elementui 同时有上传文件和批量上传文件功能,上传文件或批量上传文件后必须刷新才能再次上传文件

    报错描述: 使用element-ui的上传文件组件写一个批量上传和上传文件,但是发现每次上传文件后或者批量上传文件后,不能再次上传文件或者批量上传文件.只有进入页面第一次点击上传文件或者批量上传文件才 ...

  4. elementui 上传七牛_element ui使用上传组件上传文件到七牛(qiniu-js)

    博主正在重构博客中,刚开始时静态资源都是上传到本地服务器的,但这个项目博主最后打算真正上线运营的.索性就改进了下,把静态资源尽量放到云存储中,方便后续开发.这里把方法和遇到坑给记录下. 1.使用前提注 ...

  5. jquery 分片上传php,php 大文件分片上传

    前端部分 上传 //上传控件 uploadBig('upload','zip,rar,7z,tar',{ id: '', type: 'upload_file', } ,(res)=>{ //t ...

  6. html怎么上传qq空间,qq空间怎么上传照片

    当我们想要把照片上传到qq空间里,应该怎么办呢?下面就让学习啦小编告诉你空间上传照片的方法,希望对大家有所帮助. 空间上传照片的方法 打开QQ主界面,在主界面头像的右则有个小星星,那就是进入空间的快捷 ...

  7. 如何向天翼云服务器上传文件,天翼云盘如何上传文件?

    天翼云盘官方版是一个提供文件同步.备份及分享等服务的网络云存储平台.您可以通过网页.pc客户端及移动客户端随时随地的把照片.音乐.视频.文档等轻松地保存到网络,无须担心文件丢失.通过天翼云,多终端上传 ...

  8. 先外后里,由上而下,由左而右,盒子布局

    分两部分布局,头部tab.列表内容. html <!-- 头部 --> <view class='tab'><view class="tab-new {{sel ...

  9. 前几天在头条上收到一条私信,内容大致是这样的:“我学校比较垃圾,想自学 Java 可以吗?自学 Java 难吗?毕业后能找到一份 6k左右的工作吗?”

    前几天在头条上收到一条私信,内容大致是这样的:"我学校比较垃圾,想自学 Java 可以吗?自学 Java 难吗?毕业后能找到一份 6k左右的工作吗?" 不知道有没有人有类似的问题, ...

最新文章

  1. 基于html5游戏毕业设计数据流图,基于HTML5的网络拓扑图设计
  2. 数据字典简单例子_Python学习100天-Day14(数据分析篇-pandas02)
  3. java c s是什么_Java在C/S
  4. 0830通用问题解决
  5. 0x29——如何把自己iphone app传到iphone上
  6. Win11 不支持移动任务栏位置;苹果将推出更大尺寸的 iPad Pro;iOS 15 更新 Beta2 版本|极客头条...
  7. 【MATLAB】三维曲线(plot3)
  8. 整数实例hdu2041(超级楼梯)
  9. 数值优化-梯度下降法
  10. BM3D算法半解,带python代码
  11. 数字时钟——FPGA
  12. 正圆锥体空间方程_计算机基础算法(一)——时间与空间复杂度
  13. mro python_Python-MRO
  14. 软银将波士顿动力出售给现代;美国流行文化品牌“大嘴猴”被收购;盖茨基金会再投2.5亿美元抗疫 | 美通企业周刊...
  15. 颜色格式转换: FFmpeg源代码简单分析:libswscale的sws_getContext()
  16. 关于Palantir——第三部分:数据集成
  17. 苹果youtube无法连接网络_解决苹果手机Apple ID被禁止下载或无法连接到AppStore等问题...
  18. 基于区块链的知识共享框架-Aletheia
  19. CAD转成PDF之后,字体就变粗了?什么原因呢?
  20. 2015阿里校园招聘笔试题(8.29 测试开发工程师)

热门文章

  1. 计算机网络——数据链路层PPP、CSMA/CD协议
  2. ssm科技馆预约系统毕业设计源码182154
  3. JavaScript恶意代码
  4. 开发者所需要知道的iOS7 SDK新特性
  5. CyberLink PowerDVD Ultra Deluxe 7.3
  6. SAP ABAP 开发管理 代码内存标记 位置使用清单(Mark of memory id)
  7. 年薪50万?智能仓储物流的从业者的薪酬水平| 你拖后腿了吗?(文尾索取报告)...
  8. 产品经理面试必问5大问题(附答题攻略)
  9. 现在中国的学生也可以免费使用微软公司的开发软件了:DreamSpark计划
  10. 【UE4 第一人称射击游戏】35-击中目标时添加准心提示