LoginSignup
6
5

More than 5 years have passed since last update.

手を振るだけで Thetaでピヨッとやる

Last updated at Posted at 2015-02-11

RICOH THETAを愛用していますが、プログラムから制御するのはいまいち難しそうに思っていて、これまでやったことがありませんでした。昨日たまたまnode.jsのパッケージを見つけ、これなら自分にも使えそうと思いやってみました。

まずはMacで動かす

node.jsはインストールされている前提です。npmでricoh-thetaパッケージをインストールします。
$ npm install ricoh-theta -g

このあと、私の環境ではthetaコマンドの実行でエラーが出ました。
$ theta --help
/usr/bin/env: coffee: No such file or directory

CoffeeScriptが入ってませんでした。
$ npm install coffee-script -g

thetaコマンドに captureオプションを指定すると写真が撮れますし、バッテリー残量なども分かります。最高ですね!

Node-REDに組み込む

何かをトリガーにしてTheta写真を撮ることをやってみようと思ったので、ricoh-thetaパッケージをNode-REDのノードとして組み込んでみました。橋本商会さんのサンプルとNode-REDのサンプルを足しただけのコードです。
このノードをつなぐと、とにかくThetaにつないで写真を撮ろうとします。

theta.js
module.exports = function(RED) {
    "use strict";
    // require any external libraries we may need....
    var Theta = require('ricoh-theta');
    var fs = require('fs');

    // The main node definition - most things happen in here
    function ThetaNode(n) {
        // Create a RED node
        RED.nodes.createNode(this,n);

        // Store local copies of the node configuration (as defined in the .html)
        this.topic = n.topic;

        // copy "this" object in case we need it in context of callbacks of other functions.
        var node = this;

        var theta = new Theta();
        var filename;

        // respond to inputs....
        this.on('input', function (msg) {
            theta.connect('192.168.1.1');
            node.log("connecting to theta");
        });

        this.on("close", function() {
        });

        // capture
        theta.on('connect', function(){
          node.log("theta.on 'connect'");
          theta.capture(function(err){
            if(err) return console.error(err);
            console.log('capture success');
          });
        });

        // get picture
        theta.on('objectAdded', function(object_handle){
          node.log("theta.on 'objectAdded'"+object_handle);
          theta.getPicture(object_handle, function(err, picture){
            fs.writeFile(object_handle.toString()+'.jpg', picture, function(err){
              console.log('picture saved => '+object_handle);
              theta.disconnect();
            });
          });
        });        
    }

    // Register the node by name. This must be called before overriding any of the
    // Node functions.
    RED.nodes.registerType("theta",ThetaNode);

}

Arduinoと接続

この前作った焦電センサーを使った回路が残っていたので流用しました。周囲の動きを検出してデジタル出力するだけの簡単なものです。

Firmataを使えばデジタル入力をいい感じに扱えそうだったのですが、なぜかうまく動きませんでした。ちなみに利用したバージョンは以下の通り。
node v0.12.0
node-red v0.10.1

仕方ないので、シリアルから読み込んだ文字列を判定して、"Hello Theta!"が含まれていたらシャッターを切るようにしました。

Arduino側は、動きを検出すると"Hello Theta!"をシリアルに出力し40秒間待ちます。その間はLEDをチカチカさせます。そんだけ。

Node-REDのワークフローです。

flow
[{"id":"dd88bcdc.22774","type":"serial-port","serialport":"/dev/cu.usbmodemfa131","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":true},{"id":"b1052588.4efad8","type":"theta","name":"theta","topic":"Hello Theta!","x":412.3333282470703,"y":228.3333282470703,"z":"dfd1c22b.202e4","wires":[[]]},{"id":"1b771ddc.e488e2","type":"debug","name":"","active":false,"console":"true","complete":"payload","x":389,"y":81.42855834960938,"z":"dfd1c22b.202e4","wires":[]},{"id":"6866f4c9.97990c","type":"serial in","name":"arduino","serial":"dd88bcdc.22774","x":94.42855834960938,"y":85.4285659790039,"z":"dfd1c22b.202e4","wires":[["1b771ddc.e488e2","b92f9fdf.46d06"]]},{"id":"b92f9fdf.46d06","type":"switch","name":"","property":"payload","rules":[{"t":"cont","v":"Hello Theta!"}],"checkall":"true","outputs":1,"x":236.42855834960938,"y":204.4285659790039,"z":"dfd1c22b.202e4","wires":[["b1052588.4efad8"]]}]

動画

YouTubeへのリンク

まとめ

これだけやるのに半日くらいかかってしまいました。スキルのある方なら30分くらいの仕事と思います。もっとサクッと作れるように精進したいと思います。

今後は、Raspberry Piでnode-redもセンサーもThetaも全部制御してみたいです。たとえば、Twitterでつぶやいたらピヨッとやるのもできそうです。その場合、Thetaへの接続とインターネットへの接続の両方が必要になるので、どうやってやるんだろう?とか、そんなことろが次の課題です。

6
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5