1 6 7 package JSci.astro.telescope; 8 9 import java.io.*; 10 import javax.comm.*; 11 12 public final class LX200 extends Object { 13 private SerialPort serial; 14 private InputStreamReader in; 15 private OutputStreamWriter out; 16 19 public final static int FOCUS_FAST=1; 20 public final static int FOCUS_SLOW=2; 21 24 public final static int FOCUS_IN=1; 25 public final static int FOCUS_OUT=2; 26 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 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 43 public static float raToFloat(String 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 52 public static float decToFloat(String 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 64 public static float altToFloat(String 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 76 public static float azToFloat(String 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 85 public LX200(String 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 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 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 reply=readString(); 129 return (reply.length()==9); 130 } 131 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 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 152 public synchronized void stopFocus() throws IOException { 153 sendCmd("#:FQ#"); 154 } 155 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 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 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 193 public synchronized String getRA() throws IOException { 194 sendCmd("#:GR#"); 195 return readString(); 196 } 197 200 public synchronized String getDec() throws IOException { 201 sendCmd("#:GD#"); 202 return readString(); 203 } 204 207 public synchronized String getAlt() throws IOException { 208 sendCmd("#:GA#"); 209 return readString(); 210 } 211 214 public synchronized String getAz() throws IOException { 215 sendCmd("#:GZ#"); 216 return readString(); 217 } 218 221 public synchronized boolean setObjectCoords(String ra,String 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 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 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 262 public synchronized boolean isMoving() throws IOException { 263 final String oldRA=getRA(); 264 final String oldDec=getDec(); 265 try { 266 Thread.sleep(20000); 267 } catch(InterruptedException e) {} 268 final String newRA=getRA(); 269 final String newDec=getDec(); 270 return !(newRA.equals(oldRA) && newDec.equals(oldDec)); 271 } 272 275 public synchronized String getLocalTime() throws IOException { 276 sendCmd("#:GL#"); 277 return readString(); 278 } 279 282 public synchronized boolean setLocalTime(String time) throws IOException { 283 sendCmd("#:SL"+time+"#"); 284 return readBoolean(); 285 } 286 289 public synchronized void syncCoords() throws IOException { 290 sendCmd("#:CM#"); 291 readString(); 292 } 293 296 private void sendCmd(String cmd) throws IOException { 297 out.write(cmd); 298 out.flush(); 299 } 300 303 private boolean readBoolean() throws IOException { 304 return (in.read()=='1'); 305 } 306 309 private String readString() throws IOException { 310 StringBuffer msg=new StringBuffer (); 311 int ch=in.read(); 312 while(ch!='#') { 313 msg.append(ch); 314 ch=in.read(); 315 } 316 return msg.toString(); 317 } 318 321 public synchronized void close() throws IOException { 322 in.close(); 323 out.close(); 324 serial.close(); 325 } 326 } 327 328 | Popular Tags |