Raspberry Pi Pico W LESSON 42: Measuring Tilt With an MPU6050 Accelerometer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McCarter with toptechboy.com and we're here today with episode number 42. in our incredible new tutorial Series where you are 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 ice no sugar no sweeteners not needed and as you're pouring your coffee as always I want to give a shout out to our friends over at Sun founder Sun founder 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 most of you guys probably already have your gear but if you don't take a look down in the description there is a link over to Amazon you can hop over there and pick your kit up and believe me your life and my life are going to be a whole lot easier if you 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 41. now you remember in lesson number 41 we hooked up up the mpu 6050 and we were able to make acceleration measurements on the x-axis the y-axis and the z-axis okay so we got the chip plugged in we got it working we got everything put together but one of the things that we noticed is the reported readings seem to depend on orientation so as we started tilting our board we started seeing changes in the readings that we were getting from our accelerometers and so what your homework assignment was to go in and quantitatively calculate the actual tilt based on that data that was coming from the accelerometer how many of you guys were successful if you were leave a comment down below I Am Legend double chest bump or if you were not successful leave a comment down below I folded up like a cheap Walmart lawn chair okay hopefully most of you guys were able to do it but probably I've noticed a lot of you guys what you do is you just run out and try to find a library or you run out and you try to find somebody who has done it and like grab the code like you do it by scouring the internet and finding the little pieces you need to come up with a solution what I hope is I hope some of you guys actually figured it out for yourself because a couple of weeks ago what did I do I gave you two lessons on trigonometry practical trigonometry trigonometry that was easy to understand then I did a couple of lessons of hooking up and using the mpu 6050 now between those four lessons I gave you everything that you needed in order to figure this problem out and so what I'm going to do today is I'm going to show you how to not run out and find someone else's library but how to Think Through the problem and then come up with a solution so even if you didn't do this on your own at least watch me do it and then understand how to think through a problem like this okay hopefully that sounds good let's come over here and we need to kind of make sure that we've got things hooked up foreign so I will get out of your way you need to go to the most excellent www.toptechboy.com and you need to look for this happy little Search tool and you need to search on something like schematic for a tilt meter and you'll come to this page first of all you've got the schematic of how to hook this thing up today we're only using the mpu6050 but go ahead and hook up yours 1306 OLED because we will be using that in future lessons so just get your circuit built out most of you guys probably already do and this was that very simple code for just making measurements off of the making measurements off of the device so let's go ahead and snag that code so we don't have to rewrite everything from scratch and then we're going to come over here to thawny and we're going to paste that code in at the point that I made that we were using the round function to try to have fewer significant figures so it's not like acceleration of 1.076 432-895 way too much resolution but I found that in micro python that round function behaved unpredictably and actually delivered some confusing results so we're not going to try to round our number off because that didn't work very well the other thing is we want to go ahead and make our measurement in the z-axis as well so we'll say Z and accelerometer is equal to mpu our object dot X acceleration our function dot C what we want to actually report and then we're going to come in and print that out as well here so we'll put a comma and then we'll give it the label Z and then we'll give it the value which was which was Z acceleration Z acceleration like that and then we'll go ahead and give it the tag G for we're measuring it in cheese gravitational units and so that looks pretty good now just to make sure that the universe is in proper working order let's run this and just see if it does what we would expect okay it's running no errors and boom and if you look over here what you're seeing is the x-axis and the y-axis are reading zero G's and then the Z axis the straight down axis is measuring 1G why are we measuring 1G in that axis well because this thing is sitting here it's the Z accelerometer is experiencing the force of gravity the force of gravity is 1g and so you're measuring that acceleration of the gravity vector and we talked about that last week so everything seems to be working and then what we were saying was remember that you can't see that let me switch over here okay we're switching over here and then what you can see now that's not right let's come to this one okay what you can see is as I tilt as I tilt the x y and z accelerometers are all doing magical and mysterious things and so what your challenge is is to take all that and turn it into actual tilt values like I say I hope some of you were able to do this but if you weren't I will jump in and show you how to do it so what do we always do we always start by drawing a picture okay we always start by drawing a picture and so let's see if I can draw something here to get us going okay so now we want to look at just sort of a sketch of what's really happening when our board is tilted so I'm going to draw kind of a tilted board here let's see if I come here like this okay we're going to draw it tilted and since it's tilted I think I'll go ahead and just kind of make a triangle all right and then I'll come down here like this okay that looks pretty good and so let's just go ahead and say that what I've tilted the board some angle Theta and so what your homework assignment is is that what is Theta so how would we think about this well the first thing that I want to think about is I want to think about now on the board is the accelerometer that actual plate that is feeling the g-force so I want to draw that plate okay I want to draw that plate thing like that okay that's the actual accelerometer that's the proof mess now what we want to see is what are we seeing we are looking at that gravitational Vector what direction does the gravitational Vector Point does it point over here no it points there even like look at my stylus right the gravitational Vector is pointed straight down it's not pointing at the face of the proof Mass it is pointing straight down so let's come in and let's see if we can draw that so I'll come here and I will draw that coming straight down like that okay and what do we know we know what the magnitude of that is that is one Chi okay that is one G but now does the accelerometer measure the straight down no it measures what's coming in perpendicular to the proof Mass so when your accelerometer is flat the perpendicular Vector is exactly the same as the gravitational Vector but as we tilt we'll we're now measuring this force not this force and this force is going to be less okay because this tilted is not experiencing the full magnitude of gravity pushing it towards the substrate I hope that makes sense and so what is that actually going to be experiencing it is going to be let's let's now draw the vector that is perpendicular that is the vector that we actually measure and so let's see if I can come in here and let's see this is kind of not real easy to draw that's actually pretty good okay so that's the perpendicular vector and I kind of messed that up didn't I okay that's the perpendicular Vector that is the vector that we're measuring when we measure the Z acceleration it's that one it's not this one well now we've got to ask ourselves how big is that one well let's go ahead and kind of finish our triangle here okay and I do I do kind of want to see that that'll I think that'll be okay so I do want to come in and I got to label a few things here so so what do I see I see there's this angle here and what I have to see is that angle is the same as this angle that angle is also Theta and you can see it that if I was not tilted at all at zero then the gravitational vector and the normal Vector would be the same and the angle would be zero and so whatever angle you tilt is also that angle on that triangle okay now what is it that I really want I want to figure out Theta but at this point I just can't go in and say theta equals something but I got to write down what do I know what do I have I have a triangle that's good I see that this is this is a very poor attempt at making that a right angle but I can see that that is a right angle okay and this long leg is what this long leg is one G and on a triangle that is the what that is the hypotenuse and that is one G okay and if this is the angle then what is this side this side is the side that is adjacent adjacent to our angle and this side is the side that's what opposite our angle well what do I know I know the hypotenuse what else do I know I know the adjacent because that's what I'm measuring that normal Vector is what I'm measuring coming off of the z-axis accelerometer and so I know those two things hmm could I write an equation well how about this that we learned in that second trig that second trig well it's the cosine that uses the adjacent angle and so what I could say is I could say the cosine of that angle is equal to the what the adjacent side divided by the hypotenuse okay now what do I know I know that cosine of theta is equal to what is that adjacent side that is that z-axis acceleration it's the Z accelerometer and it is the normal Vector that is coming in so this is the what did we call this we call that the Z acceleration and then divided by what divided by the hypotenuse and that is what that's one one G right we can measure it or we can think about it it's 1G and so that is like that so now I have one equation and one unknown I want to solve for the unknown which is Theta well how do I do that I take the arc cosine of both sides and so the arc cosine of cosine it undoes the cosine so I'm just left with Theta and then that is going to be equal to the arc cosine of Z acceleration divided by 1 is just what see acceleration okay and now what I could say is we that's going to be in radians so to get it to degrees Theta degrees is equal to Theta in radians divided by 2 pi that's the fraction of the circle I've gone around times the full circle in degrees which is 360 and then when I do that I will have the what I will have the Tilt angle okay is this just was this just I mean that second lesson did not even draw something almost exactly like this for you okay I hope some of you got it you guys leave right at this point pause the video and leave some comments about was this just a dirty trick and I didn't show you enough to work this or did I give you enough in those lessons to work this okay I think I gave you enough to work it but I guess we should ask ourselves does it really work so keep those two two equations ready and then let's come back over here to our code View I think I'll just go to the pure code view for right now that's okay so now I no longer want to print okay I no longer want to print that but what I want to do is I want to calculate Theta [Music] and Theta we said was up we better import our math Library import math okay and now let's calculate Theta Theta is equal to math dot Arc cosine I'm sure I showed you how to do that in that second trig lesson of what C acceleration like that all right see acceleration now I could just run in and do it but let's go ahead and convert it to Theta degrees and what was that that was Theta divided by 2 divided by math dot pi times 360. now I could at this point just go in and run it I could just go in and run it but what I want you to see is remember when we're looking at these things there's a little bit of noise and look at that Z look at that Z value there and you see it's a little bit more than one where it can't be more than one and so that's just a little measurement noise but if you try to do an arc cosine of a number greater than one the program will crash because cosine goes between 1 and negative one so if you try to do the arc cosine of 1.0001 it's going to crash and so what we need to do is just make sure if it's a little more than one just make it one and that way our program won't crash so we're going to say if C acceleration is greater than one then make C acceleration equal to one like that and then our program shouldn't crash all right then what we probably want to do is we probably want to go ahead and say print and then we are going to say tilt angle space and then what they I have to move this I see I did this in the wrong place Theta degrees and then we'll give it the label degrees Dot okay but that needs to be after we do the calculation so I'll cut it I'll come down here and I'll put it here like that okay let's stop it better take a sip of coffee I will need everyone to hold their breath what should I see at this point what should I see at this point it should be reading very very very close to zero degrees it should be reading very very very close to zero degrees everyone hold their breath yeah [Music] oh denied no oh oh oh what did I do math thought not p y Pi is in that pipe wrong pie okay the real problem is one of you guys didn't hold your breath I I think you know who you are so this time please join the rest of us and hold your breath ah look at that boom what is it reading tilt angle of zero so we're feeling pretty good we're feeling pretty good but now will it measure tilt well let's come in let me see if I can give it about a 20 degree okay I'm coming in at about 20 degrees and then I gotta hold it real still and look at that it's measuring 20. 45 is pretty easy to estimate and so let's come into about a 45 degree angle and I'm measuring 45 degrees okay look at that do you see watch the graph let me get out of your way a little further okay you see watch the graph as I tilt you see and it's measuring in degrees boom is that pretty cool or not is that pretty cool or not I love this do you love this okay I hope you love it too that we did we learned some trigonometry we learned some engineering and we solved a real world practical problem and now we have something that measures tilt or incline okay we have something that measures tilt or incline okay let me see something yep there it goes that's actually pretty darn good I'm just trying to eyeball it in as close as I can eyeball it that is really uh that is really looking uh that is really looking good okay now this is what I love about it we went through the math we went through the physics we went through the engineering and we actually have a practical device now what do I not like about it okay what I want you to see is if I put it nose down it reads what it reads A positive angle what if I put it nose up it reads A positive angle what if I roll it towards me it measures roll okay that's a 45 degree angle there that's good okay but it's positive and if I do this so it is measuring tilt but it's measuring any and all tilt and it's measuring any and all tilt okay any and all tilt is just tilt all right so this is what I want you to see really it doesn't matter what's positive and what's negative but if this is positive then this needs to be negative or if this is negative this needs to be positive so you would see what direction are you tilting and we don't have that based on our existing solution and what you have to see is there's two distinct axises of tilt there is this we'll call this the nose of the airplane this is pitch nose up nose down this is Roll Roll To the Left roll to the right and all four of those things are just being reported the same just a positive Angle now that angle is accurate but what do we really want we want to be able to measure pitch and we want to be able to measure roll homework for next week is to take what I showed you today and now go in and do a little bit more math where you are reporting pitch and roll and they have signs like say this would be negative pitch positive pitch negative role positive role I don't care which is which but just that they're different if they're going in different directions okay now got everything you need to do it okay you've got everything you need to do it and I'll give you a little bit of a hint uh have you used all of the data that you have coming in have you used all the data that you have to get where we are now okay now I know what your I know what your inclination is a lot of you what your inclination is going to be what you're going to do is or what your intuition is going to tell you to do you're going to come over here and and you're gonna you see you're gonna jump to the data rather than jumping to the math all right and so I'm going to turn the print I'm going to turn the print off on the degrees and I'm just going to go back to the raw data and what you're going to do is you're going to say oh wow look at this I've got all this I've got all this different data here coming in off of the three accelerometers and so you're going to want to jump in and start doing some if statements and if this if that then this then that a bunch of stuff trying to look at you're going to be looking at the data as you are rotating and then you're going to come up with some wild and crazy insane scheme to then try to do the homework okay I'm going to give you a hint don't start there start with this picture okay start with this picture you might even ask yourself you might even ask yourself hmm maybe I should think a little bit more about that Vector there right I've got the gravitational vector I've got the vector normal to the z-axis accelerometer is there anything special about this Vector okay that's a little bit of a hint okay guys give me some feedback is this the type of thing you guys like to do are you guys that just like to get a library that does it and just you want to write a line of code and you want to get an answer and you want to go through live doing projects where you use someone else's library and then you make something work and there's people like that and that's okay but man I think it's exciting where I solve the problem and I learn how to think and analyze and think like an engineer you guys tell me enough of that I just want the answer or you enjoy going through these problems that are sometimes mind-bending problems okay don't feel bad don't feel bad if you didn't figure it out but if after you watched it now you understood what I was doing and how you should have been doing it that's success too and so even if you feel miserably try as hard as you can on this homework I just gave you but if you feel miserably know I'll be showing you the solution next week okay now I'll say one other thing there's two ways you can do this the really hard way that it's going to be so hard it becomes impossible and you're going to end up with this bloated impossibly complex code or you're going to do it with one line of code like you know you're going to take what we've done here and you're going to change one or two or three lines of code and then you're going to have it do all the things that I wanted to do so there's the trivially simple way to do it and then there's the impossible way to do it the impossible way to do it is to look at this data and try to predict Trends and figure out who's wearing what's doing what and try to write a lot of if statements or you can go back to our friend the math the trigonometry and you can very very simply think through it and come up with a solution okay guys man I hope you're having as much fun taking these lessons as I I am making them I love these accelerometers and and we're going to have some real fun with this we're going to keep doing things with this with this chip because there's a lot of really cool things you can do with it but rather than just giving you a library and having you go right to the solution I want you to kind of think through things and I hope you guys are enjoying it I'm having a lot of fun with it also I want to give a shout out to you guys who are helping me out over at patreon I know I've been saying this but in the last few lessons but I got to tell you you patreon guys are the guys that are keeping this content coming you guys that are standing with me and helping on patreon that is what's keeping me in the game because again I think YouTube has decided they don't want in-depth long content Rich material they want to get a person because you know when they watch those shorts a person watches a 15 second short and then they watch the next one and the next one the next one and then they're just sort of captured in this zombified state where they just sit there watching YouTube continuously it's almost like a like like some form of addiction and this real learning content this real educational content that's the old way of doing it so you guys on patreon you're keeping me in the game also you can help me by giving me a thumbs up you help by leaving a comment down below subscribing to the channel but most importantly share these videos with other people because the world needs more people doing coding fewer people sitting around watching silly cat videos Paul McWhorter with toptechboy.com I will talk to you guys later [Music]
Info
Channel: Paul McWhorter
Views: 3,925
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: GWYy121rAOE
Channel Id: undefined
Length: 28min 17sec (1697 seconds)
Published: Tue Nov 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.