python : mon compilateur ne reconnait pas une fonction
Répondre à la discussion
Affichage des résultats 1 à 12 sur 12

python : mon compilateur ne reconnait pas une fonction



  1. #1
    docEmmettBrown

    python : mon compilateur ne reconnait pas une fonction


    ------

    bonjour a tous,

    voila mon programme en gros :

    Code:
    class ADS:
              __init_(var):
                        code[...]
    
    while True:
              __init_(var)
    mais mon terminal me renvoie :
    NameError: name '__init_' is not defined

    je pense que le probleme vient que ma fonction est dans une classe mais comment resoudre le probeme

    merci d'avance

    -----

  2. #2
    fred1599

    Re : python : mon compilateur ne reconnait pas une fonction

    __init_ n'est pas une fonction, c'est rien d'ailleurs ! En python une fonction commence par le mot clé def ...

    Je pense que tu voulais faire quelque chose de ce genre

    Code:
    class ADS:
        def __init__(self, var):
            # ajout des attributs de la classe ADS
    Bref, niveau syntaxique, on n'est pas du tout bon ! Va falloir revoir les tutoriels python

  3. #3
    docEmmettBrown

    Re : python : mon compilateur ne reconnait pas une fonction

    mince !!
    oui je précise que dans mon code il y a bien le def, j'ai mal recopié dsl.

    as tu une solution du fait que mon compilateur n'accepte pas ma fonction?

  4. #4
    fred1599

    Re : python : mon compilateur ne reconnait pas une fonction

    Eh bien regarde mon code et le tiens et regarde la différence ! et surtout le nombre de tiret du 8 aux extrémités du init

  5. A voir en vidéo sur Futura
  6. #5
    docEmmettBrown

    Re : python : mon compilateur ne reconnait pas une fonction

    ok, j'ai donc mi :

    Code:
    class ADS:
              def __init__(var):
                        code[...]
    
    while True:
              __init__(var)
    mais ca me met tjs " NameError: name '__init_' is not defined "

  7. #6
    fred1599

    Re : python : mon compilateur ne reconnait pas une fonction

    Houlà, dis nous ce que tu souhaites faire, car je ne vois vraiment ce que cette boucle infinie while True fait là !

    Aussi ton erreur '__init_' prouve que tu n'as pas mis 2 tirets du huit aux extrémités du __init__, mais deux à sa gauche et un seul à sa droite, il faut être rigoureux !

    Il ne faudra pas oublier le self, en paramètre de la méthode __init__, qui représente l'instance de la classe ADS

  8. #7
    docEmmettBrown

    Re : python : mon compilateur ne reconnait pas une fonction

    Ok voila j'ai acheté un convertisseur analogie numérique et pour le faire fonctionner on me propose une bibliotheque deja toute faite, le probleme et que je n'arrive pas a la faire fonctionner

    voici le code complet :
    Code:
    #!/usr/bin/python
    import re
    import smbus
    import Adafruit_I2C
    import time
    # ===========================================================================
    # ADS1x15 Class
    #
    # Originally written by K. Townsend, Adafruit (https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_ADS1x15)
    # Updates and new functions implementation by Pedro Villanueva, 03/2013.
    # The only error in the original code was in line 57:
    #              __ADS1015_REG_CONFIG_DR_920SPS    = 0x0050
    # should be 
    #              __ADS1015_REG_CONFIG_DR_920SPS    = 0x0060     
    #
    # NOT IMPLEMENTED: Conversion ready pin, page 15 datasheet.
    # ===========================================================================
    
    class ADS1x15:
    	i2c = None
    
      # IC Identifiers
    	__IC_ADS1015                      = 0x00
    	__IC_ADS1115                      = 0x01
    
      # Pointer Register
    	__ADS1015_REG_POINTER_MASK        = 0x03
    	__ADS1015_REG_POINTER_CONVERT     = 0x00
    	__ADS1015_REG_POINTER_CONFIG      = 0x01
    	__ADS1015_REG_POINTER_LOWTHRESH   = 0x02
    	__ADS1015_REG_POINTER_HITHRESH    = 0x03
    
      # Config Register
    	__ADS1015_REG_CONFIG_OS_MASK      = 0x8000
    	__ADS1015_REG_CONFIG_OS_SINGLE    = 0x8000  # Write: Set to start a single-conversion
    	__ADS1015_REG_CONFIG_OS_BUSY      = 0x0000  # Read: Bit = 0 when conversion is in progress
    	__ADS1015_REG_CONFIG_OS_NOTBUSY   = 0x8000  # Read: Bit = 1 when device is not performing a conversion
    
    	__ADS1015_REG_CONFIG_MUX_MASK     = 0x7000
    	__ADS1015_REG_CONFIG_MUX_DIFF_0_1 = 0x0000  # Differential P = AIN0, N = AIN1 (default)
    	__ADS1015_REG_CONFIG_MUX_DIFF_0_3 = 0x1000  # Differential P = AIN0, N = AIN3
    	__ADS1015_REG_CONFIG_MUX_DIFF_1_3 = 0x2000  # Differential P = AIN1, N = AIN3
    	__ADS1015_REG_CONFIG_MUX_DIFF_2_3 = 0x3000  # Differential P = AIN2, N = AIN3
    	__ADS1015_REG_CONFIG_MUX_SINGLE_0 = 0x4000  # Single-ended AIN0
    	__ADS1015_REG_CONFIG_MUX_SINGLE_1 = 0x5000  # Single-ended AIN1
    	__ADS1015_REG_CONFIG_MUX_SINGLE_2 = 0x6000  # Single-ended AIN2
    	__ADS1015_REG_CONFIG_MUX_SINGLE_3 = 0x7000  # Single-ended AIN3
    
    	__ADS1015_REG_CONFIG_PGA_MASK     = 0x0E00
    	__ADS1015_REG_CONFIG_PGA_6_144V   = 0x0000  # +/-6.144V range
    	__ADS1015_REG_CONFIG_PGA_4_096V   = 0x0200  # +/-4.096V range
    	__ADS1015_REG_CONFIG_PGA_2_048V   = 0x0400  # +/-2.048V range (default)
    	__ADS1015_REG_CONFIG_PGA_1_024V   = 0x0600  # +/-1.024V range
    	__ADS1015_REG_CONFIG_PGA_0_512V   = 0x0800  # +/-0.512V range
    	__ADS1015_REG_CONFIG_PGA_0_256V   = 0x0A00  # +/-0.256V range
    
    	__ADS1015_REG_CONFIG_MODE_MASK    = 0x0100
    	__ADS1015_REG_CONFIG_MODE_CONTIN  = 0x0000  # Continuous conversion mode
    	__ADS1015_REG_CONFIG_MODE_SINGLE  = 0x0100  # Power-down single-shot mode (default)
    
    	__ADS1015_REG_CONFIG_DR_MASK      = 0x00E0  
    	__ADS1015_REG_CONFIG_DR_128SPS    = 0x0000  # 128 samples per second
    	__ADS1015_REG_CONFIG_DR_250SPS    = 0x0020  # 250 samples per second
    	__ADS1015_REG_CONFIG_DR_490SPS    = 0x0040  # 490 samples per second
    	__ADS1015_REG_CONFIG_DR_920SPS    = 0x0060  # 920 samples per second
    	__ADS1015_REG_CONFIG_DR_1600SPS   = 0x0080  # 1600 samples per second (default)
    	__ADS1015_REG_CONFIG_DR_2400SPS   = 0x00A0  # 2400 samples per second
    	__ADS1015_REG_CONFIG_DR_3300SPS   = 0x00C0  # 3300 samples per second (also 0x00E0)
    
    	__ADS1115_REG_CONFIG_DR_8SPS      = 0x0000  # 8 samples per second
    	__ADS1115_REG_CONFIG_DR_16SPS     = 0x0020  # 16 samples per second
    	__ADS1115_REG_CONFIG_DR_32SPS     = 0x0040  # 32 samples per second
    	__ADS1115_REG_CONFIG_DR_64SPS     = 0x0060  # 64 samples per second
    	__ADS1115_REG_CONFIG_DR_128SPS    = 0x0080  # 128 samples per second
    	__ADS1115_REG_CONFIG_DR_250SPS    = 0x00A0  # 250 samples per second (default)
    	__ADS1115_REG_CONFIG_DR_475SPS    = 0x00C0  # 475 samples per second
    	__ADS1115_REG_CONFIG_DR_860SPS    = 0x00E0  # 860 samples per second
    
    	__ADS1015_REG_CONFIG_CMODE_MASK   = 0x0010
    	__ADS1015_REG_CONFIG_CMODE_TRAD   = 0x0000  # Traditional comparator with hysteresis (default)
    	__ADS1015_REG_CONFIG_CMODE_WINDOW = 0x0010  # Window comparator
    
    	__ADS1015_REG_CONFIG_CPOL_MASK    = 0x0008
    	__ADS1015_REG_CONFIG_CPOL_ACTVLOW = 0x0000  # ALERT/RDY pin is low when active (default)
    	__ADS1015_REG_CONFIG_CPOL_ACTVHI  = 0x0008  # ALERT/RDY pin is high when active
    
    	__ADS1015_REG_CONFIG_CLAT_MASK    = 0x0004  # Determines if ALERT/RDY pin latches once asserted
    	__ADS1015_REG_CONFIG_CLAT_NONLAT  = 0x0000  # Non-latching comparator (default)
    	__ADS1015_REG_CONFIG_CLAT_LATCH   = 0x0004  # Latching comparator
    
    	__ADS1015_REG_CONFIG_CQUE_MASK    = 0x0003
    	__ADS1015_REG_CONFIG_CQUE_1CONV   = 0x0000  # Assert ALERT/RDY after one conversions
    	__ADS1015_REG_CONFIG_CQUE_2CONV   = 0x0001  # Assert ALERT/RDY after two conversions
    	__ADS1015_REG_CONFIG_CQUE_4CONV   = 0x0002  # Assert ALERT/RDY after four conversions
    	__ADS1015_REG_CONFIG_CQUE_NONE    = 0x0003  # Disable the comparator and put ALERT/RDY in high state (default)
      
      
      # Dictionaries with the sampling speed values
      # These simplify and clean the code (avoid the abuse of if/elif/else clauses)
    	spsADS1115 = {
    	8:__ADS1115_REG_CONFIG_DR_8SPS,
    	16:__ADS1115_REG_CONFIG_DR_16SPS,
    	32:__ADS1115_REG_CONFIG_DR_32SPS,
    	64:__ADS1115_REG_CONFIG_DR_64SPS,
    	128:__ADS1115_REG_CONFIG_DR_128SPS,
    	250:__ADS1115_REG_CONFIG_DR_250SPS,
    	475:__ADS1115_REG_CONFIG_DR_475SPS,
    	860:__ADS1115_REG_CONFIG_DR_860SPS
    	}    
    	spsADS1015 = {
    	128:__ADS1015_REG_CONFIG_DR_128SPS,
    	250:__ADS1015_REG_CONFIG_DR_250SPS,
    	490:__ADS1015_REG_CONFIG_DR_490SPS,
    	920:__ADS1015_REG_CONFIG_DR_920SPS,
    	1600:__ADS1015_REG_CONFIG_DR_1600SPS,
    	2400:__ADS1015_REG_CONFIG_DR_2400SPS,
    	3300:__ADS1015_REG_CONFIG_DR_3300SPS
    	}
      # Dictionariy with the programable gains
    	pgaADS1x15 = {
    	6144:__ADS1015_REG_CONFIG_PGA_6_144V,
    	4096:__ADS1015_REG_CONFIG_PGA_4_096V,
    	2048:__ADS1015_REG_CONFIG_PGA_2_048V,
    	1024:__ADS1015_REG_CONFIG_PGA_1_024V,
    	512:__ADS1015_REG_CONFIG_PGA_0_512V,
    	256:__ADS1015_REG_CONFIG_PGA_0_256V
    	}    
      
    
    
      # Constructor
    	def __init__(self, address=0x48, ic=__IC_ADS1115, debug=False):
        # Depending on if you have an old or a new Raspberry Pi, you
        # may need to change the I2C bus.  Older Pis use SMBus 0,
        # whereas new Pis use SMBus 1.  If you see an error like:
        # 'Error accessing 0x48: Check your I2C address '
        # change the SMBus number in the initializer below!
    		self.i2c = Adafruit_I2C(address)
    		self.address = address
    		self.debug = debug
    
        # Make sure the IC specified is valid
    		if ((ic < self.__IC_ADS1015) | (ic > self.__IC_ADS1115)):
    			if (self.debug):
    				print "ADS1x15: Invalid IC specfied: %h" % ic
    			return -1
    		else:
    			self.ic = ic
            
        # Set pga value, so that getLastConversionResult() can use it,
        # any function that accepts a pga value must update this.
    		self.pga = 6144    
      
        
    	def readADCSingleEnded(self, channel=0, pga=6144, sps=250):
    		"Gets a single-ended ADC reading from the specified channel in mV. \
    		The sample rate for this mode (single-shot) can be used to lower the noise \
    		(low sps) or to lower the power consumption (high sps) by duty cycling, \
    		see datasheet page 14 for more info. \
    		The pga must be given in mV, see page 13 for the supported values."
        
        # With invalid channel return -1
    		if (channel > 3):
    			if (self.debug):
    				print "ADS1x15: Invalid channel specified: %d" % channel
    			return -1
        
        # Disable comparator, Non-latching, Alert/Rdy active low
        # traditional comparator, single-shot mode
    		config = self.__ADS1015_REG_CONFIG_CQUE_NONE    | \
    				self.__ADS1015_REG_CONFIG_CLAT_NONLAT  | \
    				self.__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
    				self.__ADS1015_REG_CONFIG_CMODE_TRAD   | \
    				self.__ADS1015_REG_CONFIG_MODE_SINGLE    
    
        # Set sample per seconds, defaults to 250sps
        # If sps is in the dictionary (defined in init) it returns the value of the constant
        # othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
    		if (self.ic == self.__IC_ADS1015):
    			config |= self.spsADS1015.setdefault(sps, self.__ADS1015_REG_CONFIG_DR_1600SPS)
    		else:
    			if ( (sps not in self.spsADS1115) & self.debug):
    				print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps     
    			config |= self.spsADS1115.setdefault(sps, self.__ADS1115_REG_CONFIG_DR_250SPS)
    
        # Set PGA/voltage range, defaults to +-6.144V
    		if ( (pga not in self.pgaADS1x15) & self.debug):	  
    			print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps     
    		config |= self.pgaADS1x15.setdefault(pga, self.__ADS1015_REG_CONFIG_PGA_6_144V)
    		self.pga = pga
    
        # Set the channel to be converted
    		if channel == 3:
    			config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_3
    		elif channel == 2:
    			config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_2
    		elif channel == 1:
    			config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_1
    		else:
    			config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_0
    
    		# Set 'start single-conversion' bit
    		config |= self.__ADS1015_REG_CONFIG_OS_SINGLE
    
    		# Write config register to the ADC
    		bytes = [(config >> 8) & 0xFF, config & 0xFF]
    		self.i2c.writeList(self.__ADS1015_REG_POINTER_CONFIG, bytes)
    
    		# Wait for the ADC conversion to complete
    		# The minimum delay depends on the sps: delay >= 1/sps
    		# We add 0.1ms to be sure
    		delay = 1.0/sps+0.0001
    		time.sleep(delay)
    
    		# Read the conversion results
    		result = self.i2c.readList(self.__ADS1015_REG_POINTER_CONVERT, 2)
    		if (self.ic == self.__IC_ADS1015):
    			# Shift right 4 bits for the 12-bit ADS1015 and convert to mV
    			return ( ((result[0] << 8) | (result[1] & 0xFF)) >> 4 )*pga/2048.0
    		else:
    		# Return a mV value for the ADS1115
    		# (Take signed values into account as well)
    			val = (result[0] << 8) | (result[1])
    			if val > 0x7FFF:
    			  return (val - 0xFFFF)*pga/32768.0
    			else:
    			  return ( (result[0] << 8) | (result[1]) )*pga/32768.0
    
    while True:
    	time.sleep(1)
    	__init__(self, address=0x48, ic=__IC_ADS1115, debug=False)
    
    	result = readADCSingleEnded(self, channel=0, pga=6144, sps=250)
    	print(result)
    le code est assez long mais je pense que tu verras vite le probleme enfin j'espere :s

  9. #8
    fred1599

    Re : python : mon compilateur ne reconnait pas une fonction

    Code:
    __init__(self, address=0x48, ic=__IC_ADS1115, debug=False)
    Là il y a une erreur, si on veut créer une instance, la ligne serait plutôt quelque chose qui ressemble à

    Code:
    ads = ADS1x15(address=0x48, ic=__IC_ADS1115, debug=False)
    En prenant la remarque du dessus, la ligne

    Code:
    result = readADCSingleEnded(self, channel=0, pga=6144, sps=250)
    sera

    Code:
    result = ads.readADCSingleEnded(channel=0, pga=6144, sps=250)
    print result

  10. #9
    docEmmettBrown

    Re : python : mon compilateur ne reconnait pas une fonction

    ok, j'ai essayé ton code mais ca me met :

    NameError: name '__IC_ADS1115' is not defined

  11. #10
    polo974

    Re : python : mon compilateur ne reconnait pas une fonction

    __IC_ADS1115 n'est défini que dans la classe ADS1x15, donc il faut mettre :

    self.__IC_ADS1115 dans le def du constructeur...

    et accessoirement relire les tutos et autres docs python pour ce qui se trouve après le while True....

    genre:
    Code:
    monADS=ADS1x15(...)
    while True:
        monADS.fait quelque_chose_avec()
        time.sleep(1)
    Jusqu'ici tout va bien...

  12. #11
    docEmmettBrown

    Re : python : mon compilateur ne reconnait pas une fonction

    donc si j'ai bien compris le nouveau code est :

    Code:
    #!/usr/bin/python
    import re
    import smbus
    import Adafruit_I2C
    import time
    # ===========================================================================
    # ADS1x15 Class
    #
    # Originally written by K. Townsend, Adafruit (https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_ADS1x15)
    # Updates and new functions implementation by Pedro Villanueva, 03/2013.
    # The only error in the original code was in line 57:
    #              __ADS1015_REG_CONFIG_DR_920SPS    = 0x0050
    # should be 
    #              __ADS1015_REG_CONFIG_DR_920SPS    = 0x0060     
    #
    # NOT IMPLEMENTED: Conversion ready pin, page 15 datasheet.
    # ===========================================================================
    
    class ADS1x15:
    	i2c = None
    
      # IC Identifiers
    	__IC_ADS1015                      = 0x00
    	__IC_ADS1115                      = 0x01
    
      # Pointer Register
    	__ADS1015_REG_POINTER_MASK        = 0x03
    	__ADS1015_REG_POINTER_CONVERT     = 0x00
    	__ADS1015_REG_POINTER_CONFIG      = 0x01
    	__ADS1015_REG_POINTER_LOWTHRESH   = 0x02
    	__ADS1015_REG_POINTER_HITHRESH    = 0x03
    
      # Config Register
    	__ADS1015_REG_CONFIG_OS_MASK      = 0x8000
    	__ADS1015_REG_CONFIG_OS_SINGLE    = 0x8000  # Write: Set to start a single-conversion
    	__ADS1015_REG_CONFIG_OS_BUSY      = 0x0000  # Read: Bit = 0 when conversion is in progress
    	__ADS1015_REG_CONFIG_OS_NOTBUSY   = 0x8000  # Read: Bit = 1 when device is not performing a conversion
    
    	__ADS1015_REG_CONFIG_MUX_MASK     = 0x7000
    	__ADS1015_REG_CONFIG_MUX_DIFF_0_1 = 0x0000  # Differential P = AIN0, N = AIN1 (default)
    	__ADS1015_REG_CONFIG_MUX_DIFF_0_3 = 0x1000  # Differential P = AIN0, N = AIN3
    	__ADS1015_REG_CONFIG_MUX_DIFF_1_3 = 0x2000  # Differential P = AIN1, N = AIN3
    	__ADS1015_REG_CONFIG_MUX_DIFF_2_3 = 0x3000  # Differential P = AIN2, N = AIN3
    	__ADS1015_REG_CONFIG_MUX_SINGLE_0 = 0x4000  # Single-ended AIN0
    	__ADS1015_REG_CONFIG_MUX_SINGLE_1 = 0x5000  # Single-ended AIN1
    	__ADS1015_REG_CONFIG_MUX_SINGLE_2 = 0x6000  # Single-ended AIN2
    	__ADS1015_REG_CONFIG_MUX_SINGLE_3 = 0x7000  # Single-ended AIN3
    
    	__ADS1015_REG_CONFIG_PGA_MASK     = 0x0E00
    	__ADS1015_REG_CONFIG_PGA_6_144V   = 0x0000  # +/-6.144V range
    	__ADS1015_REG_CONFIG_PGA_4_096V   = 0x0200  # +/-4.096V range
    	__ADS1015_REG_CONFIG_PGA_2_048V   = 0x0400  # +/-2.048V range (default)
    	__ADS1015_REG_CONFIG_PGA_1_024V   = 0x0600  # +/-1.024V range
    	__ADS1015_REG_CONFIG_PGA_0_512V   = 0x0800  # +/-0.512V range
    	__ADS1015_REG_CONFIG_PGA_0_256V   = 0x0A00  # +/-0.256V range
    
    	__ADS1015_REG_CONFIG_MODE_MASK    = 0x0100
    	__ADS1015_REG_CONFIG_MODE_CONTIN  = 0x0000  # Continuous conversion mode
    	__ADS1015_REG_CONFIG_MODE_SINGLE  = 0x0100  # Power-down single-shot mode (default)
    
    	__ADS1015_REG_CONFIG_DR_MASK      = 0x00E0  
    	__ADS1015_REG_CONFIG_DR_128SPS    = 0x0000  # 128 samples per second
    	__ADS1015_REG_CONFIG_DR_250SPS    = 0x0020  # 250 samples per second
    	__ADS1015_REG_CONFIG_DR_490SPS    = 0x0040  # 490 samples per second
    	__ADS1015_REG_CONFIG_DR_920SPS    = 0x0060  # 920 samples per second
    	__ADS1015_REG_CONFIG_DR_1600SPS   = 0x0080  # 1600 samples per second (default)
    	__ADS1015_REG_CONFIG_DR_2400SPS   = 0x00A0  # 2400 samples per second
    	__ADS1015_REG_CONFIG_DR_3300SPS   = 0x00C0  # 3300 samples per second (also 0x00E0)
    
    	__ADS1115_REG_CONFIG_DR_8SPS      = 0x0000  # 8 samples per second
    	__ADS1115_REG_CONFIG_DR_16SPS     = 0x0020  # 16 samples per second
    	__ADS1115_REG_CONFIG_DR_32SPS     = 0x0040  # 32 samples per second
    	__ADS1115_REG_CONFIG_DR_64SPS     = 0x0060  # 64 samples per second
    	__ADS1115_REG_CONFIG_DR_128SPS    = 0x0080  # 128 samples per second
    	__ADS1115_REG_CONFIG_DR_250SPS    = 0x00A0  # 250 samples per second (default)
    	__ADS1115_REG_CONFIG_DR_475SPS    = 0x00C0  # 475 samples per second
    	__ADS1115_REG_CONFIG_DR_860SPS    = 0x00E0  # 860 samples per second
    
    	__ADS1015_REG_CONFIG_CMODE_MASK   = 0x0010
    	__ADS1015_REG_CONFIG_CMODE_TRAD   = 0x0000  # Traditional comparator with hysteresis (default)
    	__ADS1015_REG_CONFIG_CMODE_WINDOW = 0x0010  # Window comparator
    
    	__ADS1015_REG_CONFIG_CPOL_MASK    = 0x0008
    	__ADS1015_REG_CONFIG_CPOL_ACTVLOW = 0x0000  # ALERT/RDY pin is low when active (default)
    	__ADS1015_REG_CONFIG_CPOL_ACTVHI  = 0x0008  # ALERT/RDY pin is high when active
    
    	__ADS1015_REG_CONFIG_CLAT_MASK    = 0x0004  # Determines if ALERT/RDY pin latches once asserted
    	__ADS1015_REG_CONFIG_CLAT_NONLAT  = 0x0000  # Non-latching comparator (default)
    	__ADS1015_REG_CONFIG_CLAT_LATCH   = 0x0004  # Latching comparator
    
    	__ADS1015_REG_CONFIG_CQUE_MASK    = 0x0003
    	__ADS1015_REG_CONFIG_CQUE_1CONV   = 0x0000  # Assert ALERT/RDY after one conversions
    	__ADS1015_REG_CONFIG_CQUE_2CONV   = 0x0001  # Assert ALERT/RDY after two conversions
    	__ADS1015_REG_CONFIG_CQUE_4CONV   = 0x0002  # Assert ALERT/RDY after four conversions
    	__ADS1015_REG_CONFIG_CQUE_NONE    = 0x0003  # Disable the comparator and put ALERT/RDY in high state (default)
      
      
      # Dictionaries with the sampling speed values
      # These simplify and clean the code (avoid the abuse of if/elif/else clauses)
    	spsADS1115 = {
    	8:__ADS1115_REG_CONFIG_DR_8SPS,
    	16:__ADS1115_REG_CONFIG_DR_16SPS,
    	32:__ADS1115_REG_CONFIG_DR_32SPS,
    	64:__ADS1115_REG_CONFIG_DR_64SPS,
    	128:__ADS1115_REG_CONFIG_DR_128SPS,
    	250:__ADS1115_REG_CONFIG_DR_250SPS,
    	475:__ADS1115_REG_CONFIG_DR_475SPS,
    	860:__ADS1115_REG_CONFIG_DR_860SPS
    	}    
    	spsADS1015 = {
    	128:__ADS1015_REG_CONFIG_DR_128SPS,
    	250:__ADS1015_REG_CONFIG_DR_250SPS,
    	490:__ADS1015_REG_CONFIG_DR_490SPS,
    	920:__ADS1015_REG_CONFIG_DR_920SPS,
    	1600:__ADS1015_REG_CONFIG_DR_1600SPS,
    	2400:__ADS1015_REG_CONFIG_DR_2400SPS,
    	3300:__ADS1015_REG_CONFIG_DR_3300SPS
    	}
      # Dictionariy with the programable gains
    	pgaADS1x15 = {
    	6144:__ADS1015_REG_CONFIG_PGA_6_144V,
    	4096:__ADS1015_REG_CONFIG_PGA_4_096V,
    	2048:__ADS1015_REG_CONFIG_PGA_2_048V,
    	1024:__ADS1015_REG_CONFIG_PGA_1_024V,
    	512:__ADS1015_REG_CONFIG_PGA_0_512V,
    	256:__ADS1015_REG_CONFIG_PGA_0_256V
    	}    
      
    
    
      # Constructor
    	def __init__(self, address=0x48, ic=self.__IC_ADS1115, debug=False):
        # Depending on if you have an old or a new Raspberry Pi, you
        # may need to change the I2C bus.  Older Pis use SMBus 0,
        # whereas new Pis use SMBus 1.  If you see an error like:
        # 'Error accessing 0x48: Check your I2C address '
        # change the SMBus number in the initializer below!
    		self.i2c = Adafruit_I2C(address)
    		self.address = address
    		self.debug = debug
    
        # Make sure the IC specified is valid
    		if ((ic < self.__IC_ADS1015) | (ic > self.__IC_ADS1115)):
    			if (self.debug):
    				print "ADS1x15: Invalid IC specfied: %h" % ic
    			return -1
    		else:
    			self.ic = ic
            
        # Set pga value, so that getLastConversionResult() can use it,
        # any function that accepts a pga value must update this.
    		self.pga = 6144    
      
        
    	def readADCSingleEnded(self, channel=0, pga=6144, sps=250):
    		"Gets a single-ended ADC reading from the specified channel in mV. \
    		The sample rate for this mode (single-shot) can be used to lower the noise \
    		(low sps) or to lower the power consumption (high sps) by duty cycling, \
    		see datasheet page 14 for more info. \
    		The pga must be given in mV, see page 13 for the supported values."
        
        # With invalid channel return -1
    		if (channel > 3):
    			if (self.debug):
    				print "ADS1x15: Invalid channel specified: %d" % channel
    			return -1
        
        # Disable comparator, Non-latching, Alert/Rdy active low
        # traditional comparator, single-shot mode
    		config = self.__ADS1015_REG_CONFIG_CQUE_NONE    | \
    				self.__ADS1015_REG_CONFIG_CLAT_NONLAT  | \
    				self.__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
    				self.__ADS1015_REG_CONFIG_CMODE_TRAD   | \
    				self.__ADS1015_REG_CONFIG_MODE_SINGLE    
    
        # Set sample per seconds, defaults to 250sps
        # If sps is in the dictionary (defined in init) it returns the value of the constant
        # othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
    		if (self.ic == self.__IC_ADS1015):
    			config |= self.spsADS1015.setdefault(sps, self.__ADS1015_REG_CONFIG_DR_1600SPS)
    		else:
    			if ( (sps not in self.spsADS1115) & self.debug):
    				print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps     
    			config |= self.spsADS1115.setdefault(sps, self.__ADS1115_REG_CONFIG_DR_250SPS)
    
        # Set PGA/voltage range, defaults to +-6.144V
    		if ( (pga not in self.pgaADS1x15) & self.debug):	  
    			print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps     
    		config |= self.pgaADS1x15.setdefault(pga, self.__ADS1015_REG_CONFIG_PGA_6_144V)
    		self.pga = pga
    
        # Set the channel to be converted
    		if channel == 3:
    			config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_3
    		elif channel == 2:
    			config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_2
    		elif channel == 1:
    			config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_1
    		else:
    			config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_0
    
    		# Set 'start single-conversion' bit
    		config |= self.__ADS1015_REG_CONFIG_OS_SINGLE
    
    		# Write config register to the ADC
    		bytes = [(config >> 8) & 0xFF, config & 0xFF]
    		self.i2c.writeList(self.__ADS1015_REG_POINTER_CONFIG, bytes)
    
    		# Wait for the ADC conversion to complete
    		# The minimum delay depends on the sps: delay >= 1/sps
    		# We add 0.1ms to be sure
    		delay = 1.0/sps+0.0001
    		time.sleep(delay)
    
    		# Read the conversion results
    		result = self.i2c.readList(self.__ADS1015_REG_POINTER_CONVERT, 2)
    		if (self.ic == self.__IC_ADS1015):
    			# Shift right 4 bits for the 12-bit ADS1015 and convert to mV
    			return ( ((result[0] << 8) | (result[1] & 0xFF)) >> 4 )*pga/2048.0
    		else:
    		# Return a mV value for the ADS1115
    		# (Take signed values into account as well)
    			val = (result[0] << 8) | (result[1])
    			if val > 0x7FFF:
    			  return (val - 0xFFFF)*pga/32768.0
    			else:
    			  return ( (result[0] << 8) | (result[1]) )*pga/32768.0
    
    while True:
    	time.sleep(1)
            ads = ADS1x15(address=0x48, ic=__IC_ADS1115, debug=False)
    	result = ads.readADCSingleEnded(channel=0, pga=6144, sps=250)
            print result
    mais ca me met:
    line 133 in ADS1x15 def __init__(self, address=0x48, ic=self.__IC_ADS1115, debug=False):
    name self is not defined
    Dernière modification par docEmmettBrown ; 17/05/2015 à 23h41.

  13. #12
    fred1599

    Re : python : mon compilateur ne reconnait pas une fonction

    Ben oui dans le paramètre, t'as mis un self, ça n'existe pas en python, ça ! Faut lire un tutoriel de toute urgence...

Discussions similaires

  1. fonction mystere (python)
    Par afaf1995 dans le forum Programmation et langages, Algorithmique
    Réponses: 8
    Dernier message: 15/03/2016, 21h21
  2. fonction de dichotomie Python
    Par january dans le forum Programmation et langages, Algorithmique
    Réponses: 4
    Dernier message: 01/03/2015, 14h05
  3. fonction (min d'une liste) (python)
    Par afaf1995 dans le forum Programmation et langages, Algorithmique
    Réponses: 2
    Dernier message: 24/02/2014, 08h29
  4. Python pyserial fonction read()
    Par invitede7cc8c5 dans le forum Électronique
    Réponses: 0
    Dernier message: 03/03/2013, 17h21
  5. [PYTHON]Stopper une fonction
    Par patagouin dans le forum Programmation et langages, Algorithmique
    Réponses: 3
    Dernier message: 07/08/2012, 08h19