Exemple #1
0
class SlopeLeader(object):
    def __init__(self):
        self.adc = ADC(Pin('rINC'))
        self._timer = Timer(7, freq=100)
        self._buffer = bytearray(10)

    def read(self):
        self.adc.read_timed(self._buffer, self._timer)  # resolution 256
        value = sum(self._buffer) // len(self._buffer)
        slope = value / 256  # ratio to cover
        return slope

    def off(self):
        #value = self.read()
        #print('slope=', value)
        return True if self.read() <= 0.1 else False
class Mic:
    def __init__(self, mic_pinname, timer_id=6):
        self.mic = ADC(mic_pinname)
        self.tim = Timer(timer_id, freq=48000)
        self.samples = array.array('h', range(4800))
        self.normalized_spl = 0.0

    def level(self):
        samples = self.samples
        self.mic.read_timed(samples, self.tim)
        ave = sum(samples) / len(samples)
        self.normalized_spl = \
            min(1.0, sum((v-ave)**2 for v in samples) / len(samples) / 2278619.0)
        return self.normalized_spl

    def excited(self):
        return self.level() > 0.01
class Mic:
    def __init__(self, mic_pinname, timer_id=6):
        self.mic = ADC(mic_pinname)
        self.tim = Timer(timer_id, freq=48000)
        self.samples = array.array('h', range(4800))
        self.normalized_spl = 0.0

    def level(self):
        samples = self.samples
        self.mic.read_timed(samples, self.tim)
        ave = sum(samples) / len(samples)
        self.normalized_spl = \
            min(1.0, sum((v-ave)**2 for v in samples) / len(samples) / 2278619.0)
        return self.normalized_spl

    def excited(self):
        return self.level() > 0.01
Exemple #4
0
class SpeedLeader(object):
    M_MAX = const(20)
    SPD_MAX = const(15)  # 15 km/h

    def __init__(self):
        self._adc = ADC(Pin('rSPD'))
        self._timer = Timer(6, freq=100)
        self._buffer = bytearray(self.M_MAX)

    @property
    def speed(self):
        self._adc.read_timed(self._buffer, self._timer)  # resolution 256
        value = sum(self._buffer) // len(self._buffer)
        speed = value * self.SPD_MAX / 256  #[km/h]
        speed = trunc(speed * 10) / 10
        #print('ref speed=', speed)
        return speed

    def off(self):
        value = self.speed
        return True if value <= 2.0 else False
from pyb import Pin, ADC, Timer, ExtInt
from time import sleep_ms
import array

T = 1  # millisecond
f = 10**3 / T
nb = 100

pinX1 = Pin('X1', Pin.OUT)  # Alimentation du circuit RC
pinX2 = Pin('X2')  # Tension condensateur
adc = ADC(pinX2)  # Activation du CAN

buf = array.array("h", nb * [0x7FFF])  # h = signed short (int 2 octets)
tim = Timer(6, freq=f)  # create a timer running at 10Hz
mesures = lambda e: adc.read_timed(buf, tim)  # read analog values into buf
ext = ExtInt(Pin('X3'), ExtInt.IRQ_FALLING, Pin.PULL_NONE, mesures)

pinX1.low()  # Décharge du condensateur
sleep_ms(1000)  # Attendre 2 s
pinX1.high()  # Début de la charge
sleep_ms(1000)
ext.disable()

x = [i * 1 / f * 1000 for i in range(nb)]
y = [val for val in buf]
for i in range(len(x)):
    if y[i] < (max(y) * 0.37):
        tau = x[i]
        break
    else:
from pyb import Pin, ADC, Timer
import array

f = 1000
N = 100

adc = ADC(Pin('A0'))  # Activation du CAN
buf = array.array("h", N * [0x7FFF])  # h = signed short (int 2 octets)
tim = Timer(1, freq=f)  # create a timer running at 10Hz

adc.read_timed(buf, tim)  # Mesures

# Données
x = [i * 1 / f * 1000 for i in range(N)]
y = [val for val in buf]

for i in range(len(x)):
    print(x[i], y[i])
Fiche produit:
---> https://shop.mchobby.be/fr/micropython/1830-pybstick-lite-26-micropython-et-arduino-3232100018303-garatronic.html

MCHobby investit du temps et des ressources pour écrire de la
documentation, du code et des exemples.
Aidez nous à en produire plus en achetant vos produits chez MCHobby.

------------------------------------------------------------------------

History:
  09 april 2020 - Dominique - initial code
