21 #ifndef _KALMAN_FILTER_H_
22 #define _KALMAN_FILTER_H_
31 #define KALMAN_GAIN_PRECISION 1000
230 #define SetPoint 1500
231 #define InitialPoint 0
236 int initialX[] = {InitialPoint, InitialPoint, InitialPoint, InitialPoint};
237 int initialU[] = {0, 0, 0, 0};
243 int za = ((
MatrixGet(z, 0, 0) - yMin) * 63) / (yMax - yMin);
244 int xa = ((
MatrixGet(x, 0, 0) - yMin) * 63) / (yMax - yMin);
247 if (xa > 63) xa = 63;
249 if (xPos < lastXPos) lastXPos = 0;
251 PointOut(xPos, za,
false);
252 CircleOut(xPos, za, 2,
false);
253 LineOut(lastXPos, lastPoint, xPos, xa,
false);
255 int deviation = Sqrt(pa);
256 LineOut(xPos, (xa - deviation), xPos, (xa + deviation),
false);
266 void TestKalmanFilter()
292 for (
int j = 5; j < 110; j += 5)
294 for (
int k = 0; k < DIM; k++)
302 int t1 = CurrentTick();
304 int t2 = CurrentTick();
307 PlotKalman(z, x, P, SetPoint - 700, SetPoint + 700, j);
309 TextOut(0, LCD_LINE1,
"---------------------",
false);
310 NumOut (0, LCD_LINE1,
MatrixGet(x, 0, 0),
false);
311 NumOut (55, LCD_LINE1, t2 - t1,
false);
long MatrixInverse(Matrix matrix, long precision, Matrix &retInverse)
Finds the inverse of a matrix. The inverted matix is scalled by the returned scale factor...
void MatrixInitDiagonal(Matrix &matrix, int rows, int cols, long value)
Initializes a matrix to a diagonal matrix of size rows x cols, with all elements except diagonal elem...
The matrix class. Don't access the members directly. Use the macros MIJ() or MI() or the methods prov...
bool MatrixIsNull(Matrix &matrix)
Returns true if the matrix is NULL, i.e. all elemants are 0.
void MatrixSet(Matrix &matrix, int row, int col, long value)
Sets the given value to the element in matrix given by row x col. This is exactly same as assigning t...
void MatrixInit(Matrix &matrix, int rows, int cols)
Initializes a matrix to a matrix of size rows x cols and fills it with 0s.
void MatrixMultiply(Matrix &matrixA, Matrix &matrixB, Matrix &matrixC)
Multiplies matrix A with B and stores the result in matrix C. i.e. C = A * B. Matrix C must not be sa...
void MatrixAdd(Matrix &matrixA, Matrix &matrixB)
Adds matrix B to matrix A. i.e. A += B.
void KalmanFilterInitUnity(KalmanFilter &filter, Matrix &Q, Matrix &R, Matrix &P0, Matrix &X0)
Initializes the kalman filter in unity mode with given paramters. Unity mode runs the filter with the...
Debugging utility macros.
void KalmanFilterGetX(KalmanFilter &filter, Matrix &X)
Gets the current estimated state in the filter and returns it in X.
void KalmanFilterInit(KalmanFilter &filter, Matrix &A, Matrix &B, Matrix &H, Matrix &Q, Matrix &R, Matrix &P0, Matrix &X0)
Initializes the kalman filter with given paramters. matrix A is the state transition model...
void KalmanFilterGetP(KalmanFilter &filter, Matrix &P)
Gets the current error covariance of the current estimated state in the filter and returns it in P...
Normally distributed (Gaussian distribution) random number function.
Kalman Filter class representing the kalman model and state updates. In practice, upto only 3 dimenti...
void KalmanFilterStep(KalmanFilter &filter, Matrix &U, Matrix &Z)
Runs the filter step. matrix U is the current state control input and matrix Z is the current observa...
void MatrixInitElements(Matrix &matrix, int rows, int cols, long &elements[])
Initializes a matrix to a matrix of size rows x cols and fills it with elements from given array...
Integer matrix implementation. Provides matrix algebra, cofactor, adjugate and inverse computation...
void MatrixSubtract(Matrix &matrixA, Matrix &matrixB)
Subtracts matrix B from matrix A. i.e. A -= B.
void MatrixTranspose(Matrix &matrix)
Converts the given matrix into its Transpose, i.e. the matrix is flipped along its diagonal (top-left...
void MatrixReduce(Matrix &matrix, long scale)
Reduces (i.e. scales down) all elements in matrix A by given reduction factor. i.e. A /= scale.
int RandomNorm(int scale)
Returns a random number sample from Guassian distribution scaled by the given factor (using lookup ta...
long MatrixGet(Matrix &matrix, int row, int col)
Returns the value of element in matrix given by row x col. This is exactly same as macro MIJ()...
void MatrixInitIdentity(Matrix &matrix, int rows, int cols)
Initializes a matrix to an Identity matrix of size rows x cols, with all elements except diagonal ele...