2015年8月14日 星期五

Raspberry Pi: Pi 2 with Pi Mapper ---- 3/3

since: 2015/08/14
update: 2016/08/08
reference:
1. PiMapper
2. Raspberry Pi: Pi 2 with Pi Mapper ---- 1/3
3. Raspberry Pi: Pi 2 with Pi Mapper ---- 2/3

C. 安裝 Pi Mapper
       1. 下載軟體:
            $ cd ~/of_v0.8.4_linuxarmv7l_release/addons/
            $ git clone https://github.com/kr15h/ofxPiMapper.git

       2. 安裝相依軟體:
           $ cd ~/of_v0.8.4_linuxarmv7l_release/addons/
           $ git clone https://github.com/jvcleave/ofxOMXPlayer.git && git clone https://github.com/bakercp/ofxIO.git (約 10 分鐘)

       3. (選擇性) 讓鍵盤與滑鼠運作正常:
           $ sudo apt-get update && sudo apt-get dist-upgrade
           $ sudo rpi-update // Raspberry Pi firmware update (約 10 分)
           // A reboot is needed to activate the new firmware
           $ sudo reboot // 重新開機

       4. 編譯 Pi Mapper:
           $ cd ~/of_v0.8.4_linuxarmv7l_release/addons/ofxPiMapper/example
           $ make (約 15 分)
           $ make run // 執行 (或: ./bin/example)

       5. 模式與快捷鍵:

           模式:(按下鍵盤的數字鍵)
            1. Presentation mode (展示模式, 預設)
            2. Texture mapping mode (紋理對應模式)
            3. Projection mapping mode (投影對應模式)
            4. Source selection mode (來源選擇模式)

                說明: 一般的步驟為:
                          -> 先切換到模式3來選擇投影的形狀.
                          -> 隨後, 便可以在模式4選擇來源並於模式2編輯紋理.


          快捷鍵:
          鍵名                              功能
                                              Show info
             t                                  Add triangle surface
             q                                 Add quad surface
             f                                  Toggle fullscreen
                                             Save composition
             BACKSPACE        Delete surface         
                                             Undo command operation

       6. 備註:
           create a symbolic link:
           ln -s [TARGET DIRECTORY OR FILE] ./[SHORTCUT]

          $ cd /home/pi
          $ ln -s /home/pi/of_v0.8.4_linuxarmv7l_release/addons/ofxPiMapper ./PiMapper

           a. 預設外觀設定檔:
         /home/pi/PiMapper/example/bin/data/defaultSurfaces.xml

           b. 使用者外觀設定檔:(save 後, 會自動產生)
         /home/pi/PiMapper/example/bin/data/surfaces.xml
         
           c. 圖片來源檔位置: (支援: .png .jpg .jpeg)
         /home/pi/PiMapper/example/bin/data/sources/images/
         image1.jpg, image2.jpg, image3.jpg ....
         圖片格式 => 建議: 600 x 600 JPG(JPEG) 或 PNG 檔


           d. 影片來源檔位置: (支援: .mp4 .h264 .mov .avi .mpeg)
         /home/pi/PiMapper/example/bin/data/sources/videos/
         test.mov ....
         影片格式 => 建議: 1280 x 720 24fps 規格以下的 MP4, H264, MOV, AVI, MPEG 檔

        備註: 圖片與影片定義來源:
                  /home/pi/PiMapper/src/MediaServer/DirectoryWatcher.h


           e. ShowInfo:
                /home/pi/PiMapper/src/ofxPiMapper.cpp
(修改後, 需重新編譯)
               ....
               void ofxPiMapper::draw(){
               ....

                  if (bShowInfo){
                  // Draw instructions
                      .....
                      ss << "Press <f> to toggle fullscreen\n";
                      ss << "Press <i> to hide this message\n";
                      ss << "Press <z> to undo command operation";
                      ....
                  }

              } // draw
              ....


           f. 投影視窗大小:
              /home/pi/PiMapper/example/src/main.cpp
(
修改後, 需重新編譯)
              ....
