Save Incremental Encoder Position During Unidrive SP Power Loss

David Krause20 min read
S7-300SiemensTroubleshooting
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

1. Problem Statement: Incremental Encoder Position Loss on Drive Power-Down

A four-axis servo system built around a Siemens SIMATIC S7-300 CPU 317F-2DP and four Control Techniques Unidrive SP drives on Profibus DP-V1 loses the absolute position of every axis when the drive's main power is cycled. The PLC retentive data block still holds the last commanded preset, but the drive's actual position register reads 0 on the first Profibus scan, the application overwrites the stored value with that 0, and the machine is sent to the wrong home.

Two failure modes appear in the field:

  • The drive latches an "Under Voltage" (UU) trip after power restore. Actual position reads 0 mm. Preset value (e.g. 1000 mm) and actual position diverge.
  • The S7-300 saves the value to a retentive DB and survives drive power-down, but the value is overwritten by the next Profibus input update before OB 100 (restart) finishes.

The root cause is fundamental: an incremental encoder reports only changes, never absolute position, and its count is meaningless the moment it loses power. Anything the system relies on after a power cycle must be stored elsewhere - in the drive, in the PLC, or in the encoder itself.

2. Root Cause Analysis: Why the PLC DB Is Being Overwritten

The fault path is straightforward but easy to miss when the application reads actual position rather than commanding it. The sequence below describes what happens on a CPU 317F-2DP when drive mains is removed and then restored:

  1. Drive mains is removed. The DC bus discharges, the inverter disables, the drive latches "UU" (under-voltage) or a configurable trip.
  2. If the drive is fed only from the three-phase supply (no 24 Vdc backup), the control board powers down. The Unidrive SP cannot write to its own NVM in this state.
  3. The S7-300 continues to run on its own power supply. Its retentive DB still holds the last good preset value (e.g. 1000).
  4. When mains is restored, the Unidrive SP re-initialises its encoder interface. The incremental counter is 0, because the encoder has no absolute datum.
  5. Profibus DP data exchange resumes as soon as the drive enters the data-exchange state. The PZD word for actual position reads 0.
  6. The S7-300 application runs OB 1 (or a cyclic FB). It reads actual position, and - depending on the code - either writes the drive's 0 into the DB, or reads the DB and writes the old preset to the drive, and the drive moves the motor to chase the 0 it was just sent.

The result is the same in both cases: the saved value is destroyed or contradicted before the operator can react. The fix is to make the saved value the master on power-up, not the live readback. Specifically:

  • The first Profibus input cycle after drive power-up must be discarded, or the DB value must be written to the drive's position-reset register before the application accepts the drive's actual position.
  • The save must be triggered at the moment of trip detection, not on a periodic timer that may not fire in time.
Why this fails on a CPU 317F-2DP specifically: the F-CPU's failsafe runtime group is decoupled from the standard OB 1 cycle. If the save logic is placed only inside a failsafe FB, the standard application may overwrite the value via OB 1 before the F-runtime group can act. Always implement the save/restore in the standard application part and gate it from the F-runtime group with a non-safety handshake.

3. Safety Constraint: Motor Must Not Rotate After Power Removal

Before discussing the save mechanisms, confirm that the load cannot move between power-down and power-up. Any solution that saves a position and restores it later assumes the mechanical position is unchanged in the interval. If the load can sag, drift, or be moved by an external process, the restored value is wrong and the drive will command a step to "correct" the error.

Risk Consequence on Restore Required Action
Vertical axis without holding brake Sag during power-off. Restored value is higher than physical position; drive commands an upward step on enable. Add a spring-set / electric-release holding brake on the motor. Verify torque rating against the worst-case load.
Horizontal axis on a lead-screw with external force (spring, gravity component) Slow drift while drive is off. Restored value is offset from physical position. Add a brake or an absolute encoder. Do not rely on incremental + retentive memory.
Open-loop during power-down with manual override (e.g. handwheel) Operator may move the load. Restored value is meaningless. Use a position-validated restore (drive reads actual position at first enable, compares to stored, requires operator acknowledgement if mismatch > tolerance).
Multi-segment belt or chain with slack Position reading is non-monotonic with motor angle. Restored value does not map to a unique physical location. Use absolute encoder; do not store motor angle, store machine coordinate.

