pigpio sur Raspberry pi

introduction

Site officiel

Installation

python


$ sudo apt install pigpio python-pigpio python3-pigpio

démarrer pigpiod

python


$ sudo pigpiod

démarrage automatique du pigpio

python


$ sudo systemctl enable pigpiod.service
$ sudo shutdown -r now

#Confirmation après le redémarrage
$ sudo systemctl status pigpiod.service

Tout d'abord, L Chika

led_pigpio.py


from time import sleep
import pigpio

pig = pigpio.pi()
pig.set_mode(21, pigpio.OUTPUT)

try:
    while True:
        sleep(1)
        pig.write(21, 1)
        sleep(1)
        pig.write(21, 0)
except KeyboardInterrupt:
    pig.stop()

Traitement des événements

sw_pd_event.py


from time import sleep
import pigpio

pig = pigpio.pi()
pig.set_mode(4, pigpio.INPUT)
pig.set_pull_up_down(4, pigpio.PUD_DOWN)

def cbf(gpio, level, tick):
    print(gpio, level, tick)

cb = pig.callback(4, pigpio.RISING_EDGE, cbf)

try:
    while True:
        sleep(0.01)
except KeyboardInterrupt:
    pig.stop()

affichage LCD

lcd_pigpio.y


from time import sleep

import pigpio
from RPLCD.pigpio import CharLCD

pi = pigpio.pi()

lcd = CharLCD(pi, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23], pin_contrast=9)
lcd.write_string('Hello RasPi')
sleep(3)
lcd.clear()
lcd.write_string('Test LCD pigpio')
sleep(3)
lcd.close(clear=True)
pi.stop()

i2c: module capteur 9 axes (BMX055)

i2c_pigpio.py


from time import sleep

import pigpio
from BMX055 import bmx055

bmx = bmx055()
bmx.setup()

try:
    while True:
        ax, ay, az = bmx.read_accel()
#        print("accel: ax={0}, ay={1}, az={2}".format(ax, ay, az))

        gx, gy, gz = bmx.read_gyro()
#        print("gyro: gx={0}, gy={1}, gz={2}".format(gx, gy, gz))

        cx, cy, cz = bmx.read_compass()
#        print("comp: cx={0}, cy={1}, cz={2}".format(cx, cy, cz))

        msg = "|       |  x  |  y  |  z  |\n"
        msg += "| accel | "+str(ax)+" | "+str(ay)+" | "+str(az)+" |\n"
        msg += "| gyro  | "+str(gx)+" | "+str(gy)+" | "+str(gz)+" |\n"
        msg += "| comp  | "+str(cx)+" | "+str(cy)+" | "+str(cz)+" |\n"
        print(msg)
        sleep(0.5)
except KeyboardInterrupt:
    bmx.stop()

BMX055.py


from time import sleep

import pigpio

