Raspberry Pi LESSON 28. Controlling a Servo with Raspberry Pi and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is pulmicort er with top tech boy comm and we are here today with lesson number 28 on using your Raspberry Pi what we are going to do in this lesson is we are going to show you how you can control a servo using the Raspberry Pi a couple of things I need to tell you right off the bat number one this is not going to be as easy as controlling a servo with the Arduino I will say I'm going to show you step by step and it's very straightforward it's not just a simple matter like it is on the Arduino though of just writing to the servo and say go to a certain position we've got to do a little bookkeeping okay so what you need to know is in order to control the position of the servo you have to send PWM signals to the Raspberry Pi GPIO pins good news is in lesson number 27 I showed you all that you needed to know about doing PWM on your GPIO pins what I am suggesting now is is that if you haven't done less than twenty-seven yet or it's been a while since you've done lesson 27 go back and look at that lesson and that will make this lesson 28 very easy where is lesson 27 go to my most excellent website WWE at Raspberry Pi with lies Linux lessons and then come down and you can click on lesson number 27 and that shows you how to get GPIO PWM on your Raspberry Pi okay so that is just a heads up refresh your PWM you could probably do this lesson without it but this lesson will be easier if you go back and you look at that lesson okay item number two understand that your raspberry pi can only put out so much current and if you hook a big old servo to your raspberry pi that wants more current than what your raspberry pi can produce you can actually damage the raspberry pi and so before I hook this servo up to my raspberry pi I looked at the spec sheet of the servo and verified that it does not require more current than what the Raspberry Pi can easily produce if you are worried about that what you can see is is that I am actually powering my servo through physical pin two on the Raspberry Pi if you are worried about burning out your Raspberry Pi take this power line off and go connect it to an external five volt supply drive the servo power the servo from an external 5 volt supply and you can still control it from the Raspberry Pi through this yellow wire ok just power it from an external voltage source control it from the Raspberry Pi make sure that you use a common ground you have to connect all your grounds together for that to work okay so don't burn out your Raspberry Pi don't hook this up in power unless you're sure that you're okay I am sure that I am ok so I'm configuring my servo as follows I connect the red wire to physical pin 2 which is 5 volts out on the Raspberry Pi the black wire is ground I am connecting that to pin 1 3 5 7 9 physical pin 9 on the Raspberry Pi as a ground then physical pin 11 I use to control the servo on my servo black is ground red as power in yellow is control if you do not have a black a red and a yellow wire coming off of your servo look in the instructions and see what your color codes are but whatever the color codes are you can put power to pin 2 you can put ground to pen physical pen set 9 and you can put a control to fiscal pin 11 so let me just make sure 1 3 5 7 9 physical pin 9 is ground physical pin 11 is control okay so you can hook yours up like this and then you can follow along in these lessons alright I've got to stop and I've got to talk a little bit about how servos actually work because if you are from the arduino world you think servos work by just saying write zero or white write 180 or write 90 or write whatever your desired angle is and it just pops over there and does that on the raspberry pi it's not that easy and we have to apply pwm signals and we have to sort of understand how the servo works and so i've got some important points here I've got a little document I want to go over with you and this will allow you to understand how the servos work first of all servos or servos are controlled using PWM signals which you learned about in lesson 27 in lesson 27 I showed you how to send and control PWM to your GPIO pins on the Raspberry Pi second thing is most servos like to see a 50 Hertz control signal so when you set up your PWM on your GPIO pins you want to set it up for 50 Hertz I'll show you how to do that in a minute but just understand you can't it's not good to just go in and try to use any old frequency you want to set the frequency of your PWM signal to 50 Hertz good news is that's easy enough to do third the position of the servo depends on the pulse width of the on part of the cycle okay so when you have PWM you have a pulse train coming across like this that you're repeating where the servo goes to depends on the pulse width of the on part of the cycle how long you leave that on is what defines where the servo is going to point most servos are in the middle this would be the middle like this most servos are in the middle with a 1.5 millisecond pulse width so if I send a PWM signal and the width of the on part of that is 1.5 milliseconds I'll be kind of up and down in the middle on the servo most servos are in the full right position at two milliseconds and so if I turn this on with the two millisecond pulse it'll spin over here like this alright and most uh servos are in the full left position let's see I'm sorry I'd need to just go over these again full left is one millisecond middle is 1.5 millisecond and full right is two milliseconds okay now the thing is when you do a PWM command in the Raspberry Pi it doesn't ask you for pulse width it asks you for duty cycle so we need to go through some definitions that a little math to understand how to go from duty cycle to pulse width the first thing that we have to understand is the period is equal to 1 over the frequency when we set the raspberry pi up when we do setup the PWM we tell it what the frequency is well what you've got to understand is the period is 1 over the frequency so what frequency did we see say we wanted to use 50 Hertz I will show you how to do this in a minute or this stuff is ringing a bell if you looked at lesson number 27 so for a 50 Hertz signal the period of the signal is what the period is 1 over the frequency 1 over 50 is equal to 0.02 seconds which would be 20 milliseconds so what you need to understand is the full amount of time that we have to work with with a 50 Hertz signal the full period is 20 milliseconds to be in the full lab position we would want a pulse width of 1 milliseconds one millisecond would be a duty cycle of 5% why because what's the full period the full period is 20 milliseconds what's 5% of 20 that's point O 5 times 20 that would equal one millisecond so if I send the pin a command to go to a duty cycle of 5 percent with a 50 Hertz signal that would equal one millisecond so that would move the servo to the full left position okay to be in the middle position we would want to pulse width of one point five milliseconds one point five milliseconds would be a duty cycle of 7.5% since 7.5 percent is 0.075 times 20 milliseconds would be one point five milliseconds and remember from most servos one point five milliseconds would put the servo up and down and finally to be in the right position all the way over here we would want a pulse width of two milliseconds which would be a duty cycle of 10% since 0.1 that's 10% times 20 milliseconds that's our whole period would be 2 milliseconds so we can see sort of 3 values on duty cycle to go from 0 to 90 to 180 bottom line to move the servo full left we need a duty cycle of 5% for the center position a duty cycle of seven and a half percent and for full right a duty cycle of 10% this is all based on setting up a 50 Hertz signal to begin with intermediate values would just be linearly scaled between that so you would just linearly scale your duty cycle from 5% to 10% to go from a position of 0 to 180 okay now we must test these to see if they are reasonable in experimentation we found so we got to test these things and see if actually our particular servo really does what we think it should at five seven and a half and ten and before we go in and do more math we need to sort of tweak these values for our particular servo so what do we need to do well we need to call up our odd we need to call up our let's not that we need to call up our terminal window which I should be able to do here okay pardon me I should have had this pulled up already but I will get it done here very quickly give me just a second to get this configured where you can see it okay and I'm sorry I did not have this set up beforehand but now we are in a terminal window and hopefully you can see this all right so now we're going to start operating the servo what I like to do is I like to we're going to do this all in Python and we're going to write a Python program to do this but as I'm learning I like to run Python in the shell because then I can give commands one at a time and see if it does what I want so we're going to learn how to do this giving the commands one at a time in the shell and then we'll come back and we'll write a program in a minute but to enter the shell how do we do it we say Python but we want to enter the shell is a super user pseudo Python because okay because uh the normal PI user does not have access to the GPIO pins and so you want to go into the Python shell as super user and that way it will take your commands to mess with the pins so we're going to go sudo python ok boom those three little arrows means that we are now in the Python shell so what's the first thing we need well we need to get the rpi library so we're going to say import we're going to say RP little i dot GPIO as GPIO so we're going to import the RPI library boom it likes that if you get an error here you need to exit out of the Python shell and you need to you do that with a ctrl D and you need to go in and do a sudo apt-get update and a sudo apt-get upgrade because the later versions of the operating system will have this pre-loaded and so if you update and upgrade your system you will have this ok so we have loaded the library what do we need to do now we need to tell it what pin configuration what pin numbering configuration we want to use I like using the physical pins the physical pins are the easiest because it just goes 1 2 3 4 5 6 like that and that's easy for me to remember so I need to tell it to use the physical pin numbering convention how do I do that well I tell it GPIO dot set mode and then I'm going to go GPIO dot board when I say dot board that's saying to use the physical pin code configuration if I'd said dot DCM then it uses that goofy gpo GPIO table I've got that I think in lesson 25 but if you do board you can just sort of remember what to do so let's see if it likes that boom it likes that now what do I need to do I need to tell it that this pin 11 is an output 10 remember this is pin 1 3 5 7 9 is ground and we're connecting and controlling through 10 11 and so we need to set up 10 11 as an output so I'm going to say GPIO dot set up and then I'm going to say which pin 10 11 and it's a what it's a GPIO dot out like that boom pin 11 is now an output okay now we've got to set up the pulse width modulation so what am I going to do I'm going to create an object called PWM and I'm going to set it equal to GPIO dot PWM that's the command and what do I have to tell it well I've got to tell it the pin that's 11 and then I have to tell it what the frequency now that should be , I got to tell it the frequency what frequency do servos want to see 50 Hertz we talked about that so I see 50 okay boom I now have 10 11 set up to be a 50 Hertz PWM signal but what I've got to do now is I have got to tell it to start the PWM so I'm going to say PWM dot start and then what do I do in this particular case I need to give it the duty cycle well for a full weft I think we said a duty cycle of 5 should put it about pull left now we're going to have to tweak these values and sort of see exactly what they are but when I give this command this should turn almost all the way to the left let's see if it does not quite so let's let's play around with this a little more let's go now what I got to do is I've got to play with the duty cycle and the way you do that is you say pw m pw m dot change duty cycle okay and let's say you see i didn't go as far to the left as I wanted so let's say go to a duty cycle of 2 and let's see if that gets that over there black boom that got it over to the left let's say if we go to 3 it should be a little bit more to the right ok it moves a little bit to the right now what did we say roughly full up and down would be that was 7.5 if I remember right roughly okay and that's kinda it's a little bit past but it's kind of in the middle and then all the way to the right I believe we said would be 10 okay and that's kind of like almost all the way to the right so you see those general-purpose numbers of 5 a duty cycle of 5 7 1/2 and 10 got us in the ballpark but before we actually do the math we want to kind of you know tweak those numbers so that we get it working just exactly right with this particular servo so I want to see what the full left position is and so instead of 5 I'm going to go all the way down to one so watch the servo okay that sure went all the way over but I might have gotten too far so I'm going to look just to move it at all no to didn't move it so two is still full left what it would three be full left no three moved a little bit to the right so if I go back to two you can see two is really pretty darn close to all the way to the left so now I need to see what is all the way to the right well normally we said so I'm going to write this down full left is two and that's a duty cycle of two percent is what that is two percent duty cycle on a 50 Hertz signal so let's see what will take us all the way to the right ten does not quite take us there let's see if 11 does okay 11 took us a little further let's see 12 okay 12 seems to even take us a little bit further and so that 12 seems to be pretty good so I have two and I have 12 if I go back to 11 you can see it backs off a little bit if I go to 12 it goes a little further let's go back to all the way to the left to let's go all the way to the right 12 okay so where would we expect halfway to be well halfway between 2 and 12 would be 7 I do believe and that is pretty darn right straight up and down in the middle okay so what I need to remember is the two points that take me to the two extremes or a duty cycle of two is full left and a duty cycle of 12 is full right so let's go back and let's do a little math because I want to just send it a command you know basically of an angle that I want to go to and then I want to go to that angle based on doing some math but before I leave here before I forget it I'm going to turn the PWM off so I'm going to say PWM stop okay that turns it off and then I'm going to do GP i/o dot clean up boom okay that should kind of shut things down and you know I've sort of left gracefully and I'm going to ctrl D to get back to my blunt Linux command line all right now let's go in and let's do some math okay so what we did was we found that two percent and 11 percent what we found was that the 5 percent 7 1/2 and 10 percent weren't the perfect numbers for our servo for our servo we found that 2 percent was right on the low end and 12 percent was right for the full right position this might be different on your servo but if 2 was full left or if I was full left just put that full left and full right just make note for me it was 2 percent was full left and 12 percent was full right yours will be a little different but you can still follow along with the way I'm doing this ok well if we want to input a degree setting and have it moved to that position what we would want to do is we would sort of need two points and draw a line between them if we enter 0 degrees we want it to be full left which we know is a duty cycle of two percent we just did the experiment so the first point we want to work with is the point 0 which is the desired degree and 2 which is the duty cycle which would give me 0 degrees okay if we enter 180 degrees we want it to be full right which would be a duty cycle of what I'd said 11 percent but really it was 12 percent for me yours might be a little bit different this would be the point 180 12 because if we put a duty cycle of 12 it was making it go all the way to 180 so I have 2 points all right if I want to go to zero I apply a duty cycle of two if I want to go to 180 I apply a duty cycle of twelve and any value between zero and 180 you just come up with an equation of the line and you can move it to any position you want perfectly with two points we can calculate the slope of the line the slope of the line is y2 minus y1 over x2 minus x1 well that would be 12 minus 2 for me divided by 180 minus 0 and 12 minus 2 would be 10 over 180 which would be 1 over 18 now you can do this you do this with your own numbers that you find with your servo don't use my numbers do it the way I'm doing it but you've got to get the number here and here this 2 and the 12 that were what moved your servo hopefully that makes sense now remember the point slope form of the line is y minus y1 is equal to M over m times X minus x1 so we got to pick one of these points you can pick either point 0 + 2 or 180 + 12 but it'd be easier to use 0 + 2 so we're going to pick that point so Y minus 2 is equal to the slope which was this became 1 over 18 for me he's became 1 over 18 for me so Y minus y1 is M onto X minus X 1 Y minus y2 is equal to the slope which was 1 over 18 times X minus 0 that's that zero there so Y is equal to 1 over 18 times X + 2 so we input the degree position we desire is X and then we calculate Y which will be the duty cycle so let's take this and let's use our real variable names the duty cycle is going to be equal to 1 divided by 20 times the desired angle plus 2 then you just plug in your desired angle and it will calculate the duty cycle that will take you to that desired pretty cool huh okay let's see if we can write a Python program to do that if we are going to write a Python program we need to do what well I Nica LS and C I've got a probe I've got a folder called my Python I'm going to put things there if you don't have one you could do a make directory my Python from your home directory mine is already there so I'm not going to do that I am going to go down into it I like to go down into my folder because that way I don't have to keep typing in my path name so I'm going to go to my Python and then LS you can see that I got different things there I need to RM servo dot PI because I want to make that ID kind of practiced it before I got on camera so now what do I want to do I want to create a program how do I create a Python program why do Nano and then what do I want to create the program servo dot PI come up with a bright spankin flashy new empty file so how do I edit it what's the first thing that we do we've got to import the library what was the library our P little I got GPIO as GPIO okay capitalization matters you have to do it exactly this way big R big P little dot GPIO all caps as GPIO all caps all right what do we have to do now we have to do GPIO dot set mode okay GPIO is all caps set mode is all lowercase and then what do we do GPIO what is this command doing it's saying what pin out diagram I want to use I want to use the physical pin out so I'd say what Bo AR D remember that from earlier and from the earlier lessons ECM if you want to use GP at those GPIO configurations but here this is just straight the physical pins on the board easy to remember okay now what do I do well I'm just going say months I want to give my servo servo pin I'm going to call that physical pin 11 and that way I don't have to keep typing 11 in what do I need to do now I need to set that as an output so I do GPIO dot set up and then which can am i working with servo pin and then what do I want it to be I want it to be a GPIO dot out so I'm setting servo pin to be an output 10 servo pin is pin 11 and that's where I'm connected here all right now is where things get a little tricky I need to create my PWM objects so I'm going to say PWM you could name this whatever you want but then you've got to use that vein from then on out so PWM is going to be equal to GP i/o dot PWM and then which pin am i setting up I'm taking setting up servo 10 that's where I want to do this and then what frequency do I want okay I think I'm going to come up now I'll just do it here so I'll just say 50 Hertz remember servos want to see what frequency they want to see 50 Hertz so I tell it 50 Hertz there that should make it happy all right now let's see I'm going to do a for loop because I want to loop through this and in the loop I want it to ask me what position you want to go to and I want to go to that position and I won't say what position you want to go to and then I want to go to that position what position you want to go to I want to go to that position so I'm going to do it in a 4-week so I'm going to say for I in range okay and where do I want to go I want to go let's say I want to go 20 times and so I go from 0 to 20 which really will go stop at 19 and 0 to 19 is 20 choices and so I'm going to be able to do this 20 times all right now how do you indicate a for loop in Python you tab over so what's the first thing I need to do I'm say desired position is equal to input where do you want the servo question mark and then what we are going to say is from from 0 to 180 that's just telling them what range they can use and then I'm going to put that and then I close that off like that okay where do you want the servo alright I think that's going to work I'm sorry that this is let me let me bring this on out so that you can see it a little bit better ok so you see that that's just saying where do you want the servo question mark and prompting it that you can enter between 0 between 9 and 180 ok I just want to see that on one line so you didn't get confused by that dollar sign alright so that is straightforward now what do I need to do I needed to before I did this I needed to start before the for loop I need to start it so I need to do PWM dot start and I just got to start somewhere so I'm going to start roughly in the middle which should be about seven okay so PWM start what you put here is your starting duty cycle and so I started at a duty cycle of seven which you put it about somewhere in the middle right when I write when I turn the thing on okay now I need to come down and so I input from the user the desired position then I want to apply that position so what do I do PWD WM and then what do I do I say change duty cycle alright what do I want to change duty cycle - well I want to change it to duty Sakuragi see all right well what is DC I better calculate that okay let me tab this over okay so I better calculate duty cycle so how do I calculate duty cycle well I'm going to say duty cycle is equal to my slope I need to peak over here again my slope was 1 over 18 okay 1 divided by 18 now listen to me if you just put 1 divided by 18 or 1 divided by whatever your slope was for your servo an integer divided by an integer is 0 and your thing's not going to work so you have to force this to be floating point by saying 18 point so if I say let's say 1 point divided by 18 point that forces those two numbers that forces those two numbers to be floating-point numbers which means that you'll get a real slope when you divide that and not just get 0 hopefully that makes sense times what times our desired position okay and then if I go back and look at my equation I believe it was plus 2 so I say plus 2 so I asked for the desired position based on the math we did I know that the duty cycle should be 1 divided by 18 times the desired position plus to your equation might be a little bit different for your servo alright then I need to apply that with a PWM change duty cycle to D which I've just calculated and now that should put it there all right so as long as I'm in that loop it's just going to ask me where do you want to go I'm going to go there where do you want to go I'm going to go there where do you want to go I'm going to go there and now in the program we need to exit gracefully which means we need to do some cleanup so I need to do my or I need to do PWM stop like that to turn the PWM off and then I need to do a GPIO dot clean up and that frees those pins up and so you want to do this you want to do this stuff so that your to keep in you know keeping your bookkeeping good and keeping cleaning up after yourself all right so now how do we save it control oh and then it says file name - right servo dot pi I will accept that so I click enter and then how do I exit out control X now remember you have to run this as pseudo you have to run the program as pseudo so that you can access those pins it won't let you as PI access them so I'm going to say sudo I am down in the same folder let's look LS I'm in the same folder with the program servo PI so I don't have to give it a path name so I can say sudo python what program to run servo PI and we'll see if I made some mistakes in there whoa where do you want to get set it 909 to 180 yeah that's that's no good control X control C all right let's let's go back and edit that again nano servo PI okay why did I put it why did I put an on here that's a zero window from I always try to use the mouse when you can't use the mouse okay that should have been zero and also I'll scoot this over so you can see it I don't want my answer to the question to be jammed up against the zero sup should put a space there and that way it looks a little bit nicer when I run all right and so now we should be able to bring this back over here like that okay brings back over here a little bit and now we should control oh do I like that file name enter and then control X okay now let's do a sudo python servo dot dot pi import I must have a I must have an Arabic so let's go in and look and see what I did wrong I probably accidentally typed something in there yeah look at that I'd hit a key that I shouldn't have not have an O there so hopefully you guys saw that hopefully you don't have that problem so I'm going to ctrl o and then enter and then ctrl X now I'm going to sudo python servo PI all right now it's giving me an error it's giving me a warning because I killed the program the poke program crashed before it got to that code that cleans things up it should still run it just gives me a warning so where do I want to go I thought I'd change that non till maybe I thought I'd change that darn it I'm sorry guys let's go back and look at it okay sudo python servo PI control vex you will see something too making nano servo PI I need to edit it okay I see what I did like an idiot I added the zero but I didn't take the nine out so zero to 180 control Oh filename - right servo pie I like that so I will click enter and then I will click control X to get out of here and now sous-sous go Python and then what program do I want to run servo dot pi okay where do you want to set the servo let's try zero now you guys all eyes on the servo over here all eyes on the servo look at that all the way to zero let's see if we can go to 180 boom all the way to 180 let's see if we can go naughty in the middle right there in the middle and do the bath don't just guess and play with it do the math like I did and you see when you do the math your servo works exactly and it's not it works exactly right and it's not like every other servo it works perfectly for the servo that it is let's try 45 degrees boom I'm at 45 let's try 135 boom I'm at 135 let's go back to zero okay I am at zero 90 okay 180 okay let's see let's go 175 let's see if I can just back off just a little bit okay you see how that moved just a little bit let's go to zero now let's just go to five okay see how it moved just a little bit this is perfect servo control and to get the perfect servo control you do the calibration like we did earlier in the lesson and then you do the math and then this thing will do whatever you want it to do you can imagine now that we could write a program that would just smoothly step from zero to 180 I know you guys get tired of me but I just got a I just got to do that okay so let's let's play around with this let's go suit let's go nan-oh servo pie and this time I'll save it as a different is a different name but what I'm going to do is I'm going to say for I in range ah let's go 0 to 180 degrees okay so I'm not going to input it I'm just going to tell it to go from 0 to 180 degrees and then I'm going to come here and I delete this I'll go to the end and then delete all this okay so I'm telling it I want to go from 0 to 100 degrees I still have to calculate my DC but my DC is going to be based on I this time because I'm not inputting something so it's going to be I okay and then PWM duty cycle that should I think that should do it okay and then I'm going to kind of show my lack of remembering about Python but I think I can go for I and range I think I can go backwards I'm going to say go 180 comma 0 I might need to put like a minus 1 in there but we will see if this will work again I work with so many different programming languages that I'm always switching between them sometimes I forget the syntax but we are going to say DC is equal to 1 divided by 18 point times I plus 2 and then TW m dot change any cycle and then DC do you see what I'm trying to do I'm trying to get it to smoothly go from 0 to 180 and then from 180 back to 0 and I'm just not sure about the second for loop but we can we can give this a try I'm not sure if you got to specify a step size on here or or Python we'll figure that out okay now I'm going to control X no yes I want to yes save it but I want to save it as servo - because I want to keep my first program ok and then enter ah yes ok now let's just look at it just to make sure so how do we do that cat serve bowtie make sure that your servo - 2 pi let's just look at it ok that looks like it's the new program so now we're going to do a sudo python and then what's the program - - this time so let's see what happens all eyes on the servo okay you know what I'm probably going too fast we probably need to slow it down so let's go back to a nano servo to dot pod okay so what we're going to do here is we're going to import time okay and then down down here we got to give it a little time to settle out okay so we're going to do a crystal time dot sleep of 0.1 seconds 0.05 let's see if it can go that fast and then here we need to do the same thing we need to do a time dot sleep when you do point 0.05 we might need to rest longer than that if this doesn't work control oh yes control X okay now we are going to sudo python server - 2 pi all eyes on the servo okay look at that there it goes nice and smooth all the way over is it going to go back or is it going to crash okay it crashed because I probably have to put a minus 1 in there for the kind of the step and so let's come back and again I am revealing my lack of freshness in some of these commands I'm just kind of going on my own here so comma minus 1 let's see if it'll take that control Oh to write out enter in control X and let's try running it again pseudo Python server ok ok so now we're going to go nice and smooth from 0 to 180 and aah oh I must build something ok hopefully you guys caught that in a servo - and look at that pwm Seibel I shouldn't cycle control Oh enter control X and we're going to run it again sudo python servo - - hi okay now here we come from 0 to 180 everybody is happy and will it turn around and go back oh yes success it is going to go forward in backwards and forwarding back that's alright but it's going to stop when it gets here what if we want to make it just continue to loop what we want is we want something sorta like the you remember the old void loop you kind of get a void loop for free in arduino here we've got to set one up and so what I could do here is I could say while and then I could just say while 1 because 1 is always equal to 1 so this is a while loop that's going to loop forever but now I've got to increase my in debt everywhere by 1 so that's got to be indented that's got to be indented right all these clauses are defined by the indents and so everything is defined by the indent and so if I'm adding a loop I have to add indention to all of these things ok alright so let's look at this the while loop starts here and then it ends here this is the first command after the while loop the for loop the first one starts here and this is the last indented so all these are part of that for loop and then all of these are part of that for loop and then these two for loops are indented from there seem kind of got nested indentation so let's look at control o to write out enter and now control X and now let's do it servo nano servo to dot PI will it work it should just keep going back and forth nice and smooth oops sudo python servo to PI that runs it all eyes on the servo boom here it's coming from 0 to 180 see how nice that is and now it's going to go back nice and easy nice and easy it goes back and then it goes forwards it's going to keep running it's going to keep running isn't that great I wish I could make it go a little faster now if I make it go too fast it's going to really mess up but I'm a risk taker so we're going to see if we can make it go faster so I'm going to ctrl C to stop the program okay now we're going to edit the program again and let's see if we can make it go like about twice as fast so we're going to stop a smaller one Oh 2.0 to control Oh control enter and then control X now let's look at it pseudo Python server - whoa look at that thing go look at it go that is a thing that's a beautiful thing to behold doing exactly what we wanted I wish it was going a little faster control C you know what we're going to do now we're just going to edit it okay I was going to input the speed but I won't put you through that I won't put you through that you know where you would input how fast you want it to go but that will be for another day let's see if we can still make it go twice as fast okay ctrl o enter ctrl X ok now let's go sudo python servo - whoa look at that thing it's flying it'll I I actually probably would not try to make it go much faster than this you notice that I'm getting the full extent of motion and it's it's operating smoothly but I would probably not try to make it run any faster than that ok guys this has been a great lesson you now can control servo using Python and the Raspberry Pi the great thing is is that you've got a whole bunch of GPIO pins that you can use you could control something that was walking around you can really go in and start doing things now the secret to robotics is being able to do servo control and you have got full control of your servo now using Python thanks to top tech boy comm palma quarter if you like this give us a thumbs up think about sharing the video maybe subscribe to us we will see you guys later in lesson number 29 talk to you later
Info
Channel: Paul McWhorter
Views: 163,390
Rating: undefined out of 5
Keywords: Python (Programming Language), Raspberry Pi (Computer), Servo, GPIO, PWM
Id: SGwhx1MYXUs
Channel Id: undefined
Length: 48min 53sec (2933 seconds)
Published: Tue Jun 02 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.