送信機、受信機ともに Raspberry Pi Pico Wを使用したラジコンの作り方です。
下記の方法はMACアドレスは使わずにUUUIDで接続するサンプルファイルです。
ファイル内にはサンプルのUUIDが記載されているので、適宜変更してください。
ファイル内にはサンプルのUUIDが記載されているので、適宜変更してください。
●参考サイト
●bluetoothサンプルファイルのダウンロード先
●UUID参考サイト(ものものテック)
製作手順
①送信機側PicoW(セントラル)
に下記3つのファイルをインストールします。
・ble_advertising.py(GitHubよりダウンロード)
・ble_simple_central.py(GitHubよりダウンロード)
・main.py (引用:https://saitodev.co/microbit/rpi/article/72)
import bluetooth
import time
from ble_simple_central import BLESimpleCentral
ble = bluetooth.BLE()
central = BLESimpleCentral(ble)
not_found = False
def on_scan(addr_type, addr, name):
if addr_type is not None:
print("Found peripheral:", addr_type, addr, name)
central.connect()
else:
not_found = True
print("No peripheral found.")
central.scan(callback=on_scan)
# 今回はon_rxは不要
#def on_rx(v):
# do nothing
#central.on_notify(on_rx)
is_connect = True
while not central.is_connected():
time.sleep_ms(100)
if not_found:
is_connect = False
break
if is_connect:
print("Connected")
with_response = False
tx = 0
while central.is_connected():
if tx == 0:
tx = 1
print("ON")
else:
tx = 0
print("OFF")
try:
central.write(str(tx)+"\r\n", with_response)
except:
print("TX failed")
time.sleep(1)
print("Disconnected")をインストールする
②受信機側PicoW(ペリフェラル)
下記3つのファイルをインストールします。
・ble_advertising.py(GitHubよりダウンロード)
・ble_simple_peripheral.py(GitHubよりダウンロード)
・main.py (引用:https://saitodev.co/microbit/rpi/article/70)
import machine
import bluetooth
from ble_simple_peripheral import BLESimplePeripheral
ble = bluetooth.BLE()
p = BLESimplePeripheral(ble)
led = machine.Pin("LED", machine.Pin.OUT)
led.off()
def on_rx(v):
print("received:", v)
if v == b'1\r\n':
led.on()
p.send("ON\n")
elif v == b'0\r\n':
led.off()
p.send("OFF\n")
else:
p.send("do nothing\n")
while True:
if p.is_connected():
p.on_write(on_rx)をインストールする
これでLチカの送受信機ができます。
これでLチカの送受信機ができます。
③ラジコンを操作する処理を作る
送信機から1回で送れる文字数は最大20文字です。(たぶん試した限りでは・・・)
●ボタン操作の場合
コントローラーを押しボタンだけで処理する場合はボタン名とon、offを送れば良いのでLチカ同様に送れば良いです。
例:Aボタンonの場合⇒A1、Aボタンoffの場合⇒A0
受信機側の処理は送信機から送られた文字を条件分岐で処理すれば良いです。
●アナログジョイスティックの場合
アナログジョイスティックはADCに入力された抵抗値を16bit値に変換してから処理します。
中立位置が中央値でスティックを倒すと0、反対側が最大の65535になります。
中立位置が中央値でスティックを倒すと0、反対側が最大の65535になります。
私の場合は中立位置が0の±100%の値に変換し文字列として送信しました。
中立位置は抵抗値がハンチングするので、±0.5~3%の範囲を0とすると無操作時の挙動が安定します。
複数のボタンやアナログジョイスティックがある場合は各スイッチの状態をカンマ(、)等で1繋がりの文字列にして送信し、受信機側でカンマで文字列を分割して配列にし、配列を条件分岐で処理してモーター等を動作させます。
コメント
コメントを投稿