|
@@ -13,6 +13,7 @@ namespace bbiwarg.Utility
|
|
{
|
|
{
|
|
private Kalman kalman;
|
|
private Kalman kalman;
|
|
private float measurementNoiseFactor, processNoiseFactor;
|
|
private float measurementNoiseFactor, processNoiseFactor;
|
|
|
|
+ private float fps;
|
|
|
|
|
|
public bool Initialized { get; private set; }
|
|
public bool Initialized { get; private set; }
|
|
|
|
|
|
@@ -21,30 +22,38 @@ namespace bbiwarg.Utility
|
|
// y: current y position
|
|
// y: current y position
|
|
// v_x: velocity in x direction
|
|
// v_x: velocity in x direction
|
|
// v_y: velocity in y direction
|
|
// v_y: velocity in y direction
|
|
- public Kalman2DPositionFilter(float measurementNoiseFactor = 1.0e-1f, float processNoiseFactor = 1.0e-4f)
|
|
|
|
|
|
+ // a_x: acceleration in x direction
|
|
|
|
+ // a_y: acceleration in y direction
|
|
|
|
+ public Kalman2DPositionFilter(float measurementNoiseFactor = 1.0e-1f, float processNoiseFactor = 1.0e-4f, int fps = 30)
|
|
{
|
|
{
|
|
this.measurementNoiseFactor = measurementNoiseFactor;
|
|
this.measurementNoiseFactor = measurementNoiseFactor;
|
|
this.processNoiseFactor = processNoiseFactor;
|
|
this.processNoiseFactor = processNoiseFactor;
|
|
|
|
+ this.fps = fps;
|
|
reset();
|
|
reset();
|
|
}
|
|
}
|
|
|
|
|
|
public void reset()
|
|
public void reset()
|
|
{
|
|
{
|
|
- // 4 state variables and 2 measurements (0 controls)
|
|
|
|
- kalman = new Kalman(4, 2, 0);
|
|
|
|
|
|
+ // 6 state variables and 2 measurements (0 controls)
|
|
|
|
+ kalman = new Kalman(6, 2, 0);
|
|
|
|
+
|
|
|
|
+ // time step (s)
|
|
|
|
+ float t = 1 / fps;
|
|
|
|
|
|
// transition matrix
|
|
// transition matrix
|
|
Matrix<float> transitionMatrix = new Matrix<float>(new float[,]
|
|
Matrix<float> transitionMatrix = new Matrix<float>(new float[,]
|
|
- { {1.0f, 0.0f, 1.0f, 0.0f}, // x = x + v_x
|
|
|
|
- {0.0f, 1.0f, 0.0f, 1.0f}, // y = y + v_y
|
|
|
|
- {0.0f, 0.0f, 1.0f, 0.0f}, // v_x = v_x
|
|
|
|
- {0.0f, 0.0f, 0.0f, 1.0f}}); // v_y = v_y
|
|
|
|
|
|
+ { {1.0f, 0.0f, 1.0f * t, 0.0f, 0.5f * t * t, 0.0f},
|
|
|
|
+ {0.0f, 1.0f, 0.0f, 1.0f * t, 0.0f, 0.5f * t * t},
|
|
|
|
+ {0.0f, 0.0f, 1.0f, 0.0f, 1.0f * t, 0.0f},
|
|
|
|
+ {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f * t},
|
|
|
|
+ {0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f},
|
|
|
|
+ {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}});
|
|
kalman.TransitionMatrix = transitionMatrix;
|
|
kalman.TransitionMatrix = transitionMatrix;
|
|
|
|
|
|
// measurement matrix
|
|
// measurement matrix
|
|
Matrix<float> measurementMatrix = new Matrix<float>(new float[,]
|
|
Matrix<float> measurementMatrix = new Matrix<float>(new float[,]
|
|
- { {1.0f, 0.0f, 0.0f, 0.0f}, // first measurement = x
|
|
|
|
- {0.0f, 1.0f, 0.0f, 0.0f} // second measurement = y
|
|
|
|
|
|
+ { {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, // first measurement = x
|
|
|
|
+ {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f} // second measurement = y
|
|
});
|
|
});
|
|
kalman.MeasurementMatrix = measurementMatrix;
|
|
kalman.MeasurementMatrix = measurementMatrix;
|
|
|
|
|
|
@@ -54,13 +63,13 @@ namespace bbiwarg.Utility
|
|
kalman.MeasurementNoiseCovariance = measurementNoiseCovarianceMatrix;
|
|
kalman.MeasurementNoiseCovariance = measurementNoiseCovarianceMatrix;
|
|
|
|
|
|
// process noise covariance matrix
|
|
// process noise covariance matrix
|
|
- Matrix<float> processNoiseCovarianceMatrix = new Matrix<float>(4, 4);
|
|
|
|
|
|
+ Matrix<float> processNoiseCovarianceMatrix = new Matrix<float>(6, 6);
|
|
processNoiseCovarianceMatrix.SetIdentity(new MCvScalar(processNoiseFactor));
|
|
processNoiseCovarianceMatrix.SetIdentity(new MCvScalar(processNoiseFactor));
|
|
kalman.ProcessNoiseCovariance = processNoiseCovarianceMatrix;
|
|
kalman.ProcessNoiseCovariance = processNoiseCovarianceMatrix;
|
|
|
|
|
|
// error covariance post matrix (initial value)
|
|
// error covariance post matrix (initial value)
|
|
- Matrix<float> errorCovariancePostMatrix = new Matrix<float>(4, 4);
|
|
|
|
- errorCovariancePostMatrix.SetIdentity();
|
|
|
|
|
|
+ Matrix<float> errorCovariancePostMatrix = new Matrix<float>(6, 6);
|
|
|
|
+ errorCovariancePostMatrix.SetIdentity(new MCvScalar(processNoiseFactor));
|
|
kalman.ErrorCovariancePost = errorCovariancePostMatrix;
|
|
kalman.ErrorCovariancePost = errorCovariancePostMatrix;
|
|
|
|
|
|
Initialized = false;
|
|
Initialized = false;
|
|
@@ -69,7 +78,7 @@ namespace bbiwarg.Utility
|
|
public void setInitialPosition(Vector2D initialPosition)
|
|
public void setInitialPosition(Vector2D initialPosition)
|
|
{
|
|
{
|
|
// initial state (x, y, v_x, v_y)
|
|
// initial state (x, y, v_x, v_y)
|
|
- Matrix<float> initialState = new Matrix<float>(new float[] { initialPosition.X, initialPosition.Y, 0.0f, 0.0f });
|
|
|
|
|
|
+ Matrix<float> initialState = new Matrix<float>(new float[] { initialPosition.X, initialPosition.Y, 0.0f, 0.0f, 0.0f, 0.0f });
|
|
kalman.CorrectedState = initialState;
|
|
kalman.CorrectedState = initialState;
|
|
|
|
|
|
Initialized = true;
|
|
Initialized = true;
|
|
@@ -89,7 +98,7 @@ namespace bbiwarg.Utility
|
|
{rawPosition.Y}});
|
|
{rawPosition.Y}});
|
|
|
|
|
|
// prediction according to model
|
|
// prediction according to model
|
|
- Matrix<float> prediction = kalman.Predict();
|
|
|
|
|
|
+ kalman.Predict();
|
|
|
|
|
|
// corrected point
|
|
// corrected point
|
|
Matrix<float> estimate = kalman.Correct(rawPositionMatrix);
|
|
Matrix<float> estimate = kalman.Correct(rawPositionMatrix);
|