The communication and simulation verification between the drone controlled by the dronekit and the MAVProxy

DroneKit Python is a python library for controlling UAVs. DroneKit provides API for controlling UAV. Its code is independent of flight control. It runs separately on the company computer or other equipment, and communicates with the flight control board through MAVLink protocol through serial port or wireless.

1, Install DroneKit(linux)

Test environment: ubuntu 18.04
Reference link: https://www.ncnynl.com/archives/202106/4256.html

  1. Installation dependency
sudo apt-get install python-pip python-dev
  1. Installing the DroneKit
pip install dronekit
pip install dronekit-sitl
  1. Download the sample and get the source code of the dronekit Python sample to your local computer
git clone http://github.com/dronekit/dronekit-python.git

II Install SITL and test Donekit

Here I use the source code to install. Refer to the link: https://www.ncnynl.com/archives/202106/4257.html

  1. Installation dependency
sudo apt-get update
sudo apt-get install git
sudo apt-get install gitk git-gui
  1. Download ardupilot
git clone https://github.com/ArduPilot/ardupilot / / generally, the download speed here is very slow. If you can, go over the wall
cd ardupilot
git submodule update --init --recursive//Update program sub module

... see reference link

  1. Install MAVProxy and pymavlink
pip install --upgrade pymavlink MAVProxy --user
  1. Upgrade the mavproxy component. function
pip install -U mavproxy

3, Connect to get data

For PX4 system simulation, the interfaces provided are:

127.0.0.1:14540

For Ardupilot system simulation, the interface provided is

127.0.0.1:14551

4, Perform flight mission

Based on the simulation environment of MAVProxy, after running, continue to add and output a connected node for the dronekit to connect
The steps are as follows:

  1. Running simulation
cd ~/ardupilot/ArduCopter/
sim_vehicle.py --map --console

2. Add nodes to connect

output add 127.0.0.1:14551
  1. Create a new one in / home / Dou / dronekit Python / examples Py files, such as hello py
vim hello.py

How to create, edit and run python files under Python Ubuntu
https://jingyan.baidu.com/article/11c17a2c4a5d47b546e39d98.html

  1. Create a new py file using the vim command in the save directory. Instruction [vim filename. Py]
  2. Enter edit mode. After entering the py file display, first press the letter key "I" on the keyboard to enter the "Insert" mode.
  3. Edit python functions. After entering the insert mode, you can edit the function.
  4. Save the python file. After editing, press "Esc" to exit, and then enter ": wq" to save the edited content. After returning, you can see the newly generated files in the directory.
  5. Run the python file. The file can be run using the python directive.

The code is as follows: lift 10m, fly forward for 30s, fly right for 30s, and then switch the mode and close the connection. Refer to the official code and verify its feasibility. It has not been modified for the time being. Later, it will be controlled according to its own task.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
"""
© Copyright 2015-2016, 3D Robotics.
simple_goto.py: GUIDED mode "simple goto" example (Copter Only)
Demonstrates how to arm and takeoff in Copter and how to navigate to points using Vehicle.simple_goto.
"""
 
from __future__ import print_function
import time
from dronekit import connect, VehicleMode, LocationGlobalRelative
 
 
# Connect to the SITL simulator using UDP through the local 14551 port
connection_string = '127.0.0.1:14551'
print('Connecting to vehicle on: %s' % connection_string)
# The connect function will return an object of vehicle type, that is, the vehicle here
# It can be regarded as the main body of the UAV. Through the vehicle object, we can directly control the UAV
vehicle = connect(connection_string, wait_ready=True)
 
# Define arm_and_takeoff function to unlock the UAV and take off to the target altitude
# The parameter aTargetAltitude is the target height, in meters
def arm_and_takeoff(aTargetAltitude):
    # Conduct pre takeoff inspection
    print("Basic pre-arm checks")
    # vehicle.is_armable will check whether the flight control is started, whether there is GPS fix and Kalman filter
    # Whether initialization is complete. If the above check passes, True will be returned
    while not vehicle.is_armable:
        print(" Waiting for vehicle to initialise...")
        time.sleep(1)
 
    # Unlock the UAV (the motor will start to rotate)
    print("Arming motors")
    # Switch the flight mode of UAV to "GUIDED" (it is generally recommended to control UAV in GUIDED mode)
    vehicle.mode = VehicleMode("GUIDED")
    # By setting vehicle Armed status variable is True to unlock the UAV
    vehicle.armed = True
 
    # Before the UAV takes off, confirm that the motor has been unlocked
    while not vehicle.armed:
        print(" Waiting for arming...")
        time.sleep(1)
 
    # Send takeoff command
    print("Taking off!")
    # simple_takeoff will send a command to take off the UAV and rise to the target altitude
    vehicle.simple_takeoff(aTargetAltitude)
 
    # Block the program before the UAV rises to the target altitude
    while True:
        print(" Altitude: ", vehicle.location.global_relative_frame.alt)
        # When the altitude rises to 0.95 times of the target altitude, it is considered to have reached the target altitude and exit the cycle
        # vehicle.location.global_relative_frame.alt is the height relative to the home point
        if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
            print("Reached target altitude")
            break
        # Wait for 1s
        time.sleep(1)
 
# Call the arm declared above_ and_ Takeoff function, target height 10m
arm_and_takeoff(10)
 
# Set the default airspeed to 3m/s when moving
print("Set default/target airspeed to 3")
# vehicle. The airspeed variable is readable and writable, and has different meanings when reading and writing.
# When reading, it is the current airspeed of the UAV; When writing, set the default speed of UAV when executing waypoint task
vehicle.airspeed = 3
 
# Send the command to let the UAV go to the first waypoint
print("Going towards first point for 30 seconds ...")
# LocationGlobalRelative is a class that consists of latitude and longitude (WGS84) and height relative to the home point
# This statement will create a location located at 35.361354 s, 149.165218 e, 20m above the home point
point1 = LocationGlobalRelative(-35.361354, 149.165218, 20)
# simple_ The goto function sends the position to the UAV to generate a target waypoint
vehicle.simple_goto(point1)
 
# simple_goto function only sends instructions and does not judge whether it has reached the target waypoint
# It can be interrupted by other subsequent commands. Here, the delay is 30s, that is, let the UAV fly towards point1 for 30s
time.sleep(30)
 
# Send the command to let the UAV go to the second waypoint
print("Going towards second point for 30 seconds (groundspeed set to 10 m/s) ...")
# As before, this statement creates another point 20m higher than home
point2 = LocationGlobalRelative(-35.363244, 149.168801, 20)
# simple_goto sends the target waypoint to the UAV, ground speed = 10, and sets the ground speed during flight to 10m/s
vehicle.simple_goto(point2, groundspeed=10)
 
# As before, the delay is 30s
time.sleep(30)
 
# Send "return" command
print("Returning to Launch")
# To return, just switch the flight mode of the UAV to "RTL(Return to Launch)"
# No one has a chance to automatically return to the top of the home point and then land automatically
vehicle.mode = VehicleMode("RTL")
 
# Clear the vehicle object before exiting
print("Close vehicle object")
vehicle.close()

Keywords: ROS

Added by coltrane on Sat, 22 Jan 2022 09:26:23 +0200