From 30964db684d0844dad76ef34f87b7a21d3b55884 Mon Sep 17 00:00:00 2001 From: Dennis Hilhorst Date: Mon, 3 Mar 2025 15:48:11 +0100 Subject: [PATCH 1/5] make number of steps in loop command predictable --- .../scan/server/command/LoopCommandImpl.java | 34 ++++++------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java index 7a5816845d..024825257e 100644 --- a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java +++ b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java @@ -128,6 +128,10 @@ private double getLoopStep() return step; } + public int getNumSteps() { + return (int)Math.ceil(Math.abs(((getLoopEnd() - getLoopStart()) / getLoopStep()))); + } + /** {@inheritDoc} */ @Override public void simulate(final SimulationContext context) throws Exception @@ -135,14 +139,10 @@ public void simulate(final SimulationContext context) throws Exception final SimulatedDevice device = context.getDevice(context.getMacros().resolveMacros(command.getDeviceName())); final double start = getLoopStart(); - final double end = getLoopEnd(); final double step = getLoopStep(); - if (step > 0) - for (double value = start; value <= end; value += step) - simulateStep(context, device, value); - else // step is < 0, so stepping down - for (double value = end; value >= start; value += step) - simulateStep(context, device, value); + int num_steps = getNumSteps(); + for (int i = 0; i < num_steps; i++) + simulateStep(context, device, start + i * step); } /** Simulate one step in the loop iteration @@ -205,24 +205,10 @@ public void execute(final ScanContext context) throws Exception condition = null; double start = getLoopStart(); - double end = getLoopEnd(); double step = getLoopStep(); - if (step > 0) - for (double value = start; value <= end; value += step) - { - executeStep(context, device, condition, readback, value); - // Permit changed step and end, but keep the direction - end = getLoopEnd(); - step = Math.abs(command.getStepSize()); - } - else // step is < 0, so stepping down - for (double value = end; value >= start; value += step) - { - executeStep(context, device, condition, readback, value); - // Permit changed step and start, but keep the direction - start = getLoopStart(); - step = - Math.abs(command.getStepSize()); - } + int num_steps = getNumSteps(); + for (int i = 0; i < num_steps; i++) + executeStep(context, device, condition, readback, start + i * step); } /** Execute one step of the loop From 6f102973a0ba573fb5efe6b277024044c2697756 Mon Sep 17 00:00:00 2001 From: Dennis Hilhorst Date: Mon, 3 Mar 2025 16:38:29 +0100 Subject: [PATCH 2/5] make sure alternating scans work with predictable steps --- .../org/csstudio/scan/server/command/LoopCommandImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java index 024825257e..1e74c5d59f 100644 --- a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java +++ b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java @@ -204,9 +204,11 @@ public void execute(final ScanContext context) throws Exception else condition = null; - double start = getLoopStart(); double step = getLoopStep(); + // step is < 0 means we are stepping down + double start = step < 0 ? getLoopEnd() : getLoopStart(); int num_steps = getNumSteps(); + for (int i = 0; i < num_steps; i++) executeStep(context, device, condition, readback, start + i * step); } From 6ac30cf317110d86be9a5c5d9d0e7f6f6ba47da6 Mon Sep 17 00:00:00 2001 From: Dennis Hilhorst Date: Mon, 3 Mar 2025 16:39:38 +0100 Subject: [PATCH 3/5] also do alternating in the simulation --- .../java/org/csstudio/scan/server/command/LoopCommandImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java index 1e74c5d59f..eb84b89473 100644 --- a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java +++ b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java @@ -138,8 +138,9 @@ public void simulate(final SimulationContext context) throws Exception { final SimulatedDevice device = context.getDevice(context.getMacros().resolveMacros(command.getDeviceName())); - final double start = getLoopStart(); final double step = getLoopStep(); + // step is < 0 means we are stepping down + double start = step < 0 ? getLoopEnd() : getLoopStart(); int num_steps = getNumSteps(); for (int i = 0; i < num_steps; i++) simulateStep(context, device, start + i * step); From 0e2df3abc6abdd67a91bdaf06b6baa87cc5743fb Mon Sep 17 00:00:00 2001 From: Dennis Hilhorst Date: Tue, 4 Mar 2025 09:05:35 +0100 Subject: [PATCH 4/5] add extra step for including bounds --- .../java/org/csstudio/scan/server/command/LoopCommandImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java index eb84b89473..fbe4e29a05 100644 --- a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java +++ b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java @@ -129,7 +129,7 @@ private double getLoopStep() } public int getNumSteps() { - return (int)Math.ceil(Math.abs(((getLoopEnd() - getLoopStart()) / getLoopStep()))); + return (int)Math.ceil(Math.abs(((getLoopEnd() - getLoopStart()) / getLoopStep()))) + 1; } /** {@inheritDoc} */ From a9875c13e101771979f84a4e4b54191bd8fb264c Mon Sep 17 00:00:00 2001 From: Dennis Hilhorst Date: Tue, 4 Mar 2025 09:13:11 +0100 Subject: [PATCH 5/5] don't use functions with side effects and update getWorkUnits --- .../org/csstudio/scan/server/command/LoopCommandImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java index fbe4e29a05..a1c9c1748b 100644 --- a/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java +++ b/services/scan-server/src/main/java/org/csstudio/scan/server/command/LoopCommandImpl.java @@ -84,7 +84,7 @@ public LoopCommandImpl(final LoopCommand command) throws Exception @Override public long getWorkUnits() { - final long iterations = 1 + Math.round(Math.abs((command.getEnd() - command.getStart()) / command.getStepSize())); + final long iterations = getNumSteps(); long body_units = 0; for (ScanCommandImpl command : implementation) body_units += command.getWorkUnits(); @@ -129,7 +129,7 @@ private double getLoopStep() } public int getNumSteps() { - return (int)Math.ceil(Math.abs(((getLoopEnd() - getLoopStart()) / getLoopStep()))) + 1; + return (int)Math.ceil(Math.abs(((command.getEnd() - command.getStart()) / command.getStepSize()))) + 1; } /** {@inheritDoc} */