Saturday, August 21, 2010

Learning English via movie — Shutter Island (隔離島)





如果說是衝著哪位演員來選擇看哪一部電影的話,李奧納多一定是本人的TOP3! 即便是在「鐵達尼號」前,甚至是羅密歐與茱麗葉前的赤子本色記得小時候是跟堂哥在HBO看這部電影的,直到近幾年的血鑽石,讓我完全愛上了「李先生」XD

看完隔離島,真的只有可以形容,因為這部片子實在太懸疑了本人就不做影評了,這篇文章的目的純粹是要紀錄我在看隔離島」時學到的實用、日常、生活」英文 XD

  •  We will casting off again as soon as you two are ashore
   ◎ 船長的意思是,當Teddy與Chuck上岸後會馬上調頭走掉

  •  warden
   ◎ 典獄長


  •  Your boys seem a little on edge Mr. McPherson
   ◎ Teddy對典獄長說,你的手下看起來好像有點緊張


  •  All right, you gentelman will be accorded all the help we can offer.
   ◎ 典獄長對Teddy與Chuck說獄方會盡可能來協助他們


  •  Admittance to ward C is forbidden without the written consent and physical presence of both myself and Dr. Cawly
   ◎ 要進入ward C必須要有書面的批准,並且要有我本人及醫生Cawly的陪同


  •  You act like insanity is catching
   ◎ 你看起來像是也被精神病感染了


  •  Executive order 319 of the Federal Code of Penitentiaries states that when stays at Penitentiaries the office head of institutions has the final authority.
   ◎ 監獄的聯邦法令第319條規定,在監獄裡面典獄長擁有最高權利


  •  Correctional officers at a mental institution that's a weird sight, if you don't mind me saying
   ◎ 精神病院裡有獄警看起來很奇怪


  •  To sustain the illusion that her children never died she created an elaborate fictional structure
   ◎ 為了維持她的孩子還活著的錯覺,她創造了複雜的虛擬場景


  •  Your poison, gentlemen?
   ◎ 要喝什麼酒? (這裡是特殊用法而不是問別人要什麼毒藥XD)


  •  You don't indulge in alcohol?
   ◎ 你不喜歡喝酒?


  •  psychiatry
   ◎ 精神病學


  •  You're bunking in the orderlies quarter
   ◎ 你們會睡在勤務人員的宿舍


  •  The greatest obstacle to her recovery was her refusal to face what she had done
   ◎ 她能否康復的最大障礙就是她拒絕去面對她之前所做的事


  •  I'm gonna go back and blow the lid off this place.
   ◎ 我會回去揭露這地方的真相


  •  civil war
   ◎ a war between groups of people in the same country; 內戰


  •  ma'am
   ◎ 女士;夫人 (尊稱)


  •  no hard feelings
   ◎ 別再生氣了啦 :)


  •  I was a esteemed psychiatrist, from a respected family.
   ◎ 我來自一個受敬仰的家庭, 且曾經是個受人敬重的精神科醫生


  •  Any past trauma in your life?
   ◎ 你曾有過精神創傷嗎?


  •  For god's sake dry off...
   ◎ 天啊...把衣服弄乾吧 (註: 因為李奧納多全身濕透了, 醫生要他把衣服脫下來以便晾乾)


  •  known proclivity for violence
   ◎ 有暴力傾向

  •  You were committed here by court order 24 months ago.
   ◎ 兩年前你被法院判刑到這裡來

  •  you've uncovered a conspiracy
   ◎ 你揭露了一樁陰謀

  •  Permanent measures will be taken to ensure you can't hurt anyone ever again.
   ◎ 我們將會採取永久性的措施來確定你不再傷害任何人

  •  We're running out of time here, Andrew.
   ◎ Andrew我們已經沒時間了

  •  which would be worse? To live as a monster...or to die as a good man.
   ◎ 這句話是李奧納多在劇中的最後一句話,聽起來挺悲傷的




Coming soon...
INCEPTION (全面啟動)



Reference
  1.  Google線上字典, http://www.google.com.tw/dictionary?hl=zh-TW

Sunday, July 18, 2010

Linkage Directives: extern "C"


◎Abstract
在C++程式中,有時會呼叫其它程式語言所寫的函式,本文將會以: 如何在C++程式中呼叫「C」語言的函式為例子來說明Linkage Directives — extern的使用方法時機,以及一些使用上該注意的事項。

