This module lets you access the built-in electronic compass. Before using, the compass should be calibrated, otherwise the readings may be wrong.
此模块可让您访问内置的电子罗盘。使用前,应校准指南针,否则读数可能会错误。
Warning 警告
Calibrating the compass will cause your program to pause until calibration is complete. Calibration consists of a little game to draw a circle on the LED display by rotating the device.
校准指南针将导致您的程序暂停,直到校准完成。校准包括一个小游戏,通过旋转设备在 LED 显示屏上画一个圆圈。
Starts the calibration process. An instructive message will be scrolled to the user after which they will need to rotate the device in order to draw a circle on the LED display.
启动校准过程。将向用户滚动一条指示性消息,之后他们需要旋转设备才能在 LED 显示屏上画一个圆圈。
Returns True if the compass has been successfully calibrated, and returns False otherwise.
如果指南针已成功校准,则返回 True,否则返回 False。
Undoes the calibration, making the compass uncalibrated again.
撤消校准,使指南针再次未校准。
Gives the reading of the magnetic field strength on the x axis in nano tesla, as a positive or negative integer, depending on the direction of the field.
根据磁场的方向,以纳米特斯拉为单位,以正整数或负整数形式给出 x 轴上的磁场强度读数。
Gives the reading of the magnetic field strength on the y axis in nano tesla, as a positive or negative integer, depending on the direction of the field.
根据磁场的方向,以纳米特斯拉为单位给出 y 轴上的磁场强度读数,以正整数或负整数表示。
Gives the reading of the magnetic field strength on the z axis in nano tesla, as a positive or negative integer, depending on the direction of the field.
根据磁场的方向,以纳米特斯拉为单位给出 z 轴上的磁场强度读数,以正整数或负整数表示。
Gives the compass heading, calculated from the above readings, as an integer in the range from 0 to 360, representing the angle in degrees, clockwise, with north as 0.
根据上述读数计算得出的罗盘航向为 0 到 360 范围内的整数,表示以度为单位的顺时针角度,北为 0。
Returns an integer indication of the magnitude of the magnetic field around the device in nano tesla.
返回设备周围磁场大小的整数指示,以纳米特斯拉为单位。
"""
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])