0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

カチャカプロをNode.js版のgRPCを使ってNode-REDで制御する

Last updated at Posted at 2026-01-26

背景

カチャカプロは台車の重さを考慮すると20kgくらい運べる小型搬送機アップデートで複数タスクを指定できるようになったこともあり、標準アプリだけでも活用が出来るようになった。

でもさすがにエレベータ連携とかを標準アプリに期待は出来ないので連携できるような仕組みを構築している。 IoTシステムとしてNode-REDを使っていることもあり、Node-REDとの連携を検討したが更新が途絶えているようだった。

issuesを見ても誰もメンテナンスしてない感を感じた。枯れていればバージョン更新されていなくてもよいかと思うが・・・
https://github.com/julien-pal/node-red-contrib-grpc/issues

カチャカとしてはROS2ブリッジを立てる案もある。
https://github.com/pf-robotics/kachaka-api/blob/main/docs/ROS2.md

ただ、APIブリッジサーバを立てて管理するものを増やしたくは無い。
もやもやして代替案を模索した結果、gRPC自体はNode.js用のモジュールを配布していたので
Node.jsのライブラリを導入してNode-REDから呼ぶことにした。

方法

1.現在のNode-REDのプロジェクトに依存する公式のgRPCを導入する。

cd ~/.node-red/projects/<プロジェクト名>
npm install --save @grpc/grpc-js @grpc/proto-loader

2.最新のコマンドリストを取得する

git clone https://github.com/pf-robotics/kachaka-api.git

3.Node-REDで外部モジュールを使える設定に変更する

nano ~/.node-red/projects/<プロジェクト名>/settings.js
functionExternalModules: true,

4.Functionコードの設定を次のように設定して
image.png

対応するコマンドをコードで実行する。以下はGetRobotVersion

(async () => {
  const PROJECT_ROOT = '/home/ユーザ名/.node-red/projects/<プロジェクト名>';
  const PROTO = path.resolve(PROJECT_ROOT, 'kachaka-api', 'protos', 'kachaka-api.proto');
  const KACHAKA_ADDR = 'カチャカのIP:26400';// mdnsがいけるなら kachaka-シリアル番号.local:26400

  try {
    // .proto をロード
    const pkgDef = await protoLoader.load(PROTO, {
      keepCase: true,
      longs: String,
      enums: String,
      defaults: true,
      oneofs: true,
      includeDirs: [path.resolve(PROJECT_ROOT, 'kachaka-api', 'protos')]
    });
    const desc = grpc.loadPackageDefinition(pkgDef);

    const Service = desc.kachaka_api && desc.kachaka_api.KachakaApi;
    if (!Service || !Service.service) {
      node.error('サービス kachaka_api.KachakaApi が見つかりません', msg);
      return;
    }

    const client = new Service(
      KACHAKA_ADDR,
      grpc.credentials.createInsecure(),
      {
        'grpc.keepalive_time_ms': 60000,
        'grpc.keepalive_timeout_ms': 20000,
        'grpc.enable_retries': 1
      }
    );

    if (typeof client.GetRobotVersion !== 'function') {
      node.error('メソッド GetRobotVersion が見つかりません', msg);
      return;
    }

    client.GetRobotVersion({}, (err, res) => {
      if (err) {
        node.error(err, msg);
        return;
      }
      msg.payload = res;
      node.send(msg);
    });

  } catch (e) {
    node.error(e, msg);
  }
})();
return;

結果

{"_msgid":"XXXXXXXXXXXX","payload":{"metadata":{"cursor":{"low":999760898,"high":29,"unsigned":false}},"version":"3.14.7"},"topic":""}

バージョン取得OK
https://kachaka.zendesk.com/hc/ja/articles/14629480585487-%E3%82%AB%E3%83%81%E3%83%A3%E3%82%AB%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7%E3%82%A2%E3%82%A2%E3%83%83%E3%83%97%E3%83%87%E3%83%BC%E3%83%88%E3%81%AE%E3%81%8A%E7%9F%A5%E3%82%89%E3%81%9B-Ver-3-14-7

メリット

プロジェクトごとにgRPCのバージョンやproto(カチャカのコマンドリストなどの定義)を管理できる
Node-REDのプロジェクトクローン時に付随してコピーされる

デメリット

GUIで楽に導入できない
バージョンアップも基本コマンド

まとめ

Node-REDからNode.jsのgRPCライブラリを使って通信可能。動作もサクサク。
カチャカプロのカメラ映像も取得出来ました。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?