◎Declaring a Non-C++ Function
有兩種方式可以在C++中宣告C函式「single「compound。如果單獨宣告某個函式或變數則使用single的用法;反之,當宣告兩個以上的函式或變數時,則使用compound的用法。使用方法分別如下:
  • single
// single statement linkage directive
extern "C" int func(int);

  • compound
// compound statement linkage directive
extern "C" {
    int func(int);
    double var;
}



Compatibility between C/C++ compiler
為了讓C++的compiler可以引用C語言的一些函式並且不影響原本C語言compiler的處理,也就是,使C與C++的compiler能夠相容我們常常可以在C語言的系統標頭檔、函式庫看到以下這種用法

#ifdef __cplusplus 
extern "C" {
#endif

int func(int);

#ifdef __cplusplus
}
#endif

它代表的是,如果編譯的是C++程式碼,則函式func」會使用extern "C"」來宣告; 如果是C程式碼,則直接宣告

※ 當我們使用C++編譯時,__cplusplus會被自動定義


Overloaded Functions
我們都知道,C++支援Function Overloaded,而C語言並不支持。因此,在C++ Primer(Ref.1)中有底下這麼一題練習題
Exercise18.34:
Explain these declarations and indicate whether they are legal:
extern "C" int compute(int *, int);
extern "C" double compute(double *, double);
答案是:
  • 如果其中一個宣告單獨出現,那麼會是沒問題的
  • 但是,當兩個宣告一起出現的話,就會因為C語言不支持Function Overloaded」而產生錯誤
這是在使用extern "C"」上必須注意的地方

◎Reference
  1. C++ Primer, 4th Edition, S. B. Lippman, J. Lajoie and B. E. Moo
  2. 程式設計師的自我修養 — 連結、載入、程式庫,俞甲子、石凡、潘愛民
  3. http://en.wikipedia.org/wiki/Name_mangling

Friday, July 16, 2010

汽車保養 — 五油三水

前題
因為最近在軍中的高裝檢被分配在輪車組,而對保養進度中的「五油三水」感到好奇,well,反正以後開車也會遇到這些問題,就索性好好來認識這些保養知識 (當兵,還是可以學習很多東西滴)五油三水是汽車的基礎保養,目的就是要讓你的愛車保持健健康康的,如此也可增加行車安全 (行車安全是目前軍中除了救災外,另一項極度注重任務 XD)。雖然現今大部份的車子都擁有電腦噴射引擎系統,可以用來監控各項設施並反應在汽車內的儀表板上,但我們還是得認識一下各項設施所賦予的功能,才有辦法在故障的第一時間做出正確的解決判斷

五油
  • 引擎機油
    老舊的車子可能導致吃機油的現象
    ,我們可以利用油尺來檢察機油是否足夠
  • 汽油
    ...略!!!
    (開車必須要有汽油 XD)
  • 變速箱油(自排油)
    一樣利用油尺來檢查
    ,並且經由其顏色的鮮艷程度可以判斷該油是乾淨或是髒的;甚至如果它的味道聞起來具有焦味,則表示離合器片有不當摩擦現象
  • 煞車油
    打開引擎蓋即可觀察其數量
    ,但需要在規定的時間做更換的動作,如此可以避免因為吸入太多水氣,使沸點降低,而提高煞車失靈機率
  • 方向機油
    參考資料指出許多人會拿機油來代替方向機油
    ,但為了避免方向機受損,盡量使用方向機專用油。此外,也應該避免所謂的Understeer」及Oversteer如下圖:

  •  Understeer
  •  Oversteer


三水 
  • 引擎冷卻水(水箱水)
    水箱水
    的功能就在於散熱,又名引擎冷卻水,因此我們可以瞭解它目的就是要散引擎所產生的熱。有些人會添加水箱精,它主要可以提高沸點降低凝固點,副加價值是防銹省油
  • 雨刷水
    略!!!
    (建議直接用自來水因為參考資料說玻璃清潔劑會有副作用)
  • 電瓶水
    略!!!
    (目前多為免加水電瓶else, 就加個蒸餾水吧)

參考資料
  1. http://www.carbibles.com/
  2. http://car.cool3c.com/article/7483

Sunday, June 27, 2010

[AVR] Line Follower - An application of PID controller

3pi Line Follower
Line Follower是許多基本款的robot都會有的功能之一, 當然3pi也不例外Line Follower, 指的是robot可以延著我們所設定規劃好的路線去行進, 如左圖, 3pi將會在白底背景下, 延著黑線去行進。 它靠著line sensors去讀取黑線與白底間的比例, 來判斷左轉角度、右轉角度或是直接前進, 當然,很多的方法可以用來判斷左右轉的角度, 本篇文章的重點主要要比較「PID control-based Algorithm」以及Heuristic-based Algorithm兩種方法在line follower實驗下的差別




