001: package JSci.instruments.pi;
002:
003: import JSci.instruments.*;
004:
005: import java.io.*;
006:
007: /** A Phisik Instrumente LVPZT amplifier/servo position controller,
008: * attached to the PC through a RS232 port.
009: */
010: public class PiezoServo extends DummyPositionControl {
011:
012: private DataInput rfi;
013: private DataOutput rfo;
014: private String name;
015: private int ttyNumber;
016: private String type;
017: private boolean inverted;
018:
019: /** the E-665 controller */
020: public static final String E665 = "E-665";
021:
022: /** the E-662 controller */
023: public static final String E662 = "E-662";
024:
025: /** @param n the number of the serial port (0,1,...)
026: * @param type the type of controller (E665, E662), see E665 and E662.
027: * @param inverted the direction must be inverted?
028: */
029: public PiezoServo(int n, String type, boolean inverted)
030: throws IOException {
031: this (n, new RandomAccessFile("/dev/ttyS" + n, "rw"), type,
032: inverted);
033: }
034:
035: /** @param n the number of the serial port (0,1,...)
036: * @param raf the file connected to the port
037: * @param type the type of controller (E665, E662), see E665 and E662.
038: * @param inverted the direction must be inverted?
039: */
040: public PiezoServo(int n, RandomAccessFile raf, String type,
041: boolean inverted) throws IOException {
042: this (n, raf, raf, type, inverted);
043: }
044:
045: /** @param n the number of the serial port (0,1,...)
046: * @param rfi the input file connected to the port
047: * @param rfo the output file connected to the port
048: * @param type the type of controller (E665, E662), see E665 and E662.
049: * @param inverted the direction must be inverted?
050: */
051: public PiezoServo(int n, DataInput rfi, DataOutput rfo,
052: String type, boolean inverted) throws IOException {
053: this .type = type;
054: this .inverted = inverted;
055: this .rfi = rfi;
056: this .rfo = rfo;
057: ttyNumber = n;
058: if (type.equals(E665))
059: write("SVO A1");
060: write("*IDN?");
061: name = read();
062: if (!name.substring(0, 2).equals("PI"))
063: throw new IOException("Serial device is unknown: " + name);
064: }
065:
066: private void write(String s) throws IOException {
067: rfo.writeBytes(s);
068: rfo.writeByte(10);
069: try {
070: Thread.sleep(100);
071: } catch (InterruptedException e) {
072: }
073: }
074:
075: private String read() throws IOException {
076: String s = "";
077: byte b;
078: while ((b = rfi.readByte()) != 10)
079: s += (char) b;
080: return s;
081: }
082:
083: public String toString() {
084: return getClass().getName() + "[Port=/dev/ttyS" + ttyNumber
085: + ",Controller=" + name + "]";
086: }
087:
088: protected final void doSetPosition(double z) {
089: if (inverted)
090: z = 100.0 - z;
091: try {
092: if (type.equals(E665))
093: write("MOV A" + z);
094: if (type.equals(E662))
095: write("POS " + z);
096: } catch (IOException e) {
097: System.err.println("Error writing to the " + name + ": "
098: + e);
099: }
100: }
101:
102: public double getActualPosition() {
103: String v = "";
104: try {
105: if (type.equals(E665))
106: write("POS?A");
107: if (type.equals(E662))
108: write("POS?");
109: v = read();
110: } catch (IOException e) {
111: }
112: double z = Double.valueOf(v).doubleValue();
113: if (inverted)
114: z = 100.0 - z;
115: return z;
116: }
117:
118: public void sleep() {
119: try {
120: Thread.sleep(300);
121: } catch (InterruptedException e) {
122: }
123: }
124:
125: public static void main(String[] args) {
126: PiezoServo p = null;
127: try {
128: p = new PiezoServo(Integer.parseInt(args[0]), E665, false);
129: System.out.println(p);
130:
131: System.out.println("Position: " + p.getActualPosition());
132: p.setPosition(3.0);
133: p.sleep();
134: System.out.println("Position: " + p.getActualPosition());
135: p.setPosition(6.0);
136: p.sleep();
137: System.out.println("Position: " + p.getActualPosition());
138: p.setPosition(9.0);
139: p.sleep();
140: System.out.println("Position: " + p.getActualPosition());
141: } catch (IOException e) {
142: System.out.println(p.name + " error: " + e);
143: }
144: }
145:
146: }
|