From be8c0795d672fc86260cde403bb9e5ecdaca4f3e Mon Sep 17 00:00:00 2001 From: rusEfi Date: Mon, 8 Feb 2016 13:02:01 -0500 Subject: [PATCH] auto-sync --- .../logging/src/com/rusefi/FileLog.java | 2 +- .../src/com/rusefi/autotune/FuelAutoTune.java | 16 ++++- java_console/ui/src/com/rusefi/Launcher.java | 4 +- .../ui/src/com/rusefi/ui/FuelTunePane.java | 69 +++++++++++++++++-- 4 files changed, 81 insertions(+), 10 deletions(-) diff --git a/java_console/logging/src/com/rusefi/FileLog.java b/java_console/logging/src/com/rusefi/FileLog.java index 1980a5e353..da80cd6939 100644 --- a/java_console/logging/src/com/rusefi/FileLog.java +++ b/java_console/logging/src/com/rusefi/FileLog.java @@ -14,7 +14,7 @@ public enum FileLog { MAIN, SIMULATOR_CONSOLE; - private static final String DIR = "out/"; + public static final String DIR = "out/"; public static String currentLogName; public static final String END_OF_TIMESTAND_TAG = ": "; public static final Logger LOGGER = new Logger() { diff --git a/java_console/models/src/com/rusefi/autotune/FuelAutoTune.java b/java_console/models/src/com/rusefi/autotune/FuelAutoTune.java index da32874a01..1adc70ce28 100644 --- a/java_console/models/src/com/rusefi/autotune/FuelAutoTune.java +++ b/java_console/models/src/com/rusefi/autotune/FuelAutoTune.java @@ -23,10 +23,14 @@ public class FuelAutoTune { public static class stDataOnline { public final double AFR; + private final int rpm; + private final double engineLoad; int rpmIndex; int engineLoadIndex; - public stDataOnline(double AFR, int rpmIndex, int engineLoadIndex) { + public stDataOnline(double AFR, int rpmIndex, int engineLoadIndex, int rpm, double engineLoad) { + this.rpm = rpm; + this.engineLoad = engineLoad; if (rpmIndex < 0 || rpmIndex >= Fields.FUEL_RPM_COUNT) throw new IllegalStateException("rpmIndex " + rpmIndex); if (engineLoadIndex < 0 || engineLoadIndex >= Fields.FUEL_LOAD_COUNT) @@ -41,7 +45,7 @@ public class FuelAutoTune { if (rpmIndex < 0 || rpmIndex >= Fields.FUEL_RPM_COUNT) return null; int engineLoadIndex = (int) (engineLoad / 120.0 * SIZE); - return new stDataOnline(AFR, rpmIndex, engineLoadIndex); + return new stDataOnline(AFR, rpmIndex, engineLoadIndex, rpm, engineLoad); } int getRpmIndex() { @@ -59,6 +63,14 @@ public class FuelAutoTune { public int RPM_RT_32() { return getRpmIndex(); } + + public int getRpm() { + return rpm; + } + + public double getEngineLoad() { + return engineLoad; + } } public static class Result { diff --git a/java_console/ui/src/com/rusefi/Launcher.java b/java_console/ui/src/com/rusefi/Launcher.java index 2fdbdb07f1..5015d5c19d 100644 --- a/java_console/ui/src/com/rusefi/Launcher.java +++ b/java_console/ui/src/com/rusefi/Launcher.java @@ -32,7 +32,7 @@ import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; * @see EngineSnifferPanel */ public class Launcher { - public static final int CONSOLE_VERSION = 20160206; + public static final int CONSOLE_VERSION = 20160208; public static final boolean SHOW_STIMULATOR = false; private static final String TAB_INDEX = "main_tab"; protected static final String PORT_KEY = "port"; @@ -114,7 +114,7 @@ public class Launcher { tabbedPane.add("Bench Test", new BenchTestPane().getContent()); if (!LinkManager.isLogViewer() && false) // todo: fix it & better name? tabbedPane.add("Logs Manager", logsManager.getContent()); - if (false) + if (true) tabbedPane.add("Fuel Tune", fuelTunePane.getContent()); if (!LinkManager.isLogViewerMode(port)) { diff --git a/java_console/ui/src/com/rusefi/ui/FuelTunePane.java b/java_console/ui/src/com/rusefi/ui/FuelTunePane.java index e91c7d3866..9ad1bb325d 100644 --- a/java_console/ui/src/com/rusefi/ui/FuelTunePane.java +++ b/java_console/ui/src/com/rusefi/ui/FuelTunePane.java @@ -21,12 +21,11 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; -import java.io.IOException; +import java.io.*; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.List; /** @@ -47,6 +46,7 @@ public class FuelTunePane { private final JCheckBox collect = new JCheckBox("enable"); private final JButton clean = new JButton("clear"); private byte[] newVeMap; + private DataOutputStream dos; public FuelTunePane() { final JLabel incomingBufferSize = new JLabel(); @@ -142,6 +142,7 @@ public class FuelTunePane { private void doJob() { float veTable[][] = new float[Fields.FUEL_LOAD_COUNT][Fields.FUEL_RPM_COUNT]; loadMap(veTable, Fields.VETABLE.getOffset()); + logMap("source", veTable); List data = new ArrayList<>(); synchronized (incomingDataPoints) { @@ -149,16 +150,74 @@ public class FuelTunePane { data.add(point.asDataOnline()); incomingDataPoints.clear(); } + writeDataPoints(data); // todo: move this away from AWT thread FuelAutoTune.Result a = FuelAutoTune.process(false, data, 0.1, 14.7, veTable); - newVeMap = toByteArray(a.getKgbcRES()); + float[][] result = a.getKgbcRES(); + logMap("result", result); + newVeMap = toByteArray(result); loadData(changeMap, newVeMap, 0); upload.setEnabled(true); } + private void writeDataPoints(List data) { + DataOutputStream dos = getTuneLogStream(); + if (dos == null) + return; + try { + dos.writeBytes("Running with " + data.size() + " points\r\n"); + dos.writeBytes("AFR\tRPM\tload\r\n"); + for (FuelAutoTune.stDataOnline point : data) + dos.writeBytes(point.AFR +"\t" + point.getRpm() + "\t" + point.getEngineLoad() + "\r\n"); + + } catch (IOException e) { + FileLog.MAIN.logLine("Error writing auto-tune log"); + } + } + + private void logMap(String msg, float[][] table) { + DataOutputStream dos = getTuneLogStream(); + if (dos == null) + return; + try { + dos.writeBytes(new Date() + ": " + msg + "\r\n"); + + for (int rpmIndex = 0; rpmIndex < Fields.FUEL_RPM_COUNT; rpmIndex++) { + dos.writeChar('\t'); + dos.writeBytes(Float.toString(veRpmBins[rpmIndex])); + } + dos.writeBytes("\r\n"); + + for (int loadIndex = 0; loadIndex < Fields.FUEL_LOAD_COUNT; loadIndex++) { + dos.writeBytes(Float.toString(veLoadBins[loadIndex])); + for (int rpmIndex = 0; rpmIndex < Fields.FUEL_RPM_COUNT; rpmIndex++) { + dos.writeChar('\t'); + float v = table[loadIndex][rpmIndex]; + dos.writeBytes(Float.toString(v)); + } + dos.writeBytes("\r\n"); + } + dos.flush(); + } catch (IOException e) { + FileLog.MAIN.logLine("Error writing auto-tune log"); + } + } + + private DataOutputStream getTuneLogStream() { + if (dos == null) { + String fileName = FileLog.DIR + "tune_" + FileLog.getDate() + ".txt"; + try { + dos = new DataOutputStream(new FileOutputStream(fileName)); + } catch (FileNotFoundException e) { + FileLog.MAIN.logLine("Error creating " + fileName + ":" + e); + } + } + return dos; + } + private byte[] toByteArray(float[][] output) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -250,7 +309,7 @@ public class FuelTunePane { } public FuelAutoTune.stDataOnline asDataOnline() { - return new FuelAutoTune.stDataOnline(afr, rpmIndex, engineLoadIndex); + return new FuelAutoTune.stDataOnline(afr, rpmIndex, engineLoadIndex, rpm, engineLoad); } } }