Outils pour utilisateurs

Outils du site


issue119:python

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? 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.

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. If you are going to test Firmata/Arduino on the Raspberry Pi, you should download a version of Firmata_Test program that is compiled for the RPi from: https://github.com/freetronics/PiLeven/wiki/Direct-Control-with-Firmata, otherwise, you can download the Firmata_Test from firmata.org/wiki/Main_Page#Firmata_Test_Program. I saved it to my RPi desktop, but you can save it anywhere you want. Be sure to set the permissions to allow execution, since it’s a .bin file. Then run it ./firmata_test 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.

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 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 pyfirmata

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. Basically, we simply connect +5VDC from the Arduino through the pushbutton to Digital pin #2. Alternately, if you don’t have a small pushbutton available, you can momentarily connect a jumper between the +5VDC pin and Digital pin #2. 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.

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 while True: print s.readline() As you press the button and release it, you should see the output in the terminal change from 0 to 1 and 1 to 0. To quit the program, use <Ctrl> + C. Now you have written your first program to control the Arduino from Python.

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. Below is the Python code. I’ve named it “analogue_test1.py”. import pyfirmata PORT = “/dev/ttyACM0” # Change this to suit your setup board = pyfirmata.Arduino(PORT) In the first three lines, we import the pyfirmata library, assign the serial port and create an instance of the “board”. 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.

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.start() Now we enable the 4 analogue pins for reporting so we can read the value. We also set up digital pin 0 as an output pin (to be able to drive the LED). for pin in PINS: board.analog[pin].enable_reporting() pin2 = board.get_pin('d:2:o') 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()

board.pass_time(1)

Now we start a forever loop… while True: val = board.analog[0].read() print val if val >= .50: pin2.write(1) else: pin2.write(0) Within the loop, we read the analogue pin (remember the value will be between 0.0 and 0.9) and if it is at or greater than 0.5, then we write a 1 to the digital output pin #2, turning on the LED. Once it is under .50, then we write a 0 to the pin, turning off the LED again. 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.

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. 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.

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.txt · Dernière modification : 2017/04/12 10:52 de andre_domenech