[no commit message]

This commit is contained in:
Alexander Nozik 2016-01-30 19:49:15 +03:00
parent 9b85f7c5be
commit fbd6cb6844
7 changed files with 196 additions and 198 deletions

View File

@ -18,7 +18,7 @@ package inr.numass.cryotemp;
import hep.dataforge.content.Named; import hep.dataforge.content.Named;
import hep.dataforge.context.Context; import hep.dataforge.context.Context;
import hep.dataforge.control.collectors.RegularPointCollector; import hep.dataforge.control.collectors.RegularPointCollector;
import hep.dataforge.control.devices.DataDevice; import hep.dataforge.control.measurements.DataDevice;
import hep.dataforge.control.ports.PortHandler; import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.control.ports.TcpPortHandler; import hep.dataforge.control.ports.TcpPortHandler;
import hep.dataforge.data.DataFormatBuilder; import hep.dataforge.data.DataFormatBuilder;

View File

@ -22,8 +22,8 @@ import de.jensd.shichimifx.utils.SplitPaneDividerSlider;
import hep.dataforge.context.GlobalContext; import hep.dataforge.context.GlobalContext;
import hep.dataforge.control.devices.Device; import hep.dataforge.control.devices.Device;
import hep.dataforge.control.devices.DeviceListener; import hep.dataforge.control.devices.DeviceListener;
import hep.dataforge.control.devices.MeasurementDevice; import hep.dataforge.control.measurements.MeasurementDevice;
import hep.dataforge.control.devices.MeasurementListener; import hep.dataforge.control.measurements.MeasurementListener;
import hep.dataforge.exceptions.ControlException; import hep.dataforge.exceptions.ControlException;
import hep.dataforge.io.MetaFileReader; import hep.dataforge.io.MetaFileReader;
import hep.dataforge.meta.Meta; import hep.dataforge.meta.Meta;

View File

@ -17,7 +17,7 @@ package inr.numass.control.msp;
import hep.dataforge.context.Context; import hep.dataforge.context.Context;
import hep.dataforge.context.GlobalContext; import hep.dataforge.context.GlobalContext;
import hep.dataforge.control.devices.DataDevice; import hep.dataforge.control.measurements.DataDevice;
import hep.dataforge.control.ports.PortHandler; import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.control.ports.TcpPortHandler; import hep.dataforge.control.ports.TcpPortHandler;
import hep.dataforge.data.DataFormat; import hep.dataforge.data.DataFormat;

View File

@ -0,0 +1,145 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inr.numass.readvac;
import hep.dataforge.context.Context;
import hep.dataforge.control.measurements.RegularMeasurement;
import hep.dataforge.control.measurements.SingleMeasurementDevice;
import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.description.ValueDef;
import hep.dataforge.exceptions.ControlException;
import hep.dataforge.meta.Meta;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Alexander Nozik
*/
@ValueDef(name = "address")
@ValueDef(name = "channel")
@ValueDef(name = "port")
@ValueDef(name = "delay")
public class MKSVacDevice extends SingleMeasurementDevice<RegularMeasurement<Double>> {
// private static final String DELIMETER = ";FF";
private PortHandler handler;
public MKSVacDevice(String name, Context context, Meta meta) {
super(name, context, meta);
}
private String talk(String requestContent) throws ControlException {
String answer = handler.sendAndWait(String.format("@%s%s;FF", getDeviceAddress(), requestContent), timeout());
Matcher match = Pattern.compile("@253ACK(.*);FF").matcher(answer);
if (match.matches()) {
return match.group(1);
} else {
throw new ControlException(answer);
}
}
private String getDeviceAddress() {
//PENDING cache this?
return meta().getString("address", "253");
}
private int timeout() {
return meta().getInt("timeout", 400);
}
@Override
protected RegularMeasurement<Double> createMeasurement() {
return new MKSVacMeasurement();
}
@Override
protected Object calculateState(String stateName) throws ControlException {
if (handler == null) {
notifyError("No port connection", null);
return null;
}
switch (stateName) {
case "connection":
return !talk("T?").isEmpty();
case "power":
return talk("FP?").equals("ON");
default:
notifyError("State not found: " + stateName, null);
return null;
}
}
private Double readPressure(int channel) {
try {
String answer = talk("PR" + channel + "?");
if (answer == null || answer.isEmpty()) {
invalidateState("connection");
return null;
}
double res = Double.parseDouble(answer);
if (res <= 0) {
return null;
} else {
return res;
}
} catch (ControlException ex) {
invalidateState("connection");
return null;
}
}
public boolean isConnected() {
return getState("connection").booleanValue();
}
public boolean isPowerOn() {
return getState("power").booleanValue();
}
@Override
public String type() {
return meta().getString("type", "MKS vacuumeter");
}
private class MKSVacMeasurement extends RegularMeasurement<Double> {
@Override
protected Double doMeasurement() throws Exception {
String answer = talk("PR" + getChannel() + "?");
if (answer == null || answer.isEmpty()) {
invalidateState("connection");
this.progressUpdate("No connection");
return null;
}
double res = Double.parseDouble(answer);
if (res <= 0) {
this.progressUpdate("Non positive");
return null;
} else {
return res;
}
}
private int getChannel() {
return meta().getInt("channel", 5);
}
@Override
protected boolean stopOnError() {
return false;
}
@Override
protected Duration getDelay() {
return Duration.of(meta().getInt("delay", 5000), ChronoUnit.MILLIS);
}
}
}

