13 Şubat 2015 Cuma

Building OpenCV for ARM (Cross-Compilation)

The main purpose of this post is to build OpenCV for ARM, write a simple code, cross-compile it and make it work in our target environment (ARM). This is post is composed of two parts: building, coding and testing.

Note that the host where we're working is Ubuntu 12.04 and the cross-compiler used for ARM is arm-none-linux-gnueabi

BUILD:

In order to cross-compile OpenCV, we will use Cmake to generate makefiles. Install it first.
  • $ sudo apt-get install cmake cmake-curses-gui
We need to download OpenCV source code. We've used  opencv-2.4.9
  • $ cd ~
  • $ wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip
  • $ unzip opencv-2.4.9.zip
 Toolchain for cross-compilation should be added to your environment PATH.
  • $ gedit .bashrc
Add the path of your toolchain at the end of file as below and save it. Do not forget to update the line below with yours. After closing .bashrc, "reboot" or "logout and login".
  • export PATH=$PATH:/home/sezerb/Projects/ToolChain/arm-2012.09/bin
 For cross-compilation:
  • $ mkdir ~/build && cd ~/build
  • $ touch toolchain.cmake 
  • $ gedit toolchain.cmake
Edit toolchain.cmake as below:

Note that since we have added our toolchain to environment PATH before, we've commented out the last line.

OpenCV supports "neon" since version 2.3. Since we are using 2.4.9 now, we can enable/disable neon support from CMake gui later on.

Now it's time to cross-compile OpenCV:
  • $ cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../opencv-2.4.9/
  • ccmake .
A screen will be shown full of options. You can set options On/Off by Enter. For the first compilation we've set most of the options starting with "WITH" to Off. Adjust them according to your needs. For neon support do not forget to set USE_O2 on.
Now press 'c' and 'g' to configure and generate makefile.
  • $ make
  • $ make install
Under "/build" folder, there will be an install folder including built opencv libraries, headers etc. In "/install" there are 4 directories: bin, include, lib, share. Now we're done. We can use these libraries in our target environment.

CODING and TESTING:

Create a file named as Main.cpp as below: Create a Makefile as below and please do not forget to update the paths with yours. In terminal, go to the directory where these files located and "$ make". Now, we have a "Main" binary to be executed in our target environment. Before running this binary in our target environment, we need to copy built libraries to our target system. "/lib", "/usr/lib/" and "/usr/local/lib" are directories where you may copy the built opencv libraries in "/install/lib/". We have copied them to "/lib" folder in our target system. If you do not have any of these directories in your target system, you may create a folder such as "/usr/lib" to copy built opencv libraries. Also copy the headers located in "/install/include/" to "/usr/include/" in your target system.
After copying the libraries and header, copy the "Main" executable binary to your target system and run it by typing:
  • ./Main
It's done, OpenCV library is able to work in our target system :).

27 Eylül 2014 Cumartesi

Connect Your Notebook to Raspberrypi via VNC Server

1) Connect your Raspberrypi to your network via ethernet or wireless adapter and power it up!

2) In order to connect to Raspberrypi via ssh, we need IP adress of Raspberrypi.
Let's say IP address of Raspberrypi is 192.168.1.157
In terminal: ssh pi@192.168.1.157 Enter  your password and connect!.
(If we don't know the IP address of it, then use nmap -sP 192.168.1.0/24 command on your notebook. This will report the devices connected to your network. If nmap is not installed: sudo apt-get install nmap)

3) First of all we need to install a vnc server to Raspberry pi:  
In ssh terminal: sudo apt-get update sudo apt-get install tightvncserver

4) After installation, in order to run vncserver:
Enter vncserver :1  in ssh terminal.
At first time you'll be asked to set a password.(8 character max: e.g. raspberr)
Say "no" to the next question.

5) Now vnc server is up! Time to set vnc client in your notebook.

6) Download Vnc Viewer:
Download VNC-5.2.1-Linux-x86-DEB.tar.gz (32-bit for me)
tar -xzvf VNC-5.2.1-Linux-x86-DEB.tar.gz
sudp dpkg -i VNC-Viewer-5.2.1-Linux-x86.deb

7) Now we have vncviewer in notebook. Run it by typing vncviewer on terminal.
Enter 192.168.1.157:1, Connect and also enter your password.

8) It's done. You can use your laptop to control and monitor your Raspberrypi. Note that you cannot monitor the processes using GPU directly such as Minecraft-pi etc.




References
  •  https://learn.adafruit.com/adafruit-raspberry-pi-lesson-7-remote-control-with-vnc/installing-vnc
  •  http://askubuntu.com/questions/224559/how-to-find-all-the-used-ip-addresses-on-a-network

