Comme dirait l'autre j'suis pas faignant mais partisan du moindre effort

C'est quoi une class python ?

Modérateurs : Francois, Manfraid
Code : Tout sélectionner
#!/usr/bin/env python
#import global
import RPi.GPIO as GPIO
from time import sleep,time
#import pour comportement proche de python3
from __future__ import (unicode_literals, absolute_import,
print_function, division)
class MotorPaP(object):
""" class pour la gestion d'un moteur pas a pas
"""
seq = [[GPIO.HIGHT,GPIO.LOW,GPIO.LOW,GPIO.LOW],
[GPIO.HIGHT,GPIO.HIGHT,GPIO.LOW,GPIO.LOW],
[GPIO.LOW,GPIO.HIGHT,GPIO.LOW,GPIO.LOW],
[GPIO.LOW,GPIO.HIGHT,GPIO.HIGHT,GPIO.LOW],
[GPIO.LOW,GPIO.LOW,GPIO.HIGHT,GPIO.LOW],
[GPIO.LOW,GPIO.LOW,GPIO.HIGHT,GPIO.HIGHT],
[GPIO.LOW,GPIO.LOW,GPIO.LOW,GPIO.HIGHT],
[GPIO.HIGHT,GPIO.LOW,GPIO.LOW,GPIO.HIGHT]]
debug = False
def __init__(self,pins=[17,22,23,24],revolution=4096,rpm=10):
"""initialisation de la class avec les pins pour le moteur"""
self.pins = pins
self.rpm = rpm
self.revolution = revolution
#remise a zero des sortie
self.InitOutput()
#temp de pause entre chaque pas
self.pause = 60/(revolution*rpm)
def InitOutput(self):
for pin in self.pins:
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
def ClearOutput(self):
for pin in self.pins:
GPIO.output(pin, GPIO.LOW)
GPIO.Cleanup()
def set_all_pins_low(self):
for pin in self.pins:
GPIO.output(pin, GPIO.LOW)
def log(self,msg):
if self.debug:
print(msg)
def _setstep(self,pins):
for i in range(len(self.pins)):
GPIO.output(self.pins[i],pins[i])
def move(self,nbstep,sens=1):
if sens < 0:
self.seq.reverse()
for i in range(nbstep):
p = self.seq.pop(0)
self._setstep(p)
sleep(self.pause)
self.seq.append(p)
if sens < 0:
self.seq.reverse()
Code : Tout sélectionner
StepPins = [17,22,23,24]
motor = MotorPaP(StepPins)
sequence = [300]*50 # avance de 300pas pour 50 arret
for s in sequence:
motor.move(s)
call("raspistill -o myimage_%04d.jpg -tl 3000 -t 22500 -w 800 -h 600", shell=True)
Code : Tout sélectionner
#!/usr/bin/env python
#import global
import RPi.GPIO as GPIO
from time import sleep,time
from subprocess import call
#import pour comportement proche de python3
from __future__ import (unicode_literals, absolute_import,
print_function, division)
class MotorPaP(object):
""" class pour la gestion d'un moteur pas a pas
"""
seq = [[GPIO.HIGHT,GPIO.LOW,GPIO.LOW,GPIO.LOW],
[GPIO.HIGHT,GPIO.HIGHT,GPIO.LOW,GPIO.LOW],
[GPIO.LOW,GPIO.HIGHT,GPIO.LOW,GPIO.LOW],
[GPIO.LOW,GPIO.HIGHT,GPIO.HIGHT,GPIO.LOW],
[GPIO.LOW,GPIO.LOW,GPIO.HIGHT,GPIO.LOW],
[GPIO.LOW,GPIO.LOW,GPIO.HIGHT,GPIO.HIGHT],
[GPIO.LOW,GPIO.LOW,GPIO.LOW,GPIO.HIGHT],
[GPIO.HIGHT,GPIO.LOW,GPIO.LOW,GPIO.HIGHT]]
debug = False
def __init__(self,pins=[17,22,23,24],revolution=4096,rpm=10):
"""initialisation de la class avec les pins pour le moteur"""
self.pins = pins
self.rpm = rpm
self.revolution = revolution
#remise a zero des sortie
self.InitOutput()
#temp de pause entre chaque pas
self.pause = 60/(revolution*rpm)
def InitOutput(self):
for pin in self.pins:
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
def ClearOutput(self):
for pin in self.pins:
GPIO.output(pin, GPIO.LOW)
GPIO.Cleanup()
def set_all_pins_low(self):
for pin in self.pins:
GPIO.output(pin, GPIO.LOW)
def log(self,msg):
if self.debug:
print(msg)
def _setstep(self,pins):
for i in range(len(self.pins)):
GPIO.output(self.pins[i],pins[i])
def move(self,nbstep,sens=1):
if sens < 0:
self.seq.reverse()
for i in range(nbstep):
p = self.seq.pop(0)
self._setstep(p)
sleep(self.pause)
self.seq.append(p)
if sens < 0:
self.seq.reverse()
StepPins = [17,22,23,24]
motor = MotorPaP(StepPins)
sequence = [300]*50 # avance de 300pas pour 50 arret
for s in sequence:
motor.move(s)
call("raspistill -o myimage_%04d.jpg -tl 3000 -t 22500 -w 800 -h 600", shell=True)
Et faut pas faire une class pour la picam ?Manfraid a écrit :je te conseille de lire ceci c'est un excellent moyen d'apprendre python, et j'y vais encore cela me sert aussi de pense bête
alors en gros un class c'est un objet, par exemple dans ton projet tu pourrais avoir une class moteur, est c'est celle si qui s'occupera de faire bouger ton moteur, et dans le programme principale du utilisera cette class juste pour dire au moteur fait 100 pas a droite ou 200 a gauche (c'est un exemple)
Ça marche pas...Manfraid a écrit :Salut,
voici le code complet que je te propose par contre pas sur non plus qu'il fonctionneCode : Tout sélectionner
#!/usr/bin/env python #import global import RPi.GPIO as GPIO from time import sleep,time from subprocess import call #import pour comportement proche de python3 from __future__ import (unicode_literals, absolute_import, print_function, division) class MotorPaP(object): """ class pour la gestion d'un moteur pas a pas """ seq = [[GPIO.HIGHT,GPIO.LOW,GPIO.LOW,GPIO.LOW], [GPIO.HIGHT,GPIO.HIGHT,GPIO.LOW,GPIO.LOW], [GPIO.LOW,GPIO.HIGHT,GPIO.LOW,GPIO.LOW], [GPIO.LOW,GPIO.HIGHT,GPIO.HIGHT,GPIO.LOW], [GPIO.LOW,GPIO.LOW,GPIO.HIGHT,GPIO.LOW], [GPIO.LOW,GPIO.LOW,GPIO.HIGHT,GPIO.HIGHT], [GPIO.LOW,GPIO.LOW,GPIO.LOW,GPIO.HIGHT], [GPIO.HIGHT,GPIO.LOW,GPIO.LOW,GPIO.HIGHT]] debug = False def __init__(self,pins=[17,22,23,24],revolution=4096,rpm=10): """initialisation de la class avec les pins pour le moteur""" self.pins = pins self.rpm = rpm self.revolution = revolution #remise a zero des sortie self.InitOutput() #temp de pause entre chaque pas self.pause = 60/(revolution*rpm) def InitOutput(self): for pin in self.pins: GPIO.setup(pin,GPIO.OUT) GPIO.output(pin, GPIO.LOW) def ClearOutput(self): for pin in self.pins: GPIO.output(pin, GPIO.LOW) GPIO.Cleanup() def set_all_pins_low(self): for pin in self.pins: GPIO.output(pin, GPIO.LOW) def log(self,msg): if self.debug: print(msg) def _setstep(self,pins): for i in range(len(self.pins)): GPIO.output(self.pins[i],pins[i]) def move(self,nbstep,sens=1): if sens < 0: self.seq.reverse() for i in range(nbstep): p = self.seq.pop(0) self._setstep(p) sleep(self.pause) self.seq.append(p) if sens < 0: self.seq.reverse() StepPins = [17,22,23,24] motor = MotorPaP(StepPins) sequence = [300]*50 # avance de 300pas pour 50 arret for s in sequence: motor.move(s) call("raspistill -o myimage_%04d.jpg -tl 3000 -t 22500 -w 800 -h 600", shell=True)
Code : Tout sélectionner
seq = [[GPIO.HIGHT,GPIO.LOW,GPIO.LOW,GPIO.LOW],
[GPIO.HIGHT,GPIO.HIGHT,GPIO.LOW,GPIO.LOW],
[GPIO.LOW,GPIO.HIGHT,GPIO.LOW,GPIO.LOW],
[GPIO.LOW,GPIO.HIGHT,GPIO.HIGHT,GPIO.LOW],
[GPIO.LOW,GPIO.LOW,GPIO.HIGHT,GPIO.LOW],
[GPIO.LOW,GPIO.LOW,GPIO.HIGHT,GPIO.HIGHT],
[GPIO.LOW,GPIO.LOW,GPIO.LOW,GPIO.HIGHT],
[GPIO.HIGHT,GPIO.LOW,GPIO.LOW,GPIO.HIGHT]]
Code : Tout sélectionner
seq = [[GPIO.HIGH,GPIO.LOW,GPIO.LOW,GPIO.LOW],
[GPIO.HIGH,GPIO.HIGH,GPIO.LOW,GPIO.LOW],
[GPIO.LOW,GPIO.HIGH,GPIO.LOW,GPIO.LOW],
[GPIO.LOW,GPIO.HIGH,GPIO.HIGH,GPIO.LOW],
[GPIO.LOW,GPIO.LOW,GPIO.HIGH,GPIO.LOW],
[GPIO.LOW,GPIO.LOW,GPIO.HIGH,GPIO.HIGH],
[GPIO.LOW,GPIO.LOW,GPIO.LOW,GPIO.HIGH],
[GPIO.HIGH,GPIO.LOW,GPIO.LOW,GPIO.HIGH]]