View Javadoc
1 /* 2 * TestApp.java 3 * 4 * Copyright (c) 2003, Raben Systems, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 12 * 13 * Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * Neither the name of Raben Systems, Inc. nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 35 package com.raben.telescope.comm; 36 import java.util.Iterator; 37 import java.util.Calendar; 38 import java.util.Properties; 39 import java.util.TimeZone; 40 import java.io.IOException; 41 import java.io.BufferedReader; 42 import java.io.InputStreamReader; 43 import java.text.DateFormat; 44 import java.awt.geom.Point2D; 45 46 /*** 47 * Simple test that connects to telescope and checks that it is communicating, 48 * checks if it is aligned, if so reads altitude/azimuth, and right ascension 49 * and declination. 50 * @author Vern Raben 51 * @version $Revision: 1.2 $ $Date: 2003/09/08 16:29:53 $ 52 */ 53 public class TestApp { 54 /*** UTC Time zone */ 55 private static final TimeZone TZ = TimeZone.getTimeZone("UTC"); 56 57 /*** Date format to use */ 58 private DateFormat dateFormat 59 = DateFormat.getDateTimeInstance(); 60 61 /*** Telescope interface */ 62 private TelescopeInterface telescope; 63 64 /*** Creates a new instance of TestApp */ 65 public TestApp() { 66 dateFormat.setTimeZone(TZ); 67 } 68 69 /*** 70 * Test telescope interface 71 * @param modelStr Name of telescope model 72 * @param portName Name of communication port 73 */ 74 public void testTelescope(String modelStr, String portName) { 75 76 // First, show a solar hazard warning to user, just in case they are 77 // operating the telescope outside, during the daytime 78 /* 79 if (!isSolarHazardWarningAcknowledged()) { 80 System.out.println("TestApp exit"); 81 System.exit(0); 82 } 83 */ 84 // Second, get telescope instance from TelescopeFactory 85 if (modelStr != null) { 86 telescope = TelescopeFactory.getInstance(modelStr); 87 } else { 88 telescope = TelescopeFactory.newInstance(); 89 } 90 91 92 // Set debug to true to see commands sent and responses received 93 // telescope.setDebug(true); 94 95 try { 96 97 if (telescope == null) { 98 throw new IOException("Telescope model not found"); 99 } 100 101 System.out.println("Testing telescope model " 102 + telescope.getTelescopeModel()); 103 104 // Third, tell the interface which port to use by setting 105 // property key "com.raben.telescope.port" Not necessary if using 106 // port COM1 (the default) 107 if (portName != null) { 108 Properties props = new Properties(); 109 props.setProperty(ScopeKey.PORT, portName); 110 telescope.setProperties(props); 111 } 112 113 // Fourth, initialize communications with telescope 114 telescope.connect(); 115 /* 116 // Fifth check if communications is established 117 if ((telescope.isConnected()) && (!telescope.isCommunicating())) { 118 System.out.println( 119 "Telescope is not communicating to computer port " 120 + portName); 121 } else { 122 System.out.println("Telescope is communicating"); 123 // If commuicating, check if aligned 124 125 if (telescope.isAligned()) { 126 System.out.println("Telescope is aligned"); 127 128 // Test getting azimuth/altitude 129 Point2D azAlt = telescope.getAzimuthAltitude(); 130 System.out.println("Telescope current azimuth=" 131 + azAlt.getX() + " Altitude=" + azAlt.getY()); 132 133 // Test getting right ascension/declination 134 Point2D raDec = telescope.getRightAscensionDeclination(); 135 System.out.println("Telescope current right ascension = " 136 + raDec.getX() + " Declination = " + raDec.getY()); 137 138 testAzimuthSlewing(); 139 testRaSlewing(); 140 } else { 141 System.out.println("Telescope is not aligned"); 142 } 143 144 testGps(); 145 146 } 147 */ 148 testUpDown(); 149 testLeftRight(); 150 151 } catch (IOException e) { 152 System.out.println("Error occurred communicating with telescope " 153 + " error = " + e.getMessage()); 154 } finally { 155 156 if (telescope != null) { 157 telescope.close(); 158 } 159 } 160 161 System.out.println("Test completed"); 162 } 163 164 /*** 165 * Test azimuth/altitude slewing 166 * @exception IOException may occur 167 */ 168 private void testAzimuthSlewing() throws IOException { 169 // See if we can slew to az 0, alt 0.0 170 System.out.println("Test slewing to azimuth 0, altitude 0"); 171 Point2D az = new Point2D.Double(0.0, 0.0); 172 telescope.goToAzimuthAltitude(az); 173 waitForSlew(); 174 Point2D az2 = telescope.getAzimuthAltitude(); 175 System.out.println("Telescope slewed to azimuth " 176 + az2.getX() + " altitude=" + az2.getY()); 177 178 } 179 180 /*** 181 * Test slew to right ascension/declination 182 * @exception IOException may occur 183 */ 184 private void testRaSlewing() throws IOException { 185 System.out.println("Slew telescope to azimuth 0, altitude 45.0"); 186 telescope.goToAzimuthAltitude(new Point2D.Double(0.0, 45.0)); 187 waitForSlew(); 188 Point2D ra = telescope.getRightAscensionDeclination(); 189 ra.setLocation(ra.getX() + 6.0, ra.getY()); 190 System.out.println("Test slewing to ra " + ra.getX() 191 + " decl=" + ra.getY()); 192 193 telescope.goToRightAscensionDeclination(ra); 194 waitForSlew(); 195 Point2D ra2 = telescope.getRightAscensionDeclination(); 196 System.out.println("Telescope slewed to ra " 197 + ra2.getX() + " decl " + ra2.getY()); 198 } 199 200 /*** 201 * Test GPS commands 202 * @exception IOException may occur 203 */ 204 private void testGps() throws IOException { 205 // Check if the GPS is linked 206 boolean linked = telescope.isGpsLinked(); 207 System.out.println("Testing GPS "); 208 System.out.println(" GPS linked = " + linked); 209 210 if (linked) { 211 Calendar gpsDate = telescope.getGpsDateTime(); 212 213 if (gpsDate != null) { 214 System.out.println(" GPS date is " 215 + dateFormat.format(gpsDate.getTime()) + " UTC"); 216 } 217 218 Point2D gpsCoord = telescope.getGpsCoordinate(); 219 System.out.println(" GPS longitude is " + gpsCoord.getX() 220 + " latitude is " + gpsCoord.getY()); 221 } 222 223 System.out.println("GPS test completed"); 224 } 225 226 /*** 227 * Wait for slew to complete 228 */ 229 private void waitForSlew() { 230 boolean slewing = true; 231 System.out.print("Slewing .."); 232 233 try { 234 do { 235 System.out.print("."); 236 System.out.flush(); 237 Thread.sleep(2000L); 238 } while (telescope.isSlewing()); 239 240 } catch (InterruptedException e) { 241 // Ignore 242 } catch (IOException e) { 243 // ignore 244 } 245 System.out.println(); 246 } 247 248 249 /*** 250 * @param args the command line arguments 251 * Usage: java -cp TestApp.jar "model" port 252 * 253 * Default telescope model is "Celestron Nexstar GPS", default port is COM1 254 * If this is the case, you do not need to specify any command line args. 255 * 256 * To list available models and ports enter the following: 257 * java -cp TestApp.jar; HELP 258 */ 259 public static void main(String[] args) { 260 TestApp testApp = new TestApp(); 261 262 if ((args != null) && (args.length > 0)) { 263 String arg0 = args[0].toUpperCase(); 264 265 if (("HELP".equals(arg0)) || ("?".equals(arg0))) { 266 showHelp(); 267 } 268 269 if (args.length > 1) { 270 testApp.testTelescope(args[0], args[1]); 271 } 272 } else { 273 testApp.testTelescope(null, null); 274 } 275 276 System.out.println("TestApp exit"); 277 278 } 279 280 /*** 281 * Show how to start the Test application 282 */ 283 public static void showHelp() { 284 System.out.println("Usage: java -cp telescope.jar com.raben.telescope.comm.TestApp \"model\" port"); 285 System.out.println("Where model is one of:"); 286 287 Iterator iter = TelescopeModel.VALUES.iterator(); 288 289 while (iter.hasNext()) { 290 System.out.print(" "); 291 System.out.println((TelescopeModel) iter.next()); 292 } 293 294 System.out.println( 295 " Be sure to place double quotes (\") around model name"); 296 297 String[] ports = SerialConnection.getAvailablePorts(); 298 299 if (ports != null) { 300 System.out.println("And where port is one of "); 301 302 for (int i = 0; i < ports.length; i++) { 303 System.out.println(" " + ports[i]); 304 } 305 306 System.out.println(); 307 } 308 309 } 310 311 /*** 312 * Display warning informing user of the hazards of operating telescope 313 * outside during the daytime 314 * Ask user to acknowledge the warning 315 * @return True if user enter "Y", false otherwise 316 */ 317 public boolean isSolarHazardWarningAcknowledged() { 318 boolean acknowledge = false; 319 System.out.println("*************************************************"); 320 System.out.println("* !! WARNING !! *"); 321 System.out.println("* If you are using a telescope during daytime *"); 322 System.out.println("* be sure that you have adequate solar *"); 323 System.out.println("* filters securely and properly in place. *"); 324 System.out.println("* Failure to do so may result in blindness, *"); 325 System.out.println("* severe injuries to yourself, other persons, *"); 326 System.out.println("* or other property and equipment. *"); 327 System.out.println("* The telescope may slew and point directly at *"); 328 System.out.println("* the SUN at anytime, without warning either in *"); 329 System.out.println("* response to a command, accidentally or *"); 330 System.out.println("* otherwise. *"); 331 System.out.println("* Type \"Y\" at the prompt to acknowledge *"); 332 System.out.println("* warning. *"); 333 System.out.println("*************************************************\n"); 334 System.out.print("Do you acknowledge this warning? : "); 335 System.out.flush(); 336 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 337 338 try { 339 String str = br.readLine(); 340 341 if (str != null) { 342 343 if ("Y".equals(str.toUpperCase())) { 344 acknowledge = true; 345 } 346 } 347 } catch (IOException e) { 348 // Shouldn't occur 349 } 350 351 return acknowledge; 352 353 } 354 355 /*** Test moving telescope left and right in azimuth */ 356 public void testUpDown() throws IOException { 357 System.out.println("Testing moving telescope up"); 358 telescope.moveInAltitude(true, 9); 359 try { 360 Thread.sleep(3000L); 361 } catch (InterruptedException e) {} 362 System.out.println("send stop command"); 363 telescope.moveInAltitude(true, 0); 364 365 System.out.println("Testing moving telescope down"); 366 telescope.moveInAltitude(false, 9); 367 try { 368 Thread.sleep(3000L); 369 } catch (InterruptedException e) {} 370 System.out.println("send stop command"); 371 telescope.moveInAltitude(false, 0); 372 } 373 374 /*** Test moving telescope up and down in altitude */ 375 public void testLeftRight() throws IOException { 376 System.out.println("Testing moving telescope right"); 377 telescope.moveInAzimuth(true, 9); 378 try { 379 Thread.sleep(3000L); 380 } catch (InterruptedException e) {} 381 telescope.moveInAzimuth(true, 0); 382 383 System.out.println("Testing moving telescope left"); 384 telescope.moveInAzimuth(false, 9); 385 try { 386 Thread.sleep(3000L); 387 } catch (InterruptedException e) {} 388 telescope.moveInAzimuth(false, 0); 389 390 391 392 } 393 394 }
Back to interface details Next, another example