CamScripter 入門#

概觀#

它是什麼?#

CamScripter 是一個協助您為 CMUcam3 撰寫及除錯自訂指令碼的工具。它讓使用 Lua 程式語言撰寫指令碼成為可能。運用這個語言以及為 CMUcam3 所提供的 Lua API,您幾乎可以將相機自訂為任何您所能想到的用途。在本教學中,我們將帶您走過安裝流程,然後開發一個範例指令碼在相機上執行。

在您開始之前#

本教學假設:

安裝,第 1 部分:Lua、序列埠通訊#

Lua#

CamScripter 讓您能夠使用 Lua 程式語言為 CMUcam3 撰寫程式。在能夠這麼做之前,我們需要先在您的電腦上安裝這個語言。

  1. Download the appropriate ZIP binary. (For Windows: Lua-Windows or for other users https://luabinaries.sourceforge.net/download.html)

  2. 將 Lua 二進位檔解壓縮至 C:\lua

  3. 開啟命令提示字元(在 Windows 中,從開始功能表選擇「Run...」,然後輸入「cmd」)。

  4. cd 到您解壓縮 Lua 檔案的任何位置。以我為例,我輸入「cd C:lua」

  5. 輸入「luax.x.exe -v」,其中 x 為 Lua 版本

Assuming everything went smoothly in your installation, you should see the Lua version number printed out on your screen.

If possible, you should install lua from the CMUcam3 web site. If you want to recompile it on your own, you need to first patch the normal lua source to not use floating point (lua-5.1-no-fp.patch).

在 Windows 的 cygwin 下,使用以下幾行來編譯:

cd src
make LUAC_T=luac.exe MYCFLAGS=-mno-cygwin MYLDFLAGS=-mno-cygwin luac.exe
...
make LUA_T=lua.exe MYCFLAGS=-mno-cygwin MYLDFLAGS=-mno-cygwin lua.exe

Java 執行階段環境 (JRE)#

CamScripter 需要 Java 執行階段環境(版本 6 或更高版本)。如果您尚未安裝 JRE,您可以在 這裡 下載並安裝一個。

RXTX#

The CamScripter uses the RXTX serial communication library to communicate with the CMUcam3 over your computer's serial port. This library will have to be installed on your computer. First, download RXTX (at least version 2.1). Version 2.1.7 is here: http://rxtx.qbang.org/pub/rxtx/rxtx-2.1-7-bins-r2.zip.

若要安裝(Windows only - for other OS's, follow instructions in the rxtx INSTALL file):

  1. 將 RXTX 下載檔解壓縮,並瀏覽進入產生的資料夾

  2. 按兩下 Windows

  3. 按兩下 i368-mingw32

  4. 現在您必須找出您的 Java 執行階段環境位於何處。它可能在 C:\Program Files\Java\jre<version number>。舉例來說,我的位於 C:\Program Files\Java\jre1.6.0_01。將 rxtxSerial.dllrxtxParallel.dll 這兩個檔案從 RXTX i368-mingw32 資料夾複製到 jre/bin 資料夾中。所以以我的例子來說,我把那兩個檔案放在 C:\Program Files\Java\jre1.6.0_01\bin 資料夾中。

安裝,第 2 部分:Eclipse#

好了,我們差不多準備就緒了!只剩下幾件事要做。

如果您還沒有安裝 Eclipse3.2 版或更新版本),請安裝它。您可以從 http://eclipse.org/downloads 安裝。Eclipse Classic 是個穩妥的選擇,不過所列出的任何其他套件應該也可以運作。若要安裝,只需將您選擇的套件解壓縮至 C:\Program Files\eclipse

現在 Eclipse 已安裝,我們需要將 CamScripter 安裝為 Eclipse 外掛程式:

  1. C:\Program Files\eclipse\ 中按兩下 eclipse.exe 以開啟 Eclipse

  2. 按下 Enter 以接受預設工作區

  3. 透過選擇 Help -> Software Updates -> Find and Install 開啟安裝/更新精靈

  4. 選擇「Search for new features to install」選項按鈕,然後按一下 Next。

  5. 按一下「New Remote Site...」按鈕

  6. For name, enter Camscripter, and for URL enter http://cmucam.org/~juanpablo/. Click OK.

  7. 確保 Camscripter 旁邊的方塊已勾選,然後按一下「Finish」按鈕。

  8. 選擇「Camscripter」作為要安裝的功能,然後按一下 Next。

  9. 接受授權合約,然後按一下「下一步」

  10. 按一下 Finish - Eclipse 會為您下載外掛程式

  11. 按一下「Install」以安裝 Camscripter 外掛程式。

  12. 按一下 Yes 以重新啟動 Eclipse 平台

接著 Eclipse 會重新啟動,並為您安裝好 Camscripter 外掛程式。若要確認它已安裝,請選擇 Window -> Show View -> Other...

Click the plus sign next to CMUcam3, select "CamScripter", and click OK. You should see the CamScripter view at the bottom of Eclipse (the area surrounded in red). You can expand the bottom area if it is too small.

  1. CamScripter 檢視

  2. 顯示文字訊息的主控台窗格

  3. 顯示影像與圖形訊息的回饋窗格

  4. 執行 Lua 程式碼。您可以執行整個檔案,或僅執行選取的程式碼行。

  5. 將 Lua 程式碼安裝於 CMUcam3 的 SD/MMC 卡上

設定#

我們已經安裝好一切了!只要再幾個設定步驟,我們就能為 CamScripter 撰寫程式了。

將 camscripter 專案載入相機#

Now that we have everything installed, we need to load the camera with our CamScripter project. Run make on the camscripter project located in projects/camscripter (in the source code tree you checked out in Quick Start Guide), and then load the hex file onto the camera. Turn the camera off once the file is loaded.

設定 CamScripter 偏好設定#

現在回到 Eclipse,並開啟 CamScripter 偏好設定畫面:Window --> Preferences --> CamScripter Setup。

  • Set the Luac location to point to your luacx.x.exe (where x is the Lua version) file (note the 'c') wherever you installed Lua in Installation, Part 1: Lua, Serial Port Communication.

    • 對於有興趣的人,luac 只是編譯 lua 程式碼,而 luax.x.exe 則實際解譯並執行程式碼。我們在 Eclipse 中編譯程式碼,然後在相機上執行它——因此稱為 luac

  • 提供您連接相機所用序列埠的名稱。您可以按一下「See Serial Ports」按鈕,查看可用序列埠的清單。

Hello, World!#

Now that we have everything installed and configured properly, let's tackle the obligatory Hello, World program. First, you'll have to create a new project. Select File --> New --> Project... You'll get a box allowing you to select the type of project you'd like to create. Double-click "General", select "Project", and click the Next button.

將您的專案命名為「Hello」,然後按一下 Finish 按鈕。接著透過按一下 File --> New --> File 建立新檔案(在 Eclipse 3.3 中,是 File --> New --> Other --> General --> File)。在檔名方塊中,輸入「hello_world.lua」,並以 Hello 作為父資料夾。按一下 Finish 按鈕。

再次確認您的相機已連接至電腦的序列埠。在開啟相機之前,了解相機支援的兩種模式很重要:

  • 獨立式:相機完全不預期與 PC 有任何互動。相機預設以此模式啟動。

  • 互動式:相機接收來自 Eclipse 中 CamScripter 的已編譯 Lua 程式碼並回傳回饋。若要讓相機進入互動模式,請將其開機,當您看到橘色 LED 亮起時,按下相機按鈕(您有 3 秒的時間按下按鈕)。 此時,橘色 LED 會閃爍數次,讓您知道我們正在進入互動模式。如果一切順利,您會看到訊息 Welcome to CamScripter Interactive mode. Click the execute button to run a script.

讓我們以互動模式啟動相機,如此我們便能執行第一個 Lua 程式。

在編輯器視窗中,輸入這行程式碼:

print("Hello, World!");

儲存檔案,然後在 CamScripter 檢視中按一下 Execute 按鈕。然後瞧!!您的程式碼將透過連線傳送到相機,相機執行它,並回傳您印出的訊息。

現在讓我們嘗試一些更有趣的東西。新增另外三行程式碼,讓您的檔案 hello_world.lua 包含:

print("Hello, World!");
take_picture("pic_name");
print_picture("pic_name");
print("It's a picture!");

現在再次按一下 Execute 按鈕。這次執行會花較長的時間,因為透過序列埠傳送影像相當耗時。不過一旦完成,您應該會在 CamScripter 的主控台區域看到您的兩則訊息,並在回饋區域看到您用相機拍攝的影像(您有拿掉鏡頭蓋吧,對吧?)

You'll notice that it's a pretty low resolution image. The camera is set to take low resolution pictures by default in the CamScripter, as it takes quite a bit longer to send high resolution images through the serial port. However, you can change the camera to high resolution using function LuaApiFunctions.