#ifdef TARGET_RASPBERRY_PI
// Accept arguments in the Pi version
int main(int argc, char* argv[]) {
  cout << ">>>>>> Starting PiMapper on RASPBERRY_PI \n" << endl;

  //bool fullscreen = false;
  bool fullscreen = true; // 全螢幕

....
 
  if (fullscreen) {
   //ofSetupOpenGL(600, 500, OF_FULLSCREEN); // default
    ofSetupOpenGL(600, 500, OF_FULLSCREEN);
  } else {
    //ofSetupOpenGL(800, 450, OF_WINDOW); // default
    ofSetupOpenGL(800, 450, OF_WINDOW);
  }
  ....
}



#else
int main() {
  cout << ">>>>>> Starting PiMapper on Mac/PC \n" << endl;
  //ofSetupOpenGL(800, 600, OF_WINDOW); // default
  ofSetupOpenGL(800, 600, OF_WINDOW);
  ofRunApp(new ofApp());
}
#endif

           g. 讓 HDMI 影片的聲音正常播放:(修改以下二項設定後, 重新開機編譯)
               (1). $ sudo nano /boot/config.txt
             ....
# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes

hdmi_drive=2
             ....

               (2). $ sudo nano /home/pi/PiMapper/src/Sources/VideoSource.cpp
            ....
           //bool VideoSource::enableAudio = false;
           bool VideoSource::enableAudio = true;


           /*
            #ifdef TARGET_RASPBERRY_PI
            // Do things with the OMX player
            ....
            //settings.enableAudio = false;        //default true, save resources by disabling

            settings.enableAudio = true;        //default true, save resources by disabling          
            ....
            */

               (3). $ sudo nano /home/pi/PiMapper/example/src/ofApp.cpp
            ....            //ofx::piMapper::VideoSource::enableAudio = false;
            ofx::piMapper::VideoSource::enableAudio = true;

           $ sudo reboot

       7. 開機後直動登入執行 Pi Mapper:
            a. 開機後自動登入: (需按 Esc 離開程式, 遠端才能登入)
                $ sudo nano /etc/inittab
....
# Note that on most Debian systems tty7 is used by the X Window System,
# so if you want to add more getty's go ahead but skip tty7 if you run X.
#
#1:2345:respawn:/sbin/getty --noclear 38400 tty1
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
4:23:respawn:/sbin/getty 38400 tty4
5:23:respawn:/sbin/getty 38400 tty5
6:23:respawn:/sbin/getty 38400 tty6
....


ctrl + o: write (enter)
ctrl + x: exit

            b. 登入後自動執行 PiMapper 程式: (遠端登入不會再執行一次)
$ sudo nano ~/.bashrc
....
# auto run openFrameworks on start up but not SSH sign in
echo "tty = $(tty)"

if [ $(tty) == "/dev/tty1" ]; then

  echo "We have a match."
  cd ~/PiMapper/example
  ./bin/example

else
  echo "We dont match."
fi

ctrl + o: write (enter)
ctrl + x: exit

       8. 調整幾何圖形拖拉點的顏色與筆觸粗細:
           /home/pi/PiMapper/src/UserInterface/BaseJoint.cpp
....
void BaseJoint::setDefaultColors() {
  fillColor = ofColor(0, 255, 255, 0);
  //strokeColor = ofColor(255, 255, 255);
  strokeColor = ofColor(0, 0, 255); // 藍色
拖拉點
   
  fillColorSelected = ofColor(255, 255, 0, 0);
  strokeColorSelected = ofColor(255, 0, 0);
}

