issue119:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue119:python [2017/04/03 18:58] – créée auntiee | issue119:python [2017/04/12 10:52] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | This time, we will start controlling the Arduino with the Raspberry Pi. To do this, we will be using the Firmata library and protocol. | + | **This time, we will start controlling the Arduino with the Raspberry Pi. To do this, we will be using the Firmata library and protocol. |
What the heck is Firmata? | What the heck is Firmata? | ||
Ligne 5: | Ligne 5: | ||
Firmata is a serial communications protocol long used for communications between microcontrollers and programs on another computer. Firmata gives access directly to the Arduino board. You can communicate through the serial port of the " | Firmata is a serial communications protocol long used for communications between microcontrollers and programs on another computer. Firmata gives access directly to the Arduino board. You can communicate through the serial port of the " | ||
- | By using the " | + | By using the " |
- | Getting Started | + | Aujourd' |
+ | |||
+ | Qu' | ||
+ | |||
+ | Firmata est un protocole de communication série utilisé depuis longtemps pour les communications entre des micro-contrôleurs et les programmes sur un autre ordinateur. Firmata donne un accès direct à la carte Arduino. Vous pouvez communiquer par le port série de l' | ||
+ | |||
+ | En utilisant le script « Standard Firmata » inclus dans les exemples Arduino, vous pouvez avoir accès à, et piloter, n' | ||
+ | |||
+ | **Getting Started | ||
Load the " | Load the " | ||
Ligne 19: | Ligne 27: | ||
Set the port to the same port your Arduino is. In my case, it’s on / | Set the port to the same port your Arduino is. In my case, it’s on / | ||
- | Click on the button marked " | + | Click on the button marked " |
- | Going Further | + | Commençons |
+ | |||
+ | Chargez le script « StandardFirmata » à partir des exemples Arduino (Fichiers|Exemples|FIrmata|StandardFirmata), | ||
+ | |||
+ | Si vous testez Firmata/ | ||
+ | |||
+ | Assurez-vous de paramétrer les permissions pour qu'il puisse être exécuté, car c'est un fichier .bin. Puis, lancez-le. | ||
+ | |||
+ | ./ | ||
+ | |||
+ | Réglez le port sur le même port que celui de l' | ||
+ | |||
+ | Cliquez sur le bouton marqué « Low » pour la broche 13. Il devrait changer pour « High » et la LED de la carte Arduino devrait s' | ||
+ | |||
+ | **Going Further | ||
For now, most anything you want to do in Python will work with the " | For now, most anything you want to do in Python will work with the " | ||
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 31: | 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. | ||
Ligne 39: | Ligne 71: | ||
On the Arduino, compile DigitalRealSerial example file from the File|Examples|01 Basics menu. | On the Arduino, compile DigitalRealSerial example file from the File|Examples|01 Basics menu. | ||
- | 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. | + | 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.** |
- | import serial | + | Utiliser pySerial |
+ | |||
+ | Notre premier exemple Python nécessitera un bouton-poussoir temporaire connecté à la broche 2 de l' | ||
+ | |||
+ | De base, nous connectons simplement le +5 VDC de l' | ||
+ | |||
+ | Sur l' | ||
+ | |||
+ | Sur votre ordinateur, vous pouvez soit essayer simplement d' | ||
+ | |||
+ | **import serial | ||
s = serial.Serial('/ | s = serial.Serial('/ | ||
Ligne 53: | Ligne 95: | ||
To quit the program, use < | To quit the program, use < | ||
- | 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.** |
- | 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. | + | 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. | ||
Using the StandardFirmata Arduino code again, start up the Arduino board. | Using the StandardFirmata Arduino code again, start up the Arduino board. | ||
Ligne 71: | Ligne 126: | ||
PINS = (0, 1, 2, 3) | PINS = (0, 1, 2, 3) | ||
- | 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.** |
- | The next two lines create an iterator to handle the serial communications for the analogue ports. This is recommended in the pyfirmata documentation. | + | 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. | ||
it = pyfirmata.util.Iterator(board) | it = pyfirmata.util.Iterator(board) | ||
Ligne 87: | Ligne 160: | ||
The next two lines will read analogue pin 0 and then wait one second. This allows for the board to settle before we start the loop. | The next two lines will read analogue pin 0 and then wait one second. This allows for the board to settle before we start the loop. | ||
+ | |||
+ | val = board.analog[0].read() | ||
+ | |||
+ | 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() | val = board.analog[0].read() | ||
Ligne 92: | Ligne 184: | ||
board.pass_time(1) | board.pass_time(1) | ||
- | Now we start a forever loop… | + | **Now we start a forever loop… |
while True: | while True: | ||
Ligne 109: | Ligne 201: | ||
board.pass_time(1) | board.pass_time(1) | ||
- | 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.** |
- | The Python source code is available on pastebin at http:// | + | 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:// | ||
Now that you have the basic idea, we can move forward. | Now that you have the basic idea, we can move forward. | ||
Ligne 117: | Ligne 228: | ||
You might have realized that this solution will work only as long as we have a direct serial connection to the Arduino board. What if, however, we need to have a wireless connection to the Arduino? We’ll save that discussion for another time. | You might have realized that this solution will work only as long as we have a direct serial connection to the Arduino board. What if, however, we need to have a wireless connection to the Arduino? We’ll save that discussion for another time. | ||
- | 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.1491238689.txt.gz · Dernière modification : 2017/04/03 18:58 de auntiee