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


Saturday, February 14, 2015

Flying Dreams

Rainy evening sky from the lab window
Even though I was an aircraft enthusiast from my childhood, I've never been into RC model airplane flying stuff. It was completely out of  my reach. After returning to SCoRe lab in the latter half of last year, I was sitting in the lab while some quadcopter stuff were being done around me. For the purposes of different research projects,  some members of the lab and some undergraduate students were working with quadcopters. They have put their hands on two quadcopters namely a Parrot AR.Drone2.0 which is purchased and another domestically assembled ArduCopter. However, I didn't get a chance to look into those stuff until recently Chathura aiya enter into the subject. Recently he was assigned to fix the issues in our ArduCopter based quadcopter and make it ready to provide reliable flights for the research works which should be done using it. Mainly we need it to be able to send a video camera taking footage of the ground for the purpose of processing and identifying some specific stuff on the ground.

Our flight engineer :)
Since Chathura aiya also was not familiar with quadcopter stuff, it took him some time to carefully study the subject and look into the details. Since I was sitting next to him in the lab and we always communicated well regarding almost anything we are doing, I received the opportunity to get different quadcopter related information from him. He disassembled the quadcopter and assembled it little by little while working on many other works he was assigned to. From him, I got to know different parts of a quadcopter and how the ardupilot (on-board flight controller) handle all these things. He went to the rooftop of the UCSC building so many times and tried to lift off the quadcopter little by little. Initially, little flying robot had some issues with stabilizing in the air and therefore right after turning the motors on, it just fell off from its landing gears. Chathura aiya  used some strings and tied the quadcopter from its two arms to two metal bars on the roof top, making the aircraft to hang from the string. It avoided the quadcopter from falling off or flying into some obstacle which can damage the rotors or airframe. This method helped him to experiment with the quadcopter without unnecessary troubles.

Lots of calibrations!
Arducopter guys provide a ground control software which can be used to calibrate various parameters of the aircraft. According to my understanding, these  calibrations help the on-board flight controller to properly interpret the inputs from various sensors and make decisions to smoothly handle rotors for a successful flight. In each time a part of the quadcopter was disassembled and fitted back, Chathura aiya had to do the calibrations all over again since a little change on-board can make some of the previous calibrations invalid. Sometimes this repeated calibration task was a tiresome thing for me to watch and I was surprised that Chathura aiya didn't get fed up with it. He seemed didn't want to rest until he make this thing fly successfully.


Finally that long awaited day arrived. One day he took it to the university ground and made the first flight which was done using an RC remote controller. Due to the lack of experience on flying a quadcopter, I think he had to put lot of effort to keep it stable in the air and fly around the area. I went to the ground with him to record his efforts and it was so enjoyable to see how it lift off from the ground and wander around us with a pride. In this first flight, quadcopter didn't have a GPS receiver on-board and also there were come errors from compass according to the ground control stations details. Even though this flight ended with an accident of hitting a rugby goal post bar, it was a great moment.


Since I was so interested with the ArduPilot flight controller and its protocol MAVLink for communicating with the outside world, one day I connected it with a computer and ran MAVProxy tool to see what comes out of the ArduPilots USB port. It was so cool to see how ArduPilot responds to MAVLink commands I entered through the MAVProxy tool over USB port. The thing which I was not aware is that somehow this experiment changed the calibration settings of the flight controller and made it unable to fly the quadcopter anymore. I was feeling upset because Chathura aiya had to do the calibrations all over again to fix it but he was not worried about it. In this time he did a new thing. He attached the GPS receiver which comes with a compass to the ArduPilot unit and calibrated it too. After lot of efforts, he managed to bring the quadcopter again into the sky and this time with a GPS receiver. Somehow this addition made the quadcopter to be more stable in the sky and move around smoothly.


The most important requirement was to make the quadcopter fly autonomously according to a flight plan given to the on-board flight controller without any intervention from the ground. Last Friday, this beautiful moment arrived when the quadcopter flew around the university ground according to a set of waypoints given to it. In this time, it carried a battery level indicator on-aboard to warn us by ringing a sound when the quadcopter was flying with low battery. And also it was powered by a brand new Li-Po battery. This progress made by Chathura aiya showed me the lesson of how hard work returns with flying colors. The amazing world of autonomous flying vehicles is in front of us. It's time to move forward!

~****~

Tuesday, February 3, 2015

Moon, Sirius and Me

