Dienstag, 20. Januar 2015

Cross Compiling OpenCV on OS X for BeagleBone Black Debian


Cross compiling the Hello World cpp application was an easy one (see my last blog). Now I wanted to write a simple program for a USB webcam using the OpenCV library. I had already written and run the code on OS X Yosemite, so I didn't expect any problems - or so I thought. Many frustrating hours later, it seems that OpenCV is compiling... But let's start from the beginning.

I had built and installed OpenCV on the iMac before, but using this build was not going to work on the Debian target. OpenCV had to be cross compiled as well before my little program could be compiled.

OpenCV 2.4.10 was downloaded and copied to ~/bbb/source/opencv-2.4.10. Another directory ~/bbb/build/opencv-2.4.10 was created to hold the build. The CMake GUI was started and the two folders were set as source and build path.

Options for cross-compiling were specified manually:


Iterating between clicking configure, looking for errors and updating build variables led to better and better results. This is what I had to do:

PYTHON_INCLUDE_DIR:PATH=/usr/include/python2.7
PYTHON_LIBRARY_DEBUG:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY_RELEASE:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY:FILEPATH=/usr/lib/python2.7
PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python2.7
WITH_JASPER:BOOL=0
WITH_JPEG:BOOL=0
WITH_PNG:BOOL=0
WITH_TIFF:BOOL=0
(...one variable is still missing, see further down...)



Everything looked good, I started Terminal, changed to the build folder, and run make.

The build started, but failed all too soon with
Linking C static library libz.a
Error running link command: No such file or directory
make[2]: *** [libz.a] Error 2
make[1]: *** [CMakeFiles/zlibstatic.dir/all] Error 2
make: *** [all] Error 2

As I am an absolute beginner with make, it took me hours to find out that make can be also run as
make VERBOSE=1
and give much more information:
Linking C static library libz.a
/Applications/CMake.app/Contents/bin/cmake -P CMakeFiles/zlibstatic.dir/cmake_clean_target.cmake
/Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/zlibstatic.dir/link.txt --verbose=1
CMAKE_AR-NOTFOUND cq libz.a  CMakeFiles/zlibstatic.dir/adler32.o CMakeFiles/zlibstatic.dir/compress.o CMakeFiles/zlibstatic.dir/crc32.o CMakeFiles/zlibstatic.dir/deflate.o CMakeFiles/zlibstatic.dir/gzclose.o CMakeFiles/zlibstatic.dir/gzlib.o CMakeFiles/zlibstatic.dir/gzread.o CMakeFiles/zlibstatic.dir/gzwrite.o CMakeFiles/zlibstatic.dir/inflate.o CMakeFiles/zlibstatic.dir/infback.o CMakeFiles/zlibstatic.dir/inftrees.o CMakeFiles/zlibstatic.dir/inffast.o CMakeFiles/zlibstatic.dir/trees.o CMakeFiles/zlibstatic.dir/uncompr.o CMakeFiles/zlibstatic.dir/zutil.o
Error running link command: No such file or directory
make[2]: *** [libz.a] Error 2
make[1]: *** [CMakeFiles/zlibstatic.dir/all] Error 2
make: *** [all] Error 2

Aha! CMAKE_AR-NOTFOUND was the culprit. But only what means that? AR is an archiver, and obviously CMake can't find it. Easy: add another CMake variable pointing to the archiver in our toolchain:

CMAKE_AR:FILEPATH=/usr/local/willtm/bin/arm-willtm-linux-gnueabi-ar

This actually solved my problem, if I only had known that in order to make the variable work, I had to delete everything except CMakeCache.txt from the build directory and generate again with CMake. Cost me another few hours...

Finally OpenCV has been successfully cross compiled. Now let's go back to Eclipse and see if we can compile code that's using it.

Afterthought: To have all cross compiled libraries at one location, I made a new folder ~/bbb_target/ with the directory structure of the BeagleBone root (i.e. ./usr, ./usr/local etc.). Setting the CMake variable

CMAKE_INSTALL_PREFIX:PATH=/Users/<me>/bbb_target/usr/local/lib

and generating everything again will result in make install copying the libraries to bbb_target, where they can be referenced by Eclipse (so I hope).

To sum it up, here again all manual changes to the CMake variables:

CMAKE_INSTALL_PREFIX:PATH=/Users/<me>/bbb_target/usr/local/lib
CMAKE_AR:FILEPATH=/usr/local/willtm/bin/arm-willtm-linux-gnueabi-ar
PYTHON_INCLUDE_DIR:PATH=/usr/include/python2.7
PYTHON_LIBRARY_DEBUG:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY_RELEASE:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY:FILEPATH=/usr/lib/python2.7
PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python2.7
WITH_JASPER:BOOL=0
WITH_JPEG:BOOL=0
WITH_PNG:BOOL=0
WITH_TIFF:BOOL=0

Freitag, 19. Dezember 2014

Sharing OS X Internet Connection over USB to BeagleBone Black





After you have connected the BeagleBone Black to an iMac via USB cable, and installed the drivers from the BBB on OS X, a new network connection is created which provides tcp/ip to the BBB under the default address 192.168.7.2 over the USB cable.


It sounds natural to extend this connection to the internet. First steps are found again with Derek Molloy, who explains how set a gateway and nameserver in order to have internet access on the BBB, here my version:


#!/bin/sh
echo "Setup script for getting access to the internet via USB - Derek Molloy"
echo "Setting up the default gateway"
/sbin/route add default gw 192.168.7.1

echo "Updating the nameserver entry"
echo "nameserver 213.196.149.11" >> /etc/resolv.conf

echo "Setting the time using the Swiss ntp pool"
ntpdate -b -s -u 0.ch.pool.ntp.org

To extend the BBB network to the internet, OS X provides Internet Sharing. Unfortunately, sharing the internet to the BBB connection does not lead to a working internet connection. The reason is that by default OS X creates the internet sharing to the local network 192.168.2.x, and therefore cannot reach the BBB on 192.168.7.2. There are descriptions how to change this on OS X Lion, but Yosemite seems to work differently, and I haven't found yet a way to change the OS X Internet Sharing default network.


One solution is to change the pf.conf packet filter on the OS X host to provide additional NAT functionality. I didn't like to tamper with the pf settings, so I looked for another solution.


The solution was to change the default IP address the BBB gives itself on the USB interface on startup to match the OS X internet sharing default. This is described nicely by Eric Wong. Of course, the above script must be adapted as well regarding the default gateway.


Now the BBB starts up on 192.168.2.2, and has internet connection from the beginning.



Montag, 15. Dezember 2014

BeagleBone Black Debian Toolchain on OSX



While BeagleBone Gurus like Derek Molloy usually run their IDE in a Linux Virtual Box, this solution proved too slow on my early 2008 iMac. So next thing was to look for a toolchain for Eclipse on OS X to compile towards the Linux Debian on the BeagleBone Black.

First finding was the Carlson-Minot toolchain, which worked for a C hello world application. Going to C++, the need for a hard float toolchain turned up. This is due to the Debian distribution provided with the BeagleBone, which requires hard floats, promising execution speed. So I followed this excellent blog entry by Anders Knelson, which resulted in a working C++ hello world application.

Anders is using a toolchain provided by William Markezana, and my Eclipse project is now set up like this:




An Eclipse run configuration is set up so that the executable is automatically copied to the BeagleBone and executed there. This is done by using Target Management RSE (Remote Systems Environment), see installation and setup at Derek Molloy, and then creating an Eclipse C/C++ Remote Application run configuration:


So everything is prepared to delve into more sophisticated applications.