How to have counter reset with respect to when a value changed status?

16 visualizaciones (últimos 30 días)
Hello,
I apologize for the odd or vaguely phrased question. I'm not sure quite how to ask this.
I will say what I'm trying to model overall, and then what I'm specifically trying to do.
I am building a battery powered vehicle in Simulink. It has an electric motor, a battery, and is capable of regenerative braking. I have started out with the model in very simple terms and slowly added layers of complexity to simulate the real environment.
Now I am trying to simulate realistic battery characteristics. This includes 4 new parameters: 1) charging or 2) discharging, with each state having 2 sets of values: 3) 2-second duration, and 4) 10-second duration.
In other words, if the car uses regenerative braking and produces positive current, putting the battery in a charging state, the battery will have X value for resistance for 2-seconds, then the resistance increases rapidly. The same is true for discharging when the car wants to accelerate and draw negative current.
I believe I need a sort of counter. I have tried to explore this sort of thing extensively. I have explored the "Counter Free-Running" and "Counter Limited" blocks as well as trying to use the regular "Clock" and "Digital Clock" hoping one of them would reset upon a block system doing a cycle. It seems I cannot have my way, and the closest I've come is using the Counter Limited with the upper limit set to 2.
The issue is that the counters and clocks move independently. They have no 'trigger' input - only their time-step outputs. I can use "Compare to Zero" blocks to wired to Switches to alternate between charging/discharging status (when current is +/- 0), and I can easily make the switch (using Counter Limited block) to happen at 2 seconds, but the issue is resetting the timer dependent on this status switch (sign change).
How the vehicle switches between accel/decel (negative/positive current) is not random (at least in my model supplied with pre-set driver data), but it is not predictable because it's a ~600 second data cycle with many, many changes in acceleration demand - aka current sign changes.
So the concluding question is - How do I get a timer to reset based on a condition being true/false? For example, car demands acceleration for 2 seconds. No problem. T(0,2)= 2-sec values. And T(2, inf)=10-sec values. Easy switch at 2-sec.
BUT what if it goes like - T(0,0.5)=accel, T(0.5,1.0)=decel, T(1.0,4.0)=accel.
As far as I understand the Counter blocks, upon the return to accel (at 1.0 seconds), would not reset, and it would resume from 1-->2 seconds, therefore switching from 2-sec values to 10-sec values after only 1 second, not the desired 2 seconds.
I have had ideas such as defining the 'Upper Limit' of the Counter Limited block to be a trigger. Such as every time a 'Compare to Zero' event happened, I would run it to a "To Workspace" block, and use the name of that block as the Upper Limit. So right when it generated a value, the Counter Limited would reset. Simulink does not seem to like this.
I have also tried to look at Triggers, Trigger Subsystems, Counter block, and plenty others. They all seem useful in their own right, but the issue still remains getting a sort of clock/counter to reset variably. I can't find a way to reference time itself in Simulink.
I suppose in MATLAB code, you might have a sort of nested loop that says "if current>0, then use 2-sec values; i=0; i= i+1; when i>2, use 10-sec values;" and a matching loop for current<0. But also also awful at Script writing, so who knows
Sorry for the long answer. I just wanted to be clear as I was having a lot of trouble finding something that fit my needs, and somehow even more trouble putting what I needed into words.

Respuestas (0)

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by