Raspberry Pi Pico W LESSON 50: Removing Long Term Steady State Error from Sensor Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy dcom and we're here today with episode number 50 in our incredible new tutorial Series where you're unleashing the power of your Raspberry Pi Pico W what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over eyes no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the Kepler kit for Raspberry Pi Pico W now most of you guys probably already have your gear but if you don't look down the in the description there is a link over to Amazon you can hop on over there and pick your kid up and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and what I'm going to do is I'm going to give you the homework solution for the problem that I gave you in lesson number 49 and what was that problem that problem was that we've shown we've gone through kind of like four lessons trying to get better and better and better performance from our tilt meter that we can measure we can measure pitch and we can measure roll but we want to continue to refine the results so that we get the most elegant solution possible and what I showed you last week is I showed you that you can create a complimentary filter and the complimentary filter will sort of combine the the the best of Both Worlds of the accelerometer and the gyro data it'll it'll remove the low it'll remove the high frequency the high frequency noise from the accelerometer and it will remove that long-term drift or that longterm uh low frequency error from the gyro it'll combine those two things and it will create a result that is fast and accurate and low Noise Okay and we were successful with that but what we found when we got that stupendous result we found that if we zoomed in zoomed in zoomed in we know that if it's sitting still the right answer is the accelerometer data the result from the accelerometer and we saw that after going through the filter there was this little tiny error between the exact answer on the accelerometer and the improved filtered answer coming from the complimentary filter and so what your homework assignment was was to drive that long-term steady state little tiny error to zero were any of you successful if you were leave a comment down below I Am Legend double just bump and if you weren't I fold it up like a cheap Walmart lawn chair okay now these are things that you're taught in college a lot of times like at a master's of Science and electrical engineering kind of at a master's level you start getting into this stuff but what I want to show you is it's it's really pretty simple I'm going to explain it to you in a way that you'll see it and understand it and then you'll have a new tool in your tool kit when you run into this same problem on other sensors and other projects how do you drive that last little bit of steady state error to zero if you are doing a PID motor controller you're going to run into this if you are doing something where you're trying to do a control system to control the temperature accurately in a hot water heater just almost any type of control system or anything dealing with sensors lots of times you run into this problem I want to kind of show you a solution to it okay I'm going to show you a solution to it okay but in order to do that I will need to get out of your way and then I don't want to start from scratch so let's start where we ended up up last week where we had created the the uh pitch and uh and roll measurement that had the complimentary filter on it so I need you to go to the most excellent www.top techboy do.com I need you to use this this happy little search icon here and I need you to search on something like improving accuracy of the mpu6050 data this is the schematic of how we have our circuit hooked up and then this is that Dandy program that we uh that Dandy program that we developed last week so I'm going to get that Dandy program now we are going to come over here to th and I'm going to paste it in okay that looks good and now we're going to run it just to kind of get reoriented to how well this works how elegantly it works but then the one minor little problem that remains and so I'm going to run it and there it goes and it should Auto scale here and then it should calm down a little bit what I really want to uh what I really want to compare is I want to compare let's see I think it is the pitch of the accelerometer which is orange and the pitch of the uh uh uh the compensated in red and so you see between the orange and red there is a tiny bit of error and that error isn't going away but now if we actually look at pitch you know we're really doing doing a good job in Pitch you see they're they're tracking and particularly if we look at that uh particularly on Pitch if we look at the red one okay when I shake it you see there is no noise on the red the red is Rock Solid the Orange is the accelerometer and it's got the Noise Okay so with the red we have filtered out that noise and when we look at the red there's no long-term drift there's no short-term no noise there's no long-term Drift But what do we have we have this tiny steady state air that doesn't seem to be going away now we could play with our parameters and we could kind of get it to go away like we can see that to get that to go away we probably need to give a little bit more weight to our anchor the accelerometer and so like if we put this at 0.1 we put this at 0.1 and then we went 099 on the other ones now we're probably going to get rid of that long-term steady state drift or we're going to really eliminate it quite a bit or reduce it quite a bit so now we can see that the difference between the orange and the red is a lot less okay it's a lot less Now by playing with those parameters okay you see we've almost got it to go away but now what you can see is in doing that I've Incorporated now a a little bit of error I've uh Incorporated a little bit of error in that red in the uh compensation so what I want to do is I want to go back to 0.005 and 0.05 and then I want to go 0995 and I want to go 0995 and I just want to tell you and you can try it if you don't believe me but you can never really drw drive out that steady state error you can never really drive out that steady state error just by playing with these parameters now you can see it doesn't look like on the roll it doesn't look like you have one right it really doesn't look like that you have an error on the roll but let's see if we can look into that a little bit further and if I come here and this time let me get this I'm going to copy it and then I'm going to paste it and then I'm going to comment it out and this time I want to look only at roll so I'll look at roll on accelerometer I'll get rid of this and then I'm going to look at the roll compensated I'm going to get get rid of this so we're only looking at the two roll values now I'm going to go like this okay and what do you see even in roll there is about a there's about a half a degree of error okay do you see that the uh do you see that that orange is not being perfectly on the blue even though there it looks like it it's because it's autoscaled let's look at it again even here you see there is a long-term steady state error that is not going away between the compensated data and the uh actual data from the accelerometer okay so sometimes it's bigger sometimes it's smaller but we want to get rid of it okay we want to get rid of it so let's go ahead and put our old print statement back in there take that out let's look at it again just get a clean run okay you see we've got a longterm steady state error between the red and the orange the red is the filtered beautiful fast accurate low noise no drift value and the Orange is the actual exact value and there is an error between those and we want to drive that error out we want to drive that error out okay how are we going to do it well let me kill it and let's just say that you can't do it by playing with these parameters these parameters are affecting our filtering of the high frequency data and they're affecting the filtering of the long-term drift that low frequency drift and the high frequency noise it's dealing with those two but it's not dealing with a constant steady state error and so what we've got to do is we've got to see that I have got to incorporate this concept of error okay to deal with it I've got to I've got to introduce this idea of error and I have to distinguish between an error that might be a noise error an error that is just a long-term drift we've dealt with those but I've got to deal with the steady state error now how do I know if it's a steady state error if it's a steady state error it stays and so how do I know that I have an error that stays I'm going to just keep every time adding the error up every time through the loop I'm going to add the error and if that error is building up then I'm going to squeeze it down I'm not dealing with the error like this I'm not dealing with the drift error I'm dealing with the error that is constant so how would I do that well I'm going to introduce the this concept of let's take the error in the pitch the error P because that's the big one that's the one that's the problem and then what I'm going to say is I'm going to say that that error in Pitch is going to be the pitch accelerometer which is the actual pitch okay minus the pitch compensated which is the one where we've taken the high frequency vibration out and we've taken the long-term drift out now I am left with what error P well what's the problem if I just look in the middle and I get that error how do I know that that error isn't vibration how do I know that error isn't long-term drift well what I want to do is I want to add it up so I'm going to take the error and I am going to multiply it by how long have I had that error I've had it for the amount of time since the last time through the loop so T Loop okay so if I have that little error and I Loop and I Loop and I Loop and it's still there what I'm going to do is I'm going to add it to the error P all right now what is going to happen here what is going to happen here with this error if it's a noisy error then error p is just going to not matter it's going to be close to zero because it's big this time it's little this time it's going to average out to zero but if it's sitting there constant it is that error times the delta T plus the error times the delta T plus the RR times the delta T it's growing and initially it's little and you don't know but as it gets big you've got to deal with it okay so this error it's the integrated error the sum of all the errors they're building up and what have I got to do I've got to squeeze them down well let's go ahead and let's do the same thing let's do the same thing but for the error the error in the roll and the error in the roll and this would be roll accelerometer and roll compensated and so now what I've got is I've got this bag that I'm carrying and that bag gets heavier and heavier and heavier because every time I'm adding you've got error you got error You' got error okay now I've got to squeeze that error out and I'm going to come up here and whatever that error is in roll okay I'm going to compensate that I'm going to compensate for that by adding an error correction term and I will say that is error times P that I mean error P that's my error in my pitch and then times let's just say 0.001 like that okay so I don't want to squeeze that error out you know that that that constant error I don't want to squeeze it out all at once but I want to kind of drive it out I want to squeeze it out if I say 0.01 100 times through through the loop it will squeeze it out but that way I don't start impacting The High Frequency stuff and the other stuff that I've just done after all of that is done now I've got this squeeze function that is going to squeeze out this integrated error right I've got this integrated error that I'm carrying on my back that's getting heavier and heavier now I'm starting to throw it away away and I'm going to keep throwing it away until what until it is not there anymore so it's going to be that and then same thing here it is going to be plus error that was error pitch okay I did that wrong that would be error roll and this would be error pitch times 01 now notice this and this have to equal one because that is fusing the data between the accelerometer and the gyro to have something that is fast something that is accurate something that doesn't have drift and something that doesn't have noise but after doing all of that I end up with this annoying little error error now I'm going to come out here and I'm going to squeeze it out by taking 1% of the error one% of the error and then driving that out each time through so remember before before the pitch had a long-term steady state error of what it was about it was about 3 or 4 degrees it looked like and so now let's see what happens and we want to keep our eyes on Pitch okay because the pitch was the orange and the red okay so that orange that compensated should be pushed up now towards the red so let's take a look at it ah error R isn't defined oh yeah yeah yeah yeah yeah yeah yeah what have we got to do right this was one of those error RS plus error RS right so we have like the uh error P was the old error P the error R was the old error R we've got to set those we've got to set these up here at the very top so we're going to say error R equals 0 and we're going to say error P equals z and that way it won't crash the first time through so now let's run it again okay look at the Orange and the red you see how they are a part part but what's happening the red is chasing the orange do you see the red is chasing the orange and now what's happened we've driven that error out between the orange and the red how the red chased the orange and it says I don't like this burden I'm carrying of the sack of errors I'm going to start throwing it away and so let's watch it again let's watch the red and let's watch the orange okay look orange and red are what like 3° apart the red chased the orange it passed it it's coming back it passed it and now it is stabilized boom just like that what do you also see about the green and the blue the green chased the blue and the green put itself in the middle of the blue okay you see green and blue watch that the green is chasing the blue overshot it it came back overshot it came back Boom the green caught the blue and the red caught the orange now let's see if this thing still work so I'm going to come up and let's just say I'm going to pitch it okay I pitch and look at this right the compensation is still working you're looking at the red that's the compensated and look at that no noise in the red right no noise in the red I guess I should show you how I'm tilting this thing I should show you how I'm tilting this thing so I'm going to pitch and look boom red and orange are exactly the same but now when I introduce a vibration red is Rock Solid why because Red's not chasing that error it's only chasing the error o over time it's only chasing that steady state air now I think if I drop this I will introduce an error because I'll move it faster than it can make the measurement and so when I do that I think that you'll see an error between red and orange but then I think you'll see red Chase orange again so I'm going to bring it up and then boom look red is chasing the orange it passed it it's going to come back and then it kind of zeros in on it boom red caught orange red caught orange okay now you could imagine that you could affect these things so you see that you see watch Now red now when I come down it takes a while for red to catch Orange right it takes a little while for red to catch orange to drive that out so what could we imagine doing we could imagine coming down and instead of dropping 1% of my error 1% of my accumulated error every time what if I dropped 10% of my accumulated error every time do you think that's going to uh orange is going to catch red or Red's going to catch orange faster or slower okay now you're probably going to think faster but watch Red Chase orange okay it's going to have to rescale okay red is chasing orange but what's it doing it's going so fast it's overcompensating let me try it again Watch red and and orange red red is going to chase orange and look red goes over then it goes below now it's above now it's below and so we tried to make it go faster but it's what it's unstable it's unstable all right well let's try let's try something else so instead of 0.1 let's say 0.001 and 0.001 now it's going to be what it's going to be very very stable Red's going to Chase orange okay look red is going to chase orange red is chasing orange red is catching orange red is catching orange red is catching Orange it's very stable and it comes up red is chasing orange and then it just comes in perfectly right into the middle it didn't go above and below and above and below it just came perfectly up but what did we not like about that it was slow so we want it to go faster so 0.0 1 is very smooth but it's too slow and 0.1 is very fast but it overshoots and oscillates and so what seemed to be kind of like the magic one it was about 0.01 like that okay let's run that okay Red's chasing orange and it comes up it overshoots a little bit and then it unders shoots a little bit and then it's just perfectly on there it is perfectly on there maybe I would slow that down a little little bit what if we said 0.05 0.005 and 0.005 and maybe it would not quite overshoot okay red is going to chase orange red is chasing orange and it comes up it overshoots one time and then there it has it and so maybe 0.05 is a 0.005 is a pretty good value for having that so now let's see what our over overall performances of our sensor I am going to roll and boom it comes up and the airor is driven out okay I come down it comes up and that long-term error is driven out I roll it's tracking it perfectly I roll it's tracking it perfectly when I shake you see green stays steady it ignores the shaking of blue okay green stays steady and now I'm going to pitch and and then you see orange is a mess but red stays steady and then if I drop it it gets a little bit of air but what the red chases the error out and goes to Orange does this make sense do you understand the difference between the three types of errors that we have and how we deal with them differently with shortterm noise we apply a what a low pass filter with the long-term drift we apply a what we apply a high pass filter okay when we have some element of both of them we do a complimentary filter where we take the best of the accelerometer data and combine it with the best of the gyro characteristics and then we get something really good but then we get annoyed that when we do all of that we end up with some long-term steady state error and so what do we do we drive that error to Zero by adding in this correction factor by adding in this correction factor and then we get everything working perfectly man do you guys do you guys hate me now it's like how many of these lessons did we do how many how many of these gyro lessons did we do I'll see if I can tell you we went back to the uh we went back and it was uh it was like lesson number 38 that couldn't be right we couldn't have done 12 lessons we couldn't have really done 12 lessons on the mpu 6052 we I'm sorry I'm sorry that I got sidetracked with this but what I want you guys to see is it's not about the mpu6050 it's not about tilt and it's not about uh role what it's about is we live in the real world and when we live in the real world we get real world data and that real world data often has noise in it and that real world data often has drift in it and that real world data often has steady state errors and I'm showing you the techniques to drive those three different types of errors out and get something that is really cool and clean data like some of you guys have been talking about uh maybe something that would open and close your garage door or warn you if the garage door was open and you wouldn't want that that mpu 6050 to vibrate and then your door just start going up and down right you got to clean the data up and so I'm showing you that in real world applications you've got to be able to do that Okay I lied there's going to be one more lesson on the mpu 6050 and what I want you to do now is I I want you to come in and I want you to use this improved best version of roll and pitch and I want you to go back and make your little tilt meter now where you've got the reticle and the and the crosshairs and the the little bubble moves in response to the uh the pitch and the roll and so now bring it all home by putting that on here and having a deluxo probably over engineered and no one has ever built a level this accurate in the history of the universe but put it all together and get the display going and then we will call it a wrap on this okay guys I I really hope you don't hate me I really hope you don't hate me but it's just I want you to learn these things and if I just created a lesson that said okay here's how to do a low pass filter you're going to wonder why and then you're not going to remember it because it didn't matter so I taught you a low pass filter in the context of a real problem where you had noise data and needed to get rid of it and I showed you how to get rid of it I showed you how to do a complimentary filter why because you need to get rid of it I showed you how to get rid of a long-term steady state error because you need to get rid of it so you'll now know how to do it and you'll remember it and you will know that it is important I do hope you guys are having as much fun taking these lessons as I am making them I want to give a shout out to you guys helping me out over at patreon you're the ones that are keeping me in the the game YouTube has made it clear that they don't like grumpy old guys making long videos they like cute young girls with 20 seconds in front of a video short that's the direction YouTube's going you guys that are helping me out on patreon you're keeping me in the game and I thank you for that you all can also help me by giving me a thumbs up also helps if you leave a comment down below if you've not already subscribe to the channel when you do make sure you ring that Bell so that you'll be notified When Future lessons drop and as always share this video with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul mcorder with toptechboy tocom I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 3,261
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: VdTBBUKH43k
Channel Id: undefined
Length: 28min 46sec (1726 seconds)
Published: Tue Jan 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.