LM-2 MTS refactoring. Now plugin loads sensor inputs dynamically. This should be functional enough to test MTS service.

git-svn-id: https://svn2.assembla.com/svn/romraider/trunk@347 38686702-15cf-42e4-a595-3071df8bf5ea
This commit is contained in:
Dale Schultz 2011-09-28 04:50:42 +00:00
parent f776849834
commit 35a860ad08
6 changed files with 295 additions and 86 deletions

View File

@ -0,0 +1,111 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2010 RomRaider.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.logger.external.innovate.generic.mts.io;
import static com.romraider.logger.external.innovate.generic.mts.io.MTSFactory.createMTS;
import static org.apache.log4j.Logger.getLogger;
import java.util.HashSet;
import java.util.Set;
import org.apache.log4j.Logger;
import com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2Sensor;
public final class MTSConnector {
private static final Logger LOGGER = getLogger(MTSConnector.class);
private final MTS mts;
public MTSConnector(int mtsPort) {
this.mts = mts(mtsPort);
}
public MTS getMts() {
return mts;
}
private MTS mts(int mtsPort) {
// bail out early if we know specified mts port is invalid
if (mtsPort < 0) throw new IllegalArgumentException("Bad MTS port: " + mtsPort);
// create mts interface
MTS mts = createMTS();
mts.disconnect();
try {
// check there are ports available
int portCount = mts.portCount();
if (portCount <= 0) throw new IllegalStateException("No MTS ports found");
LOGGER.info("MTS: found " + portCount + " ports.");
// select the specified port
mts.currentPort(mtsPort);
String portName = mts.portName();
LOGGER.info("MTS: current port [" + mtsPort + "]: " + portName);
return mts;
} catch (RuntimeException t) {
// cleanup mts and rethrow exception
if (mts != null) mts.dispose();
throw t;
}
}
public Set<Lm2Sensor> getSensors() {
Set<Lm2Sensor> sensors = new HashSet<Lm2Sensor>();
try {
// attempt to connect to the specified device
mts.connect();
try {
// get a count of available inputs
int inputCount = mts.inputCount();
LOGGER.info("MTS: found " + inputCount + " inputs.");
if (inputCount > 0) {
for (int i = 0; i < inputCount; i++) {
// report each input found
mts.currentInput(i);
Lm2Sensor sensor = new Lm2Sensor();
sensor.setInputNumber(i);
sensor.setInputName(mts.inputName());
sensor.setDeviceName(mts.inputDeviceName());
sensor.setDeviceChannel(mts.inputDeviceChannel());
sensor.setUnits(mts.inputUnit());
sensor.setMinValue(mts.inputMinValue());
sensor.setMaxValue(mts.inputMaxValue());
sensors.add(sensor);
LOGGER.debug(String.format(
"MTS: InputNo: %02d, InputName: %s, InputType: %d, DeviceName: %s, DeviceType: %d, DeviceChannel: %d, Units: %s, Multiplier: %f, MinValue: %f, MaxValue: %f",
i, mts.inputName(), mts.inputType(), mts.inputDeviceName(), mts.inputDeviceType(), mts.inputDeviceChannel(), mts.inputUnit(), mts.inputAFRMultiplier(), mts.inputMinValue(), mts.inputMaxValue()));
}
}
else {
LOGGER.error("MTS: Error - no input channels found to log from!");
}
} finally {
mts.disconnect();
}
} finally {
mts.dispose();
}
return sensors;
}
}

View File

