1

empowering creative people

Videos / Pycom Pytrack Getting Started Guide and Example Code

In this getting started guide we’ll use the Pytrack from Pycom to detect our location on Earth using the onboard GPS module and use the accelerometer to control a 3D model on the PC.

Search Videos

Related Content

Transcript

+

G'day everyone I'd like to show you a getting started guide for the Pytrack from PyCom. This device is awesome because it adds a GPS receiver and an accelerometer to your PyCom microcontroller and so of course the main things we want to see it do is find our location and know its orientation and acceleration in space.

So, a quick look at the hardware here on the bench and then we'll go to the computer. So here I have my Pytrack and a WiPy, as always, the LED at the top goes with the USB socket at the top we want to orient that correctly because it could go in back to front and we'll put that straight on to our USB cable connected to the PC that's kind of all we have to do with the hardware right now. Now let's deal with the software, so in the documentation here I've linked here to a separate tutorial I've made about doing a firmware update on the Pysense and Pytrack, have a look at that if you need to update, otherwise I'm just going to go in paragraph 3 straight down to the Downloads link and open that in a new tab. So, what we're going to do is get a single file with all the libraries from PyCom and we'll pull out of that the things we want to use for Pytrack and Pysense and a very good example that shows how the accelerometer operates. So, we're going to go with that green button on the right-hand side click to download, download the zip file. Alright so now we have all the PyCom libraries. I'm just going to extract both of those zip files in my downloads folder, I've pre-downloaded processing because it's a larger file and I don't want to have to wait for it later, I'll explain processing in just a minute.

So now what we want to do is create a project because will allow us to read in the GPS data from the Pytrack. Now we usually use "Atom" for these kinds of projects, so we'll use that today and we'll use the code that's already available in the master library here, PyCom library's master. So, going to open that up, there's a subfolder in there with the same name, now in here we have, examples which we'll use, we all need to grab something out of the Lib folder and most importantly at the bottom here we have Pysense and Pytrack. So right now, we'll go into Pytrack and there's a project here already for us to use. So, I've already got Atom installed on this PC, you open the Atom IDE and we'll do an open folder, if you need to know more about installing Atom, look at a previous video called IOT from scratch, where we go through driver’s development environment and hooking all that up. So, in the Downloads folder PyCom libraries, PyCom libraries, Pytrack, ok we're just going to open this Pytrack example here, there we go. Now we need to make sure that I'm using a Comport to connect to my Pytrack now, so I'm just going to have a quick look in settings, global and run down here, yes, I'm on content and if I check my device manager that agrees with what I have here, the serial device that's connected his content, thank you Device Manager.

Alright so if we connect to our device it's currently spewing data because I've been practicing this, so what I'm going to do is interrupt it by asking it to upload this project and because the code is in the main.py file, at the end of an upload, is an automatic reboot when the reboot finds the code in the main.py file it runs and it tries to run but here we have an error no module named "pycoproc", so back into the folder structure down a couple of levels jump into the Lib folder one of the library folders in there is pycoproc, grab that code file from there, copy it, jump back in to Pytrack into the library folder Pytrack and paste it in there. Right now, we can upload again, you can see the pycoproc file is in our project now so it's writing pycoproc to the Wi-Fi and there will be a reboot and then main.py will run.

Alright, so this first project is looking at the Pytrack and what it's trying to print here for us is the location and latitude and longitude and the problem I'm having, is that I'm operating in a room that's under a metal roof that has all kinds of things in the ceiling, ducting, etc. So, the GPS chip that's on the board has no way to see the satellites above our head, so every 30 seconds it tries to synchronize to the GPS satellites and it fails and prints (None, None) that's the latitude and longitude it can't find either of those. So, unfortunately, because of where my studio is I can't fix that now. You can see in the documentation there on the left that I started off with the same problem working at my desk and what I did was move the device onto the windowsill so it was literally staring out at an angle and could see you know maybe a quarter of the sky, so that would give it enough satellites to lock onto and you can see in the documentation again that after a little while of printing (None, None) it finally gave me the latitude and longitude, with that latitude and longitude, of course, you can paste that straight into the Google Maps search box and it will give you a pin on the map to show you where you are and it just so happens that core electronics is across the street from where that pin landed.

