1.ardupilot平台
对于ardupilot平台 ,dronekit的python代码可以这样编写实现 上/下/前/后/左/右
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 = '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')
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")
通过调用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[]) {
//

本文详细介绍如何使用DroneKit的Python代码在Ardupilot和PX4平台上控制无人机进行上、下、前、后、左、右移动。文章提供了具体代码示例,包括如何通过socket连接在C程序和Python程序间发送指令,实现对无人机的精确控制。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



