天天看點

Mircopython: 在BPIbit上制作指南針Document

Document

Mircopython: 在BPIbit上制作指南針Document

指南針

  • 注意:在 1.2 的版型上沒有磁力計功能。
  • 這個子產品可以通路内置的電子羅盤(即mpu)。在使用指南針之前,應該校準指南針,否則讀數可能會出錯
  • 校準羅盤會導緻程式暫停,直到校準完成。校準由一個小遊戲組成,通過旋轉闆子在空中畫圈完成校準

關于指南針的函數

compass.calibrate()

  • 執行此函數開始校準過程,會收到一條有指導意義的資訊,然後我們需要旋轉闆子,在空中畫一'8'或着轉圈,(這個動作可以參考你的手機,手機的指南針功能在使用之前都會有一個校準的步驟),這個校準的過程會占用大概1分鐘的時間,校準期間無法執行其他程式
  • 提示資訊
    Mircopython: 在BPIbit上制作指南針Document

compass.is_calibrated ()

  • 如果羅盤校準成功,傳回True,否則傳回False。

compass.get_x ()

  • 傳回x軸上磁場強度的讀數,它是一個正整數或負整數,取決于磁場的方向。

compass.get_y ()

  • 傳回y軸上磁場強度的讀數,它是一個正整數或負整數,取決于磁場的方向。

compass.get_z ()

  • 傳回z軸磁場強度的讀數,它是一個正整數或負整數,取決于磁場的方向。

compass.heading()

  • 給出從上述讀數計算出的羅盤航向,為0到360範圍内的整數,表示按順時針方向的角度,12點鐘方向為0

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])
           
  • 在這個例子中第一步程式先校準了電子羅盤(mpu),校準完成後可以看到led面闆上有一個指南針它不管如何轉動闆子它始終指向南方
Mircopython: 在BPIbit上制作指南針Document
Mircopython: 在BPIbit上制作指南針Document