Heuristic-based Algorithm
  • 直接看Line Follower在程式核心部份所使用的heuristic演算法

// This is the "main loop" - it will run forever.
 while(1)
 {
  // Get the position of the line.  Note that we *must* provide
  // the "sensors" argument to read_line() here, even though we
  // are not interested in the individual sensor readings.
  unsigned int position = read_line(sensors,IR_EMITTERS_ON);

  if(position < 1000)
  {
   // We are far to the right of the line: turn left.

   // Set the right motor to 100 and the left motor to zero,
   // to do a sharp turn to the left.  Note that the maximum
   // value of either motor speed is 255, so we are driving
   // it at just about 40% of the max.
   set_motors(0,100);

   // Just for fun, indicate the direction we are turning on
   // the LEDs.
   left_led(1);
   right_led(0);
  }
  else if(position < 3000)
  {
   // We are somewhat close to being centered on the line:
   // drive straight.
   set_motors(100,100);
   left_led(1);
   right_led(1);
  }
  else
  {
   // We are far to the left of the line: turn right.
   set_motors(100,0);
   left_led(0);
   right_led(1);
  }
 }


  • 我們發現, heuristic演算法靠著sensor所回傳的黑白比例, 來做為左右轉或直走的依據; 並且, 轉彎時始終保持單一速度


PID control-based Algorithm
PID分別為proportional–integral–derivative的縮寫, 它被廣泛應用在溫度、流量或是電機傳動等等的控制上, 其理論基礎這篇文章沒辦法細說, 但概念上我們可以透過下面的說明來瞭解 首先Line Follower這個例子來講, 底下我們分別說明proportionalintegralderivative:
  • Proportional:比例, 經由sensor所傳回量化後的值, 我們可以算出3pi位於白底或黑線上的比例為何, 這就是P
  • Integral: 積分, I值紀錄了3pi的活動軌跡, 也就是在一段運行時間中, 其P」值的加總
  • Derivative: 微分, 「D」值紀錄了前後P」值的
當我們分別得到了PI、D, 且分別加上一些weighting (magic number)[註一], 最後所得到的值, 代表的是3pi的左右兩個motor在setting值上的, 而物理意義代表的是3pi轉彎的角度大小


[註一]: 目前weighting值是turing得到的(所以稱為magic number), 但是, 有許多的研究針對如何turing」提出很多新的方法, 底下截圖來自wiki [1]上針對不同方法的優缺點所做的整理, 有興趣的話倒是可以試試這些方法



  • PID Algorithm的Source Code
// This is the "main loop" - it will run forever.
 while(1)
 {
  // Get the position of the line.  Note that we *must* provide
  // the "sensors" argument to read_line() here, even though we
  // are not interested in the individual sensor readings.
  unsigned int position = read_line(sensors,IR_EMITTERS_ON);

  // The "proportional" term should be 0 when we are on the line.
  int proportional = ((int)position) - 2000;

  // Compute the derivative (change) and integral (sum) of the
  // position.
  int derivative = proportional - last_proportional;
  integral += proportional;

  // Remember the last position.
  last_proportional = proportional;

  // Compute the difference between the two motor power settings,
  // m1 - m2.  If this is a positive number the robot will turn
  // to the right.  If it is a negative number, the robot will
  // turn to the left, and the magnitude of the number determines
  // the sharpness of the turn.
  int power_difference = proportional/20 + integral/10000 + derivative*3/2;

  // Compute the actual motor settings.  We never set either motor
  // to a negative value.
  const int max = 60;
  if(power_difference > max)
   power_difference = max;
  if(power_difference < -max)
   power_difference = -max;

  if(power_difference < 0)
   set_motors(max+power_difference, max);
  else
   set_motors(max, max-power_difference);
 }
  • PID short summary


    PID control uses continuous functions to compute the motor speeds, so that the jerkiness of the previous example can be replaced by a smooth response. [2]
    • 上面這句話是節錄自3pi的使用文件中, 這句話完全說明了整個PID control的精神, 以及使用後所帶來的好處
    • 如果翻成中文, 再加上自己的一些解釋之後, 可以這麼說的: 在Heuristic Algorithm下運作的3pi流暢度低於PID control Algorithm, 因為PID control Algorithm使用了連續函式, 物理意義上可以說它考慮了前後軌跡, 來做為下一個行進路線的參考, 相較於Heuristic Algorithm忽略前面一個軌跡, 3pi在使用PID control Algorithm的行進當然會比較順!


