Raspberry Pi Pico W LESSON 70: Example of Cleanly Exiting Dual Core Program in MicroPython

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 70 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 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 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 jump in and talk about what I am going to teach you today and what I'm going to do is I'm going to show you my solution to the homework assignment that I gave you in lesson number 69 but I must start by asking were you able to do the homework if you are able to successfully complete it leave a comment down below I Am Legend double chest bump if you are not successful leave a comment down below I fold it up like a cheap Walmart lawn chair well hopefully most of you guys were able to do it because we've been working on this concept in the last three or four lessons and what we're doing is we're learning how to program so that we operate both cores on the Raspberry Pi PW it's a dual core machine and so we're learning how to write program s where we take advantage of both cores and we're doing that using threading okay one of the unique challenges that comes up when we're using threading is how to exit a program cleanly where when we terminate the program we clean things up and we don't leave that other thread up there running or we don't let that other thread up there crash and so we've talked in a couple of lessons about how to cleanly exit your programs so we're going to practice those things by me showing you my solution to the homework assignment that I gave you in lesson number 69 but a quick review of what that homework assignment was what you were supposed to do is you were supposed to connect up a Servo and a red mind you a red button to the Raspberry Pi Pico W and what the assignment was it's supposed to sit here and then if you press the button the servo is supposed to smoothly move from zero to 180° and then if you toggle the switch again it's supposed to go from 180 back to zero and kind of go back and forth like that as you toggle the button and exit the program cleanly when you do a keyboard interrupt or a control C okay I hope you guys were able to do this but let's jump in and let's see if we can put this program together and so we'll come over to th well let's see I guess I should show you a couple of things here on this uh my Servo the orange wire which is control is hooked up to GPI pin 17 the red wire which is power is hooked up to physical pin 40 and the brown wire which is ground is connected to physical pin 38 and then as far as the button goes as far as the button go goes I have the button connected I've got one leg of the button connected to GPI pin 16 and then the other leg of the button is hooked to ground and then we will be using a pullup resistor an internal pullup resistor on that okay so that's how you get things hooked up but let's go ahead and see if we can kind of start programming this thing and I think the first thing I want to do is just get this button working as a toggle switch and so let's go ahead and do that so I'm going to need to from machine import pin like that we're always going to need to do some delays so we're going to import time we know we're going to be doing threading so I'm going to import underscore thread like that underscore thread and then we also know that we're going to use that Servo so import Servo and I showed you last week if you don't have the servo library in last week's lesson I showed you how to how to install that Servo Library so you can go back to lesson number 69 if you need to review that then what do we know uh we know let's go ahead and do our let's call it red butt pin like that and that was pin 16 I said now let's create our button object which would be my red button you can call yours whatever you want I called mine my red button and that is pin the method that we imported from machine and then what pin do we want to do red butt pin that we just set up and it's going to be a pin do a pin. in be careful with the with the capitalization capitalization matters and all of this so we're going to set that red butt pin as an input pin and then also what it is going to be is we're going to activate the pull up resistor like that okay now we need to keep track of where the button state is this time through the loop and where the button state was last time through the loop and so if it's if it was down last time and if it's up this time then we'll want to toggle so we need to kind of know what the the red butt State now is and I'm going to go ahead and just set that to one because when it's not been pressed it's one and at the top of the program no one's pressed it so it's going to be red but State now is equal to one and then what was it last time well this is the first time it's run so it's going to be one as well meaning it hasn't been uh switched State old so we have red butt State now and uh red butt State old and we're going to set that to one as well okay now let's uh let's come in and let's uh let's define our main while loop but we know that we're going to want to exit cleanly so remember what we do to exit cleanly we put a try and then if we have a try we're going to need an accept and what are we accepting on we're accepting on keyboard interrupt like that I'm just trying to do the things the framework that I usually forget and so I'm just going to do it all right now so I have a try I have while keyboard interrupt and then we'll come in and fill in the details but what do I want to try I want to try to do a while true true when it's true true true is always true this will run until it crashes and then if it crashes or get a gets a keyboard ENT interrupt it will jump out of this okay so now we're going to say what is red butt State now we're going to read the state of the button and how do we do that we do that with a a uh my object is my red button. value so now we're reading the state of the button if it's up it's going to be a one if it's pressed down it's going to be a zero so we're going to do that and then what what are we going to say we're going to put our colon in now what are we going to say we're going to say if red but State now is equal equal to zero so this time it's not pressed and red but State old equal equal one so if last time it was pressed down a one this time it's up a zero that means the button pressed and what are we going to want to do we're going to want to toggle and so that needs to be a colon there all right so I'm just going to I'm just going to come down here and I'm going to say print toggle that'll just help me debug it so I see that I'm going to see if it if it sees that button press and I'll know it sees the button press if it prints out toggle okay now what I'm also going to do is I'm going to have something called Servo toggle that's the variable and what does this mean this means that if last time the button was down and this time it's up true I want to toggle the servo so this is a variable telling me that I need to do what I need to toggle the servo so if it's 180 I need to come to zero if it's zero I need to go to 180 so it tells me Servo toggle equal true well if I'm going to do that here I probably need to up at the top I need to up at the top talk about that and say Servo toggle equal false why false because no one's had a chance to run the press the button yet so we want to start off with do I want to toggle no because nothing's happened but it's it's usually good to Define your variables up here at the top so you don't run into any type of unanticipated uh Behavior okay so now so here I'm going to I'm setting Servo toggle equal to true I think that's good and then uh yeah that's just saying that it needs to be toggled and now what do I need to do red but State old is equal to red butt State now and so what that's doing that's just saying I'm at the bottom of my Loop and so I've got to record what my value was this time my new value now becomes the old value and now we probably need to just do a little bit of a time. sleep here about 0.1 just something to slow this thing down just a little bit okay so let's go ahead and run this and just see what kind of Errors what kind of crazy mistakes we've already made in here so let's run this thing okay line 16 right off the bat okay so we have try we have F true we have uh ah there's no colon there not on just a simple command like that that was a silly mistake hopefully you guys saw that so let's run it and now we move to line 17 now this one I am sure needs a uh needs a colon at the end of it so uh redbutt state. value oh I see what's wrong this is not indented all my indention is off by one because somehow I was treating that read like it was something that it was [Music] not okay so we toggle if we end up in this if statement which means we got a button push so let's go ahead and run this and now our problem comes down to 23 okay I see we haven't said what to do on a keyboard interrupt okay so what we're going to do is we're going to say we're going to print that we got down here just to help us debug keyboard interrupt so we see that we got there and then what I'm going to do is I'm just going to do a Time dot sleep of let's say 0.1 and then I'm just going to say print [Music] program is done now this doesn't really needed if you're just doing what we're showing here which is just the main core running we don't need to do this but we know that we're going to be adding that second core here in a minute so let's just get this basic program running before we have to make it fancy okay so that looks good it's running now if I come over and press the button okay if I come over and press the button it is not not seeing the toggle it is not seeing the toggle so I'm going to contrl C to get out of the program look at that it saw the interrupt the program is done that part's working but why is it not seeing my button why is it not seeing my button where is my button hooked up to make sure that I am on the right have this thing on the right pin here that looks like I am using gpio pin 16 I do believe and and if I come up here I have red butt pin is 16 that that looks good that looks good so why is it not seeing that let's take a look so I said try and accept while true red butt State now let's just put a print statement in here and see if it's getting that read so I'm going to say print uh one thing I see is this shouldn't be R this should be red like that there was a typo there that could have been what was causing the problems let's run it okay toggle toggle toggle okay so the push button part of the program is working now we got to take that push button and we've got to make it do something specifically we've got to make it sweep the servo so we don't want to be sitting and listening for a button push and sitting in waiting to do the servo so we're going to do that on two different cores okay so we're going to operate the servo on the other core so how do I do that well I'm going to Define my other core like that okay and now what am I going to need well I'm going to need Global I need to know what Servo toggle is okay where do I Define Servo toggle I Define that down here I set it to true if I need to toggle so I need to know what it is so what do I do I make it a global variable now this thread on this separate core will always know what that main variable Servo toggle is now the other thing that I am going to need is I'm going to need this Global variable of running right and that lets me keep track of should I keep running true or should I stop running which would make running false so is running true or false should I keep running or not keep running so I need to make that Global and if I'm going to do that then I need to come up here and I need to set my running equal true just so it starts with some sort of value okay now what do I need to know I need to kind of keep track of where my Servo is so I'm going to keep track of Servo State like what state is my Servo in or what angle is my Servo at so we got to start it somewhere so I'll say Servo state is equal to zero and that would be 0 degrees okay now while what running okay normally inside of this thread you would say while true but I want to say while running because then later outside the program if I make run running not true then that will what that will kill this thread and in fact we would do that down here right we would say uh in the excepts you hit contrl C you end up down here you print that you have a keyboard interrupt and then after you print that you have a keyboard interrupt then what you do you say running is equal to false and then what will that do that will come up here and that will make running false and then it will drop out of this while loop and the thread will stop uh the thread will stop executing I hope that makes sense I really hope that makes sense so now what do we want to do in this while loop what do we want to do in this while loop well if I get a toggle and I'm at 0 degrees I want to sweep to 180° if I get a toggle and I'm at 180° I want to sweep to 0 de and so let's think about that while running well if I'm at 0 degrees I would say if Servo State equal equal Z and Servo toggle equal equal true so what two things have to be true I'm at 0 deg and the button been pushed asking me to move well in that case what do I do I'm move and how do I do that for I in range we're at zero so we'll start at zero we'll go to 180 and we'll go in steps of one like that and then what do I want to do well I'm going to have to set up my Servo if I'm going to move it so I'm going to come up here I set Servo toggle I set running to true what am I going to have to do I'm going to have to say Servo ser pin is going to be equal to did I say that was pin 17 yeah pin 17 now I've got to create my Servo object so I'll just say my Servo you can call yours whatever you want is Servo uppercase the library. servo lowercase the method of Servo pin okay so now I've created my Servo I've given it a PIN number and I can come down here and what can I do down here I can move the servo so I'm going to go from 0 to 180 I'll say my Servo Doos of I like that and then I'm going to do a little bit of sleep time do sleep of uh Point let's say 0.05 like that okay so if the buttons been pressed and released and I'm at 0 degrees I want to sweep from 0 to 180 well what is the other case if Servo State equal equal 180 and S Servo Servo toggle equal equal true then what do I want to do well I want to go the other way for I in range from 180 to zero in steps of minus1 like that and then what do I want to do my Ser my Servo Doos of I and then a time do sleep of 0.005 okay now what is the problem the problem is right here what is going to set Servo true okay when I press the button Servo toggle is going to be set to true and because Servo toggle is set to True down below and because I'm at 0 de I'm going to go from 0 to 180 but what's wrong the next time through this while running Loop Servo toggle is still true and so what's it going to do it's going to keep doing this it's going to keep doing this so once I've done it I have done the servo toggle request so I need need to what I need to now say Servo toggle is equal to false why because you wanted a toggle I gave you a toggle now I don't need to toggle again until you press the button again so I say the servo toggle is equal to false also what is my Servo State well here my Servo state was zero and then I moved it to 180 I better down here remember that that Servo state is now 180 like that now same thing here after I go through that for loop I have completed moving the servo so I don't need to move it so I'll say Servo toggle is equal to false and then what uh Servo State well this time I went from I was at 180 and I went from 180 to zero so when I drop out of this what is my Servo State going to be it was left in zero so next time through the loop everybody knows where they are that looks pretty good now what else do I need to do now after I fall out so when running becomes false I'm going to drop out of this while loop and I'll drop all the way down to here and I just want to print other other core terminated all right so if this prints I'm going to know that I dropped out of that while loop in the thread I'm at the end of the thread and the thread has ended so this will just let me see in fact like if the program stops and it doesn't print other core terminated then I know either it's still running or it crashed in an ugly way that for me to know that I cleanly exited I need to see this thing print out on my terminal okay so uh now if we're going to do this down here after we Define the thread we need to start it so I'm going to say underscore thread. start new thread start new thread of what other core and then like that and I think I used an uppercase c as the convention I've been using I don't pass it any parameters I close that and then we always like to give it a little time to get started before we jump down into the other core so I just give it like that okay let's see what happens when we run this probably full of Errors okay let's see what happens it toggled and look boom do you see how I'm going smoothly from 0o to 180 okay very very good and it stops and now if I toggle again from 180 to zero look at that okay but now what is the real question the real question is can I exit cleanly right can I exit cleanly and so I'm going to do a contrl c a keyboard interrupt and what happens it jumps down to keyboard interrupt it sets running to false and then when running is false up here when running is false it drops out of the while loop it prints other chord terminated okay and so that happens as soon as I make running false and then after the other core is terminated then what does it do it prints that the program is done so a super super clean exit a super clean exit so we'll run it again I press the button have a sip of [Music] coffee that looks pretty good that looks pretty good okay gets over here and now I'm going to contrl C and I get a clean kill I get a clean exit Okay of this program and that is really really good okay guys let's talk about your homework for next week let's talk about your homework for next week I'm going to run this thing what if I what if I toggle it and then what well what if I toggle it and while it is running I give it a control C okay I'm going to give it a control C while it is moving boom what did I do I just broke the program now what was different earlier on I would always do control C after the servo had finished moving to its new position what did I do this time as the servo was still in motion I killed it and if I killed it then what happened it died ugly so I saw the toggle that's good and then I got the keyboard interrupt that's good so I got the keyboard interrupt that's good but then what did it do the main thread my main program my primary core main program terminated cuz I got here it terminated and what did we never see happen we never saw this core stop so either it's still running or it crashed early I mean it crashed uh it it it crashed ugly so either it's still running or it crashed ugly but if I come up and try to run the program I just get a I get an error and I would have to I would have to unplug this and plug this back in so what your homework assignment is is to take this Dandy program that hopefully you did by yourself and if you didn't at least you saw how I did it and go in and make it exit cleanly get a clean exit with no errors no ugly red marks even if you do the control C or the program exit as the servo is moving okay even if you do it as the servo is moving I want it to end cleanly okay guys I uh hope you all are having as much fun taking this class as I am making it really love the stuff that we're getting into also as always I want to give a shout out to you guys who are standing with me on patreon your support and your encouragement is what keeps me in the game here really appreciate it you can also help me by giving me a thumbs up on the video and leaving a comment down below also if you haven't already subscribed to the channel when you do make sure you ring that Bell so that you'll get notification when future lessons drop and as always 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 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: UHbboCxIOYE
Channel Id: undefined
Length: 28min 37sec (1717 seconds)
Published: Tue May 21 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.