masfaraud / BMSpy

Python Block-Model Simulator. An alternative to simulink in python.
GNU Lesser General Public License v3.0
222 stars 36 forks source link

Delay block? #18

Closed pjadzinsky closed 5 years ago

pjadzinsky commented 5 years ago

Hi, I just discovered BMS and I'm trying to figure out if this (which looks great) will be useful to me as is. I would need a 'delay' block. For example if I have signal A and B, I want to sum them but the 'sum' is actually summarizing a real process that actually tokes some time (is not like adding two electrical currents but actually doing some work). Therefore I would like to 'delay' the sum.

As I see it one option is to add a 'delay' block. Another approach is to just add a 'delay' option to some basic blocks (which might default to 0). What are your ideas? Maybe I'm missing something and this is already implemented? If not, if you suggest best approach that fits your philosophy I might be able to submit a PR.

Thanks

masfaraud commented 5 years ago

Hi,

I think the best idea is to implement a delay block with one input and one output. First you need to create your DelayBlock by subclassing the Block class, you will need to redefine some methods (see some examples https://github.com/masfaraud/BMSpy/blob/master/bms/blocks/continuous.py), in particular the Evaluate method that give output values of the block at a given iteration it with timestep ts. In the Gain block, the Evaluate method calls the InputValues method. You should not use it for your block because it will give you only a fixed number of previous values of the input variable. You should take them manually in the attribute _values of your input variable. Maybe you'll miss a method in the Variable to have the value of a variable at a given time (an interpolation of the _values attribute vector). I have implemented it in DynamicSystem.VariableValues, there is maybe something to rethink there.

Don't hesitate if you have further questions. I am waiting for the PR then :)

masfaraud commented 5 years ago

Merge the PR #19