issue114:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue114:python [2016/11/16 09:24] – d52fr | issue114:python [2016/11/18 12:27] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
The control signals are expected in a very specific “format”, | The control signals are expected in a very specific “format”, | ||
+ | |||
+ | 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 Wiring | ||
Ligne 20: | Ligne 26: | ||
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.** | ||
+ | |||
+ | 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. | **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. | ||
Ligne 43: | Ligne 65: | ||
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.** | ||
+ | |||
+ | 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) | **pwm.ChangeDutyCycle(12) | ||
Ligne 55: | Ligne 100: | ||
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)/ | ||
Ligne 72: | Ligne 129: | ||
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.1479284644.txt.gz · Dernière modification : 2016/11/16 09:24 de d52fr