For the rest of this article, assume a holding brake is fitted and verified, or that an external reference (limit switch, datum) is available at the next enable.

4. Solution 1 - Replace the Incremental Encoder with an Absolute Encoder

The cleanest fix is hardware. An absolute encoder reports a unique value at every position in its range, and a multi-turn variant retains that value across a complete power-down with no battery, no UPS, and no PLC intervention.

Encoder Type Position Retention Unidrive SP Compatibility Notes
Single-turn absolute (EnDat 2.1 single-turn, SSI single-turn) Position within one revolution survives power cycle. Multi-turn count is lost. SM-Universal Encoder Plus (15-pin D-sub), SM-Resolver Plus with SSI adapter. Re-home at every power-up if the axis can rotate more than one turn between saves.
Multi-turn absolute with mechanical gear (EnDat 2.2 multi-turn) Position within the entire mechanical range survives power cycle indefinitely. SM-Universal Encoder Plus supports EnDat 2.1/2.2 and Hiperface. No save logic required. Drive reads position on first PZD cycle.
Multi-turn absolute with battery-backed counter Position retained as long as the battery is healthy. SM-Encoder Plus / SM-Universal Encoder Plus with battery box. Schedule periodic battery replacement; an exhausted battery gives a false absolute position.
SSI multi-turn absolute Position retained with no external power. SM-Resolver Plus configured for SSI, or any 15-pin option with SSI firmware. Use gray code with even parity to avoid bit errors on power dip.
Before specifying an absolute encoder, confirm the option module you have on the Unidrive SP supports the protocol you want. SM-Resolver Plus is a 6- or 12-pole resolver interface; SM-Universal Encoder Plus handles EnDat, Hiperface, and SSI. The SM-Encoder Plus (incremental only) will not accept an absolute encoder without firmware that interprets the absolute protocol.

When the drive is fitted with an absolute encoder, the position is available in Menu 13 the moment the encoder interface powers up. The PLC reads the correct value in the first PZD cycle and the issue disappears without any code changes.

5. Solution 2 - Use the Unidrive SP Onboard PLC (DPL) to Save the Position

If the encoder cannot be changed - for cost, lead-time, or mechanical reasons - the next best option is to run a small DPL program inside the Unidrive SP. DPL is the Control Techniques equivalent of Siemens' DCC (Drive Control Chart) used on SINAMICS. The Unidrive SP has an Application Menu (Menu 18-21 depending on firmware) that hosts a DPL runtime with non-volatile RETAIN variables. The drive holds the last position in its own NVM and pushes it back into the position controller on power-up, independent of the S7-300.

For a control engineer familiar with STEP 7, the mental model is:

  • Tasks are like OBs: @INIT runs once at power-up, @FAST runs every 4 ms, @BACKGROUND is a low-priority loop, @EVENT runs on a parameter event.
  • RETAIN variables are like the S7-300 retentive MB/DB - their value survives a power cycle.
  • Drive parameters are addressed with a leading @ in DPL; the same values appear as Pr 13.xx on the keypad and in the GSD configuration.

5.1 Saving on a Trip Event

The DPL program below is a representative position-save routine. It uses a RETAIN variable to hold the last position and writes it into the position controller on power-up. The exact Menu 13 parameter addresses vary between Closed-Loop Vector and Closed-Loop Servo modes and between Unidrive SP firmware revisions; consult the Unidrive SP parameter reference guide for your specific firmware.

VAR
  pos_nv   : DINT RETAIN    // last saved position, non-volatile
  pos_act  : DINT           // live position feedback
  b_save   : BOOL           // one-shot save trigger
ENDVAR

// @INIT block: power-up restore
BEGIN
  @13.xx = pos_nv;          // position preset/offset (firmware-dependent)
  @13.yy = 1;               // issue preset command
END

