KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > astro > telescope > LX200


1 /**
2 * The LX200 class encapsulates an LX200 telescope.
3 * Copyright (C) 1999-2001 Mark Hale
4 * @author Mark Hale
5 */

6
7 package JSci.astro.telescope;
8
9 import java.io.*;
10 import javax.comm.*;
11
12 public final class LX200 extends Object JavaDoc {
13         private SerialPort serial;
14         private InputStreamReader in;
15         private OutputStreamWriter out;
16         /**
17         * Focus rates.
18         */

19         public final static int FOCUS_FAST=1;
20         public final static int FOCUS_SLOW=2;
21         /**
22         * Focus directions.
23         */

24         public final static int FOCUS_IN=1;
25         public final static int FOCUS_OUT=2;
26         /**
27         * Slew rates.
28         */

29         public final static int SLEW_SLEW=1;
30         public final static int SLEW_FIND=2;
31         public final static int SLEW_CENTER=3;
32         public final static int SLEW_GUIDE=4;
33         /**
34         * Slew directions.
35         */

36         public final static int SLEW_NORTH=1;
37         public final static int SLEW_EAST=2;
38         public final static int SLEW_SOUTH=3;
39         public final static int SLEW_WEST=4;
40         /**
41         * Convert RA from a string to a number.
42         */

43         public static float raToFloat(String JavaDoc ra) {
44                 final float hrs=Integer.valueOf(ra.substring(0,2)).floatValue();
45                 final float mins=Integer.valueOf(ra.substring(3,5)).floatValue();
46                 final float secs=Integer.valueOf(ra.substring(6,8)).floatValue();
47                 return hrs+mins/60.0f+secs/600.0f;
48         }
49         /**
50         * Convert dec from a string to a number.
51         */

52         public static float decToFloat(String JavaDoc dec) {
53                 final float degs=Integer.valueOf(dec.substring(0,3)).floatValue();
54                 final float mins=Integer.valueOf(dec.substring(4,6)).floatValue();
55                 final float secs=Integer.valueOf(dec.substring(7,9)).floatValue();
56                 if(degs>=0.0)
57                         return degs+mins/60.0f+secs/600.0f;
58                 else
59                         return degs-mins/60.0f-secs/600.0f;
60         }
61         /**
62         * Convert alt from a string to a number.
63         */

64         public static float altToFloat(String JavaDoc alt) {
65                 final float degs=Integer.valueOf(alt.substring(0,3)).floatValue();
66                 final float mins=Integer.valueOf(alt.substring(4,6)).floatValue();
67                 final float secs=Integer.valueOf(alt.substring(7,9)).floatValue();
68                 if(degs>=0.0)
69                         return degs+mins/60.0f+secs/600.0f;
70                 else
71                         return degs-mins/60.0f-secs/600.0f;
72         }
73         /**
74         * Convert az from a string to a number.
75         */

76         public static float azToFloat(String JavaDoc az) {
77                 final float degs=Integer.valueOf(az.substring(0,3)).floatValue();
78                 final float mins=Integer.valueOf(az.substring(4,6)).floatValue();
79                 final float secs=Integer.valueOf(az.substring(7,9)).floatValue();
80                 return degs+mins/60.0f+secs/600.0f;
81         }
82         /**
83         * Constructs an LX200 object.
84         */

85         public LX200(String JavaDoc port) {
86                 try {
87                         CommPortIdentifier portID=CommPortIdentifier.getPortIdentifier(port);
88                         serial=(SerialPort)portID.open("LX200",10);
89                         serial.setSerialPortParams(9600,SerialPort.DATABITS_8,
90                                 SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
91                         in=new InputStreamReader(serial.getInputStream());
92                         out=new OutputStreamWriter(serial.getOutputStream());
93                         setHighPrecision(true);
94                         setLongFormat(true);
95                 } catch(NoSuchPortException e) {
96                         System.err.println("Port does not exist: "+e.getMessage());
97                         e.printStackTrace();
98                 } catch(PortInUseException e) {
99                         System.err.println("Port is in use by another process: "+e.getMessage());
100                         e.printStackTrace();
101                 } catch(UnsupportedCommOperationException e) {
102                 } catch(IOException e) {}
103         }
104         /**
105         * Sets high precision.
106         */

107         public synchronized void setHighPrecision(boolean setHigh) throws IOException {
108                 final boolean isHigh=toggleHighPrecision();
109                 if(setHigh!=isHigh)
110                         toggleHighPrecision();
111         }
112         private boolean toggleHighPrecision() throws IOException {
113                 char reply[]=new char[14];
114                 sendCmd("#:P#");
115                 in.read(reply,0,14);
116                 return (reply[0]=='H');
117         }
118         /**
119         * Sets long format.
120         */

121         public synchronized void setLongFormat(boolean setLong) throws IOException {
122                 final boolean isLong=isLongFormatEnabled();
123                 if(setLong!=isLong)
124                         sendCmd("#:U#");
125         }
126         private boolean isLongFormatEnabled() throws IOException {
127                 sendCmd("#:GR#");
128                 String JavaDoc reply=readString();
129                 return (reply.length()==9);
130         }
131         /**
132         * Set focus rate.
133         */

134         public synchronized void setFocusRate(int rate) throws IOException {
135                 switch(rate) {
136                         case FOCUS_FAST: sendCmd("#:FF#");break;
137                         case FOCUS_SLOW: sendCmd("#:FS#");break;
138                 }
139         }
140         /**
141         * Start focus.
142         */

143         public synchronized void startFocus(int direction) throws IOException {
144                 switch(direction) {
145                         case FOCUS_IN: sendCmd("#:F+#");break;
146                         case FOCUS_OUT: sendCmd("#:F-#");break;
147                 }
148         }
149         /**
150         * Stop focus.
151         */

152         public synchronized void stopFocus() throws IOException {
153                 sendCmd("#:FQ#");
154         }
155         /**
156         * Set slew rate.
157         */

158         public synchronized void setSlewRate(int rate) throws IOException {
159                 switch(rate) {
160                         case SLEW_SLEW: sendCmd("#:RS#");break;
161                         case SLEW_FIND: sendCmd("#:RM#");break;
162                         case SLEW_CENTER: sendCmd("#:RC#");break;
163                         case SLEW_GUIDE: sendCmd("#:RG#");break;
164                 }
165         }
166         /**
167         * Start slewing the scope.
168         * @param direction the direction to start slewing in.
169         */

170         public synchronized void startSlew(int direction) throws IOException {
171                 switch(direction) {
172                         case SLEW_NORTH: sendCmd("#:Mn#");break;
173                         case SLEW_EAST: sendCmd("#:Me#");break;
174                         case SLEW_SOUTH: sendCmd("#:Ms#");break;
175                         case SLEW_WEST: sendCmd("#:Mw#");break;
176                 }
177         }
178         /**
179         * Stop slewing the scope.
180         * @param direction the direction to stop slewing in.
181         */

182         public synchronized void stopSlew(int direction) throws IOException {
183                 switch(direction) {
184                         case SLEW_NORTH: sendCmd("#:Qn#");break;
185                         case SLEW_EAST: sendCmd("#:Qe#");break;
186                         case SLEW_SOUTH: sendCmd("#:Qs#");break;
187                         case SLEW_WEST: sendCmd("#:Qw#");break;
188                 }
189         }
190         /**
191         * Returns the current RA.
192         */

193         public synchronized String JavaDoc getRA() throws IOException {
194                 sendCmd("#:GR#");
195                 return readString();
196         }
197         /**
198         * Returns the current dec.
199         */

200         public synchronized String JavaDoc getDec() throws IOException {
201                 sendCmd("#:GD#");
202                 return readString();
203         }
204         /**
205         * Returns the current alt.
206         */

207         public synchronized String JavaDoc getAlt() throws IOException {
208                 sendCmd("#:GA#");
209                 return readString();
210         }
211         /**
212         * Returns the current az.
213         */

214         public synchronized String JavaDoc getAz() throws IOException {
215                 sendCmd("#:GZ#");
216                 return readString();
217         }
218         /**
219         * Sets the object/target coordinates.
220         */

221         public synchronized boolean setObjectCoords(String JavaDoc ra,String JavaDoc dec) throws IOException {
222                 boolean rc;
223                 sendCmd("#:Sr"+ra+"#");
224                 rc=readBoolean();
225                 sendCmd("#:Sd"+dec+"#");
226                 rc&=readBoolean();
227                 return rc;
228         }
229         /**
230         * Slew to the object coordinates.
231         * @return 0 if slew is possible,
232         * 1 if object is below the horizon,
233         * 2 if object is below the higher.
234         */

235         public synchronized int slewToObject() throws IOException {
236                 sendCmd("#:MS#");
237                 final int rc=in.read();
238                 if(rc=='0') {
239                         return 0;
240                 } else if(rc=='1') {
241                         readString();
242                         return 1;
243                 } else if(rc=='2') {
244                         readString();
245                         return 2;
246                 } else
247                         return -1;
248         }
249         /**
250         * Checks the scope's position.
251         * @param ra RA to check against.
252         * @param dec dec to check against.
253         */

254         public synchronized boolean checkPosition(float ra,float dec) throws IOException {
255                 final float raError=raToFloat(getRA())-ra;
256                 final float decError=decToFloat(getDec())-dec;
257                 return (Math.abs(raError) <= 1.0/(15.0*30.0)) && (Math.abs(decError) <= 1.0/30.0);
258         }
259         /**
260         * Check whether the scope is moving.
261         */

262         public synchronized boolean isMoving() throws IOException {
263                 final String JavaDoc oldRA=getRA();
264                 final String JavaDoc oldDec=getDec();
265                 try {
266                         Thread.sleep(20000);
267                 } catch(InterruptedException JavaDoc e) {}
268                 final String JavaDoc newRA=getRA();
269                 final String JavaDoc newDec=getDec();
270                 return !(newRA.equals(oldRA) && newDec.equals(oldDec));
271         }
272         /**
273         * Returns the local time.
274         */

275         public synchronized String JavaDoc getLocalTime() throws IOException {
276                 sendCmd("#:GL#");
277                 return readString();
278         }
279         /**
280         * Sets the local time.
281         */

282         public synchronized boolean setLocalTime(String JavaDoc time) throws IOException {
283                 sendCmd("#:SL"+time+"#");
284                 return readBoolean();
285         }
286         /**
287         * Synchronize the scope coordinates to the object coordinates.
288         */

289         public synchronized void syncCoords() throws IOException {
290                 sendCmd("#:CM#");
291                 readString();
292         }
293         /**
294         * Sends a command to the scope.
295         */

296         private void sendCmd(String JavaDoc cmd) throws IOException {
297                 out.write(cmd);
298                 out.flush();
299         }
300         /**
301         * Reads a boolean from the scope.
302         */

303         private boolean readBoolean() throws IOException {
304                 return (in.read()=='1');
305         }
306         /**
307         * Reads a string from the scope, dropping the terminating #.
308         */

309         private String JavaDoc readString() throws IOException {
310                 StringBuffer JavaDoc msg=new StringBuffer JavaDoc();
311                 int ch=in.read();
312                 while(ch!='#') {
313                         msg.append(ch);
314                         ch=in.read();
315                 }
316                 return msg.toString();
317         }
318         /**
319         * Closes the connection to the scope.
320         */

321         public synchronized void close() throws IOException {
322                 in.close();
323                 out.close();
324                 serial.close();
325         }
326 }
327
328
Popular Tags