# マイクロマックイーン プラス (micro:Maqueen Plus) 自動運転(超音波による障害物の回避)




# 11.1. 目標




超音波距離センサーを使って、前の障害物を検知し、マイクロマックイーン プラス (micro:Maqueen Plus)を自律的に運転させるためのプログラミング方法を把握します。


# 11.2. 必要な機材




# PC利用時に必要な機材

No. 名称 個数 HW/SW 用途 補足
01 micro:bit(v2)本体 1 HW マイクロマックイーン プラス (micro:Maqueen Plus)のコントロール用。
プログラミングはmicro:biに対して行う
02 micro:bit用 ケース 1 HW micro:bit保護 ※ 必須ではない
03 micro:bit用 USBケーブル 1 HW micro:bitとPCの接続 Type-AとType-C両方対応できると様々な端末で利用しやすい
04 マイクロマックイーン プラス (micro:Maqueen Plus) 1 HW 車体本体
05 単3型乾電池 4 HW マイクロマックイーン プラスとmicro:bitへの電源供給
06 Chromebook、MacOS、WindowsのPC 1 HW MakeCode操作用
07 Google Chrome 1 SW MakeCodeアクセス用

# タブレット・スマートフォン利用時に必要な機材

No. 名称 個数 HW/SW 用途 補足
01 micro:bit(v2)本体 1 HW マイクロマックイーン プラスのコントロール用。
プログラミングはmicro:biに対して行う
02 micro:bit用 ケース 1 HW micro:bit保護 ※ 必須ではない
03 マイクロマックイーン プラス (micro:Maqueen Plus) 1 HW 車体本体
04 単3型乾電池 4 HW マイクロマックイーン プラスとmicro:bitへの電源供給
05 タブレット・スマートフォン 1 HW micro:bitアプリ操作用
06 micro:bit アプリ 1 SW micro:bitアプリ操作用



# 11.3. 動作イメージ




超音波センサーを使って、マイクロマックイーン プラス (micro:Maqueen Plus)と前方の障害物との距離を常に検出し、30cm以下になるとマイクロマックイーン プラス (micro:Maqueen Plus)は障害物を避けるためにランダムに左または右に曲がり、6cm以下になるとバックすることで、自動運転をさせます。

参考情報


  • 付属の超音波距離センサーを装着する必要があります。



# 11.4. プログラミング例




# MakeCodeプログラミング例(ブロック)





# MakeCodeプログラミング例(JavaScript)

let direction_random = 0
let distance_to_obstacle_cm = 0
DFRobotMaqueenPlusV2.I2CInit()
basic.forever(function () {
    distance_to_obstacle_cm = DFRobotMaqueenPlusV2.readUltrasonic(DigitalPin.P13, DigitalPin.P14)
    if (distance_to_obstacle_cm <= 30 && distance_to_obstacle_cm >= 6) {
        direction_random = randint(0, 1)
        if (direction_random == 0) {
            basic.showLeds(`
                . . # . .
                . # . . .
                # # # # #
                . # . . .
                . . # . .
                `)
            DFRobotMaqueenPlusV2.controlMotor(MyEnumMotor.eLeftMotor, MyEnumDir.eForward, 100)
            DFRobotMaqueenPlusV2.controlMotor(MyEnumMotor.eRightMotor, MyEnumDir.eForward, 0)
            basic.pause(800)
        } else {
            basic.showLeds(`
                . . # . .
                . . . # .
                # # # # #
                . . . # .
                . . # . .
                `)
            DFRobotMaqueenPlusV2.controlMotor(MyEnumMotor.eLeftMotor, MyEnumDir.eForward, 0)
            DFRobotMaqueenPlusV2.controlMotor(MyEnumMotor.eRightMotor, MyEnumDir.eForward, 100)
            basic.pause(800)
        }
    } else if (distance_to_obstacle_cm < 6) {
        basic.showLeds(`
            . . # . .
            . . # . .
            # . # . #
            . # # # .
            . . # . .
            `)
        DFRobotMaqueenPlusV2.controlMotor(MyEnumMotor.eAllMotor, MyEnumDir.eBackward, 100)
        basic.pause(800)
    } else {
        basic.showLeds(`
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
            `)
        DFRobotMaqueenPlusV2.controlMotor(MyEnumMotor.eAllMotor, MyEnumDir.eForward, 100)
    }
})



# MakeCodeプログラミング例(python)

direction_random = 0
distance_to_obstacle_cm = 0
DFRobotMaqueenPlusV2.i2c_init()

def on_forever():
    global distance_to_obstacle_cm, direction_random
    distance_to_obstacle_cm = DFRobotMaqueenPlusV2.read_ultrasonic(DigitalPin.P13, DigitalPin.P14)
    if distance_to_obstacle_cm <= 30 and distance_to_obstacle_cm >= 6:
        direction_random = randint(0, 1)
        if direction_random == 0:
            basic.show_leds("""
                . . # . .
                . # . . .
                # # # # #
                . # . . .
                . . # . .
            """)
            DFRobotMaqueenPlusV2.control_motor(MyEnumMotor.E_LEFT_MOTOR, MyEnumDir.E_FORWARD, 100)
            DFRobotMaqueenPlusV2.control_motor(MyEnumMotor.E_RIGHT_MOTOR, MyEnumDir.E_FORWARD, 0)
            basic.pause(800)
        else:
            basic.show_leds("""
                . . # . .
                . . . # .
                # # # # #
                . . . # .
                . . # . .
            """)
            DFRobotMaqueenPlusV2.control_motor(MyEnumMotor.E_LEFT_MOTOR, MyEnumDir.E_FORWARD, 0)
            DFRobotMaqueenPlusV2.control_motor(MyEnumMotor.E_RIGHT_MOTOR, MyEnumDir.E_FORWARD, 100)
            basic.pause(800)
    elif distance_to_obstacle_cm < 6:
        basic.show_leds("""
            . . # . .
            . . # . .
            # . # . #
            . # # # .
            . . # . .
        """)
        DFRobotMaqueenPlusV2.control_motor(MyEnumMotor.E_ALL_MOTOR, MyEnumDir.E_BACKWARD, 100)
        basic.pause(800)
    else:
        basic.show_leds("""
            . . # . .
            . # # # .
            # . # . #
            . . # . .
            . . # . .
        """)
        DFRobotMaqueenPlusV2.control_motor(MyEnumMotor.E_ALL_MOTOR, MyEnumDir.E_FORWARD, 100)
basic.forever(on_forever)



# MakeCodeプログラミング例のQRコード

マイクロマックイーン プラス (micro:Maqueen Plus)  自動運転(超音波による障害物の回避) プログラムサンプル QRコード





Last Updated: 2/5/2023, 3:42:18 PM