#52 4ch絶縁アナログ入力(16bitADC)
Tibbitの配置
Tibbit #52を挿入します。
ノードの配置
ch指定のために、functionノードを使用します。
以下のようにフローを配置します。
functionノードの編集を開き、コードに以下を記述します。
値を取得したいchを指定します。
1 2 |
msg.payload = [{ch: 1}]; return msg; |
chを複数取りたい場合は、以下のように指定ができます。
1 2 |
msg.payload = [{ch: 1},{ch: 2},{ch: 3},{ch: 4}]; return msg; |
ノードの設定を開き、スロットを選択します。
Node-REDデプロイ後、injectノードをクリックすると電圧16bit値と補正値を取得ですることができます。
Outputsは指定したch分、配列で取得できます。
サンプルフロー
1 |
[{"id":"f811ef22.59e6c","type":"Tibbit-#52","z":"3c31dc44.0e66fc","name":"","tpSlot":"S01","communication":"TP52","connectedStatus":"start","host":"","x":400,"y":1720,"wires":[["35230447.a9ea54"]]},{"id":"265a9a33.095086","type":"inject","z":"3c31dc44.0e66fc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":1680,"wires":[["6ec78b3.5f7ac74"]]},{"id":"35230447.a9ea54","type":"debug","z":"3c31dc44.0e66fc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":1760,"wires":[]},{"id":"6ec78b3.5f7ac74","type":"function","z":"3c31dc44.0e66fc","name":"ch指定","func":"msg.payload = [{ch: 1}];\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":1680,"wires":[["f811ef22.59e6c"]]}] |
電圧値(V)に変換
取得した値と補正値をもとに、電圧値(V)に変換する方法です。
先程のフローに「電圧値変換」というfunctionノードを追加します。
functionノードの編集を開き、コードに以下を記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// ch1 var raw = msg.payload[0][0]; var corr = msg.payload[0][1]; var sign = 0; if (raw <= 0x7FFF) { sign = 1 } else { sign = -1 raw = 0xFFFF - raw + 1 } var volt = raw * ( corr / 1000000 + 0.00030517578125 ); if (sign < 0) { volt = -volt; } msg.payload = volt; return msg; |
Node-REDデプロイ後、injectノードをクリックすると電圧値(V)を取得することができます。
サンプルフロー
1 |
[{"id":"c7787819.deeb38","type":"Tibbit-#52","z":"d0a8e665.0b3038","name":"","tpSlot":"S01","communication":"TP52","connectedStatus":"start","host":"","x":960,"y":1880,"wires":[["caa9b7d.2f557c8"]]},{"id":"80f68bd8.d27bc","type":"inject","z":"d0a8e665.0b3038","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":780,"y":1840,"wires":[["77e0ad5.190dfd4"]]},{"id":"77e0ad5.190dfd4","type":"function","z":"d0a8e665.0b3038","name":"ch指定","func":"msg.payload = [{ch: 1}];\nreturn msg;","outputs":1,"noerr":0,"x":920,"y":1840,"wires":[["c7787819.deeb38"]]},{"id":"caa9b7d.2f557c8","type":"function","z":"d0a8e665.0b3038","name":"電圧値変換","func":"// ch1\nvar raw = msg.payload[0][0];\nvar corr = msg.payload[0][1];\nvar sign = 0;\n\nif (raw <= 0x7FFF) {\n sign = 1\n} else {\n sign = -1\n raw = 0xFFFF - raw + 1\n}\n\nvar volt = raw * ( corr / 1000000 + 0.00030517578125 );\nif (sign < 0) { \n volt = -volt;\n}\nmsg.payload = volt;\nreturn msg;\n","outputs":1,"noerr":0,"x":1050,"y":1920,"wires":[["e5858072.894ba8"]]},{"id":"e5858072.894ba8","type":"debug","z":"d0a8e665.0b3038","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1110,"y":1960,"wires":[]}] |