diff --git a/resources/data_model.fboot b/resources/data_model.fboot index 298c708..8b13789 100644 --- a/resources/data_model.fboot +++ b/resources/data_model.fboot @@ -1,10 +1 @@ -; -DIN1; -DIN1; -DIN1; -DIN1; -DIN1; -DIN1; -DIN1; -DIN1; -DIN1; + diff --git a/resources/function_blocks/DATA_SIMULATOR.py b/resources/function_blocks/DATA_SIMULATOR.py deleted file mode 100644 index fdf10e4..0000000 --- a/resources/function_blocks/DATA_SIMULATOR.py +++ /dev/null @@ -1,33 +0,0 @@ -from scipy.stats import bernoulli -import numpy as np -import time - - -class DATA_SIMULATOR: - - def __init__(self): - pass - - def schedule(self, event_name, event_value, params_str, ratio, delay): - if event_name == 'INIT': - # doesn't need initialization - return [event_value, None, ''] - - elif event_name == 'READ': - params = list(eval(params_str)) - # generates the values - anom = bernoulli.rvs(ratio) - sample = [] - for norm_param, anom_param in params: - # checks if an anomaly to generate - mu, std = norm_param if anom == 0 else anom_param - # generates the value - value = round(np.random.normal(mu, std), 3) - # stores the value - sample.append(value) - # appends the anom at the end and converts it to string - sample.append(anom) - data_str = ','.join(['{0}'.format(x) for x in sample]) - # wait some time - time.sleep(delay) - return [None, event_value, data_str] diff --git a/resources/function_blocks/DATA_SIMULATOR.fbt b/resources/function_blocks/MACHINE_SIMULATOR.fbt similarity index 53% rename from resources/function_blocks/DATA_SIMULATOR.fbt rename to resources/function_blocks/MACHINE_SIMULATOR.fbt index 9e85b44..092fb93 100644 --- a/resources/function_blocks/DATA_SIMULATOR.fbt +++ b/resources/function_blocks/MACHINE_SIMULATOR.fbt @@ -1,24 +1,30 @@ - + + + - - - + + + + + + + diff --git a/resources/function_blocks/MACHINE_SIMULATOR.py b/resources/function_blocks/MACHINE_SIMULATOR.py new file mode 100644 index 0000000..77e9005 --- /dev/null +++ b/resources/function_blocks/MACHINE_SIMULATOR.py @@ -0,0 +1,112 @@ +import numpy as np +import time + + +class MACHINE_SIMULATOR: + + def __init__(self): + self.state = 'IDLE' + self.prev_state = 'WORK' + self.ttf = 0 + self.tts = 0 + self.ttr = 0 + self.mtta = 0 + + def schedule(self, event_name, event_value, params_data, lim_anom, params_mtbf, params_mtts, params_mttr, delay): + if event_name == 'INIT': + # init the states + self.state = 'IDLE' + self.prev_state = 'WORK' + # computes (gets from the distribution) the ttf + self.ttf = self.generate_time(params_mtbf) + self.mtta = self.ttf*lim_anom + return [event_value, None, '', self.state] + + elif event_name == 'READ': + # checks the state + if self.state == 'IDLE': + time.sleep(delay) + return [None, None, '', self.state] + + elif self.state == 'WORK': + # wait some time (performs the work) + time.sleep(delay) + # computes (decreases) the current TTF + self.ttf -= delay + # checks if is 0 -> failure + if self.ttf <= 0: + # computes (gets from the distribution) the tts (time to start the repair) + self.tts = self.generate_time(params_mtts) + self.state = 'BREAK' + return [None, event_value, '', self.state] + # normal operation + else: + # generates data + anom = 1 if self.ttf <= self.mtta else 0 + data_str = self.generate_data(params_data, anom) + return [None, event_value, data_str, self.state] + + elif self.state == 'BREAK': + # simulates the waiting time to repair + time.sleep(delay) + # computes (decreases) the current TTF + self.tts -= delay + # checks if is 0 -> starts repairing + if self.tts <= 0: + # computes (gets from the distribution) the ttr (time to repair) + self.ttr = self.generate_time(params_mttr) + self.state = 'REPAIR' + return [None, event_value, '', self.state] + # break state + else: + return [None, event_value, '', self.state] + + elif self.state == 'REPAIR': + # simulates the waiting time to repair + time.sleep(delay) + # computes (decreases) the current TTF + self.ttr -= delay + # checks if is 0 -> starts repairing + if self.ttr <= 0: + # computes (gets from the distribution) the ttr (time to repair) + self.ttf = self.generate_time(params_mtbf) + self.mtta = self.ttf * lim_anom + self.state = 'WORK' + return [None, event_value, '', self.state] + # break state + else: + return [None, event_value, '', self.state] + + elif event_name == 'ON-OFF': + # switches the button + if self.state == 'IDLE': + # updates the data + self.state = self.prev_state + return [None, event_value, '', self.state] + else: + self.prev_state = self.state + self.state = 'IDLE' + return [None, None, '', self.state] + + @staticmethod + def generate_data(params_str, anom): + params = list(eval(params_str)) + # generates the values + sample = [] + for norm_param, anom_param in params: + # checks if an anomaly to generate + mu, std = norm_param if anom == 0 else anom_param + # generates the value + value = round(np.random.normal(mu, std), 3) + # stores the value + sample.append(value) + # appends the anom at the end and converts it to string + sample.append(anom) + data_str = ','.join(['{0}'.format(x) for x in sample]) + return data_str + + @staticmethod + def generate_time(params_str): + params = list(eval(params_str)) + t = round(np.random.normal(params[0], params[1])) + return t diff --git a/resources/function_blocks/PROD_MAN.fbt b/resources/function_blocks/PROD_MAN.fbt new file mode 100644 index 0000000..159cf07 --- /dev/null +++ b/resources/function_blocks/PROD_MAN.fbt @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/function_blocks/PROD_MAN.py b/resources/function_blocks/PROD_MAN.py new file mode 100644 index 0000000..40731c5 --- /dev/null +++ b/resources/function_blocks/PROD_MAN.py @@ -0,0 +1,22 @@ + + +class PROD_MAN: + + def __init__(self): + self.state = 'IDLE' + + def schedule(self, event_name, event_value): + + if event_name == 'INIT': + self.state = 'IDLE' + return [event_value, None, 'IDLE'] + + elif event_name == 'ON-OFF': + # switches the button + if self.state == 'IDLE': + # updates the data + self.state = 'WORK' + return [None, event_value, self.state] + else: + self.state = 'IDLE' + return [None, event_value, self.state] diff --git a/resources/function_blocks/WRITE_CSV.py b/resources/function_blocks/WRITE_CSV.py index a8620d9..b805284 100644 --- a/resources/function_blocks/WRITE_CSV.py +++ b/resources/function_blocks/WRITE_CSV.py @@ -18,7 +18,7 @@ def schedule(self, event_input_name, event_input_value, data, path): if data is None: return [None, event_input_value, "No data received"] - print("Data on write CSV:", data) + # print("Data on write CSV:", data) with open(path, "a+") as f: f.write(data)