8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

enebularAdvent Calendar 2019

Day 22

enebularとObnizを使ってWebsocketからモータや電磁石を動かしてみる

Last updated at Posted at 2019-12-21

まえがき

Node-REDを使ってObnizからモノを動かす話が少なそうに見えたこともあり、備忘録を兼ねて書いてみる。

やること

Node-REDからObnizを操作して、モータとか電磁石を動かす。

用意するものとか前提条件とか

  • Obniz Board(今は亡きBasic Licenseを使っていますが、他サービスを介せるライセンスならOKなはず)
  • DCモータ(aitendoとかに売っている黄色いギアードモータでもOK)
  • 電磁石(私はAliで仕入れましたが、国内でもルートはありそう。https://ja.aliexpress.com/item/32815809960.html)
  • ジャンパとかブレッドボードとか
  • モバイルバッテリーなどの電源とmicroUSBケーブル
  • enebularのアカウント(enebularの使い方は本家tutorial等でお願いしますm(__)m)

モノに関する準備

写真のようにモータあるいは電磁石とObnizの0,1ピンに差し込むだけでOK
P_20191208_195745_vHDR_On.jpg

モータに関してはバッドノウハウ感がありますがゆるしてください...orz
P_20191208_202406_vHDR_On.jpg

P_20191208_194034_vHDR_On.jpg

P_20191208_202742_vHDR_On (1).jpg

Node-REDに関する話

Obnizノードを取り込んで、フローを"読み込み"→"クリップボード"にて読み込めばOK
ただしObnizIDは各自のものをいれること。

Obnizノードの設定.png

flow.json
[{"id":"633ac2dc.6dd72c","type":"template","z":"9834e454.6fb268","name":"actuator MAX","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 0,\n            \"freq\": 1000,\n             \"pulse\": 1\n        }\n    },{\n        \"io1\": false\n    }\n]","output":"json","x":517,"y":170,"wires":[["b3090c0f.f44ce"]]},{"id":"5f851bd4.5a6bc4","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":319,"y":171,"wires":[["633ac2dc.6dd72c"]]},{"id":"1d6c22d3.82926d","type":"template","z":"9834e454.6fb268","name":"actuator OFF","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 0,\n            \"freq\": 1000,\n             \"pulse\": 0\n        }\n    },{\n        \"io1\": false\n    }\n]","output":"json","x":509,"y":288,"wires":[["b3090c0f.f44ce"]]},{"id":"8cf73cc4.ddb89","type":"template","z":"9834e454.6fb268","name":"actuator half","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 0,\n            \"freq\": 1000,\n             \"pulse\": 0.5\n        }\n    },{\n        \"io1\": false\n    }\n]","output":"json","x":516,"y":229,"wires":[["b3090c0f.f44ce"]]},{"id":"d90360ef.25f91","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":318,"y":230,"wires":[["8cf73cc4.ddb89"]]},{"id":"f4a79ca.ca2e86","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":314,"y":289,"wires":[["1d6c22d3.82926d"]]},{"id":"14e282fd.4301dd","type":"template","z":"9834e454.6fb268","name":"actuator MAX(rev)","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 1,\n            \"freq\": 1000,\n             \"pulse\": 1\n        }\n    },{\n        \"io0\": false\n    }\n]","output":"json","x":518,"y":399,"wires":[["b3090c0f.f44ce"]]},{"id":"efc11452.8914b8","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":400,"wires":[["14e282fd.4301dd"]]},{"id":"afdc712d.3af71","type":"template","z":"9834e454.6fb268","name":"actuatorhalf(rev)","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 1,\n            \"freq\": 1000,\n             \"pulse\": 0.5\n        }\n    },{\n        \"io0\": false\n    }\n]","output":"json","x":517,"y":458,"wires":[["b3090c0f.f44ce"]]},{"id":"11e0a0d1.9819ff","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":309,"y":459,"wires":[["afdc712d.3af71"]]},{"id":"b76c0cf7.83d65","type":"template","z":"9834e454.6fb268","name":"reset","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n\n    {\n        \"system\": {\n            \"reset\": true\n        }\n    },{\n        \"display\": {\n            \"clear\": true\n        }\n    }\n\n]","output":"json","x":510,"y":100,"wires":[["b3090c0f.f44ce"]]},{"id":"d754e73d.101408","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":342,"y":95,"wires":[["b76c0cf7.83d65"]]},{"id":"b3090c0f.f44ce","type":"obniz","z":"9834e454.6fb268","obnizId":"XXXX-XXXX","x":839,"y":173,"wires":[]}]

読み込むとこんな感じ。actuator MAXで出力最大でhalfで1/2の出力、OFFで停止する。
あと、正方向から逆方向に出力する際にピンのアサインをかえるため、リセットボタンを用意した。
フロー.png

フローの中身を見ると、PWMで出力変えているだけだったりします。

actuator_max
[
    {
        "pwm0": {
            "io": 0,
            "freq": 1000,
             "pulse": 1
        }
    },{
        "io1": false
    }
]

リセットはこんな感じ。displayの初期化はピンアサイン表示が切り替わる対策として
入れてるだけ。

reset
[

    {
        "system": {
            "reset": true
        }
    },{
        "display": {
            "clear": true
        }
    }

]

動かしてみる。

enebular上でデプロイしたのちに、actuator maxにつながったtimestanpからinjectをすると、モータが回ったり電磁石に金属がくっつくはず。そのあとにreset側でinjectを流すとモータが止まったり、電磁石からくっついていたものが落ちたりする。
ちなみに、reset側でinjectを実施した後に(rev)を付与したノードにinjectを流すと逆方向に動く。

P_20191208_195554_vHDR_On.jpg

あとがき

センサの値をフィードバックして何かしてみたいなぁ

8
2
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
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?