How-to
Scale a PID real output to analog output counts
Short answer
Scale the PID's REAL output into the channel's documented count range with one linear equation, clamp to the endpoints, then convert with REAL_TO_DWORD. The 4294967295 value is only the DWORD type's maximum (2^32 - 1); a 16-bit channel tops out at 65535 counts, so the module datasheet is the only full scale to use.
On this page
Scale the PID's REAL output into the analog output channel's documented count range with one linear equation, clamp the result to the endpoints, and only then convert with REAL_TO_DWORD. The 4294967295 you may be mapping to 20 mA is the largest value a DWORD can hold (2^32 - 1); it says nothing about the hardware. A 16-bit channel documented as 0-65535 counts is the full scale that matters, and its endpoints come from the module datasheet for the output range you configured. The equation, a worked example with the arithmetic, and the endpoint checks follow.
The DWORD maximum is a storage limit, not a full scale #
The DWORD is the container the command travels in; it holds 2^32 values, 0 through 2^32 - 1. The channel does the digital-to-current conversion at its own resolution, and that resolution, not the container, sets the useful counts. A 16-bit channel distinguishes 2^16 = 65536 values, 0 through 65535, no matter how wide the DWORD you write is: the DWORD span is 65536 times wider than the channel's.
Scale a 0-100 % PID span onto the full DWORD span and the arithmetic fails at the bottom: the channel's top count arrives at 65535 / 4294967295 x 100 % = 0.0015 % of span. Essentially every command you will ever issue lands above the channel's ceiling, and whether the module clips, faults, or wraps at that point is documented module behavior the data type does not control.
| Number | What it is | Where it comes from |
|---|---|---|
| 4294967295 | largest DWORD (2^32 - 1) | the data type |
| 65535 | top count of a 16-bit, 0-65535 channel | the module datasheet, for the configured output range |
| PID span endpoints | loop output range in engineering units | the PID configuration |
If the module side is unfamiliar, how PLC analog input and output modules convert signals covers where those counts come from; the range and resolution belong to the module, not to the variable that carries the command.
What REAL_TO_DWORD does, and what a UNION would do #
REAL_TO_DWORD is a numeric conversion: it keeps the integer value and truncates the fraction, so 24510.09 counts becomes 24510. TO_DWORD is the shorthand for the same conversion. Both assume the REAL already holds counts, which after scaling it will.
A UNION is the opposite operation. It overlays one memory area with another, so the DWORD reads the REAL's IEEE-754 layout, exponent and mantissa, rather than its numeric value, and a correctly scaled command comes out as an unrelated, enormous count. Bit-level moves are for packing a REAL into holding registers for transmission; an analog output channel wants a number, not a bit pattern.
Truncation itself is cheap: it costs at most 1 count, and on a 16-bit current loop that is 16 mA / 65535 = 0.00024 mA of command error, orders of magnitude below anything a hydraulic valve responds to.
Scale, clamp, then convert #
Map the PID's engineering range onto the channel's documented counts, stay in REAL until the last line, and clamp before converting so a wound-up loop cannot hand the channel a count outside its range:
VAR
// Illustrative names - rename for your program
PID_Out : REAL := 37.4; // PID output, percent of command
PID_Min : REAL := 0.0; // PID output at zero command
PID_Max : REAL := 100.0; // PID output at full command
AO_Min : REAL := 0.0; // documented bottom count of the channel
AO_Max : REAL := 65535.0; // documented top count of the channel
Scaled : REAL := 0.0; // scaled command, still REAL
AO_Cmd : DWORD := 0; // value written to the channel
END_VAR
IF PID_Max <> PID_Min THEN
Scaled := AO_Min + (PID_Out - PID_Min) * (AO_Max - AO_Min) / (PID_Max - PID_Min);
IF Scaled > AO_Max THEN Scaled := AO_Max; END_IF
IF Scaled < AO_Min THEN Scaled := AO_Min; END_IF
AO_Cmd := REAL_TO_DWORD(Scaled); // numeric conversion, truncates
END_IF
Every variable above is illustrative; REAL_TO_DWORD and TO_DWORD are the documented conversions. The guard on PID_Max <> PID_Min keeps a zero-span loop from dividing by zero. This is the same linear equation you already ran in reverse on the feedback side, the one scaling a 4-20 mA analog input to engineering units in CPM2 ladder walks through, with only the direction changed.
Worked example: a 37.4 % command on a 16-bit channel #
Take a PID configured for 0.0-100.0 % output and a channel documented 0-65535 counts over a 4-20 mA range. At a 37.4 % command:
Scaled = 0.0 + (37.4 - 0.0) x (65535.0 - 0.0) / (100.0 - 0.0) = 37.4 x 655.35 = 24510.09 counts
REAL_TO_DWORD truncates that to 24510, and the channel sources 4 mA + 24510 / 65535 x 16 mA = 4 + 5.98 = 9.98 mA. The full check set, including what windup must produce:
| PID command | Scaled count | AO_Cmd | Current at the terminals |
|---|---|---|---|
| 0.0 % | 0.0 | 0 | 4.00 mA |
| 37.4 % | 24510.09 | 24510 | 9.98 mA |
| 100.0 % | 65535.0 | 65535 | 20.00 mA |
| 118.0 % windup | 77331.3, clamped | 65535 | full scale, same as above |
The windup row is 65535 x 1.18 = 77331.3 before the clamp; the clamp holds the channel at its documented top instead of passing a count it cannot represent.
Procedure #
- Write down the PID output's units and configured range, for example 0.0-100.0 %.
- Read the channel's raw count endpoints for the configured output range from the module datasheet, not from the input module's range and not from the DWORD type.
- Enter the scaling with the divide-by-zero guard, keeping the intermediate result in REAL.
- Clamp the scaled count to the documented endpoints.
- Convert once, with
REAL_TO_DWORD, and write the DWORD to the channel. - Force the PID output to its minimum and maximum; confirm the channel counts equal the documented endpoints.
- Put a meter in series and confirm 4.00 mA, 9.98 mA at the 37.4 % point, and the full-scale current.
- Stroke the cylinder open-loop in both directions before enabling the loop.
Verify direction and endpoints before closing the loop #
Do the open-loop strokes with the rod clear of tooling and people and the e-stop within reach: a reversed sign or a swapped endpoint pair commands full output against the cylinder's mechanical limit, and hydraulics will win. Confirm that increasing PID output moves the cylinder the way the process expects, for a position loop that feedback counts rise as the cylinder travels toward the setpoint, and set output limits in the PID block as a second clamp in engineering units. If the direction is wrong, change the sign in exactly one place, the scaling or the loop gain, never both.
Questions asked
Should full-scale current be 4294967295 counts?
No. 4294967295 is the largest value a DWORD can hold (2^32 - 1); the full-scale point is the top count of the channel you configured, 65535 on a 16-bit 0-65535 range. Take both endpoints from the module datasheet for that range, and do not copy the input module's range onto the output module: the two are different hardware with different datasheets.
Does REAL_TO_DWORD round or truncate?
It truncates toward zero, so 24510.09 becomes 24510; TO_DWORD is the shorthand for the same conversion. The cost is at most 1 count, about 0.00024 mA on a 16-bit 4-20 mA channel, which a hydraulic valve will never notice. If you want round-half-up behavior on positive counts, add 0.5 to the scaled value just before converting.
Can a UNION move the REAL into the DWORD instead?
Not for commanding counts. A UNION overlays the same bytes, so the DWORD reads the REAL's IEEE-754 bit pattern rather than its value: the REAL 1.0 comes back as 1065353216, not 1. Bit-level moves belong to packing a REAL into registers for transmission; scaling followed by REAL_TO_DWORD is what produces a usable command.
My channel reserves counts for overrange. Does anything change?
Only the endpoints. Some modules place the live-range endpoints inside the raw span and reserve the extremes for under- and overrange indication, so the endpoints for your configured range come from the datasheet table, not from 0 and the top of the type. The equation, clamp, and conversion are unchanged.
Should I clamp the PID output or the scaled count?
Both, in different units. Set output limits in the PID block in engineering units to stop windup at a sane command, and clamp the scaled count immediately before the conversion so the DWORD never leaves the channel's documented endpoints. The second clamp still protects the channel if the loop limit is widened later.