まえがき
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
モータに関してはバッドノウハウ感がありますがゆるしてください...orz
Node-REDに関する話
Obnizノードを取り込んで、フローを"読み込み"→"クリップボード"にて読み込めばOK
ただしObnizIDは各自のものをいれること。
[{"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で停止する。
あと、正方向から逆方向に出力する際にピンのアサインをかえるため、リセットボタンを用意した。
フローの中身を見ると、PWMで出力変えているだけだったりします。
[
{
"pwm0": {
"io": 0,
"freq": 1000,
"pulse": 1
}
},{
"io1": false
}
]
リセットはこんな感じ。displayの初期化はピンアサイン表示が切り替わる対策として
入れてるだけ。
[
{
"system": {
"reset": true
}
},{
"display": {
"clear": true
}
}
]
動かしてみる。
enebular上でデプロイしたのちに、actuator maxにつながったtimestanpからinjectをすると、モータが回ったり電磁石に金属がくっつくはず。そのあとにreset側でinjectを流すとモータが止まったり、電磁石からくっついていたものが落ちたりする。
ちなみに、reset側でinjectを実施した後に(rev)を付与したノードにinjectを流すと逆方向に動く。
あとがき
センサの値をフィードバックして何かしてみたいなぁ