"""

from pyb import ADC, Timer
from time import sleep

SAMPLE_FREQ = 50  # Smapling time @ 50Hz, so 50 sample per second

buffer = bytearray(200)  # collect 200 values
tim = Timer(9, freq=50)  # Set the sampling timer

adc = ADC("S26")
# Fill the buffer @ timer frequency. This will take 4 seconds in this case
# This call is blocking
adc.read_timed(buffer, tim)
# print out the collected values
timing = 0.0
for value in buffer:
    print("%5.4f : %3i" % (timing, value))
    timing += 1 / SAMPLE_FREQ
from pyb import Pin, ADC, Timer
from array import array

f = 500                           # Fréquence d'échantillonnage (750 kHz max.)
N = 20                            # Nombre de points
buffer = array("h", N*[0xFFFF])   # Tableau de N x 16 bit pour stocker les mesures
                                  # "h" pour signed short (int 2 octets)
                                        
adc = ADC(Pin('A0'))              # Déclaration du CAN sur la broche A0

tim = Timer(6, freq=f)            # Le timer 6 fixe la fréquence d'échantillonnage f

adc.read_timed(buffer, tim)       # Lancement des mesures

f = tim.freq()
t = [i*1/f     for i in range(N)]
y = [buffer[i] for i in range(N)]

print(t)
print(y)
Exemple #9
0
from pyb import ADC
from pyb import Pin

adc = ADC('X22')
print(adc)

adc.read()

buf = bytearray(100)
adc.read_timed(buf, 500)
Exemple #10
0
from pyb import Pin

pin = Pin('X22', mode=Pin.IN, pull=Pin.PULL_DOWN)
adc = ADC('X22')
print(adc)

# read single sample
val = adc.read()
assert val < 500

# timer for read_timed
tim = pyb.Timer(5, freq=500)

# read into bytearray
buf = bytearray(50)
adc.read_timed(buf, tim)
print(len(buf))
for i in buf:
    assert i < 500

# read into arrays with different element sizes
import array

ar = array.array('h', 25 * [0])
adc.read_timed(ar, tim)
print(len(ar))
for i in buf:
    assert i < 500
ar = array.array('i', 30 * [0])
adc.read_timed(ar, tim)
print(len(ar))
# Charge d'un condensateur à travers une résistance
# R = 10k et C > 10 nF
from pyb import Pin, ADC, Timer
from time import sleep_ms
import array

f = 500E3  # 500 kHz max
n = 1000

pinE = Pin('A3', Pin.OUT)  # Alimentation du circuit RC
adc = ADC(Pin('A2'))  # Activation du CAN
u = array.array("h", n * [0x7FFF])  # h = signed short (int 2 octets)
tim = Timer(6, freq=f)  # create a timer running at 10Hz

while True:
    pinE.on()  # Décharge du condensateur
    sleep_ms(1000)  # Attendre
    pinE.off()  # Début de la charge
    adc.read_timed(u, tim)  # Mesures
    seuil = u[0] * 0.37
    for i in range(n):
        if u[i] < seuil:
            tau = i / f * 1000  # millisecond
            break
    C = tau / 10 * 1000  # nF
    print(nb_pts, tau, "ms", C)
Exemple #12
0
from pyb import Pin

pin = Pin('X22', mode=Pin.IN, pull=Pin.PULL_DOWN)
adc = ADC('X22')
print(adc)

# read single sample
val = adc.read()
assert val < 500

# timer for read_timed
tim = pyb.Timer(5, freq=500)

# read into bytearray
buf = bytearray(50)
adc.read_timed(buf, tim)
print(len(buf))
for i in buf:
    assert i < 500

# read into arrays with different element sizes
import array
ar = array.array('h', 25 * [0])
adc.read_timed(ar, tim)
print(len(ar))
for i in buf:
    assert i < 500
ar = array.array('i', 30 * [0])
adc.read_timed(ar, tim)
print(len(ar))
for i in buf:
# Charge d'un condensateur à travers une résistance
# R = 10k et C = 220 nF

from pyb import Pin, ADC, Timer
from time import sleep_ms
from array import array

f = 3000  # max = 750 kHz [84Mhz/4/(12+15) = 778 kHz]
n = 200  # nombre de points

pinE = Pin('A0', Pin.OUT)  # A0 en sortie digitale
adc = ADC(Pin('A1'))  # CAN sur A1

buffer = array("h", n * [0x7FFF])  # h = signed short (int 2 octets)
tim = Timer(6, freq=f)  # Declaration du timer

pinE.on()  # Décharge du condensateur
sleep_ms(1000)  # Attendre 1 s
pinE.off()  # Début de la charge
adc.read_timed(buffer, tim)  # Acquisition

# Affichage CSV
f = tim.freq()  # Fréquence réelle utilisée par le timer
print("t;u")
print("ms;_")
for i in range(n):
    print(i / f * 1E3, ";", buffer[i])
Exemple #14
0
start_time = pyb.millis()
start_time2 = pyb.millis()

#tim = pyb.Timer(4, freq=20)         # create a timer running at 10Hz
#buf = bytearray(100)                # creat a buffer to store the samples
#adc.read_timed(buf, tim)            # sample 100 values, taking 10s

#for val in buf:                     # loop over all values
    #print(val)                      # print the value out

for a in ('b' , 'B' , 'h', 'H' , 'i' , 'I' , 'l' , 'L' , 'q' , 'Q' , 'f' , 'd'):
#while True:
    print("read" + str(adc.read() / 16))
    #buf = bytearray(10) #array.array('h')                # create a buffer of 100 bytes
    buf = array.array(a)                # create a buffer of 100 bytes
    adc.read_timed(buf, 10)             # read analog values into buf at 10Hz
                                        #   this will take 10 seconds to finish
    print(buf)
    for val in buf:                     # loop over all values
        print(val)                      # print the value out

sys.exit()
start_time = pyb.millis()
while pyb.elapsed_millis(start_time) < 50:
    print(adc.read())

while pyb.elapsed_millis(start_time) < 5000 or abs(max_pulse_sensor - min_pulse_sensor) < 100:
    pulse_sensor = adc.read()
    #print("ADC = %d" % (pulse_sensor))
    if  pyb.elapsed_millis(start_time2) > 1000:
        min_pulse_sensor = pulse_sensor
from pyb import Pin, ADC, Timer
from array import array
from math import log
from linear_regression import linear_reg
from time import sleep_ms

f = 750E3  # max = 750 kHz [84Mhz/4/(12+15) = 778 kHz]
nb = 100  # Nombre de points de mesure

pinE = Pin('A2', Pin.OUT)  # Source du circuit RC
adc = ADC(Pin('A3'))  # Activation du CAN
buf = array("h", nb * [0x7FFF])  # h = signed short (int 2 octets)
tim = Timer(6, freq=f)

while True:
    pinE.on()  # Décharge du condensateur E=0
    sleep_ms(100)  # Attendre 100 ms
    pinE.off()  # Début de la charge E=Vcc

    adc.read_timed(buf, tim)  # Lance les mesures

    f = tim.freq()  # Fréquence réelle utilisée par le timer
    x = [i / f * 1E6 for i in range(nb)]  # Tableau des fréquence en µs
    y = [log(val) for val in buf]  # Tableau des ln(u)

    a, b = linear_reg(x, y)  # Regression linéaire
    C = -1 / (a * 10)  # Calcul de la capacité
    print("C = ", C)  # Affichage

    sleep_ms(1000)
Exemple #16
0
adct = ADC(16) # Temperature 930 -> 20C
print(adct)
adcv = ADC(17) # Voltage 1500 -> 3.3V
print(adcv)

# read single sample; 2.5V-5V is pass range
val = adcv.read()
assert val > 1000 and val < 2000

# timer for read_timed
tim = Timer(5, freq=500)

# read into bytearray
buf = bytearray(b'\xff' * 50)
adcv.read_timed(buf, tim)
print(len(buf))
for i in buf:
    assert i > 50 and i < 150

# read into arrays with different element sizes
import array
arv = array.array('h', 25 * [0x7fff])
adcv.read_timed(arv, tim)
print(len(arv))
for i in arv:
    assert i > 1000 and i < 2000

arv = array.array('i', 30 * [-1])
adcv.read_timed(arv, tim)
print(len(arv))
from pyb import Pin, ADC, Timer
from array import array

f = 500  # Fréquence d'échantillonnage (750 kHz max.)
N = 20  # Nombre de points
mesures = array("h",
                N * [0xFFFF])  # Tableau de N x 16 bit pour stocker les mesures
# "h" pour signed short (int 2 octets)

adc = ADC(Pin('A0'))  # Déclaration du CAN sur la broche A0

tim = Timer(6, freq=f)  # Le timer 6 fixe la fréquence d'échantillonnage f

adc.read_timed(mesures, tim)  # Lancement des mesures

print(mesures)  # Affichage des mesures