only:EPIC: Improve toolset for default tune canned tune generation #4871

This commit is contained in:
rusefillc 2023-12-28 01:21:59 -05:00
parent 815598b6be
commit e2f81b1a3c
4 changed files with 123 additions and 32 deletions

View File

@ -103,9 +103,9 @@ public class TS2C {
@NotNull
public static String getTableCSourceCode2(String msqFileName, String tableName, IniFileModel model, CurveData xRpmCurve, CurveData yLoadBins) throws IOException {
float[][] table = TableData.readTable(msqFileName, tableName, model);
TableData table = TableData.readTable(msqFileName, tableName, model);
return getTableCSourceCode(tableName, yLoadBins, xRpmCurve, table);
return getTableCSourceCode(tableName, yLoadBins, xRpmCurve, table.floats);
}
private static String getTableCSourceCode(String tableName, CurveData loadBins, CurveData rpmBins, float[][] table) {
@ -131,6 +131,13 @@ public class TS2C {
}
}
public static void writePlainTable(int rows, int columns, StringBuilder sb, ValueSource valueSource) {
for (int row = 0; row < rows; row++) {
String line = writePlainTableLine(valueSource, row, columns);
sb.append(line);
}
}
/**
* @param fileName text file to open
* @param magicStringKey magic string content to scroll to
@ -152,6 +159,17 @@ public class TS2C {
return br;
}
private static String writePlainTableLine(ValueSource valueSource, int loadIndex, int loadSize) {
StringBuilder sb = new StringBuilder("{");
for (int rpmIndex = 0; rpmIndex < loadSize; rpmIndex++) {
sb.append(String.format("%3.3f", valueSource.getValue(loadIndex, rpmIndex)) + ",\t");
}
sb.append("},\n");
return sb.toString();
}
private static String writeTableLine(CurveData loadBins, CurveData rpmBins, ValueSource valueSource, int loadIndex) {
StringBuilder sb = new StringBuilder("{");
@ -175,8 +193,8 @@ public class TS2C {
return x;
}
interface ValueSource {
float getValue(int loadIndex, int rpmIndex);
public interface ValueSource {
float getValue(int rowIndex, int columnIndex);
}
}

View File

@ -10,21 +10,31 @@ import java.io.Reader;
import java.util.Arrays;
import java.util.function.Function;
public class TableData {
@NotNull
public static float[][] readTable(String msqFileName, String tableName, IniFileModel model) throws IOException {
ArrayIniField field = (ArrayIniField) model.allIniFields.get(tableName);
public class TableData implements HoHo {
private final int rows;
private final int columns;
public final float[][] floats;
private final String tableName;
if (field.getRows() != field.getCols())
throw new UnsupportedOperationException("Not square table not supported yet");
// todo: replace with loadCount & rpmCount
int rows = field.getRows();
return readTable(msqFileName, tableName, rows, TS2C.fileFactory, field.getCols());
public TableData(int rows, int columns, float[][] floats, String tableName) {
this.rows = rows;
this.columns = columns;
this.floats = floats;
this.tableName = tableName;
}
@NotNull
public static float[][] readTable(String msqFileName, String tableName, int rows, Function<String, Reader> factory, int columns) throws IOException {
public static TableData readTable(String msqFileName, String tableName, IniFileModel model) throws IOException {
ArrayIniField field = (ArrayIniField) model.allIniFields.get(tableName);
int rows = field.getRows();
int columns = field.getCols();
return readTable(msqFileName, tableName, rows, TS2C.fileFactory, columns);
}
public static TableData readTable(String msqFileName, String tableName, int rows, Function<String, Reader> factory, int columns) throws IOException {
float[][] table = new float[rows][];
for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
table[rowIndex] = new float[columns];
@ -32,7 +42,7 @@ public class TableData {
BufferedReader reader = TS2C.readAndScroll(msqFileName, tableName, factory);
readTable(table, reader, rows, columns);
return table;
return new TableData(rows, columns, table, tableName);
}
private static void readTable(float[][] table, BufferedReader r, int rows, int columns) throws IOException {
@ -62,4 +72,39 @@ public class TableData {
rowIndex++;
}
}
public String getCTable() {
StringBuilder output = new StringBuilder();
TS2C.writePlainTable(rows, columns, output, (loadIndex, rpmIndex) -> floats[loadIndex][rpmIndex]);
return output.toString();
}
private String getCannedMethod() {
return "canned" + tableName + "()";
}
private String getCannedName() {
return "hardCoded" + tableName;
}
@NotNull
public String getCsourceCode() {
return "static const float " +
getCannedName() + "[" + rows + "][" + columns + "] = {\n" +
getCTable() +
"};\n";
}
@Override
public String getCsourceMethod(String reference) {
return "static void " + getCannedMethod() + " {\n"
+ "\t" + getCsourceCode() +
"\tcopyTable(" + reference + tableName + ", " + getCannedName() + ");\n" +
"}\n\n";
}
@Override
public String getCinvokeMethod() {
return "\t" + getCannedMethod() + ";\n";
}
}

View File

@ -180,16 +180,6 @@ public class TuneCanTool {
}
if (cf.isArray()) {
if (cf.getArraySizes().length == 2) {
float[][] tableData = TableData.readTable(currentTuneFileName, name, ini);
System.out.printf(" " + name);
continue;
}
CurveData data = CurveData.valueOf(currentTuneFileName, name, ini);
if (data == null)
continue;
String parentReference;
if (cf.getParent().getName().equals("engine_configuration_s")) {
parentReference = "engineConfiguration->";
@ -202,6 +192,21 @@ public class TuneCanTool {
continue;
}
if (cf.getArraySizes().length == 2) {
TableData tableData = TableData.readTable(currentTuneFileName, name, ini);
System.out.printf(" " + name);
methods.append(tableData.getCsourceMethod(parentReference));
invokeMethods.append(tableData.getCinvokeMethod());
continue;
}
CurveData data = CurveData.valueOf(currentTuneFileName, name, ini);
if (data == null)
continue;
methods.append(data.getCsourceMethod(parentReference));
invokeMethods.append(data.getCinvokeMethod());

View File

@ -1,5 +1,6 @@
package com.rusefi.tune;
import com.rusefi.tools.tune.TS2C;
import com.rusefi.tools.tune.TableData;
import org.junit.jupiter.api.Test;
@ -12,8 +13,7 @@ import static org.junit.Assert.assertEquals;
public class TableDataTest {
@Test
public void read() throws IOException {
String s = "<constant cols=\"10\" digits=\"0\" name=\"scriptTable4\" rows=\"8\" units=\"value\">\n" +
String input = "<constant cols=\"10\" digits=\"0\" name=\"scriptTable4\" rows=\"8\" units=\"value\">\n" +
" 0.0 0.0 0.0 333 0.0 0.0 0.0 0.0 0.0 0.0 \n" +
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" +
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" +
@ -21,12 +21,35 @@ public class TableDataTest {
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" +
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" +
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" +
" 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" +
" 771 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 777 \n" +
" </constant>\n";
Reader r = new StringReader(s);
float[][] data = TableData.readTable("x", "scriptTable4", 8, s1 -> r, 10);
Reader r = new StringReader(input);
TableData data = TableData.readTable("x", "scriptTable4", 8, s1 -> r, 10);
assertEquals(333, (int)data[0][3]);
assertEquals(333, (int) data.floats[0][3]);
assertEquals("{0.000,\t0.000,\t0.000,\t333.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{444.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{771.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t777.000,\t},\n", data.getCTable());
assertEquals("static const float hardCodedscriptTable4[8][10] = {\n" +
"{0.000,\t0.000,\t0.000,\t333.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{444.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t},\n" +
"{771.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t0.000,\t777.000,\t},\n" +
"};\n", data.getCsourceCode());
}
}