Raspberry Pi Pico W LESSON 68: MicroPython Multicore Threading Example with LEDs and Servo

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 68 in our incredible nude 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 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 easier 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 67 but I must first ask how many of you were successful if you were successful leave a comment down below I Am Legend double chest bump and if you were not successful leave a comment down below I fold it up like a cheap Walmart lawn chair okay I'm really interested to see how many of you guys were able to do this because I taught you everything that you need to know in order to do the homework but I'm just curious if you guys were able to make that connection between what I taught you so far and what the problem at hand is because while it sounded really simple you kind of had to think through things now what was the homework assignment what was the homework assignment let me move over here and let me get out of your way and let me have a little of my Go Juice the homework assignment was to hook a Servo up to hook a Servo up to the Raspberry Pi Pico W and to hook a red LED up and a green led up and then to move the servero from 0 to 180 and from 180 back to zero and if you are moving in the clockwise Direction the forward Direction be blinking the green LED and if you're going in the backwards Direction then blink the red LED now that sounds pretty simple but you got to really think through it you have to think through what do we have we have three elements we have a red LED we have a green LED and we have a Servo now if we had three cores it would be very easy because you would just say I'll do the red LED on a core the green LED on a core and I would do the servo on a core okay but the thing is we have two cores so we have to figure out what work are we going to put on One Core and what work are we going to put on the other core because the servo has to be moving while this is blinking and the servo has to be moving while this is blinking and you got to kind of figure out how you're going to split this problem up and so I think that number one I'm interested in how many of you were able to do it and number two I'm going to be interested in seeing how complicated you made made it because I could imagine there's going to be some pretty crazy Solutions maybe you got it working but maybe you did some some pretty uh some pretty uh complicated things when really I think it's not that simple if you think through it okay so let's jump over here to our code View and let's jump in and let's see if we can start working on this first of all what I started seeing is I said I've got two cores I got three components what I really want to do is I want to do the LED blinking on one core and I want to do the servo on the other core and it doesn't matter which one it doesn't matter if you do the servo on the threaded core or you do the servo on the main core but I think the best way way to do it is to put the two LEDs on one core and to put the servo on the other so let's jump in and see how to do that well we're going to be working with some pin so from machine from machine we need to import pin I guess I should also tell you before we get started about how I hooked this up uh on the servo the red Power let me move this over a little bit make sure you can see it the red power wire is going to pin 40 which is the 5 volts that comes from the USB connection and then I have over here on pin 38 physical pin 38 the ground and then the orange control wire on the servo I hooked to GPI pin 17 then the green LED I have connected to pin 14 and the red LED I have connected to gpio pin 15 so that's the basic simple circuit that I use to hook this thing up and I'll also tell you that the uh I'll tell you that I'm using the library for the servo that we developed in what was that I think it was lesson 66 and so if you want to if you want to use that Servo real easily make sure you did lesson 66 that's where we wrote the servo to control the servo library to control the uh to to control the servo so you guys that are in the class you've already done that if you're a drive by shooter and are just dropping into this lesson you probably need to go back to lesson number 66 to make sure that you've got that Servo library on your Raspberry Pi Pico W all right enough of this background information let's keep coding so I'm going to need to do some delays so I'm going to import time I'm going to have to use both processors both cores so what I'm going to do is I'm going to need to import underscore thread so I can use both cores and then import Servo this is the library that we developed in lesson number 66 if you didn't do lesson number 66 you probably certainly don't have the servo Library so go back and do that if you haven't okay let's set our pins up we've got green pin we said was on gpio pin 14 we have red pin and it was on gp15 we have Servo pin which was on GPI pin 177 we said and then what do I have I'm going to create my red LED and that is the method pin for the red pin and that is a pin. out like that and then we are likewise going to set up the green LED and that's going to be equal to pin and that's going to be green pin and that's going to be be a pin. out like that okay now we're going to need to Define our delays and so I'm going to uh let's see here I'm just thinking through yeah I'm going to have for the uh red LED now on these they're both going to they're both going to be blinking fast so the red LED and the green LED will blink at the same rate so I can just do a delay on is equal to a tenth of a second and the delay off is equal to a tenth of a second so whether I'm blinking green or blinking red they both blink at a 0.1 second on and 0.1 second off okay that looks good now I need to create my Servo object so I'll call that my Servo and that's equal to servvo the library that we imported dot Servo the class in that library and then what we do we got to tell it the servo pin which we already set up that looks good let's start let's start by just uh moving the servo okay let's just make sure we can move the servo and then we'll deal with the LEDs later now I'm going to do the servo on the main core so how would I do that I would just say while true when is true true true is always true and then I'm going to say for I for I in range from 0° to 18080 degrees one degree increments and then what am I going to do I'm going to do my CVO doos and I want ice it's going to go from an angle of 0o to 180 doing what I've just done here we need to not go too fast so I'll do a time do sleep of point 01 okay now that'll go from 0er to 180 now we got to go back the other way for I in range from 180 back to zero and minus1 degree increments and then what do we do my Servo Doos of what I the angle I and then time do sleep of 0.01 like that now when I run this hopefully this is pretty straightforward with the library we have hopefully on that main core this will go from 0 to 180 and then from 180 back to zero so let's just see if that part works okay let's run it and nothing is happening what do we do wrong okay let me make sure yep I'm hooked up to pin 17 does anybody see my mistake let's make sure everything is plugged in good okay let's make sure that we set Servo pin ah 17 a typo that kind of scared me okay let's try it now and boom from 0 to 180 180 to zero we are working with one Servo on One Core now what do we need to do we need to get the blinking going on the other core and so this is uh let's see let's just start here let's just set up let's just set up that other core and just see if we can get the other core running to begin with okay so I'm going to stop this and now I'm going to see if I can get that other core running and then we'll figure out how to make it do what we want but to begin with let's just get it running so I'm going to say Define the other core you can call it whatever you want I'm just calling it other core and then it's going to need a delay one the on delay and the delay to the off delay okay like that and then what I'm going to need to do is right this core it just needs to keep running this core just needs to keep running so what I'm going to do is I'll put my while loop inside of there while true when is true true true is always true and then what I'm just going to do is I'm just going to see if I can can blink the LED so I'm going to say red. red led. value turn it on okay and then time. sleep of the delay one the on delay and then red l.v value turn it off and then time do sleep of delay 2 like that now if this is working right I should just be blinking the red LED in both directions I should just be blinking the red LED in both directions all right I've defined that other core function now I've got to start it and so I'm going to go underscore thread the library that I imported dot start new thread and what is my thread other core like that and now I've got to pass it the parameters well what I want is to pass it those timing parameters which was the delay on and the delay off and so I put these here delay on and delay off like that and now I close that tle I close the start of the thread and now I'm going to that should not be indented you see I had that in indented and it's still part of that function so I've got to remove that indent and now it's not part of that function now I'm going to put a little bit of a delay in time. sleep just to give it time to start the thread before I come and start doing this other stuff just to make sure that that thread is started before I go down and start things on the other core so let's see I probably got some tyos in there but all eyes on the servo and the red LED we're going to run it hold your breath okay look at that we're going backwards and we're going forwards and we're blinking and so we're using both cores One Core is running the red LED the other core is running the servo and we are off to the races uhhuh okay but now what is the problem the problem is that in the forward Direction I want to be blinking red and then when I come back in the backwards Direction I want to be blinking green okay so what do I need well I need to figure out whether I want to be blinking the I want to figure out up here whether I should be blinking the red l LED or blinking the green LED so how would I do that well I need to know whether I'm moving forwards or moving backwards who knows whether I'm moving forwards or backwards the main core knows whether I'm moving forward or backwards so what I could do is I could just say that my LED this is just a a parameter the LED is equal to Red I just give it that value and then when I'm here in this part of the code what is the variable LED going to contain it is going to contain red now what should I put here LED is equal to Green like that okay and so when I'm when I am moving forward or this is actually back Wards when I'm moving backwards the variable LED is set to red when I am moving the other way the LED is is set the the variable LED is set to green and so LED here it's just a variable when I'm here what is the value of LED it's red when I'm here what is the value of LED it's green so who knows who knows which color LED should be blinking the main core knows okay but what I've got to do is I've got to get that information up to here okay so how would I do that how would I do that well I need this function to know the value of the LED variable how do I do that inside the function I tell it hey friends I want led to be a global variable so when I get here it doesn't try to create a new variable LED it uses the global LED okay now if I'm going to use LED I better at least initialize it to something so down here up here what I'm going to do is I'm going to just set it to blank and that way if I hit an if statement it at least knows what led is so here LED has a value now what do I do I start my thread when I come into my thread I say LED is global and it has a value because you see if I hit this before this assigned a value the program would crash so I give it an initial value here just so the first time through that Loop the program doesn't crash so now led is going to be shared among all of the different functions okay the main program and also this function which is running on the second core brilliant brilliant did you guys figure this out did you figure this out now it becomes pretty simple now it becomes pretty simple I set this to a global variable now this is going to Define what led is and this one's going to know what led is because I made it a global variable and don't cry foul don't throw a yellow flag at me because back in the lesson that I taught you functions I taught you how to use Global variables inside of those you know the class with functions and methods in class okay so now what do I want to do well if L is equal to Red then what do I want to do I want to Blink red now if LED is equal to Green what do I want to do well I want to put the quote there for sure and then let's just see if we can snag all this and put it here and then this would be G gine and then this would be gin like that now let's stop it okay and again what I found is this needed a little bit of a delay so I start the thread and then I give the thread a little bit of time to get going but what is this going to do it let's say this started really really quickly what's it going to do well at that point the LED is set to nothing and so it's going to be sitting here looping waiting for it to hear that the the the LED has a red value or a green value which will be set down here so it's just a little bit of timing and it doesn't affect the operation at all because this will just sit in Loop waiting for this to get either a value of red or a value of green it's crazy but it just might work okay so let's hold our breath and all eyes on the ser o the red LED and the green LED hold your breath ah line 23 what is this line 23 nonsense rookie mistake equal equal for a conditional rookie mistake equal equal for a conditional hopefully you guys saw that hold your breath and all eyes on the circuit boom that's going but I'm not getting any blinking action okay why am I not getting any blinking action I got an error that time isn't defined well I imported time and I imported time okay this probably is a little bit of a timing problem I start the thread I sleep let me give it a little more time okay let me give it a little more time I don't think it needs that much time but you see I'm going to give it time to be running up here before I start the main core so let's try it again still doesn't like it okay this is perplexing me a little bit because it's acting like it doesn't know what time is in the other core let's see if we put a delay here sometimes you got to play with this timing a little bit okay now it's working boom okay but it's not BR blinking green it's blinking red that's good but coming back the other way it's not blinking so green let's make sure I spelled things right ah look at that hopefully you guys saw that g r e e n like that now it's going to work let's watch it okay comes over red and green boom we're running two LEDs and we're running the servo all together now I am a little bit disturbed you see if I don't put a time do sleep here here when I get down here it doesn't know what time is and I'm a little bit perplexed at that timing problem why did it like time dos sleep here but didn't like it there let me take this back out let's try it again okay now it seems to be working maybe what I should have done was just unplug the thing to just make sure that nothing funny was going on okay nothing funny was going on but you see that's working now uh let's see if we just took this completely out I think it probably will not work real well if we do that but let's try it okay yeah I got that time error okay I got that time error so it needs a delay here to give the thing time to start and let's say if I go one one second like that that'll give T things time to settle out and boom it's working and green is working yes success but I want you to see the way I did this is it's working perfectly synchronized even like if I did 10 second delay even if I did a 10-second delay let's watch what happens what's it doing now it's sitting in that other core is running but red or green the led doesn't have a value so nothing's blinking but now blink and then blink that was left on just because the gpio pin was left on when I stopped the program okay but even with a 10 second delay do you see how it stays perfectly timed so when you're running the two cores you got to think a little bit about your timing let's see if I could go to 0.01 seconds let's see if that would be enough for it to work going going yeah it looks like it just needs a little bit of a delay there and for some reason if you jump up here too quickly it doesn't seem to know what time. sleep is and I'm not exactly sure why but if you just give it a little time to settle out and get running then it seems to work okay all right guys I'm I'm really I'll tell you I am really looking forward to seeing your homework assignments I hope I'm not boring you with this but what we're doing is we're spending some time to really get comfortable of how we can use two chords now if we had six cores it would be a lot easier but with two cores sometimes you got to figure out how to split your work up between the two cores what we decided to do was to put the servo on one core to the put the two LEDs on the other core and then have the main core and the second core communicate by This Global variable okay guys I hope you're having as much fun taking these classes as I am making them want to give a big shout out to you guys who are helping me out over at patreon your support and encouragement is what keeps these great classes coming you can also help me by giving me a thumbs up on this video it will help me with the YouTube juice if you will leave a comment down below and if you haven't already subscribed to the channel when you do ring that Bell to make sure that you get notification 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 toptechboy tocom I will talk to you guys [Music] later
Info
Channel: Paul McWhorter
Views: 2,212
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: n2eQTw9axHg
Channel Id: undefined
Length: 27min 18sec (1638 seconds)
Published: Tue May 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.