-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigitaltwin.py
210 lines (161 loc) · 6.91 KB
/
digitaltwin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from datetime import datetime
import random
import time
import sys
from pprint import pprint
# Aktuatoren simulieren die Signale des Steuergeräts (MController) und führen Aktionen mit dem Werkstück aus
# input meistens = Werkstück
class RotaryActuator:
def run(self, input, instruction):
workpiece_weight = input.weight
workpiece_height = input.height
workpiece_weight = workpiece_weight - instruction * 2
workpiece_height = workpiece_height - instruction * 1
output = {"workpiece_height": workpiece_height, "workpiece_weight": workpiece_weight}
return output
class PositioningActuator:
def run(self, input, instruction):
output = {}
if instruction == "clamp_workpiece":
output["workpiece_position"] = "fixed"
return output
elif instruction == "release_workpiece":
output["workpiece_position"] = "loosely"
return output
# Aufgaben, für die Parameter definiert werden können
class MTask():
tasks = {"clamp_workpiece" : {"time": 4, "requirements": {"actuator":"PositioningActuator"}},
"rotate": {"time": 4, "requirements": {"actuator":"RotaryActuator"}, "transitions": {"position":"fixed"}},
"release_workpiece":{"time": 4, "requirements": {"actuator": "PositioningActuator"}}}
def start(self):
now = datetime.now()
self.starttime = now.strftime("%H:%M:%S")
return self.starttime
def stop(self):
now = datetime.now()
self.stoptime = now.strftime("%H:%M:%S")
return self.stoptime
def checktransitions(self, workpiece):
if self.task_data.get("transitions") is not None:
transitions = self.task_data.get("transitions")
for key, value in transitions.items():
subject = getattr(workpiece, key)
if subject == value:
return True
else:
return False
else:
return True
def instruct(self, taskname, instructions):
task_data = MTask.tasks.get(taskname)
self.task = taskname
self.task_data = task_data
self.instructions = instructions
return self
class Workpiece():
def __init__(self):
self.type = "Grenztaster"
self.weight = 400
self.height = 20
self.position = "loosely"
pass
# Sendet die durch eine Aufgabe veränderten Daten (Werkstück, Aufgabe, Maschine) an den DataObserver, der die Notes im Namespace aktualisiert
class DataPublisher():
def __init__(self):
self._observers = []
def subscribe(self, observer):
self._observers.append(observer)
def dispatch(self, args):
for obs in self._observers:
obs.update(args)
def unsubscibe(self, observer):
self._observers.remove(observer)
# Ein Programm besteht aus x beliebigen Aufgaben, welche in der angegebenen Reihenfolge ausgeführt werden. Zudem können Anweisungen mitgegeben werden
class Program():
programs = {"simulation":
{"tasks": [
{"task": "clamp_workpiece"},
{"task": "rotate", "instruction": 23},
{"task":"rotate", "instruction": 44},
{"task": "release_workpiece"}]}}
def set(self, name):
program = Program.programs.get(name)
self.name = name
self.program = program
return self
# Maschine führt die Aufgaben aus und gibt das Ergebnis zurück
class Machine():
def __init__(self):
self.temp = 20
pass
def executetask(self, controller, task):
#if task.checktransitions(controller.workpiece) == True:
starttime = task.start()
time.sleep(task.task_data.get("time"))
output = getattr(self, task.task)(task, controller)
stoptime = task.stop()
output["task_starttime"] = starttime
output["task_stoptime"] = stoptime
output["task"] = task.task
return output
#else:
# output = {"task_status": "requirements not met"}
# return output
def clamp_workpiece(self, task, controller):
workpiece = self.processoutput_withactuator(task, controller.workpiece, "clamp_workpiece")
output = {"task_status": "finished", "workpiece_status": "unprocessed", "workpiece_height": 20, "workpiece_weight": 400}
output.update(workpiece)
return output
def release_workpiece(self, task, controller):
workpiece = self.processoutput_withactuator(task, controller.workpiece, "release_workpiece")
output = {"workpiece_status": "processed", "task_status": "finished"}
output.update(workpiece)
return output
def rotate(self, task, controller):
workpiece = self.processoutput_withactuator(task, controller.workpiece, task.instructions)
avaible_status = ["finished", "malfunction"]
status = random.choices(avaible_status,[0.9, 0.1])
temps = [56,60,61,55,66,71,80,50, 67, 74]
temp = random.choices(temps, [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1, 0.1])
output = {"task_status" : status[0], "machine_temp": temp}
output.update(workpiece)
return output
def processoutput_withactuator(self, task, workpiece, instructions):
required_actuator_name = task.task_data.get("requirements").get("actuator")
actuator = getattr(sys.modules[__name__], required_actuator_name)
output = actuator.run(actuator, workpiece, instructions)
return output
# Der Controller ist das Interface des digitalen Zwillings und stellt alle verfügbaren Methoden bereit
class MController():
def instructmanualtaskexecution(self, taskname, instructions):
task = MTask()
task = task.instruct(taskname, instructions)
output = self.machine.executetask(self, task)
self.dispatchoutputs(output)
return output
def setup(self, obs):
self.machine = Machine()
self.workpiece = Workpiece()
self.pub = DataPublisher()
self.pub.subscribe(obs)
return self
def startmachine(self):
self.pub.dispatch({"machine_status" : True})
pass
def stopmachine(self):
self.pub.dispatch({"machine_status": False})
pass
def dispatchoutputs(self, output):
self.pub.dispatch(output)
pass
def runprogram(self, programname):
program = Program()
program = program.set(programname)
self.taskloop(program)
pass
def taskloop(self, program):
tasklist = program.program.get("tasks")
for task in tasklist:
output = self.instructmanualtaskexecution(task.get("task"), task.get("instruction"))
output["program_name"] = program.name
self.dispatchoutputs(output)