サンプルフロー 差動2輪

KeiganMotorを2つ組み合わせて差動2輪ロボット として動作させる場合のサンプルフローです。
KeiganMotor (KM-1) ドリーキットを操作するのに適しています。
以下のフローをNode-REDのクリップボードにペーストして使用して下さい。

[{"id":"7affae99.0d185","type":"tab","label":"2-Wheeled Car","disabled":false,"info":""},{"id":"111cb572.6cc96b","type":"comment","z":"7affae99.0d185","name":"接続","info":"","x":103,"y":44,"wires":[]},{"id":"21225e2e.32b672","type":"km-motor","z":"7affae99.0d185","name":"","allowConnectMotor":"KM-1 D5EP","x":853,"y":324,"wires":[["aa917535.b47b78","ad8654f3.6262c8"],[],[],[]]},{"id":"aa917535.b47b78","type":"debug","z":"7affae99.0d185","name":"status","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1129,"y":312,"wires":[]},{"id":"c7371099.41c5d","type":"km-BLEscan","z":"7affae99.0d185","name":"BLE scanner","x":443,"y":104,"wires":[["21225e2e.32b672","3d2c566d.7d385a","aae6bba.1fad148"]]},{"id":"1336dafe.0ddc85","type":"trigger","z":"7affae99.0d185","op1":"{\"cmd\":\"cmdEnable\"}","op2":"0","op1type":"json","op2type":"str","duration":"100","extend":false,"units":"ms","reset":"","bytopic":"topic","name":"{\"cmd\":\"cmdEnable\"}","x":993,"y":224,"wires":[["21225e2e.32b672"]]},{"id":"ad8654f3.6262c8","type":"switch","z":"7affae99.0d185","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"connect","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":803,"y":224,"wires":[["1336dafe.0ddc85"]]},{"id":"7bbd798a.9f9468","type":"comment","z":"7affae99.0d185","name":"※安全装置","info":"※注意 別のノードで既に接続されているモーターは接続されません。一旦接続を切って下さい\n\n","x":813,"y":184,"wires":[]},{"id":"aae6bba.1fad148","type":"km-motor","z":"7affae99.0d185","name":"","allowConnectMotor":"KM-1 MCKI","x":880,"y":520,"wires":[["626958cb.dd8f88","5f0bfe61.15afa"],[],[],[]]},{"id":"d58ccf9.8a73b3","type":"trigger","z":"7affae99.0d185","op1":"{\"cmd\":\"cmdEnable\"}","op2":"0","op1type":"json","op2type":"str","duration":"10","extend":false,"units":"ms","reset":"","bytopic":"topic","name":"{\"cmd\":\"cmdEnable\"}","x":993,"y":424,"wires":[["aae6bba.1fad148"]]},{"id":"626958cb.dd8f88","type":"switch","z":"7affae99.0d185","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"connect","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":803,"y":424,"wires":[["d58ccf9.8a73b3"]]},{"id":"5f0bfe61.15afa","type":"debug","z":"7affae99.0d185","name":"status","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1108,"y":527,"wires":[]},{"id":"b7b0e515.ed0948","type":"comment","z":"7affae99.0d185","name":"※安全装置","info":"※注意 別のノードで既に接続されているモーターは接続されません。一旦接続を切って下さい\n\n","x":813,"y":384,"wires":[]},{"id":"efa0b20e.48196","type":"ui_slider","z":"7affae99.0d185","name":"maxSpeed","label":"SPEED","tooltip":"","group":"8f350e48.d2eda","order":1,"width":"6","height":"1","passthru":false,"outs":"all","topic":"default","min":"10","max":"50","step":1,"x":143,"y":444,"wires":[["a9401f69.73b07"]]},{"id":"ede1e76b.f55698","type":"comment","z":"7affae99.0d185","name":"UI入力","info":"","x":103,"y":384,"wires":[]},{"id":"2f28765b.a31bca","type":"ui_button","z":"7affae99.0d185","name":"F","group":"8f350e48.d2eda","order":3,"width":"6","height":"2","passthru":false,"label":"↑","tooltip":"","color":"","bgcolor":"","icon":"","payload":"F","payloadType":"str","topic":"","x":209,"y":600,"wires":[["22536473.646fcc"]]},{"id":"7bb41773.801a38","type":"ui_button","z":"7affae99.0d185","name":"B","group":"8f350e48.d2eda","order":6,"width":"6","height":"2","passthru":false,"label":"↓","tooltip":"","color":"","bgcolor":"","icon":"","payload":"B","payloadType":"str","topic":"","x":209,"y":680,"wires":[["22536473.646fcc"]]},{"id":"7f312a7.d6c09d4","type":"ui_button","z":"7affae99.0d185","name":"L","group":"8f350e48.d2eda","order":4,"width":"3","height":"2","passthru":false,"label":"←Left","tooltip":"","color":"","bgcolor":"","icon":"","payload":"L","payloadType":"str","topic":"0","x":129,"y":640,"wires":[["22536473.646fcc"]]},{"id":"1ce31ac2.2b5f45","type":"ui_button","z":"7affae99.0d185","name":"R","group":"8f350e48.d2eda","order":5,"width":"3","height":"2","passthru":false,"label":"Right→","tooltip":"","color":"","bgcolor":"","icon":"","payload":"R","payloadType":"str","topic":"","x":269,"y":640,"wires":[["22536473.646fcc"]]},{"id":"22536473.646fcc","type":"function","z":"7affae99.0d185","name":"Diff two wheel Robo","func":" let arrowCmd = msg.payload ;\n \n let maxSpeed=context.get('maxSpeed')||20;\n let moveTime=context.get('moveTime')||1;\n let motorLeftRevers=true;\n let motorRightRevers=false;\n\nlet ans_x=0,ans_y=0;\nswitch(arrowCmd){\n case 'F':\n ans_x=1;ans_y=1;\n break;\n \n case 'R':\n ans_x=-0.5;ans_y=0.5;\n break;\n \n case 'L':\n ans_x=0.5;ans_y=-0.5;\n break;\n \n case 'B':\n ans_x=-1;ans_y=-1;\n break;\n \n case \"STOP\":\n return [{\"payload\":{\"cmd\":\"cmdFree\"}}, {\"payload\":{\"cmd\":\"cmdFree\"}} ];\n break;\n \n default:\n if( typeof arrowCmd===\"object\" && arrowCmd[\"maxSpeed\"]!==undefined){\n \n maxSpeed= parseInt(arrowCmd[\"maxSpeed\"]);\n context.set('maxSpeed',maxSpeed);\n }else if( typeof arrowCmd===\"object\" && arrowCmd[\"moveTime\"]!==undefined){\n moveTime= parseFloat(arrowCmd[\"moveTime\"]);\n context.set('moveTime',moveTime);\n }\n}\n\nconsole.log(\"maxSpeed\",maxSpeed,\"moveTime\",moveTime);\n \n\nlet lr=motorLeftRevers?ans_x*maxSpeed*-1:ans_x*maxSpeed;\nlet rr=motorRightRevers?ans_y*maxSpeed*-1:ans_y*maxSpeed;\n\n//速度 rpm ->radian/sec(Math.PI*2/60)\n\nlet leftDistance=lr * 0.10471975511965977*moveTime;\nlet rightDistance=rr * 0.10471975511965977*moveTime;\nlet leftSpeed=Math.abs(lr) * 0.10471975511965977;\nlet rightSpeed=Math.abs(rr) * 0.10471975511965977;\n\n\nreturn [{\"payload\":{\"cmd\":\"cmdMoveByDistance\",\"arg\":[leftDistance,leftSpeed]}},{\"payload\":{\"cmd\":\"cmdMoveByDistance\",\"arg\":[rightDistance,rightSpeed]}}];\n \n \n ","outputs":2,"noerr":0,"x":420,"y":360,"wires":[["21225e2e.32b672"],["aae6bba.1fad148"]],"inputLabels":["{}"],"outputLabels":["{}","{}"]},{"id":"b3eb751b.023e78","type":"ui_button","z":"7affae99.0d185","name":"STOP","group":"8f350e48.d2eda","order":7,"width":"6","height":"2","passthru":false,"label":"STOP","tooltip":"","color":"","bgcolor":"","icon":"","payload":"STOP","payloadType":"str","topic":"","x":210,"y":720,"wires":[["22536473.646fcc"]]},{"id":"a1446462.573788","type":"inject","z":"7affae99.0d185","name":"Scan 20sec 10Motor","topic":"","payload":"20,10","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":175,"y":96,"wires":[["c7371099.41c5d"]]},{"id":"44f13cad.775814","type":"inject","z":"7affae99.0d185","name":"Scan Stop","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":143,"y":143,"wires":[["c7371099.41c5d"]]},{"id":"593f5579.bbbf9c","type":"inject","z":"7affae99.0d185","name":"","topic":"","payload":"{\"cmd\":\"disConnect\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":200,"wires":[["21225e2e.32b672","aae6bba.1fad148"]]},{"id":"a9401f69.73b07","type":"function","z":"7affae99.0d185","name":"{\"speed\":msg.payload}","func":"\nreturn {\"payload\":{\"maxSpeed\":msg.payload}};","outputs":1,"noerr":0,"x":370,"y":440,"wires":[["22536473.646fcc"]]},{"id":"2537f7ab.6c5358","type":"ui_slider","z":"7affae99.0d185","name":"moveTime","label":"moveTime","tooltip":"","group":"8f350e48.d2eda","order":2,"width":"6","height":"1","passthru":false,"outs":"all","topic":"default","min":"0.2","max":"1.5","step":"0.2","x":132,"y":514,"wires":[["5de6d1a3.c517d"]]},{"id":"5de6d1a3.c517d","type":"function","z":"7affae99.0d185","name":"{\"moveTime\":msg.payload}","func":"\nreturn {\"payload\":{\"moveTime\":msg.payload}};","outputs":1,"noerr":0,"x":372,"y":507,"wires":[["22536473.646fcc"]]},{"id":"ef8d672e.c96d48","type":"inject","z":"7affae99.0d185","name":"","topic":"","payload":"{\"cmd\":\"cmdEnable\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":260,"wires":[["21225e2e.32b672","aae6bba.1fad148"]]},{"id":"38894487.a3d81c","type":"comment","z":"7affae99.0d185","name":"ティーチング","info":"","x":110,"y":800,"wires":[]},{"id":"ef03c3df.57bcb","type":"ui_button","z":"7affae99.0d185","name":"1 REC","group":"8f350e48.d2eda","order":9,"width":"2","height":"1","passthru":false,"label":"1 REC","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"cmd\":\"cmdStartTeachingMotion\",\"arg\":[0,10000]}","payloadType":"json","topic":"","x":90,"y":880,"wires":[["21225e2e.32b672","aae6bba.1fad148"]]},{"id":"b8e5ee0c.cb1cd","type":"ui_button","z":"7affae99.0d185","name":"1 PLAY","group":"8f350e48.d2eda","order":10,"width":"2","height":"1","passthru":false,"label":"1 PLAY","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"cmd\":\"cmdStartPlaybackMotion\",\"arg\":[0,1,1]}","payloadType":"json","topic":"","x":100,"y":920,"wires":[["21225e2e.32b672","aae6bba.1fad148"]]},{"id":"3d2c566d.7d385a","type":"debug","z":"7affae99.0d185","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":840,"y":100,"wires":[]},{"id":"336d5bb5.f772c4","type":"ui_button","z":"7affae99.0d185","name":"2 REC","group":"8f350e48.d2eda","order":12,"width":"2","height":"1","passthru":false,"label":"2 REC","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"cmd\":\"cmdStartTeachingMotion\",\"arg\":[1,10000]}","payloadType":"json","topic":"","x":270,"y":880,"wires":[["aae6bba.1fad148","21225e2e.32b672"]]},{"id":"ff10712e.03655","type":"ui_button","z":"7affae99.0d185","name":"2 PLAY","group":"8f350e48.d2eda","order":13,"width":"2","height":"1","passthru":false,"label":"2 PLAY","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"cmd\":\"cmdStartPlaybackMotion\",\"arg\":[1,1,1]}","payloadType":"json","topic":"","x":260,"y":920,"wires":[["aae6bba.1fad148","21225e2e.32b672"]]},{"id":"83a3cc8e.b15b3","type":"ui_button","z":"7affae99.0d185","name":"1 Erase","group":"8f350e48.d2eda","order":8,"width":"2","height":"1","passthru":false,"label":"1 Erase","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"cmd\":\"cmdEraseMotion\",\"arg\":[0]}","payloadType":"json","topic":"","x":100,"y":840,"wires":[["21225e2e.32b672","aae6bba.1fad148"]]},{"id":"deadd703.af8228","type":"ui_button","z":"7affae99.0d185","name":"2 Erase","group":"8f350e48.d2eda","order":11,"width":"2","height":"1","passthru":false,"label":"2 Erase","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"cmd\":\"cmdEraseMotion\",\"arg\":[1]}","payloadType":"json","topic":"","x":260,"y":840,"wires":[["aae6bba.1fad148","21225e2e.32b672"]]},{"id":"8f350e48.d2eda","type":"ui_group","z":"","name":"コントローラー","tab":"26971cf1.8d1c64","disp":false,"width":"6","collapse":false},{"id":"26971cf1.8d1c64","type":"ui_tab","z":"","name":"ホーム","icon":"dashboard"}]

操作UI