Store previous signal values in C-Script

Hi,

I want to implement C-Script that calculate my system performances in PLECS standalone.
I need to calculate the difference between my current signal value and previous ones. Is it possible to keep trace of previous signal data point inside block C? or at least one previous value.
i have seen the block " store custom state function" but i am not sure how to use it .

thank you in advance.

Yes it is possible. The idea of storing a previous value is discussed in the PLECS: Using the C-Script Block tutorial. Another example is in the MPPT controller C-Script in the “Single-Phase PV Inverter” demo model. Both of those should be straightforward to understand, however those examples are based on a Discrete Periodic sample time.

For a variable sample time, one could look at the “Pulse Delay” block implementation. Add the “Pulse Delay” block to your schematic and look under the mask. You’ll notice that memory management is not trivial.

If you’re not familiar with C or the PLECS C-Script, you may be better served by trying to implement your logic with basic PLECS blocks such as delays and other math blocks. If your model does not require the data to be processed while the simulation runs, it might also be more straightforward to perform your data analysis offline using a tool like Python or Matlab.

The StoreCustomState function is only relevant when starting a model from a stored state, not when running the simulation. From the PLECS Manual section on C-Scripts. “This function is called at the end of a simulation before the terminate function.”.

Thank you for your answer. If i understand well, we can only store one previous value, not a dynamic array that keeps storing new values in new cells.
I also tried to perform, “P=P+P_old” with P=P_old in update/start function to attempt to have a sum of my values but it didn’t work, could you suggest an alternative?
thank you in advance for your help.

Thank you for your answer. If i understand well, we can only store one previous value, not a dynamic array that keeps storing new values in new cells.

In the PLECS “State” you can only store one double value. You need to create your own data structure if you want to store multiple values. That is the approach taken in the “Pulse Delay” block. This requires some C knowledge and understanding of memory management.

You could also consider if a running sum or integral is appropriate, which you would reset over your averaging interval.

I also tried to perform, “P=P+P_old” with P=P_old in update/start function to attempt to have a sum of my values but it didn’t work, could you suggest an alternative?

It’s not clear what you intended here without more context such as an example model, as in the original script P is re-assigned in the Output function.

Hello Bryan,

Attached is my Plecs model. I tried to perform the follwing tasks:
Identify the instances where my signal changes direction.
Store the peak-to-peak value and the time period between these two instances of change.
Apply a specific loss formula for this portion of the signal.

So far, I haven’t encountered any particular issues with these tasks. I have successfully calculated the instantaneous loss contributions. However, I would like to define a variable to continuously sum these contributions. My intention is to update the loss variable using the formula: loss_old = loss + loss_contribution. with loss_old=loss in update/start function.
Unfortunately, this approach does not seem to be working.

example_cscript.plecs (17.7 KB)

I haven’t verified your overall logic, but I noted you haven’t assigned loss in the C-Script.

loss = loss_old;
...
loss+=loop_contribution;
out2=loss;