Saturday, August 27, 2016

My Pi 3 Install Log (8) - usb tethering to an android phone

On the road, the cellphone data plan is the only way how Pi stays connected.

1. edit this file: /etc/network/interfaces

2. append these lines:
iface usb0 inet dhcp
auto eth0
iface eth0 inet dhcp
iface usb0 inet dhcp

3. save and quit file

4. connect android phone and start
    sudo ifup usb0

5. similarly to stop:
  sudo ifdown usb0

ref:
https://xsatria.workpress.com/2013/07/09/usb-tethering-from-android-to-raspberry-pi


Labels: , , , ,

Friday, August 26, 2016

My Pi 3 Install Log (6): OpenCV (part 2) on Raspberry Jessie (Lite)

(4 hours)

# in part 1 after creating a virtual env for python 2.7, i changed my mind.
# thought like redo in python3 because my physical env is python2 and has numpy installed already.


$ deactivate # leave env
$ rm -rf .virtualenvs/cv
$ mkvirtualenv cv -p python3


Already in, have (cv) before prompt
If not in (cv) yet
$ source .profile
$ workon cv


pip list # show what has been installed


pip install numpy # took quite long time ~10mins!!
# version 1.11.1


$ cd ~/opencv-3.1.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D INSTALL_PYTHON_EXAMPLES=ON \
   -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
   -D BUILD_EXAMPLES=ON ..


# I want to see cmake output so startx and ran from lxterminal.
# it was a mistake to also call ‘make’ there took me 102mins while the instruction said 75mins.


$ make -j4


# but it completed successfully.  :) 2.7GB was generated within the build folder
# install to system
$ make install
$ ldconfig


## step 6


$ cd /usr/local/lib/python3.4/site-packages/
$ ls -l
total 1852
-rw-r--r-- 1 root staff 1896140 Aug 22 21:43 cv2.cpython-34m.so
$ sudo mv cv2.cpython-34m.so cv2.so
$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/

$ ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so

* * *
congrats, installation completed

* * *

## step 7 verify installation


# open a new terminal
$ source ~/.profile
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.1.0'
>>>


## my step 8 (optional)


The two opencv folders at ~ no longer necessary for python code.  
Before moving on, I want to back up the 'build' folder for what have been compiled and generated during 102 mins wait.
# plug in a big enough USB disk, say have a label myusbdisk
$ tar cvf /media/pi/myusbdrive/opencv-3.1.1_build.tar /home/pi/opencv-3.1.0/build/


(edited)
built samples and examples in ~/opencv-3.1.1/build/bin
to try them, first start the gui
open a terminal, and enter
sudo modprobe bcm2835-v4l2 # see ref below

sometimes example stuck won't quit
kill that process
1. ctrl-shift-n for a new terminal
2. ps ax | grep facedetect
3. (note the pid, eg. 12345)
4. kill 12345


* * *
ref:
I think the difference is that he is not using the Raspberry Pi camera but a USB webcam - at 4:40 guvcview is reporting using driver uvcvideo. I've just tried it and guvcvideo fails immediately for me complaining about the contents of /proc/cpuinfo (no CPU frequency, although I don't know why it needs to know that!). 
I haven't listened through all of the tutorial, but I think the bit you need is 
CODE:  sudo modprobe bcm2835-v4l2
to load the RaspiCam V4L2 driver. raspistill and raspivid talk to the MMAL service directly, whereas most applications (including OpenCV) use V4L2.

* * *

# to build cpp examples myself   https://www.learnopencv.com/how-to-compile-opencv-sample-code/
# Gcc using opencv
You can use pkg-config command to get the compiler/linker options. Like this

