Raspberry Pi Pico W LESSON 48: Measuring Rotation Using the Gyros on the MPU6050

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 48 in our incredible new tutorial Series where you are unleashing the power of your raspberry pie 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 ice no sugar no sweeteners none need it 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 picw now most of you guys here at lesson number 48 probably already have your gear but if you're one of the slow to order guys look down in the description there is a 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 show you my solution to the homework assignment that I gave you in lesson number 47 now remember where did we end up with in the last few lessons well using the mpu 6050 and using the three accelerometers on the 6050 we were able to create a little Gadget that would measure a board it would measure its pitch and it would measure its roll so it would measure its tilt angle along two axis it would sort of be like a carpenter's level but instead of just looking in one axis it would look in two axis and we even came up with a cool little graphic on the uh on the OLED 1306 where we had a little bubble that was moving around in a reticle showing you what your tilt was giving you kind of both quantitative and qualitative feedback and so that was really a cool little project but what was the problem that we found the problem that we found is is that we could very accurately measure tilt but because we were measuring tilt using an accelerometer if our device experienced vibration or if our device experienced deceleration or acceleration it would interpret all of those things as tilt so we have our nice accurate tilt measurement and then we superimpose on top of that an error signal from either vibration or acceleration or deceleration so what we worked on last week was we worked on removing that noise signal or mitigating that noise signal using our what our friend was who our friend was a low pass filter so we incorporated a low pass filter so we could remove that vibrational error signal from from our readings well if we did that why do we even need to go on we could get rid of we could get rid of the error signal but what was the challenge that we faced we faced a tradeoff between the more we squeezed out that error signal the more sluggish our sensor became so we could be very very accurate and very very low noise but it was very slow to respond to tilt or we could have it behave Snappy in a Snappy fashion and then we would still have some of that noise in there so we were trying to find that that sweet spot in the Middle where we got rid of most of the noise but we uh but we were still Snappy enough so we had to make tradeoffs well where Engineers do we like tradeoffs no we hate tradeoffs what do we want we want something that is fast and stable okay fast and stable and that's what I'm going to kind of start working on today the low pass filter was a huge step forward we'll probably keep using it as part of our solution but what was the fundamental problem the fundamental problem was we were measuring tilt using an accelerometer and an accelerometer measures Accel ation it's kind of like a fundamental error that we built into our approach well I ask you this how do we go the next step well I want you to think the mpu 6050 we call it a what a 6axis IMU or Sixaxis inertial measurement unit or a Sixaxis mpu for motion processing unit six axes how many axes have we used three we're looking acceleration in X acceleration in y and acceleration in Z this bad boy has three other sensors that we're not using it has an x-axis gyro a y AIS gyro and a z-axis gyro okay there's three more sensors that we aren't using the accelerometers measure what they measure acceleration the gyos measure what they measure rotational velocity how many degrees per second something is rotating about the xaxis how many degrees per second something is rotating around the Y AIS and uh thirdly how many degrees per second something is rotating about the Z axis and that gives us new Tools in our toolbox to make our little Gadget work better what do we want low noise and snappy okay well let me get out of your way let's talk about the accelerometer a little bit we've got some fishermen out there working this afternoon I love looking at the window and and seeing seeing the fishermen but enough of this gazing off into the distance at fisherman let's come over to our sketch pad okay so now what do we get from the ex from the Gyros what we get is a measurement of the Velocity the angular velocity so it might tell us that we are rotating and we've got three different numbers we've got the the velocity uh the angular velocity about the xaxis we have the angular velocity about the Y AIS and we've got the angular velocity about the z-axis okay what is angular velocity well it is how fast something is rotating in degrees per second so if I am rotating at 10 degrees per second for 1 second how far did I go well 10 degrees per second times 1 second would be 10 or if I went 10° per second for 2 seconds I would have rotated 20° okay hopefully that makes sense so these are all telling you about the axises how many degrees per second you are spinning now what I don't want to understand tilt I don't want to know how fast it's rotating I want to know what the angle is well what is the angle the angle is going to be the angle is going to be the angular velocity times the time now life would be easy if we were like a clock and just sitting there rotating at a constant rate but that's not the way the real world works works right we're rocking and rolling and we're moving around and our angular velocity is not constant so how do we calculate an angle in that case okay well let's say that this is the angular velocity and understand you solved the problem three times once using the xaxis acceler uh gyro once using the y-axis gyro and once using the z-axis gyro okay so if you do one then the other two are just the same you just swap out which you know which gyro that you're using but let's say that I am rotating at different angular velocities over time well what we say is how do we actually get the actual angle okay and I can't just multiply it by time because it hasn't been at that velocity for the whole time so what do I say I say the new angle is equal to whatever the old angle was an instant ago plus the instantaneous [Music] velocity okay times not t but delta T that is the time between this measurement and the last measurement so I make a measurement here and I make a measurement here and I make a measurement here and I make a measurement here well what Theta is going to be is it's whatever Theta we were at before and then what do I do I look at multiplying delta T okay that little tick in time that little tick in time times my velocity so it's this velocity times this time and now I take another measurement and then I do it again I take where I was before and then I add this times this and so you're taking little bitty baby steps little Delta T's times your last velocity and add it to what the angle was you were at before and with that then you can take the angular velocity that is changing and you can come up with your tilt angle over time does that make sense I hope it does actually what we did there we just did a little calculus I didn't want to tell you it was calculus because that would have scared you one day one day I will show you how to do calculus okay but this is not that day we're just going to jump back over here and I think we're ready to actually start coding now I think we're ready to start to start coding uh you know I think we yeah let me let me cheat a little bit let's come up here I think we can get a quicker start if we come to the most excellent www.top techboy do.com and we're going to look up the lesson measure your pitch and roll with the mpu 6050 remember this is the schematic that we're using so you can get things hooked up like us and this was how we were measuring the Tilt how we were measuring the Tilt using the accelerometer but we're going to kind of cheat and take that and then we're going to come over here let's see getting a little bit of a glitch there let me kill that sometimes th gets a little a little contener there and I find the best thing is just turn it off and start over Okay so now I'm going to paste that code in DG G it I let me try this again this is not cooperating very well is it okay we're going to do it the oldfashioned way and just grab it like this sometimes it doesn't get that copy right from the website and when it does you can just do it like that okay so now that works this is just let's just make sure that this works this should just be giving me pitch and roll this should be giving me pitch and Roll Just based on uh just based on the accelerometers and so let's try this and just [Music] see I've got to hook up to [Music] the I guess I didn't get the first line of that when I copied it so I'll add it up here from IMU import mpu 6050 so I'm having trouble copying and pasting today and that's pretty sad isn't it okay 6050 okay there it is now as I let me make this full screen okay as I roll it's rolling I'll show you here as I roll it's rolling and it's giving me accurate measurements as I pitch it pitches okay and so if it's doing that what's the problem right what's the problem well the problem is that when I shake it it's interpreting that vibration it's interpreting that vibration as a tilt as a roll or a pitch okay so that's what we want to get rid of so now we're going to kill this really we just copied this program because it's got some stuff that's kind of useful in it so we come up here and this top part is setting things up this is setting up our mpu 6050 yeah we want to go ahead and say roll is zero pitch is zero and I also want to incorporate A New Concept remember this was Pitch positive pitch is up negative pitch is nose down then I've got roll left and I got roll right so I've got Pitch and I've got Roll But I want to introduce A New Concept and that is yah that's turned this direction you can't measure yah with an accelerometer but you can approximate it with gyro so we're going to get a little bit of data that we didn't have before okay so what I'm going to do is I'm going to say roll is zero pitch is zero and I'm also going to set yah equal to zero like that so we've got that thing all set up and then what I'm also going to do is I'm going to come in here and then instead of having X acceleration I'm going to have X gyro instead of Y acceleration I'm going to have y gyro instead of Z acceleration I'm going to have g z gyro like that and then I'm going to have instead of measuring mpu Excel X it's going to be mpu gyro X mpu gyro Y and mpu Gyro Z what is XJ it is the rotational velocity in degrees per second about the xaxis what is y gyro it is the rotational velocity in degrees per second about the Y AIS what is MP what is gyro what is z gyro that is the rotational velocity about the Z axis in degrees per second does that make sense and so what I want to do is I got to get rid of all of this nonsense here I just didn't I didn't not much of that at all was very useful was it okay so let's get rid of all that and then let's even get rid of this but at least we're making the three measurements now so let's go ahead and let's just print those three measurements okay so what I'm going to do is I'm going to print just the raw data coming off I'm going to print the raw data coming off and that raw data is going to be xjo comma yeah that's going to be let's put in a label xjo and then comma X gyro and then y gyro comma y gyro and then comma Z gyro comma Z ch Arrow so I should get three measurements and I should see them coming out across my plotter here so let's go ahead and run this and just see if I get data what is this invalid syntax in line 18 what is this nonsense uh need a closing parentheses now let's try it hey I'm getting measurements and let me let me kind of shake this around a little bit so as it's sitting there what is my rotation velocity my rotational velocity is 0 0 0 why I am not rotating okay now it is the uh the green one is the rotation about the Z AIS and that's the yah let me see if I give it some yah if it sees a signal in the green so I'm going to turn it and what it read counterclockwise is positive and clockwise is negative okay clockwise is negative counterclockwise is positive and what do I see now what I want you to see is if I go from here to here I see a signal if I go from here to here I see a signal but it always goes back to zero why I'm not measuring position I am measuring velocity how do I get position by adding adding adding every little delta T time the last measurement so I take the rotational velocity times the delta T since the last measurement and I add to that the rotational velocity times the delta T since the last measurement and then I should just be able to add up all these numbers and I should be able to get what the actual position whether we're talking about Pitch roll or yah okay is that pretty cool I hope it is so let's come in here and what I have to do is first of all I need to create a stopwatch because I need to know how long it has been since my last measurement and that's going to kind of be my Loop time so I need to start my stopwatch at the first of the loop and I need to stop my stopwatch at the end of the loop and I'm going to go ahead and so it doesn't crash the first time I'm going to just say the T Loop I'm just going to set the first one equal to zero just because it has a value it has a place to start and then what I'm going to do I'm going to start my stopwatch by saying T start is going to be equal to time. tick in milliseconds like that what does that give me the number of milliseconds since the epoch and that is not s start that should be t Okay so that starts it and then I'm I'm going to come down all the way to the bottom and then I'm going to say t stop is equal to time do time underscore underscore time dot time time. _ milliseconds like that okay and now T Loop is going to be equal to T stop my minus t start like that now what you got to see is this is reporting in milliseconds and we want to do our calculation in seconds so we need to convert milliseconds to seconds and we do that by multiplying by 0.001 and now what we should have is this is what is that got an extra one there okay now what is this going to do that is going to be the time that it goes through the loop that is our delta T that's our little increment of time which we multiply by the rotational velocity and we add it to our last angle okay so what are we going to do here let's do yah first let's just see if we can do yah okay so I'm going to come up here and yah is about the what it's about the Z axis that's the one that we couldn't do before right the Z the z-axis comes down like this straight down and we're rotating about Z like that and we couldn't do that with just the accelerometer so let's come in and now what am I going to say I'm going to say yah which is an angle that's how far I've rotated that's an angle yah is an angle rotated about that rotated like that okay yah is equal to the what the old yah plus my rotational velocity which was Z gyro times what delta T what is our delta T T Loop the amount of time since the last measurement okay now let's see if we can just print yah like that could it really be that easy could it really be that easy no it's not quite that easy okay it's not quite that easy what you got to see is this is very very dependent on accurately getting those measurements and what we've got to see is that print statement when we do print print print that print statement is kind of a slow that print statement is kind of a slow step okay the print statement is kind of a slow step and so what I want to do is I don't I want to measure every time through the loop but I don't want to print out every time through the loop so I'm going to come up here I'm going to create a counter and I'm going to say count is equal to Zer and then I'm going to come down here here and what I'm going to do is right before I kind of do the the whole uh right before I do the whole calculation of the T Loop what I'm going to say is if count equal if equal 50 then if count equal equal 50 what am I going to do and I probably up here should increment the counter CNT equals CNT + one like that so I increment the counter and then I only want to print every 50th measurement so if count is equal to equal 50 then what I'm going to do is reset the counter count equals zero and then I'm going to print yah so I only print yah every 50 measurements and it's still going to be pretty darn fast okay and why do I have the 0.05 here of sleep just so I'm not hammering that mpu6050 faster than it can make measurements okay so I'm going to need everyone to hold their breath we're getting data okay and I want to give it a little bit of a little bit of a read there ah I'm doing it the wrong way let's just start over here I was rotating the wrong way I should have been yawing okay so I'm going to start at zero and then what I'm going to do is I'm going to yaw to 90 and then I'm going to yaw back to zero okay so let's run this thing okay starts at zero I yaw to What minus 9990 I yaw back and it comes back to zero let's try it again okay do you see that I am measuring yah okay I am measuring yah look at that boom giddy up how cool is that we couldn't measure yah with the accelerometers but now I'm measuring yah so what do we want to do what do we want to do we also want to measure pitch and roll okay and so we've made these measurement months but now we just got to convert them to roll pitch and yaah so roll is going to be equal to roll plus roll is this way and that is the Y AIS so that would be mpu mpu Dojo doy I sure hope I'm thinking about this right and Pitch is equal to pitch Plus mpu do g.x if I'm thinking about it right so let's just see and then what we're going to do is here we are going to print we're going to print I always think in terms of roll pitch and yaah like that and why do I Define it you see to me I'm thinking of this as an airplane so this is the nose of the airplane pitch is like this roll is like this and yaah is like this so I kind of have this mentally an airplane in my mind if that makes sense okay so let's take a look at this and what I'm going to do is I'm going to come in and then I'm going to yaw and look at that I yaw just beautifully okay now pitch what okay pitch is am I doing pitch yeah that was pitch and now roll there is something very disturbing here okay this was a this was a mistake even here I should take that out and I'm going to go because I only want to print every 50th one so I said I was printing every 50th one but I was printing every time before so now I'm going to print roll pitch and law yaw and I'm actually going to put labels in r r p and yeah the labels will help us because since we've got three we want to since we got three we want to kind of keep track of them like that okay so now I'm going to try to make it where you can see more of what's going on and then when I run this thing which when I run this thing wow what is that crazy slow business for some reason it doesn't like every 50 what if I print every 10 times like that let's try that that is still just crazy slow what if I take the time to sleep out is there anything else that's slowing me up in here okay I'm going to stop this I don't know I'm going to save this save as and I'll save it as gyro2 sometimes it's like that USB port gets confused and it goes real slow and so let's just kill it and uh let's see if I can open th [Music] again open and it was gyro 2 okay and let's see let's see what happens this time okay now it's going nice and fast okay and so we got kind of a crazy value there on the uh roll the pitch the pitch the Orange is crazy so let's see what's going on there roll pitch and yaah and then we have uh X gyro y gyro Z gyro and we've got yeah I see I see I think that roll is mpu chyro I forgot to multiply by the tloop times T Loop it worked on yah but then when I did Pitch I didn't put the times T loopin hopefully you guys saw that okay so now yaah pitch and roll and really what I need to do is it would be better to use the measurements that I just made I'm not exactly sure what I was thinking when I did that but I wasn't thinking real clearly so roll is about the Y AIS so that's going to be y gyro and then pitch is going to be X gyro let's try that I might still have a little glitch in here somewhere but let's see what happens okay we should now have this set up where we should be able to see roll we should be able to see Pitch and we should be able to see ya all three axes so I'm going to turn this and I will then start by I will start by yawing and then pitching and then rolling and then see what happens okay here we go are you ready and you'll be watching the data and you'll be watching what my hand's doing and so we're going to come in and I am going to run it and then we are going going to look at that we are yawing we are pitching and we are rolling look at that so you see I can go and it is tracking the position in all three axises and so that is very good now what else do we like about this let me try this again okay what let me try this again so I'm going to run it and then I am going to yaw look at that I'm yawing I'm going to roll very nicely I am going to pitch very nicely now watch if I shake this thing I'm really if I rotate it shake it yeah I'll get some noise but if I just do it linearly and not rotate it what I'm not getting any noise and so what do I like about the gyro compared to the accelerometer what I like about the gyro compared to the accelerometer is I don't have any noise due to vibration okay what do I not like about the gyro now I was distracting you with everything moving but what is something that you notice what is something that you notice now the second one the Orange is the pitch that is this one right okay what is happening to that pitch value am I pitching right now no but the angle is slowly changing I don't have a noise problem with the Gyros but I have a what I have a drift problem and let me show you that drift problem I'll stop and watch I'm not going to touch it because I don't touch it it should sit and it should read what a roll of 0 degrees a pitch of 0 degrees and a yacht of 0 degre but watch me run this okay oh look it's zero that's great oh and what do we have on the orange the pitch is what Off to the Races and also the roll is Off to the Races the green is Off to the Races the Orange is Off to the Races amazingly what we see is is that the Z which is the yah it seems to be fairly stable but I've got very large drift in that that uh in that pitch and in fact the pitch seems to be drifting at about a degree a second no noise but a drift what is the difference between drift and noise noise is something that's on top of the signal that's going up and down like that that's noise what is drift drift is when you're not moving and you're sitting and your values just drift off and again look at that it's saying that I've moved this thing 180° it's saying that I did this with it when I didn't do that at all now let's think back about tilt from the accelerometer lots of noise but no drift right it would sit there with noise but there was no drift at all it was Rock Solid drift wise but it had noise what about the gyro it has no noise at all but it has drift what do I want I want to measure tilt that doesn't have any noise that doesn't have any drift and is very Snappy so what do we need to do we need to come up with a clever way of taking not just do it with the accelerometers alone not just do it with the Gyros alone but to somehow magically combine or fuse the data from the Gyros and the accelerometer and maybe even on the accelerometers throw in that that uh low pass filter and let's see if we can get something that is Snappy and doesn't have noise and doesn't have drift okay guys man I really hope that I am not making you all weary with all of these lessons on the mpu6050 but guys I'm having a lot of fun with this I am really having a lot of fun with this and hopefully if you few of you guys will be enjoying it as well okay you know that is going to be your homework assignment for next week see if you can come up with a way of fusing the data from the accelerometer to the gyro to take the advantages of the uh accelerometer and combine them with the advantages of the gyro and get really clean clean signals out of that okay guys as always I want to thank you all who are helping me out with over at patreon YouTube's made it pretty clear that they don't love me or people like me anymore and so really it's you guys that are helping out on patreon you're the ones that keep that's keeping me in the game and so I want to thank you for that you all can also help me by giving me a thumbs up it also helps if you leave a comment down below if you haven't already make sure you subscribe to the channel and when you do ring that Bell so that you will get notifications When Future lessons come out and most importantly share this video with other people because the world needs more people doing coding and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy tocom I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 3,581
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: XZIJasvCB44
Channel Id: undefined
Length: 36min 48sec (2208 seconds)
Published: Tue Dec 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.