// @BACKGROUND block: continuous save
BEGIN
  pos_act = @13.zz;         // read live position feedback
  IF b_save OR @10.01 = 0 THEN  // @10.01 = drive healthy bit
    pos_nv = pos_act;
    @0.00 = 1000;           // commit NVM by writing 1000 to Pr 0.00
    b_save = FALSE;
  ENDIF
END
  • Replace 13.xx, 13.yy, and 13.zz with the actual Menu 13 parameter numbers for your Unidrive SP firmware. They are firmware- and mode-dependent.
  • Writing 1000 to Pr 0.00 triggers a non-volatile save of all drive parameters. Confirm the save value for your firmware in the Unidrive SP user guide under "Parameter save and restore". Some firmware revisions use 1000 for "save", 1001 for "save and reset to defaults", and 1002 for "load defaults".
  • Each NVM write has a limited endurance. Do not write on every cycle. Trigger on the falling edge of the drive's healthy bit, or on a trip event, not on a periodic timer.

5.2 Why DPL Beats the S7-300 for This Application

  1. The drive has visibility into its own trip state. The Unidrive SP sets the "drive healthy" bit (e.g. Pr 10.01) to 0 within a few milliseconds of detecting under-voltage. A DPL task can read this and write to NVM at the moment of the trip, before the control board loses its auxiliary supply.
  2. For multi-axis synchronisation, the drives exchange position data among themselves. A drive-resident save can use the same path to keep the four axes coherent. A PLC-resident save can only see one drive at a time over Profibus.

6. Solution 3 - S7-300 Retentive Data Block with Power-Loss Trigger

If the application must use a Siemens-native solution, the next option is a retentive DB on the S7-300 with a save trigger derived from the drive's healthy signal. STEP 7 / SIMATIC Manager and the HW Config tool are part of the standard Siemens Industry Online Support documentation set.

6.1 Configuring the Retentive DB

  1. In STEP 7 / SIMATIC Manager, open the DB and select "Properties".
  2. On the "General - Part 2" tab, clear the "Non-Retain" checkbox. With the box cleared, all tags in the DB are retentive; with the box set, no tags are.
  3. Confirm the DB is loaded into the work memory, not just the load memory. Retentive tags need to be in the work-memory image.

6.2 Retentive Memory Sizing on the CPU 317F-2DP

Retentive data on the CPU 317F-2DP is divided into three areas: bit memory, counters, and timers. The default is 0-15 MB, 0-7 counters, 0-7 timers. To increase the retentive area, open HW Config > CPU properties > "Retentive Memory" and enter the new byte/count ranges. The remainder of the work memory is allocated to the DBs.

Area Default Range (CPU 317F-2DP) Maximum Configurable Use
Bit memory (MB) MB 0 - MB 15 0 - 16 KB Status flags, single-word position markers
S7 counters C 0 - C 7 0 - 512 Event counters (e.g. power-on count)
S7 timers T 0 - T 7 0 - 512 Timeouts, watchdog
DB data 0 bytes (default) up to full work memory ~1 MB work memory on the 317F-2DP Position data, configuration, recipes

6.3 Save Trigger on a Loss-of-Drive-Healthy Event

Wire the Unidrive SP's status word into the cyclic PZD input. Most Unidrive SP Profibus configurations expose the drive status word in PZD1 and the actual position in PZD2-PZD3 (the exact slot depends on the GSD file). The bit "drive healthy" is bit 0 in some status words, but the Unidrive SP uses a non-standard mapping - consult the Unidrive SP Profibus manual for the slot assignment.

In STEP 7, the save is implemented as a falling-edge trigger on the drive's healthy bit:

// SCL example for the position-save FB
FUNCTION_BLOCK FB_PosSave
VAR
  DriveHealthyOld : BOOL;       // edge-detection memory
END_VAR
BEGIN
  // Read drive healthy bit (slot depends on Unidrive SP GSD)
  // Common mapping: bit 0 of the drive status word
  IF NOT "Drive".Status.Healthy AND DriveHealthyOld THEN
    "DB_Pos".SavedValue := "Drive".ActualPosition;
    "DB_Pos".SavedTimestamp := WORD_TO_INT("SysTime");
  END_IF;
  DriveHealthyOld := "Drive".Status.Healthy;