Now one of the things that disappoints me about the example code for this, is that what we really get from it is latitude and longitude there are many more fields to know from a GPS satellite including, what's the resolution that's operating at, does it know your position within, + or - 200m, 50m, 5m, what's the acquisition time, what is the atomic clock time as synchronized with satellites, there is a lot a lot of data including velocity and altitude that all comes from every GPS device but the example we've got here doesn't show any of that. If we try to find out what's going on we can see at the top of our main Python file the L76GNSS is the device that we're talking to, that's the GPS and of course we could just click directly on the L76GNSS file and see what's going on. What we have here is a function called coordinates, which is what's been called in our main file and at the end it returns latitude and longitude as decimals, if it fails to lock onto a satellite it returns none and none.

Okay so how do we get to all of that other interesting data that's available from the GPS, well reading back through the code and using a little bit of knowledge that I have about GPS, is that GPS is usually outputting their data as strings in a particular format called NMEA I can't remember what the acronym stands for at the moment, but here we have a line of code that says a variable NMEA, add to that variable or append to the end of that variable "read". So, there's a function here called "_read", all right so looking up through the code here's "read" defined at the top, the line 26 and all its really doing is using the I2C to read data directly from the GPS device 64 bytes at a time. So, the GPS is constantly spewing out information about what it knows about its positioning and that is read as text and then that text must be passed to find the meaning in the in the text. So if you were to build a project that wanted more information from the GPS, you would need to find another GPS library and hopefully one that you can find already in Python that takes NMEA text data in which case you can show it how to read data directly from the device and it might if you're lucky output GPS.velocity, GPS.altitude, GPS.acquisition time, all of those other features the code here is pretty brief and only really gives you allows you the longitude.

So, we'll go on from there and we'll try a different project, what we want to do is visualize the accelerometer data, part of the documentation, part of the files that we've just downloaded has a very good example of how to visualize accelerometer data using a 3D object that appears on screen and we'll do that now. So, what we need for that is a different development environment we'll just drop into my downloads folder, again I said I would explain processing later, well I'll explain processing now. If you've used Arduino, then processing feels right at home not because processing is based on Arduino but the other way around Arduino came after processing. So, processing is a is a little editor and when you run the code in your code editor, it will give you a graphical display box in which you can draw objects. So, you could draw a 2-dimensional or 3-dimensional or print text in there, but it's designed to be a graphical output where Arduino gives you an output to a microcontroller. So, we'll go into the processing folder that I've downloaded, and it doesn't have a Windows installer it just executes from where it is, so I'm going to run the processing executable. Alright I don't need to see that thank you very much, by default we're writing in Java in the top right of the window, here we can add a mode what we really want to do is Python mode, which is different again from Arduino because we would normally do C and C++ code in Arduino. So I'm just running through the installing processing and python mode subheading here on the documentation and very quickly we should have Python mode activated and through the wonders of video editing you didn't have to sit through that agonizingly slow download, so now for some reason the UI doesn't update itself so I'm going to click the tools tab and come back to modes, there we go we go green tick next to Python mode for processing 3, great. Close that back in the processing editor, will change modes to Python there we go, now the example code we want to open comes from, again our Python libraries going to go into the subfolder with the same name and then in two examples and then to Pytrack_Pysense_accelerometer, so that's the project to open.

Now we need to build two projects in here one is to use processing for the visualization of what's going on, the other one is to use Atom to get the correct code onto the microcontroller. So here we have a visualizer folder and we really need to create next to it another folder which we'll call "device". All right now the main Python file belongs on the device and in the device, we also need to have a libraries folder, which is applicable to the PyCom expansion board that you've got, you can use either a Pytrack or a Pysense for this because they both have the same accelerometer fitted. So now I've got the Pytrack wide up now, so I'm going to jump back down to the libraries and go into Pytrack and grab the Lib folder from there, go back to my project, examples, accelerometer, device, paste the folder in there, have a look in the Lib folder, it's even got pycoproc, that's good. Now in Atom, we want to open this project, so we'll go to Atom and say open folder and go to the examples, accelerometer, device and select that folder. I'm just going to get rid of the Atom IDE behind that. Okay so here's our project, here's our main Python file for the project and what it does is it creates an object to control the Pytrack and then connects to the accelerometer device and then reads from the accelerometer pitch and roll and prints those out. So, let's do an upload.

