80 lines
2.6 KiB
Python
Executable File
80 lines
2.6 KiB
Python
Executable File
from ina219 import INA219, DeviceRangeError
|
|
from time import sleep
|
|
import time
|
|
import math
|
|
try:
|
|
import RPi.GPIO as GPIO
|
|
except RuntimeError:
|
|
print("Error importing RPi.GPIO!")
|
|
GPIO.setmode(GPIO.BOARD)
|
|
|
|
SHUNT_OHMS = 0.1
|
|
#MAX_EXPECTED_AMPS = 2.0
|
|
MAX_EXPECTED_AMPS = 0.3
|
|
ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS)
|
|
#ina.configure(ina.RANGE_16V, ina.GAIN_1_40MV)
|
|
ina.configure(ina.RANGE_16V)
|
|
marchearret = ''
|
|
# résistance interne du moteur (ohm)
|
|
r = 3.17
|
|
temps = 0
|
|
|
|
def read_ina219():
|
|
global r
|
|
brocheHall = 11
|
|
# On est obligé de mettre à up la broche de Hall, car sinon, elle
|
|
# est aléatoirement fixée.
|
|
GPIO.setup(brocheHall, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
nbtours = 100
|
|
n = 0
|
|
drapeau = 'declanche'
|
|
temps0 = time.time()
|
|
while (n <= nbtours):
|
|
etatBrocheHall = GPIO.input(brocheHall)
|
|
if (etatBrocheHall == GPIO.LOW):
|
|
if (drapeau != 'enclanche'):
|
|
n = n + 1
|
|
#print('Enclanché')
|
|
drapeau = 'enclanche'
|
|
else:
|
|
#print('-')
|
|
drapeau = 'declanche'
|
|
time.sleep(0.001)
|
|
temps = time.time()-temps0
|
|
frequence = nbtours/temps
|
|
omega = 2*math.pi*frequence
|
|
print(temps)
|
|
print('Fréquence mot. : {0:0.3f}trs/s'.format(frequence))
|
|
print('Vit. angulaire : {0:0.3f}rad/s'.format(omega))
|
|
try:
|
|
print('Bus Voltage: {0:0.3f}V'.format(ina.voltage()))
|
|
print('Shunt Voltage: {0:0.3f}mV'.format(ina.shunt_voltage()))
|
|
print('Bus Current: {0:0.3f}mA\n'.format(ina.current()))
|
|
eprime = ina.voltage()-r*(ina.current()/1000)
|
|
fichier = open("data_mot_hall.txt", "a")
|
|
fichier.write('{0:0.3f}'.format(omega))
|
|
fichier.write(' {0:0.3f}'.format(eprime))
|
|
fichier.write(' {0:0.3f}'.format(ina.voltage()))
|
|
fichier.write(' {0:0.3f}'.format(ina.current()))
|
|
iw = frequence * 0.001/temps
|
|
fichier.write(' {0:0.3f}'.format(iw))
|
|
iv = ina.voltage()*0.005
|
|
ia = ina.current()*0.005
|
|
ieprime = iv + r*(ina.current()/1000)*(0.1/r+ia/(ina.current()/1000))
|
|
fichier.write(' {0:0.3f}'.format(ieprime))
|
|
fichier.write(' {0:0.3f}'.format(iv))
|
|
fichier.write(' {0:0.3f}\n'.format(ia))
|
|
fichier.close()
|
|
|
|
except DeviceRangeError as e:
|
|
# Current out of device range with specified shunt resister
|
|
print(e)
|
|
|
|
while marchearret != 'q':
|
|
marchearret = input('Tapez ENTER pour enregistrer une mesure, q pour quitter et n pour une nouvelle série de mesure : ')
|
|
read_ina219()
|
|
if marchearret == 'n':
|
|
fichier = open("data_mot_hall.txt", "a")
|
|
fichier.write('\n\n')
|
|
fichier.close()
|