Sunday, August 28, 2016

Arduino Motor Shield: Soldering Stackable Headers

Got a motor shield for my robot car.

The shield comes with standard header unsoldered yet.  Optionally can put stackable headers on them.

Aligning headers maybe challenging.  If they are misaligned, the pins will not be insertable into other boards' headers.

Here shares a little trick - I have a probe shield handy, so use it as a platform to align the pins.

Probe shield top - basically nothing, make it perfect for a soldering base

probe shield bottom

Plug new headers (10pins x 1, 8pins x 2, 6pins x 1) onto the probe shield

Then the motor shield on top
Now pin-by-pin soldering... don't heat up too long time or the probe shield components get loosen up.

Ta-da!  This is how it looks when done

Test connectivity:  Stack all three together - probe shield on motor shield then on arduino 


Simple blinking LED.  Passed.

Labels: , ,

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: , , , ,

My Pi 3 Install Log (7): Camera module with python or from command line

In order to use OpenCV, I need a camera.

Although a normal USB camera works, I want to try the official one for these reasons:
  • dedicated camera port
  • light weight
  • look cool putting the camera inside the enclosure




Following this page:
Getting started with picamera | Raspberry Pi Learning Resources

Test camera, command line taking pictures:
$ raspistill -o image.jpg

# to see the pictures, i noticed I don't have a pic viewer yet.
# Install few more missing (non-camera-related) packages.

$ sudo apt-get install vim  # my favourite text editor
$ sudo apt-get install gpicview    # remember my jessie is a lite, no lxdm applications were installed
$ sudo apt-get install exfat-utils exfat-fuse  # to support exfat, a n file system works for both linux and osx


sudo apt-get install shasum    # for checksum


* * *

Before trying the code on this page:
http://picamera.readthedocs.io/en/release-0.6/quickstart.html

Of course, also the python-bind camera package
$ sudo apt-get install python3-picamera

Now edit this file and save as 'camera.py'
import time
import picamera

camera = picamera.PiCamera()
try:
    camera.start_preview()
    time.sleep(10)
    camera.stop_preview()
finally:
    camera.close()
and try

$ python3 camera.py

On the monitor shows a clear 8MB pixel video with less than 1/4 sec lagging.  :)

Add this line after the sleep line to take a photo:
    camera.capture()


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: , , ,

Sunday, August 21, 2016

My Pi 3 Install Log (4): Audio config on Raspbian

Notes reading book pi for secret agents
{
# pi audio setup


cat /proc/asound/cards
ls -l /proc/asound
alsamixer


amixer cset numid=3 1: # set audio out to 3.5mm analog jack
amixer cset numid=3 2: # set audio out to HDMI cable


# test speakers
speaker-test -c2 -t wav


alsamixer -c1 # card 1


# test microphone
sudo apt-get install sox libsox-fmt-mp3
sox -t alsa plughw:1 -d # start a monitoring loop


# writing to a WAV file
sox -t alsa plughw:1 myrec.wav # record
sox myrec.wav -d # playback
sox myrec.wav -t alsa plughw:1 # playback from usb audio device output


# mp3 and ogg
sudo apt-get install lame vorbis-tools
lame myrec.wav # wav -> mp3
oggenc myrec.wav # wav -> ogg
sox -t alsa plughw:1 -t wav - | lame - myrec.mp3 # record as mp3
sox -t alsa plughw:1 -t wav - | oggenc - -o myrec.ogg # record as ogg
sox myrec.mp3 -d # playback mp3
# shortcut
alias record='sox -t alsa plughw:1 -t wav - | lame -'
record newrec.mp3


# terminal multiplex
sudo apt-get install tmux


# what is
sudo nano /boot/cmdline.txt
}


From github a japanese shared, pi's audio recording
{
apt-get install alsa-utils # what for
lsusb # list usb devices
arecord -l # list of CAPTURE hardware devices
export AUDIODRIVER=alsa
AUDIODEV=plughw:1,0 rec test.mp3
AUDIODEV=plughw:0,0 play test.mp3
AUDIODEV=plughw:1,0 play test.mp3

}

On my pi then do,

$ sudo apt-get install sox libsox-fmt-mp3 alsa-utils lame vorbis-tools
# alsa already exists on raspbian

$ sudo apt-get install tmux flac

my preferred format flac now played by sox, too.  But board 3.5mm bad quality - quite noticeable electrical noise.

Using alsamixer my soundblaster usb soundcard was recognized but tried sox a flac no sound. This is what I want to make it works.


Labels: ,