Writing main.py again, we have the main code in our main.py file, so every time the device reboots if I was to reset it now, it would come back up and start printing data immediately. So, there we go, there's a stream of data coming from the device which is a roll and pitch, so imagine an aircraft pitching would be nose-up and nose-down, rolling would be banking to the left banking, to the right the other factor there would be yaw, which would be turning around its centre point, but we can't get that data and there's a reason for that.

Okay so we have the device spewing out its pitch and roll information and now we want our visualizer to turn that into an on-screen 3D representation of what the device is doing itself. So, we need to disconnect here, because we're going to use the serial port in processing. So, in processing we will open, what I've got here is PyCom libraries, examples, accelerometer, visualizer and we're going to open the visualizer.pyde. Okay the main thing we need to fix here is at the top of the file we have the wrong Comport define, change that to Com10 and we should be able to run immediately. So, the Comport is going to be used to read data, not to write data and it will read the data that's coming from the Pytrack.

So, hit play this is what happens with processing you get a graphical window and if I now start to move the device in space it should rotate the model on the screen but as I find this doesn't work straight away. So I'm going to rotate the device into a tail down position and what you'll find is that after a minute or so the visualizer will start to catch up it's going to slide the visualizer over here for a minute, what I found is that each time the visualizer starts it seems to need to work through a backlog of something and what it's actually doing is, when it connects to the serial port to read the serial port is already full of data, there are 64 thousand bytes of data in there and they quickly build up. So it first has to read through all those 64,000 bytes of data from when the thing was lying flat on the table and then it starts to get up to date, but if you look at the code there we go it's starting to move, now it's in its tail down position, if I move it to head down position it should eventually roll over, there we go but it's very laggy it's very jittery it's now tail down again, it's taking a good few seconds to catch up.

What's going on there's a problem here. Well through all sorts of debugging, fault finding on this code, I realized that there is a line here that says my port clear and helpfully the note above says, clear the serial buffer and consume the first line in case we missed the start of it. So, it basically says make sure you're starting with right now, for some reason depending on the system, my port clear action is not acted on it seems to be that that line too quickly follows the configuration of the serial port itself. So, above the serial port is created and the serial port microseconds later is cleared but the action never actually happens. So, if we implement a slight delay, one-millisecond delay, we can give the background processes on the device a millisecond to catch up, we'll need to import the time library for Python time library, so we can use the time.sleep function and now we'll start it again. Now it looks less jittery already, if I put it tail down it hasn't worked, let's go with a larger value, let's give it 10 milliseconds, well it's still not very happy, this is my solution, this is the code that I've created to make this work, how am I getting it wrong, for some reason I had to give it a hundred times more delay, than what I imagined was necessary okay now hopefully you can see on the two camera views that as I rotate this device around it's being mimicked on-screen, now that poses a question.

So, if this is an accelerometer as advertised on the brochure then, how is it working out roll and pitch and if it is working out roll and pitch, isn't that a gyroscope effect well it is and there's another thing interesting here is, that we can't determine what the yaw rate is. If we turn the device around the central axis the axis of gravity you can't actually detect that that happens, you can detect a tilt in either plane but you can't actually detect a rotation around the centroid and the reason for that's a little complicated to do with how accelerometers work it really is a 3D accelerometer, in the examples you have another example that's for the Pysense I think, that will print you out not only the roll and pitch but it will print you out acceleration in the x-axis and in the y-axis, what would it be x and Y and Z, so you can see those values. They're a little hard to understand just from looking at the numbers though but the accelerometer really does give you the output when the device moves up and down in its that's probably at z-axis its x-axis and its y-axis you can get all of those values but from those values you can also work out basically a gyroscopic effect what is the angle on the board and that's basically to do with a small mass that's hanging inside the accelerometer and it can work out which direction gravity is active on that mess.

So, I hope you like that little getting started guide for the Pytrack, hopefully you've got a window or somewhere close to the outdoors that you can use your GPS device and get some good coordinates off it because I can't get them here and hope you find the accelerometer fun to use as well. As usual reminder that the documentation is down the bottom, thanks for watching, Core Electronics.

comments