149 lines
5.0 KiB
Python
149 lines
5.0 KiB
Python
#!/usr/bin/python
|
|
|
|
import time
|
|
import datetime
|
|
import ADS1256
|
|
import RPi.GPIO as GPIO
|
|
import redis
|
|
|
|
#configurazione per lavorare su server redis locale
|
|
REDIS_PORT = 6379
|
|
REDIS_HOST = '127.0.0.1'
|
|
redSrv0 = redis.Redis(
|
|
host=REDIS_HOST,
|
|
port=REDIS_PORT,
|
|
db=0)
|
|
|
|
redSrv0.set('SETTINGS:LOG:STATUS',0)
|
|
|
|
#se non c'è un valore nei campi di redis, popolo il db
|
|
|
|
LOG_path = "/home/pi/Flythis/logger/log/"
|
|
DATA_path = "/home/pi/Flythis/logger/data/"
|
|
|
|
if redSrv0.get('SETTINGS:SELECTED_CH') is None:
|
|
redSrv0.set('SETTINGS:SELECTED_CH',0)
|
|
|
|
for numCh in range(0,8,+1):
|
|
|
|
if redSrv0.get('SETTINGS:IN:MAX:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:IN:MAX:'+str(numCh),100)
|
|
|
|
if redSrv0.get('SETTINGS:IN:MIN:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:IN:MIN:'+str(numCh),0)
|
|
|
|
if redSrv0.get('SETTINGS:OUT:MAX:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:OUT:MAX:'+str(numCh),100)
|
|
|
|
if redSrv0.get('SETTINGS:OUT:MIN:'+str(numCh)) is None:
|
|
redSrv0.set('SETTINGS:OUT:MIN:'+str(numCh),0)
|
|
|
|
#intervallo in millisecondi fra un campionamento e il successivo
|
|
redSampleFreq = 'SETTINGS:LOG:FREQ'
|
|
startingFreq = 1000
|
|
redSrv0.set(redSampleFreq,startingFreq)
|
|
|
|
try:
|
|
CH = ADS1256.ADS1256()
|
|
CH.ADS1256_init()
|
|
|
|
#funzione stampa otto valori letti su terminale
|
|
def videoPrint(CHvalue):
|
|
for chIndex in range(0,8,+1):
|
|
print("CH "+str(chIndex)+"= %lf"%(CHvalue[chIndex]*5.0/0x7fffff))
|
|
|
|
#funzione salva time+data e otto valori letti su file
|
|
def fileSave(CHvalue):
|
|
try:
|
|
logFile = open(LOG_path, "a")
|
|
except FileNotFoundError:
|
|
print("Creating new PyLog.txt")
|
|
logFile = open(LOG_path, "w+")
|
|
logFile.write("\nTime: "+str(now))
|
|
for chIndex in range(0,8,+1):
|
|
logFile.write(" | CH "+str(chIndex)+" = %lf"%(CHvalue[chIndex]*5.0/0x7fffff))
|
|
|
|
#funzione salva time+data di log e otto valori su redis
|
|
def redisSave(CHvalue):
|
|
redLastLog = 'RTDATA:TIME:LOG'
|
|
lastLog = datetime.datetime.now()
|
|
redSrv0.set(redLastLog,str(lastLog))
|
|
for chIndex in range(0,8,+1):
|
|
redAreaIn = 'RTDATA:CH:'+str(chIndex)
|
|
redAreaOut = 'RTDATA:OUT:'+str(chIndex)
|
|
strValIn = "%.4f" % (CHvalue[chIndex]*5.0/0x7fffff)
|
|
redSrv0.set(redAreaIn,strValIn)
|
|
# salvo i valori scalati in OUT
|
|
strValOut = "%.4f" % scaleVal(CHvalue[chIndex]*5.0/0x7fffff, chIndex)
|
|
redSrv0.set(redAreaOut,strValOut)
|
|
|
|
#funzione di refresh valori scalati
|
|
def scaleVal(inValue, chIndex):
|
|
#rileggo da redis i valori min/max secondo chIndex
|
|
chInMin = float(0)
|
|
if (redSrv0.get('SETTINGS:IN:MIN:'+str(chIndex)) != 0):
|
|
chInMin = float(redSrv0.get('SETTINGS:IN:MIN:'+str(chIndex)))
|
|
|
|
chInMax = float(3.3)
|
|
if (redSrv0.get('SETTINGS:IN:MAX:'+str(chIndex)) != 100):
|
|
chInMax = float(redSrv0.get('SETTINGS:IN:MAX:'+str(chIndex)))
|
|
|
|
chOutMin = float(0)
|
|
if (redSrv0.get('SETTINGS:OUT:MIN:'+str(chIndex)) != 0):
|
|
chOutMin = float(redSrv0.get('SETTINGS:OUT:MIN:'+str(chIndex)))
|
|
|
|
chOutMax = float(1000)
|
|
if (redSrv0.get('SETTINGS:OUT:MAX:'+str(chIndex)) != 100):
|
|
chOutMax = float(redSrv0.get('SETTINGS:OUT:MAX:'+str(chIndex)))
|
|
|
|
# check denom zero
|
|
deltaOut = (chOutMax - chOutMin)
|
|
deltaIn = (chInMax - chInMin)
|
|
if(deltaIn==0):
|
|
deltaIn = 1
|
|
|
|
# calcolo scalato
|
|
outVal = chOutMin + ((inValue-chInMin) * (deltaOut / deltaIn))
|
|
|
|
return outVal
|
|
|
|
#ciclo principale, salva time attuale e se LOG:STATUS è 1 fa il ciclo principale
|
|
while(1):
|
|
redTime = 'RTDATA:TIME:SRV'
|
|
now = datetime.datetime.now()
|
|
redSrv0.set(redTime,str(now))
|
|
#print(logStatus)
|
|
# solo se su redis LOG è 1 eseguo il ciclo principale
|
|
#logStatus = redSrv0.get('SETTINGS:LOG:STATUS').decode('utf-8')
|
|
#if logStatus == "1" :
|
|
CHvalue = CH.ADS1256_GetAll()
|
|
#videoPrint(CHvalue)
|
|
#fileSave(CHvalue)
|
|
redisSave(CHvalue)
|
|
time.sleep(int(redSrv0.get(redSampleFreq))/1000)
|
|
|
|
#eccezione da ctrl+c in terminale e chiusura
|
|
except KeyboardInterrupt:
|
|
GPIO.cleanup()
|
|
print (str(now)+" Program End. Ctrl+C from user\r\n")
|
|
try:
|
|
logFile = open(LOG_path+"PyLog.txt", "a")
|
|
except FileNotFoundError:
|
|
print("Creating new PyLog.txt")
|
|
logFile = open(LOG_path+"PyLog.txt", "w+")
|
|
logFile.write(str(now)+" Program end. Ctrl+C from user\r\n")
|
|
logFile.close()
|
|
exit()
|
|
|
|
#eccezione da errore e chiusura
|
|
except Exception as errorMessage:
|
|
GPIO.cleanup()
|
|
print(str(now)+" Caught error " + str(errorMessage)+"\r\n")
|
|
try:
|
|
logFile = open(LOG_path+"PyLog.txt", "a")
|
|
except FileNotFoundError:
|
|
print("Creating new PyLog.txt")
|
|
logFile = open(LOG_path+"PyLog.txt", "w+")
|
|
logFile.write(str(now)+" Caught error: "+str(errorMessage)+"\r\n")
|
|
logFile.close()
|
|
exit() |