3 Kasım 2013 Pazar

PyQt via QtDesigner

In this post, we will use a Widget created with QtDesigner, transform it to python code and run it with python interpreter in any machine you want. This is really useful for GUI programming in Raspberry pi. Because Raspberry pi comes with a good python support.
My environment:
  • Ubuntu 12.04 LTS
  • Python 2.7.3 (comes with Ubuntu default)

In order to use PyQt with QtDesigner, we have to install required packages:
$ sudo apt-get install python-qt4 qt4-dev-tools python-qt4-dev pyqt4-dev-tools qt4-designer
After the installation open a terminal and write:
$ python
>>>import sys
>>>from PyQt4 import QtGui
If you do not get any errors, that means you sucessfully installed PyQt. With the installation of "pyqt4-dev-tools", pyuic4 is also installed which is able to transform an XML formatted Qt ui file to python code. To check it, write pyu and press tab in the terminal to see the name completely.
I will not explain how to create a custom Widget in Qt designer here, however I'm gonna give .ui file created via QtDesigner here. You can open it via QtDesigner and play on it.
Assume that we have a hello.ui file, open the terminal and write:
$ pyuic4 -x hello.ui -o hello.py
Now we have hello.py to run in python. Note that "-x" parameter is for transforming the xml ui code to fully python code.
Again in the terminal write:
$ python hello.py
Tata! You will see a "Hello World" label in window and change it by writting something to see how it works. Yes, your GUI design in Qt is working as a python code. I love coding in C++, however this will simplify my job while dealing with my Raspberry pi. The hello.ui code is given below:

22 Ekim 2013 Salı

Simple Makefile Including Compilation of Shared Object Library

This post covers two main concepts: a simple Makefile generation and compiling a shared object library within this Makefile.
Directory structure is designed as this:

  • ../MakeFileProj (MakeFile)
  • ../MakeFileProj/inc (Headers: test1.h, test2.h)
  • ../MakeFileProj/src (Source Files: main.c test1.c test2.c)
  • ../MakeFileProj/obj (Object Files: main.o test1.o test2.o)
  • ../MakeFileProj/lib (Library Files: libtest2.so -> test2.c is compiled as shared object library)

The source and header files are given below:

Makefile is as shown below:
As source, test1.c and main.c are compiled and object files are created under /obj directory. However test2.c is compiled as a shared object library and the .so file created under /lib. Finally, the object files and the .so library are linked to create "test" executable.

After compilation succesfully one can try the executable file in console as:
$ ./test
$ ./test: error while loading shared libraries: libtest2.so: cannot open shared object file: No such file or directory
At run-time the executable file cannot find shared object library, so we should add it to LD_LIBRARY_PATH.
$ export LD_LIBRARY_PATH=/home/sezerb/Desktop/MakeFileProj/lib:$LD_LIBRARY_PATH
Do not forget to use your own path, where libtest2.so is located. Then try ./test again:
$ ./test
$ sZr Welcome to MakeFileProject...
$ sZr test1Message: This is not a shared library
$ sZr test2Message: This is a SHARED LIBRARY

This Makefile should be a reference one for our projects...
Take care yourselves!..

25 Eylül 2013 Çarşamba

Creating SVN Repositoy in your Dropbox in 4 steps.


Sometimes while you're coding in your local workspace or writing your thessis it's better to have your source as an SVN repository. You can reach it from everywhere for editing and  doing some modifications.
  1. Install Tortoise SVN or an SVN program to your device and get a Dropbox account.
  2. Create a folder in your Dropbox folder via right clicking mouse button.
  3. Right click on the just created folder and click Tortoise SVN -> Create Repository here...
  4. Go to your source folder, right click on it and  click Tortoise SVN ->Import. Choose the address of the repository folder in your Dropbox folder and it's DONE.
Kind Regards...

3 Haziran 2013 Pazartesi