View File

@ -1,193 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inr.numass.readvac;
import hep.dataforge.context.Context;
import hep.dataforge.control.devices.AbstractMeasurementDevice;
import hep.dataforge.control.ports.ComPortHandler;
import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.description.NodeDef;
import hep.dataforge.description.ValueDef;
import hep.dataforge.exceptions.ControlException;
import hep.dataforge.exceptions.PortException;
import hep.dataforge.meta.Meta;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jssc.SerialPortException;
/**
*
* @author Alexander Nozik
*/
@NodeDef(name = "mks", target = "metod::inr.numass.readvac.getQuery")
@ValueDef(name = "port")
@ValueDef(name = "delay")
public class MKSVacuumeterDevice extends AbstractMeasurementDevice<Double> implements PortHandler.PortController {
// private static final String QUERY = "@253PR5?;FF";
private static final String DELIMETER = ";FF";
PortHandler handler;
public MKSVacuumeterDevice(String name, Context context, Meta annotation) {
super(name, context, annotation);
}
private int timeout() {
return meta().getInt("timeout", 400);
}
private void initHandler() throws PortException {
try {
String com = meta().getString("port");
handler = new ComPortHandler(com);
handler.setDelimeter(DELIMETER);
} catch (SerialPortException ex) {
throw new PortException("Can't init port");
}
}
/**
* Get pressure query string. The default is "@253PR5?;FF"
*
* @return
*/
@ValueDef(name = "address")
@ValueDef(name = "channel")
protected String getQuery() {
String deviceAddres = meta().getString("mks.address", "253");
String channelNumber = meta().getString("mks.channel", "5");
return String.format("@%sPR%s?;FF", deviceAddres, channelNumber);
}
@Override
protected void doStart(Meta measurement) throws ControlException {
Meta meta = buildMeasurementLaminate(measurement);
initHandler();
setState("connection", checkConnection());
if (isConnected()) {
setState("power", getPowerState());
int delay = meta.getInt("delay", 5000);
executor.scheduleWithFixedDelay(() -> {
Double val = read();
measurementResult(null, val);
}, 0, delay, TimeUnit.MILLISECONDS);
} else {
getLogger().warn("No connection for " + getName());
}
}
@Override
protected void doStop() throws ControlException {
setPowerState(false);
try {
handler.close();
} catch (Exception ex) {
getLogger().error("Can not close the port", ex);
}
}
private Double read() {
if (handler == null) {
setState("connection", false);
return null;
}
try {
String answer = talk(getQuery());
if (answer == null || answer.isEmpty()) {
setState("connection", false);
return null;
}
double res = Double.parseDouble(answer);
if (res <= 0) {
return null;
} else {
return res;
}
} catch (ControlException ex) {
setState("connection", false);
return null;
}
}
private String talk(String request) throws ControlException {
String answer = handler.sendAndWait(request, timeout());
// if (answer.isEmpty()) {
// throw new ControlException("No answer from " + getName());
// }
Matcher match = Pattern.compile("@253ACK(.*);FF").matcher(answer);
if (match.matches()) {
return match.group(1);
} else {
throw new ControlException(answer);
}
}
private boolean getPowerState() throws ControlException {
String answer = talk("@253FP?;FF");
return answer.equals("ON");
}
private boolean checkConnection() {
try {
return !talk("@253T?;FF").isEmpty();
} catch (ControlException ex) {
return false;
}
}
public boolean isConnected() {
return getState("connection").booleanValue();
}
public boolean isPowerOn() {
return getState("power").booleanValue();
}
/**
* Set cathode power state and return result
*
* @param state
* @return
* @throws ControlException
*/
public void setPowerState(boolean state) throws ControlException {
boolean powerState = getPowerState();
if (state != powerState) {
if (state == true) {
String ans = talk("@253ENC!OFF;FF");
if (!ans.equals("OFF")) {
getLogger().warn("The @253ENC!OFF;FF command is not working");
}
ans = talk("@253FP!ON;FF");
if (!ans.equals("ON")) {
throw new ControlException("Can't set cathod power state");
}
} else {
String ans = talk("@253FP!OFF;FF");
if (!ans.equals("OFF")) {
throw new ControlException("Can't set cathod power state");
}
}
setState("power", getPowerState());
}
}
@Override
public void accept(String message) {
//ignore async responses
}
@Override
public void error(String errorMessage, Throwable error) {
getLogger().error(errorMessage, error);
}
}

