Placeholder Image

字幕表 動画を再生する

  • Hi. I'm Phil Gilmore with the Northern Utah .NET Users Group.

  • Welcome to this part of our Scary Gadgets collection - Servo Control Using Raspberry Pi.

  • I'm going to be using Raspberry Pi GPIO pins to do this.

  • I'm using some header jumper wires I purchased at Radio Shack.

  • They're called Schmartboard wires and they run about seven bucks. They're pricey

  • but they're very nice to use if you don't have a Pi Cobbler.

  • I'm going to attach one of these jumpers to pin 11 so I'm going to count 1, 3, 5, 7, 9, 11...

  • and I'm going to jiggle this guy in there very gently.

  • Now I'm also going to use a ground and I know that this one is a ground.

  • The very last one in the corner. I'll stick the second one on there.

  • Now I'm going to use a breadboard to break out to leads from the Pi to the

  • servo motor circuitry.

  • First I'll take my breadboard and I'll add a little 3-pin jumper.

  • This is actually a 2 row jumper instead of a single row but I like these better in a

  • breadboard because they like to stay seated a little tighter.

  • Now I have a way to connect my Pi to my motor.

  • A servo motor has three leads coming out of it. These leads are black then red in the

  • middle and the third is either yellow or

  • white. The red is going to be your positive voltage and the black is ground or 0 voltage as you would expect.

  • The yellow lead takes a PWM, or "pulse width modulation", signal from the Raspberry Pi

  • this is the signal tells the motor to which position it should move.

  • Here's the schematic will be using. I'll be keeping it on the

  • screen for reference but it's going to be small.

  • First I'll connect this lead from the ground on the Raspberry Pi to the jumper header

  • with the black wire coming from the servo motor.

  • Then of course the second jumper wire

  • coming from pin 11 on the Raspberry Pi goes to the yellow or white

  • lead coming from the servo motor.

  • I'll be using an external power source to power the servo motor.

  • We could power the motor off the 5 volt rail coming from header on Raspberry Pi

  • and that's a little simpler to do.

  • But I'm going to show you how to do it this way in case you have the need.

  • I'll just take this battery out so we're not powering our circuit before it's completed.

  • Now I'll connect the negative from my power supply to the black wire

  • on the servo motor, which is also conjoined with the ground on the Raspberry Pi.

  • This will connect the positive from the servo motor to the positive on my power supply.

  • That's our last connection,

  • so it's now safe to power this circuit. I'll pop my battery back in.

  • And we're done. Let's take a look at some code.

  • Here's our first line.

  • It imports the Raspberry Pi GPIO library and gives it an alias.

  • We'll need this import to use the sleep() method later on.

  • Raspberry Pi allows you to reference its pins in either of two modes.

  • The "GPIO" pin references

  • are different depending on which Raspberry Pi model you have.

  • So I prefer to use the other mode, which is the "Board" mode.

  • This allows you to reference pins based on the pin number on the P1 header

  • So you can just count the pins to figure out which one you're referring to.

  • I set that here

  • Most of the GPIO pins on Raspberry Pi can be set to one of several modes,

  • depending on the function that you want them to perform. Here I'm going to set

  • physical pin 11's function to digital output.

  • To send a PWM signal on one of the GPIO pins,

  • this particular Python library allows us to create a PWM object.

  • Its constructor takes

  • a pin number and frequency. I'm going specify pin 11

  • at a frequency of 50 Hertz.

  • Even though we'll be driving the servos using a PWM signal,

  • the library we're using composes a PWM signal as a combination of a

  • frequency in Hertz and duty cycle as a percentage

  • of each cycle in that frequency. But the servo motors don't work that way.

  • They want pulse times of an absolute time rather than a percentage

  • of the frequency. So we're going to have to specify

  • what those pulse times are and then calculate

  • the duty cycle percentage based on that in a moment. So here I specify

  • the absolute times that our motors like. Most of these motors are pretty much the same.

  • They have a Left position somewhere around 1 millisecond, maybe a little less.

  • And a Right position somewhere around 2.5 milliseconds. I'm calculating a left, right and

  • middle position because we're going to set our motor to each those positions later on.

  • You can play with his pulse times to see what works best for your motor.

  • In order to convert our absolute pulse times to a percentage duty cycle,

  • we need to know how many milliseconds there are per cycle. The formula for that

  • is to divide 1000 by our frequency

  • in Hertz as specified in the constructor of our PWM object.

  • Now that we have most of the data we need to calculate our PWM signal,..

  • I have a set of nested loops here. The inner loop

  • just goes one step at a time through the positions specified in the sequence,

  • that I constructed up above, and it calculates

  • the duty cycle percentage, which is just the position converted to a percentage

  • based on the mill seconds per cycle.

  • I'll put some data about each position as we go through them.

  • And after I output it I set the PWM signal to represent it.

  • And then I sleep for 0.5 seconds and start all over again.

  • When those loops have finished executing, we send a stop signal to the PWM to

  • terminate the signal

  • and relax the motor.

  • Most importantly when we're finished with the hardware, we relinquish all control of it

  • to any other processes that might be trying to use it.

  • Let's try it.

Hi. I'm Phil Gilmore with the Northern Utah .NET Users Group.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

Raspberry Piを使ったサーボ制御。 (Servo control using Raspberry Pi.)

  • 522 14
    Sea Monster に公開 2021 年 01 月 14 日
動画の中の単語