> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/workflows/ja/chtoriaru/integrate-with-ignition/live-data/write-results-to-ignition.md).

# 結果をIgnitionに書き込む

Roboflow Workflow の結果を OPC UA 経由で Ignition のタグに書き込み、ビジョンの結果をトリガーとして Ignition のタグスクリプト、アラーム、カウンターを動作させます。

この方法では、エッジデバイス上で実行される Roboflow Workflow が、OPC UA 経由で結果を Ignition タグに直接書き込みます。その後、Ignition のタグスクリプトがアクション（例：アラーム、排除、カウンター、データベースへの記録）を実行します。

{% hint style="info" %}
OPC UA Writer Sink、OPC UA Reader、OPC UA Server は、Enterprise のお客様のみご利用いただけます。 [詳しくは Roboflow の営業チームに](https://roboflow.com/sales) お問い合わせください。
{% endhint %}

{% hint style="info" %}
以下の手順とスクリーンショットは、Ignition 8.1（Designer 8.1.55）のものです。メニュー名、ダイアログ、プロパティ名は、8.3 を含む他のバージョンでは異なる場合があります。
{% endhint %}

## Ignition OPC UA サーバーを設定する

この方法では、Roboflow デバイスが OPC UA クライアント、Ignition がサーバーになります。Ignition のデフォルト設定では、3つの点でこの接続がブロックされます。Gateway で「Config」>「OPC UA」>「Server Settings」の順に移動し、「Show advanced properties」をクリックします。

<table data-search="false"><thead><tr><th>設定</th><th>デフォルト</th><th>設定値</th><th>理由</th></tr></thead><tbody><tr><td>「Bind Addresses」</td><td><code>localhost</code></td><td><code>0.0.0.0</code></td><td>デバイスは localhost にバインドされたサーバーに接続できません</td></tr><tr><td>「Security Policies」</td><td><code>Basic256Sha256</code></td><td><code>None,Basic256Sha256</code></td><td>OPC UA Writer Sink はクライアント証明書なしで接続します</td></tr><tr><td>「Expose Tag Providers」</td><td>オフ</td><td>オン</td><td>これをオンにしないと、Ignition タグは OPC UA 経由で参照できません</td></tr></tbody></table>

<figure><img src="https://4077895066-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO0Ceoy9R0lHDZATcPyh%2Fuploads%2Fgit-blob-d4eb99bbba88f95838f26b1e964fe879a743d24d%2Fignition-opcua-server-settings.png?alt=media" alt="The Ignition 8.1 OPC UA Server Settings page with Bind Addresses set to 0.0.0.0, Security Policies set to None,Basic256Sha256, Anonymous Access Allowed off, User Source opcua-module, and Expose Tag Providers on"><figcaption><p>この方法で使用する OPC UA サーバー設定。</p></figcaption></figure>

「Save Changes」をクリックします。OPC UA モジュールが再起動するまで、サーバーには以前の設定が適用されたままです。「Config」>「System」>「Modules」の順に移動し、「OPC-UA」行の「restart」をクリックして、確認します。

匿名アクセスはオフのままにしてください。Ignition の `opcua-module` ユーザーソースには、ユーザー `opcuauser` と、文書に記載されたデフォルトパスワードが含まれており、その `AuthenticatedUser` ロールにはタグへの書き込み権限があります。パスワードを変更するか、Roboflow 用の別のユーザーを追加してください。

## タグの例

このページでは、手順を説明するために2つのタグを使用します。これらは説明用の例です。ご利用のラインに合わせて名前、フォルダー、データ型を独自に定義し、Workflow でも同じ名前を使用してください。

<table data-search="false"><thead><tr><th>タグ</th><th>データ型</th><th>説明</th></tr></thead><tbody><tr><td><code>Roboflow/Trigger</code></td><td>ブール値</td><td>Workflow の条件が成立したとき（例：モデルが人を検出したとき）、デバイスが <code>true</code> を書き込みます。Ignition のタグスクリプトがアクションを実行し、その後タグを <code>false</code>.</td></tr><tr><td><code>Roboflow/TriggerCount</code></td><td>整数（Int4）</td><td>アクションの実行回数を数えます。実際のアクション（例：排除、アラーム、データベースへの記録）の代わりに使用します。</td></tr></tbody></table>

## タグとアクションを作成する

Designer で、Tag Browser の `default` プロバイダーを選択し、次の名前のフォルダーを作成します。 `Roboflow`。フォルダーを右クリックし、「New Tag」>「Memory Tag」を選択します。Tag Editor で「Name」に `Trigger` を設定し、「Data Type」を「Boolean」にして、「OK」をクリックします。同じ方法で `TriggerCount` を追加し、「Data Type」を「Integer」に設定します。

<figure><img src="https://4077895066-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO0Ceoy9R0lHDZATcPyh%2Fuploads%2Fgit-blob-fadbfc0c495319954ba514b198edbb4475dd076c%2Fignition-designer-new-memory-tag.png?alt=media" alt="The Ignition 8.1 Tag Editor for a new tag with Name Trigger, Value Source Memory, and Data Type Boolean"><figcaption><p>新しい <code>Trigger</code> メモリータグの Tag Editor。</p></figcaption></figure>

Writer Sink は「Object Name」と「Variable Name」を組み合わせてタグを指定するため、この例ではタグをフォルダー内に配置しています。代わりにプロバイダーのルートにあるタグへ書き込むには、「Object Name」にタグの完全なパス（例： `[default]Trigger`）にして、「変数名」は空欄にします。

ダブルクリックして `Trigger` を開きます。タグエディターで「スクリプト」>「値の変更」に移動し、スクリプト本文に次のコードを貼り付けます。

```python
という名前の物体検出出力があることを前提としています。
if initialChange or not currentValue.value:
    return
system.util.getLogger("Roboflow").info("Roboflow trigger fired")
count = system.tag.readBlocking(["[default]Roboflow/TriggerCount"])[0].value or 0
system.tag.writeAsync(["[default]Roboflow/TriggerCount", "[default]Roboflow/Trigger"], [count + 1, False])
```

<figure><img src="https://4077895066-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO0Ceoy9R0lHDZATcPyh%2Fuploads%2Fgit-blob-17772b93bfee8a9f18bc3f8e751ae9929ca3ec9d%2Fignition-designer-trigger-script.png?alt=media" alt="The Ignition Tag Editor for the Trigger tag, on Scripting > Value Changed, with a script that logs Roboflow trigger fired, increments TriggerCount, and sets Trigger back to false"><figcaption><p>の値変更スクリプトは <code>Trigger</code>.</p></figcaption></figure>

ロガーとカウンターの行を、独自のアクション（例： `system.tag.writeAsync` PLCタグへの書き込み、 `system.db.runPrepUpdate` によるイベントの記録、またはアラーム）に置き換えます。最後の書き込みは残してください。これは `Trigger` を `false`に戻します。これがないと、次の `true` は値の変化とみなされず、スクリプトは再実行されません。

## ワークフローを作成する

ワークフローはモデルを実行し、条件を確認して、条件を満たすと `true` を `Trigger` に書き込みます。ワークフローエディターで次の手順を実行します。

1. [ワークフローを作成し、](https://docs.roboflow.com/workflows/ja/gou-zhu/create-a-workflow.md) モデルを指定した「物体検出モデル」ブロックを追加します。
2. モデルの後に「条件を満たす場合に続行」ブロックを追加し、アクションを開始する条件を設定します（例：モデルが少なくとも1つの `person`).
3. 「条件を満たす場合に続行」の後に「OPC UA Writer Sink」ブロックを追加します。「Workflow Blocks」で「OPC UA」を検索すると見つかります。

<figure><img src="https://4077895066-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO0Ceoy9R0lHDZATcPyh%2Fuploads%2Fgit-blob-53083c474eb542fb6f0c3b3555447b728e7546ed%2Fignition-workflow-opc-writer-graph.png?alt=media" alt="A Workflow in the Roboflow Workflow editor with an Object Detection Model block named model, a Continue If block, and an OPC UA Writer Sink block named ignition_trigger, with outputs predictions, opc_error_status, and opc_message" width="400"><figcaption><p>モデル、条件、Writer Sink。</p></figcaption></figure>

OPC UA Writer Sinkをクリックして各フィールドに入力し、「追加プロパティを表示」をクリックしてサインインと検索の設定を行います。Ignitionでは、これらの設定が重要です。

<table data-search="false"><thead><tr><th>フィールド</th><th>値</th><th>理由</th></tr></thead><tbody><tr><td>「URL」</td><td><code>opc.tcp://&#x3C;ignition-host>:62541</code></td><td>IgnitionのOPC UAエンドポイント</td></tr><tr><td>「名前空間」</td><td><code>urn:inductiveautomation:ignition:opcua:tags</code></td><td>Ignitionのタグの名前空間URIです。ブロックは接続時にインデックスを検索します。</td></tr><tr><td>「オブジェクト名」</td><td><code>[default]Roboflow</code></td><td>タグプロバイダーとフォルダー</td></tr><tr><td>「変数名」</td><td><code>Trigger</code></td><td>タグ名</td></tr><tr><td>「値」</td><td><code>1</code></td><td>書き込む値です。このフィールドには数値を指定し、ブロックは <code>1</code> として書き込みます <code>true</code> という Boolean タグにします。</td></tr><tr><td>「値の型」</td><td>「Boolean」</td><td>タグのデータ型と一致させる必要があります</td></tr><tr><td>「クールダウン（秒）」</td><td><code>5</code></td><td>書き込み間の最短時間（秒）。 <code>0</code> クールダウンを無効にします。</td></tr><tr><td>「ユーザー名」と「パスワード」</td><td>Ignition の OPC UA ユーザー（例： <code>roboflow</code>)</td><td>Ignition は匿名ユーザーによる書き込みを拒否します</td></tr><tr><td>「ノード検索モード」</td><td>「direct」</td><td>Ignition はタグを文字列の NodeId で指定するため、このブロックは <code>ns=&#x3C;index>;s=[default]Roboflow/Trigger</code>を生成します。デフォルトの「hierarchical」では Ignition のタグを見つけられません。</td></tr><tr><td>「Fire And Forget」</td><td>オフ</td><td>書き込みエラーはブロックの <code>error_status</code> および <code>message</code> 出力に表示されます。有効にすると、ブロックはバックグラウンドで書き込みを行い、 <code>error_status</code> 常に <code>false</code>.</td></tr></tbody></table>

<figure><img src="https://4077895066-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO0Ceoy9R0lHDZATcPyh%2Fuploads%2Fgit-blob-22cf7e9f9d1816d7c7bba9f934b7a8c0cda6c191%2Fignition-workflow-opc-writer-settings.png?alt=media" alt="The OPC UA Writer Sink settings in the Workflow editor with Url opc.tcp://192.168.1.20:62541, the Ignition tags namespace, Object Name [default]Roboflow, Variable Name Trigger, Value 1, Value Type Boolean, Cooldown Seconds 5, and Additional Properties with User Name roboflow, a password, and Node Lookup Mode direct"><figcaption><p>この例で設定する Writer Sink <code>Trigger</code> タグ。</p></figcaption></figure>

ユーザー名とパスワードは Workflow に保存されるため、Roboflow には必要なタグだけに書き込みできる Ignition ユーザーを割り当ててください。

デバイス上で書き込みエラーを確認するには、ブロックの `error_status` および `message` を Workflow の出力に追加します（例： `opc_error_status` および `opc_message`）。次に「保存」をクリックして Workflow を公開します。

パルスではなく値を書き込むには、「Value」を別のブロックの出力（例：Property Definition ブロックの検出数）に接続し、「Value Type」をタグに合わせます：

<table data-search="false"><thead><tr><th>Ignition タグの型</th><th>「値の型」</th></tr></thead><tbody><tr><td>ブール値</td><td>ブール値</td></tr><tr><td>整数（Int4）</td><td>Int32</td></tr><tr><td>Double（Float8）</td><td>Double</td></tr><tr><td>文字列</td><td>文字列</td></tr></tbody></table>

ブロックの「Integer」値型は「Int64」の別名であるため、Int4 である Ignition の「Integer」タグとは一致しません。「Int32」を使用してください。このブロックは「Float」、「Int16」、「SByte」、「UInt16」、「UInt32」、「UInt64」にも対応しています。

## Workflow をデバイスにデプロイする

デバイス上で Workflow をストリームとして実行します。Deployment Manager でデバイスを開き、「ストリームを追加」をクリックします。「Workflow ID」で Workflow を選択し、すべてのフレームで Workflow が実行されるよう「Mode」を「Continuous」に設定して、カメラの「RTSP URL」を入力します。「ストリームを作成」をクリックします。詳しくは、 [ストリームの追加](https://docs.roboflow.com/deployment/self-hosted/enterprise/deployment-manager/setting-up/add-a-stream).

<figure><img src="https://4077895066-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO0Ceoy9R0lHDZATcPyh%2Fuploads%2Fgit-blob-984d374ce6073fa4a5e55d5aa3b143d738ffa06e%2Fignition-dm-add-stream-continuous.png?alt=media" alt="The Deployment Manager Add New Video Stream dialog with Name line-1-camera, the Workflow selected, Workflow Version Latest (auto-update), Mode Continuous, Stream Type RTSP Stream, and an RTSP URL" width="400"><figcaption><p>連続ストリームでは、すべてのフレームで Workflow が実行されます。</p></figcaption></figure>

OPC UA の設定は Workflow に保存されるため、ストリーム側でほかの設定を行う必要はありません。

## 接続をテストする

ストリームが「Running」と表示されたら、デバイスが書き込みを行います `true` まで `Trigger` 条件を満たすたびに発火しますが、5秒間に1回までです。両方の側で確認してください：

* Ignition では、 `TriggerCount` が増加し、ゲートウェイログに `Roboflow trigger fired`.

<figure><img src="https://4077895066-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoO0Ceoy9R0lHDZATcPyh%2Fuploads%2Fgit-blob-3be63b7b4cb1d061c4389780e21dc7b5e8887e4e%2Fignition-designer-roboflow-tags.png?alt=media" alt="The Ignition Designer Tag Browser with the default provider selected and the Roboflow folder expanded, showing Trigger false and TriggerCount 3"><figcaption><p><code>TriggerCount</code> がデバイスから3回書き込まれた後に表示されます。</p></figcaption></figure>

* Deployment Manager では、デバイスログに `OPC Writer successfully wrote` が書き込みごとに info レベルで表示され、失敗時には `OPC Writer` の警告またはエラーが表示されます。確実な確認方法は `TriggerCount` です。成功メッセージは、デバイスログが info レベルの場合にのみ表示されるためです。

ライン稼働前にテストするには、Workflow を2つ目のストリームとして追加し、「Mode」を「Triggered」に設定します。カメラを条件を満たす対象に向けてから、Deployment Manager でストリームの「Trigger」をクリックします。 `TriggerCount` が1増加します。詳しくは、ストリームを [トリガーする方法](https://docs.roboflow.com/deployment/self-hosted/enterprise/deployment-manager/making-changes/trigger-a-stream).

## トラブルシューティング

<table data-search="false"><thead><tr><th>症状</th><th>原因</th><th>対処方法</th></tr></thead><tbody><tr><td>デバイスログ <code>WRONG NAMESPACE ERROR</code></td><td>Namespace URI がサーバーと一致していないか、Ignition タグが OPC UA 経由で公開されていません</td><td>「Namespace」を確認し、「Expose Tag Providers」を有効にしてください</td></tr><tr><td>デバイスログ <code>WRONG OBJECT OR PROPERTY ERROR</code></td><td>タグパスが間違っている、ルートタグに「Variable Name」が設定されている、または Ignition に対して「Node Lookup Mode」が「hierarchical」に設定されています</td><td>「Object Name」と「Variable Name」を確認し、「Node Lookup Mode」を「direct」に設定してください</td></tr><tr><td>デバイスログ <code>WRONG TYPE ERROR</code></td><td>「Value Type」がタグのデータ型と一致していません</td><td>型を一致させてください（例：Ignition の「Integer」タグには「Int32」）</td></tr><tr><td>デバイスログ <code>Connection is closed</code> Ignition の再起動後に1回だけ発生します</td><td>ブロックでプールされている OPC UA セッションが古くなり、その1回の書き込みが失われます</td><td>修正は不要です。次の書き込み時に再接続されます。</td></tr><tr><td><code>TriggerCount</code> デバイスが書き込みを続けているのに増加しなくなる</td><td><code>Trigger</code> が <code>true</code>のままになっているため、新しい書き込みは値の変更として認識されません</td><td>設定 <code>Trigger</code> を <code>false</code>にし、スクリプト内のリセット行はそのまま残してください</td></tr><tr><td><code>Trigger</code> または <code>Inspect</code> のままになります <code>true</code> トライアルゲートウェイ上で</td><td>Ignitionのトライアル期間が終了しました。OPC UAの書き込みは反映されますが、タグスクリプトは実行されなくなります。</td><td>トライアルをリセットしてから、タグを次の値に戻してください： <code>false</code></td></tr></tbody></table>

## セキュリティ

デバイスとIgnitionゲートウェイは、プラントネットワークまたはOTネットワーク上に置いてください。

* OPC UA Writer SinkとOPC UA Readerは、次のセキュリティポリシーで接続します： `None`。そのため、OPC UAの通信は署名も暗号化もされません。
* IgnitionのOPC UAサーバーを次にバインドすると、 `0.0.0.0` Ignitionホストのすべてのネットワークインターフェイスでポート62541が開きます。
* Roboflow専用のIgnition OPC UAユーザーを作成し、次のフォルダーのみに書き込み権限を付与してください： `Roboflow` 。
