CamScripterを始める#

概要#

これは何ですか?#

CamScripter は、CMUcam3 向けのカスタムスクリプトの作成とデバッグを支援するツールです。Lua プログラミング言語を使ってスクリプトを書くことができます。この言語と、提供される CMUcam3 用の Lua API を使えば、思いつくほぼあらゆる用途にカメラをカスタマイズできます。このチュートリアルでは、インストール手順を順を追って説明し、その後カメラで実行するサンプルスクリプトを開発します。

始める前に#

このチュートリアルは以下を前提としています:

  • すでに CMUcam3 をお持ちです

  • You've already gotten the camera working, using either the Quick Start Guide or the SDK installation guide

  • カメラをコンピュータに接続するために必要なものはすべて揃っています

インストール、パート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. Lua ファイルを解凍した場所に cd します。私の場合は '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 のみ - 他の OS の場合は、rxtx の INSTALL ファイルの手順に従ってください):

  1. RXTX のダウンロードを解凍し、生成されたフォルダに移動します

  2. Windowsをダブルクリックします

  3. i368-mingw32をダブルクリックします

  4. 次に、Javaランタイム環境がどこにあるかを見つける必要があります。おそらく C:\Program Files\Java\jre<version number> にあります。例えば私の場合は C:\Program Files\Java\jre1.6.0_01 にあります。RXTX i368-mingw32フォルダから rxtxSerial.dllrxtxParallel.dll のファイルを jre/bin フォルダにコピーします。したがって私の例では、これら2つのファイルを C:\Program Files\Java\jre1.6.0_01\bin フォルダに置きました。

インストール、パート2:Eclipse#

さあ、ほぼ準備完了です!やることはあと少しです。

まだお持ちでない場合は、Eclipseバージョン3.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 を選択して、Install/Updateウィザードを開きます

  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. ライセンス契約に同意し、Next をクリックします

  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. テキストメッセージが表示されるConsoleペイン

  3. 画像とグラフィックメッセージが表示されるFeedbackペイン

  4. Luaコードを実行します。ファイル全体を実行することも、選択したコード行だけを実行することもできます。

  5. CMUcam3のSD/MMCカードにLuaコードをインストールします

セットアップ#

すべてインストールできました!あといくつかのセットアップ手順で、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として "hello_world.lua" と入力します。Finishボタンをクリックします。

カメラがコンピュータのシリアルポートに接続されていることを再確認してください。カメラの電源を入れる前に、カメラがサポートする2つのモードを理解することが重要です:

  • スタンドアロン:カメラは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ボタンをクリックします。すると、ほら!!あなたのコードが電線を通ってカメラに送られ、カメラがそれを実行して、出力したメッセージを送り返します。

では、もう少し面白いことを試してみましょう。3行のコードを追加して、ファイルhello_world.luaが次の内容を含むようにします:

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

では、再度Executeボタンをクリックします。シリアルポートを介した画像の送信はかなり時間がかかるため、今回は実行に時間がかかります。しかし、完了すると、CamScripterのConsole領域に2つのメッセージが、Feedback領域にカメラで撮影した画像が表示されるはずです(レンズキャップは外しましたよね?)

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.