加速度センサー

このオブジェクトでボード上の加速度センサーにアクセスできます。

MicroPython はデフォルトで加速度センサーの測定範囲を +/- 2000 mg に設定します(g は標準重力に基づく加速度の単位)。この測定範囲から加速度センサーの関数が返す最大値と最小値も決まります。microbit.accelerometer.set_range() を使うと測定範囲を変更できます。

加速度センサーはジェスチャーをの検出にも使えます。認識できるジェスチャーは文字列で表され、次のものがあります: up, down, left, right, face up, face down, freefall, 3g, 6g, 8g, shake

注釈

ジェスチャーはバックグラウンドで更新されないので、ジェスチャーの検出を行うには何らかの加速度センサーのメソッドを常に呼び出す必要があります。たいてい、ジェスチャーは microbit.sleep() による短い遅延のあるループを使って検出できます。

関数

microbit.accelerometer.get_x()
戻り値:x 軸のミリg単位の加速度。値は正または負の整数値です。
microbit.accelerometer.get_y()
戻り値:y 軸の加速度(ミリg単位)。値は正または負の整数値です。
microbit.accelerometer.get_z()
戻り値:z 軸の加速度(ミリg単位)。値は正または負の整数値です。
microbit.accelerometer.get_values()
戻り値:すべての軸の加速度。X, Y, Z の並びで整数値のタプルが返ります。
microbit.accelerometer.get_strength()

すべての軸を合成した加速度測定値を正の整数値で得ます。これは X軸、Y軸、Z軸のピタゴラス和になります。

戻り値:すべての軸の合成加速度(ミリg単位)。
microbit.accelerometer.current_gesture()
戻り値:現在のジェスチャーの名前を表す文字列。
microbit.accelerometer.is_gesture(name)
パラメータ:name -- チェックするジェスチャーの名前を表す文字列。
戻り値:指定した名前のジェスチャーが認識されているかを示す真偽値。
microbit.accelerometer.was_gesture(name)
パラメータ:name -- チェックするジェスチャーの名前を表す文字列。
戻り値:直前の呼出し以降に指定した名前のジェスチャーが認識されたかを示す真偽値。
microbit.accelerometer.get_gestures()

記録されたジェスチャーの履歴リストを得ます。

この呼出しを行うと、結果を戻す前にジェスチャーの履歴をクリアします。

戻り値:ジェスチャー履歴のタプル。タプルは古い順に並び、最新のものが最後の要素になります。
microbit.accelerometer.set_range(value)

加速度センサーの感度範囲を g (標準重力)で設定します。設定値は、ハードウェアがサポートする最も近い値、すなわち 1, 2, 4, 8 g のいずれかに丸められます。

パラメータ:value -- 加速度センサーの新しい測定範囲。 g 単位の整数値で指定します。

サンプルコード

マジック8ボール占いです。質問の後にデバイスをゆさぶってください。

# マジック8ボール占い 作成者:Nicholas Tollervey 2016年2月。
#
# 質問の後にデバイスを振ってゆさぶってください。
#
# このプログラムはパブリックドメインで公開します。
from microbit import *
import random

answers = [
    "It is certain",
    "It is decidedly so",
    "Without a doubt",
    "Yes, definitely",
    "You may rely on it",
    "As I see it, yes",
    "Most likely",
    "Outlook good",
    "Yes",
    "Signs point to yes",
    "Reply hazy try again",
    "Ask again later",
    "Better not tell you now",
    "Cannot predict now",
    "Concentrate and ask again",
    "Don't count on it",
    "My reply is no",
    "My sources say no",
    "Outlook not so good",
    "Very doubtful",
]

while True:
    display.show('8')
    if accelerometer.was_gesture('shake'):
        display.clear()
        sleep(1000)
        display.scroll(random.choice(answers))
    sleep(10)

単純なスラロームです。デバイスを動かして障害物を避けてください。

# 単純なスラローム 作成者:Larry Hastings 2015年9月。
#
# このプログラムはパブリックドメインで公開します。

import microbit as m
import random

p = m.display.show

min_x = -1024
max_x = 1024
range_x = max_x - min_x

wall_min_speed = 400
player_min_speed = 200

wall_max_speed = 100
player_max_speed = 50

speed_max = 12


while True:

    i = m.Image('00000:'*5)
    s = i.set_pixel

    player_x = 2

    wall_y = -1
    hole = 0

    score = 0
    handled_this_wall = False

    wall_speed = wall_min_speed
    player_speed = player_min_speed

    wall_next = 0
    player_next = 0

    while True:
        t = m.running_time()
        player_update = t >= player_next
        wall_update = t >= wall_next
        if not (player_update or wall_update):
            next_event = min(wall_next, player_next)
            delta = next_event - t
            m.sleep(delta)
            continue

        if wall_update:
            # 新しいスピードを計算
            speed = min(score, speed_max)
            wall_speed = wall_min_speed + int((wall_max_speed - wall_min_speed) * speed / speed_max)
            player_speed = player_min_speed + int((player_max_speed - player_min_speed) * speed / speed_max)

            wall_next = t + wall_speed
            if wall_y < 5:
                # 古い壁を消す
                use_wall_y = max(wall_y, 0)
                for wall_x in range(5):
                    if wall_x != hole:
                        s(wall_x, use_wall_y, 0)

        wall_reached_player = (wall_y == 4)
        if player_update:
            player_next = t + player_speed
            # 新しい X 座標を得る
            x = m.accelerometer.get_x()
            x = min(max(min_x, x), max_x)
            # print("x accel", x)
            s(player_x, 4, 0) # 古いピクセルをオフ
            x = ((x - min_x) / range_x) * 5
            x = min(max(0, x), 4)
            x = int(x + 0.5)
            # print("have", position, "want", x)

            if not handled_this_wall:
                if player_x < x:
                    player_x += 1
                elif player_x > x:
                    player_x -= 1
            # print("new", position)
            # print()

        if wall_update:
            # 壁の位置を変更
            wall_y += 1
            if wall_y == 7:
                wall_y = -1
                hole = random.randrange(5)
                handled_this_wall = False

            if wall_y < 5:
                # 新しい壁を表示
                use_wall_y = max(wall_y, 0)
                for wall_x in range(5):
                    if wall_x != hole:
                        s(wall_x, use_wall_y, 6)

        if wall_reached_player and not handled_this_wall:
            handled_this_wall = True
            if (player_x != hole):
                # 衝突! ゲームオーバー!
                break
            score += 1

        if player_update:
            s(player_x, 4, 9) # 新しいピクセルをオン

        p(i)

    p(i.SAD)
    m.sleep(1000)
    m.display.scroll("Score:" + str(score))

    while True:
        if (m.button_a.is_pressed() and m.button_a.is_pressed()):
            break
        m.sleep(100)