codey rocky(ESP32)でMicroPythonを動かす(3) – テレビのリモコンでrockyを動かす

ども。こんばんは。

なぜだかわかりませんが思いの外ハマっています。楽しい。

ラズパイに比べてできることに制約が多いので、制約の中でできることを探しながら色々試すのが楽しいですね。

で、今回は、赤外線(Infrared)を使って、テレビのリモコンでrockyを操作するコードを作ってみました。

・Codey
Aボタン:リモコンから受け取ったコードを表示するだけ
Bボタン:全スクリプトを停止
Cボタン:リモコン操作で動くモード

・リモコン
音量大小:速度を変更(1-100)
上下左右:押している間rockyがその方向にすすむ

・おまけ
前に障害物がある場合は、止まる。

動かすとこんな感じです。
音量キーでの速度の調整がやたら早いので少しwaitを入れたいのですが、他のコード受信時と同じ処理でやっているので、waitをかけるとrockyの動きがカクカクになります。。。

 

 

■コード

import codey,rocky,event

#このグローバル変数イマイチ
rspeed = 10

def speedctl(flg):
    global rspeed
    if flg == "up":
        if rspeed < 100: rspeed = rspeed + 5 else: if rspeed > 10:
            rspeed = rspeed - 5


#リモコンモード
# TOSHIBA製リモコン
# 64 26 音量大
# 64 30 音量小
# 64 62 上
# 64 64 下
# 64 95 左
# 64 91 右
@event.button_c_pressed
def button_c_cb():
    global rspeed
    codey.display.show("RC",wait=True)
    while True:
        ope = codey.ir.receive_remote_code()[1]
        codey.display.show(rspeed,wait=True)

        if ope == 26:
            speedctl('up')
        elif ope == 30:
            speedctl('down')

        if ope == 0:
            rocky.stop()
        elif ope == 91:
            rocky.turn_right(rspeed)
        elif ope == 95:
            rocky.turn_left(rspeed)
        elif ope == 62:
            if not rocky.color_ir_sensor.is_obstacle_ahead():
                rocky.forward(rspeed)
            else:
                rocky.stop()
        elif ope == 63:
            rocky.backward(rspeed)
        else:
            rocky.stop()

#ただのリモコン読み取りモード
@event.button_a_pressed
def on_button_a_pressed():
    codey.display.show("Recv mode",wait=True)
    while True:
        ope = str(codey.ir.receive_remote_code()[0])
        ope2 = str(codey.ir.receive_remote_code()[1])
        codey.display.show(ope2,wait=True)

#### TEMPLATE ####
#基本的な構文間違いなどにきづけるように起動時にok!と表示 
@event.start
def on_start():
    codey.display.show("ok!")

#スクリプト停止
@event.button_b_pressed
def on_button_b_pressed():
    codey.display.show("Stopped",wait=True)
    codey.stop_all_scripts()

 

ちなみに、赤外線の送信はまだうまく行ってません・・・。というか、送信機能未完成っぽい?

フォーラムでテクニカルサポートからこんな回答がでてます・・・。

リモコンに信号を送るには2つ送らないと行けないけどまだエミッターができてないよ、的な・・・。

2年経ったけどいかがでしょうか・・・。

Sensing IR signals

I am afraid that currently IR control port on mblock software is not open for users yet, although the firmware supports that. Users now cannot use remote controller to control codey rocky.
We will update the information if we add this function to mblock software officially.
Thank you for the feedback, we’ll complete APIs for IR reception emitter then.
 

【バックナンバー】

ではでは。またの機会に。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください