welcome to the lecture on control engineering so i will be taking you on to the applications of the complementary filter so the complementary filter is a sensing ah is a sensor fusion algorithm where we calculate the angle from the accelerometer and then after the angle computation block from the accelerometer we get the angle underscore accelerometer we pass it through the low pass filter and bring it into the combination block from the gyroscope we also get the angle by numeric int ah integration the formula for numeric integration is shown below this angle from the gyroscope is pass through the high pass filter and it comes to the combination of the low pass filter of your angle from your accelerometer and your high pass filtered angle from your gyroscope so ah as discussed previously the complementary filters equation is as follows theta cf is equal to alpha which is your ah filter coefficient of your complementary filter into the theta of the high pass filter which is the angle which comes from the gyroscope plus one minus alpha cf into the theta which comes from the angle calculated from the accelerometer so these two are combined ah depending on the dynamics of the moment that we have the complementary filter equation is this where theta cf is your filter coefficient for the complementary filter theta cf is the angle computed from the complementary filter the theta high passed is the high passed value of angle from your gyroscope and theta low pass is the low pass value of the angle from your accelerometer so i will be showing you ah the matlab implementations of ah why we use the low pass filter and the high pass filter and how do we combine these two angles from the accelerometer and the gyroscope in the complementary filter here are the three codes that we have ah for the actual implementation of the high pass filter for the gyroscope angle and the low pass filter from your accelerometer angle and the complementary filter block which combines the high pass filter and the low pass filter angles in one particular computational block so i will show you first how ah we have the drift ah in a particular stationary position and how we correct the drift in this particular experiment so here we have a csv file which is in the in the names ah stationary dot csv i will show you the ah excel file for that so this is the excel file where we have collected data from an imu that we have so this imu ah consist of an accelerometer and a gyroscope where the accelerometer is the ad accel three four five and the gyroscope which is lg three four two hundred d so here we can see that ah how we save the data is column number a which is accel x so this is the axis of the accelerometer which is pointing forward so if we consider this be the accelerometer and i keep this stationery the axis which is pointing forward is the x axis the axis which is perpendicular in this manner is the y axis and the axis which goes through the device is the z axis so axis x of the accelerometer is in column a axis y of the accelerometer is in column b axis z of the accelerometer is in column c the same holds true for the gyroscope and ah when kept stationary so the axis x of the gyroscope is in column d the axis y of gyroscope is in column e and the axis z of the gyroscope is in column f so i go back to the matlab code and ah i read this values ah into a particular variable so from the file i get into a variable which is called data so my accel x is the first column as we know so we call it from the first column access y ah is from the second column accelerometer z is the third ah column

so we call it ah respectively the gyroscope data which is in which is the x axis gyroscope data is in column number four the gyroscope y data is in column number five and the gyroscope z data is in column number six so this is how we read the data from the csv file after reading the data from the csv file we calculate the mean bias value as professor viswanath has already spoken to you got bias ah we will be calculating the bias of these values that we have in ah an experiment where we have collected the data so we take around two hundred samples we calculate the sum of this two hundred samples and divided by two hundred so we get the mean bias which is in a stationery condition of the xy and z axis of the gyroscope this mean gyroscope value ah has to be subtracted from each of the axis now we consider only the y axis so i am i am subtracting the gyroscope mean value of y into the gyroscope y value so the bias is a basically removed so here are some experimental ah results of the bias where x axis is measured as point zero four the y axis bias is measured as two point eight five and the z axis bias is measured as one point zero two nine so here ah this is an experimental set up where we have measure these bias which may not be true all the all the time so if you want to calculate the bias we have to keep the imu stationery on the ground and calculate the bias and remove of the bias initially from the gyroscope value ok so ah next we would ah say what is the data rate at which we are getting the data from the accelerometer the gyroscope so the data rate at which we are getting is around two hundred samples per second its in the code where we actually call the data in at this ah sample rate so we get two hundred samples per seconds so ts which is also delta t is one by two hundred so this is the delta where delta t where we need to compute the alpha for the high pass filter and the low pass filters so here specifically for the high pass filter so as we know that the sensor is kept stable on stationery on on the ground on on the table so we consider the first value of the angle of the y axis to be zero and we have a for loop where we integrate the gyroscope y value as we said we consider the y axis you can consider the z axis or the x axis but here in this case we are considering the y axis so we keep the y axis ah stationery or the imu completely stationery and we calculate the angle from the y axis so ang y is a variable where we get the value of ah the angle from the gyroscope so if i plot this value of the gyroscope so here is the graph of the angle measure by the gyroscope so the y axis is ah measuring the angle the z the x axis is measuring the samples so we can say that over a period of time which is around eight thousand samples my value of drift ah my value of the angle which is when kept stationery has move from zero to around two point two five so we see that ah there is a lot of drift in this ah experiment that we conducted so how do we remove the drift of the angle computed from the gyroscope is by passing through the high pass filter so i will be telling you how ah to pass through the high pass filter as we have seen ah how to calculate the filter coefficient previously in the lectures ah given by professor viswanath i will be going through the same again so fc is the cutoff frequency where fc is equal to one by two pi rc now we calculate for rc which is your time constant tau is equal to one by two pi fc from this we calculate your tau value as a tau is equal to rc and when you want to calculate the filter coefficient alpha hp is equal to tau divided by tau plus delta t or in this case it is it is mentioned as t ts

so ah filter coefficient that we have calculated is for three hertz your cutoff frequency of this filter is at three hertz and we get the alpha value of point eight six four one so we pass this this the angle ah computed from your gyroscope through the high pass filter ah using this filter coefficient so now that we know that the alpha ah which for the high pass filter is point eight six four one we put it in the high pass equation which is defined previously which is explained you by professor viswanath so we get ah the value of the high pass signal as anggyryhp so if i have to plot this value of anggyryhp with ah i would get ah graph which looks similar to this yeah now you can see that your blue line as seen previously is your angle from your gyroscope which is drifting of even when kept stationary and when we passed through the high pass filter we get a very smooth ah line which is almost at zero all the time so here is how we correct the drift from the angle of the gyroscope by passing it through the high pass filter now i will be taking you through ah how to ah reduce or how how to reduce the jitter from your accelerometer by passing it through the low pass filter yeah so all these codes are available in your forum if you have any doubts with the codes you can always contact me ah so over here we are getting the data of the same stationary value we know that your accelerometer data is in ah column number one accelerometer y is in column number two accelerometer z is in column number three here we do not consider your gyroscope but for computational i have ah given the values over here as we note from the previous ah experiment for the high pass filter ts is your delta t and my sampling rate is around two hundred hertz so i get two hundred samples per second where delta t is one by two hundred this has to be checked properly while your conducting the experiment because every time we will not get ah standard data rate unless you have ah defined your data rates well so please check this before you use the same code now we calculate the angle from your accelerometer so the angle from your accelerometer is calculated using this formula so we have already shown to you as ah how what the formula for the accelerometer ah angle computation is so this is how we implemented in matlab so once we have ah got this data i want to design a low pass filter for this accelerometer data so as ah this as shown before i will go through the cutoff frequencies and how we calculate your filter coefficients from the cutoff frequencies so fc is your cutoff frequency where the formula of fc is equal to one by two pi rc rc is also your tau which is your time constant which can be said as one by two pi fc so now your alpha for a low pass filter is delta t divided by delta t plus tau once we substitutes the values of tau and ah delta t or ts in the equation we get the value of alpha for the low pass filter again over here the alpha for a cutoff of three hertz is around zero point zero nine zero nine so we consider this three hertz as ah for the high pass filter we have also consider the three hertz as the cutoff frequency we consider the three hertz cutoff frequency even for your low pass filter so we pass the angle computed previously from your ah accelerometer data through the low pass filter so this is your equation which is being implemented in matlab the equation is previously shown so you can ah implement this equation for a low pass filter in this manner once we pass ah the accelerometer data through the low pass filter we can see how we remove the jitter from the angle computed from your acceler meter data

so here is the graph where one caps stationary the accelerometer not moving so we need to get an angle which is ideally a straight line but as we see but the angle calculated from your row acceleration value which is in terms of red has a lot of jitter and the angle computed after passing through the high pass ah the angle computed from your accelerometer after passing through the low pass filter is in ah yellow so we can see there is a considerable amount of jitter which is being removed ah while pass through the low pass filter so now i will be showing you on how we combine low pass filter and a high pass filter to get the complimentary filter now i will be showing you how ah the complimentary filter block is design where we combined the angle calculated from the gyroscope and the angle calculated from the accelerometer in the previous experiments i have shown ah over here the basic steps are very similar so we have the data we read from the stationary file and then we ah assigned to variables which are a accx accelerometer x first column accelerometer y the second column accelerometer z which is the third column the fourth is the gyroscope x fifth is the gyroscope y and sixth is the gyroscope z so we find the mean which is the bias and we remove the bias in this way and we know that the ts which is the delta t is one by two hundred so now we need to calculate the angles from each of the ah um data it each with after sensor which is the accelerometer and is gyroscope so we found out we keep ah the initial condition from the accelerometer which is ah this formula atan two accel x divided by square root of y square plus z square and the gyroscope is also initially given the same angle which is accel x divided by root of tan inverse of accel x divided by square root of y square plus z square so now here we calculate the angle from the gyroscope in this way and we calculate the angle from the accelerometer in this way now we have the complimentary filter which is ah a ca a combination of your low pass filter with the high pa with with your ah high pass filter using a particular filter coefficients here we know that we have a stationary position so we need to trust the ah accelerometer more than the gyroscope because the gyroscope has drift so we ah choose our filter coefficients of are a complimentary filter in this manner where we choose one minus alpha is equal to nine point point nine eight and ah alpha is equal to point zero two so now we can plot this signal ah so we get it in this manner as we have seen previously the blue line is the angle computer from your gyroscope and the green line which is super imposed by the red line over here is your ah angle computed from your accelerometer and the red line is the actual complementary filter angle student: now this is them without filtering without passing each of the angles through the high pass filter or the low pass filter so lets see on how ah the dynamic change when we pass the angles calculated from the gyroscope and the accelerometer through the high pass and the low pass filters as previously calculated ah are a filter coefficients for the low pass filter and the high pass filter is are follows the alpha for the high pass filter is previously calculated us point a zero point eight six four one and for the low pass is zero point ah zero nine zero nine we have seen that in the previous experiment as as to how to calculate the alpha values the initialize the values which are required for the computation of

