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)
Ejemplo n.º 2
0
X = dp.load_data('linearX.csv')
y = dp.load_data('linearY.csv')

X = X.reshape((-1, 1))

#Normalizing the training examples
X, meu, sigma = dp.normalize(X)

#Initialising parameters for gradient descent
m, n = X.shape
init_theta = np.zeros(n + 1)
eta = 0.017
epsilon = 1e-10

# Executing gradient descent
theta, iterations, theta_history, cost_history = rg.linear_reg(
    X, y, init_theta, eta, epsilon)

# Plotting the data and hypothesis
dp.plot_training_data(X, y, 'Wine density vs acidity', 'Wine acidity',
                      'Wine density')

print('No. of iterations = ', iterations)
print('Theta = ', theta)

input('Press Enter to draw Hypothesis:')

#Plotting hypothesis
x_test = np.linspace(np.amin(X) - 1, np.amax(X) + 1, num=500)
y_test = rg.pred(x_test.reshape((-1, 1)), theta)
dp.plot_hypothesis(x_test, y_test)
Ejemplo n.º 3
0
aff = Seg7x4(i2c, address=0x70)

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)  # Déclaration du timer

while True:
    pinE.on()  # E=Vcc (charge)
    sleep_ms(100)  # Attendre 100 ms
    pinE.off()  # E=0   (décharge)

    adc.read_timed(buf, tim)  # Mesures

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

    a, b = linear_reg(y, x)  # Regression linéaire
    C = -a / 10  # Calcul de la capacité
    h = round(1.713 * C - 17.90, 1)

    aff.fill(0)  # Efface l'affichage précédent
    aff.text(str(h))  # Affichage
    aff.show()  # ...

    sleep_ms(1000)  # Temporisation