View File

@ -0,0 +1,47 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inr.numass.scripts
import hep.dataforge.maths.integration.GaussRuleIntegrator;
import hep.dataforge.maths.integration.UnivariateIntegrator;
import inr.numass.models.LossCalculator;
import org.apache.commons.math3.analysis.UnivariateFunction
UnivariateIntegrator integrator = new GaussRuleIntegrator(400);
def exPos = 12.695;
def ionPos = 13.29;
def exW = 1.22;
def ionW = 11.99;
def exIonRatio = 3.6;
def cutoff = 25d
UnivariateFunction func = {double eps ->
if (eps <= 0) {
return 0;
}
double z1 = eps - exPos;
double ex = exIonRatio * Math.exp(-2 * z1 * z1 / exW / exW);
double z = 4 * (eps - ionPos) * (eps - ionPos);
double ion = 1 / (1 + z / ionW / ionW);
double res;
if (eps < exPos) {
res = ex;
} else {
res = Math.max(ex, ion);
}
return res;
};
//caclulating lorentz integral analythically
double tailNorm = (Math.atan((ionPos - cutoff) * 2d / ionW) + 0.5 * Math.PI) * ionW / 2d;
final double norm = integrator.integrate(func, 0d, cutoff) + tailNorm;
println 1/norm;

View File

@ -113,7 +113,6 @@ public class LossCalculator {
double tailNorm = (Math.atan((ionPos - cutoff) * 2d / ionW) + 0.5 * Math.PI) * ionW / 2d; double tailNorm = (Math.atan((ionPos - cutoff) * 2d / ionW) + 0.5 * Math.PI) * ionW / 2d;
final double norm = integrator.integrate(func, 0d, cutoff) + tailNorm; final double norm = integrator.integrate(func, 0d, cutoff) + tailNorm;
return (e) -> func.value(e) / norm; return (e) -> func.value(e) / norm;
} }
public static UnivariateFunction getSingleScatterFunction(NamedDoubleSet set) { public static UnivariateFunction getSingleScatterFunction(NamedDoubleSet set) {