Second guiding tool

This commit is contained in:
gac-S_Changer
2021-08-03 15:18:30 +02:00
parent ac1719b6af
commit 4239b7fb57
149 changed files with 3566 additions and 3606 deletions
+90 -90
View File
@@ -1,90 +1,90 @@
import ch.psi.pshell.device.Readable;
import ch.psi.pshell.serial.SerialPortDevice;
import ch.psi.pshell.serial.SerialPortDeviceConfig;
import static ch.psi.utils.BitMask.*;
import ch.psi.utils.Convert;
import ch.psi.utils.State;
import java.io.IOException;
/*
*
*/
public class LaserUE extends SerialPortDevice {
final Readable readable;
final double range;
final double offset;
public LaserUE(String name, String port, double range, double offset) {
super(name, port, 921600, SerialPortDeviceConfig.DataBits.DB_8, SerialPortDeviceConfig.StopBits.SB_1, SerialPortDeviceConfig.Parity.None);
this.range = range;
this.offset = offset;
this.setMode(Mode.FullDuplex);
readable = new Readable() {
@Override
public Object read() throws IOException, InterruptedException {
return take();
}
@Override
public String getName() {
return LaserUE.this.getName() + "_readout";
}
};
}
@Override
protected void doInitialize() throws IOException, InterruptedException {
super.doInitialize();
//TODO: Start DAQ in ILD1320: http://www.micro-epsilon.com/download/manuals/man--optoNCDT-1320--en.pdf
setState(State.Ready);
write("OUTPUT RS422\n\r");
}
public Readable getReadable() {
return readable;
}
int value = 0;
int count = 0;
@Override
protected void onByte(int rx) {
int index = ((rx & BIT7) > 0) ? 2 : (((rx & BIT6) > 0) ? 1 : 0);
if (count == index) {
if (index == 0) {
value = rx & 0x3F;
count = 1;
} else if (index == 1) {
value = ((rx & 0x3F) << 6) + value;
count = 2;
} else if (index == 2) {
value = ((rx & 0x0F) << 12) + value;
//double val = ((double)value)/1000;
double val = (0.01) * (102.0 * value / 65520.0 - 1) * range;
if ((val <= 0.0) || (val > range)) {
val = Double.NaN;
} else {
val+=offset;
val = Convert.roundDouble(val, 3);
}
setCache(val);
count = 0;
} else {
count = 0;
}
} else {
count = 0;
}
}
public static void main(String[] args) throws Exception {
LaserUE l = new LaserUE("UE", "COM3", 50.0, 35.0);
l.setMonitored(true);
l.initialize();
Thread.sleep(10000);
l.close();
}
}
import ch.psi.pshell.device.Readable;
import ch.psi.pshell.serial.SerialPortDevice;
import ch.psi.pshell.serial.SerialPortDeviceConfig;
import static ch.psi.utils.BitMask.*;
import ch.psi.utils.Convert;
import ch.psi.utils.State;
import java.io.IOException;
/*
*
*/
public class LaserUE extends SerialPortDevice {
final Readable readable;
final double range;
final double offset;
public LaserUE(String name, String port, double range, double offset) {
super(name, port, 921600, SerialPortDeviceConfig.DataBits.DB_8, SerialPortDeviceConfig.StopBits.SB_1, SerialPortDeviceConfig.Parity.None);
this.range = range;
this.offset = offset;
this.setMode(Mode.FullDuplex);
readable = new Readable() {
@Override
public Object read() throws IOException, InterruptedException {
return take();
}
@Override
public String getName() {
return LaserUE.this.getName() + "_readout";
}
};
}
@Override
protected void doInitialize() throws IOException, InterruptedException {
super.doInitialize();
//TODO: Start DAQ in ILD1320: http://www.micro-epsilon.com/download/manuals/man--optoNCDT-1320--en.pdf
setState(State.Ready);
write("OUTPUT RS422\n\r");
}
public Readable getReadable() {
return readable;
}
int value = 0;
int count = 0;
@Override
protected void onByte(int rx) {
int index = ((rx & BIT7) > 0) ? 2 : (((rx & BIT6) > 0) ? 1 : 0);
if (count == index) {
if (index == 0) {
value = rx & 0x3F;
count = 1;
} else if (index == 1) {
value = ((rx & 0x3F) << 6) + value;
count = 2;
} else if (index == 2) {
value = ((rx & 0x0F) << 12) + value;
//double val = ((double)value)/1000;
double val = (0.01) * (102.0 * value / 65520.0 - 1) * range;
if ((val <= 0.0) || (val > range)) {
val = Double.NaN;
} else {
val+=offset;
val = Convert.roundDouble(val, 3);
}
setCache(val);
count = 0;
} else {
count = 0;
}
} else {
count = 0;
}
}
public static void main(String[] args) throws Exception {
LaserUE l = new LaserUE("UE", "COM3", 50.0, 35.0);
l.setMonitored(true);
l.initialize();
Thread.sleep(10000);
l.close();
}
}
+149 -149
View File
@@ -1,149 +1,149 @@
import ch.psi.pshell.imaging.SourceBase;
import ch.psi.pshell.imaging.SourceConfig;
import ch.psi.pshell.imaging.Utils;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
/**
* Image source receive frames from a mjpeg server.
*/
public class MjpegSource2 extends SourceBase {
final String url;
final boolean flushOnUpdate;
public MjpegSource2(String name, String url) {
this(name, url, false);
}
public MjpegSource2(String name, String url, boolean flushOnUpdate) {
super(name, new SourceConfig());
this.url = url;
this.flushOnUpdate = flushOnUpdate;
}
InputStream stream;
@Override
protected void doInitialize() throws IOException, InterruptedException {
super.doInitialize();
URL aux = new URL(url);
stream = aux.openStream();
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
}
Thread monitoredThread;
@Override
protected void doSetMonitored(boolean value) {
if (value && (monitoredThread == null)) {
monitoredThread = new Thread(() -> {
try {
while (true) {
try {
doUpdate();
Thread.sleep(1);
} catch (IOException ex) {
getLogger().log(Level.FINE, null, ex);
}
}
} catch (InterruptedException ex) {
return;
}
});
monitoredThread.setDaemon(true);
monitoredThread.start();
} else if (!value && (monitoredThread != null)) {
monitoredThread.interrupt();
monitoredThread = null;
}
}
final byte[] START_OF_FRAME = {(byte) 0xFF, (byte) 0xD8};
final byte[] END_OF_FRAME = {(byte) 0xFF, (byte) 0xD9};
final int MAX_FRAME_SIZE = 512 * 1024;
@Override
protected void doUpdate() throws IOException, InterruptedException {
byte[] data = null;
if (stream != null) {
if (flushOnUpdate) {
flush();
}
try {
data = readData();
} catch (EOFException ex) {
//Try to reopen stream
doInitialize();
data = readData();
}
}
if (data == null) {
pushImage(null);
} else {
BufferedImage img = Utils.newImage(data);
pushImage(img);
}
}
byte[] readData() throws IOException {
if (stream != null) {
stream.mark(MAX_FRAME_SIZE);
int startOfFrame = waitBytes(START_OF_FRAME) - START_OF_FRAME.length;
if (startOfFrame >= 0) {
int endOfFrame = waitBytes(END_OF_FRAME);
if (endOfFrame >= 0) {
stream.reset();
stream.skip(startOfFrame);
int length = endOfFrame ;//- END_OF_FRAME.length ;
byte[] data = new byte[length];
stream.read(data, 0, length);
return data;
}
}
}
return null;
}
int waitBytes(byte[] data) throws IOException {
int index = 0;
int dataPos = 0;
while (true) {
int ret = stream.read();
if (ret < 0) {
throw new EOFException();
}
byte value = (byte) ret;
if (value == data[dataPos]) {
dataPos++;
if (dataPos == data.length) {
return (index + 1);
}
} else {
dataPos = 0;
}
index++;
if (index >= MAX_FRAME_SIZE) {
return -1;
}
}
}
public void flush() throws IOException {
//stream.skip(stream.available());
//TODO: Skipping won't make the current image to be displayed
stream.close();
stream = new URL(url).openStream();
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
}
}
import ch.psi.pshell.imaging.SourceBase;
import ch.psi.pshell.imaging.SourceConfig;
import ch.psi.pshell.imaging.Utils;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
/**
* Image source receive frames from a mjpeg server.
*/
public class MjpegSource2 extends SourceBase {
final String url;
final boolean flushOnUpdate;
public MjpegSource2(String name, String url) {
this(name, url, false);
}
public MjpegSource2(String name, String url, boolean flushOnUpdate) {
super(name, new SourceConfig());
this.url = url;
this.flushOnUpdate = flushOnUpdate;
}
InputStream stream;
@Override
protected void doInitialize() throws IOException, InterruptedException {
super.doInitialize();
URL aux = new URL(url);
stream = aux.openStream();
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
}
Thread monitoredThread;
@Override
protected void doSetMonitored(boolean value) {
if (value && (monitoredThread == null)) {
monitoredThread = new Thread(() -> {
try {
while (true) {
try {
doUpdate();
Thread.sleep(1);
} catch (IOException ex) {
getLogger().log(Level.FINE, null, ex);
}
}
} catch (InterruptedException ex) {
return;
}
});
monitoredThread.setDaemon(true);
monitoredThread.start();
} else if (!value && (monitoredThread != null)) {
monitoredThread.interrupt();
monitoredThread = null;
}
}
final byte[] START_OF_FRAME = {(byte) 0xFF, (byte) 0xD8};
final byte[] END_OF_FRAME = {(byte) 0xFF, (byte) 0xD9};
final int MAX_FRAME_SIZE = 512 * 1024;
@Override
protected void doUpdate() throws IOException, InterruptedException {
byte[] data = null;
if (stream != null) {
if (flushOnUpdate) {
flush();
}
try {
data = readData();
} catch (EOFException ex) {
//Try to reopen stream
doInitialize();
data = readData();
}
}
if (data == null) {
pushImage(null);
} else {
BufferedImage img = Utils.newImage(data);
pushImage(img);
}
}
byte[] readData() throws IOException {
if (stream != null) {
stream.mark(MAX_FRAME_SIZE);
int startOfFrame = waitBytes(START_OF_FRAME) - START_OF_FRAME.length;
if (startOfFrame >= 0) {
int endOfFrame = waitBytes(END_OF_FRAME);
if (endOfFrame >= 0) {
stream.reset();
stream.skip(startOfFrame);
int length = endOfFrame ;//- END_OF_FRAME.length ;
byte[] data = new byte[length];
stream.read(data, 0, length);
return data;
}
}
}
return null;
}
int waitBytes(byte[] data) throws IOException {
int index = 0;
int dataPos = 0;
while (true) {
int ret = stream.read();
if (ret < 0) {
throw new EOFException();
}
byte value = (byte) ret;
if (value == data[dataPos]) {
dataPos++;
if (dataPos == data.length) {
return (index + 1);
}
} else {
dataPos = 0;
}
index++;
if (index >= MAX_FRAME_SIZE) {
return -1;
}
}
}
public void flush() throws IOException {
//stream.skip(stream.available());
//TODO: Skipping won't make the current image to be displayed
stream.close();
stream = new URL(url).openStream();
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
}
}
+14 -14
View File
@@ -1,14 +1,14 @@
import ch.psi.pshell.device.DeviceConfig;
/**
*
*/
public class SmartMagnetConfig extends DeviceConfig{
public double holdingCurrent;
public double restingCurrent;
public double mountCurrent;
public double unmountCurrent;
public double remanenceCurrent;
}
import ch.psi.pshell.device.DeviceConfig;
/**
*
*/
public class SmartMagnetConfig extends DeviceConfig{
public double holdingCurrent;
public double restingCurrent;
public double mountCurrent;
public double unmountCurrent;
public double remanenceCurrent;
}
+26 -26
View File
@@ -1,26 +1,26 @@
/*
* 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.
*/
/**
*
* @author gac-S_Changer
*/
public class TestZMQ {
public static void main(String[] args) {
String server = "raspberrypi:5556";
org.zeromq.ZMQ.Context context = org.zeromq.ZMQ.context(1);
org.zeromq.ZMQ.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB);
subscriber.connect("tcp://" + server);
subscriber.subscribe("Status".getBytes());
while (!Thread.currentThread().isInterrupted()) {
String type = subscriber.recvStr();
String contents = subscriber.recvStr();
System.out.println(type + " : " + contents);
}
subscriber.close();
context.term();
}
}
/*
* 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.
*/
/**
*
* @author gac-S_Changer
*/
public class TestZMQ {
public static void main(String[] args) {
String server = "raspberrypi:5556";
org.zeromq.ZMQ.Context context = org.zeromq.ZMQ.context(1);
org.zeromq.ZMQ.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB);
subscriber.connect("tcp://" + server);
subscriber.subscribe("Status".getBytes());
while (!Thread.currentThread().isInterrupted()) {
String type = subscriber.recvStr();
String contents = subscriber.recvStr();
System.out.println(type + " : " + contents);
}
subscriber.close();
context.term();
}
}
+96 -96
View File
@@ -1,96 +1,96 @@
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.State;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*/
public class gui extends Panel {
public gui() {
initComponents();
}
//Overridable callbacks
@Override
public void onInitialize(int runCount) {
}
@Override
public void onStateChange(State state, State former) {
}
@Override
public void onExecutedFile(String fileName, Object result) {
}
//Callback to perform update - in event thread
@Override
protected void doUpdate() {
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
renderer1 = new ch.psi.pshell.imaging.Renderer();
linePlotJFree1 = new ch.psi.pshell.plot.LinePlotJFree();
jButton1.setText("Abort");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(renderer1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(82, 82, 82))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(linePlotJFree1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(250, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(renderer1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(linePlotJFree1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try {
abort();
} catch (InterruptedException ex) {
Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton1ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private ch.psi.pshell.plot.LinePlotJFree linePlotJFree1;
private ch.psi.pshell.imaging.Renderer renderer1;
// End of variables declaration//GEN-END:variables
}
/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.State;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*/
public class gui extends Panel {
public gui() {
initComponents();
}
//Overridable callbacks
@Override
public void onInitialize(int runCount) {
}
@Override
public void onStateChange(State state, State former) {
}
@Override
public void onExecutedFile(String fileName, Object result) {
}
//Callback to perform update - in event thread
@Override
protected void doUpdate() {
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
renderer1 = new ch.psi.pshell.imaging.Renderer();
linePlotJFree1 = new ch.psi.pshell.plot.LinePlotJFree();
jButton1.setText("Abort");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(renderer1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(82, 82, 82))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(linePlotJFree1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(250, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(renderer1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(linePlotJFree1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try {
abort();
} catch (InterruptedException ex) {
Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton1ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private ch.psi.pshell.plot.LinePlotJFree linePlotJFree1;
private ch.psi.pshell.imaging.Renderer renderer1;
// End of variables declaration//GEN-END:variables
}