From 877535b8ffcca59016960693ea9097e27dd9cd9c Mon Sep 17 00:00:00 2001 From: rusefillc Date: Wed, 17 Nov 2021 23:34:49 -0500 Subject: [PATCH] proportional spark cut #3427 --- firmware/controllers/algo/launch_control.h | 4 +- .../models/test/SoftLimiterSandbox.java | 75 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 java_console/ui/src/test/java/com/rusefi/models/test/SoftLimiterSandbox.java diff --git a/firmware/controllers/algo/launch_control.h b/firmware/controllers/algo/launch_control.h index 60e4ed0c94..b5804641e2 100644 --- a/firmware/controllers/algo/launch_control.h +++ b/firmware/controllers/algo/launch_control.h @@ -35,7 +35,9 @@ private: Timer m_launchTimer; }; - +/** + * See also SoftLimiterSandbox.java + */ class SoftSparkLimiter { public: /** diff --git a/java_console/ui/src/test/java/com/rusefi/models/test/SoftLimiterSandbox.java b/java_console/ui/src/test/java/com/rusefi/models/test/SoftLimiterSandbox.java new file mode 100644 index 0000000000..85d3eb622c --- /dev/null +++ b/java_console/ui/src/test/java/com/rusefi/models/test/SoftLimiterSandbox.java @@ -0,0 +1,75 @@ +package com.rusefi.models.test; + +import java.util.ArrayList; +import java.util.Random; + +public class SoftLimiterSandbox { + private Random r = new Random(); + + public static void main(String[] args) { + new SoftLimiterSandbox().testSoftLimit(); + } + + public void testSoftLimit() { + double targetMissRatio = 0.3; + + int windowSize = 50; + State state = new State(windowSize); + + System.out.println("Target miss ratio: " + targetMissRatio); + System.out.println("Window length: " + windowSize); + System.out.println("eventIndex,state,currentWindowDeepRatio"); + + for (int i = 0; i < 250 * windowSize; i++) { + state.add(targetMissRatio); + System.out.println(i + "," + state.getLatest() + "," + state.getCurrentRatio()); + } + } + + class State { + private final int windowSize; + + ArrayList states = new ArrayList<>(); + + + public State(int windowSize) { + this.windowSize = windowSize; + states.add(false); + } + + boolean getLatest() { + return states.get(states.size() - 1); + } + + double getCurrentRatio() { + int from = Math.max(0, states.size() - windowSize); + int count = 0; + for (int i = from; i < states.size(); i++) { + if (states.get(i)) + count++; + } + return count * 1.0 / windowSize; + + + } + + public void add(double targetMissRatio) { + if (!getLatest()) { + // never skip two in a row + states.add(true); + } else { + double currentRatio = getCurrentRatio(); + //double thisEventProbability = + + /** + * Due to "never miss two events in a row" requirement we want more or less double the target in comparison + * if current ratio is above target we want to lower probability for next event to fire + */ + + boolean newState = r.nextDouble() > 2 * targetMissRatio; + states.add(newState); + + } + } + } +}