Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Magnetometer.java

Go to the documentation of this file.
00001 /*
00002  * Magnetometer.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki.squid;
00024 
00025 import ikayaki.Settings;
00026 
00027 import java.util.Stack;
00028 import java.util.concurrent.SynchronousQueue;
00029 import java.util.concurrent.TimeUnit;
00030 
00036 public class Magnetometer implements SerialIOListener {
00037 
00041     private Stack<String> messageBuffer;
00042 
00046     private SynchronousQueue<String> queue;
00047     private int pollTimeout = 60;
00048 
00052     protected SerialIO serialIO;
00053 
00054     private boolean waitingForMessage = false;
00055 
00056     private boolean measuring = false;
00057 
00058 
00065     public Magnetometer() throws SerialIOException {
00066         this.serialIO = SerialIO.openPort(new SerialParameters(Settings.getMagnetometerPort(), 1200, 0, 0, 8, 1, 0));
00067         serialIO.addSerialIOListener(this);
00068         messageBuffer = new Stack<String>();
00069         queue = new SynchronousQueue<String>();
00070         try {
00071             //Original sets range and filter to 1x and disable fast-slew, TODO: check if right, do we need status confirm?
00072             serialIO.writeMessage("XCR1\r");
00073             serialIO.writeMessage("XCF1\r");
00074             serialIO.writeMessage("XCSE\r");
00075             serialIO.writeMessage("YCR1\r");
00076             serialIO.writeMessage("YCF1\r");
00077             serialIO.writeMessage("YCSE\r");
00078             serialIO.writeMessage("ZCR1\r");
00079             serialIO.writeMessage("ZCF1\r");
00080             serialIO.writeMessage("ZCSE\r");
00081             //and original resets all
00082             configure('A', 'L', 'P');
00083             configure('A', 'L', 'C');
00084             resetCounter('A');
00085 
00086         } catch (SerialIOException e) {
00087             System.err.println("Error using port in degausser:");
00088             e.printStackTrace();
00089         }
00090     }
00091 
00096     public void updateSettings() {
00097         //no settings for this.. really. Only COM port.
00098     }
00099 
00105     protected void reset(char axis) {
00106         if (axis != 'X' && axis != 'Y' && axis != 'Z' && axis != 'A') {
00107             throw new IllegalArgumentException("axis = " + axis);
00108         }
00109         try {
00110             this.serialIO.writeMessage(axis + "R\r");
00111         } catch (SerialIOException e) {
00112             e.printStackTrace();
00113         }
00114     }
00115 
00121     protected void resetCounter(char axis) {
00122         if (axis != 'X' && axis != 'Y' && axis != 'Z' && axis != 'A') {
00123             throw new IllegalArgumentException("axis = " + axis);
00124         }
00125         try {
00126             this.serialIO.writeMessage(axis + "RC\r");
00127         } catch (SerialIOException ex) {
00128             System.err.println(ex);
00129         }
00130     }
00131 
00149     protected void configure(char axis, char subcommand, char option) {
00150         if (axis != 'X' && axis != 'Y' && axis != 'Z' && axis != 'A') {
00151             throw new IllegalArgumentException("axis = " + axis);
00152         }
00153         try {
00154             this.serialIO.writeMessage(axis + "C" + subcommand + option + "\r");
00155         } catch (SerialIOException ex) {
00156             System.err.println(ex);
00157         }
00158     }
00159 
00163     protected void latchAnalog(char axis) {
00164         if (axis != 'X' && axis != 'Y' && axis != 'Z' && axis != 'A') {
00165             throw new IllegalArgumentException("axis = " + axis);
00166         }
00167         try {
00168             this.serialIO.writeMessage(axis + "LD\r");
00169         } catch (SerialIOException e) {
00170             e.printStackTrace();
00171         }
00172     }
00173 
00177     protected void latchCounter(char axis) {
00178         if (axis != 'X' && axis != 'Y' && axis != 'Z' && axis != 'A') {
00179             throw new IllegalArgumentException("axis = " + axis);
00180         }
00181         try {
00182             this.serialIO.writeMessage(axis + "LC\r");
00183         } catch (SerialIOException e) {
00184             e.printStackTrace();
00185         }
00186     }
00187 
00201     protected String getData(char axis, char command, String datavalues) {
00202         if (axis != 'X' && axis != 'Y' && axis != 'Z') {
00203             throw new IllegalArgumentException("axis = " + axis);
00204         }
00205         if (command == 'D' || command == 'C') {
00206             try {
00207                 this.serialIO.writeMessage(axis + "S" + command + "\r");
00208             } catch (SerialIOException ex) {
00209                 System.err.println(ex);
00210                 return null;
00211             }
00212         } else if (command == 'S') {
00213             try {
00214                 this.serialIO.writeMessage(axis + "S" + command + datavalues + "\r");
00215             } catch (SerialIOException ex) {
00216                 System.err.println(ex);
00217                 return null;
00218             }
00219         } else {
00220             throw new IllegalArgumentException("command = " + command);
00221         }
00222         waitingForMessage = true;
00223         String answer = null;
00224         try {
00225             answer = (String) queue.poll(pollTimeout, TimeUnit.SECONDS);
00226         } catch (InterruptedException e) {
00227             e.printStackTrace();
00228         }
00229         waitingForMessage = false;
00230         return answer;
00231     }
00232 
00238     public void pulseReset(char axis) {
00239         configure(axis, 'L', 'P');
00240         configure(axis, 'L', 'C');
00241     }
00242 
00248     public void clearFlux(char axis) {
00249         resetCounter(axis);
00250     }
00251 
00257     public double[] readData() {
00258 
00259         // wait for magnetometer to settle down
00260         try {
00261             Thread.sleep(1000);
00262         } catch (InterruptedException e) {
00263             e.printStackTrace();
00264         }
00265 
00266         measuring = true;
00267         latchCounter('A');
00268 
00269         //read all latched counter values
00270         double counterX = Double.parseDouble(getData('X', 'C', ""));
00271         double counterY = Double.parseDouble(getData('Y', 'C', ""));
00272         double counterZ = Double.parseDouble(getData('Z', 'C', ""));
00273 
00274         latchAnalog('A');
00275 
00276         //Maybe need to read many times and take mean value.
00277         //But we do it only ones (default for old software)
00278         //read all latched analog values
00279         double analogX = Double.parseDouble(getData('X', 'D', ""));
00280         double analogY = Double.parseDouble(getData('Y', 'D', ""));
00281         double analogZ = Double.parseDouble(getData('Z', 'D', ""));
00282 
00283         double[] result = new double[3];
00284 
00285         //when to use flux counting and when not? TODO
00286         result[0] = (counterX + analogX) * Settings.getMagnetometerXAxisCalibration();
00287         result[1] = (counterY + analogY) * Settings.getMagnetometerYAxisCalibration();
00288         result[2] = (counterZ + analogZ) * Settings.getMagnetometerZAxisCalibration();
00289 
00290         measuring = false;
00291         return result;
00292     }
00293 
00294     public boolean isMeasuring() {
00295         return measuring;
00296     }
00297 
00304     public char[] getFilters() {
00305         char[] data = new char[3];
00306 
00307         String answer = getData('X', 'S', "F");
00308         data[0] = answer.charAt(1);
00309 
00310         answer = getData('Y', 'S', "F");
00311         data[1] = answer.charAt(1);
00312 
00313         answer = getData('Z', 'S', "F");
00314         data[2] = answer.charAt(1);
00315 
00316         return data;
00317     }
00318 
00325     public char[] getRange() {
00326         char[] data = new char[3];
00327 
00328         String answer = getData('X', 'S', "R");
00329         data[0] = answer.charAt(1);
00330 
00331         answer = getData('Y', 'S', "R");
00332         data[1] = answer.charAt(1);
00333 
00334         answer = getData('Z', 'S', "R");
00335         data[2] = answer.charAt(1);
00336 
00337         return data;
00338 
00339     }
00340 
00346     public boolean[] getSlew() {
00347         boolean[] data = new boolean[3];
00348 
00349         String answer = this.getData('X', 'S', "S");
00350         data[0] = (!answer.equals("SD"));
00351 
00352         answer = this.getData('Y', 'S', "S");
00353         data[1] = (!answer.equals("SD"));
00354 
00355         answer = this.getData('Z', 'S', "S");
00356         data[2] = (!answer.equals("SD"));
00357 
00358         return data;
00359     }
00360 
00366     public boolean[] getLoop() {
00367         boolean[] data = new boolean[3];
00368 
00369         String answer = this.getData('X', 'S', "L");
00370         data[0] = (answer.equals("LO"));
00371 
00372         answer = this.getData('Y', 'S', "L");
00373         data[1] = (answer.equals("LO"));
00374 
00375         answer = this.getData('Z', 'S', "L");
00376         data[2] = (answer.equals("LO"));
00377 
00378         return data;
00379     }
00380 
00386     public boolean isOK() {
00387         if (serialIO != null) {
00388             return true;
00389         }
00390         return false;
00391     }
00392 
00393     public void serialIOEvent(SerialIOEvent event) {
00394         //TODO: problem when Degausser and Magnetometer uses same port :/
00395         String message = event.getCleanMessage();
00396         if (message != null) {
00397             if (waitingForMessage) {
00398                 try {
00399                     queue.put(message);
00400                 } catch (InterruptedException e) {
00401                     System.err.println("Interrupted Magnetometer message event");
00402                 } catch (NullPointerException e) {
00403                     System.err.println("Null from SerialEvent in Magnetometer");
00404                 }
00405             }
00406             messageBuffer.add(message);
00407         }
00408     }
00409 }

Generated on Fri May 6 16:00:33 2005 for Squid by  doxygen 1.4.1