|
@@ -0,0 +1,79 @@
|
|
|
|
|
+from cbd.core import CBD
|
|
|
|
|
+from cbd.simulator import Simulator
|
|
|
|
|
+from cbd.lib.std import ProductBlock, AdderBlock, IntegratorBlock, ConstantBlock
|
|
|
|
|
+import matplotlib.pyplot as plt
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class SpringMassDamper(CBD):
|
|
|
|
|
+ """
|
|
|
|
|
+ Causal Block Diagram of a Spring Mass Damper system.
|
|
|
|
|
+
|
|
|
|
|
+ ┌───────┐ ┌───────┐ ┌───────┐
|
|
|
|
|
+ ──|1|──(100)──>│ │ │ │ │ │
|
|
|
|
|
+ │ │ │ │ │ │
|
|
|
|
|
+ ┌─────>│ Σ ├────|y"|────>│ ∫ ├────|y'|────>│ ∫ ├────|y|────>
|
|
|
|
|
+ │ │ │ │ │ │ │ │ │
|
|
|
|
|
+ │ ┌──>│ │ │ │ │ │ │ │
|
|
|
|
|
+ │ │ └───────┘ └───────┘ │ └───────┘ │
|
|
|
|
|
+ │ │ │ │
|
|
|
|
|
+ │ └───────────────|-2y'|──────(-2)────────┘ │
|
|
|
|
|
+ │ │
|
|
|
|
|
+ └────────────────────────────────|-100y|────────────(-100)───────┘
|
|
|
|
|
+ """
|
|
|
|
|
+
|
|
|
|
|
+ def __init__(self, block_name: str):
|
|
|
|
|
+ CBD.__init__(self, block_name, output_ports=["y"])
|
|
|
|
|
+
|
|
|
|
|
+ # Start counting at 1 to keep consistency with the CBD library
|
|
|
|
|
+ blocks = [
|
|
|
|
|
+ ProductBlock("prod1"),
|
|
|
|
|
+ ProductBlock("prod2"),
|
|
|
|
|
+ ProductBlock("prod3"),
|
|
|
|
|
+ AdderBlock("sum1", numberOfInputs=3),
|
|
|
|
|
+ IntegratorBlock("int1"),
|
|
|
|
|
+ IntegratorBlock("int2"),
|
|
|
|
|
+ ConstantBlock("const1", 100),
|
|
|
|
|
+ ConstantBlock("const2", -2),
|
|
|
|
|
+ ConstantBlock("const3", -100),
|
|
|
|
|
+ ConstantBlock("const4", 0),
|
|
|
|
|
+ ConstantBlock("const5", 1),
|
|
|
|
|
+ ]
|
|
|
|
|
+ for block in blocks:
|
|
|
|
|
+ self.addBlock(block)
|
|
|
|
|
+
|
|
|
|
|
+ self.addConnection("const5", "prod1", output_port_name="OUT1", input_port_name="IN1")
|
|
|
|
|
+ self.addConnection("const1", "prod1", output_port_name="OUT1", input_port_name="IN2")
|
|
|
|
|
+ self.addConnection("prod1", "sum1", output_port_name="OUT1", input_port_name="IN1")
|
|
|
|
|
+ self.addConnection("sum1", "int1", output_port_name="OUT1", input_port_name="IN1")
|
|
|
|
|
+ self.addConnection("int1", "int2", output_port_name="OUT1", input_port_name="IN1")
|
|
|
|
|
+ self.addConnection("int1", "prod2", output_port_name="OUT1", input_port_name="IN1")
|
|
|
|
|
+ self.addConnection("const2", "prod2", output_port_name="OUT1", input_port_name="IN2")
|
|
|
|
|
+ self.addConnection("prod2", "sum1", output_port_name="OUT1", input_port_name="IN2")
|
|
|
|
|
+ self.addConnection("int2", "y", output_port_name="OUT1")
|
|
|
|
|
+ self.addConnection("int2", "prod3", output_port_name="OUT1", input_port_name="IN1")
|
|
|
|
|
+ self.addConnection("const3", "prod3", output_port_name="OUT1", input_port_name="IN2")
|
|
|
|
|
+ self.addConnection("prod3", "sum1", output_port_name="OUT1", input_port_name="IN3")
|
|
|
|
|
+ self.addConnection("const4", "int1", output_port_name="OUT1", input_port_name="IC")
|
|
|
|
|
+ self.addConnection("const4", "int2", output_port_name="OUT1", input_port_name="IC")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def run_experiment():
|
|
|
|
|
+ """Use the defined Spring Mass Damper to run the experiment."""
|
|
|
|
|
+ smd = SpringMassDamper("SpringMassDamper")
|
|
|
|
|
+ sim = Simulator(smd)
|
|
|
|
|
+ sim.setDeltaT(0.0000001)
|
|
|
|
|
+ sim.run(7.0)
|
|
|
|
|
+
|
|
|
|
|
+ time_values = []
|
|
|
|
|
+ y_values = []
|
|
|
|
|
+ for signal in smd.getSignals()["y"]:
|
|
|
|
|
+ time_values.append(signal.time)
|
|
|
|
|
+ y_values.append(signal.value)
|
|
|
|
|
+ fig, ax = plt.subplots(figsize=(5, 5), dpi=100)
|
|
|
|
|
+ ax.plot(time_values, y_values, label="Position", color=[1.00, 0.55, 0.22])
|
|
|
|
|
+ ax.legend()
|
|
|
|
|
+ plt.show()
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ run_experiment()
|