KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > instruments > pi > PiezoServo


1 package JSci.instruments.pi;
2
3 import JSci.instruments.*;
4
5 import java.io.*;
6
7 /** A Phisik Instrumente LVPZT amplifier/servo position controller,
8  * attached to the PC through a RS232 port.
9  */

10 public class PiezoServo extends DummyPositionControl {
11
12     private RandomAccessFile rf;
13     private String JavaDoc name;
14     private int ttyNumber;
15     private String JavaDoc type;
16     private boolean inverted;
17
18     /** the E-665 controller */
19     public static final String JavaDoc E665="E-665";
20
21     /** the E-662 controller */
22     public static final String JavaDoc E662="E-662";
23
24     /** @param n the number of the serial port (0,1,...)
25      * @param type the type of controller (E665, E662), see E665 and E662.
26      * @param inverted the direction must be inverted?
27      */

28     public PiezoServo(int n,String JavaDoc type,boolean inverted) throws IOException {
29     this.type=type;
30     this.inverted=inverted;
31     rf=new RandomAccessFile("/dev/ttyS"+n,"rw");
32     ttyNumber=n;
33     if (type.equals(E665)) write("SVO A1");
34     write("*IDN?");
35     name=read();
36     if (!name.substring(0,2).equals("PI"))
37         throw new IOException("Serial device is unknown: "+name);
38     }
39
40     private void write(String JavaDoc s) throws IOException {
41     rf.writeBytes(s);
42     rf.writeByte(10);
43     try {Thread.sleep(100);} catch (InterruptedException JavaDoc e) {}
44     }
45
46     private String JavaDoc read() throws IOException {
47     String JavaDoc s="";
48     byte b;
49     while ((b=rf.readByte())!=10) s+=(char)b;
50     return s;
51     }
52
53     public String JavaDoc toString() {
54     return getClass().getName()+
55             "[Port=/dev/ttyS"+ttyNumber+
56             ",Controller="+name+"]";
57     }
58
59     protected final void doSetPosition(double z) {
60     if (inverted) z=100.0-z;
61     try {
62         if (type.equals(E665)) write("MOV A"+z);
63         if (type.equals(E662)) write("POS "+z);
64     }
65     catch (IOException e) {
66         System.err.println("Error writing to the "+name+": "+e);
67     }
68     }
69
70     public void dispose() {
71     try {rf.close();} catch (IOException e) {}
72     }
73
74     public double getActualPosition() {
75     String JavaDoc v="";
76     try {
77         if (type.equals(E665)) write("POS?A");
78         if (type.equals(E662)) write("POS?");
79         v=read();
80     } catch (IOException e) {}
81     double z = Double.valueOf(v).doubleValue();
82     if (inverted) z=100.0-z;
83         return z;
84     }
85     
86     public void sleep() {
87     try {Thread.sleep(300);} catch (InterruptedException JavaDoc e) {}
88     }
89
90     public static void main(String JavaDoc [] args) {
91     PiezoServo p = null;
92     try {
93         p = new PiezoServo(Integer.parseInt(args[0]),E665,false);
94         System.out.println(p);
95
96         System.out.println("Position: "+p.getActualPosition());
97         p.setPosition(3.0);
98         p.sleep();
99         System.out.println("Position: "+p.getActualPosition());
100         p.setPosition(6.0);
101         p.sleep();
102         System.out.println("Position: "+p.getActualPosition());
103         p.setPosition(9.0);
104         p.sleep();
105         System.out.println("Position: "+p.getActualPosition());
106     }
107     catch (IOException e) {
108         System.out.println(p.name+" error: "+e);
109     }
110     finally {
111         if (p!=null) p.dispose();
112     }
113     }
114
115 }
116
Popular Tags