Logging into Aeronet: running a standalone quadrocopter in a virtual environment


To fly on a quadcopter now, if desired, perhaps, everyone. But in order to solve the problem of autonomous control, so that you do not need to move the remote control stick and constantly monitor the drone - in this formulation, the solution may require a lot of resources: buy, assemble, solder, adjust, take off, drop, and after a fall - return to the beginning of the cycle.


Teaching the Aeronet judges / teachers on our project, we faced the need for a simplified “entry into the topic” of programming unmanned vehicles for teachers of robotics / computer science, who already possess a set of basic knowledge.


There is an easy way to learn the basics of drone flight programming - a virtual simulation environment, a step-by-step example of the use of which we will look at in our article.


To upgrade the basic drone programming skills, you do not need to buy anything - it is enough to use the jMAVSim project drone simulator of the PX4 project. The PX4 is a powerful set of open source software designed for use on various unmanned vehicles, both flying and driving on the ground. Source code of the project lies on Gitkhab .


Initially, Linux Ubuntu LTS is recommended as a development environment by the PX4 authors. There is also Mac support. This year, a simulation and development environment for Windows appeared in Cygwin, which can make life easier for Russian educational institutions that use Windows in computer science classes.


Next, we look at the process of installing, building and running the simulator under Linux and under Windows.


Installing and running jMAVSim on Linux Ubuntu


PX4 developers recommend Linux Ubuntu 16.04 LTS as a standard system. Linux allows you to build the PX4 package for all supported systems (NuttX-based hardware platforms, Qualcomm Snapdragon Flight, Linux, simulation environments, ROS).


First of all, add the user to the dialout group:


sudo usermod -a -G dialout $USER

Let's log in to the changes to take effect.


The development of the development toolchain for Pixhawk / NuttX, including jmavsim, is done automatically using the ubuntu_sim_nuttx.sh script . You need to download the script to the user directory and run it with the command


source ubuntu_sim_nuttx.sh

All questions asked by the script to answer positively.


At the end of the script to restart the computer.


It remains for us to download the source code of the controller that controls the flight and assemble it.


Clone the software repository of the flight controller PX4 with github:


git clone https://github.com/PX4/Firmware.git

In the Firmware folder, now we have the complete source code, which is executed in the flight controller (and in the simulator). In the future, it can be useful both for the purpose of studying and making changes to it. Go to the copied folder of the firmware repository:


cd src/Firmware

We compile and run the simulator:


make px4_sitl jmavsim

The process of the first compilation takes some time. After successful completion, the PX4 console appears:


PX4 console


You can send a drone into flight by entering the command in the console:


pxh> commander takeoff

Vitrual drone in flight


Landing a drone - command commander land, stop simulation - CTRL + C or command shutdown.


Installing and running jMAVSim on Windows


The Cygwin PX4 tool kit appeared in 2018. This is the most productive way to compile / develop a PX4 for Windows. To install - download and run the installation file from Gitkhab or Amazon .


By default, the toolchain is installed in the C: \ PX4 folder.


At the last step of the installer, you can tick "clone the PX4 repository, build and run simulation with jMAVSim" (clone the PX4 repository, compile and run the jMAVSim simulator).


The development environment in Cygwin is launched using the run-console.bat file in the installation directory (by default, C: \ PX4).


If you forgot to tick the jMAVSim launch checkbox during the installation process, you can clone the repository in Cygwin and start the simulator using the commands:


git clone --recursive -j8 https://github.com/PX4/Firmware.git
cd Firmware
make px4_sitl jmavsim

After compiling, the PX4 console and the simulator window will appear on the screen:


Windows jMAVSim simulator window


The sky and trees are not displayed on my Windows yet, instead of them - a black background, as reported by the developers of the simulator .


The console commands for controlling the drone are the same: take off - commander takeoff, landing - commander land, stop simulation - CTRL + C or a command shutdown.


Flights using the QGroundControl ground station program


The QGroundControl program allows you to fully customize your drones on PX4 or ArduPilot platforms, as well as plan and carry out autonomous flights outdoors using GPS.


The program code is fully open, and it works on Windows, OS X, Linux, iOS and Android platforms. The installation file for the desired platform can be downloaded in the Download section of the program’s website .


For Windows, download and run this file .
After installation and launch, if jMAVSim is already running on our computer, the program will connect to it automatically.


You can launch a drone in flight using the Fly-Takeoff button, and land it. You can also perform a virtual flight over GPS points:


QGroundControl


Autonomous flight programming with mavros