END_FUNCTION_BLOCK

6.4 Restore on Drive Power-Up

Restore runs in OB 100 (warm restart) and in OB 1 on a rising edge of the drive's healthy bit. The restore writes the saved value into the drive's position-reset register and issues a reset command before the application accepts the drive's actual position.

// OB 100 - warm restart
// Read the saved value, push it to the drive, mark the DB as "applied"
IF "DB_Pos".SavedValue <> 0 AND NOT "DB_Pos".Applied THEN
  "Drive".Command.Word := "Drive".Command.Word OR 16#0020;  // preset bit (slot-dependent)
  "Drive".Command.Position := "DB_Pos".SavedValue;
  "DB_Pos".Applied := TRUE;
END_IF;
The exact preset command bit depends on the Unidrive SP Profibus mapping. With the Control Techniques GSD file, the preset is often issued by writing a non-zero value to the relevant parameter over the PKW channel, not by setting a bit in the PZD control word. Check the Unidrive SP Profibus Options User Guide for your firmware.

7. Solution 4 - Unidrive SP SMARTCARD and Parameter Save

The Unidrive SP supports a SMARTCARD slot that can store a complete parameter set, including user-defined parameters. The SMARTCARD is hot-pluggable, but for the position-save application the most useful mode is the "auto-restore on power-up" mode.

  1. Insert a SMARTCARD with the write-protect tab in the "enabled" (write) position.
  2. From the keypad, navigate to Pr 0.00 and enter the save value. On most Unidrive SP firmware revisions, writing 1000 to Pr 0.00 copies the active user menu to the card. The drive returns Pr 0.00 to 0 on success.
  3. Set the "load on power-up" parameter (often in Menu 11, slot-dependent) to enable automatic restore.
  4. Remove the SMARTCARD if no further changes are needed, or leave it in place if the application is in a shipping state.

This is the simplest method for a static commissioning, but it has three limitations for the position-save problem:

  • The SMARTCARD restore happens on drive power-up, but the position counter on the Unidrive SP is a runtime value, not a parameter that can be written by the SMARTCARD in all firmware versions. Whether the position is part of the saved set is firmware-dependent.
  • Writing the SMARTCARD is slow. Each write takes hundreds of milliseconds, which is longer than the time the drive's control board is alive on a mains-loss event.
  • SMARTCARD endurance is lower than the drive's internal NVM. Use it for static configuration, not for cycle-by-cycle position saves.

8. Solution 5 - UPS-Backed Power-Fail Save Sequence

For applications where neither the encoder nor the drive's NV storage is reliable, the last resort is to keep the drive and the PLC alive long enough to perform a complete save. This requires a UPS on the 24 Vdc control rail of the Unidrive SP, on the S7-300 PS 307, and on any encoder interface electronics.

8.1 Power-Fail Detection and Sequence

  1. The UPS monitors the AC mains. On mains loss, it signals the PLC via a digital input or via a serial/Profibus UPS module.
  2. OB 40 (hardware interrupt) is triggered by the UPS digital input. The OB 40 code performs the save: reads the current actual position from each of the four Unidrive SP drives via the acyclic SFB 52 (RDREC) interface, and stores the values in the retentive DB.
  3. The UPS holds the system up for at least 5 seconds, comfortably more than the S7-300 OB 40 execution time and the Profibus acyclic read time (typically < 50 ms per drive).
  4. On power restoration, OB 100 (warm restart) reads the retentive DB and pushes the saved value to each drive before the application code reads the drive's actual position.

8.2 Acyclic Profibus Read for Position

For a CPU 317F-2DP, the acyclic read is implemented with SFB 52 (RDREC) and SFB 53 (WRREC). The Unidrive SP supports the standard PKW parameter channel. To read the actual position from drive 3:

// SCL - read parameter from Unidrive SP over DP-V1 acyclic channel
// The slot/index combination is firmware-dependent
CALL SFB 52, DB52
  REQ    := TRUE
  IOID   := B#16#54              // input/output identifier (read)
  LADDR  := W#16#03E8            // Profibus I/O address of the drive
  RECNUM := 0                    // record number for PKW channel
  RET_VAL := "Rd_RetVal"
  BUSY   := "Rd_Busy"
  RECORD := "ParamChannel"
  LEN    := 8;
IF NOT "Rd_Busy" AND "Rd_RetVal" = 0 THEN
  "DB_Pos".SavedValue[3] := DWORD_TO_DINT("ParamChannel".PKW_Value);
END_IF;
The Unidrive SP PKW interface is documented in the Unidrive SP Profibus Options User Guide. The record number for the parameter channel is firmware-dependent. On older firmware (V01.xx), the PKW records use slot 0 / index 0; on V02.xx and later, the slot/index mapping is different. Always confirm against the manual for your firmware revision.

9. Implementation: Position Restore Sequence After Drive Power-Up

Once the save mechanism is in place, the restore sequence must be deterministic. The recommended order on a CPU 317F-2DP is:

  1. OB 100 (warm restart): the PLC has just powered up. For each drive, read the saved value from the retentive DB and issue a position-preset command. The drive's status word should not be read yet - the drive may still be initialising.
  2. OB 82 / OB 86 (diagnostics): as the Profibus slaves re-enter data exchange, the diagnostic OBs are called. Do not use the actual position from the drive here. The drive has not yet received the preset.
  3. OB 1 cycle 1: the drive status word is now valid. Confirm the preset was accepted (drive returns "preset complete" or "no fault").
  4. OB 1 cycle 2 onwards: the actual position word is now valid and matches the saved value (within one encoder line per motion step during the power-down). The application can resume normal control.
Step OB Action Drive State Required
1. Restore preset OB 100 Write saved value to position preset register Drive in "rdy" or "trip cleared"
2. Confirm drive present OB 82 / 86 Handle Profibus diagnostics, do not read position DP slave re-entering data exchange
3. Verify preset OB 1, first scan Read drive status word, confirm preset complete Drive in "ready to run"
4. Resume application OB 1, normal Read actual position, use for motion control Drive in "run"

10. Verification and Commissioning Checklist

Before returning the system to production, run the following verification on each of the four axes:

  1. Set the drive's command word to "disable" and remove mains. Confirm the drive enters "UU" or the configured trip.
  2. Restore mains. Confirm the drive returns to the "ready" state without operator intervention.
  3. Read the actual position on the S7-300 HMI or via Pr 13.xx on the Unidrive SP keypad. Confirm the value matches the value before the power cycle.
  4. Issue a small motion command (e.g. 10 mm) and confirm the drive responds without a step or stall.
  5. Repeat steps 1-3 ten times. Verify the value is consistent on every cycle.
  6. Inspect the Unidrive SP NVM write count, if exposed, to confirm the save trigger is firing on every cycle and only on every cycle.
  7. Inspect the S7-300 retentive DB on the PG. Confirm the saved value is updated on every power cycle.
A common commissioning failure is that the drive's incremental encoder was mechanically jogged while the drive was off (e.g. by a handwheel, by an external mechanical event, or by gravity on a vertical axis). The "saved value" is correct, but the actual mechanical position is different. Add a position-error threshold: if the saved value and the first valid actual position differ by more than one revolution, raise an alarm and require an operator reset.

11. Troubleshooting Matrix

