hello in this video i will show you how to implement a common filter in simulink
the system that we will use is an inverted pendulum attached to a cart in the simulink
model we have a controller that adjusts the cart's position to keep the pendulum upright we give
the cart an initial push and have an external force applied to the pendulum for one second
six seconds into the simulation to simulate the system in action simscape blocks are used
when i run the simulink model the cart pendulum system is simulated in real time in the window to
the left as you can see the model has a controller and is moving correctly so why add a common filter
while the simulation might be working perfectly there might be noise in the system when we deploy
this algorithm to the real world suppose there is noise in the sensors that are being used
let's simulate this by adding in a white noise block and add that to the x position
when we run the simulation again we see that the system is shaking back and forth our controllers
can't do anything to alleviate the noise because it's acting based on the noisy data being received
this is where the common filter comes in to save the day the common filter will be used to
estimate the position of our system by filtering out the noise i won't go into depth into how the
algorithm works but if you're interested be sure to check out this video series on common filters
my colleague melda does a fantastic job of explaining the topic to implement the filter
we will use the common filter block our system is continuous so i'll switch to that option
but if you need to deploy a model to any hardware be sure to select discrete time next we need to
enter in the state space model for our system having an accurate state-space representation of
your system is very important as this is how the filter will estimate the behavior of the system
in this case i will have the matrices for a b c and e defined in matlab
instructions for deriving the state space for the system can be found in the description we'll
leave the initial states at 0 and move on to the noise covariances in my case i didn't put any
process noise into the system but let's assume that there is some processed noise associated
with the angular velocity of the pendulum because our system has four state variables
we need to use the diagonal function and input the q covariance as the fourth parameter
lastly we will enter a value similar to the noise power in as the r covariance
with the common filter block configured let's hook it up the u port is associated with the
input force so let's connect those together the y port is for the state variables we want to measure
in this case this is the x position of the cart and the angle of the pendulum we'll need
a mux block to input both signals into the common filter the output of the common filter block will
be a four by one array corresponding to our state variables we need the x position of the cart and
the pendulum angle which is the first and third elements respectively i will use the selector
block to specify these elements the demux block is then used to separate out the individual signals
now let's run the model and see the results we can see that there's still some jerkiness
but when we compare it to the model without the common filter we see a vast improvement let's log
some of the data so that we can further examine the results looking at the plots of the estimated
and actual x position we can see that they are very similar up to the pendulum disturbance at the
six second mark this is great results especially when we consider what the noisy system looked like
the difference seen at the six second mark can be attributed to the estimated angle looking at the
estimated and actual angle we can see a large error at the point of the external disturbance
this error is because as the angle changes the state space would change so you would need a
common filter that would let you adjust the state space model as dynamics change this can be done
by using an extended or unscented common filter which are also available as blocks in simulink
to learn about extended and unscented common filters i highly recommend you check out this
video series on common filters linked in the description it goes in depth into common filters
and implementing them in matlab and simulink thanks for watching