Explorar el Código

Create initial cbd system

Arkadiusz Ryś hace 2 años
padre
commit
cab80f9b33
Se han modificado 6 ficheros con 91 adiciones y 2 borrados
  1. 6 0
      README.md
  2. 1 1
      damper/__init__.py
  3. 3 0
      damper/__main__.py
  4. 79 0
      damper/system.py
  5. BIN
      docs/trace.png
  6. 2 1
      requirements.txt

+ 6 - 0
README.md

@@ -2,3 +2,9 @@
 
 
 A causal block diagram representation of a spring mass damper.
 A causal block diagram representation of a spring mass damper.
 Simulated using [a causal block simulator](https://git.rys.one/dtdesign/cbd).
 Simulated using [a causal block simulator](https://git.rys.one/dtdesign/cbd).
+
+The formula we follow to achieve this is: `0 = 100 - 2y' - 100y - y"`.
+Here the friction coefficient is `0.02 Ns/m` and the spring constant is `1 N/m` with the block weighing in at `0.01 kg`.
+
+The simulated position (distance) of the spring is described by the following graph.
+![execution-trace](docs/trace.png)

+ 1 - 1
damper/__init__.py

@@ -1,3 +1,3 @@
-"""Simulate CBD models in Python."""
+"""A causal block diagram representation of a spring mass damper system."""
 __version__ = "0.0.1"
 __version__ = "0.0.1"
 __version_info__ = tuple((int(num) if num.isdigit() else num for num in __version__.replace("-", ".", 1).split(".")))
 __version_info__ = tuple((int(num) if num.isdigit() else num for num in __version__.replace("-", ".", 1).split(".")))

+ 3 - 0
damper/__main__.py

@@ -0,0 +1,3 @@
+from damper.system import run_experiment
+
+run_experiment()

+ 79 - 0
damper/system.py

@@ -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()

BIN
docs/trace.png


+ 2 - 1
requirements.txt

@@ -1,5 +1,6 @@
 # Damper
 # Damper
-#causal #maybe cbd
+# cbd ~= 1.6.0
+matplotlib ~= 3.7.1
 # Test
 # Test
 pytest ~= 7.3.1
 pytest ~= 7.3.1
 # Doc
 # Doc