cool tool

This commit is contained in:
rusefillc 2023-07-31 00:01:10 -04:00
parent fa9e9a0a34
commit 7f627e2a8a
8 changed files with 129 additions and 91 deletions

View File

@ -15,6 +15,24 @@ public class CANPacket {
this.data = data;
}
public StringBuilder asLua(String arrayName) {
StringBuilder result = new StringBuilder();
result.append(arrayName + " = {");
byte[] data = getData();
System.out.println(String.format("Got ECU 0x%x", getId()) + " " + data.length);
for (int index = 0; index < data.length; index++) {
if (index > 0)
result.append(", ");
result.append(String.format("0x%02x", data[index]));
}
result.append("}\n");
return result;
}
/**
* @param index, starting from zero
*/

View File

@ -1,10 +1,6 @@
package com.rusefi.can.analysis;
import com.rusefi.can.CANPacket;
import com.rusefi.mlv.LoggingContext;
import com.rusefi.mlv.LoggingStrategy;
import com.rusefi.sensor_logs.BinaryLogEntry;
import com.rusefi.sensor_logs.BinarySensorLog;
import java.io.*;
import java.util.*;
@ -22,9 +18,7 @@ public class ByteRateOfChange {
writeByteReport(reportDestinationFolder, simpleFileName, traceFileMetaIndex);
TraceReport traceReport = new TraceReport(packets, simpleFileName, traceFileMetaIndex.statistics);
traceReport.createMegaLogViewer(traceFileMetaIndex.SIDs);
return traceReport;
return new TraceReport(packets, simpleFileName, traceFileMetaIndex.statistics);
}
private static void writeByteReport(String reportDestinationFolder, String simpleFileName, TraceFileMetaIndex traceFileMetaIndex) throws FileNotFoundException {
@ -38,7 +32,7 @@ public class ByteRateOfChange {
private static void writeByteReport(String reportDestinationFolder, String simpleFileName, List<ByteStatistics> allStats) throws FileNotFoundException {
String outputFileName = reportDestinationFolder + File.separator + simpleFileName + ".txt";
System.out.println("Wring byte report to " + outputFileName);
System.out.println("Writing byte report to " + outputFileName);
PrintStream ps = new PrintStream(new FileOutputStream(outputFileName, false));
for (ByteStatistics byteStatistics : allStats) {
@ -97,7 +91,7 @@ public class ByteRateOfChange {
}
public static class ByteId implements Comparable {
public static class ByteId implements Comparable<ByteId> {
final int sid;
final int index;
@ -124,7 +118,7 @@ public class ByteRateOfChange {
}
@Override
public int compareTo(Object o) {
public int compareTo(ByteId o) {
ByteId other = (ByteId) o;
if (other.sid != sid)
return sid - other.sid;
@ -162,64 +156,5 @@ public class ByteRateOfChange {
return statistics;
}
public void createMegaLogViewer(Set<Integer> SIDs) {
List<BinaryLogEntry> entries = new ArrayList<>();
for (ByteId key : statistics.keySet()) {
entries.add(BinaryLogEntry.createFloatLogEntry(key.getLogKey(), Integer.toBinaryString(key.sid)));
}
for (Integer sid : SIDs) {
for (int i = 0; i < 7; i++) {
{
String twoBytesKey = getTwoBytesKeyM(sid, i);
entries.add(BinaryLogEntry.createFloatLogEntry(twoBytesKey, Integer.toBinaryString(sid)));
}
{
String twoBytesKey = getTwoBytesKeyL(sid, i);
entries.add(BinaryLogEntry.createFloatLogEntry(twoBytesKey, Integer.toBinaryString(sid)));
}
}
}
LoggingContext context = new LoggingContext();
BinarySensorLog<BinaryLogEntry> log = context.getBinaryLogEntryBinarySensorLog(entries, simpleFileName + LoggingStrategy.MLG);
context.writeLogContent(packets, log, packetContent -> {
byte[] bytes = packetContent.getData();
for (int i = 0; i < bytes.length; i++) {
int value = bytes[i] & 0xFF;
int sid = packetContent.getId();
{
String name = new ByteId(sid, i).getLogKey();
context.currentSnapshot.put(name, (double) value);
}
{
if (i < bytes.length - 1) {
int value2 = bytes[i + 1] & 0xFF;
{
String name = getTwoBytesKeyM(sid, i);
context.currentSnapshot.put(name, (double) value2 * 256 + value);
}
{
String name = getTwoBytesKeyL(sid, i);
context.currentSnapshot.put(name, (double) value2 + value * 256);
}
}
}
}
return true;
});
}
}
private static String getTwoBytesKeyM(Integer sid, int i) {
return dualSid(sid) + "__" + i + "_" + (i + 1);
}
private static String getTwoBytesKeyL(Integer sid, int i) {
return dualSid(sid) + "__" + (i + 1) + "_" + i;
}
}

View File

@ -79,6 +79,8 @@ public class ByteRateOfChangeReports {
PerSidDump.handle(reportDestinationFolder, simpleFileName, logFileContent);
CounterScanner.scanForCounters(reportDestinationFolder, logFileContent);
CanToMegaLogViewer.createMegaLogViewer(reportDestinationFolder, logFileContent, simpleFileName);
ByteRateOfChange.TraceReport report = ByteRateOfChange.process(reportDestinationFolder, simpleFileName, logFileContent);
reports.add(report);
}, fileNameSuffix);

View File

@ -0,0 +1,87 @@
package com.rusefi.can.analysis;
import com.rusefi.can.CANPacket;
import com.rusefi.mlv.LoggingContext;
import com.rusefi.mlv.LoggingStrategy;
import com.rusefi.sensor_logs.BinaryLogEntry;
import com.rusefi.sensor_logs.BinarySensorLog;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CanToMegaLogViewer {
public static void createMegaLogViewer(String reportDestinationFolder, List<CANPacket> canPackets, String simpleFileName) {
List<BinaryLogEntry> entries = new ArrayList<>();
Set<ByteRateOfChange.ByteId> byteIds = new HashSet<>();
Set<Integer> SIDs = new HashSet<>();
for (CANPacket packet : canPackets) {
SIDs.add(packet.getId());
for (int byteIndex = 0; byteIndex < packet.getData().length; byteIndex++) {
ByteRateOfChange.ByteId key = new ByteRateOfChange.ByteId(packet.getId(), byteIndex);
byteIds.add(key);
}
}
for (ByteRateOfChange.ByteId key : byteIds) {
entries.add(BinaryLogEntry.createFloatLogEntry(key.getLogKey(), Integer.toBinaryString(key.sid)));
}
for (Integer sid : SIDs) {
for (int i = 0; i < 7; i++) {
{
String twoBytesKey = getTwoBytesKeyM(sid, i);
entries.add(BinaryLogEntry.createFloatLogEntry(twoBytesKey, Integer.toBinaryString(sid)));
}
{
String twoBytesKey = getTwoBytesKeyL(sid, i);
entries.add(BinaryLogEntry.createFloatLogEntry(twoBytesKey, Integer.toBinaryString(sid)));
}
}
}
LoggingContext context = new LoggingContext();
BinarySensorLog<BinaryLogEntry> log = context.getBinaryLogEntryBinarySensorLog(entries, reportDestinationFolder + File.separator + simpleFileName + LoggingStrategy.MLG);
context.writeLogContent(canPackets, log, packetContent -> {
byte[] bytes = packetContent.getData();
for (int i = 0; i < bytes.length; i++) {
int value = bytes[i] & 0xFF;
int sid = packetContent.getId();
{
String name = new ByteRateOfChange.ByteId(sid, i).getLogKey();
context.currentSnapshot.put(name, (double) value);
}
{
if (i < bytes.length - 1) {
int value2 = bytes[i + 1] & 0xFF;
{
String name = getTwoBytesKeyM(sid, i);
context.currentSnapshot.put(name, (double) value2 * 256 + value);
}
{
String name = getTwoBytesKeyL(sid, i);
context.currentSnapshot.put(name, (double) value2 + value * 256);
}
}
}
}
return true;
});
}
private static String getTwoBytesKeyM(Integer sid, int i) {
return ByteRateOfChange.dualSid(sid) + "__" + i + "_" + (i + 1);
}
private static String getTwoBytesKeyL(Integer sid, int i) {
return ByteRateOfChange.dualSid(sid) + "__" + (i + 1) + "_" + i;
}
}

View File

@ -7,6 +7,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
@ -31,17 +32,26 @@ public class PerSidDump {
String outputFileName = filteredDestinationFolder + File.separator + simpleFileName + "_filtered_" + dualSid(sid, "_") + ".txt";
PrintWriter pw = new PrintWriter(new FileOutputStream(outputFileName));
List<CANPacket> filteredPackets = new ArrayList<>();
for (CANPacket packet : packets) {
if (packet.getId() != sid)
continue;
// no specific reason to use SteveWriter just need any human-readable writer here
SteveWriter.append(pw, packet);
filteredPackets.add(packet);
}
pw.close();
}
int middleIndex = filteredPackets.size() / 2;
CANPacket middlePacket = filteredPackets.get(middleIndex);
String middleOutputFileName = filteredDestinationFolder + File.separator + simpleFileName + "_filtered_" + dualSid(sid, "_") + "_middle.txt";
PrintWriter middle = new PrintWriter(new FileOutputStream(middleOutputFileName));
middle.println(middlePacket.asLua("payload" + middlePacket.getId()));
middle.close();
}
}
}

View File

@ -43,7 +43,7 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog, Aut
@Override
public void writeSensorLogLine() {
if (stream == null) {
System.out.println("Writing to " + outputFileName);
System.out.println(getClass().getSimpleName() + ": Writing to " + outputFileName);
try {
stream = new DataOutputStream(new FileOutputStream(outputFileName));

View File

@ -31,24 +31,10 @@ public class PolarisSandbox {
continue;
replayed.add(packet.getId());
byte[] data = packet.getData();
System.out.println(String.format("Got ECU 0x%x", packet.getId()) + " " + data.length);
replayLua.append(" data = {");
for (int index = 0; index < data.length; index++) {
if (index > 0)
replayLua.append(", ");
replayLua.append(String.format("0x%x", data[index]));
}
replayLua.append("}\n");
replayLua.append(String.format(" txCan(1, 0x%x, 1, data)\n", packet.getId()));
String arrayName = "payload" + packet.getId();
replayLua.append(packet.asLua(arrayName));
replayLua.append(String.format(" txCan(1, 0x%x, 1, " + arrayName + ")\n", packet.getId()));
}
System.out.printf(replayLua.toString());

View File

@ -39,7 +39,7 @@ public class CANoeCanValidator {
}
String outputFileName = "all_ids.txt";
System.out.println("Writing to " + outputFileName);
System.out.println("CANoeCanValidator: Writing to " + outputFileName);
try (FileWriter fw = new FileWriter(outputFileName)) {
for (Integer id : allIds.keySet()) {
fw.write(Integer.toHexString(id) + "\r\n");