Outils pour utilisateurs

Outils du site


issue119:python

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
issue119:python [2017/04/03 18:58] – créée auntieeissue119: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 "host" computer using just about any serial language, including Python. It is based on the MIDI spec. 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 "host" computer using just about any serial language, including Python. It is based on the MIDI spec.
  
-By using the "Standard Firmata" script included in the Arduino examples, you can access and control any of the digital and analogue pins on the Arduino board, without having to write any custom code. If you want, you can write specialized code incorporating the Firmata library for the Arduino that does specialized functions.+By using the "Standard Firmata" script included in the Arduino examples, you can access and control any of the digital and analogue pins on the Arduino board, without having to write any custom code. If you want, you can write specialized code incorporating the Firmata library for the Arduino that does specialized functions.**
  
-Getting Started+Aujourd'hui, nous commençons le pilotage de l'Arduino avec le Raspberry Pi. Pour le faire, nous utiliserons la bibliothèque et le protocole Firmata. 
 + 
 +Qu'est-ce donc que Firmata ? 
 + 
 +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'ordinateur « hôte » en utilisant à peu près n'importe quel langage série, dont Python. Il est basé sur la spécif. MIDI. 
 + 
 +En utilisant le script « Standard Firmata » inclus dans les exemples Arduino, vous pouvez avoir accès à, et piloter, n'importe quelle connexion analogique ou numérique de la carte Arduino, sans avoir à écrire du code spécifique. Si vous le voulez, vous pouvez écrire du code spécialisé en incorporant la bibliothèque Firmata pour l'Arduino qui réalise des fonctions spécialisées. 
 + 
 +**Getting Started
  
 Load the "StandardFirmata" script from the Arduino examples (File|Examples|FIrmata|StandardFirmata), compile and send it to the Arduino. Load the "StandardFirmata" script from the Arduino examples (File|Examples|FIrmata|StandardFirmata), compile and send it to the Arduino.
Ligne 19: Ligne 27:
 Set the port to the same port your Arduino is. In my case, it’s on /dev/ttyACM0. Then after a few seconds, the program will show the 13 digital ports and the 5 analogue ports. Set the port to the same port your Arduino is. In my case, it’s on /dev/ttyACM0. Then after a few seconds, the program will show the 13 digital ports and the 5 analogue ports.
  
-Click on the button marked "Low" for pin 13. This should change to "High" and the LED on the arduino board should light up. Click it again and it should turn back to "Low" and the LED should go out.+Click on the button marked "Low" for pin 13. This should change to "High" and the LED on the arduino board should light up. Click it again and it should turn back to "Low" and the LED should go out.**
  
