罗盘¶
此模块可让您访问内置电子罗盘。使用前,应校准罗盘,否则读数可能有误。
警告
校准指南针将导致您的程序暂停,直到校准完成。校准包括通过旋转设备在 LED 显示屏上画一个圆圈的小游戏。
职能¶
-
microbit.compass.calibrate()¶ 开始校准过程。将向用户滚动一条指导性消息,然后他们需要旋转设备才能在 LED 显示屏上画一个圆圈。
-
microbit.compass.is_calibrated()¶ 返回
True如果指南针已成功校准,并返回False否则。
-
microbit.compass.clear_calibration()¶ 取消校准,使指南针再次取消校准。
-
microbit.compass.get_x()¶ 根据磁场
x方向,以纳米特斯拉为单位给出轴上磁场强度的读数,以正整数或负整数表示。
-
microbit.compass.get_y()¶ 根据磁场
y方向,以纳米特斯拉为单位给出轴上磁场强度的读数,以正整数或负整数表示。
-
microbit.compass.get_z()¶ 根据磁场
z方向,以纳米特斯拉为单位给出轴上磁场强度的读数,以正整数或负整数表示。
-
microbit.compass.heading()¶ 给出根据上述读数计算的罗盘航向,以 0 到 360 范围内的整数表示,以度为单位表示角度,顺时针,北为 0。
-
microbit.compass.get_field_strength()¶ 以纳米特斯拉为单位返回设备周围磁场大小的整数指示。
例子¶
"""
compass.py
~~~~~~~~~~
Creates a compass.
The user will need to calibrate the compass first. The compass uses the
built-in clock images to display the position of the needle.
"""
from microbit import *
# Start calibrating
compass.calibrate()
# Try to keep the needle pointed in (roughly) the correct direction
while True:
sleep(100)
needle = ((15 - compass.heading()) // 30) % 12
display.show(Image.ALL_CLOCKS[needle])