| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Math::KalmanFilter; | ||
| 5 | |||
| 6 | # PODNAME: Math::KalmanFilter | ||
| 7 | # ABSTRACT: Kalman Filter(also known as Linear Quadratic Estimation) implementation for sensor fusion and such | ||
| 8 | # | ||
| 9 | # This file is part of Math-KalmanFilter | ||
| 10 | # | ||
| 11 | # This software is copyright (c) 2016 by Shantanu Bhadoria. | ||
| 12 | # | ||
| 13 | # This is free software; you can redistribute it and/or modify it under | ||
| 14 | # the same terms as the Perl 5 programming language system itself. | ||
| 15 | # | ||
| 16 | 1 | our $VERSION = '0.07'; # VERSION | |
| 17 | |||
| 18 | # Dependencies | ||
| 19 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | |||
| 23 | 1 | has qAngle => ( | |
| 24 | is => 'rw', | ||
| 25 | default => 0.001, | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | 1 | has qBias => ( | |
| 30 | is => 'rw', | ||
| 31 | default => 0.003, | ||
| 32 | ); | ||
| 33 | |||
| 34 | |||
| 35 | 1 | has rMeasure => ( | |
| 36 | is => 'rw', | ||
| 37 | default => 0.03, | ||
| 38 | ); | ||
| 39 | |||
| 40 | |||
| 41 | 1 | has bias => ( | |
| 42 | is => 'rw', | ||
| 43 | default => 0, | ||
| 44 | ); | ||
| 45 | |||
| 46 | |||
| 47 | has covariance => ( | ||
| 48 | is => 'rw', | ||
| 49 | 0 | ▶ | default => sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 50 | 0 | return [ [ 0, 0 ], [ 0, 0 ] ]; | |
| 51 | } | ||
| 52 | 1 | ); | |
| 53 | |||
| 54 | |||
| 55 | 1 | has angle => ( is => 'rw', ); | |
| 56 | |||
| 57 | |||
| 58 | 0 | ▶ | sub getAngle { |
|
Subroutine
getAngle: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 59 | 0 | my ( $self, $newAngle, $newRate, $deltaTime ) = @_; | |
| 60 | |||
| 61 | 0 | my $covariance = $self->covariance; | |
| 62 | 0 | my $angle = $self->angle; | |
| 63 | 0 | my $bias = $self->bias; | |
| 64 | |||
| 65 | 0 | my $rate = $newAngle - $bias; | |
| 66 | 0 | $angle += $deltaTime * $rate; | |
| 67 | |||
| 68 | 0 | $covariance->[0]->[0] += | |
| 69 | $deltaTime * | ||
| 70 | ( $deltaTime * $covariance->[1]->[1] - | ||
| 71 | $covariance->[0]->[1] - | ||
| 72 | $covariance->[1]->[0] + | ||
| 73 | $self->qAngle ); | ||
| 74 | 0 | $covariance->[0]->[1] -= $deltaTime * $covariance->[1]->[1]; | |
| 75 | 0 | $covariance->[1]->[0] -= $deltaTime * $covariance->[1]->[1]; | |
| 76 | 0 | $covariance->[1]->[1] += $deltaTime * $self->qBias; | |
| 77 | |||
| 78 | 0 | my $innovationCovariance = $covariance->[0]->[0] + $self->rMeasure; | |
| 79 | |||
| 80 | 0 | my $kalmanGain = [ | |
| 81 | $covariance->[0]->[0] / $innovationCovariance, | ||
| 82 | $covariance->[1]->[0] / $innovationCovariance, | ||
| 83 | ]; | ||
| 84 | |||
| 85 | 0 | my $y = $newAngle - $angle; | |
| 86 | 0 | $angle += $kalmanGain->[0] * $y; | |
| 87 | 0 | $bias += $kalmanGain->[1] * $y; | |
| 88 | |||
| 89 | 0 | $covariance->[0]->[0] -= $kalmanGain->[0] * $covariance->[0]->[0]; | |
| 90 | 0 | $covariance->[0]->[1] -= $kalmanGain->[0] * $covariance->[0]->[1]; | |
| 91 | 0 | $covariance->[1]->[0] -= $kalmanGain->[1] * $covariance->[0]->[0]; | |
| 92 | 0 | $covariance->[1]->[1] -= $kalmanGain->[1] * $covariance->[0]->[1]; | |
| 93 | |||
| 94 | 0 | $self->covariance($covariance); | |
| 95 | 0 | $self->angle($angle); | |
| 96 | |||
| 97 | 0 | return $angle; | |
| 98 | } | ||
| 99 | |||
| 100 | 1 | 1; | |
| 101 | |||
| 102 | __END__ | ||