issue114:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| issue114:python [2016/11/02 09:36] – créée auntiee | issue114:python [2016/11/18 12:27] (Version actuelle) – auntiee | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | This month, we are going to interface a servo motor to our RPi. This requires only a servo motor, the Raspberry Pi, the breadboard, some jumpers, and the battery pack we used last month. | + | **This month, we are going to interface a servo motor to our RPi. This requires only a servo motor, the Raspberry Pi, the breadboard, some jumpers, and the battery pack we used last month. |
| A servo motor is simply a motor that has a control circuit and a potentiometer to give the control circuit the position of the output shaft. MOST servos will rotate the shaft between 0° and 180°. There are some that go 360° and more, but they cost a lot. Gears do all the connections between the motor, the potentiometer, | A servo motor is simply a motor that has a control circuit and a potentiometer to give the control circuit the position of the output shaft. MOST servos will rotate the shaft between 0° and 180°. There are some that go 360° and more, but they cost a lot. Gears do all the connections between the motor, the potentiometer, | ||
| - | The control signals are expected in a very specific “format”, | + | The control signals are expected in a very specific “format”, |
| - | The Wiring | + | Ce mois-ci, nous allons interfacer un servo-moteur à notre RPi. Il ne faut qu'un servo-moteur, |
| + | |||
| + | Un servo-moteur est simplement un moteur qui a un circuit de commande et un potentiomètre pour donner au circuit de commande la position de l'axe de sortie. La PLUPART des servos feront tourner leur axe entre 0 ° et 180 °. Quelques-uns vont jusqu' | ||
| + | |||
| + | Les signaux de commande sont attendus sous une « forme » très spécifique et nous utiliserons PWM ( Pulse Width Modulation - Modulation de largeur d' | ||
| + | |||
| + | **The Wiring | ||
| The connections are very simple this month. The battery pack powers the motor, so +voltage on the servo goes to the + voltage rail, and the negative servo wire goes to the negative rail. We connect the negative voltage from the battery pack (negative rail) to pin 6 of the RPi. GPIO pin 23 (pin 16) goes to the control wire of the servo. | The connections are very simple this month. The battery pack powers the motor, so +voltage on the servo goes to the + voltage rail, and the negative servo wire goes to the negative rail. We connect the negative voltage from the battery pack (negative rail) to pin 6 of the RPi. GPIO pin 23 (pin 16) goes to the control wire of the servo. | ||
| Ligne 19: | Ligne 25: | ||
| Frequency = 50 Hertz | Frequency = 50 Hertz | ||
| - | So now, when we set up our code, we can set the GPIO.pwm command to the control pin and use 50 for our frequency. | + | So now, when we set up our code, we can set the GPIO.pwm command to the control pin and use 50 for our frequency.** |
| - | Our first program will start somewhere close to 0° and then move to close to 90° then move to close to 180°. I keep saying close, because every servo is a bit different. We don’t want to set the DutyCycle to 0 and have it slam to the limit and potentially damage the servo, so we will start off with a low number close to 0 and end with a number close to 12 in order to start to “dial in” a set of values that work. For my servo, the numbers 3 for 0° and 12 for 180° worked well. | + | Le câblage |
| + | |||
| + | Les connexions sont très simples ce mois-ci. Le bloc de piles alimente le moteur, de sorte que la tension + sur le servo aille sur la ligne + et que le fil négatif du servo aille sur le ligne -. Nous connectons la tension négative du bloc de piles (ligne négative) sur le picot 6 du RPi. Le picot 23 de GPIO (picot 16) est relié au fil de commande du servo. | ||
| + | |||
| + | Maintenant, un peu de maths. Comme nous avons dit précédemment, | ||
| + | |||
| + | Pwm = GPIO.pwm({RPi Pin}, | ||
| + | |||
| + | Nous connaissons le numéro du picot (23), mais nous devons convertir les 20 ms en Hertz pour paramétrer la commande de réglage de pwm. Comment le faire ? C'est simple. | ||
| + | |||
| + | Fréquence = 1/temps | ||
| + | Fréquence = 1/0,02 (20 ms) | ||
| + | Fréquence = 50 Hertz | ||
| + | |||
| + | Ainsi, maintenant, quand nous préparons notre code, nous pouvons régler la commande GPIO.pwm pour le picot de commande et utiliser 50 pour notre fréquence. | ||
| + | |||
| + | **Our first program will start somewhere close to 0° and then move to close to 90° then move to close to 180°. I keep saying close, because every servo is a bit different. We don’t want to set the DutyCycle to 0 and have it slam to the limit and potentially damage the servo, so we will start off with a low number close to 0 and end with a number close to 12 in order to start to “dial in” a set of values that work. For my servo, the numbers 3 for 0° and 12 for 180° worked well. | ||
| Servo1.py | Servo1.py | ||
| Ligne 42: | Ligne 64: | ||
| sleep(5) | sleep(5) | ||
| - | This should put the rotor into the centre position (90°). If you have changed the first number or the next number, this will have to change as well. | + | This should put the rotor into the centre position (90°). If you have changed the first number or the next number, this will have to change as well.** |
| - | pwm.ChangeDutyCycle(12) | + | Notre premier programme commencera au voisinage de 0 ° et se déplacera à proximité de 90 ° puis ira jusqu' |
| + | |||
| + | Servo1.py | ||
| + | |||
| + | import RPi.GPIO as GPIO | ||
| + | from time import sleep | ||
| + | |||
| + | GPIO.setmode(GPIO.BCM) | ||
| + | GPIO.setup(23, | ||
| + | |||
| + | pwm = GPIO.PWM(23, | ||
| + | pwm.start(2) | ||
| + | pwm.ChangeDutyCycle(3) | ||
| + | sleep(5) | ||
| + | |||
| + | 3 vous donnera le premier angle, mais vous pourriez avoir à essayer avec 2, 1, 0 ou 4 pour y parvenir. Notez bien ce numéro. | ||
| + | |||
| + | pwm.ChangeDutyCycle(6) | ||
| + | |||
| + | sleep(5) | ||
| + | |||
| + | Ceci devrait placer le rotor en position centrale (90 °). Si vous avez modifié le premier chiffre ou le suivant, celui-ci devra aussi être changé. | ||
| + | |||
| + | **pwm.ChangeDutyCycle(12) | ||
| sleep(5) | sleep(5) | ||
| Ligne 54: | Ligne 99: | ||
| Finally we call GPIO.cleanup() to set everything to normal. | Finally we call GPIO.cleanup() to set everything to normal. | ||
| - | Now comes the heavy duty math part. We will use the values 3 and 12 as y1 and y2 respectively for the formula. Substitute your numbers. | + | Now comes the heavy duty math part. We will use the values 3 and 12 as y1 and y2 respectively for the formula. Substitute your numbers.** |
| + | |||
| + | pwm.ChangeDutyCycle(12) | ||
| + | |||
| + | sleep(5) | ||
| + | |||
| + | Le dernier chiffre devrait vous amener à la position 180 °. Là encore, essayez quelques chiffres d'un côté ou de l' | ||
| + | |||
| + | GPIO.cleanup() | ||
| + | |||
| + | Enfin, nous appelons GPIO.cleanup() pour un retour à la normale. | ||
| + | |||
| + | Maintenant arrive le gros morceau de maths. Nous utiliserons les valeurs 3 et 12 pour y1 et y2 respectivement dans la formule. Remplacez-les par vos propres numéros. | ||
| - | Offset = (y2-y1)/ | + | **Offset = (y2-y1)/ |
| Offset = (12-3)/ | Offset = (12-3)/ | ||
| Offset = 9/180 | Offset = 9/180 | ||
| Ligne 71: | Ligne 128: | ||
| Well, that’s about it for this month. Next time, we will be working with a stepper motor, somewhat a cross of both a servo and a regular motor. | Well, that’s about it for this month. Next time, we will be working with a stepper motor, somewhat a cross of both a servo and a regular motor. | ||
| - | Until then, enjoy! | + | Until then, enjoy!** |
| + | |||
| + | Offset = (y2-y1)/ | ||
| + | Offset = (12-3)/ | ||
| + | Offset = 9/180 | ||
| + | Offset = .05 | ||
| + | |||
| + | Maintenant, si nous réglons le rapport cyclique pour un angle entre 0 ° et 180 °, nous utilisons la formule suivante : | ||
| + | |||
| + | DutyCycle = (Offset * angle) + 2.0 | ||
| + | |||
| + | DutyCycle = (.05 * angle) + 2.0 | ||
| + | |||
| + | Quand je l'ai fait, ça fonctionnait, | ||
| + | |||
| + | Voilà, c'est tout pour ce mois-ci. La prochaine fois, nous travaillerons avec un moteur pas-à-pas, une sorte de croisement entre un servo et un moteur ordinaire. | ||
| + | |||
| + | Jusque-là, amusez-vous bien ! | ||
issue114/python.1478075786.txt.gz · Dernière modification : 2016/11/02 09:36 de auntiee
