Due to hardware restrictions, I’m forced to read data from 2 digital proximity switches as analog values (0…1000mV). <~50mV is 0, >~900mV is 1.
I would like to log this data to our SQL database using historian. However I’m wondering what the most elegant solution is to achieve this. Ideally, I would like to avoid having a second set of tags (if possible).
Accquire data, threshold logic, log to MSSQL.
I’m currently logging the data as the analog value but this seems wasteful. I’d like to log this as a boolean on change of value.
By “threshold logic” I presume you mean hysteresis? That requires prior state when evaluating new samples. I’m biased towards the objectScript() expression function from my Simulation Aids module for such cases, using its state variable to hold prior output. Something like this:
def thresholdFunction(state, milliVolts):
if 'prior' in state:
if state['prior']:
result = milliVolts > 50
else:
result = milliVolts > 900
else:
result = milliVolts > 475
state['prior'] = result
return result