Analog and Digital Signal Handling in SCL
Kenneth Jensen
Automation, Integration, & Controls Engineer / Electrical Engineer / Power Coordinator / Drive Engineer
Introduction
Analog and digital signals are fundamental aspects of industrial controls and automation systems. Handling these signals effectively in Structured Control Language (SCL) is crucial for developing robust and efficient PLC programs. In this article, we'll explore the essentials of reading and writing analog and digital signals, along with practical examples and best practices.
Advice for Beginners in Signal Handling
Start with simple functions and develop a systematic approach to signal processing. Create a map or flowchart of how you will handle most signals to ensure consistency and reliability in your programs.
Reading Analog Input Signals in SCL
Reading analog input signals involves bringing in raw values, converting them to engineering units, and processing them for further use.
Common Pitfalls in Signal Processing
A common issue is not knowing the scaling of the analog device in the field. It's crucial to understand the scaling parameters so you can accurately scale your code to match the actual device behavior.
Accurate Analog Input Reading Strategies
To read analog input signals accurately:
Converting Analog Input Signals
When converting analog input signals for processing in PLCs:
Example: Reading an Analog Input
FUNCTION_BLOCK FB_ReadAnalogInput
VAR_INPUT
AnalogRaw : INT; // Raw analog input value
END_VAR
VAR_OUTPUT
AnalogEng : REAL; // Converted engineering value
END_VAR
VAR
EngMin : REAL := 0.0; // Minimum engineering value
EngMax : REAL := 100.0; // Maximum engineering value
RawMin : INT := 0; // Minimum raw value
RawMax : INT := 27648; // Maximum raw value (assuming 16-bit resolution)
END_VAR
BEGIN
// Convert raw value to engineering units
AnalogEng := (AnalogRaw - RawMin) * (EngMax - EngMin) / (RawMax - RawMin) + EngMin;
END_FUNCTION_BLOCK
In this example, AnalogRaw is the raw analog input value, and AnalogEng is the converted engineering value. The conversion formula scales the raw value to a meaningful engineering unit range.
Practical Tip: Bring in raw value, use engineering value for process value conversion, develop an averaging function, and implement the logic.
Writing Analog Output Signals in SCL
Writing analog output signals is similar to reading inputs but involves converting a processed value back to a raw format suitable for output.
Approach to Writing Analog Output Signals
Writing analog output signals involves:
Critical Role of Analog Output Signals
Analog output signals are essential for various control applications, such as:
Example: Writing an Analog Output
FUNCTION_BLOCK FB_WriteAnalogOutput
VAR_INPUT
AnalogEng : REAL; // Input engineering value
END_VAR
VAR_OUTPUT
AnalogRaw : INT; // Raw analog output value
END_VAR
VAR
EngMin : REAL := 0.0; // Minimum engineering value
EngMax : REAL := 100.0; // Maximum engineering value
RawMin : INT := 0; // Minimum raw value
RawMax : INT := 27648; // Maximum raw value (assuming 16-bit resolution)
END_VAR
BEGIN
// Convert engineering value to raw format
AnalogRaw := INT((AnalogEng - EngMin) * (RawMax - RawMin) / (EngMax - EngMin) + RawMin);
END_FUNCTION_BLOCK
In this example, AnalogEng is the input engineering value, and AnalogRaw is the converted raw value for output.
Practical Tip: It's almost an inverse of reading analog input signals, but instead of averaging, use output filtering based on the process.
领英推荐
Handling Digital Input Signals in SCL
Digital input signals are straightforward to handle, but it's essential to ensure accurate conversion and processing.
Example: Reading a Digital Input
FUNCTION_BLOCK FB_ReadDigitalInput
VAR_INPUT
DigitalIn : BOOL; // Digital input signal
END_VAR
VAR_OUTPUT
ProcessedValue : REAL; // Processed value
END_VAR
BEGIN
// Convert digital input to a floating-point value for processing
IF DigitalIn THEN
ProcessedValue := 1.0;
ELSE
ProcessedValue := 0.0;
END_IF;
END_FUNCTION_BLOCK
In this example, DigitalIn is the digital input signal, and ProcessedValue is the floating-point value used for further processing.
Practical Tip: Use floating (or real) values for calculations. If a value is an integer, it makes precise math difficult. To save memory, you can use an integer of 32767 to be equivalent to 327.67, allowing for smaller memory usage as long as the number isn't going to be large.
Generating Digital Output Signals in SCL
Generating digital output signals involves setting the output based on specific conditions.
Example: Writing a Digital Output
FUNCTION_BLOCK FB_WriteDigitalOutput
VAR_INPUT
Condition : BOOL; // Condition to set output
END_VAR
VAR_OUTPUT
DigitalOut : BOOL; // Digital output signal
END_VAR
BEGIN
// Set digital output based on condition
DigitalOut := Condition;
END_FUNCTION_BLOCK
In this example, Condition is the input condition, and DigitalOut is the digital output signal.
Real-world application: Controls for drives, proportional valves, analog output blocks to other controlling items, and function outputs can be used for logical controls in other functions.
Practical Example: Signal Processing in an Automation System
Let's combine the concepts discussed above in a real-world example: controlling a motor's speed based on analog input and output signals.
FUNCTION_BLOCK FB_MotorControl
VAR_INPUT
SpeedInput : INT; // Raw speed input (analog)
StartButton : BOOL; // Start button (digital)
StopButton : BOOL; // Stop button (digital)
END_VAR
VAR_OUTPUT
MotorSpeed : INT; // Raw motor speed output (analog)
MotorStatus : BOOL; // Motor status (digital)
END_VAR
VAR
SpeedEng : REAL; // Engineering speed value
Setpoint : REAL := 50.0; // Desired speed setpoint
Running : BOOL; // Motor running state
END_VAR
BEGIN
// Convert raw speed input to engineering units
SpeedEng := (SpeedInput - 0) * (100.0 - 0.0) / (27648 - 0) + 0.0;
// Start/Stop logic
IF StartButton AND NOT Running THEN
MotorStatus := TRUE;
Running := TRUE;
ELSIF StopButton THEN
MotorStatus := FALSE;
Running := FALSE;
END_IF;
// Set motor speed if running
IF Running THEN
MotorSpeed := INT((Setpoint - 0.0) * (27648 - 0) / (100.0 - 0.0) + 0);
ELSE
MotorSpeed := 0;
END_IF;
END_FUNCTION_BLOCK
Common Pitfalls: People often don't know the scaling of the analog device in the field. It is important to know this information so you can scale your code accurately.
Ensuring Reliable Signal Processing
In extreme or hazardous environments, the wiring of your signals matters a lot. Here are a few tips for ensuring reliability of your signals in the field:
Troubleshooting Tools and Methods
Often, in the field, you will need to troubleshoot signal handling issues. Some field instruments you may use are:
Function Block for Fill Valve Control
Explanation
Usage
FUNCTION_BLOCK FB_FillValveControl
VAR_INPUT
Level1 : REAL; // Level signal from detector 1
Level2 : REAL; // Level signal from detector 2
UseLevel1 : BOOL; // Selector for using Level1 or Level2
MaxDiff : REAL := 5.0; // Maximum allowable difference between levels
MaxRateDiff : REAL := 0.2; // Maximum allowable difference between rates of change
SafetyInterlock : BOOL; // Safety interlock signal
END_VAR
VAR_OUTPUT
FillValve : REAL; // Proportional output to fill valve
AlarmDiff : BOOL; // Alarm for level difference
AlarmRate : BOOL; // Alarm for rate of change difference
ActiveLevel : REAL; // Active level used for control
END_VAR
VAR
PrevLevel1 : REAL := 0.0; // Previous level 1 value for rate calculation
PrevLevel2 : REAL := 0.0; // Previous level 2 value for rate calculation
RateLevel1 : REAL; // Rate of change for level 1
RateLevel2 : REAL; // Rate of change for level 2
SelectedLevel : REAL; // Selected level based on UseLevel1 input
END_VAR
BEGIN
// Select the active level based on the input selector
IF UseLevel1 THEN
SelectedLevel := Level1;
ELSE
SelectedLevel := Level2;
END_IF;
// Calculate the rate of change for both level signals
RateLevel1 := ABS(Level1 - PrevLevel1);
RateLevel2 := ABS(Level2 - PrevLevel2);
// Update previous level values for the next cycle
PrevLevel1 := Level1;
PrevLevel2 := Level2;
// Check for the difference alarm
AlarmDiff := ABS(Level1 - Level2) > MaxDiff;
// Check for the rate of change difference alarm
AlarmRate := ABS(RateLevel1 - RateLevel2) > MaxRateDiff;
// Safety interlock logic
IF SafetyInterlock OR AlarmDiff OR AlarmRate THEN
FillValve := 0.0; // Disable the fill valve
ELSE
FillValve := SelectedLevel; // Control the fill valve proportionally
END_IF;
// Update the active level for monitoring
ActiveLevel := SelectedLevel;
END_FUNCTION_BLOCK
Final Thoughts
Handling analog and digital signals effectively is crucial for developing robust PLC programs. By understanding the process of reading and writing these signals in SCL, you can create more efficient and reliable control systems.
About the Author
Ken Jensen brings over 24 years of experience in electrical, automation, & controls engineering across diverse industries like nuclear, steel, and glass. He is a highly skilled problem-solver with expertise in electrical systems, PLCs, SCADA, and Industry 4.0 technologies. He is adept at leading projects, driving innovation, and fostering collaboration.
--
1 个月Thanks for sharing your knowledge and thoughts.