Clear Filters
Clear Filters

Update the Kalman Filter in Simulink model

7 views (last 30 days)
Duc Pham Minh
Duc Pham Minh on 12 Apr 2024
Answered: MULI on 18 Jun 2024
I want to creat a Filter in Simulink. For example:
kf = trackingKF('MotionModel', '2D Constant Acceleration', 'State', zeros(6, 1));
Then in simulink model, I want to update the filter and predict the states after receive feedback during the simulation. The function is defined as:
function predictedState = fcn(x,y)
% Receive feedback about the present position
presentPosition = [x;y];
% Correct step: Update the filter with observed position
correct(filter, presentPosition);
% Predict future positions after receiving feedback
predictedState = predict(filter);
end
My problem is: How to define this Filter in Simulink that can be updated by using 'correct' command, and how can I build a structure that can update this filter using my above function during my simulation. Thank you for your helps !
I try to define the Kalman Filter in my Predict function but it seems not good because the filter need to be updated to become more precise. Can the callback function in Model Explorer give a help ?

Answers (1)

MULI
MULI on 18 Jun 2024
Hi Pham,
I understand that you need to update the filter states based on feedback received during simulation. To integrate and continuously update a Kalman Filter in Simulink, you can use a "MATLAB Function" block. Within this block, utilize a "persistent" variable to maintain the filter's state across function calls, as given below:
function predictedState = fcn(x, y)
% Persistent variable to hold the filter
persistent filter
% Initialize the filter if it's the first execution
if isempty(filter)
filter = trackingKF('MotionModel', '2D Constant Acceleration', 'State', zeros(6, 1));
end
% Correct the filter's state based on new measurements
presentPosition = [x; y];
correct(filter, presentPosition);
% Predict the next state
predictedState = predict(filter);
end
You may refer to this documentation link for more information on persistent variable.

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!