class bmx055:
    def __init__(self):
        self.pi = pigpio.pi() 
        self.i2c_bus = 1 
        self.accel_addr = 0x19 
        self.accel = None 
        self.comp_addr = 0x13
        self.comp = None 
        self.gyro_addr = 0x69
        self.gyro = None 
        
    def open(self, i2c_addr):    
        return self.pi.i2c_open(self.i2c_bus, i2c_addr)

    def close(self, handle):
        self.pi.i2c_close(handle)

    def stop(self):
        self.close(self.accel)
        self.close(self.comp)
        self.close(self.gyro)
        self.pi.stop()

    def setup(self):
        self.accel = self.open(self.accel_addr) 
        self.pi.i2c_write_byte_data(self.accel, 0x0f, 0x03)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.accel, 0x10, 0x08)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.accel, 0x11, 0x00)
        sleep(0.1)
        
        self.gyro = self.open(self.gyro_addr) 
        self.pi.i2c_write_byte_data(self.gyro, 0x0f, 0x04)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.gyro, 0x10, 0x07)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.gyro, 0x11, 0x00)
        sleep(0.1)

        self.comp = self.open(self.comp_addr) 
        self.pi.i2c_write_byte_data(self.comp, 0x4b, 0x83)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.comp, 0x4b, 0x01)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.comp, 0x4c, 0x00)
        self.pi.i2c_write_byte_data(self.comp, 0x4e, 0x84)
        self.pi.i2c_write_byte_data(self.comp, 0x51, 0x04)
        self.pi.i2c_write_byte_data(self.comp, 0x52, 0x16)

        sleep(0.3)

    def read_accel(self):
        x_l = self.pi.i2c_read_byte_data(self.accel, 0x02)
        x_m = self.pi.i2c_read_byte_data(self.accel, 0x03)
        y_l = self.pi.i2c_read_byte_data(self.accel, 0x04)
        y_m = self.pi.i2c_read_byte_data(self.accel, 0x05)
        z_l = self.pi.i2c_read_byte_data(self.accel, 0x06)
        z_m = self.pi.i2c_read_byte_data(self.accel, 0x07)

        x = ((x_m*256) + (x_l&0xf0))/16
        if (x > 2047):
            x -= 4096
        y = ((y_m*256) + (y_l&0xf0))/16
        if (y > 2047):
            y -= 4096
        z = ((x_m*256) + (z_l&0xf0))/16
        if (z > 2047):
            z -= 4096

        return (x, y, z)


    def read_compass(self):
        x_l = self.pi.i2c_read_byte_data(self.comp, 0x42)
        x_m = self.pi.i2c_read_byte_data(self.comp, 0x43)
        y_l = self.pi.i2c_read_byte_data(self.comp, 0x44)
        y_m = self.pi.i2c_read_byte_data(self.comp, 0x45)
        z_l = self.pi.i2c_read_byte_data(self.comp, 0x46)
        z_m = self.pi.i2c_read_byte_data(self.comp, 0x47)
        t_l = self.pi.i2c_read_byte_data(self.comp, 0x48)
        t_m = self.pi.i2c_read_byte_data(self.comp, 0x49)

        x = (x_m << 8) + (x_l >> 3)
        if (x > 4095):
            x -= 8192
        y = (y_m << 8) + (y_l >> 3)
        if (y > 4095):
            y -= 8192
        z = (x_m << 8) + (z_l >> 3)
        if (z > 16383):
            z -= 32768

        return (x, y, z)

    def read_gyro(self):
        x_l = self.pi.i2c_read_byte_data(self.gyro, 0x02)
        x_m = self.pi.i2c_read_byte_data(self.gyro, 0x03)
        y_l = self.pi.i2c_read_byte_data(self.gyro, 0x04)
        y_m = self.pi.i2c_read_byte_data(self.gyro, 0x05)
        z_l = self.pi.i2c_read_byte_data(self.gyro, 0x06)
        z_m = self.pi.i2c_read_byte_data(self.gyro, 0x07)

        x = (x_m*256) + x_l
        if (x > 32767):
            x -= 65536
        y = (y_m*256) + y_l
        if (y > 32767):
            y -= 65536
        z = (x_m*256) + z_l
        if (z > 32767):
            z -= 65536

        return (x, y, z)

Contrôler le servomoteur (SG92) avec PWM

Tableau des spécifications du servomoteur (SG92)

article valeur Remarques
Angle mobile 180 degrés
la vitesse 0.1s /60 degrés
Tension 4.8 ~ 5.0V
Période d'impulsion 10,000 - 20,000μs
largeur d'impulsion 500 - 2,400μs ※Fiche technique:1,000 - 2,000μs

SG90.py


import pigpio

class SG90:
    """SG90_Spec:
    #Angle mobile:180 degrés
    #la vitesse: 0.1s /60 degrés
    #Tension: 4.8 ~ 5V
    #Période d'impulsion: 10,000 - 20,000μs
    #largeur d'impulsion: 500 - 2,400μs * Fiche technique:1,000 - 2,000μs
    """
    def __init__(self, pin=None):
        self.range_of_motion = 180
        self.min_pulse_width = 500
        self.max_pulse_width = 2400
        self.pig = pigpio.pi()
        self.pin = pin
    
    def move(self, rad):
        if self.pin == None:
            pass
        else:
            spw = (rad/self.range_of_motion) * (self.max_pulse_width-self.min_pulse_width) + self.min_pulse_width
            self.pig.set_servo_pulsewidth(self.pin, spw)

    def stop(self):
        self.pig.set_servo_pulsewidth(self.pin, 0)

