Blog

How to run Cross-Compiled OpenCV project on AR Drone

If you have followed my last three posts, you should have:

  1. The Cross-Compiler ready.
  2. Cross-Compiled OpenCV libraries.
  3. Cross-Compiled OpenCV project.

USB Flash Drive with AR Drone

If your compiled libraries are too large to be stored in AR Drone’s internal memory, you will need to use a USB drive. Make sure that the USB drive is formatted with fat32 filesystem. I have heard that a lot of people have problems getting AR Drone to recognize the flash drive. I have gotten lucky in this regard, both my Kingston 16 GB and a Chinese No-Name 8 GB USB flash drives have worked flawlessly with AR Drone.

After you have plugged in the USB to the Drones USB port, wait for about 15-30 seconds for the Drone’s firmware to auto-mount the drive.

You can run the following simple command to see a list of file systems in a human readable format.

df -h

I get the following list:

Filesystem                Size      Used Available Use% Mounted on
ubi1:system              26.3M     14.1M     10.9M  56% /
tmp                      57.7M    688.0K     57.0M   1% /tmp
dev                      57.7M         0     57.7M   0% /dev
ubi0:factory              4.8M     76.0K      4.5M   2% /factory
ubi2:update              13.2M     28.0K     12.5M   0% /update
ubi2:data                53.5M      8.1M     42.7M  16% /data
/tmp/udev/dev/sda1        7.6G      1.8G      5.8G  23% /tmp/udev/dev/.mnt/sda1
/tmp/udev/dev/sda1        7.6G      1.8G      5.8G  23% /data/video/usb0

Notice the last line. It means that my 8 GB usb flash drive in mounted to /data/video/usb0. I can navigate to this directory and see the contents of my flash drive.

Configuring the Linker

You have your cross-compiled binary and the shared libraries on the USB. Now all you have to do is to configure the dynamic linker so that it can see the libraries on your flash drive. The dynamic linker loads and links the shared libraries from the path specified by the environment variable $LD_LIBRARY_PATH. So we just need to add the path to lib folder in our flash drive to $LD_LIBRARY_PATH.

export LD_LIBRARY_PATH=/data/video/usb0/lib
Note: You will have to do this on evey telnet session.

Running the Program

Now you can simply run the program like so:

./programName

Terminating the Program

If your program is in an infinite while loop, and you want to stop the program, Ctrl + C will not work here. If you want to exit a program you will need to kill the process. Get the list of running processes with

ps

you will get a list like the following

1176 root      2740 S    /bin/sh 
1229 root     43264 R    ./AR_embedded 
1259 root      2604 S    sleep 10 
1260 root      2740 S    /bin/sh

my program AR_embedded has process ID of 1229, now that I have noted the ID of the process I want to terminate, I can go ahead and kill it

kill 1229

How to Cross-Compile OpenCV project for AR Drone (ARM Processor)

You have already cross compiled OpenCV in your home/$USER/ARM_INSTALL directory. All you need to do now is to specify the include directory of the header files and path to shared libraries to the linker.

You can do this by using the following compiler options:

  • -I option to “add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories”

  • -L option to “specify directory of linker files”

  • -l option to “search the named library when linking”

Creating a Test Project

Lets create a simple OpenCV project that takes an image and saves a grey-scale version of that image to disk.

mkdir ~/testProject
cd ~/testProject
gedit imgToGrey.cpp

paste the following code into the file.

Compiling for Host Machine

If you have OpenCV installed on your host computer, it would be good idea to compile and test the code on host machine. Else you can skip this step.

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g imgToGrey.cpp -o PC_imgToGrey -lopencv_core -lopencv_imgproc -lopencv_highgui
./PC_imgToGrey test.jpg

Compiling for ARM

arm-none-linux-gnueabi-g++ -I$ARMPREFIX/include -I$ARMPREFIX/include/opencv -I$ARMPREFIX/include/opencv2/imgproc -I$ARMPREFIX/include/opencv2 -L$ARMPREFIX/lib -g -o AR_imgToGrey imgToGrey.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui

This will create an executable AR_imgToGrey, which you can transfer to your AR Drone.

Writing these lengthy commands for compilation can get cumbersome, so I have written a bash script that makes the cross-compilation process easier and automatically transfers the cross-compiled executable to AR Drone. See Cross Compilation made easy for more details.