Sirius from my phone app
While sitting in my room in the silence, I suddenly remembered stars. I forgot to look at them recently. I checked my phone and searched for some app which can be useful to me to star gaze and remind me to do it regularly. Luckily I found something great which is google sky map app. I installed it and went outside. Unfortunately, there were only two in the night sky. First one is the Moon which was shining so brightly. The other thing was Sirius. While everybody else failed the battle with the cloudy atmosphere of the Earth in this night, only Sirius could show up her veil of  light  to me. Everything happens for a good reason. There were nobody else in the sky except the Moon and Sirius, I think because I was supposed to look at Sirius so closely than ever. It was so bright as it is the brightest star that appear in the night sky of the Earth. It's a system of two stars named as Sirius A and Sirius B. Among the those, Sirius A is the  brightest one which gives the white light to the earth. The other one is a white dwarf which has already spent the most important part of its lifetime.

Sirius has been drawing the attention of humans from ancient civilizations at different times. Mostly, from different readings I did about her, Sirius has been considered as a source of life, a teacher and a guide in the right path. Being the brightest star in the night sky, she could easily draw the attention of humans who looked at the heavens with their naked eyes for thousands of years and become a part of their civilization and culture. I stayed outside my home for a while looking at the beauty that glow around 8.6 light years away from me. After 200 million years, she is still beautiful.

Here's a nice song which comes to my mind at this moment. Lyrics are from here
Good night fellow humans! :-)

"Starlight"

Far away
This ship is taking me far away
Far away from the memories
Of the people who care if I live or die

Starlight
I will be chasing the starlight
Until the end of my life
I don't know if it's worth it anymore

Hold you in my arms
I just wanted to hold
You in my arms

My life
You electrify my life
Let's conspire to ignite
All the souls that would die just to feel alive

But I'll never let you go
If you promised not to fade away
Never fade away

Our hopes and expectations
Black holes and revelations
Our hopes and expectations
Black holes and revelations

Hold you in my arms
I just wanted to hold
You in my arms

Far away
This ship is taking me far away
Far away from the memories
Of the people who care if I live or die

And I'll never let you go
If you promise not to fade away
Never fade away

Our hopes and expectations
Black holes and revelations
Our hopes and expectations
Black holes and revelations

Hold you in my arms
I just wanted to hold
You in my arms
I just wanted to hold 



~******~

Sunday, February 1, 2015

Sampling RSSI values in 2.4GHz spectrum

Signals from nearby wi-fi enabled phone
When we need to check RSSI values around 2.4GHz frequency range, we need a spectrum analyzer which can sample signal strength in that frequency. However, since we don't have such an equipment, another easy way to achieve it is using a 2.4GHz transceiver chip which can be programmed to switch between different frequencies in 2.4GHz range and sample the signal strength in each channel. This has become much more easier since we have an app called rssi-scanner which comes with Contiki OS that can run on a Tmote Sky sensor node having a CC2420 transceiver  on board.

This is how I did it. First we have to checkout Contiki source code from the GitHub since currently released Contiki version which is 2.7 at the time of this writing (2015-02-01) does not have this app in the examples directory. It is available in the Sky directory but I didn't try it in that way. So, first we checkout the source code from GitHub using the following command.

git clone https://github.com/contiki-os/contiki.git

Now we should move into the rssi-scanner application which is available in the examples directory.

cd contiki/examples/rssi-scanner

Connect a Tmote Sky sensor mote into the Linux computer (mine is an Ubuntu 12.04 LTS) and run following command to program it with the rssi-scanner app.

make TARGET=sky rssi-scanner-cc2420.upload

Now our sampling device is ready. We need a program which runs on our computer and visualize the sampled RSSI values from Tmote Sky node. For that, we have a java program which is available in the same directory. We compile and run it using the following command. Make sure that your Tmote Sky device is mounted on your Linux computer as a serial device on /dev/ttyUSB0. Sometimes it can be /dev/ttyUSB1 or some other thing in which case, you have to change the command accordingly.

make ViewRSSI.class
make login MOTES=/dev/ttyUSB0 | java ViewRSSI

Now, a window should open where the RSSI values at different frequency channels around 2.4GHz spectrum is displayed in a beautiful graph. Since 802.15.4 and 802.11 both operate in the save 2.4GHz ISM band, I think we can use this RSSI scanner app to check how the channels get affected from both types of transmitters which coexist in a particular place.

Overlapping of 802.11 and 802.15.4 channels. (ref: [1])


References:

[1] Liang, Chieh-Jan Mike, et al. "Surviving wi-fi interference in low power zigbee networks." Proceedings of the 8th ACM Conference on Embedded Networked Sensor Systems (SenSys). ACM, 2010.
Link: http://dl.acm.org/citation.cfm?id=1870014