Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

大きさ

これまでは磁場の方向を扱ってきましたが、その実際の大きさはどれくらいなのでしょうか? mag_data() 関数のドキュメントによると、取得している x y z の値は ナノテスラ単位です。つまり、磁場の大きさをナノテスラで求めるために計算しなければならないのは、 x y z の値が表している 3D ベクトルの大きさだけです。学校で習ったように、これは単純に次のようになります。

#![allow(unused)]
fn main() {
// core にはまだこの関数がないので、以前の atan2f と同様に
// libm を使用します。
use libm::sqrtf;
let magnitude = sqrtf(x * x + y * y + z * z);
}

これらをすべてプログラムにまとめると、次のようになります。

#![deny(unsafe_code)]
#![no_main]
#![no_std]

use cortex_m_rt::entry;
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};

mod calibration;
use crate::calibration::calc_calibration;
use crate::calibration::calibrated_measurement;

use libm::sqrtf;

use microbit::{display::blocking::Display, hal::Timer};

#[cfg(feature = "v1")]
use microbit::{hal::twi, pac::twi0::frequency::FREQUENCY_A};

#[cfg(feature = "v2")]
use microbit::{hal::twim, pac::twim0::frequency::FREQUENCY_A};

use lsm303agr::{AccelOutputDataRate, Lsm303agr, MagOutputDataRate};

#[entry]
fn main() -> ! {
    rtt_init_print!();
    let board = microbit::Board::take().unwrap();

    #[cfg(feature = "v1")]
    let i2c = { twi::Twi::new(board.TWI0, board.i2c.into(), FREQUENCY_A::K100) };

    #[cfg(feature = "v2")]
    let i2c = { twim::Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100) };

    let mut timer = Timer::new(board.TIMER0);
    let mut display = Display::new(board.display_pins);

    let mut sensor = Lsm303agr::new_with_i2c(i2c);
    sensor.init().unwrap();
    sensor.set_mag_odr(MagOutputDataRate::Hz10).unwrap();
    sensor.set_accel_odr(AccelOutputDataRate::Hz10).unwrap();
    let mut sensor = sensor.into_mag_continuous().ok().unwrap();

    let calibration = calc_calibration(&mut sensor, &mut display, &mut timer);
    rprintln!("Calibration: {:?}", calibration);
    rprintln!("Calibration done, entering busy loop");
    loop {
        while !sensor.mag_status().unwrap().xyz_new_data {}
        let mut data = sensor.mag_data().unwrap();
        data = calibrated_measurement(data, &calibration);
        let x = data.x as f32;
        let y = data.y as f32;
        let z = data.z as f32;
        let magnitude = sqrtf(x * x + y * y + z * z);
        rprintln!("{} nT, {} mG", magnitude, magnitude/100.0);
    }
}

このプログラムは、磁場の大きさ(強さ)をナノテスラ(nT)およびミリガウス(mG)で報告します。地球の 磁場の大きさは 250 mG から 650 mG の範囲にあります(大きさは地理的な位置によって 変化します)ので、その範囲内、またはそれに近い値が表示されるはずです – 私の環境では およそ 340 mG の大きさが見えます。

いくつか質問です。

ボードを動かさずにいると、どのような値が見えますか? 常に同じ値が見えますか?

ボードを回転させると、大きさは変化しますか? 変化するべきでしょうか?