en conclusion

Recommended Posts

pigpio sur Raspberry pi
Cython sur Raspberry Pi
Introduction de pyenv sur Raspberry Pi
Utilisez NeoPixel avec la tarte aux framboises
Installez OpenCV4 sur Raspberry Pi 3
Installez TensorFlow 1.15.0 sur Raspberry Pi
MQTT sur Raspberry Pi et Mac
raspberry pi 4 centos7 installer sur docker
Essayez d'utiliser ArUco avec Raspberry Pi
Procédure d'installation d'OpenCV sur Raspberry Pi
Allumer / éteindre le Raspberry pi avec Arduino
Détecter l'état du commutateur avec Raspberry Pi 3
Installez OpenMedia Vault 5 sur Raspberry Pi 4
L Chika avec Raspberry Pi C #
Construisez wxPython sur Ubuntu 20.04 sur Raspberry Pi 4
Raspberry Pi "Lampe de notification Honwaka" Partie 2
Détectez la "luminosité" en utilisant python sur Raspberry Pi 3!
Démarrage USB sur Raspberry Pi 4 modèle B
Raspberry Pi "Lampe de notification Honwaka" Partie 1
Activer la communication série UART + avec Raspberry Pi
Adafruit Python BluefruitLE fonctionne sur Raspeye.
Accélérez l'apprentissage en profondeur avec le processeur Rasperry Pi 4
Définir l'espace d'échange sur Ubuntu sur Raspberry Pi
Programmation normale avec la programmation Node-RED avec Raspberry Pi 3
Utiliser le capteur Grove avec Raspberry Pi
Installez la version 64 bits du système d'exploitation (bate) sur Raspberry Pi
Installez docker-compose sur le système d'exploitation Raspberry Pi 64 bits
Exécutez un servomoteur en utilisant python sur Raspberry Pi 3
Raspberry Pi "Lampe de notification Honwaka" Partie 3
Travailler avec des capteurs dans Mathematica sur Raspberry Pi
Construire un environnement OpenCV-Python sur Raspberry Pi B +
Détectez la température à l'aide de python sur Raspberry Pi 3!
Multiplication matricielle sur GPU Raspberry Pi (partie 2)
Comment installer NumPy sur Raspeye
Travailler avec le GPS en Python pour Raspberry Pi 3
Qu'est-ce que Raspberry Pi?
Pourquoi detectMultiScale () est lent sur Raspberry Pi B +
GPGPU avec Raspberry Pi
Détectez les interrupteurs à glissière à l'aide de python sur Raspberry Pi 3!
Construire un environnement Django sur Raspai (MySQL)
Essayez d'utiliser le code QR avec Raspberry Pi
Détectez les commutateurs magnétiques à l'aide de python sur Raspberry Pi 3!
Caméra vidéo Raspberry Pi
Profitez du travail électronique avec GPIO de Raspberry Pi
Mauvaise connaissance Raspberry Pi
Faisons Raspberry Pi?
Allumez / éteignez votre PC avec Raspberry Pi
Grove - Capteur de température et d'humidité (DHT11) avec Raspberry Pi
Rendre DHT11 disponible avec Raspeye + python (Remarque)
Démarrage de la compilation croisée pour Raspberry Pi Zero sur Ubuntu
Sonnez le buzzer en utilisant python sur Raspberry Pi 3!
Afficher la température du processeur toutes les 5 secondes sur Raspberry Pi 4
DigitalSignage avec Raspberry Pi
Introduction de Ceph avec Kubernetes sur Raspberry Pi 4B (ARM64)
Notes de configuration du Raspberry Pi 4
Connectez-vous à MySQL avec Python sur Raspberry Pi