Virtual drone jMAVSim can be controlled using the mavlink protocol, which is described in several articles (for example, 1 , 2 ). To implement mavlink communication, we will use the mavros package of the ROS system (robot operating system) .


PX4 developers recommend using ROS Kinetic.


The mavros package provides MAVLink communication between the computer running ROS (for example, a Linux virtual machine, or Raspberry PI) and a flight controller (real or in a simulator environment).


The mavros package is installed along with other packages during the full installation of Kinetic ROS .


The mavros package is launched with connection to the simulator using the roslaunch command, indicating the ip address and port of the computer on which the simulator is running:


roslaunch mavros px4.launch fcu_url:="udp://@192.168.0.22:14557"

If the simulator is not running on the same host on which jMAVSim is running, before connecting mavros, you must enable mavlink messaging on the network using the command param set MAV_BROADCAST 1in the jMAVSim console. When executing the command, the ip-address of the host used for the mavlink protocol is displayed. The port can be found using the command mavlink statusin the simulator console:


Mavlink status


The success of the connection to the flight controller should be checked with the command:


rostopic echo /mavros/state

If the connection is successful - so-called messages will appear in the console. mavlink heartbeat, about once per second:


mavlink heartbeat


If the messages do not appear / are not updated / the field Connected = False - the connection with the flight controller is not established, and you should find out why.


At the time of this writing, under Windows, after the param set MAV_BROADCAST 1 command in the jMAVSim console, messages were started in a loop:


WARN [mavlink] getting required buffer size failed. 

In order for the simulator to work properly under Windows, you should add the line 1029 of the src / modules / mavlink / mavlink_main.cpp file:


#if defined(__APPLE__) && defined(__MACH__) || defined(__CYGWIN__)

And recompile / restart the simulator command make px4_sitl jmavsim.


This problem has been reported to the developers , possibly, it will be fixed in subsequent releases.


After a successful connection, you can launch the drone in an autonomous flight using the following commands of the ROS system console:


  • We publish 5 times per second target point for the flight of the drone in OFFBOARD mode. In order for the drone not to “fall out” of the OFFBOARD autonomous flight mode, it is necessary to publish the target point all the time, several times a second:

    
    rostopic pub -r 5 /mavros/setpoint_position/local geometry_msgs/PoseStamped "header: seq: 0 stamp: secs: 0 nsecs: 0 frame_id: '' pose: position: x: 0.0 y: 0.0 z: 5.0 orientation: x: 0.0 y: 0.0 z: 0.0 w: 0.0"


  • We transfer the drone to OFFBOARD mode (in a new separate terminal session):

    rosservice call /mavros/set_mode "base_mode: 0 custom_mode: 'OFFBOARD'"

  • We send the command to transfer the drone to flight mode (i.e., “charge”):

    rosservice call /mavros/cmd/arming "value: true"

After the last command, the virtual drone should fly up and freeze at a height of 5 meters:


Flying jMAVSim


You can also launch a drone using a simple Python code:


import rospy
from geometry_msgs.msg import PoseStamped
from mavros_msgs.srv import CommandBool, SetMode
rospy.init_node("offbrd",anonymous=True)
rate=rospy.Rate(5)
setpoint_pub=rospy.Publisher("/mavros/setpoint_position/local",PoseStamped,queue_size=10)
arming_s=rospy.ServiceProxy("/mavros/cmd/arming",CommandBool)
set_mode=rospy.ServiceProxy("/mavros/set_mode",SetMode)
setpt=PoseStamped()
setpt.pose.position.z=5for i in range (0,10):
    setpoint_pub.publish(setpt)
    rate.sleep()
set_mode(0,"OFFBOARD")
arming_s(True)
while (rospy.is_shutdown()==False):
    setpoint_pub.publish(setpt)
    rate.sleep()

The code above uses the same ROS services as the command line example.


The code needs to be copied to a text file, for example, fly_jmavsim.py, and run it from the command line using the command python fly_jmavsim.py.


In the process of debugging this example, I encountered a feature of the jMAVSim simulator - it requires a productive processor for normal operation. On a Linux virtual machine, he managed to calculate only 10 FPS, and fell immediately after takeoff. On a laptop while I was writing an article - he also periodically lost control / fell. It helped the laptop power from the network - because when powered by a battery, the power saving mode is turned on, which underestimates processor performance, which directly affects the operation of the simulator.


Based on the above examples, those who wish can develop autonomous flight programs themselves (by square, circle, arbitrary trajectory, etc.). Performing such exercises can be useful to prepare for programming autonomous missions on a real quadcopter.


We wish you all successful flights!


References:


Robot Operating System (ROS)
Autopilot PX4


Also popular now: