From c28d45d62d86ac306089f11e9ee7bb422b18e5d5 Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sun, 13 Sep 2015 14:02:32 -0400 Subject: [PATCH] auto-sync --- firmware/controllers/math/engine_math.cpp | 33 ++++++++++--------- .../models/src/com/rusefi/BinarySearch.java | 29 ++++++++++++++++ .../src/com/rusefi/test/BinarySearchTest.java | 32 ++++++++++++++++++ unit_tests/test_trigger_decoder.cpp | 2 +- 4 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 java_console/models/src/com/rusefi/BinarySearch.java create mode 100644 java_console/models/src/com/rusefi/test/BinarySearchTest.java diff --git a/firmware/controllers/math/engine_math.cpp b/firmware/controllers/math/engine_math.cpp index 87500ef4d1..cddd965307 100644 --- a/firmware/controllers/math/engine_math.cpp +++ b/firmware/controllers/math/engine_math.cpp @@ -234,7 +234,7 @@ float getSparkDwellMsT(int rpm DECLARE_ENGINE_PARAMETER_S) { return interpolate2d(rpm, engineConfiguration->sparkDwellBins, engineConfiguration->sparkDwell, DWELL_CURVE_SIZE); } -static int findAngleIndex(float angleOffset DECLARE_ENGINE_PARAMETER_S) { +static int findAngleIndex(float target DECLARE_ENGINE_PARAMETER_S) { /** * Here we rely on this to be pre-calculated, that's a performance optimization */ @@ -242,7 +242,6 @@ static int findAngleIndex(float angleOffset DECLARE_ENGINE_PARAMETER_S) { efiAssert(engineCycleEventCount > 0, "engineCycleEventCount", 0); - uint32_t middle; uint32_t left = 0; uint32_t right = engineCycleEventCount - 1; @@ -250,21 +249,21 @@ static int findAngleIndex(float angleOffset DECLARE_ENGINE_PARAMETER_S) { * Let's find the last trigger angle which is less or equal to the desired angle * todo: extract binary search as template method? */ - while (true) { - middle = (left + right) / 2; + while (left <= right) { + int middle = (left + right) / 2; angle_t eventAngle = TRIGGER_SHAPE(eventAngles[middle]); - if (middle == left) { - return middle; - } - if (angleOffset < eventAngle) { - right = middle; - } else if (angleOffset > eventAngle) { - left = middle; - } else { - return middle; - } - } + if (eventAngle < target) { + left = middle + 1; + } else if (eventAngle > target) { + right = middle - 1; + } else { + // Values are equal + return middle; // Key found + } + } + return left - 1; + } void findTriggerPosition(event_trigger_position_s *position, angle_t angleOffset DECLARE_ENGINE_PARAMETER_S) { @@ -367,6 +366,7 @@ static NamedOutputPin * getIgnitionPinForIndex(int i DECLARE_ENGINE_PARAMETER_S #if EFI_ENGINE_CONTROL || defined(__DOXYGEN__) +int is700 = 0; void prepareOutputSignals(DECLARE_ENGINE_PARAMETER_F) { engine_configuration2_s *engineConfiguration2 = engine->engineConfiguration2; @@ -379,6 +379,9 @@ void prepareOutputSignals(DECLARE_ENGINE_PARAMETER_F) { } for (int angle = 0; angle < CONFIG(engineCycle); angle++) { + if (angle==700) { + is700++; + } int triggerShapeIndex = findAngleIndex(angle PASS_ENGINE_PARAMETER); if (engineConfiguration->useOnlyFrontForTrigger) triggerShapeIndex = triggerShapeIndex & 0xFFFFFFFE; // we need even index for front_only diff --git a/java_console/models/src/com/rusefi/BinarySearch.java b/java_console/models/src/com/rusefi/BinarySearch.java new file mode 100644 index 0000000000..96318acfbd --- /dev/null +++ b/java_console/models/src/com/rusefi/BinarySearch.java @@ -0,0 +1,29 @@ +package com.rusefi; + +public class BinarySearch { + public static int binarySearch(double target, double[] angles) { + System.out.println("Testing " + target); + + int left = 0; + int right = angles.length - 1; + + while (left <= right) { + int mid = (left + right) / 2; + double midVal = angles[mid]; + + if (midVal < target) + left = mid + 1; // Neither val is NaN, thisVal is smaller + else if (midVal > target) + right = mid - 1; // Neither val is NaN, thisVal is larger + else { + if (midVal == target) // Values are equal + return mid; // Key found + else if (midVal < target) // (-0.0, 0.0) or (!NaN, NaN) + left = mid + 1; + else // (0.0, -0.0) or (NaN, !NaN) + right = mid - 1; + } + } + return left - 1; + } +} \ No newline at end of file diff --git a/java_console/models/src/com/rusefi/test/BinarySearchTest.java b/java_console/models/src/com/rusefi/test/BinarySearchTest.java new file mode 100644 index 0000000000..66cda281b2 --- /dev/null +++ b/java_console/models/src/com/rusefi/test/BinarySearchTest.java @@ -0,0 +1,32 @@ +package com.rusefi.test; + +import com.rusefi.BinarySearch; +import junit.framework.TestCase; + +import java.util.Arrays; + +public class BinarySearchTest extends TestCase { + public static void main(String[] args) { + new BinarySearchTest().testBinary(); + } + + public void testBinary() { + + double[] angles = new double[] {0, 56, 126, 180, 236, 279, 306, 416, 486, 540, 596, 666}; + + Arrays.sort(angles); + + assertEquals(0, BinarySearch.binarySearch(0, angles)); + + assertEquals(0, BinarySearch.binarySearch(40, angles)); + assertEquals(1, BinarySearch.binarySearch(56, angles)); + + assertEquals(1, BinarySearch.binarySearch(60, angles)); + + assertEquals(10, BinarySearch.binarySearch(660, angles)); + + assertEquals(11, BinarySearch.binarySearch(666, angles)); + + assertEquals(11, BinarySearch.binarySearch(700, angles)); + } +} \ No newline at end of file diff --git a/unit_tests/test_trigger_decoder.cpp b/unit_tests/test_trigger_decoder.cpp index a567e6aa11..d7e15b849a 100644 --- a/unit_tests/test_trigger_decoder.cpp +++ b/unit_tests/test_trigger_decoder.cpp @@ -475,7 +475,7 @@ void testTriggerDecoder(void) { assertEquals(666, t->eventAngles[11]); assertEqualsM("index for 665", 10, t->triggerIndexByAngle[665]); - assertEqualsM("index for 668", 10, t->triggerIndexByAngle[668]); // todo: WHY? looks like a bug with last index? + assertEqualsM("index for 668", 11, t->triggerIndexByAngle[668]); } // testTriggerDecoder2("miata 1990", MIATA_1990, 0, 0.6280, 0.0);