Raspberry Pi Pico & MPU9250 with MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so do you want to know how to use the mpu9250 accelerometer gyroscope and magnetometer with a raspberry pi pico using micropython then this is the show for you and i've also got some more exciting news to share with you as well from the smiles community so let's dive straight in my name's kevin come with me as we build robots bring them to life with code and have a whole load of fun along the way okay so yes this show is all about the mpu 9250 which is that uh that chip just on the screen there next to the raspberry pi pico um so let's talk about what this is and what we can do with it so as i mentioned there it's a accelerometer it's a gyroscope and a digital compass so today the session we will be looking at orientation we're looking at how accelerometers work we'll be looking at the uh the mpu9250 what that can do for us how we can connect that to a raspberry pi pico taking some readings doing some stuff with it and it actually works this time i did a show a couple of months ago where i experimented with the um uh was it the 6500 chip and i couldn't get it to work this time i've stuck with it and i've got it to work so and the magic there is we'll be calibrating the sensor which is uh where the magic happens okay so what is this mpu 9250 so let's have a take a look at this board shall we so it's an accelerometer a gyroscope and a magnetometer so what does that actually mean so a gyroscope um the way that that works if you think about an aircraft um and and there's usually quite a few different um things that a pilot needs to know about when he's flying an aircraft first of all it's on the runway and he wants to take off so the aircraft is flat and he's going to pitch the nose up so pitch is one movement that we need to understand once it's in the air um he'll want to bank and that's roll so he's going to roll the aircraft over and sometimes the aircraft gets blown by the wind and it sort of pivots on that access and that's called yaw and you need to sort of use your your flaps at the back your rudder and so on to sort of correct for that so that's what we can measure with gyroscopes we can measure orientation then with the accelerometer that's going to tell us how fast it's going to measure the velocity so from standing still to moving forward how quickly how many g's have we moved forward or backwards or up or down or all three dimensions at once and then finally the magnetometer is going to measure the magnetic field around us and we're going to use that to measure where magnetic north is and it's not as easy as you'd think and then it's also conversely easier than you think so we're gonna have a look at that so let's have a little bit more about this uh this chip itself so it's actually two chips in one so the mpu9250 is actually an mpu 6500 and the ak-8963 lots of numbers there but essentially what that means is there's two chips that are in one package that it's called 9250 and it contains the the 6500 contains the gyroscope the accelerometer but it doesn't contain the magnetometer that's the the extra chip that's the ak-8963 and it also contains a temperature sensor as well and that's because when it's doing the readings there's quite a few things to take into account to figure out where magnetic north is and one of the things is actually temperature because that can affect the um its sensitivity to the magnetic fields around it so we can actually use that temperature center as well there is one in the raspberry pi picot as well but obviously if the chip gets hotter then yes it's not gonna it's not gonna be very accurate but it's better than not having one at all so orientation so we touched on that very briefly just a second ago so it's essentially uh euler rotation so there was a very clever mathematician in the what was it the 18th century 1700 something another uh call i always think it was euler but it's pronounced euler and he created a lot of the mathematical notation that we know today but one of the things he looked at was using trigonometry and using um pi pythagoras's theorem and so on you can figure out the translations that you need to make in three-dimensional space from one orientation to another so we need to to understand that so that we can use it in our um in our in our code because what we will get from the sensor is pretty much raw data and we need to be able to put that into a formula to figure out things like heading to figure out how we move 90 degrees so degrees radians and all that kind of stuff pop up um i always remember when i was at school and things like syn cos and tan were always a bit of a mystery to me i knew that you could pop figures into these and they would come out with a the thing that you're after but i never quite understood how they work or how they're related so this is something that um was uh i had to come back to and sort of relearn for this too um we're not going to be going into um are they called equatorons quaterions i don't know how you quite pronounce that it looks like quarter neons but without the r in there quarter theons and that's because if you want to move if you want to do translations you can get gimbal lock with euler mathematics so gimbal lock is where you have um i remember they had that on apollo 13 didn't they so when you've got these three rotations and if one of the one of the um the axes is perpendicular to another one you can get this um this effect where you can't actually figure out which orientation you're actually in because of the way that the the limitations of this three-dimensional space so using these quaternions you can actually figure out 4d space and it's about movement over a sphere so it's quite a complicated thing to get your head around we don't need to go into that today so that's really cool hey tom hey helly um and who else have we got on there as well um we've got adam of course joining us on the show so let's go back to the next bit so of course yes if you like these videos please do me the honor of liking this video if you're watching live on youtube if you watch on facebook give it a like there as well um leave me a comment let me know what you think let me know what you'd like me to work on next i'm always interested to know what um kind of subject matter you're looking for next this is definitely one of the ones that people voted for in the the youtube poll that i did last week um raspberry pi pecors are definitely a hot topic people want to know about more about them and this is something that's been on my to-do list for quite some time because last time i had to go this i couldn't get it to work so this time i've cracked it um and yes if you hit the bell you'll get notified when um i go live or minutes before i go live and all that kind of stuff so um i'm very reliable about going live every sunday um but the midweek one can vary so if you hit the bell you'll get notified about that okay so the digital magnetometer how does that work so yes magnets are cool so we're gonna have a look at mems in a second um and so this ak 8963 chip actually measures the magnetic field around it and the more strongest magnetic field around it is obviously the earth's core it can measure where you are on the earth and which way magnetic north is pointing now you need to remember as well magnetic north isn't true north um it's slightly off from that and we need to correct for that with something called declination and we'll have a look at a website that can help us figure out where our local declination is and this is one of the reasons why if you buy um an aeronautical navigation unit these things can be like 10 000 pounds that they are very expensive um whereas these little chips are very very cheap we're talking you know a pound something like that very very cheap you know maybe a dollar or so and one of the reasons is and they don't know where you are on the earth and they have no way of knowing that it hasn't got a gps chip to sort of say this is where you are on the earth so we need to give it that extra bit of information just to correct it ever so slightly it'll work fine without that but it'll always be off by a certain percentage so this does this digital magnetometer um it doesn't just give us um a coordinate like you are north east it would be so easy if it just gave us them sort of compass coordinates no instead we get some um very interesting figures we get three coordinate systems from not systems three coordinates values from the magnetometer and they're measured in micro teslas so it's the strength of the magnetic field and what we need to do the very first thing we need to do is calibrate it and figure out which which way is the strongest magnetic force because that's most likely going to be our our magnetic north and there might be some other fields local to us there might be a magnet there might be some large pieces of metal that sort of interfere with and bend the magnetic field locally so we need to correct out for them and that's one of the things we'll be looking at quite a bit in here and in our code too so we're going to do some conversions from these these micro teslas into some headings so we can figure out which way is north magnetic north and therefore which way are we pointing and how do we how do we point to that so we can tell our robot to sort of move 90 degrees to you know to the west or whatever so declination we'll have a look at that in a minute so mems sensors micro electronical mechanical systems i was blown away when i found out that's how these things actually work so inside this chip is a really small um piece of mass and that's on some springs and there's actually one for each um each axis and if you think how small this thing is this is insane these things must be like microns just a couple of atoms wide or something it's insane and what happens is this mass shifts around as we move the accelerometer around and between those little prongs are it's essentially a capacitor so it can measure the capacitance change and that's how it knows what direction it's going in um which is our gyroscope and also how quickly it's going there which is our accelerometer so you always get those two in the sort of pairs the accelerometer and gyroscope because one is um essentially just the direction and and how quickly you do that is the the acceleration so changing capacitance is how that works just absolutely mind-blowing that there's things that move about in there because i always wondered how would you measure this um digitally how could you measure for that and this is how we do it so micro electronic mechanical systems mems they are mind-blowing so let's have a look at the calibration this is the magic that makes all this stuff actually work so we need to do some data processing the raw data that we get from the sensor uh is jittery it also wildly uh move around and we can have a look at that in a minute on funny we can plug that into the um the plotter and we can see what the values actually look like and then we can figure out how we can actually dampen them down using some filters there's a low pass filter and it's very simple um quite elegant in how simple it is they'll just dampen those things out to make it less noisy more accurate for us you do get a trade-off with the um when i say accuracy um these things tend to sort of filter out over time so you end up with a flattened more stable thing but that means it's not quite as quick to respond and they're probably some very smart algorithms for getting a quick less noisy response but they're much more a headache to sort of code up and explain as well so i've gone for the simple one to explain a low-pass filter you can probably look for some better ones um if you're interested in that so we yes local conditions need to be mitigated through calibration so we'll have a look at hard iron soft iron biases in a second and what we want to end up with is these them very simple uh heading type things where we can just say north northwest southeast and so on but what we get from the sensor is something in microteslas that's sort of a u symbol with a t we we want to be able to convert that and the formula for doing that is very simple it's just the heading is a tan two y and then x and it's y and then x that's one of the things that threw me that must have spent about two hours thinking why is this just giving me garbage and it's because i haven't read what somebody had said there now we do need to do a bit of extra processing on that again to get rid of some of the noise and to make it more accurate otherwise our compass would be sort of going all over the show and i'll show you how we can do that so hard iron and soft iron biases so things that can affect our accurate measurement it's a hard iron if you think about a magnet itself that is going to pull the magnetic field away from our sensor and it's going to mean that what we should have as a nice strong signal is going to be less so because of this magnet or it could mean that this magnet is going to overwhelm our sense because it's so strong and we're not actually going to know where the the magnetic north is it's just going to point to the magnet instead so it's quite easy to get rid of hard iron biases we can just um adjust for them and we do that by moving the sensor around and taking lots of readings and then averaging them out it's as simple as that soft iron biases are where we have um things like a metal case so one of the tutorials i was reading the guy had an m5 stack which i've just ordered myself which is an esp32 with a display three chips and some firmware beautifully designed and they sort of stack onto other boards so um a bit like shield but in reverse they sort of stack onto the bottom of the the device and one of the bottom devices had a metal plate on it so when this person was doing their measurement they found if they took this metal away they could get much more accurate readings and that was a soft iron bias so it's not magnetic itself but the very fact that it's ferrous and it can um it can conduct magnetic waves it means that it can move them away from our sensor or we can adjust them so what we have to do there is take again more readings and we have to adjust for them so we will have a look at hard and soft biases as well in our code and yes note calibration is not optional so if you think you can just sort of bang the code in and get really good results without doing any kind of calibration that was probably my biggest mistake from last time so when i created um let me just show you on the screen now so this is one of my smart robots and i created this little little backpack thing for it so this would slide into the the back of the robot there and it contains a little accelerometer if you can see that there one of them smaller ones um you can see the x y and z at the top there um and i didn't understand why when i had this on the bench and i was taking readings i'd get reasonably accurate readings but when i put this back onto the um to the robot there and there's a there's a motor right next to it right next to it within within about one centimeter of it and i hadn't calibrated this there was no calibration in my code had i calibrated it i would have got more useful data out of the robot and the positioning but i must have spent about a week trying to figure that out so i was really frustrated at the time and i gave up so calibration is not optional we just need to learn that and we can move on so magnetic declination i did mention that a bit earlier when was looking at um how they work so if you think the magnetic field around the earth um i don't know why that animation keeps pausing like that it's very frustrating it does do oops it should um let us back up a second there we go yes the declination is wherever you are on the earth um the local magnetic field will be pointing in a particular way so if you're um the very top of the hemisphere um you might be just getting those sort of weaker parts of the field if you're in the middle you'll be getting the sort of long la narrow bands of the the magnetic field and depending where you are you'll get a different view of that so magnetic declination is something that people put into their algorithms and we just need to feed in an adjustment a figure so there is a website that helps us figure out where we are and what figure we need to use for our magnetic declination and again in those 10 000 pound aeronautical navigation systems they will factor that in already they will have already plotted that out it will know because the gps where you are and it can pick the right one from the from the list so we need to figure out where we are there's a website that's called magnetic declination.com and if you go in there and you put your local address in it will tell you what your declination is so mine there is minus a zero minus zero uh and then 58 um what's that 58 minutes um and i would just plop that into the the algorithm and then it could adjust for that so i'm not going to do that i'm just going to leave that out for now in my code but that's something that i would need to look at if i want this to be super accurate okay so how do we plug this in how do we get it to work it's i squared c so it's dead easy we just need four pins one is ground one is voltage and it works with 3.3 for the pico which is fantastic and the other two are just clock and data so what i've done if i just put up the uh the bench we can have a look overhead there so i've got a raspberry pi pico here i'm just using pin zero and pin one there for the data i always use a blue cable for the data we'll just unplug them so that we don't get confused and i have the ground and the um the voltage there so the voltage is going to the the fifth pin down there is that pin 35 36 something like that and then the ground there is um pin 37 i think pin 40 is the one that's the very top there and then into the uh the accelerometer i've just got the voltage the ground the clock and the data simple as that and that works for both parts of the chip so we don't need it like all the pins to be used so the other pins that you can see on there are for other things so if you want to be able to um adjust the address for example that this is on you can actually change that ado and i think you just pull that up and you get a different address i've not looked at them to be honest because i don't need to think about them there's also an interrupt pin on there as well so um yes i do a new video every single sunday um let me just put my other call to action for that one um so it's just a reminder when you're watching this if you're a new viewer and you're wondering when i post my videos it's every sunday around 7 00 p.m gmt bst whichever is my local time i'm based in the uk so if you're based in the us or canada you'll be in the um us canada or america's i should say south america too you'll be in the left-hand column if you're in the european uh indian pakistan russia time zones you'll be in the middle column if you're in china australia new zealand and russia you'll be in the writing column so you know where you live you know where your time zones are if you've not checked out smartphone.com as well it's a good time to go and have a look over there i've recently launched the the new smart learning platform so you can learn um some robotics 101 and some python 101 stuff as well so a nice gentle introduction to both of them the kind of things that you need to know and there's also two um tutorials as well on how to build smart and a smart quad robot so have a look over there and let me know what you think of that too and also um if you want to help support the show you can buy me a coffee so you might have seen that little um widget i had on the screen just there now so yeah if you want to help out um the show just buy me a coffee um that'll help pay for all the stuff that i have to pay for just to run this show such as the website hosting the royalty free music graphics software the streaming software the equipment and so on and so on so you get a picture okay so oops have we not got that looks a bit weird there we go okay so um so um geek guy saying i think those times for usa should be pdt etc there's so many time zones i didn't know which ones to pick is the answer there is so many time zones i thought i would just pick a few because if you don't know what your local time is converted into like greenwich mean time or you think when is it i thought i'd just give you a slide to give you a flavor of that um so you probably know when when i'm live now it's this time 25 minutes ago every single sunday the midweek ones will be whenever i feel like it it's just when i can get around to doing them i try to do them around the same kind of time so be between six and seven ish possibly eight okay so uh the next bit then is the code library so i found some amazing code from uh mika zak tupola um and um i've tweaked that ever so slightly i've uploaded it to my github account so if you've got a github dot com slash kevin maclean mpu9250 you'll find the code for this show that we're gonna we're gonna play with now um and it's there's only about two three three files that we need as the library um and we'll have a look at those shortly all works fine on the raspberry pi pico so should we have a look at this demo time my favorite time so let me bring up visual studio i'm just thinking about the best way to in fact let's let's bring up funny i'm going to bring up funny i'm going to just go over to here just so i can get all my screen ready and then we can have a look um there we go funny and i just need to connect to the picot and then i shall show you my screen so how we i just need to disconnect the camera of course and then load up there there we go okay so let me just go over to the screen let me see if i can get this uh better for you uh we don't need that in the background let's get rid of that and that okay so so i'm just going to connect to the the picot i think that's all good there so raspberry pi pico there we go okay so i'm just going to open up from the pico i've got all the the libraries loaded into there um let me just see if we can get a better view of that if i go to that but then click on that is that better i think that might be better there we go right so i've got quite a few files on here i've actually not cleared out stuff from last week's but the one that's called simple is the one that we're going to play with but uh before we do that i'm just going to create a new file and show you how this works and then we'll have a look at the libraries behind it so the first thing we need to do with any raspberry pi pico is bringing machine so we're going to import i to c and pin and we're also going to bring in um some maths functions so from math import square root um that will help with some of the filtering we're going to bring in eight and two we're gonna bring in pi and we're gonna bring in copy sign which copies the sign and we're gonna bring in sin and cos we don't need to bring in tan because we've got a ton instead so yes time for the fourth dimension and we're also going to bring in um from mpu which we'll have a look at in a minute but i just want to show you how easy this is to use and we're going to import the mpu 9250 class and we're also just going to bring in a sleep so let's need import there as well import sleep okay so the first thing that we need to do is we just need to um set up our i squared c bus so let's say id equals zero our clock is going to be on pin um this is where clock is on one and data is on zero and we need to have them wrapped in a pin so that it recognizes them when we pass them into the i squared c class and that's what we do next so i squared c equals uppercase i squared c so id equals id clock equals clock uh why is it complaining about that just because of bracket isn't it and then data equals data okay and what we're going to do as well we're just going to print out what we can see on the bus because that just gives us a bit of confidence that we've actually got this thing working so i'm just going to run that and we're just going to see in the uh we're going to save this to the pico i'm just going to call this simple 2. pie okay so why is it not happy with that what have i done wrong there so dude so it's not happy with that scanning thing why is it happy with that now that should work fine what's it complaining about let's try that again funny funny funny let me just move that up there so i can see everything and what is going on there so okay global why does they need to get globals right i'm gonna just flick over to the one that actually works and we are gonna just have a look through that i'd rather type it out so you can see what i'm doing right so we've we've just typed all that in as well and we've got down to this bit where we are scanning the bus so that's going to read out it's going to print out a value um so adam says it's copy say not copy sign is that what's going on um no no no copy sign that is correct uh because what we want to do is um when we use the a tan function 8 on 2 it can actually get rid of the sign on some of the um the values are read in and we want to preserve the sign so we copy that from the value that we bring in um as you can see there that's got a copy signed too this code actually works because i've just been playing with it so what we're going to do we're going to print that out um what i might do is just um should i should i just copy all that out for a second let's just do that right let's just try running this this should work okay so it's got 104 104 is um no we didn't we didn't get rid of that as well 104 is the address of the um the mpu9250 we just loaded up our calculator and we type in what was that 109 was it 104 sorry so we go to decimal 104 and then we look at that as hexadecimal it comes out as 68 and that's correct that's what this device defaults to right so what we're going to do next then is we're going to create um an mpu9250 object we're going to pass in the i squared c bus because that's where it lives and we're just going to call this m just to make it really simple for our code then what we're going to do is we're going to read in three values we're going to read in an x a y and a z and the way that this works this there is a function that's called acceleration it's one called gyro and there's one called magnetometer i think uh let me just have a quick look down to see in the code so the magnetometer one um what i've not done i know why this doesn't work now now he realizes so i've just been using um visual studio to create the code and what i've not done is uploaded the code that i've created here onto the chip so let me just disconnect from there so it's going to go run and disconnect and then i'm just going to go back over to here and then just upload it so i might just need to quit funny to do that though because it you say disconnect it and then it doesn't disconnect it it's a bit naughty there we go disconnect okay let's try connecting through here and if that doesn't work let's just quit funny all together okay i want to use thorny because we can have a look at the um the plotter and that's a really good way of seeing the values that come through so let me just upload this code and then we can we can take it from there so if i go to that simple we connect to our now pico why do things never behave i'm just going to quit visual studio it does sometimes do this i've found um i don't know if it's one of the plugins i've got stored but you sometimes just need to quit it and then load it back up again so i'm using this on the same mac that i'm broadcasting on as well that shouldn't make any difference um but you never know right so let me just give that a second to load all its bits and bobs and then we right we should be connected momentarily there we go connected to find out upload this simple code um there we go it's written it to the board i can now disconnect from there load funny back up and away we go let's just open up simple there we go okay right so what we did just previously then was we created the um the mpu 9250 object we called it m and what i want to do is just cut out all this for a second and i just want to do m dot and then we should be able to see all the um dots why doesn't it show you the uh accelerometer i think that's all we need to do if we just do print that let's see if we get some values out from it um so what is the name of the put my notes next to me here i did do all the acceleration sorry beg your pardon acceleration there we go right let's just run that okay just have to ignore that management error thing that keeps popping up don't know what that's complaining about we can see there though we've got some readings we've got one reading two readings and three readings so this acceleration object that we get is actually the x y and the z in that order so the first one so if we look at just um zero thing in the array that's going to be the um x value currently so we can see there not point two something rather so they're actually um uh they're actually um g forces i believe so we can grab them and put them into a variable such as x y and z so if we just very quickly do that if we just say x equals m dot dot acceleration and then let's just do that one there excel sorry having a moment here so acceleration excel uh yeah nation okay just copy that and then let's change these to y and z and then we just change that to one and two and then let's just stick this in a while true loop and then let's just move these out a little bit okay so we should have a little loop running around and once that's done i don't understand why it keeps coming up with this uh error message what if i done wrong there you can see as well there the uh the address is the 68 we've defaulted to that we don't actually pass that in but that's um that's one of the addresses that we use so let's just try that one more time yes that's a good point thank you there we do need to type the word print sorry i'm having a moment here i'll tell you about this so when you're live streaming it's like you get a bit of brain fog because you're um you're conscious about everything else that's going on around you so i can see the comments being typed i can see all my surroundings they actually call this the uh live adrenaline monster so there's a cup there i tamed lamb i've not quite tamed lam yet right let's try that again shall we so we should get some uh values popping through oh man alive why is thoni being so funny fail to execute clowns get globals we don't need any globals i don't know what to do about that mirror message this was working fine previously uh what i wanted to do is i wanted to show you the value as well that's not going to work is it so let's just grab that code and let's just stick it in a new file in here because this does work um simple test dot pi let's just bang that in and let's just go we do need to grab the rest of the top of that code though because it's just missed that there okay and then we can we can just stay in here to be honest so let's just quit disconnect quit funny goodbye for now and let's just stick with in here so if i just open that up i'm just going to reconnect the board and then it's going to run this code okay what we should find is we get several readings being shown on the thing i don't know if i've got the uh the latest version of thoni there that's a good point um what i think uh slime 23 yeah i thought the funny doesn't do like proper indentation it doesn't do auto complete there's all kinds of things um that's not great so yeah and that's that's why it's complained about that let's just do x and then that like i said i was having a bit of a moment there you probably are looking at it's going what are you doing why are you trying to set a value with inside a print statement that's never going to work and then let's just do z and then what we will get is these three values just being squirted out to the screen um although we can't plot them we can see the values change so that's what i want it to do right um so that's not great because we can't actually see the values side by side so what i'm going to do is just change that print statement just so that it has all these things side by side so we can just see them on one line because that's going past way too fast because what i want to show you is um the values change in real time um so yeah adam says you just have to load this on a 4k screen just so you can see it let me see if i can zoom this in for you a little bit as well so if i go to appearance and zoom in so it's just that equals there we go okay right so what i'm going to do i'm going to go over to this one here as well so as i tilt this accelerometer watch the values change you can see that x there just went to minus as i tilted it up and if i go to the tilt that one as well let's try tilting it that way you can see the y is now changing to a minus figure and if i just move this round on its axis 9 is not really changing but you can see the values change there as i sort of move this thing around which is all well and good we know we're getting values from this accelerometer but what do they actually mean how can we actually use them in our code to be something meaningful such as like an euler orientation is it pitch is it roll is it your and so on so i'm just going to stop that from running uh let's just go back to the more full screen so that one there and i'm just going to hide that for a second and we can have a look at our more complicated program now okay so this is um the more full version so instead of just doing that printing out we're going to do something useful with this so the first thing that we need to do is we need to do that calibration remember i said it's not optional when we want to actually start working with things like the the pitch and the roll and and the magnetometer particularly we need to calibrate this so it just so happens that the library and remember i said there was three library files there is the ak8963.pi and in there there is if i just move that there is all the complicated stuff you can see there mike tupelo has created this to polar and there's the links to his original sheets and micropython stuff and what he does there's a whole load of constants that we bring in there and these are the actual values that get passed on the i squared c bus so we don't need to worry about them all the power management you have to do awful translations between 14 and 16 bit words and stuff and understand how that works we'd have to worry about that he's done all the hard work for us but essentially what he's doing in there is things like the adjustments so in here there is um the magnetic one returns in micro teslas floating point numbers then there is a calibration thing and in here this this count thing where it it's going to grab a number of values over however many counts we pass in it defaults to 256 which is great um so it's going to grab 256 values and it's going to look for the minimum and maximum value of each of them um for the x y and z and then we're going to store in there the minimum x minimum y maximum x maximum y minimum maximum z and then it's going to divide them um it's going to add them together and divide by 2 and that will do the hard iron correction so if there's any large magnetic sources or small magnetic sources this will average them out then what it will do is it will do a soft iron correction as well so you can see that it's going to average them out just similarly what we've done before the maximum minimum divided by two it's going to add all those together divide by three to get an average delta then it's going to scale it and this is where it gets clever so the scaling we take that average and then we divide it by the average x delta that we worked out there and then what we then do is we set a scale for each of those x y and z's so that will then be used in the calculation um further down when we actually grab a value let me see i can show you that when we actually get the x y and z and it times it by the scaling factor so there we go so xyz um is times by the scaling factor and what that does is makes it um a value between um zero and one i think that is our minus one to one um okay so that's the ak-8963 library that that does basically the magnet magnetometer then we have the 6500 and what that does is everything else the gyro and the acceleration we don't need to do too much in the way of um calibrating of that so i don't think there's a great deal in there we just get the acceleration which is the x y and z or gyro they're the two main functions in there and then the 9250 dot pi library essentially just brings those two together it creates um a singular wrapper around each of them so you can see there it passes it has an acceleration and it just goes and grabs that from the 6500 same with the gyro and then for the temperature we can grab that and for the magnetic one it just grabs that from the other library too so what we need to do then is we need to do some calibrations if i go back to our simple one and i've just said count to 100 we could leave it as like 256 it just takes a while for it to do that while it's counting what we need to do is rotate it on the spot uh i've read somewhere that you can do um a figure of eight but what we need to do is um i found is just rotate it on the spot so i'm going to run that in a minute and you'll see it count down i've just done um a backwards count that's what i mean if i've tweaked it slightly so while it's doing that count it will actually print out what number it is and then count down to zero so you know how many rotations and things you've got left and the slower you do the rotation you want at least one full rotation but the slower you do that the more accurate it will be if you're doing this i'd also try and do this on a flat surface as well because if you're moving it up and down as you're rotating it that can also affect its accuracy slightly um we're also going to have two biases that we want to account for too which is the pitch and the roll so when we the the board itself will assume that it starts on a level playing field but when it takes all the values and everything it might not return those values so we just need to store what the very first values are and then we can pass them into our function later on as a bias so i'm just going to set these to be zero and then we're going to adjust for them um when we do our measurements in a second then there's a low pass filter so the low pass filter is going to average out our values over time just so that we don't get these lots of spikes and noise it's going to flatten them out and to start off we just want to have a variable so i've just created one so it's just zero we could do 0.0 actually on each of these just to make them a nice float um declination i've just left there because i was getting a bit confused as i was writing this code and i thought i'll come back to that i know it's you just literally need to take it away or add it to the end result it's as simple as that and then there's some other things as well so i've created a bit of code in here which converts degrees to headings so i want a heading that says north north east east south east south south west north west and west no particular order and you can just see that between zero and um 300 and degrees we want to have one of those values so there's just several if statements for that it's very weird that python doesn't have a case statement i've never quite got around that so you just have to have a lot of if statements i guess it's the same thing really okay and this is where the magic happens so get reading so what we're going to do we're going to grab the filtered y and filtered x we have to use that global if we want to bring in a global variable again a bit weird that we have to do that and then we've got an x y and z and we're just going to take the accelerometer zero one and two which we know we just get those x y and z values then this is the first bit of code that makes something happen so we're gonna get some radians um if you've ever wondered what a radian is i'll probably give you a very quick explanation of this because it is really simple to understand i'll just grab a pen and show you this on the overhead so if we have um a circle and that has a radius then the the length of that radius there that's r if we um create a triangle that's um that that length there our but around the edge there that arc which is of length radius that is one radian so they always end up being about 58 point something degrees uh and it's always the same number um within a within a circle and it's just one of the ways that we use to um let me just go back to the right view it's one of the things that we use um well i'm not getting this right kevin let me just go back to that there we go sorry about that right so it's one of the ways that we use um to calculate things uh when it comes to circles uh and the eight and two is um tangent um what will will get us half the weight there we need to use um an arc tangent of 2 to get the full 360 degree so we we start off with minus x we get the square root of z times z plus y times y and because that's square rooted it's squished back down and we get the roll rate rule in radians then we get the pitch in radians by doing the same thing but with z and we want to copy the sign from y square root that and then we want to multiply it by 0.1 which means it's a really small fraction and we're square rooting that those two together as a product and that will give us our pitch so you'll be able to look through this code in your leisure if you wish and then all we then do to make them into degrees so we convert some radians into degrees is we just times it by 118 divided by pi so that gives us our pitch and our roll so if we did nothing further at that point we'd be able to figure out our pitch and our roll we'll have a look at that in a second next up we have um we're going to take some readings from the magnetometer so with x y and z we're not going to use the z but we're going to take them uh from the um from the m which is our mpu class object and magnetic is the thing that returns those little tuple of values there so then next up the filtered value oops the filtered value let me just go back up to there sorry just it just flicked when i i was trying to sort of pinch to zoom out that didn't work on here so filtered value we created that before just as a holding value and this is going to be using our low pass filter because what we want to do is store a value bring in a value and then just adjust it slightly so that over time it does move to the new value but only gradually and that means it'll filter out the noise because we're getting hundreds of thousands of these readings a minute so we want to filter them down so it's not just wildly moving about and the low pass filter i'll show you that function that's one i've created there it's very simple it takes in um a value and it will just add it to that filtered value it'll add 20 of the value that it's passed in to filtered value that's all it does it just adds 20 to it uh we do that with x and y and that just means we get less noise on our x and y for our pitch and our roll and we can also do that for our our compass that's that's what that particular one's doing there and then a zed is azimuth i've just kept with that so we do 90 minus eight and two we pass in our filters at x and our y from our magnetometer we times it by 180 divided by pi which gives us a value in degrees and those degrees we can change into headings which is just amazing one of the things we need to do as well we will get negative values coming out of that so if it's less than zero we just add 360 to it and then the values will be between 0 and 360. we do need to adjust for the original bias and what we do here is we just say pitch to take away whatever the pitch bias was and we're going to grab that further down the code and just store what the very first value is so that if it's at a slight angle we're going to make sure that that's zero as we start out because um that's what we want to do we want to just adjust for any any bias there and then the heading is going to be the degrees to heading which we created above we're going to pass in azimuth which is um is degrees and what we're going to get back into heading is a text that says n or w or sw or something like that so next up we've got the the low pass filter so i said we pass in a raw value and then there's a remembered value that we we're going to pass in as well and we're going to apply 20 of the raw value to that filtered one so we just store a value of 0.8 so if we have 1 as a whole number and 8 of that alpha the remaining percent is 20 20 or 0.2 and that's what we're going to pass in there we're just going to take one and then we're going to take off the alpha and then just multiply that 0.2 by the raw value and store it in our remembered value so and then we return that so what that means in effect is we just get a value we're going to bring in another value and we're just going to apply 20 of that new value to the old value so if we get a very large swing we're only going to see a fractional you know 20 a fifth of that very large swing applied to our new number whereas if it's just quite a small number a small change anyway or not very much change we'll not see anything happen at all there so it's a really nice way of just doing a low pass filter okay so we're nearly at the end of this code then so show is where we do some magic so we're going to grab an x y and z a pitch roll an azimuth and azimuth raw i just wanted to play with that and then get reading which is what was going on up here this is get reading it's just going to return if we look down there the xyz the pitch roll the azimuth and the heading so um that raw value is actually the the heading i should actually just change that if i got a rename symbol and call that um heading value something like that that will have renamed heading value there as well and then just like we did before we're going to print out on one line the pitch we're going to print out the roll we're going to print out the compass heading in degrees and we're going to print out the heading itself and then we're going to sleep for um 20th of a second 25th of a second okay and if we go down to the very so that's that's a function that's called show so our actual code the very first time we run it it's going to set xyz pitch bias roll bias azimuth and this raw this heading to um it's gonna basically just do one one reading but the pitch bias and the roll bar so the things that were actually stored there we don't care about anything else and then we just do a loop that just says show so it's just gonna run this same thing over and over again so let's run it shall we and let's see what happens when we do this so what it's going to do it's going to count down and i need to rotate so if i go to this overhead now i need to be rotating this around and this is where it gets a bit tricky because all your wires get sort of tangled up but i'm going to keep rotating that round until it stops counting down i might just go back the other way it doesn't matter which direction you you're rotating but i am keeping it flat um just to help with the accuracy and trying to keep it reasonably accurate right so let me get my compass out on my phone i know this isn't 100 accurate because i haven't given it my declination uh so compass but um it does work so true north is pointing in that direction at the moment so if i get my little thing here and point it um i think it needs to be pointed in that direction let's see if we can get this to be there we go north so it's a little bit out if i put that right next to it i found that it's about 20 degrees out or so actually it's not far off that actually about 15 degrees out and that's the declination so the iphone understands declination this doesn't so we can see there if we watch the heading on the very right all the the uh the degrees that are here you can see them changing as we rotate this round so the other thing we can look at let's have a look at the pitch shall we so i think the pitch if i tilt the board up like so and if i do it 90 degrees you can see that's gone to minus 90 degrees pretty much if i flick that back and then do it the other way you'll see it go plus 90 degrees if i then roll it so if i just tilt this up you'll see the roll is now changing i put that back down and i tilt it the other way you can see the rolls now going back up as well try not to tilt try not to change the pitch i'm just trying to change one thing at a time and there we go we've got our mpu actually working and working properly so yes you can put this near a motor depending on the noise that the motor makes so that you can do the hard iron offset from the motor so the fact that it's a big chunk of iron and it has a metal in it um it will adjust for that i'm not sure how noisy the motor is if that's changing its magnetic field um as it's struggling as it's as it's moving about um not sure i think that we'll need to do some experimentation i'd probably recommend try and move it as far away on the robot from the motor so if we look at the uh pico cat for a second um this has a space on here at the very top of the robot away from all the servos and that's where you you screw the uh the accelerometer into so it's kind of away and high up and you also want it to be pretty central to the the mass of the robot so that when it turns it's turning accurately because if it's one very extreme and you turn then it's going to look a bit weird on the the data that comes back but we can certainly put this into a smiles robot and say turn 90 degrees or face north or go forward and then turn 270 degrees and it will work it will do that we can also get it to detect if it's upside down so a couple of things i put on github that i want to work on with this um because i also want to have this work with the cat so if the cat um falls upside down because it's it's toppled over i want to be able to detect that from the mpu and i'd simply want to be able to detect upside down so we just need to look at the um the role or the pitch just to see are they opposite way round and we can make it do some kind of alert so yes so tom was saying this never worked on his it never worked on mine either and what i discovered was it was to do with calibration so i played with this um probably since wednesday this week trying to look at why is the code working or not working so i could get the raw values i think everyone can get them then putting them into something that's useful so changing it from um either g forces or micro teslas into um angles it's quite a complicated thing to get your head around and i was like i don't want to do that kind of thinking i just want to find somebody else who hasn't used their code that's kind of what i was shooting for and the reason is again if i just do a quick naughty diagram of this um if you think you've got um 3d space so you've got x y and z z pointing down um if you want to orient your robot you need to take um you need to be aware of two coordinate systems so there is the sort of local roblox coordinate system that's supposed to be our smart robot there so that will also have um a local x a local z and a local y coordinate system and if that rotates round we want it to be able to detect that but in relation to the real world and that's where it starts getting confusing because our brains don't naturally think very three-dimensionally when it comes to these kind of math so we need to get our heads around that um just to complicate these things further so when you take um a measurement from one of your um your either your accelerometer or your gyroscope that's fine in and of itself but if your object is moving you do need to have those other values be represented in the algorithm because otherwise it won't be accurate so what i mean by that is if you um let me try and describe this so the way that we work out how something is um again let me just go back to the overhead if we want to work out um we have our little sensor this is our sensor and this is i don't know the ground and we want to work out what our orientation is to the ground we've actually getting we actually have three two sensors to begin with let's just do like an x and another z um and depending what orientation they're in we will get different readings so if they're if they're pointing like that so our x is slightly at 45 and our z is at 45 this is our local z then we're actually going to get 50 percent of the gravity that's sort of pulling down on this spread across each of them and we need to add those up to figure out ah right so if we get 50 and 50 on our x and our z then that means we're in this kind of orientation if it's pointing i don't know if it's like a 40 40 60 then that's a different split and that's just in two dimensions and we're actually working in three with acceleration with um when we accelerate it means that the the gravity that we're experiencing is sort of moving away from us so it's not it's not consistently applied like that um so the acceleration affects the gyroscopes readings um similar with the the magnetometer as well so that doesn't work just on a flat two-dimensional plane depending which way you are tilted and that's why if you get like a pocket compass um that's actually on a little point um so it's always trying to be on a top two-dimensional plane but if you sort of tilt it too much there'll be a point where it can't read and you get really weird um readings on there and in fact if you go to the the apple one of whichever one you have there's a little little disc in the in the center there and as you sort of tilt this you can't really see because the way that uh maybe i can do it on here as i tilt the phone you can see that little grey disc also tilts and it gets more or less accurate depending uh where that is so ideally you want that centered and then you can and they've got really nice smooth filtering algorithm there and they give you all the local data too a bit of a ramble that but what i'm saying though is these things are quite complicated and when you start getting into different um integration algorithms so if you think we've got these two chips we have our 65 50 chip 6500 chip and we have a 8963 chip and they're giving us two sets of values but we want to combine the two together that's called a fusion algorithm and there's lots of different fusion algorithms you can look at calman and uh what's the other one i can't remember anyway i don't need to worry about that because somebody else has done all that for me they've um magic that's the other one i was thinking of um you can just look at the code you can see what they what design decisions they've taken um and sometimes when they're when they're when they're working on these you might want to have more of one than another so it's kind of a sliding scale you might want to take 80 from your magnetometer and 20 from your gyroscopes as a number because you want to be more absolutely accurate but that means you get a lot more noise um whereas if you use the um the gyroscope without the magnetometer you can do dead reckoning but you'll get walk over time so that that'll drift so you'll not know where true north is you'll just know where you started and it can't accurately remember where it started from and that's where using a bit of your magnetometer can correct that so how much you can correct them there's lots of different complicated algorithms uh to do with that so it's gonna have a look through the other comments now before i get on to the other cool news that i've got to share with you so we have had quite a few comments from people today hey plasma as well um hey geek guy so um let me have a see what we've said there so lots of um adam would say you didn't know you could do that with a calculator yes the um the calculator is quite a cool um i've got it in my screen there but you can't see that it's a um i just bring that down here if i go to view there is like the basic mode which is what you're probably used to and then there's scientific mode but we want programmer and you can change between the different bases so you can go from like octal you can go for decimal or you can go for um 60 bit you can even click on the buttons to to do binary so if i do like one if i do two or if i do three which is both of them or four you get the idea um so if you want to see what a particular number is there's a t-shirt that somebody had um some hexadecimal values on and it was like i'm a geek but written in hexadecimal and then in ascii um code page and i figured it out just by using this so yeah you can you can do all kinds of cool stuff like that you can hide the binary as well you can look at ascii codes there you go so um is it 65 is a if i remember so we just go in there and just do a 65 clear that i'm sure that's like capital a there we go anyway so yes um didn't know you could do the calculator could do that um so yes you're saying you could do accelerator x y and z equals yes you can do that sometimes i'll do things in a kind of really laborious long-winded way because it's easier to read and explain rather than the efficient program away particularly when doing like comparison sometimes i'll do like um if az if a is less than three and greater than two or something whatever the number might be i'll write it out like that rather than using the sort of left bracket thing so bryce says hey from glasgow awesome another uk person and then acceleration yes i got the idea from that and um yes um jram was saying about the print command i was i was i was doing the right thing but i wasn't printing it to the console so we couldn't see that anything was happening there and those issues i was having with funny like it's a bit of a love-hate relationship with body if i'm honest i quite like the fact you can plot things out and i'm sure that there is a visual studio um plug-in for doing mono you know for doing plotting and stuff but i i've not found that yet um but that's the thing i found quite useful um and funny does work with micro bits as well and esp32s esps and all the rest of it i don't want to say you have to load this onto a 4k screen so you could read it so i'll hopefully make the the text a bit larger for you there as well so mark's saying this is all new for me what program are we writing the code in so i'm using visual studio visual studio code uh so if you go to um microsoft.com visual studio i think you can download it it's free works on macs windows and also raspberry pi's as well which is cool um they're now they're recommended platform for doing that and i like it because it has source code control built in so i can write some code can squirt it up to github and then you can download that code too and yes the language i'm using is python and it's a version of python that's designed for these raspberry pi pico chips and it's called micro python so we get all the power and beauty of python the simplicity of that um but on a tiny little micro computer microprocessor and then fernando's saying um got here late thank god the rewind button and the repo recording going on fantastic and uh i'm always i'm always watching your amazing stuff as well fernando's i'm a big fan of yours and um what else we got my mom saying roll or roll rule or roll um and uh ladies and gentlemen saying hey already running wow yes we got it actually working i was so proud that i managed to get this working after last time i was doing the um um positioning stuff when we were looking at um i'm gonna say the i think it was an arduino build it was probably the smart robot and i was doing like a builder smart robot series it was one of the very early ones whereas you could see the staircase behind me i wasn't in my nice studio then and yeah i'd spent so much time trying to get this to work and i just couldn't get my head around it because it's quite a complicated one and some of the videos i watched online said this is going to be a three-parter so we've we've done this in one show we've managed to squeeze that in there so yeah tom was saying can we run that um near the motor near the sensor uh um i i i'm not sure about that i think it's best to try and keep it as far away and you don't need to keep it too far away remember there's the inverse square law so that means that the further you go away um the much less that that interference will be affected affecting your sensor so yeah never worked on yours so it also helps me with mm5 stack smiles yes absolutely so i'm looking at um getting an m5 stack i've ordered it should arrive tomorrow i've gone for the uh is the fire version with um you get the battery and you get some other stuff with it as well but yeah i quite like the idea of that um so it sounds calibrated everything's fine until we turn the motors on yep that's what the motors are very noisy i mean they're not very well shielded either we could shield them i mean you could experiment with that with like maybe tin foil you don't need much to shield magnetic stuff away uh maybe put in a bit of tin foil as a sort of temporary shield that should probably be enough you could do some around the sensor as well as around the motors that might work and it's very thin um so and adam as if to read my mind says if you try putting some foil over motors see great minds adam absolutely um never try that so you mean give them a shield yes so i would say put some tinfoil in the bottom of the smiles robot then put the motors in and just have that tin foil come round them a bit i mean you don't want to short them out but you want it to be um just shielding the magnetism away from them you don't need much and it is yeah adam says it's like a faraday shield um cool come on filter yep absolutely so jram says um all the different fusion algorithms available in matlab absolutely that was one of the videos i watched actually was a matlab they had a really good explanation really easy to understand explanation of this um i did have to read all them to them figure out and i i'd spent maybe yesterday afternoon going through creating my own hard iron and soft iron calibration bias get rid of her and then i've figured out that that's already done in that um that library that ate the ak 89 63 library calibrates all that away for you anyway so we don't even need to do that and that's why it works so so well um saying the adafruit feather rp 2040 better than pico pecan replaced adafruit yes so we do have um i've got two of them actually here um so one width and one without pins so these things are absolutely tiny and that's why they're called tinies so they they contain the raspberry pi 2040 chip you can just see the the logo there um and it's the exact same chip that's in our raspberry pi pico but um a lot smaller obviously we don't have as many pins and as many buses available to us um but it's the same chip runs the same code and these things are great for putting in really small robots such as our our smiles at mini um so yeah adam says there are two types of people absolutely those who know binary and that's the best thing about this today isn't it it's a bank holiday um and i understand in japan and china they get about three days bank holiday as well they get wednesday up to wednesday which is pretty cool we just get the uh the one day in the uk um so for power supply in the small smiles have you looked at super capacitors i have not brian i will have to have a look at that i have ordered some more batteries some more um lipo batteries i've got several of these um lipo small lipo booster board things so there's that's not the right one i think that's it so the adafruit and they call micro let's see if we can get that to zoom in there micro lipo and it's a small um lipo charger so you put micro usb in one side and your your jumper a bit like that one um into there like so and um i've got another variant of that it's got the that's not a micro usb what's that called it's the soft slightly larger version compare the sort of size you can get ones with have a usbc as well but yeah think about having them on board the robot as well i'm also thinking about maybe a smart xl so um we can have more stuff on our smiles so talking about smiles have i got something to show you guys so just one more thing i was talking to kevin thomas this week and he said can i help him out with something because he's been working on something behind the scenes he's been working on a new smiles robot so it looks quite like the open cat it's an experimental design he's got the fusion 360 files he shared with me and yes it looks pretty awesome it's got this really interesting mechanism so if i just uh play that one forward again there it's like a scissor mechanism so you can see there that the there is two servos for each limb but they're right next to each other so you haven't got the weight being carried on the the sort of elbow so if we think about this cat robot that we have here it's having to carry around its own its own servos on its leg there so it's got one on its body on its sort of shoulder and then the one that's the um the elbow that's quite a bit of weight to carry around on the limb you don't have that problem on here because it's not carrying any wheat on the uh on on the actual leg itself the leg is just there to sort of hold it up um the complicate the complicated thing the thing that kevin needs help with is the um the kinematic the reverse kinematic model that you need to create to make that foot move you need to have all the different angles figured out for each of these um so that's what we're going to do and there's also i think there's 12 servos in this so there's two per limb and then there is also another four to make the limbs articulate so just in in here there's another one so they'll sort of move up and down as well as um being able to sort of do the walking motion so yeah pretty interesting design that uh he's printed one out himself i'm gonna get my printer fired up this week and printing out a version of that too and uh yeah can't wait to share that with you too so a bit of a smiles excel smiles titan this hasn't got a name so kevin was saying what should we call this currently it's just got the uh the name qx 21 uh i guess because it's um an experimental model in the year 2021 but i haven't got a name for this so maybe we should have a competition who can come up with the best name for this what what do you guys think what do you guys and girls think about this so yeah that's the exciting news for this week hope that's something that uh interests you and um yeah watch this space for more information about this as we as we work on this so yeah poll for name absolutely i'll do a poll in the uh the youtube channel i'll put a picture up and and i shall do the uh the poll and we can come up with some good names um but yes i shall do that what i'll probably do is do a post with the picture on and then say what you want to call this people can submit names in the comments and then we'll just pick the top four names because i think you can have four things in a poll um and yes we'll just pick the the top four and then people can you know whichever one gets the most votes it gets the name so how cool is that so you enjoyed this one this was a really fun one to do today um i was really pleased that i got it to work that's how much faith i had in myself getting this one to work just because i've been burned so badly last time trying to figure this one out but uh calibration is the key there we just need to spin it round on the spot slowly get at least 100 data points to do that proper calibration if you try and cheat and just do a couple it won't calibrate properly and then you put it in your robot and make it so it's aware of its world around it so i can't wait to see what you guys do with that and i shall see you for the midweek video next week so if there's nothing else from anybody else i shall see uh see you soon then thanks everyone bye-bye so [Music] [Music] [Music] [Music] [Music]
Info
Channel: Kevin McAleer
Views: 14,315
Rating: undefined out of 5
Keywords: mpu9250, mpu6500, ak8963, gyroscope, magnetometer, accelerometer, raspberry pi pico, pico, micropython, Kevin McAleer, SMARS, How to get compass headings from raw values, compass, digital compass, compass headings, MicroPython, Positioning, raspberry pi, raspberry pico, raspberry pi rp2040, rpi pico, raspberry pi projects, rp2040 microcontroller, raspberry pi pico programming, raspberry pi pico micropython, pi pico projects, raspberry pi projects python, robotics, robot, Pico robot
Id: ph10GSO8pDk
Channel Id: undefined
Length: 74min 36sec (4476 seconds)
Published: Mon May 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.