Experiments
  • 實驗目標及方向很明確, 我們想要證明: 3pi在使用PID control-based Algorithm時會比使Heuristic Algorithm走得順, 沒錯, 就是要證明一個字 順!
  • 底下我們直接展示兩種不同演算法在3pi上的成果, 相信大家都看得出來哪一個比較smooth
  • Heuristic Algorithm
  •  
     
  • PID control-based Algorithm
     




Conclusion
Line Follower, 常被拿來當成機器人競賽的項目, 而賽車比賽中的勝負常常在轉彎的時候來決定, 從本篇文章實驗的部份就可發現不同的方法影響了轉彎時不同的流暢度, 它靠的是PID control-based AlgorithmHeuristic-based Algorithm多考慮了歷史的軌跡。然而, PID control-based Algorithm畢竟採用的是magic number, 如何讓這些參數變得更有意義將會是未來研究上的議題


「Reference
  1. http://en.wikipedia.org/wiki/PID_controller
  2. http://www.pololu.com/docs/0J21/7

Monday, June 21, 2010

How to Install OpenCV on Linux (Ubuntu)

前言
對許多人來講, 在Windows下安裝OpenCV會比在Linux下安裝相對來得輕鬆Windows下, 沒錯, 您幾乎只需要重複「下一步」的動作就可以安裝大部分的程式, 但其實在Linux下亦不難, 或許只是您不熟悉如何去下指令? 沒關係, 這篇文章簡單整理了在Ubuntu底下安裝OpenCV的兩種方法其一, 是當您想更改OpenCV的source code並自己重新編譯的方法;  另外, 則是一般的apt-get安裝方式。底下我將一步步來介紹整個流程

Before the installation」
  • 為了可以重新編譯OpenCV, 系統中必須要有C++ compiler, 如果沒有, 我們可以透過以下指令來安裝

    $ sudo apt-get install g++
    
  • 為了圖片的播放, 我們需要GTK+, 它是一個跨平台的圖形介面函式庫, GNOME即是利用GTK+來開發的。首先, 我們可以藉由以下命令來查看系統安裝了哪個版本的GTK+

    $ dpkg -l | grep libgtk
    or, 可以直接安裝

    $ sudo apt-get install libgtk2.0-dev


「Install OpenCV on Ubuntu (method 1)」
  1. http://sourceforge.net/projects/opencvlibrary/files/下載原始碼(Linux版
  2. 解壓該原始碼檔案, 且切換至該目錄假設下載的是opencv-1.0.0.tar.gz, 則我們可以使用下列命令

    tar zxvf opencv-1.0.0.tar.gz
    cd opencv-1.0.0
  3. 檢查系統所需環境是否已經安裝完畢
  4. ./configure
    當出現「Now run make...」則表示可以開始編譯了, 否則, 必須安裝所需安裝的套件
  5. 編譯OpenCV
  6. make
  7. 使用root權限來安裝OpenCV
  8. sudo make install
  9. 增加路徑/usr/local/lib」到/etc/ld.so.conf」中, 這一步主要是在設定動態函式庫的路徑,  其中/etc/ld.so.conf就是目前系統中主要用來放置函式庫的目錄
  10. 設定完路徑後, 必須重建動態函式庫目錄
  11. ldconfig



「Install OpenCV on Ubuntu (method 2)
Ubuntu底下的Synaptic Package Manager方便了我們對於各種軟體的安裝, 並且隨時可以線上更新最新版本。對於利用Synaptic來安裝OpenCV, 我們只需收尋OpenCV且套用所收尋到的套件, 就可以直接安裝了, 安裝畫面如下


或者, 您也可以打開terminal輸入想安裝的套件, 如下面指令
$ sudo apt-get install libcv4 libcvaux4 libhighgui4
$ sudo apt-get install libcv-dev libcvaux-dev libhighgui-dev
當然, 指令會因函式庫的版本名稱不同而有所不一樣最後, 設定環境變數
export LD_LIBRARY_PATH=/home/opencv/lib
export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig

「執行OpenCV範例程式
底下, 我測試了OpenCV給的範例程式camshiftdemo.c,切換到範例所在目錄且利用以下命令編譯及執行
$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm camshiftdemo.c
$ ./a.out
執行畫面如下