-Going Further+Commençons 
 + 
 +Chargez le script « StandardFirmata » à partir des exemples Arduino (Fichiers|Exemples|FIrmata|StandardFirmata), compilez-le et envoyez-le sur l'Arduino. 
 + 
 +Si vous testez Firmata/Arduino sur le Raspberry Pi, vous devrez télécharger une version du programme Firmata Test qui est compilée pour le RPi sur : https://github.com/freetronics/PiLeven/wiki/Direct-Control-with-Firmata ; autrement, vous pouvez télécharger Firmata Test depuis firmata.org/wiki/Main_Page#Firmata_Test_Program. Je l'ai sauvegardé sur le bureau de mon RPi, mais vous pouvez le sauvegarder où vous voulez. 
 + 
 +Assurez-vous de paramétrer les permissions pour qu'il puisse être exécuté, car c'est un fichier .bin. Puis, lancez-le. 
 + 
 +./firmata_test 
 + 
 +Réglez le port sur le même port que celui de l'Arduino. Dans mon cas, c'est sur /dev/ttyACM0. Ensuite, après quelques secondes, le programme affichera les 13 ports digitaux et les 5 ports analogiques. 
 + 
 +Cliquez sur le bouton marqué « Low » pour la broche 13. Il devrait changer pour « High » et la LED de la carte Arduino devrait s'allumer. Cliquez à nouveau : il devrait revenir à « Low », avec extinction de la LED. 
 + 
 +**Going Further
  
 For now, most anything you want to do in Python will work with the "Standard" Firmata code uploaded to the Arduino by using the pyFirmata Python library. However, you can write your own Firmata code on the Arduino. For now, most anything you want to do in Python will work with the "Standard" Firmata code uploaded to the Arduino by using the pyFirmata Python library. However, you can write your own Firmata code on the Arduino.
  
 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'Arduino en utilisant la bibliothèque Python pyFirmata. Cependant, vous pouvez écrire votre propre code Firmata sur l'Arduino.
 +
 +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'Arduino. La disposition sur la plaque d'essai en Fritzing est ci-dessous. 
 + 
 +De base, nous connectons simplement le +5 VDC de l'Arduino à la borne digitale n° 2 de l'Arduino en passant par le bouton-poussoir. À la place, si vous n'avez pas de petit bouton-poussoir disponible, vous pouvez faire une connexion brève avec un cavalier entre la broche +5 VDC et la borne digitale n° 2. 
 + 
 +Sur l'Arduino, compilez le fichier exemple DigitalRealSerial tiré du menu Fichiers|Exemple|01 Bases. 
 + 
 +Sur votre ordinateur, vous pouvez soit essayer simplement d'utiliser l'interface du terminal Python, soit entrer le code suivant dans un IDE comme Geany et le lancer dans un terminal. 
 + 
 +**import serial
  
 s = serial.Serial('/dev/ttyACM0',9600) # You might need to change this to ACM1 or whatever your Arduino is connected to s = serial.Serial('/dev/ttyACM0',9600) # You might need to change this to ACM1 or whatever your Arduino is connected to
Ligne 53: Ligne 95:
 To quit the program, use <Ctrl> + C. To quit the program, use <Ctrl> + C.
  
-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('/dev/ttyACM0',9600) # Vous devrez peut-être changer ceci à ACM1 ou à un autre endroit auquel votre Arduino est connecté. 
 +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 <Ctrl> + C. 
 + 
 +Vous venez d'écrire votre premier programme en Python pour piloter l'Arduino. 
 + 
 +**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'entrée analogique A0 et, si la tension est au-dessus d'une certaine valeur (0,50), nous allumerons la LED connectée à la sortie digitale n° 2. La plaque d'essai en Fritzing est ci-dessous. 
 + 
 +En utilisant à nouveau le code StandardFirmata de l'Arduino, démarrez la carte Arduino. 
 + 
 +Le code Python est ci-dessous. Je l'ai nommé « analogue_test1.py ». 
 + 
 +import pyfirmata 
 + 
 +PORT = "/dev/ttyACM0" # Modifiez ceci pour refléter votre configuration. 
 + 
 +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, même si nous n'allons en utiliser que la broche 0 dans cet exemple. 
 + 
 +**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, ce qui nous permet de lire la valeur. Nous paramétrons aussi la broche digitale 0 en sortie (pour être capable de piloter la LED).
 +
 +for pin in PINS:
 +    board.analog[pin].enable_reporting()
 +
 +pin2 = board.get_pin('d:2:o')
 +
 +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://pastebin.com/xG9VJ34i+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'elle est repassée sous 0,50, nous écrivons un 0 sur la borne, et éteignons la LED. 
 + 
 + board.pass_time(1) 
 + 
 +La commande board.pass_time prend, sous forme d'entier, un nombre t de secondes entières. C'est un appel non bloquant ; aussi, il ne bloque pas d'autre traitement. 
 + 
 +**The Python source code is available on pastebin at http://pastebin.com/xG9VJ34i
  
 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://pastebin.com/xG9VJ34i 
 + 
 +Maintenant que vous avez l'idée de départ, nous pouvons aller de l'avant. 
 + 
 +Vous aurez peut-être réalisé que cette solution ne marche qu'aussi longtemps que nous avons une communication série directe avec la carte Arduino. Cependant, que se passe-t-il si nous devons utiliser une connexion sans fil avec l'Arduino ? Nous garderons ce sujet pour une autre fois. 
 + 
 +Amusez-vous bien !
issue119/python.1491238689.txt.gz · Dernière modification : 2017/04/03 18:58 de auntiee