9-Axis IMU LESSON 12: Passing Data From Arduino to Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Applause] hello guys this is Palma quarter from top tech boy calm and we're here today to learn how to pass data from your Arduino to Python running on your desktop computer a lot of reasons you would want to do this we love the Arduino because it's very easy to interact with sensors and actuators in the real world with the Arduino but very quickly you're gonna reach the point that you need more horsepower in analyzing that data best way to do it take the data on Arduino pass it to Python and then once you're in Python you can live graph data you can create 3d visualizations of the data you can do very sophisticated data analysis you can send it other places once you get the data into Python the sky's the limit so what we're going to talk about today is how to pass that data now specifically in this series of tutorials for you guys that are playing along at home what we are doing is we are building an on axis inertial measurement system based on the Adafruit B and 0:55 non axis sensor connected to an Arduino Nano now what we're going to show you today is how to take that data that is being taken by the Nano and to pass it to Python now if you're not playing along with this particular project it's okay what I'm going to show you is something that will work for just about any project that you're going to do now I have to tell you that I am NOT trying to show you the most efficient way of doing this I'm not trying to show you the fastest way of doing it I'm not trying to show you the smartest way to do it I'm trying to show you the simplest way that will work for almost anything and it's very easy to understand and I will just kind of give you a real quick overview of the way it works on the there's kind of two parts to the problem on the arduino side you've got to collect the data and then you've got to pass it okay on the python side you've got a catch so there's two parts to today's lesson first part is going to be how to pass the data from the arduino the second part is on python how to catch it so let's just jump right in oh and then so i'm going to show you just real simple we're gonna make up data show you how to pass it so if you're just here kind of like a drive-by shooter you're just here to learn how to do this you'll learn it for you guys that are playing along with this project then I will show you for this data that we're collecting off the sensor this data that we're collecting off the sensor I'll show you how to pass that data the data that we ended up with I believe that data we ended up with in lesson number 10 I'll show you how to pass that to Python so so this will work for either people that are going through the series or people that just want to know how to do it but in either case I need you to not forget your iced coffee and I will need you to call up your Arduino IDE and I think this probably looks like a pretty good view here start with the blank program and what we need to do is I want to show you that we can send dynamic data and we can send kind of packets of data or a different channel so I'm going to create three channels an X variable a Y variable and a Z variable in the real world this might be temperature pressure and humidity or something like that but we're just going to say I'm going to have an int in that I'm going to call X and I'll initialize it to 0 I'm going to have an int that I call Y and I'm going to initialize it to 0 and I'm gonna have an int that I'm gonna call Z and you guessed it I'm gonna initialize it to 0 now we have our three variables now the way you send data to Python you send it over the comm port so if I'm gonna send data over the comm port I need to turn it on how do we do that with our old friend serial dot V again and while we seem to be in the habit of using 9600 when I'm getting data into Python I don't want to fool around I want to get it there quickly so I will use 115 200 so now my serial port is turned on being mindful not to forget my semicolon as I do in so many different times okay now we want the data changing so let's just say that X is equal to X plus 1 let's say that y is equal to y plus 2 and then not you Y is equal to y plus 2 and then we're gonna have Z is equal to Z plus 4 so we can kind of see the data changing alright now this is the simple bottom line on the Arduino side the way you send the data is kind of like we would do a print X and then we need a delimiter so we'll do print X and then print the character the comma and then we'll print Y and then we will print the character comma and then we will print Z but since Z is the last number in a packet we'll do a print L in so it's sort of like print number print comma print number print comma print line number and then that's a packet that we can pull in all at once on the Python site and so let's come down here and let's just jump right in and so I will say serial dot print and what do I want to print I want to print my first number which is X and then I got to put in that delimiter so I want to print and it's got to be the string right it's got to be the string so it will be the string comma and then serial dot print and what do I want to print now I want to print the variable Y and now I got to put in another dilemma or serial dot print and another comma okay and now don't forget the semicolon now we got to bring it home right we got to put in that Z okay we have to put in that Z so I'm going to say serial print L in okay because this is the last number I've got to tell it this is the end so I put do it with a serial print Nolan and now I do Z with a print all in okay let's download this and two things to make note of remember 115200 on the bottom and we have to make note of what comport we are on I come down here under tools and I look and for me I'm on calm five you'll be on something different but make note what your comport is and now let's download Oh denied Oh pint at the time did you guys catch that print okay hopefully you didn't dumbly copy my mistake okay boom this is gonna work okay now let's just pop open the serial monitor and see if it makes sense what we are saying okay oh and that's going by so fast you know guys you can send things faster than you can really deal with it we should have put a little bit of a delay in here and so I'm gonna say delay 100 just so that I am not cramming the data faster than what it is easy to deal with so let's download this again and we will pop open the serial monitor okay much much better you see X is counting by one y is counting by two Z's counting by four this is not surprising right but this is just making sure before we jump in and try to do things in Python let's make sure things are behaving as we would expect on the arduino side okay we are ready to go now and start writing on the python side a couple of things about python okay if you're a Python expert you already know how to you know install things and all that sort of stuff but if you're new to Python make sure that you install Python the way I showed you in lesson number 11 because I showed you how to install Python in a way that our helper program pip will be there and will work and also your paths are set up so it won't get lost okay if you just have an old version of Python and you don't know what you're doing go back to lesson number 11 and install it like I show you and also if you want that pip you've got to install like three points something the two points something doesn't have PIP and then pip itself can be kind of hard to install so bottom line is do what I did in Lesson number 11 and then things today in lesson number 12 will work now there is one thing that you are going to need okay there is one thing that you are going to need that did not come with Python so in Windows I need you to open up a command line if you go to the bottom left of your screen and in your little search bar you type CMD you will see that this command prompt app comes up you need to open your windows command top which I've just done okay now since you followed the less that you followed the instructions in the lesson 11 pip should already work for you and what I need you to do is do a pip PEP install pip install PI P Y ser al pip install PI serial ok it's collecting it it's installing successfully installed boom we should have PI serial now what does pass serial do remember we are throwing data down that comm port on the arduino side this PI serial is a library that will allow a Python to go out and catch the data that is coming down that comport alright so that is installed I can close that now and I think we are ready to open up python so again i'll go to the lower left of my screen and I will search on IDL e that's my integrated development environment for python and you can see here I have idle for Python 3.7 for me at 64-bit for you it might be 32-bit if you have a 32-bit machine that is fine so let's open that up and boom one thing another thing that annoys me is it opens up a shell instead of a program window but that's ok while we're in the show you can kind of just type in individual lines of code for Python and it will execute them one at a time so I want to just make sure that that past serial installed so I'm just going to type in a command import serial and the only reason that I'm doing this is just to see if it installed okay boom I didn't get an error which means when I give the command it goes out and finds it forget that now we know that it works all right so now what I want to do is I want to open up a window to create a program so I come to file and I say new file okay this is the environment now that I will write my program and let's see I think you can see that I think that a person like you should be able to see that I'm always try try to be mindful of my fonts that I'm doing something that would be easy for you to do for easy for you to see okay so we're now going to write a program that will go out and grab that data that is coming off of the comm port so first thing since I'm going to be interacting with the comm port I need to import serial I find it a little awkward that the library is called PI serial but then we import serial but that's just the way it is when we set up that comport we need to put a delay in there another annoying thing about python is if you want to delay you have to import the time library so now we will be able to delay okay now I want to create an object that I associate with that comport okay and then I enter if I want to interact with the comport I interact with the object so what I'm going to call the object I could call it whatever I wanted to but I'm going to call it Arduino data okay that's the object puts it equal to serial that's that serial library dot s IL so notice be mindful of the upper case lower case so serial all lower case dot serial with an upper case s and now I have to tell it what comport am I gonna work with well I showed you on mine it was comm five on yours it's gonna be different but whatever comport that Arduino is on for me it's comm five what was our baud rate 115 200 and so now my comport here has to match what the Arduino is in the baud rate estimate match now I've got an object with which I can interact get the data now when I set that up I want to give it a little delay so I'm going to do a time dot sleep and this is in units of second so I'm just going to wait a second to make sure that that serial port has a chance to get all set up nicely okay now I want a while loop I want a while loop that looks loops forever so I will say while one equal equal one when does one equal equal one one always equals equals one so this is kind of like your avoid loop in Arduino which is going to loop forever and then remember in Python we keep track of our clauses or our Wiles or our ifs based on indentation so after it sees that : and I hit a carriage return or an enter what you notice is that indents everywhere in this while loop you've got to keep that indentation straight okay so now what do I want to do well I want to wait until there's data on that comport so I need another while loop so while Arduino data dot in waiting equal equal zero and then another : so what is this thing I'm gonna go out I'm gonna look at the serial data and I'm gonna sit there and just loop until there's data there so if there's no data there the in weighting data equal equal zero what do I want to do absolutely nothing I just say pass and so what this little loop does is it just hangs until there's data there all right then once there's data there I pop out of that while loop and I come back indented for the things that are gonna be inside of this file loop and what do I want to do there's data there if I get to this point in the program there's data there what do I want to do I want to read it being so I need to create a variable huh I think I will call it data package you could call it whatever you want it but I think data packet makes sense because I'm going to read that whole thing from the first character to the first line ending which is that print I went I'm going to get that first data packet data packet is equal to what's my object or do we know data and then what do I want to do I want to dot read line that will read everything until that first return or that first line ending so now I have data packet you know what I think at this point I should have I should have a data packet so let's just print it and see if this thing is gonna work so I'm gonna print the data packet okay and so now let's run module okay source must be saved this annoys me I gotta figure out something to save so I will just call it catch data and then overwrite what's already there yes okay so now it is starting the shell comes back in boom look at that boom in Python I am grabbing that data that is coming from Arduino a couple of little things to notice it brings it in as one big string and then just to be annoying it decides to put this be single quote in front of the string that you sent now also this / r /n is kind of like the carriage return that could be associated with that println that could be the thing that tolet was the end of the line so i'm not going to blame python for that that's probably what we sent from arduino but we need to get rid of this annoying be okay this be : and there is a little command that will do that for you let me kill this and we come over here and so before we print data let's strip that little annoying thing off so I'm going to say data packet is equal to the string value of date date packet only I'm going to give it a format that I want I want the format utf-8 what is that format that is the format that doesn't have that annoying B on it so now if I print data packet if I'm careful to make sure that I do the capitalization the uppercase lowercase correctly this should print shouldn't have that be now so let's run module okay boom do you see that okay X is counting by one y is counting by 2 and Z is counting by 4 that is most excellent so I'm getting that data in there okay now that looks good but what you got to still realize is that's a big long string and if we're passing this over we're kind of gonna probably want numbers okay we're probably gonna want numbers and not a string and so I need to take this string which is number comma number comma number and I need to split it into an array of kind of like the first number the second number and the third number so I need to create an array in what will we call that array well I'm going to call it the split packet okay so it's going to take data packet and instead of being one string it's going to split it into three strings and so I'm gonna say split packet which is going to be an array it's going to be equal to data packet okay dot split so this is saying take data packet split it into its individual numbers and put it into an array called split packet and if I'm gonna split it I got to tell it what character to split it on now what was our delimiter what was our delimiter it was the comma so I can put a single quote and then I can put a comma and then I can put a single quote and that should split data packet at the comma and then it should put the values in the array split packet okay now split packet should have those three numbers that I want of XY and Z so now I can just come in and say X is equal to split packet this is an array so I got to give it an index X was the first one what's the number that goes with the first one it is the zeroth term so it will be the 0th term unless we count from zero in programming many times Y is equal to this split package and that will be the one term and then Z is equal to split packet two which is the third term all right if I just did this I would end up with three things XY and Z but they would be strings because if I take a string and I split it I'm gonna end up with strings and I don't want the string six or the string 45 I want the number six or the number 45 so what I need to do is I need to float these when I do this so this will convert the strings to a float okay so that I will float I will convert when I put them in X I will convert them to floats and that way I'm dealing with numbers because if you try to do math with strings it gets sometimes it'll actually do it and you get really crazy results and you could kind of start driving yourself insane okay now I have XY and Z let's print them out nicely format it so I'm going to say print and first thing I want to do is print a label so I'm going to have the label x equals and then a comma and then the value X and then the label I need to put space before the Y so that things aren't jammed together y equal and then print Y and then print the label space space Z equal and then comma and then print Z I think this should work the neat thing is now I have a number I can work with X a number I can work with Y a number I can work with Z so I passed the data I caught it I parsed it and now I'm gonna see if I can print it let's hope this works run module okay hey this looks promising boom look at that do you see that look at that in there numbers they're not strings x equals counting one y equal counting by two Z equal counting by four so if you came here to learn how to pass data from Python to I mean from Arduino to Python mission accomplished okay but if you came here to move forward on our little non access inertial measurement system let's go ahead now and let's do a little bit more so instead of just passing these made-up numbers let's pass that data that we ended up with in Lesson number ten and so what I need you to do is I need you to go to if you have your code from lesson number ten good for you but I know most of you guys and so you probably don't have your code so what I need you to do is go to the most excellent wwq tech boy calm and I need you to search on non axis I am you less than ten and you cannot see that Canyon I'm sitting here ridiculing you okay where is the here it is okay so I need you to go to www.hsn.com to arduino okay and this was our arduino window so I'm just gonna do a ctrl a to select everything that's control a to select everything ctrl V to paste our old program right now we pasted our old program and remember what that old program did was it was coming up with approximations for roll pitch and yaw and then we would do a better approximation a better approximation and a better approximation so if we download this this is gonna run because we ran this before oh okay very good very good okay I was getting cocky right I was getting cocky remember you can only touch that comport with one thing at a time and I do believe that we left this Python program running and because we left this Python program running it has locked up that comport so we got to kill it and then we come back over here and now we download it I'm very confident this time very very confident okay ready waiting for it it's kind of slow to download okay boom got it now let's just pop open our serial monitor and you see that we have all of this data all these different things that you know from the earlier lessons what those things mean okay but now that we're going over to Python I don't need all those intermediate results remember we had like a tilt directly from the accelerometer that was a pretty good tilt but it was subject to vibration so we had all those intermediate ones and I don't want those now I do want the calibration data so I want the excel the gyro the mg and the system those were our calibration numbers so I'm going to or the status of our calibration so I'm going to keep those but everything before that I am going to eliminate so this is the calibration status of accelerometer gyro of magnetometer and system and now I start getting a lot of those intermediate calculations for roll pitch and yaw and I'm still calculating them but I'm just not going to print them because it's just gonna slow me down I'm gonna come down to this point so system calibration a comma and now theta is approximation of pitch Phi is approximation of roll and then sy is approximation of yaw so I'm just gonna send those seven numbers so let's download it and we're gonna check it because man make sure before you go you know what I see students do they write the Arduino code then they go to Python and write hundreds of lines of code and then it doesn't work before you go to Python make sure that Arduino is working like you expected it to so we will pop open our serial monitor okay and now we have one two three four five six seven channels of data I see commas between them all so this looks good all right because it looks good I am now going to go over and I am going to go over to my my Python program because remember this Python program is already doing almost everything that we want so we import serial we import time we open the comm 5 port we rest for a second then we have the loop the loop now we read data packet that all should be the same ok then we strip the nonsense off the data packet that should be the same and now split packet should be the same but now we're gonna have like 7 values here so I'm just going to take this I'm gonna just paste it and then I can come in and fix things ok but that first number was like the accelerometer Cal the second number that we sent was the gyro Cal so we'll call it G Cal and then the third number was the magnetometer Cal the status one though I still need one more right because there were 7 and I have 6 so I need 7 of these things because there's 7 pieces of data so accelerometer calibration status gyro calibration magnetometer calibration and then this would be a system calibration and then this I believe we sent was pitch and then we sent throw and then we sent yaw all approximations suitable for impressing your girlfriend but not to be put on fighter aircraft or drones or wonders okay so the a calibration would be the zero than one two in this one is the third one this one is the for this is the five and this is the six and since we started counting at zero then that's seven numbers all right now we are going to want to print out those seven numbers and I need to be mindful to make sure that you can see this I think you could see everything that I did so far okay so now what we need to do is we need to print all this nonsense out so I'm going to hopefully you know what I'm going to do I am going to go to the full screen view because otherwise this thing is going to go off of the it's going to go off the side of the screen so that you can see it so now we want to do our print so the first thing that I want to do is I want to print a label for a Cal okay a Cal and then I want to print I guess I should say a Cal equals and then a comma and then I'm going to print the variable a Cal and then I'm going to print G Cal and then I'm going to print the variable G Cal and then I am going to print I am still gonna run out of space you know I'm gonna be tiny before this thing is over so I'm gonna print G Cal and then I am going i said say cheek l equals is a good label G count equal G Cal and now I'm gonna have em Cal equal trauma in Cal and this is going to go on forever huh and then you know what I'm gonna do I'm gonna do something crazy here I'm gonna flip myself up here I think I'll be less likely to be in your way okay so M Cal and then we're gonna print sis sis s Cal my s Cal and then we are going to print the label roll the excitement builds as we gonna figure out what happens as I run out of space here roll equal comma and then we're gonna print I think pitch was first pitch equals pitch I think you can see that right and then we're going to print roll and then comma comma roll and then we're going to print the label y'all equal oh yeah now let's go back and clean this up let's use equals everywhere that I didn't use an equal I think I miss that one and then let's go up and look at the roll pitch and yaw yaw was uppercase as well so I need to make sure that that one is uppercase does that make sense I think it really should you know this is terribly annoying because this Idol doesn't have a left and right scroll bar at the bottom so that makes it most annoying okay but I think that looks good so I do not like to be floating up here at all I feel very comfortable in the lower corner I do not like to be floating up there so let's see there we go alright so let's see if we can run this crazy program huh be mindful of my window here I think I need to go back to this other yeah let's go back to this one and now let's come over here and run it run module ok tension builds boom we are getting data man we are getting data ok I'm going to go back to the let's see I'll go back to the full view and what I'm going to do is I'll try to hold it up here where you can see it but if I just swing it around the gyro had already calibrated the Chi Cal was 3 I swing it around and now the mag Cal is 3 in the system Cal is also 3 but I need to get the accelerometer calibrated so I'm going to give it a 45 degree angle I'm going to give it a 45 degree angle right hold it for like 3 seconds I'm gonna give it the upside down view upside down with pitch and then upside down with roll ok and now I've got everything calibrated ok everything is calibrated and we will come back to this view here so you can see the data so what is good is is what is very good is that we have everything calibrated but now I need to change the view a little bit I need to change the view so that you can actually see the roll pitch in Yong because that's what we care about so let's see if I can do that and ok I'm gonna make the font a little bit smaller so I'm going to come up here and say configure idles so that everything's on one row I try to keep the font as big as I can so you can see it but here it is not okay and I think you will do best with this view alright so now you can see that we're all calibrated and so let's see you guys watch pitch are you watching pitch I bring the nose up and pitch goes up I bring the nose down negative okay roll to the right that looks good roll to the left that looks good and now as I y'all let me go back to pointing about north okay pointing about north looks really good east south west okay guys boom do you know how important this is we are getting numbers right we are getting numbers and I just had one terrible thought I need to make sure that when we did this yeah when we copied and pasted the floats and so those really are numbers okay guys this is really really exciting do you realize how significant this is we are taking data and we are getting very good approximations for rolled or not very good we're getting approximations for roll pitching you off which are pretty good probably up to about 45 degrees and we have done some compensation right we did that complementary filter so we've got numbers that are not really too susceptible to just normal motion and vibration and we've got the data over to Python so what I want you guys to do is play around with it and just see if the numbers make sense as you're reading them off of Python and the next lesson which will be lesson number 13 we are going to install visual Python which is a library that will allow us to do those simulations and those really cool 3d visuals okay guys I've had fun today I hope you've had fun leave me a comment down below let me know if anyone is still playing along at home we are on lesson number 12 thousands and thousands of people started how many people have made it through lesson number 12 let me know palma quarter from top tech boy comm i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 9,939
Rating: undefined out of 5
Keywords: BNO055, Arduino, IMU, 9-axis
Id: 8IUHfKKE0tM
Channel Id: undefined
Length: 37min 48sec (2268 seconds)
Published: Thu Oct 31 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.