void BaseJoint::setDefaultProperties() {
  enabled = true;
  visible = true;
  position = ofVec2f(20.0f, 20.0f);
  clickDistance = ofVec2f(0.0f, 0.0f);
  bDrag = false;
  selected = false;
  //strokeWidth = 1.5f;
  strokeWidth = 5.5f; // 拖拉點筆觸粗細
}



       9. 調整 Pi 本機登入 Command line 的字型大小:
           a. 進入 console setup:
               $ sudo dpkg-reconfigure console-setup

           b. 選擇 UTF-8 > Enter

           c. 選擇 Guess optimal charater set > Enter

           d. 選擇 TerminusBold > ok > Enter

           e. 選擇 16x32 (framebuffer only) > ok > Enter

           f. 重新開機
               $ sudo shutdown -h now
              (或重啟 sonsole: $ sudo /etc/init.d/console-setup restart


2015年8月13日 星期四

Raspberry Pi: Pi 2 with Pi Mapper ---- 2/3

since: 2015/08/13
update: 2016/06/08
reference:
1. PiMapper
2. Raspberry Pi: Pi 2 with Pi Mapper ---- 1/3
3. Raspberry Pi 2 Setup Guide

B. 安裝 openFrameworks
     1. 確認目前所安裝的 Raspbian "wheezy" 是 "hard float", 而非 "soft-float" 版本.
          -> "hard float" 版本才會有 /lib/arm-linux-gnueabihf 這目錄;
     
"soft-float" 版本才會有 /lib/arm-linux-gnueabi 目錄.

         p.s. Hard float just means the floating point calculations are done by
                on chip hardware rather than being emulated by software.


     2. 設定 Pi:
         a. 說明:
              Raspberry Pi 2 uses the armv7 architecture,
              here will install openFrameworks (0.8.4)

         b. Expand the file system:
             $ sudo raspi-config
             選擇: 1. Expand Filesystem -> Select > Ok

         c. 編譯 openFrameworks 前, 須先確認 CPU192MB 的RAM可使用.
            (先將分配給 GPU 的記憶體縮小, 之後再改回來)

           選擇: 8. Advanced Options > Select


           選擇: A3. Memory split: Change the amount of memory made available
                                                 to the GPU > Select

            輸入: 64 > OK > YES > reboot
      (限制分配給 GPU 的記憶體; 提供較多的記憶體給 CPU 使用)

     結果:
     $ df -h

     3. 更新套件:
         $ sudo apt-get clean
         $ sudo apt-get update
    // 或: $ sudo apt-get update --fix-missing
         $ sudo apt-get upgrade

     4. 下載 openFrameworks 0.8.4 linuxarmv7 distribution
          $ cd ~
          $ curl -O http://www.openframeworks.cc/versions/v0.8.4/of_v0.8.4_linuxarmv7l_release.tar.gz



    5. 解壓縮檔案:
         $ cd ~
         $ tar xvf of_v0.8.4_linuxarmv7l_release.tar.gz

     6. Add the updated Raspberry Pi 2 makefiles to the release
        $ cd ~
        $ curl https://raw.githubusercontent.com/openframeworks/openFrameworks/master/libs/openFrameworksCompiled/project/linuxarmv7l/config.linuxarmv7l.rpi2.mk -o of_v0.8.4_linuxarmv7l_release/libs/openFrameworksCompiled/project/linuxarmv7l/config.linuxarmv7l.rpi2.mk

     7. Install the linux dependencies:
      $ cd ~/of_v0.8.4_linuxarmv7l_release/scripts/linux/debian/
      $ sudo ./install_dependencies.sh    (約 7 分鐘)
     
     8. Set an environmental variable:
         This must be done before using the makefile system each time you enter a new
         terminal or restart the Raspberry Pi 2. It can be automated by adding it to the
         end of your bash init script (e.g. your bash ~/.profile file).


         The following tells the make system to use all 4 cores for compiling and to use
         the Raspberry Pi 2 variant of the armv7 makefiles.

         $ sudo nano ~/.profile
     
         # ctrl + g : get help
         # ctrl + o : write
         # ctrl + x : exit

         ....
        # openframeworks: to use all 4 cores for compiling & armv7 makefiles   
 
        export MAKEFLAGS=-j4 PLATFORM_VARIANT=rpi2
     
        # check the changes
        $ cat ~/.profile
  
     9. logout & login again

    10. 由於已經編譯好 openFrameworks 了, 我們可以將之前限制分配給 GPU
          64 MB 記憶體, 調整為 256 MB(或者更多). 

    11. Compile an example:
          $ cd ~/of_v0.8.4_linuxarmv7l_release/examples/3d/3DPrimitivesExample/   
          $ make
          $ make run (須在 Pi 2 上才看得到結果)

Raspberry Pi: Pi 2 with Pi Mapper ---- 1/3

since: 2015/08/13
update: 2016/02/29
reference:
1. PiMapper

A. 安裝作業系統影像檔
    1.  下載 Raspbian 映像檔: https://www.raspberrypi.org/downloads/  (1.03 G)
         建議用 torrent 下載, 解壓縮成: 2015-05-05-raspbian-wheezy.img (3.28G)

    2. 把 microSD 卡(在此為 8GB 48MB/秒 讀取)放入 Mac 的讀卡機中, 利用
        SD Card Formatter,將其格式化為 FAT32. (可能需要轉接卡)

    3. Apple -> 關於這台 Mac -> 系統報告... -> 讀卡機:
         ->確認其 BSD 名稱: disk1

    備註: 利用 $ diskutil list 來確認其磁碟名稱, 而不是分割區的名稱

    4. 利用磁碟工具程式, 將剛剛新增的分割區 Unmount (不是退出),
         之後才可以將影像檔寫入.

    5. 寫入影像檔: 從終端機執行(約 21 分鐘)
         $ cd /Lanli/RD/software
         $ sudo dd bs=1m if=2015-05-05-raspbian-wheezy.img of=/dev/rdisk1

    6. 將裝好作業系統的 microSD, 放到 Pi 的 microSD卡槽裡, 幫 Pi 接上無線網卡,
         鍵盤, 螢幕等最後接上電源時便會啟動開機.

    7. 登入系統後, 先將預設帳號 pi 更改密碼: (預設密碼為:raspberry)
         $ sudo passwd pi

    8. 無線網卡設定(在此使用 LB-LINK 型號: BL-LW05-AR5)
        Pi 支援的無線網卡: RPi USB Wi-Fi Adapters - eLinux.org

        => 確認系統是否有抓到網卡:
             $ sudo lsusb

        => 啟動 X 視窗系統: $ startx
             , 接著便可在右上角設定無線網路了, 設好後, 請重新開機回到文字模式下.
                  $ sudo reboot
             或 $ sudo shutdown -h now

    9. 啟用遠端 SSH 登入功能:
        a. $ sudo raspi-config
        b. 選擇: 8 Advanced Options > Select

        c. 選擇: A4 SSH > Select > Enable > OK > Finish

   10. SSH 遠端登入
         a. 查詢 Pi 2 的 IP:
             $ ifconfig
wlan0     Link encap:Ethernet  HWaddr ac:a2:13:d5:a0:5d 
              inet addr:192.168.1.33  Bcast:192.168.1.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1160 errors:0 dropped:200 overruns:0 frame:0
              TX packets:169 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:204769 (199.9 KiB)  TX bytes:24217 (23.6 KiB)


         b.  遠端登入:
              $ ssh pi@192.168.1.33

   11. 查詢剩餘空間與記憶體
         $ df -h
         $ free -h

2015年8月1日 星期六

Using the Jetson TK1 as a remote development environment for CUDA

since: 2015/08/01
update: 2015/08/01

reference:
1. How to use the Jetson TK1 as a remote development environment for CUDA
2. Remote application development using NVIDIA® Nsight™ Eclipse Edition
3. NVIDIA CUDA GETTING STARTED GUIDE FOR LINUX v6.5
4. CUDA 6.0 in Ubuntu 14.04.1 LTS - NVIDIA Developer Forums
5. CUDA Toolkit 6.5
6. CUDA Toolkit Documentation v7.0


A. 在 Local (Mac) 上安裝 CUDA toolkit
     1. 安裝 Xcode

     2. 安裝 Xcode Command Line Tools:
         $ xcode-select --install

     3. 到 CUDA Toolkit 6.5 下載 CUDA Toolkit 6.5 for Mac OSX
         版本: cuda_6.5.14_mac_64.pkg  (latest 64-bit CUDA 6.5 package)

     4. 點二下安裝, 其中 CUDA DriverLocal 處可以不需要安裝, 除非要執行 CUDA-Z.

-----------------------------------------------------------------------------------------------

B. 在 Remote (Jetson TK1) 上安裝 CUDA toolkit
     1. Verify You Have a CUDA-Capable GPU
         $ lspci | grep -i nvidia
         00:00.0 PCI bridge: NVIDIA Corporation Device 0e13 (rev a1)

     2. 到 CUDA Toolkit 6.5 下載 CUDA Toolkit 6.5 for Linux ARM
         版本: ONLY to be used for Jetson TK1
         cuda-repo-l4t-r21.2-6-5-prod_6.5-34_armhf.debf (L4T 21.2 DEB)
         (將檔案傳送到 remote 的 /home/ubuntu/RD/software 目錄下)

     3. 安裝:
          $ cd /home/ubuntu/RD/software
          $ sudo dpkg -i cuda-repo-l4t-r21.2-6-5-prod_6.5-34_armhf.deb
          $ sudo apt-get update
          $ sudo apt-get install cuda-toolkit-6-5

     4. Add the user to the video group
          $ sudo usermod -a -G video ubuntu

     5. Environment Setup ...
          To change the environment variables for 32-bit ARM operating systems:
          $ export PATH=/usr/local/cuda-6.5/bin:$PATH
          $ export LD_LIBRARY_PATH=/usr/local/cuda-6.5/lib:$LD_LIBRARY_PATH

     6. check the L4T version with the following command
         $ head -1 /etc/nv_tegra_release
            # R21 (release), REVISION: 4.0, GCID: 5650832, BOARD: ardbeg, EABI: hard, DATE: Thu Jun 25 22:38:59 UTC 2015

     7. install g++-4.6
         $ sudo apt-get install g++-4.6

     8. (Optional) Install Writable Samples
          $ cd /usr/local/cuda-6.5/bin
          $ cuda-install-samples-6.5.sh /home/ubuntu/RD/projects/
          $ cd NVIDIA_CUDA-6.5_Samples
          $ make

          The resulting binaries will be placed under:
          /home/ubuntu/RD/projects/NVIDIA_CUDA-6.5_Samples/bin


     9. After restart the system, Finally start one of the samples! 
         $ cd /home/ubuntu/RD/projects/NVIDIA_CUDA-6.5_Samples/bin
         $ cd armv7l/linux/release/gnueabihf
         $ ./deviceQuery


         $ ./bandwidthTest

-----------------------------------------------------------------------------------------------

C. Configure Git
       To synchronize CUDA projects between local(Mac) and target(Jetson TK1) systems,
       you need to configure git on both the local and the target systems using these
       commands:


       $ git config --global user.email "lanli0210@gmail.com"
       $ git config --global user.name "lanli"

-----------------------------------------------------------------------------------------------

D.Create nsight project on local(Mac)
     1. 開啟 nsight:
         YourMac > Developer/NVIDIA/CUDA-6.5/libnsight/nsight.app

     2. Select a workspace


     3. Create a new project
         File > New > CUDA C/C++ Project

     4. Project type: Empty Project

     5. Basic settings:

     6. Target system > Manage... > Remote Connections

     7. Configure CUDA Toolkit

     8. Target System(Project Path)

     9. remove the Local System

    結果:

     10. 完成:

-----------------------------------------------------------------------------------------------

E. Adding the source code for CUDA project
     1. File > New > Source File

     2. Source File Configure (just use a template)

     3. 完成:

     4. 按下 "編譯"
       p.s. Note that the build command is /usr/local/cuda-6.5/bin/nvcc,
              which is the compiler on the Jetson, not on the Macbook.

-----------------------------------------------------------------------------------------------

F. Debugging the code on CUDA project
     1. Debug Configurations

     2. Debugging ...
         p.s. By default the debugger will break in main() – this can be switched off
                in the debug configuration.