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



Sunday, January 18, 2015

Using ATtiny85 With nRF24L01+ For Wireless Communication

After learning to program ATtiny85 MCUs using Arduino as ISP, next thing I wanted to try was how to use them with nRF24L01+ wireless modules. I found some libraries which facilitate it and after successfully trying it, I decided to write the steps down here.

Setting up software for Arduino IDE:

(1) Download the codes from the GitHub project https://github.com/stanleyseow/arduino-nrf24l01. I forked it into my account for my own future use which can be found in https://github.com/asanka-code/arduino-nrf24l01.

(2) Extract the downloaded ZIP file and copy the two directories "Mirf" and "SPI85" into the libraries directory inside your sketchbook directory inside home directory.

(3) Download the "attiny" directory from the following project into the "hardware" directory of the sketchbook directory. It contains the board definitions for ATtiny85 and other ATtiny MCUs. This is a fork from the high-low-tech guys ATtiny library.

Now our software setup is ready. We are going to setup an ATtiny85 chip with an nRF24L01+ module to behave like a client program which send packets continuously into another device which is an Arduino Uno board with an nRF24L01+ module.

Setting up ATtiny85 with nRF24L01+:

(1)  Follow the steps in my previous article which can be  found in this link and
setup ATtiny85 chip together with Arduino Uno board as the ISP programmer. Plug this Arduino board into the computer.

(2) Open the Arduno IDE (I used version 1.0.5) and open the example program
"attiny85_counter_example" which is available inside  out newly copied "Mirf" directory. Program it into the ATtiny85 chip.

(3) Remove the ATtiny85 chip from the above programming setup and set it up
with another Arduino board and nRF24L01+ module as shown. Pin connectivity between nRF24L01+ module and ATtiny85 chip should go as below.

MISO -> PB0
MOSI -> PB1
SCK -> PB2
CE -> PB4
CSN -> PB3


VCC pin of the nRF24L01 module should be provided with 3.3v while the VCC
pin of the ATtiny85 module should be provided with 5v. Both can be achieved
by connecting them with the appropriate pins in an Arduino board. Both GND
pins also should be grounded properly. 

ATtiny85 with nRF24L01+ powered by an Arduino board
This ATtiny85 MCU is now ready for communication. It can be powered up by an
Arduino board.

Setting up Arduino Uno with nRF24L01+:

(1) Plug a new Arduino Uno board with the computer.

(2) Start Arduino IDE again and open the example program "ping_server" inside "Mirf" directory. Program it into the Arduino Uno board.

(3) Connect an nRF24L01 module with this Arduino board according to the pin
connectivity details given below. 

MISO -> 12
MOSI -> 11
SCK -> 13
CE -> 8
CSN -> 7
GND -> GND
VCC -> 3V3


This Arduino Uno board is also now ready for communication. 

Arduino Uno board connected with nRF24L01+
Running the setups:

Now, supply power to both setup, Arduino server and ATtiny85 client. Open the
serial monitor of the Arduino server side using Arduino IDE and check for any information on a baud rate of 9600. You will see that the server is receiving data from the client as the figure shown below. What happens is our ATtiny85 chip continuously send packets which are received and retransmitted by the Arduino Uno board as a reply.

Output of the serial monitor at the server side


Thursday, January 15, 2015

Programming ATtiny85 MCU using Arduino as ISP

I was in need of learning how to program ATtiny microcontrollers and more specifically ATtiny85 for a project work using Arduino IDE. There are different programmer boards available to program ATtiny85 chips in the market. Among those different methods, one nice way of doing it is using an Arduino board as an ISP (in-system programming) device. After doing some searches on the web, I tried it yesterday. I'm writing down the steps I followed to program an ATtiny85 chip with a simple LED blink program using Arduino as ISP. I tried it  on an Ubuntu 12.04 LTS system and used an Arduino Uno board as the supporting hardware for the ISP programming task.

Turn the Arduino board into an ISP programmer:

(1) I used Arduino IDE 1.0.5 version since 1.0 version which I had initially didn't work as I expected. Arduino IDE 1.0.5 version can be downloaded from the link and start it.

(2)  Open the ArduinoISP program from the Examples and program it into an Arduino Uno board. Now our Arduino Uno board is ready as an ISP programmer.

Programming ATtiny using Arduino board:

(1) Download the master branch of this attiny project as a ZIP file from GitHub which is available in this link. I forked it for my future use which is available in this link.

(2) Create a new directory called hardware inside my sketchbook directory inside home directory.

(3) Unzip the downloaded attiny project ZIP file and copy the attiny directory into the hardware directory which we just created inside sketchbook.

(4) Start Arduino IDE and goto the Tool->Board menu where we should see the ATtiny chips now appear as boards.

(5) Connect Arduino Uno board with the ATtiny85 chip through a breadboard as shown in the following figure. We need a 10uF capacitor in addition to the breadboard and jumper wires to complete this task. Pay close attention to the correct pin connectivity.

(Image source: http://highlowtech.org/?p=1706)

(6) Open the Blink example which is available in the Arduino IDE and change the blinking pin from 13 to 0. Select the ATtiny85 board and Arduino as ISP options from the Tools menu. Now program the Blink application. When we say Pin 0 in Arduino IDE, what we actually mean is the physical pin 5 on ATtiny85 MCU. Refer to the following figure to understand the pin numbering convention between Arduino boards and ATtiny85 MCUs.

Click on image to enlarge. (image source: http://www.pighixxx.com/)

(7) After  programming the Blink application, connect an LED in series with a resister (I used a 1kΩ resister) between the ATtiny85 MCU's physical pin 5 and GND to see it blinking by the program running on ATtiny85 chip.

References:
[1] http://highlowtech.org/?p=1706
[2] http://highlowtech.org/?p=1695
[3] http://www.instructables.com/id/Program-an-ATtiny-with-Arduino/?ALLSTEPS

Friday, January 9, 2015

Reading RFID tags with Arduino

Last night I tried a little thing, following the details provided in this link. I received a simple RFID reader module which I wanted to try by connecting to an Arduino board. According to the details provided in the above link, I succeeded in reading a passive RFID tag details. For my future reference, I'm writing it down in this article. The RFID module I used for this task is RDM6300 which comes as a small PCB together with externally pluggable coil for the antenna. We have to connect this gadget with an Arduino board to be able to access it from our Arduino program. Pin connectivity details of the device is as follows.

Arduino pin RFID module pin
2 LED
6 TX
+5V +5V
GND GND

Pins of the RFID module can be identified from the following diagram.


After connecting the RFID module to the Arduino board, we have to program the Arduino board with a special firmware which can read RFID tag details. In Maniacbug's blog post, there are two programs provided where first one just print the raw data read from RFID tags while the second one prints the correct TAG ID after making some processing with checksum calculations. I forked his codes from GIST for my purposes which can be accessed from following links.


Running the second program resulted in the following output in my serial monitor which shows the tag ID correctly.

Serial monitor output when reading a tag.