Tuesday, February 17, 2015

ArduCopter in Software-in-the-loop (SITL)

While working on some MAVLink communication application related to ArduCopter, I was in need of communicating with the ArduCopter flight controller through it's USB cable. However, this approach is sometimes hard since it require us to have a dedicated flight controller for these experiments. Initially I was using the ArduCopter flight controller which is mounted on our ArduCopter but it is not practical to use it all the time in my work. So, I switched into run ArduCopter firmware on top of Linux by compiling it as a native application and run with the process called software-in-the-loop (SITL). In this article, I'm writing down the steps I followed to get ArduCoper running as SITL. I did all these things on a computer running Ubuntu 12.04 LTS.

(1) First of all, let's get the ArduPilot source code. It's hosted in GitHub and therefore we can get the code using the following command.

git clone git://github.com/diydrones/ardupilot.git

Then, we also should download the source code of a flight simulator called JSBSIM.

git clone git://github.com/tridge/jsbsim.git

(2) Now, let's install some packages.

sudo apt-get install libtool automake autoconf libexpat1-dev

sudo apt-get install python-matplotlib python-serial python-wxgtk2.8 python-lxml
 
sudo apt-get install python-scipy python-opencv ccache gawk git python-pip python-pexpect
 
sudo pip install pymavlink MAVProxy

(3) Set some paths by running the following commands in your terminal and then reload your .bashrc file. When, you run these commands, make sure that the paths are correct according the location of your downloaded ArduPilot and JSBSim source codes.

export PATH=$PATH:$HOME/ardupilot/Tools/autotest

export PATH=$PATH:$HOME/jsbsim/src

export PATH=/usr/lib/ccache:$PATH
 
. ~/.bashrc


(4) Now move into the downloaded JSBSim source code directory and run the following command to build it.

cd jsbsim

./autogen.sh
 
make

(5) It's time to start ArduCopter on the SITL mode. Run the following command from the terminal. It will start ArduCopter and will popup some windows with different information. One of the most important windows of them is the 'Map' window which shows the location of the quadcopter in a map.

sim_vehicle.sh -v ArduCopter --console --map --aircraft test

In case you see an error in the 'Console' window saying 'APM: PreArm: RC not calibrated', you have to enter the following command in the Ardupilot prompt.

param set ARMING_CHECK 0

Now, let's arm the quadcopter using following command.

arm throttle

Let's load some waypoints to instruct the quadcopter to fly through them. Insert the following command which provides the path to a waypoint file inside ArduCopter source code. Make sure the path is correct.

wp load Tools/autotest/ArduPlane-Missions/CMAC-toff-loop.txt

Quadcopter will not immediately start flying through the waypoints. To make it go, we have to switch it to the 'auto' mode and give some RC input using the following commands.

auto

rc 3 1500


Now, you should notice that the quadcopter is flying through the provided waypoing in the 'Map' window. To stop this flying loop, we have to switch back to the stabilize mode and then give a land command.

stabilize
LAND


That's it! :) However, if you feel like it's better to have simulated flights on your own starting location, you have to do, something little more. Just open the file 'ardupilot/Tools/autotests/locations.txt' and a line like the following. The name given is something you like to call that place. First and second parameters are the latitude and longitude of the location. Third parameter is the altitude above sea level while the fourth parameter is the heading  direction which can be a value between 0 and 359.

UCSC=6.902052468326728,79.86113727092743,10.84,0

Now, when you start the simulation you can give the place name as a parameter to start from there as follows.

sim_vehicle.sh -L UCSC -v ArduCopter --console --map --aircraft test

Following figure shows the architecture of ArduCopter when running in the SITL  mode.

Source: http://dev.ardupilot.com


No comments:

Post a Comment