Raspberry Pi Pico W LESSON 69: Cleanly Exit MicroPython Threads When Program Terminates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy dcom and we're here today with episode number 69 in our incredible new tutorial Series where you're unleashing the power of your Raspberry Pi Pico W what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the Kepler kit for Raspberry Pi Pico W now hopefully most of you guys already have your gear but if you don't take a look down in the description there is a link over to Amazon and you can hop on over there and pick your kit up and believe me your life and my life are going to be a whole lot simpler if we are working on identical Hardware but enough of this Shameless self-promotion let's talk about what I am going to teach you today and what we're going to do is we're going to take up where we left off in lesson number 68 now what we did in lesson number 68 I showed you how you can run both cores on your Raspberry Pi Pico W and we did that by creating a thread that ran on the second core while we ran our main program on the primary core okay so we could run basically two programs on the two cores one a thread and one the main program Now it worked very well but if you played around with it very much probably you started running into some issues when you tried to terminate the program and this is what happens when you are doing threading on the Raspberry Pi Pico W now nor normally let me come over here now normally when you are running a program and you come up here and hit the little stop button it stops the main program and that is just dandy but the problem is is that if you start a thread and you come up here and then click the stop button it will stop the main program but often times it will leave that thread running and leaving that thread running running can cause several problems first of all if you come up and try to save a program you can't save it because the old program is still running the thread of the old program is still running the other thing that can C happen is if you come in and try to run the program again you'll get all types of strange error messages talking about the backend this and the back in that and so what we need to learn how to do today is we need to to learn how to terminate our program where we cleanly exit and cleanly terminate the thread as well as the main program does that make sense I hope it does okay now how are we going to do this we're going to do this basically with the same circuit that we were working on last week in lesson number 68 just pretend this beautiful red button isn't there because we're not going to use it in today's lesson it will be part of the next homework assignment but just pretend this red button isn't there okay so what we have is we have a Servo connected up we connected it just like we had it last week we have the red power wire coming to physical pin 40 which is 5 volts we have the brown which is ground coming to physical pin 38 here and then the orange wire which is the control wire we have coming to GPI pin 17 okay and even though you can see that behind my head it's basically three wires and it is physical pin 40 for power it is physical pin 38 for ground and then it is GPO pin 174 control then we have two LEDs a red LED and a green led the long leg of the red LED comes to GPI pin 15 and the long leg of the green LED comes to GPO pin 14 and then the short leg of the red LED goes through through a 330 Ohm resistor to our ground Rail and then our ground rail is established over here connected to physical pin 3 and then similarly for the green led the short leg of the green LED is connected to the ground rail through a 330 Ohm resistor okay so that's how the circuit is set up and then what we're going to do is we're going to go out and get that code that we ended up with in lesson number 68 and so you guys can come over here to the most excellent www.top techboy docomo is use this happy little search icon and search on something like micropython multicore programming and uh then you should come to this page and you can come down here and if you click these two little page icons you can copy that code and then you can come back over here to th and you can p paste that code into th now just one quick reminder we did save a Servo we installed a Servo Library if you have not done that yet you will need to come to www.top techboy do.com and this time you need to use the happy little search icon to search on something like Servo library for the Raspberry Pi Pico something like that you'll come here what you'll need to do is you'll need to snag this code and then you will need to come over here you'll need to paste it into fonnie th and then you'll need to save it in the lib folder in your lib folder on your Raspberry Pi Pico W you'll need to save it as uppercase serv. lowercase py so uppercase s r v o. lowercase py now I'll just show you that here to make sure that it makes sense what I'm saying we can come over here and View and we can View files and you see down here we have the files on our Raspberry Pi Pico W you should then look in lib and you should have that uppercase Servo dopie once you've done that then everything should run just like what you're seeing us do here okay so wow that was a lot of background it is time for us to get in here and start coding this thing okay let's start by running this program and let's just see if this does what we expect now what we expect is the green LED should be blinking fast as the servo is going clockwise and then it's as it's going backwards or counterclockwise the red LED should be blinking and we did that by operating LEDs in this second core this other core and we did that by creating This Thread okay let's just run it and make make sure that it still works I'm going to run that so backwards the red LED is running forwards the green backwards the red and forwards the green so that is really working exactly like how we wanted it to now what I want you to see though is watch down here and when I try to stop the program okay what it did was it tried to stop stop and then it did a soft reboot and then it hung up okay so the program seems to be hung and if I try had to run it again uh it said could not interrupt the process so what was the problem that time what happened was that time what happened was it killed the main program but that thread that thread called other thread kept running so what we're going to have to do is we're going to have to unplug this and then we're going to plug it in again and let's try running it again so this time I need to come back over here and connect to the board again right back behind me I'm connecting to the board again now that I'm connected I'm going to run it okay and the thing is running again this time I'm going to try to kill it with contrl C and contrl C doesn't Okay and again I got all these errors here different error than before but is still again hung up on soft reboot all right so that could be a problem that you are having so how are we going to address it there's going to be a couple of things that I need you to to do and the first is I need you to make sure that you are running on the same version of the micropython software that I am because if you want to get the same result I do you need to make sure that we are on the same micropython version and so what you can do is to find out what version you are on you can come over here and you can down at the command line say import and what are you going to import OS and then what are you going to do you're going to print the os. uname like that okay you're going to print that like that okay and then we run it and what was that ah I didn't close okay now I've really got a problem let me try it again import OS colon or that's a semicolon and then print and I need to print os. you name okay like that open close and then close the print and then this shows you that I am on version 1.22 do2 I'm on 1.22 do2 if you don't have that go back and download the new micro Python and you do that just like we did it in lesson number one okay but you go in and you get the new one and then how do you put it on your Raspberry Pi Pico W remember you hold the white button down plug it in and then your Raspberry Pi picw will show up as a drive in your windows folder and you open that drive and then you drop the micro python the new micro python over to that drive then you unplug your Raspberry Pi Pico W plug it back in and you should be ready to go and if that is confusing you go back and look at it we did it in lesson number one okay so you've got the software you've got the servo Library you're on the same version of micro python that I'm on and you also need to be on the same version of th and a lot of us installed Donnie a long time ago and it was really kind of buggy it was really kind of buggy with uh with some of the things in the micr Python and the Raspberry Pi Pico W so you want to go out and get the same version of th that I have which is th 4.1.4 okay now normally just doing micropython and simple things on the Raspberry Pi Pico W it really didn't matter very much which version of micropython and it really didn't matter very much which version of th you have but you can see as you start getting to more complex and more sophisticated topics like dual threading like like programming on two cores you really need to make sure that you have this uh this same uh versions that I have because those earlier versions were very very buggy and these more recent versions are a whole lot less buggy okay and so there's a lot of issues you can have when you're trying to do the uh the threading and the most important thing is do the update just like I showed you On th and on the uh on the micropython and then the second most important thing is to exit your program cleanly which is now what we are going to do so I showed you the example of just running the software like we have it here how you can run into problems okay and so how are we going to address that well what we want is we want to kind of put a hook in here that instead of this running forever once we fire this thread up it runs forever until something nasty happens and it crashes and so we don't want to do that we only want to run as long as let's say let's create a new variable okay let's create a new variable and that new variable is going to be called ah it might have actually been this shouldn't have been there already but uh when you copied and pasted it it was there but what we're going to do is we're going to create a new variable that is called running and that is going to be true and so what we want is we want as long as running is true as long as running is true we want this thread to keep running so instead of saying while true what we want to say is while running now what you could say is while running equal equal true and then when running isn't equal equal true then it will drop out of this but all you really need to say is while running now for this to know what running is from the main program what do we have to do we we got to make that a global variable so we're going to make running a global variable here okay so this running now is going to be the same as this running and when it gets to this point and when you find up the thread it is going to come in here running is already going to be true and so it is going to run this much of the code now what is going to happen when running is not true well then it will drop out of the while loop and it will drop to right here well what we probably need to do right there is print a message which says print and what we're going to print is uh thread other other core has terminated and this will allow us to just verify as we go through and get this thing working it allow us to verify that we have in fact left that thread because after this there's nothing else to do and what is it going to do the thread is going to terminate itself but because it's gotten to the end there's not another line of code for it to run so it will stop so once we break out of this while loop we will get this message and then it will stop okay so now what we need is we need some way we need some way to change running that when we want to stop the program we want running to what become false now there's really not too much hope that we can do it with this little red stop button but what we can do is we can put some code down in our main program we can put some code down in our main program where we can put in a keyboard interrupt and then with that keyboard interrupt what we can do is we can cleanly exit the program and the way you do that is I don't know if we have done try or accept before but what we're going to do is we're going to say try and what do I want to try I want to try all of this code here and so it now needs to be tabbed over each line needs to be tabbed over so it is underneath that try and so each one of these I'm putting over by one tab okay like that all right and that all looks good and so what does try do it tries to do it it tries to do it if it can't do it what does it do it comes down to accept and then in accept you say well you try and if you can't do it come down to accept and then do the lines of code in the accept and so if you tried and there was an error you would jump down to accept but we're not going to to have an error so we want to jump down not to accept but we want to jump down to accept key with an uppercase cake key keyboard interrupt like that so what's it going to do it's going to go to try and then it's going to keep looping in the while and it will Loop until it gets a what a a keyboard interrupt and then it's going to jump down to the accept keyboard interrupt does that make sense it's going to keep luk being in that while loop until it sees the keyboard interrupt and then it will jump down here well what do we want to do here well we want to print we want to see that it has seen the keyboard interrupt so we're going to print keyboard interrupt like that okay and then what do we want to do we want to make running is is equal to false like that and then let's see what happens here let's see what happens here now what's going to happen this is going to be up here looping looping looping looping and then when I hit the keyboard interrupt I'm going to come down to accept keyboard interrupt and then running is going to be made false and because it is a global variable then this becomes false so when it gets back up to the top of the loop it is going to find that running is false and then it's going to come down here and it's going to print that that thread is terminated so let's run this thing and see what happens so I'm going to run it and it right off the bat doesn't like line 18 and what is that what's wrong with that ah I put a coal in there not needed let's try it again okay now look at this so we are going forward in green and backwards with red red count uh clockwise green counterclockwise red that looks good now what is the keyboard interrupt you come over here and contrl C is your keyboard interrupt and look at that I still got an error code and what error code did I get it says uh it got the keyboard interrupt that's good so what did it do it saw the keyboard interrupt and it got here and then we're assuming it made running false but then what happened could not read the command configuration all this strange thing so somehow even though this was false what happened up here is this thing crashed okay this thing crashed it didn't get down here to this now what probably happened what probably happened is it makes running false and then immediately the main thread dies and as soon as the main thread dies you know our main program dies it's like all of these variables or at least some of these variables are probably going to die too and so this thread doesn't even know what to do so what do we want before we kill the main thread the main thread doesn't die our main program doesn't die until the last command has been completed so we want to give it a little time for that false to get up there and for it to get to that line of code so let's do a di. sleep of let's just say 0.1 it doesn't need to be very much and probably I'm going to have to unplug this again and then plug it back in like that okay and now we'll come over here and we will connect back to the board again back behind me that looks good now let's run it okay going backwards going forwards running it's always been running the question is can we kill it okay I'm going to come up here contrl C boom look at that it rebooted we got the keyboard interrupt and then we got the thread other core terminated and so you see we got the boom boom boom we got everything that we wanted now the question is can we run it again without disconnecting it boom look at that and now we're going to come over here contrl c yeah look at that let's try it again contrl C run it again contrl C bulletproof bulletproof so what you see in today's lesson is what you see is when you're running two cores and you have different threads on the different cores you've got to think about your timing and so what did we have to do we had to put this keyboard interrupt in and then we had to set fall we had to set running to false and then we had to give this time to make sure that it went through and got back up here to the top before we let this main thread or this main program die does that make sense I hope it does ma'am we have learned learned a lot of stuff here today and this is getting into some pretty deep stuff so I really need to give you a homework assignment I need to give you a homework assignment where you can kind of play around with this so I'm going to give you a Threading homework assignment and let me go ahead and I'll come over here what I've got is I've added a button I'm not going to use the LEDs now but I have a button and I have a Servo and this is what your program needs to do do this is what your program needs to do and let me uh okay this is what your program needs to do okay so I want you to watch this so I'm going to run the program and did I I don't think I ran the right program there no I think this is the same program that we had running before so let me stop that cleanly with a contrl c and wow that kind of you lost the to focus on that as well all right so let me uh let me try this program here so this I think is your assignment so I'm going to run the program okay now nothing is happening but if I press the button what I want you to see is is that the after I press the button I rotate from 0 to 180 degrees the servo slowly sweeps from 0 to 180 de and then it stops there now if I press this again it goes from 180° back to zero now if I press the button in the middle it ignores it right it ignores it if I press it in the middle because it completes its operation the First Command that it got and it stops okay and then I press it again and it goes so okay I've got one button and I've got one servo and you need to do this using both cores and you need to do this using you need to do this using threads your thread and then what you also need to do is you need to make sure that the you need to make sure that the program doesn't crash when you do a control C so what I'm going to do is I'm going to control C I killed the program you can see the program kill is killed because it doesn't do work anymore now I'm going to run it and then I come over here and you see it is working I can control C and uh I can I'll kill it here in a minute oh you saw it right so control C kills it and then it terminates that thread properly okay it terminates the thread properly and then it toggles based on the uh based on the uh pressing of the red button that that is your homework assignment and you see what am I trying to get you to think of two things how do you do threading how do you do use both cores and then finally how do you exit that program cleanly where you can run it again and again and again without having that problem of the dangling thread running okay guys I hope you're having as much fun taking these classes as I am making them uh as always I want to give a shout out to you guys who are helping me out over at patreon you're the ones that are keeping me in this game and I really appreciate it you can also help me by giving me a thumbs up on the video and leaving a comment down below if you haven't already subscribed to the channel and when you do ring that Bell so you'll get notifications When Future lessons drop and most importantly share this video with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul mcarter with with toptechboy do.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 1,851
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: mcXxqx0lZYo
Channel Id: undefined
Length: 27min 6sec (1626 seconds)
Published: Tue May 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.