Structured Text Formula

MDG1

Member
Join Date
Mar 2018
Location
Halton, ON
Posts
2
Hello all,

I'm stuck on some code I want to write. The idea is to have a formula that accepts a DINT tag setpoint, compares it to the previous value, and uses the ABS difference between them to create an increment value so that the Current_Value = New_Value over 30 seconds.


IF Current_Value <> New_Value THEN

Difference:= Current_Value - New_Value;
Absolute_Difference:= ABS(Difference);
Ramp_Value:= Absolute_Difference / 30;

Ramp_Timer.PRE:= 1000;
Ramp_Timer.TimerEnable:= 1;
Ramp_Timer.Reset:= Ramp_Timer.DN;

IF Ramp_Timer.DN AND New_Speed > Current_Speed THEN

Current_Value:= Current_Value + Ramp_Value;

ELSIF Ramp_Timer.DN AND New_Speed < Current_Speed THEN

Current_Value:= Current_Value - Ramp_Value;
END_IF;


END_IF;

This almost does what I want but my problem is the Absolute_Difference changes with every increment, when I don't want it to. Is there a way I can 'capture' a constant initial value for the Absolute_Difference? Nothing I've tried has worked, but it feels like I'm missing something simple here, any ideas would be appreciated.

Thank you.
 
So you only want it to be checked every thirty seconds?


Run your comparison code inside a while loop or something similar. Have that loop run if the a counter is zero, increment the counter at the end of the first pass and the loop will stop. Reset the counter once the ramp time has elapsed.


Your ramp code would have to be outside of this loop obviously or the ramp won't happen.
 
You could just set a 'RampInProgress' bit once a new differential is found and you start the timer. Use this to prevent checking for another differential. Once the timer is done, reset the bit.
 
The "simple" fix is to one-shot your Difference calculation line. Doing so will prevent the Difference, Absolute_Difference, and Ramp_Value tags from changing until Current_Value = New_Value again at the end of the ramp.

But since ST doesn't have an ONS instruction, the implementation is less simple. Perhaps something like:

Code:
IF Current_Value <> New_Value AND NOT ONS_bit THEN
     Difference:= Current_Value - New_Value;
END_IF;

ONS_bit := Current_Value <> New_Value

IF Current_Value <> New_Value THEN	

     Absolute_Difference:= ABS(Difference); 
     Ramp_Value:= Absolute_Difference / 30;
etc....

(Note: untested code. Probably has a bug)
 
Last edited:
A couple things. You don’t need the absolute function, you can add a negative to get the result you want:

IF Current_Value <> New_Value THEN

If Ramp_Value = 0 THEN
Difference:= New_Value - Current_Value;
Ramp_Value:= Difference / 30;
END_IF;

Ramp_Timer.PRE:= 1000;
Ramp_Timer.TimerEnable:= 1;
Ramp_Timer.Reset:= Ramp_Timer.DN;

IF Ramp_Timer.DN THEN

Current_Value:= Current_Value + Ramp_Value;

If Current_Value = New_Value THEN

Ramp_Value = 0;

END_IF;

END_IF;


END_IF;

Somewhere in your first scan logic you will want to ensure Ramp_Value is set to zero, and Maybe some other logic to make sure it gets reset. Also, You might have to change the current value = New value to allow for some error, say plus or minus a bit.

I’m doing this on my phone, please forgive typos...
 
I think you probably need to use a bit to signify the ramp sequence being in process. Once the sequence has finished then you enable it to be started again. Alternatively you could look at the new value vs current value and every time they don't match provide a oneshot to restart the sequence.
 
You don't say what PLC you are trying to use, but it looks like RSLogix 5000, so I'll go with that. Since any ideas are appreciated.
1) Change from using structure text and go with ladder. You are using IF THENs. But since you aren't using ELSEs, it is much clearer and easier to follow when you run the test, and execute to logic. This is the type of logic structure where ladder is easier to see what is going on, and is there easier to program and troubleshoot.

2) The logic you showed looks like you are trying to ramp exponentially with updates every second. If this is what you really want to, put the following logic under a task that executes once per second.
CPT(Current_Value,(Current_Value+New_Value)/30))
That's all the logic you need.

3) The logic that you say you want is to ramp it up over 30 seconds. In this case, since the ladder doesn't have a ramping instruction, you could use function blocks and use the RMPS instruction.

4) If you don't want to or can't use function blocks, then use ladder. In your example code, you used a timer to trigger the addition, and seem to want to add every time it triggers. If this is the case, your method may run into problems, as you seem to be wanting to add a 30th at a time to an initial value until it reaches the target value. However, due to loss of accuracy, dividing a number by 30, then adding the result 30 times may not add up to the initial number. It could be off by a very small number. So don't assume that after 30 additions your Current_Value will equal the New_Value. It may continue adding past your target value! So I would use a counter explicitly stop after the 30 additions.

Here's the ladder that I would start with:

NEQ(New_Value,Old_Value)CPT(RAMP,(New_Value - Current_Value)/30)MOV(New_Value,Old_Value)RES(RampCount);
XIO(RampCount.DN)CTU(RampCount,?,?)ADD(RAMP,Current_Value,Current_Value)OTU(RampCount.CU);
XIC(RampCount.DN)MOV(New_Value,Current_Value)

This would run in routine executing every second. Otherwise, you will need to add timer logic which unnecessarily complicates things.
 

Similar Topics

Does anybody have a Scaler Formula they would share? Thanks
Replies
11
Views
4,291
Good morning. I'm doing a rehab and I need to recycle some part of the old code that won't change and that I need. This is a calculation that...
Replies
22
Views
1,170
I'm writing some structured text that's handling a data structure that comes from a PC. The PC structure is in the "new" LREAL 64-bit floating...
Replies
3
Views
440
Trying to put these equations in structured text program in a Micro 830. when I run the verify I get this error. ;:expected before this new...
Replies
4
Views
395
Hey all, Studio v20.05 Background. We are piggy backing another system to record attributes for product travelling down the line and several...
Replies
21
Views
3,422
Back
Top Bottom