these angels now we ah we compute the angles of the accelerometer using this formula after computing the angle we pass these this angle which is angaccel one inth ah into your ah low pass filter where your accelerometer data of the x y and the z is passed through the low pass filter next we compute the gyroscope angle in this way so the gyro angyro is ah added with the gyroscope y value and multiplied with delta t where the numerical [nu/numerical] numerical integration is the gyroscope into delta t which gives you ah angular position so now we we pass this gyroscope value through the high pass filter the high pass filter of the gyroscope ah the angle calculated from the gyroscope is this equation so the equation ah is already previously mentioned where we pass the angle from the gyroscope through the high pass filter now we see that ah this two have to be combined using the complimentary filter so the filter coefficients from your complimentary filter is alpha is equal to point zero two and the one minus alpha cf is equal to point nine eight so now we can see ah the difference in the graphs that we get with pre filtering and without pre filtering from the high pass and the low pass filters yeah on the left side we see ah a plot which is the angle calculated from your gyro which is the blue ah graph the angle calculated from your accelerometer is super imposed by your complimentary filter which is the red graph but the green graph is your angle ah computed from your accelerometer the red graph over here is the complimentary filter angle so here we do not pass the angle from your gyroscope or the angle from your accelerometer through the high pass or the low pass filter is respectively we just ah pass it we just combine these two angles directly i have to get the complimentary filter and the right side we have ah the angle from your accelerometer which is in green the angle from your gyroscope which is in blue and the angle from your complimentary filter which is in red again here we pass the angle computed from your gyroscope and your accelerometer through the high pass and the low pass filter and we combined these two to get this ah complimentary filter value i will tell you why the gyroscope value is ah towards zero that is because we see over here that the gyroscope is basically stationary so now the moment or ah what ah the angular velocity which is present in the gyroscope is very low so we pass it when when we compute the angle it is basically a stationary angle with lower frequency dynamics as we see that ah the angles computed ah from ah the gyroscope when pass through the high pass filter the lower frequency which is below the cut off frequency is actually filter out an attenuated hence we get the value ah which is close to zero because all the frequencies which ah are present in stationary is actually attenuated by the high pass filter and we get a zero angle so now we can see that in a stationary condition where the the sensor is not moving we can ah trust the accelerometer more than the gyroscope so now ah we have go we have seen the complimentary filter ah where we combine the angles ca ah computed from the accelerometer and the gyroscope ah which is pass through the low pass filter and the high pass filter respectively in a stationary position now i will show you an experiment where we have actually ah taken the gyroscope which is strapped on to the hand and we move the act the gyroscope and the accelerometer which is ah sensor which is over here in zero to ninety degrees ah manner and bring it back to zero degree so it goes from zero degrees to ninety degrees and back all the way to zero degrees so here is a file where ah which is called moment out zero i will i wont run through that code because it is a same code i will just show you the graphs so the graph on the left which is your figure one is ah where we find the angle from your accelerometer and the angle from your gyroscope

without passing through the high pass and the low pass filter and combining these two angles together in a complimentary filter so here we can see that ah the blue line is the angle computed from your gyroscope the green line again which is super imposed ah by the red line is your accelerometer angle and you see that the red line is your complimentary filter angle here we can see the value of drift your ah we can see that though the angle computed from your gyroscope is drifting in a way where we get peaks from zero to around sixty degrees but at sample number seven thousand we actually start ah the va the angle value from sixty degrees to around hundred and twenty degrees so here we can see the gradient in which there is the drift so now again on the right side we have the angle computed from your gyroscope which is the blue line the angle computed from your accel which is your green line and the angle computed from your complimentary filter which the red line here we can see that so once we compute the angles and pass it through the high pass and the low pass filter respectively or we get this kind of a response from the complimentary filter which is your red axis after computing the angles we see that we are trusting the accelerometer more than the gyroscope but ah um we can we can play around with the alpha value of the complimentary filter to trust either the accelerometer or the gyroscope you would trust the gyroscope more in ah past dynamic moment so an example to show you how we can trust the gyroscope more on the left side we can see that till sample number three thousand we have a slow speed motion where ah we do not move very rapidly we have ah ah value where we compute in a very slow manner on the right half where we start from around six thousand five hundred to eight thousand we have past dynamics so here if i zoom in to the plot you can say that the frequencies ah above three hertz also are captured from ah what is compared previously there are the dynamics of the past ah motions which are also captured so here we can actually compute ah the complimentary filter by trusting the gyroscope more so now that we see the ah the higher dynamics ah on and for after gyroscope where the gyroscope can be trusted more i requested you to play around with the alpha of the complimentary filter where if you trust the gyroscope more where the alpha cf is given a higher weightage towards the gyroscope the complimentary filter will actually follow the gyroscope so i request you ah to please try by changing the alpha for the complimentary filter if you have any doubts ah you can contact me on the forum all these codes are ah posted over that you can get in touch with me thank you all the best