リモコンバギーカーに後進機能を付与【電子工作始めますか?】

タイトル

スマートフォンでコントロールできるバギーカーにHブリッジ回路を載せてモータを逆回転させバックできるようにします。HブリッジにはTB6612FNGのモータドライバモジュールを使いました。

Hブリッジ

ブラシ付きモータは電流を流す向きを変えれば回転する方向を変えられます。これを実現するにはHブリッジという回路が使われます。
Hブリッジの説明は他のサイト様がたくさんしているでしょうから省きます。
自分ですべて作るには色々面倒な所があります。電源をショートさせてしまう可能性があり、それを避けるには回路の規模が大きくなってしまいます。専用のICがありますのでそれを使った方が良いでしょう。

HブリッジにはTB6612FNGのモータドライバモジュールを使います。

モータコントロールIC TB6612FNG

TB6612FNG | 東芝デバイス&ストレージ(㈱)
ブラシ付きモータードライバーIC
ブラシ付きモータ2つをコントロールできるICです。1Aくらいまで電流を流せるので模型に最適です。
モータの回転とブレーキを制御するのに2本の信号線を使います。速度制御にPWM信号を加えられます。少なくとも3本の制御線でモータを1つコントロールできます。

モータードライバモジュール

TB6612FNGはSSOP24パッケージなので2.54mmピッチの万能基板にハンダ付けできません。ブレークアウト基板を使う所ですが、モジュールになったものが売られています。

回路図

回路図
モータドライバとRaspberry Piとを接続する回路図です。前回のFET回路に比べ配線が多くなります。
コネクタJ3はモーターとモーター電源のコネクタです。
1pin モータへ
2pin モータへ
3pin +V モータ電源
4pin GND

コネクタJ2はサーボモータのコネクタです。
サーボのケーブル番号に統一的な仕様は無いようです。番号が逆の場合もあります。手元のサーボモータに合わせてください。

この回路はこんな基板になります。
基板イメージ

表と裏の手配線はこんな経路をイメージしてはんだ付けしましょう。
基板表パターン
基板表パターン
基板裏パターン
基板裏パターン

はんだ付けした現実はこう
実際の基板

前回の回路ではモータにダイオードを付けていました。そのダイオードは取り外します。ダイオードが付いていると後進の時にショートしてしまいます。ダイオードの代わりに0.1μFほどのセラミックコンデンサを付けるとノイズ対策に良いでしょう。

ソフトウエア

ソフトウエア
Node-REDのプログラムを変更します。
前進後進を指定するGPIOポート2つを追加して、前後に合わせた信号を出します。ブレーキ用の信号制御も追加しました。
アクセルコントロールは-100~100の数値を使い、プラスを前進、マイナスを後進としました。
Raspberry Pi Zero用のバッテリー PiSugar2の残容量を取得してMQTTで送信するようにしました。

