Raspberry Pi LESSON 14: Installing Vpython (VIsual Python) on the Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com and we're here today with episode number 14. in our incredible new tutorial series where you're teaching your raspberry pi who's the boss now what i'm going to need you to do is pour yourself a nice tall glass of iced 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 sun founder sun founder is actually sponsor sponsoring this series of video lessons and in these lessons i will be using the most excellent the most excellent sun founder ultimate raspberry pi kit now hopefully most of you guys already have your gear but if you don't have your gear yet look down in the description there is a link over to amazon where you can pick up this most excellent kit and believe me in this class your life and my life are going to be easier if we are working on identical hardware so if you don't have it yet skidaddle on over there and get your gear ordered but enough of this shameless advertising let's talk about what i am going to teach you today and what i'm going to teach you is i'm going to teach you how to install visual python on your raspberry pi and how to start out by making a very simple 3d model a very simple 3d animation of what we were doing the project that we were doing in lesson number 13. now i did warn you guys to not take your circuit apart in lesson number 13 because we're going to start today with the circuit and the code that we had ended up with in lesson number 13. now go ahead and admit it i know one of you guys didn't listen to me and took the stud you know took took the circuit apart so go ahead i know you're out there leave a comment down below it was me i was the guy that took my circuit apart even though you told me not to there's always that one guy right there is always that one guy but in case you guys uh need just a little reminder this is the circuit this is the circuit i believe that we ended up with and give me just a second i need to do a quick little a quick little windows management thing here get everything up to date and then i will move out of your way okay i think that looks good and so this is the circuit that we ended up with in lesson number on 13. you can see that we've got three switches three toggle buttons a red a green and a blue we have an rgb led an rgb led over here and we can control the color of that rgb led by subsequently pressing red and green and blue buttons and then we can mix together all types of different colors okay so also just in case you guys uh have lost your code you can go to the most excellent www tech and i need to switch over there to that view now you guys can do a screen capture here of this circuit if you don't uh if you don't have it but let's go over to the most excellent www.toptechboy.com okay and that uh let's see guys give me just a second i want to make sure that we are ending up with the same view here okay i think that's uh i've gotta i've got to adjust that that is just not quite right okay there we go so you go to the most excellent www.toptechboy.com and then using this happy little search icon here you can search on something like raspberry pi lesson 13 set the color of rgb led with push buttons search on that and should come up with this and then here i have that code that we ended up with in lesson number 13 so i will copy that and then we can come back to our terminal and we can create a new program to paste that into and so remember we are working in the python folder and so i can just create a new program with nano and let's see it would be in python and then let's just call it r g b and then dim and then v python because we're going to put the v python in there and then dot p y and here we are now i will come over and i will right mouse click and i will paste that code in there i will control x and a y for yes and then enter and now it should all be in there now let's just see if we run this thing instead of doing a nano we will python 3 it and then boom it's running and now if you keep your eyes on the led here if i press red you can see it turns on very dimly red if i press blue now blue and red are on to give me a very dim purple and then i can step up red or i can step up blue to get various shades of purple and pink and various shades of magenta and i can just play with those buttons and i can mix any color that i want but we learned all of that in lesson number we learned all of that in lesson number 13 and so i'm just kind of reminding you of where we were in lesson number 13. okay now what we want to do is just like i have a real led i have a real led here i want to create now a virtual led and i'm going to create the virtual led using v python so how do we get v python on the raspberry pi i remind you i am on the buster operating system and i am on python 3.7 which comes with buster now as i go back and read comments on videos that i make the number one issue is for you guys that jump in and just watch one of my individual lessons and not taking the whole class and working on a different operating system in a different version of python and a different this and a different that and then what i show here doesn't work for these things to work you got to be on the same operating system i'm on buster and you got to be on the same python python 3.7 which comes with buster all right but enough of that disclaimer stuff let's have a little coffee and now what we want to do is we want to do pip install right wrong no pip would install it in python 2 likely so we've got to make sure we tell it we want it to be in python 3 and so we do that by saying pip 3 install and then what do we want to pip3 install the python like that everyone hold their breath we want to see like it finding things going out okay looking that looks good okay we're getting some little uh some little suggestions and stuff there but so far all right that's what we want to see you see the happy little bar going across it found the vp python package it is downloading it it is going in and putting it in it is doing different things it's pulling it up yours might look a little different you see that i'm seeing a lot of things that say uh let's see it looks like some of this stuff was already installed and so yours is going to look a little different but what you should be doing is you should be seeing things going across and you should be seeing things download and so forth and so let's see if that actually worked for us yours might take a little bit longer because it looked like because i always practice these lessons before i do them in front of you and so it looks like some of these things were already downloaded so yours might go a little bit slower than mine just did but let's type python3 okay and then let me get further out of your way there okay and now let me just say import v python like that and we got an import successfully i think this also probably installs numpy for you and so let's just see if we got numpy as well for free yeah it looks like we got numpy as well for free so when you install the python it installs some other useful libraries for you and so that looks good and let's just for fun see if we can do our first little v python animation and so what i am going to say is i'm going to say make a ball and the ball is going to be equal to a sofa ear and that's ear is going to have a radius equal to two arbitrary units and then the color is going to be equal to color dot red like that and then okay what did i do wrong sphere that sure looks right what did i do wrong oh oh oh yeah yeah yeah okay uh what i need to do is from the python import star what i did was i just imported it to see you know is it there yet it was there but here this way i don't have to type v python every time i want to use the v python library and let's see if this will work now and so now we're going to say ball is equal to sphere radius of 2 color is equal to color dot red and we are holding our breath and shazam look at that we got a red sphere in three dimension and we can come in with our mouse and we can kind of go up and down and do things like that you know i'm scrolling with my center scroll wheel and i can zoom in and zoom out but let's kill that okay and then let's get rid of this and then let's come over here and uh let me let me do it one more time i'll say python three okay and i am going to save from v python import star which means import everything and now i'm going to say my box is equal to uh what is that that will be a box okay and the box is going to be what it is going to be color equal color dot blue and then the link is going to be equal to 3 the width is going to be equal to 2 and the height hgig height height h-e-i-g-h-t that's a hard word to spell you guys know that that is a hard word to spell let me see if i can bring this up to make sure you guys are seeing it okay i think you can see that good height is equal to one let's say all right so this should make a box boom okay now what you can see is i can back away and then i can right mouse click and spin it and you can see that that box i made so you see we are making 3d animations using vp python and the command line and that is pretty neat okay but what we want to do now is instead of just doing that what we actually want to do is we now want to start building something inside of that other program that we had and so what we're going to do is we're going to go back and see if we can nano that rgb dim v python dot python.pi okay that looks really good and what we're going to want to do to that program we're going to want to start by saying from the python we want to import star okay now we're going to go ahead and do all of our button work and all of our gpio work and all of that good stuff okay and then here what we're oops here what we're going to want to do is go ahead and let's just make a sphere and so i'm going to say i'll call it my my ear is equal to a suff ear and then the color is equal to color dot white i think it'll do white white and then the radius is equal to radius radius is equal to let's just say one okay so this should make a white sphere and then everything else should be okay then inside of this loop we just sometimes be python wants a rate statement and so i'll just say rate of 20 which means you could do this at like 20 times a second now we've got some other things in there where that are going to determine how fast it actually runs but you just sometimes v python wants to see this rate statement so now i'm going to go control x yes enter and now we'll get it to do some interesting stuff here in a minute but let's just see if we can uh that was unexpected oh okay [Music] okay we don't want to just python 3 we want to python 3 the program okay and you guys can't see that we want to python 3 that program that we just created how about that okay you can see that now so we're going to python 3 that program and then it doesn't like the indentation and so let's see what we did wrong there that was inside the while inside the try wow that sure looks right oh man you know what i think when we cut and pasted i think it did a very terrible thing which is it put spaces instead of tabs and so if you're having this problem you're going to have to space over like that and i will have to figure out how to fix that but this is the first time that i've copied from toptechboy.com and paste it into nano and we got an unexpected result there so i'll get to the bottom of it but i think we can power on through this lesson so let's go and let's run the program again and this time okay it comes up and we get a sphere we get a white sphere and the program here is still working okay now nothing's happening to the ball because all we did was we just created the ball okay we just created the ball so we will kill that and then we will come over here and when we killed that it seems like it killed the python program which is okay i guess i should probably control c to cleanly so i shouldn't kill that graphic i should cleanly kill it over here with ctrl c but let's nano it again okay and here we go we're going to come down now what would be very very interesting for me to do inside of this while loop okay and that is after i do all of these if statements okay and then when i'm ready to before i sleep [Music] and remember you gotta scoot over now with the space bar because it's using spaces instead of tabs okay and same thing here i need to space over i need to say my sphere dot color is equal to what it's going to be equal to a vector and what you could imagine is i want to give this color a red component a green component and a blue component just like i gave the led here a red component a blue component and a green component using these buttons well where are those levels stored they are stored in the variable dc for duty cycle for blue dc g for duty cycle for green and dcr duty cycle for red and the duty cycles go between what and what they go between one and a hundred or between zero and a hundred well 99 really but we're not going to worry about the difference between 99 and 100 so if i wanted but you see the problem is v python v python wants its color range not from zero percent to a hundred percent but between zero and one and so if i want to apply the r like if if you think the r the g and the b to that animation that i just made the r the g and the b what would that be that would be d c little r d dc little g in dc little b for duty cycle red duty cycle green and duty cycle blue all right but now what's the problem these numbers are between 0 and 100 and we want a number between 0 and 1. so what do we really want to do here we want to divide by 100 we want to divide by 100 and we want to divide by 100 and those that takes the numbers that the gpio pins and pulse width modulation and the are in the led in the real world and turns them into the scale wanted by visual python so let's control x out of here let's see yes and then let's enter and now let's run it again python3 that looks good let's run it whoa okay yeah so why did i get that error message i got that error message because i got that error message because i hadn't cleanly exited i exited by killing the graphic and then it crashed the program and i never did the gpio cleanup okay now let's think about it i have now a black led up here why do you think it's black i said it i set the color to white why is it black it's black because right now this led is in effect black because there's no red there's no green there's no blue there's no color in the real led and in the virtual led there's no color because i applied what dcr dcg dcb divided by 100 all three of those numbers would be zero so what am i getting a big fat zero now this is the moment of truth if i press this red button what two things should i see the two things i should see is this should get very dimly red and i should see something up here happen okay and i did get very dim red here but i see almost nothing happening there and so let's see if we bring it on up what we will get okay it's just this one in the real the one in the virtual world is turning red a lot more gradually okay and so they aren't calibrated perfectly okay but do you see and let me see if i can show this trying to show a lot of different stuff at once and so you can see the red strength is at sixty percent and now the red strength is at ninety percent the led is almost completely red and then when i go again then they both turn off now what you can see is is that these aren't perfectly calibrated in that you can see in the real one a little bit quicker you can see it turning uh turning red and the red and the virtual one is really not quite it's not quite uh perceivable and so let's see what we could do there what if i'm going to be smart this time i'm going to come over here and i'm going to control c here and then that cleanly exited the program so that's good and now what i'm going to do is i'm going to nano the program and i think maybe what we could do is uh i would have to think about how to i wasn't really expecting that result so i would have to think about how to normalize those better but maybe instead of dividing by a hundred maybe it would normalize it better if i divided by ten because i think what's happening is the real led is more on an exponential scale and the uh and the the virtual one is more on a linear scale and so i'm gonna try to have that color come up quicker here and uh we might get a little smarter about this in a minute so let's uh let's say control x yes enter and now i'm going to come up with python 3 and uh i wouldn't have expected the error that time but we're still getting it okay so now what i'm going to do is i'm going to come over here and press red and yeah i'm getting a perceptible red right off the bat and now it's perceptible and then it's coming on up and then it's coming on up okay you see they're kind of matching better there i think okay and then that's like really bright there [Music] and then they both go back off now let's see if we do the red and the blue you see we've got a very faint purple there and we're bringing the purple up virtually and we're bringing the purple up in the real world okay and so that is pretty cl that's pretty pretty slick [Music] okay so now let's come back over here i you can see that it's kind of working but uh [Music] what we got to think is i just python 3 nano okay you see that what is happening now is i need to do something better if i divide by 10 then it works on the low end but then at the higher end at the higher end it begins to it begins to saturate at the higher end it begins to saturate too quickly so i can have it working on the high end i can have it working on the low end and i need to uh [Music] so i divide by 10 i'm just trying to think i think i need to like take the log of this but to get it to work more perfectly what i'm going to do is i'm just going to divide by 25 and then it'll work a little bit on the low end it'll work a little bit on the high end but we need to figure out how to more perfectly get this thing calibrated we need to figure out how more perfectly to get the caliber the calibration working at the low end and the high end but maybe 25 would be a good compromise at this uh at this point yeah i think that'll be a i think that'll be a good compromise but but one thing is you see that here i've got a i've got an led that is a complicated shape and all i made it was just all i made was just a sphere and so what if we came up here and besides my sphere if i made my cylinder and my cylinder is equal to cylinder and the color equal color dot white and the radius is equal to one okay and then uh if i'm going to do that then what i'm also going to do let's just look at that and see what happens when we run it so i'll say control x yes and enter let me do one more thing there python 3 nano let me also down here let me change its color when i change the color the sphere what i'm also going to do is i am also going to [Music] here i'm also going to say my cylinder dot color is equal to vector and i'll do the same thing dc r divided by 25 comma dc g divided by 25 comma d c blue divided by divided by 25 okay and now control x yes enter and now we'll come and we will python three it [Music] okay and so now i'm gonna turn on the green okay i'm gonna turn on the green and you can see that i got a little green and then i'm going to turn on the blue and i'm getting a little blue and i'm bringing up green and blue okay and i'm getting a very beautiful aqua here i'm getting a very beautiful aqua in the real world and i'm getting a very beautiful aqua in the virtual world and so that looks good now what do we not like about that we don't like that the cylinder came out this way and what we want is we want the cylinder to come out this way and then we want the sphere to be moved up so that's two things that we need to do we need to move the sphere up [Music] okay we need to move the sphere up and we need to turn the cylinder up and so what we're going to do is we're going to come in here and when we create when we create this cylinder we are going to give it an axis which direction do we want it to point well we've got to remember that x goes this way okay y goes this way and then z comes out so you go from x to y and your thumb points in z and so the first it's kind of like x y z so we need to give it an axis do i want it to point in the x direction no i put a 0 do i want it to point in the y direction yes i do so i give it a 1. and then do i want it to point out in the z direction no so i give it a zero all right and so that should pop that cylinder pointing upwards in the y direction and then the sphere if uh let's see i need to give a length i need to give a length to the cylinder and so i will say linkatha and let's say the link at the is 1.5 so it's going to be a little taller than it is across but when the radius would be it would have a diameter of two so we we would want the height to we would want the height to be like 2.5 like that okay and so now it's going to be standing up it is going to be a little bit taller than it is wide all right but now if we do that we need to move this sphere we need to move the sphere up and so how far would we want to move the sphere up we would give it a position and the position would be a vector and we're going to give it an x value a y value and a z value do we want it to move in x no so we give it a what a zero do we want to move it in y the sphere yes we want it to go up how far 2.5 do we want it to move in z no we give it a zero okay then we'll close that out control x yes enter and now we are going to python 3 it and what what on earth did i misspell axis what did i do wrong there okay positional argument follows keyword argument oh i know what i did wrong okay i know what i did wrong it should be axis equal vector i was not thinking just like i said position is equal to vector here i needed to say axis equal vector like that now we're going to do ctrl x yes enter and now let's see what happens everyone hold their breath still getting the warning okay boom look at that let's start bringing some color up here let's bring it up as a purple okay let's bring it up as a purple wow that's starting to look like an led okay so look at this what if i kind of lay this over a little bit if i lay this over a little bit you can see color led in the real world color led in the virtual world okay now if you look at your led how could i make this a little bit more realistic do you see the bottom of the led has a little bit of a disk why don't we put that disk in there so i'm going to do i'm going to come over here and i'm going to do a control c okay control c all right so now let's nano this thing again and if i want to make a disk what would i do to make a disk i need another what what would i need i need another my cylinder but i'll call it my base okay and that is going to be a cylinder and the color equal color dot white the radius do i want it the same radius as the body no because then it wouldn't show i want it a little bit bigger so i'll say radius is equal to 1.2 like that and then uh what would the length be i want a little little guy a little guy so i'll say length is equal to 0.25 so it'll be like 10 of the height and then i've got a point it's it would be a disk like this i've got to point it up so i give it a what an axis equal vector and then what zero one zero zero one zero close the vector and then close the you guys cannot quite see that can you let me make sure that you can see this okay [Music] no that's not good either you see nano doesn't like these long lines okay so i will have to figure out how to fix that better for you but just know that you've got to close the vector here and then out here there is another close to close the cylinder all right and so hopefully that will make sense so now let's control what else do we need to do i was fixing to control x out of here but then what do i need to do down here down here i need to [Music] okay down here i need to say my base dot color is equal to a vector and then the same thing dc r divided by 25 comma dc g divided by 25 comma dcb divided by 25 like that okay control x yes enter and now let's run it and see what happens and oh i see i put a dot instead of a instead of a comma simple mistake where was that down here yeah you i hope i hope you guys saw that okay comma ctrl x yes enter and now let's run it python3 okay still getting that old annoying it's like the one time i didn't clean it up now it's not letting me clean up i would probably need to reboot the pie but i don't want to do that okay did we get something let's go red and green this time so we're gonna kind of be bringing up a yellow shazam look at that is that cool or what is that cool or what that this thing is doing whatever i do in the real world is being mimicked in the virtual world as far as matching those colors okay now if we were really gonna just i think that i've got a little bit of time so if we were going to do one more thing what would we want to do here now this is strange because i'm doing a control c but i'm not getting that gpio good to go you see for some reason in my try and accept [Music] it's not coming down here except keyboard interrupt which is the control c try it's not jumping down there correctly but i'll get it figured out maybe i'll have a supplemental lesson later on but but the main learning you are getting here and the main learning that you are supposed to be getting is how to build this thing so let's say what if i now made a the legs let's see if i could make the legs of the uh leds okay let's just say my leg one is equal to a box and the box should be uh the box should be i'm going to give it a size instead of length and width and height i don't want to do that because what's length what's width what's height i don't know and so you know like i don't know which way it's oriented so i'm going to make the size i'm going to make the size equal to a vector and i know in x i want it to be very small so i'll say 0.1 and in y i want it to be long and so i'll say that it's like 1 and then in z coming out it would be 0.1 so i'll make it 0.1 and then i will say color equal color let's see i'll make it a vector i don't know if gray is considered a color so i'll make it a vector and i will just say it's 0.2 comma 0.2 comma 0.2 and if i make rgb the same then it should kind of turn to white but i didn't go to one so it should be you know it should be a it should be a gray okay what i don't know what i don't know is if i need to pop that thing up but let's see what happens if we just run this control x yes enter and then we are going to nano that thing nope we should python 3 that thing [Music] okay so that is kind of interesting it looks about the right size but i need to move it down okay i need to move it down and i need to make it longer because it's putting the center at the origin and i need to move it down so i am going to come back over here and ctrl c and then i'm gonna nano that all right and i am going to come down here my leg one and so that i think is a pretty good size and i'm going to need to give it a position so what i'll do here is i will say position is equal to vector and then what do i need to do is i need i'm going to make the height 2 okay so that leg is going to be in the y direction 2 is that good well then i need to move it down by i need to move it down and so also i'm going to need to move it in x right i'm going to need to move it a little bit in x to the left because the pin isn't coming straight down from the bottom so in x i'm going to need to move it by let's say 3 4 of the radius and so i'm going to move it to the left by point minus 0.75 [Music] and then i am going to move it down by 1 because that's half of how tall it is i'm going to move it down by 1. x positive is up x negative is down and then in c i don't want to move it okay like that ctrl x yes enter and now let's python 3 this thing and i put some sort of mistake in there i got like an extra parenthesis it looks like how did that happen that parentheses probably goes somewhere okay i always hate taking a parenthesis out because usually it actually belonged in there somewhere okay so let's control x yes enter let's run it and i got an error and that's why it didn't go on [Music] so position is equal to vector size is equal to vector color is equal to vector a vector ah i didn't put in the third component okay i see d do you guys see what [Music] i didn't complete that correctly so we're going to nano it and then we're going to come down here and i somehow it didn't get that zero you see i put a comma and that needs a 0 in there so now control x yes enter and now we will python 3 it we get our old friend the error message okay look at that that looks pretty good and now i'm gonna start turning it red okay that looks really good okay that looks really good now what we will do is we'll come over here ctrl c enter and you can see that that really needs to be quite a bit longer that that was too short and so that leg we would want to make that leg longer than that and really if you think about it the leg is way longer than the thing is tall and so where we made the size we made it a two that should really be like a six okay and then we still need to move it down by half of six which would be three like that ctrl x yes enter and now we will run it and shazam look at that watch this we'll bring it up blue and i'll try not to cover up okay you see as it's getting blue in the virtual world it is getting blue in the real world and then we come up to like maximum brightness here in a second okay and then it goes and it wraps back around [Music] shazam that is pretty darn neat let's see if we can get that beautiful aqua color again okay coming up and getting the beautiful aqua color all right guys that is pretty slick okay like i say my goal here was not to make you a visual python expert in one lesson but you can see you can control the color you can control the orientation you can control the position and you can make a sphere you can make a box or you can make a cylinder and with that you can do some pretty incredible stuff okay now this is your homework assignment i think that was pretty that was pretty cool i think that was pretty cool but we do need to talk about your homework assignment okay we got a couple of things that i need to think through a little bit and that is how to better orient the color uh so that the color matches the the hue and the shade match better better calibration between the virtual world and the real world but what i see is is that your eye perceives color in the case of the real world led you know uh you know logarithmically so we had to increase the color you know the intensity by a factor of two with each button push okay but that doesn't work very well in the uh you know in the virtual world that maybe it would have done better with a linear increase and so i got to think a little bit about how to get the color to more perfectly matched just because i'm a perfectionist but what your homework assignment is is to go in and to continue to build out your led because what did we end up with we ended up with one leg on the led so i want you guys to go ahead and build out your model more completely and then what i want you to do is post your solution to youtube so post your solution to youtube in the description of your youtube video leave a link back to this video and when you leave a link back to this video then people have context when they're looking at your solution and then down below in this video leave a comment i am legend and then leave a link over to your homework solution and let's see who can be the most ocd and over the top in their simulation like you could imagine that you could do the simulation the v python simulation of the led by including four legs and all four legs are not the same length so you could kind of do things that way and you might even think about could you have just like something maybe for the breadboard and the buttons i mean just how many components with what we've learned so far which is you can make a sphere you can make a box and you can make a cylinder how much of this could you do in creating a simulation of this in for in visual python all right guys man i hope that you're having as much fun taking these lessons as i am making them i think it's just really cool to be bringing together programming on the raspberry pi with real hardware which are our buttons and our leds and then having a simulation or a model of that on the raspberry pi using visual python you see we're bringing together a lot of different and really cool stuff guys as always i want to give a shout out to you all who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet think about looking down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up and also you really help me if you'll thumbs up this video if you'll leave a comment down below if you'll subscribe to the channel share this with other people because the world needs more programmers and engineers and fewer people sitting around watching silly cat videos paul mcquarter with toptechboy.com i'll talk to you guys later
Info
Channel: Paul McWhorter
Views: 12,931
Rating: undefined out of 5
Keywords:
Id: _q5t46kIC30
Channel Id: undefined
Length: 47min 12sec (2832 seconds)
Published: Thu Jun 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.