gcc `pkg-config --cflags opencv` `pkg-config --libs opencv` new.c
Please note the above punctuation is ` not '.

Or you can use CMake. Write a CMakeLists.txt like this

cmake_minimum_required (VERSION 2.6)
project (new1)
find_package (OpenCV REQUIRED)
include_directories (${OpenCV_INCLUDE_DIRS})
add_executable (new1 new.c)
target_link_libraries (new1 ${OpenCV_LIBS})
then you can build like this

mkdir build
cd build
cmake -DOpenCV_DIR=<the-directory-contating-OpenCVConfig.cmake> ..
make


Labels: , ,

My Pi 3 Install Log (5): OpenCV (part 1) on Raspberry Jessie (Lite)


$ apt-get upgrade

########################
## now play with OpenCV ##
########################


Title: opencv w/ python bindings


## step 1 expand file system (no need)


## step 2 dependencies


# dev tools
(no need, already have) $ sudo apt-get install build-essential cmake pkg-config


# image I/O packages
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev


# video I/O packages
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev


# Opencv has a sub-module highgui, for display image and basic GUI, has dependency on gtk2.0 (crap! Download 20.6MB->60.x)
$ sudo apt-get install libgtk2.0-dev


# matrix operations needs a few optimization libraries, which are important on resource constrained devices like Pi. (dl 9MB->45MB)
$ sudo apt-get install libatlas-base-dev gfortran


# python 2.7 and 3 header files for opencv w/ python bindings(dl 44->70MB)
$ sudo apt-get install python2.7-dev python3-dev


## step 3: opencv source 3.1 (first 3.x stable version) 76MB


# main source
$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
$ unzip opencv.zip


# need features such as SIFT and SURF, so opencv_contrib (52.6MB)
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
$ unzip opencv_contrib.zip


# install ‘pip’, a Python package manager
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py


## step 4: python


# virtualenv and virtualenvwrapper
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip


# ‘df -h’ now 3.8G (27%) used


# append to ~/.profile
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh


# now take effect
$ source ~/.profile


## next to do creating your python virtual environment

# 2.7 or 3  ??

# I did:
$ mkvirtualenv cv -p python2

-- to be continue .. --

Labels: , , ,

Saturday, July 30, 2016

My Pi 3 Install Log (3): ROS Kinetic on Raspbian Jessie (Lite)

(2 hours)

# installing ROS


# follow installation ubuntu
  • sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
  • sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 0xB01FA116
  • sudo apt-get update
  • sudo apt-get upgrade     # will download 87.8 MB of archives
after upgrade, now 972MB used
'free -m' shows 690/925 MB memory used <-- probably buffering
sudo shutdown -r now    #  or sudo reboot
'free -m' now using 76/925 MB memory only

now starts differently - ROS has 4 configurations to choose from.  Should I go for the ros-base installation?

* * *
If desktop-full, lite has no GUI, so try LXDE as in install log 2 http://robotera.blogspot.ca/2016/08/install-log-2-lxde-on-raspbian-jessie.html
* * *

ok now try ros desktop, error cannot locate packages.
apt-get update hits ROS packages correctly but received nothing about ROS??
apt-cache search ros-kinetic returns nothing

So slow method install from source! 2.5 hours

# Bootstrap Dependencies
$ sudo apt-get install -y python-rosdep python-rosinstall-generator python-wstool python-rosinstall build-essential cmake
→ used 1636140/15304884 1k-blocks (used 1.6G)

# init dependencies
sudo rosdep init
rosdep update
→ Query rosdistro index and then added distro

### installation now
# catkin workspace - use wstool to fetch the core packages so to build them.
# install ROS-Comm (no GUI), not Desktop
  • $ rosinstall_generator ros_comm --rosdistro kinetic --deps --wet-only --tar > kinetic-ros_comm-wet.rosinstall
    $ wstool init src kinetic-ros_comm-wet.rosinstall
# unavailable Dependencies
mkdir -p ~/ros_catkin_ws/external_src
cd ~/ros_catkin_ws/external_src
wget http://sourceforge.net/projects/assimp/files/assimp-3.1/assimp-3.1.1_no_test_models.zip/download -O assimp-3.1.1_no_test_models.zip
unzip assimp-3.1.1_no_test_models.zip
cd assimp-3.1.1
cmake .
make
sudo make install
# assimp is needed by collada_urdf, a tool to convert Unified Robot Description Format (URDF) documents into COLLAborative Design Activity (COLLADA) documents.
# get remaining dependencies
cd ~/ros_catkin_ws
$ rosdep install -y --from-paths src --ignore-src --rosdistro kinetic -r --os=debian:jessie
# add swap space
sudo nano /etc/dphys-swapfile
And change the line
CONF_SWAPSIZE=100
into
CONF_SWAPSIZE=1024
Next you can either restart your PI or just the swap daemon:
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
The second instruction will take bout 30 seconds to run (it creates the swap file) so don't be alarmed if the prompt doesn't return immediately.
Usefree -m to make sure that you have 1024 of swap memory.

# build catkin workspace
$ sudo ./src/catkin/bin/catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release --install-space /opt/ros/kinetic
# done installation.  Now source its environment
$ echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
$ source ~/.bashrc


* * *
For future reference (not done yet), cut-and-paste the followings from the official page:

Maintaining a Source Checkout


## in the future, periodically update the source by updating first rosinstall file, download the latest sources, and rebuild our workspace.

$ mv -i kinetic-desktop-full-wet.rosinstall kinetic-desktop-full-wet.rosinstall.old
$ rosinstall_generator desktop_full --rosdistro kinetic --deps --wet-only --tar > kinetic-desktop-full-wet.rosinstall
Then, compare the new rosinstall file to the old version to see which packages will be updated:
$ diff -u kinetic-desktop-full-wet.rosinstall kinetic-desktop-full-wet.rosinstall.old
If you're satified with these changes, incorporate the new rosinstall file into the workspace and update your workspace:
$ wstool merge -t src kinetic-desktop-full-wet.rosinstall
$ wstool update -t src
Now that the workspace is up to date with the latest sources, rebuild it:
$ ./src/catkin/bin/catkin_make_isolated --install
Once your workspace has been rebuilt, you should source the setup files again:
$ source ~/ros_catkin_ws/install_isolated/setup.bash

Adding Released Packages

You may add additional packages to the installed ros workspace that have been released into the ros ecosystem. First, a new rosinstall file must be created including the new packages (Note, this can also be done at the initial install). For example, if we have installed ros_comm, but want to add ros_control and joystick_drivers, the command would be:
$ cd ~/ros_catkin_ws
$ rosinstall_generator ros_comm ros_control joystick_drivers --rosdistro kinetic --deps --wet-only --tar > kinetic-custom_ros.rosinstall
You may keep listing as many ROS packages as you'd like separated by spaces.
Next, update the workspace with wstool:
$ wstool merge -t src kinetic-custom_ros.rosinstall
$ wstool update -t src
After updating the workspace, you may want to run rosdep to install any new dependencies that are required:
Raspbian Jessie:
$ rosdep install --from-paths src --ignore-src --rosdistro kinetic -y -r --os=debian:jessie
Finally, now that the workspace is up to date and dependencies are satisfied, rebuild the workspace:
$ sudo ./src/catkin/bin/catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release --install-space /opt/ros/kinetic

Labels: , , , ,