Skip to content

Commit

Permalink
creation dinasore-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseu31 committed Feb 22, 2023
1 parent 8fe159c commit 027dccd
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 48 deletions.
11 changes: 1 addition & 10 deletions resources/data_model.fboot
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
;<Request ID="1" Action="CREATE"><FB Name="DIN1" Type="EMB_RES" /></Request>
DIN1;<Request ID="2" Action="CREATE"><FB Name="WRITE_CSV" Type="WRITE_CSV" /></Request>
DIN1;<Request ID="3" Action="WRITE"><Connection Source="/home/eliseu/PycharmProjects/db.csv" Destination="WRITE_CSV.PATH" /></Request>
DIN1;<Request ID="4" Action="CREATE"><FB Name="DATA_SIMULATOR" Type="DATA_SIMULATOR" /></Request>
DIN1;<Request ID="5" Action="WRITE"><Connection Source="[((15, 0.4), (10, 0.4)), ((19, 0.1), (16, 0.1))]" Destination="DATA_SIMULATOR.PARAMS" /></Request>
DIN1;<Request ID="6" Action="WRITE"><Connection Source="1" Destination="DATA_SIMULATOR.RATIO" /></Request>
DIN1;<Request ID="7" Action="WRITE"><Connection Source="1" Destination="DATA_SIMULATOR.DELAY" /></Request>
DIN1;<Request ID="8" Action="CREATE"><Connection Source="DATA_SIMULATOR.READ_O" Destination="WRITE_CSV.RUN" /></Request>
DIN1;<Request ID="9" Action="CREATE"><Connection Source="DATA_SIMULATOR.DATA" Destination="WRITE_CSV.DATA" /></Request>
DIN1;<Request ID="10" Action="START"/>

33 changes: 0 additions & 33 deletions resources/function_blocks/DATA_SIMULATOR.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE FBType SYSTEM "http://www.holobloc.com/xml/LibraryElement.dtd">
<FBType Name="DATA_SIMULATOR" OpcUa="DEVICE.SENSOR">
<FBType Name="MACHINE_SIMULATOR" OpcUa="DEVICE.SENSOR">
<InterfaceList>
<EventInputs>
<Event Name="INIT" Type="Event"/>
<Event Name="READ" Type="Event"/>
<Event Name="ON-OFF" Type="Event"/>
</EventInputs>
<EventOutputs>
<Event Name="INIT_O" Type="Event"/>
<Event Name="READ_O" Type="Event">
<With Var="DATA"/>
<With Var="STATE"/>
</Event>
</EventOutputs>
<InputVars>
<VarDeclaration Name="PARAMS" Type="STRING"/>
<VarDeclaration Name="RATIO" Type="REAL"/>
<VarDeclaration Name="DELAY" Type="INT"/>
<VarDeclaration Name="PARAMS_DATA" Type="STRING"/>
<VarDeclaration Name="LIM_ANOM" Type="REAL"/>
<VarDeclaration Name="PARAMS_MTBF" Type="STRING"/>
<VarDeclaration Name="PARAMS_MTTS" Type="STRING"/>
<VarDeclaration Name="PARAMS_MTTR" Type="STRING"/>
<VarDeclaration Name="DELAY" Type="REAL"/>
</InputVars>
<OutputVars>
<VarDeclaration Name="DATA" Type="STRING"/>
<VarDeclaration Name="STATE" Type="STRING"/>
</OutputVars>
</InterfaceList>
</FBType>
112 changes: 112 additions & 0 deletions resources/function_blocks/MACHINE_SIMULATOR.py
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions resources/function_blocks/PROD_MAN.fbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE FBType SYSTEM "http://www.holobloc.com/xml/LibraryElement.dtd">
<FBType Name="PROD_MAN" OpcUa="SERVICE">
<InterfaceList>
<EventInputs>
<Event Name="INIT" Type="Event"/>
<Event Name="ON-OFF" Type="Event">
</Event>
</EventInputs>
<EventOutputs>
<Event Name="INIT_O" Type="Event"/>
<Event Name="ON-OFF_O" Type="Event">
<With Var="STATE"/>
</Event>
</EventOutputs>
<InputVars>
</InputVars>
<OutputVars>
<VarDeclaration Name="STATE" Type="STRING"/>
</OutputVars>
</InterfaceList>
</FBType>
22 changes: 22 additions & 0 deletions resources/function_blocks/PROD_MAN.py
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion resources/function_blocks/WRITE_CSV.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 027dccd

Please sign in to comment.