Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use const generics. #1

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions examples/gravity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use minikalman::{
create_buffer_temp_S_inv, create_buffer_temp_x, create_buffer_u, create_buffer_x,
create_buffer_y, create_buffer_z, matrix_data_t, Kalman, Measurement,
};
use stdint::uint_fast8_t;

/// Measurements.
///
Expand All @@ -39,12 +38,12 @@ const MEASUREMENT_ERROR: [matrix_data_t; 15] = [
-0.33747, 0.75873, 0.18135, -0.015764, 0.17869,
];

const NUM_STATES: usize = 3;
const NUM_INPUTS: usize = 0;
const NUM_MEASUREMENTS: usize = 1;

#[allow(non_snake_case)]
fn main() {
const NUM_STATES: uint_fast8_t = 3;
const NUM_INPUTS: uint_fast8_t = 0;
const NUM_MEASUREMENTS: uint_fast8_t = 1;

// System buffers.
let mut gravity_x = create_buffer_x!(NUM_STATES);
let mut gravity_A = create_buffer_A!(NUM_STATES);
Expand Down Expand Up @@ -74,9 +73,7 @@ fn main() {
let mut gravity_temp_PHt = create_buffer_temp_PHt!(NUM_STATES, NUM_MEASUREMENTS);
let mut gravity_temp_KHP = create_buffer_temp_KHP!(NUM_STATES);

let mut filter = Kalman::new_from_buffers(
NUM_STATES,
NUM_INPUTS,
let mut filter = Kalman::<NUM_STATES, NUM_INPUTS>::new_from_buffers(
&mut gravity_A,
&mut gravity_x,
&mut gravity_B,
Expand All @@ -88,9 +85,7 @@ fn main() {
&mut gravity_temp_BQ,
);

let mut measurement = Measurement::new_direct(
NUM_STATES,
NUM_MEASUREMENTS,
let mut measurement = Measurement::<NUM_STATES, NUM_MEASUREMENTS>::new_direct(
&mut gravity_H,
&mut gravity_z,
&mut gravity_R,
Expand Down Expand Up @@ -132,7 +127,7 @@ fn main() {
}

/// Initializes the state vector with initial assumptions.
fn initialize_state_vector(filter: &mut Kalman) {
fn initialize_state_vector(filter: &mut Kalman<'_, NUM_STATES, NUM_INPUTS>) {
filter.state_vector_apply(|state| {
state[0] = 0 as _; // position
state[1] = 0 as _; // velocity
Expand All @@ -148,7 +143,7 @@ fn initialize_state_vector(filter: &mut Kalman) {
/// v₁ = 1×v₀ + T×a₀
/// a₁ = 1×a₀
/// ```
fn initialize_state_transition_matrix(filter: &mut Kalman) {
fn initialize_state_transition_matrix(filter: &mut Kalman<'_, NUM_STATES, NUM_INPUTS>) {
filter.state_transition_apply(|a| {
// Time constant.
const T: matrix_data_t = 1 as _;
Expand All @@ -175,7 +170,7 @@ fn initialize_state_transition_matrix(filter: &mut Kalman) {
/// This defines how different states (linearly) influence each other
/// over time. In this setup we claim that position, velocity and acceleration
/// linearly are linearly independent.
fn initialize_state_covariance_matrix(filter: &mut Kalman) {
fn initialize_state_covariance_matrix(filter: &mut Kalman<'_, NUM_STATES, NUM_INPUTS>) {
filter.system_covariance_apply(|p| {
p.set(0, 0, 0.1 as _); // var(s)
p.set(0, 1, 0 as _); // cov(s, v)
Expand All @@ -195,7 +190,9 @@ fn initialize_state_covariance_matrix(filter: &mut Kalman) {
/// ```math
/// z = 1×s + 0×v + 0×a
/// ```
fn initialize_position_measurement_transformation_matrix(measurement: &mut Measurement) {
fn initialize_position_measurement_transformation_matrix(
measurement: &mut Measurement<'_, NUM_STATES, NUM_MEASUREMENTS>,
) {
measurement.measurement_transformation_apply(|h| {
h.set(0, 0, 1 as _); // z = 1*s
h.set(0, 1, 0 as _); // + 0*v
Expand All @@ -208,7 +205,9 @@ fn initialize_position_measurement_transformation_matrix(measurement: &mut Measu
/// This matrix describes the measurement covariances as well as the
/// individual variation components. It is the measurement counterpart
/// of the state covariance matrix.
fn initialize_position_measurement_process_noise_matrix(measurement: &mut Measurement) {
fn initialize_position_measurement_process_noise_matrix(
measurement: &mut Measurement<'_, NUM_STATES, NUM_MEASUREMENTS>,
) {
measurement.process_noise_apply(|r| {
r.set(0, 0, 0.5 as _); // var(s)
});
Expand Down
Loading