issue119: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 | ||
issue119:python [2017/04/08 13:32] – d52fr | issue119:python [2017/04/12 10:52] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 11: | Ligne 11: | ||
Qu' | Qu' | ||
- | Firmata est un protocole de communication série utilisé depuis longtemps pour les communications entre des microcontrolleurs | + | Firmata est un protocole de communication série utilisé depuis longtemps pour les communications entre des micro-contrôleurs |
- | En utilisant le script « Standard Firmata » inclus dans les exemples Arduino, vous pouvez avoir accès et piloter n' | + | En utilisant le script « Standard Firmata » inclus dans les exemples Arduino, vous pouvez avoir accès |
**Getting Started | **Getting Started | ||
Ligne 31: | Ligne 31: | ||
Commençons | Commençons | ||
- | Chargez le script « StandardFirmata » à partir des exemples Arduino (Fichiers|Exemples|FIrmata|StandardFirmata), | + | Chargez le script « StandardFirmata » à partir des exemples Arduino (Fichiers|Exemples|FIrmata|StandardFirmata), |
- | Si vous testez Firmata/ | + | Si vous testez Firmata/ |
- | Assurez-vous | + | Assurez-vous |
./ | ./ | ||
- | Réglez le port sur le même port que celui de l' | + | Réglez le port sur le même port que celui de l' |
- | Cliquez sur le bouton marqué « Low » pour le picot 13. Il devrait changer pour « High » et la LED de la carte Arduino devrait s' | + | Cliquez sur le bouton marqué « Low » pour la broche |
**Going Further | **Going Further | ||
Ligne 48: | Ligne 48: | ||
For the Python side of things, we will need to install two libraries (if they aren't already installed). The first is pySerial and the second is pyFirmata. You can use pip to install both. | For the Python side of things, we will need to install two libraries (if they aren't already installed). The first is pySerial and the second is pyFirmata. You can use pip to install both. | ||
+ | |||
+ | sudo pip install pyserial | ||
+ | |||
+ | sudo pip install pyfirmata** | ||
+ | |||
+ | Approfondissons | ||
+ | |||
+ | En ce moment, la plupart de ce que vous voulez faire dans Python marchera avec le code « StandardFormata » téléversé dans l' | ||
+ | |||
+ | Pour le côté Python des choses, nous devrons installer deux bibliothèques (si elles ne sont pas déjà installées). La première est pySerial et la seconde pyFormata. Vous pouvez utiliser pip pour installer les deux. | ||
sudo pip install pyserial | sudo pip install pyserial | ||
Ligne 53: | Ligne 63: | ||
sudo pip install pyfirmata | sudo pip install pyfirmata | ||
- | Using pySerial | + | **Using pySerial |
Our first Python example will require a momentary push-button connected to pin 2 of the Arduino. Below is the Fritzing breadboard layout. | Our first Python example will require a momentary push-button connected to pin 2 of the Arduino. Below is the Fritzing breadboard layout. | ||
- | Basically, we simply connect +5VDC from the Arduino through the pushbutton to Digital pin #2. Alternately, | + | Basically, we simply connect +5VDC from the Arduino through the pushbutton to Digital pin #2. Alternately, |
- | Approfondissons | + | On the Arduino, compile DigitalRealSerial example file from the File|Examples|01 Basics menu. |
- | En ce moment, la plupart de ce que vous voulez faire dans Python | + | On your computer, you can either simply use the Python |
- | Pour le côté Python des choses, nous devrons installer deux bibliothèques (si elles ne sont pas déjà installées). La première est pySerial | + | Utiliser |
- | **On the Arduino, compile DigitalRealSerial example file from the File|Examples|01 Basics menu. | + | Notre premier exemple Python nécessitera un bouton-poussoir temporaire connecté à la broche 2 de l'Arduino. La disposition sur la plaque d' |
- | On your computer, you can either simply use the Python Terminal Interface or enter the following code into an IDE like Geany and run it in a terminal. | + | De base, nous connectons simplement le +5 VDC de l' |
- | import serial | + | Sur l' |
+ | |||
+ | Sur votre ordinateur, vous pouvez soit essayer simplement d' | ||
+ | |||
+ | **import serial | ||
s = serial.Serial('/ | s = serial.Serial('/ | ||
Ligne 82: | Ligne 96: | ||
Now you have written your first program to control the Arduino from Python.** | Now you have written your first program to control the Arduino from Python.** | ||
+ | |||
+ | import serial | ||
+ | |||
+ | s = serial.Serial('/ | ||
+ | while True: | ||
+ | |||
+ | print s.readline() | ||
+ | |||
+ | Si vous appuyez sur, puis relâchez, le bouton, vous devriez voir la sortie sur le terminal passer de 0 à 1 puis de 1 à 0. | ||
+ | |||
+ | Pour quitter le programme, utilisez < | ||
+ | |||
+ | Vous venez d' | ||
**Now we’ll do something a bit more complicated. We’ll “monitor” an analogue voltage (controlled by a potentiometer) on analogue input A0 and if the voltage is over a certain value (.50) we’ll turn on a LED connected to digital pin #2. Below is the Fritzing breadboard. | **Now we’ll do something a bit more complicated. We’ll “monitor” an analogue voltage (controlled by a potentiometer) on analogue input A0 and if the voltage is over a certain value (.50) we’ll turn on a LED connected to digital pin #2. Below is the Fritzing breadboard. | ||
Ligne 100: | Ligne 127: | ||
Here we are setting up for 4 analogue pins to be handled, even though we are only going to use pin 0 for this example.** | Here we are setting up for 4 analogue pins to be handled, even though we are only going to use pin 0 for this example.** | ||
+ | |||
+ | Maintenant, nous allons faire quelque chose d'un peu plus compliqué. Nous allons « suivre » une tension analogique (contrôlée par un potentiomètre) sur l' | ||
+ | |||
+ | En utilisant à nouveau le code StandardFirmata de l' | ||
+ | |||
+ | Le code Python est ci-dessous. Je l'ai nommé « analogue_test1.py ». | ||
+ | |||
+ | import pyfirmata | ||
+ | |||
+ | PORT = "/ | ||
+ | |||
+ | board = pyfirmata.Arduino(PORT) | ||
+ | |||
+ | Dans les trois premières lignes, nous importons la bibliothèque pyfirmata, assignons le port série et créons une instance de la « carte ». | ||
+ | |||
+ | PINS = (0, 1, 2, 3) | ||
+ | |||
+ | Ici, nous faisons le paramétrage pour pouvoir manipuler 4 broches analogiques, | ||
**The next two lines create an iterator to handle the serial communications for the analogue ports. This is recommended in the pyfirmata documentation. | **The next two lines create an iterator to handle the serial communications for the analogue ports. This is recommended in the pyfirmata documentation. | ||
Ligne 119: | Ligne 164: | ||
board.pass_time(1)** | board.pass_time(1)** | ||
+ | |||
+ | Les deux prochaines lignes créeront un itérateur pour gérer les communications série pour les ports analogiques. Ceci est recommandé dans la documentation de pyfirmata. | ||
+ | |||
+ | it = pyfirmata.util.Iterator(board) | ||
+ | |||
+ | it.start() | ||
+ | |||
+ | Maintenant, nous activons le suivi des 4 bornes analogiques, | ||
+ | |||
+ | for pin in PINS: | ||
+ | board.analog[pin].enable_reporting() | ||
+ | |||
+ | pin2 = board.get_pin(' | ||
+ | |||
+ | Les deux prochaines lignes liront la borne analogique 0 puis attendront une seconde. Ceci permet à la carte de prendre un état stable avant de débuter la boucle. | ||
+ | |||
+ | val = board.analog[0].read() | ||
+ | |||
+ | board.pass_time(1) | ||
**Now we start a forever loop… | **Now we start a forever loop… | ||
Ligne 138: | Ligne 202: | ||
The board.pass_time command takes an integer of t number of whole seconds. It is a non-blocking call, so it does not block other processing.** | The board.pass_time command takes an integer of t number of whole seconds. It is a non-blocking call, so it does not block other processing.** | ||
+ | |||
+ | Maintenant, nous démarrons une boucle sans fin : | ||
+ | |||
+ | while True: | ||
+ | |||
+ | val = board.analog[0].read() | ||
+ | |||
+ | print val | ||
+ | |||
+ | if val >= .50: | ||
+ | pin2.write(1) | ||
+ | else: | ||
+ | pin2.write(0) | ||
+ | |||
+ | Dans cette boucle, nous lisons la borne analogique (souvenez-vous que la valeur sera entre 0,0 et 0,9) et, si elle est égale ou supérieure à 0,5, nous écrirons alors un 1 vers la sortie digitale de la broche n° 2, allumant la LED. Une fois qu' | ||
+ | |||
+ | board.pass_time(1) | ||
+ | |||
+ | La commande board.pass_time prend, sous forme d' | ||
**The Python source code is available on pastebin at http:// | **The Python source code is available on pastebin at http:// | ||
Ligne 146: | Ligne 229: | ||
Enjoy.** | Enjoy.** | ||
+ | |||
+ | Le code Python est disponible sur pastebin à http:// | ||
+ | |||
+ | Maintenant que vous avez l' | ||
+ | |||
+ | Vous aurez peut-être réalisé que cette solution ne marche qu' | ||
+ | |||
+ | Amusez-vous bien ! |
issue119/python.1491651121.txt.gz · Dernière modification : 2017/04/08 13:32 de d52fr