コードはここです。
SignalFlagZ/flow.json | GitHub Gist
タミヤバギーカーコントロール用フロー
[
{
"id": "a7ce1198.3edae",
"type": "tab",
"label": "フロー 1",
"disabled": false,
"info": ""
},
{
"id": "99f43180.8923f",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "100",
"payloadType": "num",
"x": 190,
"y": 60,
"wires": [
[
"60c1e84f.d152d8"
]
]
},
{
"id": "12786a59.723276",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 190,
"y": 100,
"wires": [
[
"60c1e84f.d152d8"
]
]
},
{
"id": "d6404cbb.c264f",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "-50",
"payloadType": "num",
"x": 190,
"y": 140,
"wires": [
[
"60c1e84f.d152d8"
]
]
},
{
"id": "d5df3823.f91a98",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "-1",
"payloadType": "num",
"x": 190,
"y": 200,
"wires": [
[
"61d891ff.82ef9"
]
]
},
{
"id": "47ed2489.9cb6ac",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 190,
"y": 240,
"wires": [
[
"61d891ff.82ef9"
]
]
},
{
"id": "f5b634cf.130c98",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "1",
"payloadType": "num",
"x": 190,
"y": 280,
"wires": [
[
"61d891ff.82ef9"
]
]
},
{
"id": "1ec0d37c.061d8d",
"type": "mqtt in",
"z": "a7ce1198.3edae",
"name": "",
"topic": "pwm1",
"qos": "2",
"datatype": "auto",
"broker": "3883e1.b8ed0c2",
"x": 530,
"y": 240,
"wires": [
[
"93fe60ff.30056"
]
]
},
{
"id": "61d891ff.82ef9",
"type": "mqtt out",
"z": "a7ce1198.3edae",
"name": "",
"topic": "pwm1",
"qos": "",
"retain": "",
"broker": "3883e1.b8ed0c2",
"x": 330,
"y": 240,
"wires": []
},
{
"id": "60c1e84f.d152d8",
"type": "mqtt out",
"z": "a7ce1198.3edae",
"name": "",
"topic": "pwm0",
"qos": "",
"retain": "",
"broker": "3883e1.b8ed0c2",
"x": 330,
"y": 100,
"wires": []
},
{
"id": "372856b9.ce616a",
"type": "mqtt in",
"z": "a7ce1198.3edae",
"name": "",
"topic": "pwm0",
"qos": "2",
"datatype": "auto",
"broker": "3883e1.b8ed0c2",
"x": 530,
"y": 100,
"wires": [
[
"961bdabc.1a34e8",
"436b233a.40948c"
]
]
},
{
"id": "93fe60ff.30056",
"type": "range",
"z": "a7ce1198.3edae",
"minin": "-1",
"maxin": "1",
"minout": "100",
"maxout": "0",
"action": "clamp",
"round": false,
"property": "payload",
"name": "",
"x": 700,
"y": 240,
"wires": [
[
"5a6152ba.a6bd9c"
]
]
},
{
"id": "e962e45e.ce3768",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "1",
"payloadType": "num",
"x": 190,
"y": 340,
"wires": [
[
"5cc29722.d57f18"
]
]
},
{
"id": "d193ba10.53c608",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 190,
"y": 380,
"wires": [
[
"5cc29722.d57f18"
]
]
},
{
"id": "5cc29722.d57f18",
"type": "mqtt out",
"z": "a7ce1198.3edae",
"name": "",
"topic": "DR",
"qos": "",
"retain": "",
"broker": "3883e1.b8ed0c2",
"x": 330,
"y": 380,
"wires": []
},
{
"id": "da48ffcf.d35ea",
"type": "mqtt in",
"z": "a7ce1198.3edae",
"name": "",
"topic": "DR",
"qos": "2",
"datatype": "auto",
"broker": "3883e1.b8ed0c2",
"x": 530,
"y": 380,
"wires": [
[
"cca3cbcb.f7fe38"
]
]
},
{
"id": "28933afe.a14216",
"type": "mqtt out",
"z": "a7ce1198.3edae",
"name": "",
"topic": "brake",
"qos": "",
"retain": "",
"broker": "3883e1.b8ed0c2",
"x": 330,
"y": 500,
"wires": []
},
{
"id": "1a829150.9bed8f",
"type": "mqtt in",
"z": "a7ce1198.3edae",
"name": "",
"topic": "brake",
"qos": "2",
"datatype": "auto",
"broker": "3883e1.b8ed0c2",
"x": 530,
"y": 500,
"wires": [
[
"2b3863c5.3e93bc"
]
]
},
{
"id": "cca3cbcb.f7fe38",
"type": "function",
"z": "a7ce1198.3edae",
"name": "前後",
"func": "var msg1 = { payload: 0};\nvar msg2 = { payload: 0};\n\nif (msg.payload >= \"1\") {\n msg1.payload = 1;\n msg2.payload = 0;\n}\nif (msg.payload == \"0\") {\n msg1.payload = 0;\n msg2.payload = 1;\n}\nif (msg.payload == \"-1\") {\n msg1.payload = 0;\n msg2.payload = 0;\n}\n\nreturn [msg1,msg2];",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 650,
"y": 380,
"wires": [
[
"2bfe5b1.2090ea4"
],
[
"45428a05.3a90b4"
]
]
},
{
"id": "2b3863c5.3e93bc",
"type": "function",
"z": "a7ce1198.3edae",
"name": "ブレーキ",
"func": "var msg1 = { payload: 0};\nvar msg2 = { payload: 0};\nvar msg3 = { payload: 0};\n\nif (msg.payload == \"1\") {\n msg1.payload = 1;\n msg2.payload = 1;\n} else {\n msg1.paylaod = 0;\n msg2.paylaod = 0;\n}\n\nreturn [msg1, msg2, msg3];",
"outputs": 3,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 660,
"y": 500,
"wires": [
[
"2bfe5b1.2090ea4"
],
[
"45428a05.3a90b4"
],
[]
]
},
{
"id": "91a88392.95e38",
"type": "comment",
"z": "a7ce1198.3edae",
"name": "Accele",
"info": "",
"x": 530,
"y": 60,
"wires": []
},
{
"id": "d7e6aee6.50c76",
"type": "comment",
"z": "a7ce1198.3edae",
"name": "Steering",
"info": "",
"x": 540,
"y": 200,
"wires": []
},
{
"id": "ad0f2ef9.daa96",
"type": "comment",
"z": "a7ce1198.3edae",
"name": "Drive/Reverse",
"info": "",
"x": 550,
"y": 340,
"wires": []
},
{
"id": "39785beb.984db4",
"type": "comment",
"z": "a7ce1198.3edae",
"name": "Brake",
"info": "",
"x": 530,
"y": 460,
"wires": []
},
{
"id": "b595b0eb.63284",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "1",
"payloadType": "num",
"x": 190,
"y": 480,
"wires": [
[
"28933afe.a14216"
]
]
},
{
"id": "e5bc9f7.8b60b6",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 190,
"y": 520,
"wires": [
[
"28933afe.a14216"
]
]
},
{
"id": "961bdabc.1a34e8",
"type": "function",
"z": "a7ce1198.3edae",
"name": "前後判定",
"func": "var ret={ payload: \"-1\"};\nif (msg.payload >= 10) {\n ret.payload = 1;\n}\nif (msg.payload <= -10) {\n ret.payload = 0;\n}\nreturn ret;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 680,
"y": 140,
"wires": [
[
"1d154c3b.8b00b4"
]
]
},
{
"id": "1d154c3b.8b00b4",
"type": "mqtt out",
"z": "a7ce1198.3edae",
"name": "",
"topic": "DR",
"qos": "",
"retain": "",
"broker": "3883e1.b8ed0c2",
"x": 870,
"y": 140,
"wires": []
},
{
"id": "436b233a.40948c",
"type": "function",
"z": "a7ce1198.3edae",
"name": "絶対値とゼロ",
"func": "msg.payload = Math.abs(msg.payload);\nif (msg.payload < 10) {\n msg.payload = 0 ;\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 700,
"y": 100,
"wires": [
[
"f2d7ea81.266fb8"
]
]
},
{
"id": "f2d7ea81.266fb8",
"type": "pi-gpiod out",
"z": "a7ce1198.3edae",
"name": "",
"host": "localhost",
"port": 8888,
"pin": "17",
"set": "",
"level": "0",
"out": "pwm",
"sermin": "1000",
"sermax": "2000",
"freq": "80",
"x": 880,
"y": 100,
"wires": []
},
{
"id": "5a6152ba.a6bd9c",
"type": "pi-gpiod out",
"z": "a7ce1198.3edae",
"name": "",
"host": "localhost",
"port": 8888,
"pin": "18",
"set": "",
"level": "0",
"out": "ser",
"sermin": "1000",
"sermax": "2000",
"freq": "800",
"x": 880,
"y": 240,
"wires": []
},
{
"id": "45428a05.3a90b4",
"type": "pi-gpiod out",
"z": "a7ce1198.3edae",
"name": "",
"host": "localhost",
"port": 8888,
"pin": "27",
"set": true,
"level": "0",
"out": "out",
"sermin": "1000",
"sermax": "2000",
"freq": "80",
"x": 880,
"y": 500,
"wires": []
},
{
"id": "2bfe5b1.2090ea4",
"type": "pi-gpiod out",
"z": "a7ce1198.3edae",
"name": "",
"host": "localhost",
"port": 8888,
"pin": "22",
"set": true,
"level": "1",
"out": "out",
"sermin": "1000",
"sermax": "2000",
"freq": "80",
"x": 880,
"y": 380,
"wires": []
},
{
"id": "dfba26d9.6b34e8",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "-1",
"payloadType": "num",
"x": 190,
"y": 420,
"wires": [
[
"5cc29722.d57f18"
]
]
},
{
"id": "2d8a1360.e719bc",
"type": "mqtt out",
"z": "a7ce1198.3edae",
"name": "",
"topic": "bat",
"qos": "",
"retain": "",
"broker": "3883e1.b8ed0c2",
"x": 330,
"y": 620,
"wires": []
},
{
"id": "232a7f65.6fc6b",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "100",
"payloadType": "num",
"x": 190,
"y": 600,
"wires": [
[
"2d8a1360.e719bc"
]
]
},
{
"id": "61b49ade.c82e64",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "0",
"payloadType": "num",
"x": 190,
"y": 640,
"wires": [
[
"2d8a1360.e719bc"
]
]
},
{
"id": "a8bc0258.81f72",
"type": "inject",
"z": "a7ce1198.3edae",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "5",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "get battery",
"payloadType": "str",
"x": 170,
"y": 700,
"wires": [
[
"57068f79.32c8f"
]
]
},
{
"id": "ac8cee78.1aa93",
"type": "tcp in",
"z": "a7ce1198.3edae",
"name": "",
"server": "client",
"host": "127.0.0.1",
"port": "8423",
"datamode": "stream",
"datatype": "utf8",
"newline": "",
"topic": "",
"base64": false,
"x": 570,
"y": 700,
"wires": [
[
"1025feef.14d781"
]
]
},
{
"id": "57068f79.32c8f",
"type": "tcp out",
"z": "a7ce1198.3edae",
"host": "127.0.0.1",
"port": "8423",
"beserver": "reply",
"base64": false,
"end": false,
"name": "",
"x": 370,
"y": 700,
"wires": []
},
{
"id": "1025feef.14d781",
"type": "change",
"z": "a7ce1198.3edae",
"name": "",
"rules": [
{
"t": "change",
"p": "payload",
"pt": "msg",
"from": "battery: ",
"fromt": "str",
"to": "",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 800,
"y": 700,
"wires": [
[
"57f06208.fb83fc"
]
]
},
{
"id": "57f06208.fb83fc",
"type": "mqtt out",
"z": "a7ce1198.3edae",
"name": "",
"topic": "bat",
"qos": "",
"retain": "",
"broker": "3883e1.b8ed0c2",
"x": 990,
"y": 700,
"wires": []
},
{
"id": "35528f9a.cbaad",
"type": "comment",
"z": "a7ce1198.3edae",
"name": "Battery",
"info": "",
"x": 530,
"y": 660,
"wires": []
},
{
"id": "3883e1.b8ed0c2",
"type": "mqtt-broker",
"name": "",
"broker": "localhost",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": false,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
}
]
view raw flow.json hosted with ❤ by GitHub

ビデオ


コメント

最近のコメント

Threaded Recent Comments will be here.