Symptom Root Cause Diagnostic Resolution
DB value overwritten with 0 on first Profibus scan Application reads drive's actual position and writes it to DB before preset is acknowledged Cross-check DB value, drive's actual position word, and preset status bit timing in trace Add preset before reading position, or delay position read by one OB 1 cycle
DB value correct, drive's actual position reads 0 Preset command was sent before drive was in "ready" state Check drive status word, confirm "ready" bit before issuing preset Sequence preset after OB 82/86, not in OB 100
DB value correct, drive moves to wrong position on enable Mechanical drift between power-down and power-up; holding brake missing or under-rated Add a position marker to the axis; compare physical position to saved value on first enable Fit a holding brake or use an absolute encoder
NVM write fails silently Save command is being issued faster than the drive's NVM commit cycle Check NVM write count if exposed; check the write return code in DPL Debounce the save trigger; do not write on every cycle
Position is correct, but the four axes are out of sync The four drives saved at slightly different times; no global synchronisation signal Compare saved timestamps in the S7-300 trace Add a synchronous "save" command from the PLC on the rising edge of the UPS mains-loss signal
Drive's incremental encoder counter is non-zero but wrong on power-up Encoder wiring noise (Z reference pulse missed) or wrong encoder type selected in Pr Read Pr 3.xx (encoder feedback) on the Unidrive SP; check the Z-pulse wiring on the option module Use a shielded cable, ground the shield at the drive end, terminate at the encoder if required
F-CPU diagnostic buffer reports "F-runtime group cycle time exceeded" after power cycle The save logic is inside the F-runtime group and runs slower than the save deadline Read the diagnostic buffer with STEP 7; check the F-cycle time Move the save/restore to the standard application part; gate the F-runtime group with a non-safety handshake
Preset value written but drive ignores it Preset register address does not match the active drive mode (CLV vs CLS) or firmware revision Read the active parameter map from the Unidrive SP with CTSoft Cross-reference the parameter with the Unidrive SP parameter reference guide for the active firmware
SMARTCARD write returns "fail" after a few minutes SMARTCARD endurance exceeded or write-protect tab in wrong position Check SMARTCARD LED on the drive; check Pr 11.36 for transfer status Replace the SMARTCARD; use the drive's internal NVM for cycle-by-cycle saves

Frequently Asked Questions

Why does the saved value in my S7-300 data block get overwritten with 0 after the drive restarts?

The Unidrive SP's incremental encoder has no absolute datum, so the position counter inside the drive resets to 0 on power-up. The S7-300 reads this 0 from the cyclic PZD input and the application code writes it into the data block on the next scan, overwriting the previously stored value. To prevent this, the saved value must be the master on power-up: push it to the drive's position-preset register before the application accepts the drive's actual position.

Can I use the S7-300 retentive bit memory to save the position instead of a DB?

Yes, but only for a small range - on the CPU 317F-2DP, the default is MB 0 - MB 15, configurable up to 16 KB. A 32-bit position fits in MD 0 - MD 3. The value is still only as accurate as the last good read before the power-down, so you must trigger the save on the falling edge of the drive's healthy bit, not on a periodic timer. For higher endurance, mark the DB as "Retain" and store the position in the DB rather than in the bit-memory area.

What is the simplest hardware fix for the position loss?

Replace the incremental encoder with a multi-turn absolute encoder. The Unidrive SP supports EnDat 2.1/2.2, Hiperface, and SSI feedback on the SM-Universal Encoder Plus option module. The position is available on the first Profibus cycle after power-up, and no preset sequence, no DB, and no UPS is required.

What is the difference between DCC and DPL?

DCC (Drive Control Chart) is the Siemens add-on for SINAMICS drives, integrated with STARTER and SCOUT. DPL (Drive Programming Language) is the Control Techniques equivalent for Unidrive SP, programmed with CTSoft or Unidrive M Connect. Both provide a PLC-like runtime inside the drive with non-volatile variables, and both can implement a SAV_D-style block to commit a position to NVM on a trigger. The Unidrive SP version uses RETAIN variables and the @ notation for parameter access.

How do I save parameters to the Unidrive SP SMARTCARD?

Insert a SMARTCARD with the write-protect tab in the enabled position and write the value 1000 to Pr 0.00. The drive copies the active user menu to the card and returns Pr 0.00 to 0 on success. To restore on power-up, set the load-on-power-up parameter in Menu 11 (slot-dependent) to the appropriate value. Note that the SMARTCARD is intended for static commissioning, not for cycle-by-cycle position saves; the write endurance is lower than the drive's internal NVM.

Back to blog