背景
メイン基板には傾きを検出するセンサが搭載されているようです。
(正確には加速度のベクトルから傾きを計算しているのかもしれません。)
傾きを検出するセンサを入力として扱うプログラミングを行いました。
Scratch
傾きを検出するセンサは取得した値を変数として扱いになっているので、値を取得する命令をするブロックがあるわけではないようです。
「起動後にFrontLEDが白く点灯する。
そのとき後ろ方向に傾いていたらBackLEDが赤色に点灯し、前方向に傾いていたらBackLEDが水色に点灯する。」
最初にスタビライゼーションオフを入れないと持ち上げたときにspheroが暴れました。
特に向き(yaw)が起動時から変化すると嫌がるようです。
プログラムの終了後にスタビライゼーションは自動的にオンになるようなので、
spheroの向きが変わらないように置いてあげましょう。
傾けた方向と光り方を対応させると、ピッチのオリエンテーションの正負は右ねじの方向になっていました。
例えば、spheroを左向きにおいたとき、右に傾けると正、左に傾けると負でした。
spheroV2
testにあったBoltTest.pyのコメントアウトを参考に同様な動作をするスクリプトを作成しました。
import time
from spherov2 import scanner
from spherov2.sphero_edu import EventType, SpheroEduAPI
from spherov2.types import Color
print("Testing Starting...")
print("Connecting to Bolt...")
toy = scanner.find_BOLT()
if toy is not None:
print("Connected.")
with SpheroEduAPI(toy) as droid:
print("Testing Start...")
droid.reset_aim()
droid.set_stabilization(False)
droid.set_front_led(Color(r=255, g=255, b=255))
time.sleep(1)
orient = droid.get_orientation()
pitch = orient['pitch'] # 辞書から値を取得
print(f"pitch: {pitch:.1f}")
if pitch > 0:
droid.set_back_led(Color(r=255, g=0, b=0))
else :
droid.set_back_led(Color(r=0, g=255, b=255))
time.sleep(1)
print("Testing End...")
実行結果はこちらのようになりました。
後ろ方向に傾けた(BackLEDが赤色に点灯)
git\spherov2.py\spherov2\test>python BoltTest_orientationSensor.py
Testing Starting...
Connecting to Bolt...
Connected.
Testing Start...
pitch: 31.1
Testing End...
前方向に傾けた(BackLEDが水色に点灯)
git\spherov2.py\spherov2\test>python BoltTest_orientationSensor.py
Testing Starting...
Connecting to Bolt...
Connected.
Testing Start...
pitch: -19.4
Testing End...
まとめ
ジャイロセンサを入力に使ったプログラミングを実験しました。
spheroV2は傾きの値自体を表示する機能があるので、遊ぶ幅が広がりそうです。