How to Implement an Inertial Measurement Unit (IMU) Using an Accelerometer, Gyro, and Magnetometer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is Scott Lobdell this is just a quick tutorial video on how to go about implementing an inertial measurement unit or an IMU using conventional sensors that you might have in a phone or either just sort of a sensor kit it's typically a you know fairly commonplace so busy that involves a setup where you have an accelerometer a gyroscope and a magnetometer her let me spell correctly okay so if you have these three sensors and you can effectively fuse them together and toss them into an eye on you and get some outputs from them so if we throw these into an IMU and we'll go through the implementation details of this then the output of that should be pitch yaw and roll and then you'll also be able to turn ascertain an acceleration vector in terms of absolute absolute value so you can get absolute acceleration north east and down and so this dramatically simplifies sort of localization problems so just to sort of expand from here if I have these three sensors then what I'm going to get is a you know vector in every single direction so X Y and G XY and Z for the gyroscope a magnetometer and accelerometer alright so here are your raw input two fairly straightforward to sort of get these values from your sensors now the first thing that you're going to have to account for is that your magnetometer will likely not be calibrated so what this means is that depending on the device that you have what's sort of you know like where the battery is placed in that phone or a single board computer or whatever and then whatever else you might have the fate of a phone mounted on a car mount for example that might affect the magnetic field of the sensor so what you'll need to do is calibrate for your particular location and your particular setup and so what this effectively is is that if you or what you can do at least if you just take the phone and you sort of start rotating it in all directions then you're going to end up it'll be possible to like plot some points and start to see here but basically you'll end up getting some points that you know look something like this if I plot like a whole bunch so this is the y-axis you know you get the same on the x-axis here you'll get like a bunch of point two plot it I won't draw the z axis but you get the idea ideally this should create like a perfect sphere if you rotate it in all directions so all you need to do is just take the median values so if I take the median X now take the median Y and then I shift these values then I'm going to get up get a thing where everything is centered as I rotate it so basically you just need to take the median x y&z value over a period of time where you've calibrated and then apply some offset to get calibrated values so essentially if we take these now apply some offset then we'll get you know cleaned em X and then Y and NZ okay so that is the first step the next step now that now we have a good magnetometer reading we have gyroscope we see we have acceleration as L as well now if we pass all of these values into an a HRF which is an attitude heading in reference system we can get a rotation matrix from this so I just sort of gloss over some details so I'll catch up on that real quick so essentially there's multiple implementations of a HRSA's which is essentially some sort of sensor fusion between all nine of these inputs so you can get you know more clean and accurate data if you if you use various sensors that cancel out each other's errors something to that effect I'm not I'm a pragmatic programmer not a college professor so excuse me if my my explanations here aren't very great but one such implementation of an h RS the one that I've been using is the magic quaternion update and you can google this and you'll get much more in-depth explanations of this you can rewrite papers about it but that's a good thing you you actually want reference so the point being here is that you don't need to know how to implement this you just need some sort of implementation of it that takes these inputs been shown to spit out some outputs the output here is going to be a quaternion which if you don't know what quaternions are i'll do my best to explain them concisely but I would recommend taking the time to go and watch another tutorial video to sort of explain what these are and what they mean exactly but if I if I just give you a gloss over concise explanation a quant earn ian is a is impossible to visualize in your head because it is an abstract mathematical concept but essentially it allows you to have a vector with an up direction and so in this way you can represent a pitch you on roll you can represent any sort of three-dimensional angle and the reason why you can't visualize it is because it's like a four dimensional vector on a hypersphere so I won't go into details on that the important thing to note is just that this a quaternion is a way to represent angles in a 3d environment and so once you have a quaternion from here you can get your pitch yaw and roll values and you can almost stop there but again the this is this is how to get pitch yaw and roll from here is just really an implementation detail all you need to do is just do a little bit of googling it's also important to note that if you were to stop here you essentially need to account for magnetic declination offset here so when you if you're not familiar with this term basically when you read a compass and the compass is pointing north that's magnetic north but it is not true north because essentially there is the Earth's magnetic fields are not a fields are not straight up and down depending on your location there's going to be some offset so what you'll need to do is take your particular location and basically just apply some offset so I'm here in San Francisco so that happens to be at this point in time a 13.2 magnetic declination off the offset so all I need to do is rotate rotate these values by that amount and now I have pitch yaw and roll but if we want to complete this and get acceleration values as well in usable format we can keep going so the acceleration values you get from your sensor is going to be you know in body coordinates they're going to be relative to your device that's making the measurements so this isn't particularly useful if you're doing any sort of like localization or or mapping or things like that so you want to be able to to output values and some of you can understand so we can stead do is we can transform these sorry we can transform these in terms of North East and down which is a much more understandable format in addition to this if we if we do this rotation then it's also trivial to strip gravity out of the quest that equation so let's go from there so if we have a quaternion then we can convert that into a rotation matrix and then this will make it so that we can actually do some sort of simple math to go about rotating an input vector so you this is again another implementation detail it's not really hugely important but what you need to know at least is that this outputs a three by three matrix and so again I'm glossing over some details here but in a lot of these cases you don't need to understand the implementation details if you can just sort of get a copy on stackoverflow or whatever but you should at least understand how matrix multiplication works or at least like dimensions of rotate of matrix multiplication so for example it's important to understand that you can have a three by three matrix and you can multiply that by a three by one vector that is you know useful to know so anyway I'm going to sort of like move this over here so we have R plus R again we invert it into rotation matrix so what this means then is that if I have some rotation matrix and my device is at rest so I'm gonna move this up little five rotation matrix and my device is at then that means that I will get acceleration value in terms of freefall of gravity so this is what my a X a Y and a Z is going to look like at rest essentially zero zero one here's the gravity vector so if I take my rotation matrix and multiply it by this vector here I should end up getting my actual a X a Y and a Z coordinates so this would be a good point to validate that everything you've done correctly up to this point is working if you can take a rotation matrix multiplied by this vector and get your ax a y AZ values then the way that you've computed work your rotation matrix is correct so now if then if I say I'm going to go and get another piece of paper here so if I say that this rotation matrix multiplied by this vector is this value then I should be able to you know given these values and given this rotation matrix I should be able to solve for this right here which is what we want one output acceleration in Northeast and down values so what I can instead say in this equation is that you know absolutely will say acceleration north so eration East acceleration down equals now you know with normal arithmetic here you just divide right here you know divided by this and then put it over here but these are matrices so you can't actually divide so it's just an inverse matrix so if I say this is rotation matrix inverse multiplied by my my acceleration vector then I should be able to solve for all of these values so this is another sort of detail where you can find some code that just takes the inverse of a 3x3 matrix you would then multiply these this matrix by this vector and now you will be able to get acceleration in terms of northeast and down it's also important note that my coordinates or the direction of my axes are not necessarily correct so this might actually be acceleration southeast and down the point is it doesn't really matter but you should be able to sort of implement this and then find out which way your vector is facing and then just apply the appropriate multiplier to put them into the correct axes that you can at least work with but the important thing is that these are at least in these directions so anyway back to where was that you can imagine that if my device is sitting at rest then I will be getting some values that are close to you know zero zero and one for gravity it was also nice at this point because this now you can tribute remove gravity from your accelerometer so if basically if I just offset this value by one then at rest I'm now zero zero zero and now I can easily sort of move my device around and I can get acceleration in absolute terms there are a lot easier to work with and understand so now we finally have all these values the last thing to account for is that same magnetic declination offset I brought up earlier so over here so now I have a vector in north east and and down if I take these two values I'll just go ahead and rewrite this as acceleration north acceleration East and acceleration down if I take these values and I rotate them by magnetic declination offset because these are still in terms of magnetic north and not absolute north apply that rotation and now I have my final values and so from here I'll do like another tutorial video video but you can now take your accelerometer and sort of merge it with GPS values for instance so you can dent dead wreckin and get like better localization and so forth so yeah that's all thanks for watching you [Music]
Info
Channel: Scott Lobdell
Views: 109,250
Rating: 4.9259257 out of 5
Keywords: AHRS, IMU, Emlid Navio2, Accelerometer, Gyroscope, Magnetometer, Inertial Measurement Unit
Id: T9jXoG0QYIA
Channel Id: undefined
Length: 13min 16sec (796 seconds)
Published: Sun Mar 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.