@ -30,17 +30,18 @@ import org.apache.log4j.Logger;
import com.romraider.logger.external.core.Stoppable; import com.romraider.logger.external.core.Stoppable;
import com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2MtsDataItem; import com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2MtsDataItem;
import com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2SensorType;
public final class MTSRunner implements Stoppable { public final class MTSRunner implements Stoppable {
private static final Logger LOGGER = getLogger(MTSRunner.class); private static final Logger LOGGER = getLogger(MTSRunner.class);
private final Map<Lm2SensorType, Lm2MtsDataItem> dataItems; private final Map<Integer, Lm2MtsDataItem> dataItems;
private final MTS mts; private final MTS mts;
private boolean running; private boolean running;
private boolean stop; private boolean stop;
public MTSRunner(int mtsPort, Map<Lm2SensorType, Lm2MtsDataItem> dataItems) { public MTSRunner(int mtsPort, Map<Integer, Lm2MtsDataItem> dataItems) {
this.mts = mts(mtsPort); // this.mts = mts(mtsPort);
MTSConnector connection = new MTSConnector(mtsPort);
this.mts = connection.getMts();
this.dataItems = dataItems; this.dataItems = dataItems;
} }
@ -63,31 +64,32 @@ public final class MTSRunner implements Stoppable {
while (running && currentTimeMillis() < timeout) sleep(100L); while (running && currentTimeMillis() < timeout) sleep(100L);
} }
private MTS mts(int mtsPort) { // private MTS mts(int mtsPort) {
// bail out early if we know specified mts port is invalid // // bail out early if we know specified mts port is invalid
if (mtsPort < 0) throw new IllegalArgumentException("Bad MTS port: " + mtsPort); // if (mtsPort < 0) throw new IllegalArgumentException("Bad MTS port: " + mtsPort);
//
// create mts interface // // create mts interface
MTS mts = createMTS(); // MTS mts = createMTS();
// mts.disconnect();
try { //
// check there are ports available // try {
int portCount = mts.portCount(); // // check there are ports available
if (portCount <= 0) throw new IllegalStateException("No MTS ports found"); // int portCount = mts.portCount();
LOGGER.info("MTS: found " + portCount + " ports."); // if (portCount <= 0) throw new IllegalStateException("No MTS ports found");
// //LOGGER.info("MTS: found " + portCount + " ports.");
// select the specified port //
mts.currentPort(mtsPort); // // select the specified port
String portName = mts.portName(); // mts.currentPort(mtsPort);
LOGGER.info("MTS: current port [" + mtsPort + "]: " + portName); // String portName = mts.portName();
// //LOGGER.info("MTS: current port [" + mtsPort + "]: " + portName);
return mts; //
} catch (RuntimeException t) { // return mts;
// cleanup mts and rethrow exception // } catch (RuntimeException t) {
if (mts != null) mts.dispose(); // // cleanup mts and rethrow exception
throw t; // if (mts != null) mts.dispose();
} // throw t;
} // }
// }
private void doRun() { private void doRun() {
try { try {
@ -97,13 +99,13 @@ public final class MTSRunner implements Stoppable {
try { try {
// get a count of available inputs // get a count of available inputs
int inputCount = mts.inputCount(); int inputCount = mts.inputCount();
LOGGER.info("MTS: found " + inputCount + " inputs."); //LOGGER.info("MTS: found " + inputCount + " inputs.");
if (inputCount > 0) { if (inputCount > 0) {
for (int i = 0; i < inputCount; i++) { for (int i = 0; i < inputCount; i++) {
// report each input found // report each input found
mts.currentInput(i); mts.currentInput(i);
LOGGER.info(String.format( LOGGER.debug(String.format(
"MTS: InputNo: %02d, InputName: %s, InputType: %d, DeviceName: %s, DeviceType: %d, DeviceChannel: %d, Units: %s, Multiplier: %f, MinValue: %f, MaxValue: %f, MinVolts: %f, MaxVolts: %f", "MTS: InputNo: %02d, InputName: %s, InputType: %d, DeviceName: %s, DeviceType: %d, DeviceChannel: %d, Units: %s, Multiplier: %f, MinValue: %f, MaxValue: %f, MinVolts: %f, MaxVolts: %f",
i, mts.inputName(), mts.inputType(), mts.inputDeviceName(), mts.inputDeviceType(), mts.inputDeviceChannel(), mts.inputUnit(), mts.inputAFRMultiplier(), mts.inputMinValue(), mts.inputMaxValue(), mts.inputMinVolt(), mts.inputMaxVolt())); i, mts.inputName(), mts.inputType(), mts.inputDeviceName(), mts.inputDeviceType(), mts.inputDeviceChannel(), mts.inputUnit(), mts.inputAFRMultiplier(), mts.inputMinValue(), mts.inputMaxValue(), mts.inputMinVolt(), mts.inputMaxVolt()));
} }
@ -118,7 +120,7 @@ public final class MTSRunner implements Stoppable {
int function = mts.inputFunction(); int function = mts.inputFunction();
int sample = mts.inputSample(); int sample = mts.inputSample();
LOGGER.trace("MTS: type = " + type + ", function = " + function + ", sample = " + sample); LOGGER.trace("MTS input: " + i + ", type = " + type + ", function = " + function + ", sample = " + sample);
float data = 0f; float data = 0f;
@ -136,7 +138,12 @@ public final class MTSRunner implements Stoppable {
if (function == 9) { if (function == 9) {
float min = mts.inputMinValue(); float min = mts.inputMinValue();
float max = mts.inputMaxValue(); float max = mts.inputMaxValue();
data = ((max - min) * ((float) sample / 1024f)) + min; float range = max - min;
data = (((float) sample / 1024f) * range) + min;
if (mts.inputDeviceChannel() == -1)
LOGGER.debug(String.format(
"MTS: InputNo: %02d, InputName: %s, InputType: %d, DeviceName: %s, DeviceType: %d, DeviceChannel: %d, Units: %s, Multiplier: %f, MinValue: %f, MaxValue: %f, MinVolts: %f, MaxVolts: %f, Function: %d, data: %f",
i, mts.inputName(), mts.inputType(), mts.inputDeviceName(), mts.inputDeviceType(), mts.inputDeviceChannel(), mts.inputUnit(), mts.inputAFRMultiplier(), mts.inputMinValue(), mts.inputMaxValue(), mts.inputMinVolt(), mts.inputMaxVolt(), mts.inputFunction(), data));
} }
} }
@ -144,20 +151,28 @@ public final class MTSRunner implements Stoppable {
// Take each sample step as .001 Lambda, // Take each sample step as .001 Lambda,
// add 0.5 (so our range is 0.5 to 1.523 for our 1024 steps), // add 0.5 (so our range is 0.5 to 1.523 for our 1024 steps),
// then multiply by the AFR multiplier // then multiply by the AFR multiplier
if (type == 0 || type == 1) { if (type == 1) {
// MTS_FUNC_LAMBDA // MTS_FUNC_LAMBDA
if (function == 0) { if (function == 0) {
//float multiplier = mts.inputAFRMultiplier(); float multiplier = mts.inputAFRMultiplier();
//data = ((float) sample / 1000f + 0.5f) * multiplier; data = ((float) sample / 1000f + 0.5f) * multiplier;
data = ((float) sample / 1000f + 0.5f);
} }
// MTS_FUNC_O2 // MTS_FUNC_O2
if (function == 1) { if (function == 1) {
data = ((float) sample / 10f); data = ((float) sample / 10f);
} }
} }
Lm2SensorType sensor = Lm2SensorType.valueOf(function, mts.inputDeviceChannel());
Lm2MtsDataItem dataItem = dataItems.get(sensor); // Lambda
// Identical to AFR, except we do not multiply for AFR.
if (type == 0) {
// MTS_FUNC_LAMBDA
if (function == 0) {
data = (float) sample / 1000f + 0.5f;
}
}
// set data for this sensor based on inputNumber
Lm2MtsDataItem dataItem = dataItems.get(i);
if (dataItem != null) dataItem.setData(data); if (dataItem != null) dataItem.setData(data);
} }
sleep(10L); sleep(10L);

View File

@ -22,22 +22,25 @@ package com.romraider.logger.external.innovate.lm2.mts.plugin;
import static com.romraider.logger.external.core.ExternalDataConvertorLoader.loadConvertors; import static com.romraider.logger.external.core.ExternalDataConvertorLoader.loadConvertors;
import com.romraider.logger.ecu.definition.EcuDataConvertor; import com.romraider.logger.ecu.definition.EcuDataConvertor;
import com.romraider.logger.ecu.definition.ExternalDataConvertorImpl;
import com.romraider.logger.external.core.DataListener; import com.romraider.logger.external.core.DataListener;
import com.romraider.logger.external.core.ExternalDataItem; import com.romraider.logger.external.core.ExternalDataItem;
import com.romraider.logger.external.core.ExternalSensorConversions; import com.romraider.logger.external.core.ExternalSensorConversions;
public final class Lm2MtsDataItem implements ExternalDataItem, DataListener { public final class Lm2MtsDataItem implements ExternalDataItem, DataListener {
private EcuDataConvertor[] convertors; //private EcuDataConvertor[] convertors;
private final String name; private final String name;
private int channel; private int channel;
private double data; private double data;
private String units;
public Lm2MtsDataItem(String name, int channel, ExternalSensorConversions... convertorList) { public Lm2MtsDataItem(String name, int channel, String units) {
super(); super();
this.name = name; this.name = name;
this.channel = channel; this.channel = channel;
convertors = new EcuDataConvertor[convertorList.length]; this.units = units;
convertors = loadConvertors(this, convertors, convertorList); // convertors = new EcuDataConvertor[convertorList.length];
// convertors = loadConvertors(this, convertors, convertorList);
} }
public String getName() { public String getName() {
@ -56,11 +59,15 @@ public final class Lm2MtsDataItem implements ExternalDataItem, DataListener {
return data; return data;
} }
public String getUnits() {
return units;
}
public void setData(double data) { public void setData(double data) {
this.data = data; this.data = data;
} }
public EcuDataConvertor[] getConvertors() { public EcuDataConvertor[] getConvertors() {
return convertors; EcuDataConvertor[] convertors = {new ExternalDataConvertorImpl(this, units, "x", "0.00")};
} return convertors; }
} }

View File

@ -19,20 +19,6 @@
package com.romraider.logger.external.innovate.lm2.mts.plugin; package com.romraider.logger.external.innovate.lm2.mts.plugin;
import static com.romraider.logger.external.core.SensorConversionsLambda.AFR_146;
import static com.romraider.logger.external.core.SensorConversionsLambda.AFR_147;
import static com.romraider.logger.external.core.SensorConversionsLambda.AFR_155;
import static com.romraider.logger.external.core.SensorConversionsLambda.AFR_172;
import static com.romraider.logger.external.core.SensorConversionsLambda.AFR_34;
import static com.romraider.logger.external.core.SensorConversionsLambda.AFR_64;
import static com.romraider.logger.external.core.SensorConversionsLambda.AFR_90;
import static com.romraider.logger.external.core.SensorConversionsLambda.LAMBDA;
import static com.romraider.logger.external.core.SensorConversionsOther.VOLTS_DC;
import static com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2SensorType.THERMALCOUPLE1;
import static com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2SensorType.THERMALCOUPLE2;
import static com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2SensorType.THERMALCOUPLE3;
import static com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2SensorType.THERMALCOUPLE4;
import static com.romraider.logger.external.innovate.lm2.mts.plugin.Lm2SensorType.WIDEBAND0;
import static com.romraider.util.ThreadUtil.runAsDaemon; import static com.romraider.util.ThreadUtil.runAsDaemon;
import static java.lang.Integer.parseInt; import static java.lang.Integer.parseInt;
import static java.util.Collections.unmodifiableList; import static java.util.Collections.unmodifiableList;
@ -42,6 +28,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.swing.Action; import javax.swing.Action;
@ -50,20 +37,27 @@ import org.apache.log4j.Logger;
import com.romraider.logger.ecu.EcuLogger; import com.romraider.logger.ecu.EcuLogger;
import com.romraider.logger.external.core.ExternalDataItem; import com.romraider.logger.external.core.ExternalDataItem;
import com.romraider.logger.external.core.ExternalDataSource; import com.romraider.logger.external.core.ExternalDataSource;
import com.romraider.logger.external.innovate.generic.mts.io.MTSConnector;
import com.romraider.logger.external.innovate.generic.mts.io.MTSRunner; import com.romraider.logger.external.innovate.generic.mts.io.MTSRunner;
public final class Lm2MtsDataSource implements ExternalDataSource { public final class Lm2MtsDataSource implements ExternalDataSource {
private static final Logger LOGGER = getLogger(Lm2MtsDataSource.class); private static final Logger LOGGER = getLogger(Lm2MtsDataSource.class);
private final Map<Lm2SensorType, Lm2MtsDataItem> dataItems = new HashMap<Lm2SensorType, Lm2MtsDataItem>(); private final Map<Integer, Lm2MtsDataItem> dataItems = new HashMap<Integer, Lm2MtsDataItem>();
private MTSRunner runner; private MTSRunner runner;
private int mtsPort = -1; private int mtsPort = 0;
{ {
dataItems.put(WIDEBAND0, new Lm2MtsDataItem("LM-2", 0, LAMBDA, AFR_147, AFR_90, AFR_146, AFR_64, AFR_155, AFR_172, AFR_34)); MTSConnector mts = new MTSConnector(mtsPort);
dataItems.put(THERMALCOUPLE1, new Lm2MtsDataItem("TC-4", 1, VOLTS_DC)); Set<Lm2Sensor> sensors = mts.getSensors();
dataItems.put(THERMALCOUPLE2, new Lm2MtsDataItem("TC-4", 2, VOLTS_DC)); dataItems.put(0, new Lm2MtsDataItem("LM-2", 0, "AFR")); // a default entry
dataItems.put(THERMALCOUPLE3, new Lm2MtsDataItem("TC-4", 3, VOLTS_DC)); for (Lm2Sensor sensor : sensors) {
dataItems.put(THERMALCOUPLE4, new Lm2MtsDataItem("TC-4", 4, VOLTS_DC)); dataItems.put(sensor.getInputNumber(), new Lm2MtsDataItem(sensor.getDeviceName(), sensor.getDeviceChannel(), sensor.getUnits()));
}
// dataItems.put(LC_1_0, new Lm2MtsDataItem("LM-2", 0, LAMBDA, AFR_147, AFR_90, AFR_146, AFR_64, AFR_155, AFR_172, AFR_34));
// dataItems.put(TC_4_1, new Lm2MtsDataItem("TC-4", 1, DEG_F));
// dataItems.put(TC_4_2, new Lm2MtsDataItem("TC-4", 2, DEG_C));
// dataItems.put(TC_4_3, new Lm2MtsDataItem("TC-4", 3, DEG_C));
// dataItems.put(TC_4_4, new Lm2MtsDataItem("TC-4", 4, DEG_C));
} }
public String getId() { public String getId() {

View File

@ -0,0 +1,89 @@
/*
* RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2010 RomRaider.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.romraider.logger.external.innovate.lm2.mts.plugin;
public class Lm2Sensor {
private int inputNumber = 0;
private String inputName = "";
private String deviceName = "";
private int deviceChannel = 0;
private String units = "";
private float minValue = 0f;
private float maxValue = 0f;
public Lm2Sensor() {
}
public int getInputNumber() {
return inputNumber;
}
public void setInputNumber(int input) {
inputNumber = input;
}
public String getInputName() {
return inputName;
}
public void setInputName(String name) {
inputName = name;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String name) {
deviceName = name;
}
public int getDeviceChannel() {
return deviceChannel;
}
public void setDeviceChannel(int channel) {
deviceChannel = channel;
}
public String getUnits() {
return units;
}
public void setUnits(String unit) {
units = unit;
}
public float getMinValue() {
return minValue;
}
public void setMinValue(float value) {
minValue = value;
}
public float getMaxValue() {
return maxValue;
}
public void setMaxValue(float value) {
maxValue = value;
}
}

View File

@ -20,33 +20,26 @@
package com.romraider.logger.external.innovate.lm2.mts.plugin; package com.romraider.logger.external.innovate.lm2.mts.plugin;
public enum Lm2SensorType { public enum Lm2SensorType {
//<sensor#>(function, channel) //SENSOR_NAME(InputNumber)
WIDEBAND0(0, 0), LC_1_0(0),
THERMALCOUPLE1(9, 1), TC_4_1(17),
THERMALCOUPLE2(9, 2), TC_4_2(18),
THERMALCOUPLE3(9, 3), TC_4_3(19),
THERMALCOUPLE4(9, 4); TC_4_4(20);
private final int value; private final int inputNumber;
private final int channel;
private Lm2SensorType(int value, int channel) { private Lm2SensorType(int inputNumber) {
this.value = value; this.inputNumber = inputNumber;
this.channel = channel;
} }
public int getValue() { public int getInputNumber() {
return value; return inputNumber;
} }
public int getChannel() { public static Lm2SensorType valueOf(int inputNumber) {
return channel;
}
public static Lm2SensorType valueOf(int value, int channel) {
for (Lm2SensorType type : values()) { for (Lm2SensorType type : values()) {
if (type.getValue() == value && if (type.getInputNumber() == inputNumber)
type.getChannel() == channel)
return type; return type;
} }
return null; return null;