Embedded Linux Practical Hints


  • Every embedded linux system requires a MMU (Memory management unit) in order to work. MMU is responsible for handling accesses to memory requested by the CPU.
  • Every system is mainly composed of 4 methods: open, read, write and close.
  • Nowadays linux kernel 2.6.32 and 2.6.35 are robost versions for embedded development.
  • We always need Toolchains for embedded linux development. We should create or copy the toolchains under /opt directory.
  • While dealing with arm boards, one can face with eabi, oabi etc. These are related with D and I busses. eabi stands for 32-bit-fast, and oabi stands for 8-bit slow. One can also face with hard-float and soft-float issues which are confusing. soft-float means implemented on fpga as a software, and hard-float means implemented as hardware.
  • arm-linux-gnueabi-gcc can be the cross-compiler of your life.
  • In order to simulate or emulate embedded linux system, you can use qemu.
    • sudo apt-get install qemu qemu-kvm qemu-kvm-extras
    • For instance, in order to cross-compile a mips architecture you can use qemu-system-mips compiler.
    • In order to run a cross-compiled program in arm architecture just use qemu-arm
  • You always get tar.bz2 or tar.gz while download toolchains or kernel etc.
            tar.bz2->                         tar -jxvf  ....... -C /your directory
            tar.gz->                           tar -zxvf ....... -C /your directory
  • Kernel source code has architecture under the /arch directory. There are template configuartion files for these architectures.
  • For cross-compilation of Kernel source: (an example of arm versatile)
    • $ make ARCH=arm versatile_defconfig
    • $ make ARCH=arm menuconfig
    • Enable EABI from Kernel features
    • $ make ARCH=arm CROSS_COMPILE=arm-angstrom-linux-gnueabi-
    • the cross-compiler appends gcc at the end of arm-angstrom-linux-gnueabi- at the compile time.
    • After the compilation, the output files will be located in /arch/arm/boot/
    • To test zImage (kernel image) via qemu:
      • qemu-system-arm -M versatilepb -m 128M -kernel zImage
      • After run the command above, you will get a rootfs problem, since we do not have it yet.
  • In order to create rootfs or let's say userspace, we can use busybox or System.
  • However buildroot is a really useful tool to build user space. Let's use it:
    • cd buildroot
    • make menuconfig
    • Choose Target Architecture (arm) and Target Architecture Variant (arm926t)
    • Choose Target ABI (EABI)
    • Choose Target Filesystem options ->cpio->compression method (gzip)
    • After the configuartion, save and exit.
    • make V=1
    • the command above will start to build your userspace. V can be between 1 and 10. 1 means low compression, 10 means high compression.
    • After the operation completes, the output is rootfs.cpio.gz.
    • Now we can test our system with qemu:
      • qemu-system-arm -M versatilepb -m 128M -kernel zImage -initrd rootfs.cpio.gz
  • A linux system is mainly composed of:
    • primary bootloader ( cpu )
      • intermediate stage MLO (Mloader which is specific to board producers.)
    • secondary bootloader (FAT file system)
    • Kernel
    • User Space
      • /User Space/boot/Kernel. User Space shall include kernel image too. Because after boot up and the rootfs is mounted, the kernel operations should be done in user space not in fat partition. So, after boot up,  a swap operation is done and the kernel in the user space which is the same as in the FAT file system becomes active.
  • To boot a simple linux system up from SD Card:
    • First of all, divide your SD card into two partitions: boot (FAT) and Angstrom(ext3). You should use a script. Angstrom is just an example, change it according to your system.
    • To FAT partition, copy MLO (MLoader), u-boot.bin and uImage under the boot partition sequentially. The copy order is very important. Because in FAT file system, the files are written with starting adress 0.
    • To ext3 partition, copy uImage under /boot/ with the name uImage.
    • Our SD card is ready now to boot up.
  • To access hardware in a linux system, just check /sys/class/. 
    • cd gpio
    • ls -l
      • export unexport
    • Let's say our gpio is 139.
    • echo 139 > export
    • gpio139 folder is formed.
    • cd gpio139
    • cat direction
      • in
    • echo out > direction
    • echo 1 > value
    • We have set gpio to output and the value to 5.
  • In order to start our target linux system with a program:
    • Write your script that is running your program.
    • copy your script under /etc/init.d/
    • Make sybolic links under /etc/rc*.d (rc0.d, rc1.d, rc2.d...rc5.d) In rc5.d root screen appears.
    • rc0.d and rc1.d are mostly responsible for hardware initalizations, you can start your applications after these two operations.K stands for Kill, S stands for Start.
    • To automatize above operations, write your script, boot up your target system, copy script under /etc/init.d and write command: update -rc.d blabla.sh defaults

1 Mayıs 2013 Çarşamba

Dealing with Complex Languages as Hebrew, Arabic etc. as a Programmer

Programmers sometimes have to deal with some complex languages as Hebrew, Arabic etc. For instance, let's say we have to design a user-interface including Hebrew strings. For such situations for you to overcome and for me not to forget and to be able look behind after a while, I suggest you to read the references given below. The first one is explaining character sets and the historical improvement of it. The next one is about an engine which can be ported to your project to handle these language stuff.



http://www.joelonsoftware.com/articles/Unicode.html

http://userguide.icu-project.org/layoutengine

Kind Regards...