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 - 对于其他操作系统,请遵循 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中。将文件 rxtxSerial.dllrxtxParallel.dll从 RXTX i368-mingw32 文件夹复制到文件夹 jre/bin中。所以在我的示例中,我将这两个文件放在文件夹 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 打开安装/更新向导

  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. 显示文本消息的控制台窗格

  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 的 Console 区域看到您的两条消息,并在 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.