top of page
Search

Practical use case: Linear ramp function

  • Writer: Ivaylo Fiziev
    Ivaylo Fiziev
  • 20 hours ago
  • 2 min read

From the theory: "A linear ramp function generates a signal that linearly increases or decreases from a starting value to a target value over a specific time. It smooths out sharp changes in signals to prevent mechanical stress on machines."

There are a lot of examples on the internet but here I'll present one that works well in the context of Process Simulate.

Its called a function but rather I'll create a function block since the implementation keeps track of the current value of the ramp. The FB takes the start value, the target value, the ramping time as well as the enable/reset flags on input.

On output it returns the ramp value.

This is how it looks:

FUNCTION_BLOCK "LinearRamp"
VAR_INPUT
    StartValue: LREAL;    // Initial value 
    TargetValue : LREAL;  // Target value	
    RampTime    : LREAL;  // Time for the ramp to complete (sec.)
    Enable      : BOOL;   // Enable the ramp
    Reset       : BOOL;   // Reset ramp
END_VAR
VAR_OUTPUT
    RampValue   : LREAL;   // Output value
END_VAR
VAR
    RampStep    : LREAL;   // Incremental step
    InternalValue : LREAL; // Internal memory
	trig : R_TRIG;
END_VAR
BEGIN
IF #Enable THEN
    // Calculate step
    #RampStep := (#TargetValue - #StartValue) / ((#RampTime * 1000) / LOGIC_UPDATE_RATE());
	// Reset ramp ?
	#trig(CLK:=#Reset);
	IF #trig.Q THEN
		#InternalValue := #StartValue;
	END_IF;
    // Do a step
    IF ABS(#TargetValue - #InternalValue) > ABS(#RampStep) THEN        		#InternalValue := #InternalValue + #RampStep;
    ELSE
        #InternalValue := #TargetValue;
    END_IF;
END_IF;
#RampValue := #InternalValue;
END_FUNCTION_BLOCK

As always: save this to LinearRamp.scl text file, import it (Import SCL block command) or put it in the SCL_PATH (Modifying the SCL search path). Then call it like any other FB in the SCL editor.

FUNCTION_BLOCK "MAIN"
VERSION:1.0
VAR_INPUT
	reset : BOOL;
	target : LREAL;
	initial : LREAL;
END_VAR
VAR_OUTPUT
	out : LREAL;
END_VAR
VAR
	ramp : "LinearRamp";
END_VAR
BEGIN

#ramp(Enable:=true,		// enable the ramp
	RampTime:= 5,			// 5 sec. ramp
	RampValue=>#out,		// 
	TargetValue:=#target,	// target value
	StartValue:=#initial,	// initial value
	Reset:=#reset);		// reset flag

END_FUNCTION_BLOCK

Paste this in the SCL editor, connect the signals and observe how it behaves.

Eventually you'll get a ramp like this one:


When you reset, the ramp starts from the StartValue once again. Values can be both positive and negative.

I think It is about time to introduce charts for SCL variables. Maybe the debugger will be a good place for these. Just like the watch window but with graphics... It will be a lot easier to monitor the values this way.



 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Subscribe Form

Thanks for submitting!

bottom of page