Skip to content

Commit

Permalink
finished ch 10
Browse files Browse the repository at this point in the history
  • Loading branch information
BartMassey committed Jul 9, 2024
1 parent 645cee7 commit 8b89788
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 71 deletions.
3 changes: 0 additions & 3 deletions mdbook/src/03-setup/linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ $ cat /etc/udev/rules.d/69-microbit.rules

``` text
# CMSIS-DAP for microbit
ACTION!="add|change", GOTO="microbit_rules_end"
SUBSYSTEM=="usb", ATTR{idVendor}=="0d28", ATTR{idProduct}=="0204", TAG+="uaccess"
LABEL="microbit_rules_end"
```

Expand Down
30 changes: 0 additions & 30 deletions mdbook/src/09-led-compass/build.rs

This file was deleted.

6 changes: 0 additions & 6 deletions mdbook/src/09-led-compass/memory.x

This file was deleted.

57 changes: 37 additions & 20 deletions mdbook/src/09-led-compass/src/calibration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ use core::fmt::Debug;
use embedded_hal::i2c::I2c;
use embedded_hal::delay::DelayNs;
use libm::{fabsf, sqrtf};
use lsm303agr::interface::I2cInterface;
use lsm303agr::mode::MagContinuous;
use lsm303agr::Lsm303agr;
use lsm303agr::Measurement;

use lsm303agr::{interface::I2cInterface, mode::MagContinuous, Lsm303agr};

use microbit::display::blocking::Display;

const PERIMETER_POINTS: usize = 25;
const PIXEL1_THRESHOLD: i32 = 200;
const PIXEL2_THRESHOLD: i32 = 600;
const CALIBRATION_INCREMENT: i32 = 200;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Measurement {
pub x: i32,
pub y: i32,
pub z: i32,
}

impl Measurement {
pub fn new((x, y, z): (i32, i32, i32)) -> Self {
Self { x, y, z }
}
}

#[allow(unused)]
#[derive(Debug)]
pub struct Calibration {
Expand All @@ -37,29 +49,27 @@ impl Default for Calibration {
}
}

pub fn calc_calibration<I, T, E>(
pub fn calc_calibration<I, T>(
sensor: &mut Lsm303agr<I2cInterface<I>, MagContinuous>,
display: &mut Display,
timer: &mut T,
) -> Calibration
where
T: DelayNs,
I: I2c,
E: Debug,
{
let data = get_data(sensor, display, timer);
return calibrate(&data);
calibrate(&data)
}

fn get_data<I, T, E>(
fn get_data<I, T>(
sensor: &mut Lsm303agr<I2cInterface<I>, MagContinuous>,
display: &mut Display,
timer: &mut T,
) -> [Measurement; 25]
) -> [Measurement; PERIMETER_POINTS]
where
T: DelayNs,
I: I2c,
E: Debug,
{
let mut leds = [
[0, 0, 0, 0, 0],
Expand All @@ -73,10 +83,11 @@ where
let mut samples = 0;

while samples < PERIMETER_POINTS {
while !sensor.accel_status().unwrap().xyz_new_data {}
let accel_data = sensor.accel_data().unwrap();
let x = accel_data.x;
let y = accel_data.y;
while !sensor.accel_status().unwrap().xyz_new_data() {
timer.delay_us(10u32);
}
let (x, y, _) = sensor.acceleration().unwrap().xyz_mg();

if x < -PIXEL2_THRESHOLD {
cursor.1 = 0;
} else if x < -PIXEL1_THRESHOLD {
Expand Down Expand Up @@ -106,14 +117,19 @@ where

if leds[cursor.0][cursor.1] != 1 {
leds[cursor.0][cursor.1] = 1;
while !sensor.mag_status().unwrap().xyz_new_data {}
let mag_data = measurement_to_enu(sensor.mag_data().unwrap());
while !sensor.mag_status().unwrap().xyz_new_data() {
timer.delay_us(10u32);
}
let measurement = Measurement::new(
sensor.magnetic_field().unwrap().xyz_nt()
);
let mag_data = measurement_to_enu(measurement);
data[samples] = mag_data;
samples += 1;
}
display.show(timer, leds, 200);
}
return data;
data
}

fn difference_square(a: Measurement, b: Measurement) -> f32 {
Expand Down Expand Up @@ -153,9 +169,10 @@ fn calibrate(data: &[Measurement]) -> Calibration {
center.z += point.z;
}

center.x = center.x / data.len() as i32;
center.y = center.y / data.len() as i32;
center.z = center.z / data.len() as i32;
let ndata = data.len() as i32;
center.x /= ndata;
center.y /= ndata;
center.z /= ndata;

let mut current = center;
let mut score = measure_score(current, data);
Expand Down
30 changes: 18 additions & 12 deletions mdbook/src/09-led-compass/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ 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 crate::calibration::{Measurement, calc_calibration, calibrated_measurement};

use microbit::{display::blocking::Display, hal::Timer};
use embedded_hal::delay::DelayNs;
use microbit::{
display::blocking::Display,
hal::{Timer, twim},
pac::twim0::frequency::FREQUENCY_A,
};

use microbit::{hal::twim, pac::twim0::frequency::FREQUENCY_A};

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

#[entry]
fn main() -> ! {
Expand All @@ -23,21 +25,25 @@ fn main() -> ! {

let i2c = { twim::Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100) };

let mut timer = Timer::new(board.TIMER0);
let mut timer0 = 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();
sensor.set_mag_mode_and_odr(&mut timer0, MagMode::HighResolution, MagOutputDataRate::Hz10).unwrap();
sensor.set_accel_mode_and_odr(&mut timer0, AccelMode::HighResolution, AccelOutputDataRate::Hz10).unwrap();
let mut sensor = sensor.into_mag_continuous().ok().unwrap();

let calibration = calc_calibration(&mut sensor, &mut display, &mut timer);
let calibration = calc_calibration(&mut sensor, &mut display, &mut timer0);
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();
while !sensor.mag_status().unwrap().xyz_new_data() {
timer0.delay_ms(1u32);
}
let mut data = Measurement::new(
sensor.magnetic_field().unwrap().xyz_nt()
);
data = calibrated_measurement(data, &calibration);
rprintln!("x: {}, y: {}, z: {}", data.x, data.y, data.z);
}
Expand Down

0 comments on commit 8b89788

Please sign in to comment.