From: kruland2607 Date: Sun, 8 Jan 2012 02:25:22 +0000 (+0000) Subject: moving to core/ X-Git-Tag: upstream/12.03~1^2~172 X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=c00405819a2d0a5820625c801932d92458b9dd26;p=debian%2Fopenrocket moving to core/ git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@299 180e2498-e6e9-4542-8430-84ac67f01cd8 --- diff --git a/core/src-extra/AirStart.java b/core/src-extra/AirStart.java new file mode 100644 index 00000000..9334c0d7 --- /dev/null +++ b/core/src-extra/AirStart.java @@ -0,0 +1,21 @@ +import net.sf.openrocket.simulation.SimulationStatus; +import net.sf.openrocket.simulation.exception.SimulationException; +import net.sf.openrocket.simulation.listeners.AbstractSimulationListener; +import net.sf.openrocket.util.Coordinate; + +/** + * Simulation listener that launches a rocket from a specific altitude. + */ +public class AirStart extends AbstractSimulationListener { + + /** Launch altitude */ + private static final double ALTITUDE = 1000.0; + + @Override + public void startSimulation(SimulationStatus status) throws SimulationException { + Coordinate position = status.getRocketPosition(); + position = position.add(0, 0, ALTITUDE); + status.setRocketPosition(position); + } + +} diff --git a/core/src-extra/altimeter/Alt15K.java b/core/src-extra/altimeter/Alt15K.java new file mode 100644 index 00000000..0fba1356 --- /dev/null +++ b/core/src-extra/altimeter/Alt15K.java @@ -0,0 +1,562 @@ +package altimeter; + +import gnu.io.CommPortIdentifier; +import gnu.io.PortInUseException; +import gnu.io.SerialPort; +import gnu.io.UnsupportedCommOperationException; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.nio.charset.Charset; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Enumeration; +import java.util.TimeZone; + +/** + * Class to interface the PerfectFlite Alt15K/WD altimeter. + * + * Also includes a main method that retrieves all flight profiles and saves them to files. + * + * @author Sampo Niskanen + */ + +public class Alt15K { + public static final int TIMEOUT = 500; + public static final int RWDELAY = 5; + + private static final boolean DEBUG = false; + + private static final Charset CHARSET = Charset.forName("ISO-8859-1"); + + private final CommPortIdentifier portID; + private SerialPort port = null; + private InputStream is = null; + private OutputStream os = null; + + + + @SuppressWarnings("unchecked") + public static String[] getNames() { + ArrayList list = new ArrayList();; + + Enumeration pids = CommPortIdentifier.getPortIdentifiers(); + + while (pids.hasMoreElements()) { + CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement(); + + if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL) + list.add(pid.getName()); + } + return list.toArray(new String[0]); + } + + + + @SuppressWarnings("unchecked") + public Alt15K(String name) throws IOException { + CommPortIdentifier pID = null; + + Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); + while (portIdentifiers.hasMoreElements()) { + CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); + + if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL && + pid.getName().equals(name)) { + pID = pid; + break; + } + } + + if (pID==null) { + throw new IOException("Port '"+name+"' not found."); + } + this.portID = pID; + } + + + /** + * Get altimeter flight data. The flight profile is chosen by the parameter n, + * 0 = latest flight, 1 = second latest, etc. + * + * @param n Which flight profile to use (0=newest, 1=second newest, etc) + * @return The altimeter flight data + * @throws IOException in case of IOException + * @throws PortInUseException in case of PortInUseException + */ + public AltData getData(int n) throws IOException, PortInUseException { + AltData alt = new AltData(); + ArrayList data = new ArrayList(); + byte[] buf; + byte[] buf2 = new byte[0]; + boolean identical = false; // Whether identical lines have been read + + if (DEBUG) + System.out.println(" Retrieving altimeter data n="+n); + + try { + open(); + + // Get version and position data + byte[] ver = getVersionData(); + alt.setVersion(new byte[] { ver[0],ver[1] }); + + // Calculate the position requested + if (n > 2) + n = 2; + int position = ver[2] - n; + while (position < 0) + position += 3; + + if (DEBUG) + System.out.println(" Requesting data from position "+position); + + // Request the data + write("D"); + write((byte)position); + write("PS"); + + sleep(); + + // Read preliminary data + buf = read(4); + int msl_level = combine(buf[0],buf[1]); + int datacount = combine(buf[2],buf[3]); + + if (DEBUG) + System.out.println(" Preliminary data msl="+msl_level+" count="+datacount); + + alt.setMslLevel(msl_level-6000); + alt.setDataSamples(datacount); + + if (DEBUG) + System.out.println(" Retrieving "+datacount+" samples"); + + long t = System.currentTimeMillis(); + + int count = 0; + while (count < datacount) { + sleep(); + write("G"); + sleep(); + buf = read(17); + + if (buf.length == 17) { + // Checksum = sum of all bytes + 1 + // (signedness does not change the result) + byte checksum = 1; + for (int i=0; i<16; i++) + checksum += buf[i]; + if (checksum != buf[16]) { + printBytes("ERROR: Checksum fail on data (computed="+checksum+ + " orig="+buf[16]+")",buf); + System.out.println("Ignoring error"); + } + } else { + System.err.println("ERROR: Only "+buf.length+" bytes read, should be 17"); + } + + for (int i=0; i 0) { + System.err.println("ERROR: Data available after transfer! (length="+buf.length+")"); + } + + + + + + + // Create an int[] array and set it + int[] d = new int[data.size()]; + for (int i=0; i"); + System.err.println("Files will be saved -old.log, -med and -new"); + return; + } + + + String device = null; + String[] devices = Alt15K.getNames(); + for (int i=0; i= 0) + return b; + else + return 256 + b; + } + + @SuppressWarnings("unused") + static private int combine(int a, int b) { + return 256*a + b; + } + + static private int combine(byte a, byte b) { + int val = 256*unsign(a)+unsign(b); + if (val <= 32767) + return val; + else + return val-65536; + + } + +} diff --git a/core/src-extra/altimeter/AltData.java b/core/src-extra/altimeter/AltData.java new file mode 100644 index 00000000..63314c7a --- /dev/null +++ b/core/src-extra/altimeter/AltData.java @@ -0,0 +1,81 @@ +package altimeter; + +public class AltData { + + private int mslLevel = 0; + private int samples = 0; + private int[] data = null; + private byte[] version = null; + + + public void setMslLevel(int msl) { + mslLevel = msl; + } + public int getMslLevel() { + return mslLevel; + } + + public void setDataSamples(int s) { + samples = s; + } + public int getDataSamples() { + return samples; + } + + public void setVersion(byte[] v) { + if (v==null) + version = null; + else + version = v.clone(); + } + public byte[] getVersion() { + if (version == null) + return null; + return version.clone(); + } + + public void setData(int[] data) { + if (data==null) + this.data = null; + else + this.data = data.clone(); + } + public int[] getData() { + if (data == null) + return null; + return data.clone(); + } + + public int getApogee() { + if (data == null || data.length==0) + return 0; + int max = Integer.MIN_VALUE; + for (int i=0; i max) + max = data[i]; + } + return max; + } + + @Override + public String toString() { + String s = "AltData("; + s += "MSL:"+getMslLevel()+","; + s += "Apogee:"+getApogee()+","; + s += "Samples:"+getDataSamples(); + s += ")"; + return s; + } + + public void printData() { + System.out.println(toString()+":"); + for (int i=0; i + */ + +public class RotationLogger { + private static final boolean DEBUG = false; + + private static final int BYTES = 65536; + + + private final CommPortIdentifier portID; + private SerialPort port = null; + private InputStream is = null; + private OutputStream os = null; + + + + @SuppressWarnings("unchecked") + public static String[] getNames() { + ArrayList list = new ArrayList();; + + Enumeration pids = CommPortIdentifier.getPortIdentifiers(); + + while (pids.hasMoreElements()) { + CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement(); + + if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL) + list.add(pid.getName()); + } + return list.toArray(new String[0]); + } + + + + + + @SuppressWarnings("unchecked") + public RotationLogger(String name) throws IOException { + CommPortIdentifier portID = null; + + Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); + while (portIdentifiers.hasMoreElements()) { + CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); + + if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL && + pid.getName().equals(name)) { + portID = pid; + break; + } + } + + if (portID==null) { + throw new IOException("Port '"+name+"' not found."); + } + this.portID = portID; + } + + + + + + + public void readData() throws IOException, PortInUseException { + int c; + + int[] data = new int[BYTES]; + + FileOutputStream rawdump = null; + + + try { + open(); + + System.err.println("Sending dump mode command..."); + + for (int i=0; i<16; i++) { + os.write('D'); + try { + Thread.sleep(10); + } catch (InterruptedException ignore) { } + } + + System.err.println("Waiting for response..."); + while (true) { + c = is.read(); + if (c == 'K') { + break; + } else { + System.err.printf("Received spurious c=%d\n",c); + } + } + + System.err.println("Received response."); + + + + System.err.println("Opening 'rawdump'..."); + rawdump = new FileOutputStream("rawdump"); + + + + System.err.println("Performing dump..."); + + os.write('A'); + + byte[] buffer = new byte[1024]; + int printCount = 0; + for (int count=0; count < BYTES; ) { + if ((BYTES-count) < buffer.length) { + buffer = new byte[BYTES-count]; + } + + int n = is.read(buffer); + if (n < 0) { + System.err.println("Error condition, n="+n); + return; + } + + rawdump.write(buffer, 0, n); + + for (int i=0; i 1024) { + System.err.println("Read "+count+" bytes..."); + printCount = count; + } + } + + + System.err.println("Verifying checksum..."); + int reported = is.read(); + + byte computed = 0; + for (int i=0; i < data.length; i++) { + computed += data[i]; + } + if (computed == reported) { + System.err.println("Checksum ok ("+computed+")"); + } else { + System.err.println("Error in checksum, computed="+computed+ + " reported="+reported); + } + + System.err.println("Communication done."); + + } catch (UnsupportedCommOperationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + close(); + if (rawdump != null) + rawdump.close(); + } + + convertData(data); + + } + + + + //////////// Data interpretation ////////////// + + + private static void convertData(int[] data) { + + System.err.println("Converting data..."); + + int lastBuffer = data[0xffff]; + if (lastBuffer < 0 || lastBuffer > 3) { + System.err.println("Illegal last accessed buffer: "+lastBuffer); + return; + } + System.err.println("Last used buffer: "+lastBuffer); + + for (int i=4; i>0; i--) { + int n = (lastBuffer + i) % 4; + int bufNumber = 4-i; + + convertBuffer(data, n * (BYTES/4), bufNumber); + } + + } + + + private static void convertBuffer(int[] data, int position, int bufNumber) { + int startPosition; + + startPosition = data[position + 0xfd] << 8 + data[position+0xfe]; + + // 50 samples per 128 bytes + int startTime = (startPosition -position) * 50 / 128; + + System.err.println(" Buffer "+ bufNumber + " (at position "+position+")..."); + System.err.println(" Start position "+startPosition+" time "+startTime); + + System.out.println("# Buffer "+bufNumber); + System.out.println("# Start position t="+startTime); + + + int t = 0; + for (int page = 0; page < 128; page++) { + int pageStart = position + page * 128; + + if (pageStart == startPosition) { + System.out.println("# ---clip---"); + } + + for (int i=0; i<125; i += 5) { + int sample1, sample2; + + int start = pageStart + i; +// System.err.println("page="+page+" i="+i+ +// " position="+position+" pageStart="+pageStart+" start="+start); + + sample1 = (data[start] << 2) + (data[start+1] >> 6); + sample2 = ((data[start+1] & 0x3f) << 4) + (data[start+2] >> 4); + System.out.printf("%d %4d %4d %4d\n", bufNumber, t, sample1, sample2); + t++; + + sample1 = ((data[start+2] & 0x0f) << 6) + (data[start+3] >> 2); + sample2 = ((data[start+3] & 3) << 8) + data[start+4]; + System.out.printf("%d %4d %4d %4d\n", bufNumber, t, sample1, sample2); + t++; + } + } + } + + + + private void open() throws PortInUseException, IOException, + UnsupportedCommOperationException { + + if (port != null) { + System.err.println("ERROR: open() called with port="+port); + Thread.dumpStack(); + close(); + } + + if (DEBUG) { + System.err.println(" Opening port..."); + } + + port = (SerialPort)portID.open("OpenRocket",1000); + + port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, + SerialPort.PARITY_NONE); + + port.setInputBufferSize(1); + port.setOutputBufferSize(1); + + is = port.getInputStream(); + os = port.getOutputStream(); + } + + + private void close() { + if (DEBUG) + System.err.println(" Closing port"); + + SerialPort p = port; + port = null; + is = null; + if (p != null) + p.close(); + } + + + + private static int unsign(byte b) { + if (b >= 0) + return b; + else + return 256 + b; + } + + + + + public static void main(String[] arg) throws Exception { + + if (arg.length > 2) { + System.err.println("Illegal arguments."); + return; + } + if (arg.length == 1) { + FileInputStream is = new FileInputStream(arg[0]); + byte[] buffer = new byte[BYTES]; + int n = is.read(buffer); + if (n != BYTES) { + System.err.println("Could read only "+n+" bytes"); + return; + } + + int[] data = new int[BYTES]; + for (int i=0; i + */ + +public class SerialDownload { + private static final boolean DEBUG = false; + + private static final int MAGIC = 666; + + private final CommPortIdentifier portID; + private SerialPort port = null; + private InputStream is = null; + + + + @SuppressWarnings("unchecked") + public static String[] getNames() { + ArrayList list = new ArrayList();; + + Enumeration pids = CommPortIdentifier.getPortIdentifiers(); + + while (pids.hasMoreElements()) { + CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement(); + + if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL) + list.add(pid.getName()); + } + return list.toArray(new String[0]); + } + + + + + + @SuppressWarnings("unchecked") + public SerialDownload(String name) throws IOException { + CommPortIdentifier portID = null; + + Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); + while (portIdentifiers.hasMoreElements()) { + CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); + + if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL && + pid.getName().equals(name)) { + portID = pid; + break; + } + } + + if (portID==null) { + throw new IOException("Port '"+name+"' not found."); + } + this.portID = portID; + } + + + + + + + public void readData() throws IOException, PortInUseException { + long t0 = -1; + long t; + + int previous = MAGIC; + + + try { + open(); + + System.err.println("Ready to read..."); + while (true) { + int c = is.read(); + t = System.nanoTime(); + if (t0 < 0) + t0 = t; + + System.out.printf("%10.6f %d\n", ((double)t-t0)/1000000000.0, c); + + if (previous == MAGIC) { + previous = c; + } else { + System.out.printf("# Altitude: %5d\n", previous*256 + c); + previous = MAGIC; + } + + if (c < 0) + break; + } + + + } catch (UnsupportedCommOperationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + close(); + } + } + + + + private void open() throws PortInUseException, IOException, + UnsupportedCommOperationException { + + if (port != null) { + System.err.println("ERROR: open() called with port="+port); + Thread.dumpStack(); + close(); + } + + if (DEBUG) { + System.err.println(" Opening port..."); + } + + port = (SerialPort)portID.open("OpenRocket",1000); + + port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, + SerialPort.PARITY_NONE); + + port.setInputBufferSize(1); + port.setOutputBufferSize(1); + + is = port.getInputStream(); + } + + + private void close() { + if (DEBUG) + System.err.println(" Closing port"); + + SerialPort p = port; + port = null; + is = null; + if (p != null) + p.close(); + } + + + + + + public static void main(String[] arg) throws Exception { + + String device = null; + String[] devices = SerialDownload.getNames(); + for (int i=0; i - */ - -public class Alt15K { - public static final int TIMEOUT = 500; - public static final int RWDELAY = 5; - - private static final boolean DEBUG = false; - - private static final Charset CHARSET = Charset.forName("ISO-8859-1"); - - private final CommPortIdentifier portID; - private SerialPort port = null; - private InputStream is = null; - private OutputStream os = null; - - - - @SuppressWarnings("unchecked") - public static String[] getNames() { - ArrayList list = new ArrayList();; - - Enumeration pids = CommPortIdentifier.getPortIdentifiers(); - - while (pids.hasMoreElements()) { - CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement(); - - if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL) - list.add(pid.getName()); - } - return list.toArray(new String[0]); - } - - - - @SuppressWarnings("unchecked") - public Alt15K(String name) throws IOException { - CommPortIdentifier pID = null; - - Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); - while (portIdentifiers.hasMoreElements()) { - CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); - - if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL && - pid.getName().equals(name)) { - pID = pid; - break; - } - } - - if (pID==null) { - throw new IOException("Port '"+name+"' not found."); - } - this.portID = pID; - } - - - /** - * Get altimeter flight data. The flight profile is chosen by the parameter n, - * 0 = latest flight, 1 = second latest, etc. - * - * @param n Which flight profile to use (0=newest, 1=second newest, etc) - * @return The altimeter flight data - * @throws IOException in case of IOException - * @throws PortInUseException in case of PortInUseException - */ - public AltData getData(int n) throws IOException, PortInUseException { - AltData alt = new AltData(); - ArrayList data = new ArrayList(); - byte[] buf; - byte[] buf2 = new byte[0]; - boolean identical = false; // Whether identical lines have been read - - if (DEBUG) - System.out.println(" Retrieving altimeter data n="+n); - - try { - open(); - - // Get version and position data - byte[] ver = getVersionData(); - alt.setVersion(new byte[] { ver[0],ver[1] }); - - // Calculate the position requested - if (n > 2) - n = 2; - int position = ver[2] - n; - while (position < 0) - position += 3; - - if (DEBUG) - System.out.println(" Requesting data from position "+position); - - // Request the data - write("D"); - write((byte)position); - write("PS"); - - sleep(); - - // Read preliminary data - buf = read(4); - int msl_level = combine(buf[0],buf[1]); - int datacount = combine(buf[2],buf[3]); - - if (DEBUG) - System.out.println(" Preliminary data msl="+msl_level+" count="+datacount); - - alt.setMslLevel(msl_level-6000); - alt.setDataSamples(datacount); - - if (DEBUG) - System.out.println(" Retrieving "+datacount+" samples"); - - long t = System.currentTimeMillis(); - - int count = 0; - while (count < datacount) { - sleep(); - write("G"); - sleep(); - buf = read(17); - - if (buf.length == 17) { - // Checksum = sum of all bytes + 1 - // (signedness does not change the result) - byte checksum = 1; - for (int i=0; i<16; i++) - checksum += buf[i]; - if (checksum != buf[16]) { - printBytes("ERROR: Checksum fail on data (computed="+checksum+ - " orig="+buf[16]+")",buf); - System.out.println("Ignoring error"); - } - } else { - System.err.println("ERROR: Only "+buf.length+" bytes read, should be 17"); - } - - for (int i=0; i 0) { - System.err.println("ERROR: Data available after transfer! (length="+buf.length+")"); - } - - - - - - - // Create an int[] array and set it - int[] d = new int[data.size()]; - for (int i=0; i"); - System.err.println("Files will be saved -old.log, -med and -new"); - return; - } - - - String device = null; - String[] devices = Alt15K.getNames(); - for (int i=0; i= 0) - return b; - else - return 256 + b; - } - - @SuppressWarnings("unused") - static private int combine(int a, int b) { - return 256*a + b; - } - - static private int combine(byte a, byte b) { - int val = 256*unsign(a)+unsign(b); - if (val <= 32767) - return val; - else - return val-65536; - - } - -} diff --git a/src-extra/altimeter/AltData.java b/src-extra/altimeter/AltData.java deleted file mode 100644 index 63314c7a..00000000 --- a/src-extra/altimeter/AltData.java +++ /dev/null @@ -1,81 +0,0 @@ -package altimeter; - -public class AltData { - - private int mslLevel = 0; - private int samples = 0; - private int[] data = null; - private byte[] version = null; - - - public void setMslLevel(int msl) { - mslLevel = msl; - } - public int getMslLevel() { - return mslLevel; - } - - public void setDataSamples(int s) { - samples = s; - } - public int getDataSamples() { - return samples; - } - - public void setVersion(byte[] v) { - if (v==null) - version = null; - else - version = v.clone(); - } - public byte[] getVersion() { - if (version == null) - return null; - return version.clone(); - } - - public void setData(int[] data) { - if (data==null) - this.data = null; - else - this.data = data.clone(); - } - public int[] getData() { - if (data == null) - return null; - return data.clone(); - } - - public int getApogee() { - if (data == null || data.length==0) - return 0; - int max = Integer.MIN_VALUE; - for (int i=0; i max) - max = data[i]; - } - return max; - } - - @Override - public String toString() { - String s = "AltData("; - s += "MSL:"+getMslLevel()+","; - s += "Apogee:"+getApogee()+","; - s += "Samples:"+getDataSamples(); - s += ")"; - return s; - } - - public void printData() { - System.out.println(toString()+":"); - for (int i=0; i - */ - -public class RotationLogger { - private static final boolean DEBUG = false; - - private static final int BYTES = 65536; - - - private final CommPortIdentifier portID; - private SerialPort port = null; - private InputStream is = null; - private OutputStream os = null; - - - - @SuppressWarnings("unchecked") - public static String[] getNames() { - ArrayList list = new ArrayList();; - - Enumeration pids = CommPortIdentifier.getPortIdentifiers(); - - while (pids.hasMoreElements()) { - CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement(); - - if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL) - list.add(pid.getName()); - } - return list.toArray(new String[0]); - } - - - - - - @SuppressWarnings("unchecked") - public RotationLogger(String name) throws IOException { - CommPortIdentifier portID = null; - - Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); - while (portIdentifiers.hasMoreElements()) { - CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); - - if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL && - pid.getName().equals(name)) { - portID = pid; - break; - } - } - - if (portID==null) { - throw new IOException("Port '"+name+"' not found."); - } - this.portID = portID; - } - - - - - - - public void readData() throws IOException, PortInUseException { - int c; - - int[] data = new int[BYTES]; - - FileOutputStream rawdump = null; - - - try { - open(); - - System.err.println("Sending dump mode command..."); - - for (int i=0; i<16; i++) { - os.write('D'); - try { - Thread.sleep(10); - } catch (InterruptedException ignore) { } - } - - System.err.println("Waiting for response..."); - while (true) { - c = is.read(); - if (c == 'K') { - break; - } else { - System.err.printf("Received spurious c=%d\n",c); - } - } - - System.err.println("Received response."); - - - - System.err.println("Opening 'rawdump'..."); - rawdump = new FileOutputStream("rawdump"); - - - - System.err.println("Performing dump..."); - - os.write('A'); - - byte[] buffer = new byte[1024]; - int printCount = 0; - for (int count=0; count < BYTES; ) { - if ((BYTES-count) < buffer.length) { - buffer = new byte[BYTES-count]; - } - - int n = is.read(buffer); - if (n < 0) { - System.err.println("Error condition, n="+n); - return; - } - - rawdump.write(buffer, 0, n); - - for (int i=0; i 1024) { - System.err.println("Read "+count+" bytes..."); - printCount = count; - } - } - - - System.err.println("Verifying checksum..."); - int reported = is.read(); - - byte computed = 0; - for (int i=0; i < data.length; i++) { - computed += data[i]; - } - if (computed == reported) { - System.err.println("Checksum ok ("+computed+")"); - } else { - System.err.println("Error in checksum, computed="+computed+ - " reported="+reported); - } - - System.err.println("Communication done."); - - } catch (UnsupportedCommOperationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } finally { - close(); - if (rawdump != null) - rawdump.close(); - } - - convertData(data); - - } - - - - //////////// Data interpretation ////////////// - - - private static void convertData(int[] data) { - - System.err.println("Converting data..."); - - int lastBuffer = data[0xffff]; - if (lastBuffer < 0 || lastBuffer > 3) { - System.err.println("Illegal last accessed buffer: "+lastBuffer); - return; - } - System.err.println("Last used buffer: "+lastBuffer); - - for (int i=4; i>0; i--) { - int n = (lastBuffer + i) % 4; - int bufNumber = 4-i; - - convertBuffer(data, n * (BYTES/4), bufNumber); - } - - } - - - private static void convertBuffer(int[] data, int position, int bufNumber) { - int startPosition; - - startPosition = data[position + 0xfd] << 8 + data[position+0xfe]; - - // 50 samples per 128 bytes - int startTime = (startPosition -position) * 50 / 128; - - System.err.println(" Buffer "+ bufNumber + " (at position "+position+")..."); - System.err.println(" Start position "+startPosition+" time "+startTime); - - System.out.println("# Buffer "+bufNumber); - System.out.println("# Start position t="+startTime); - - - int t = 0; - for (int page = 0; page < 128; page++) { - int pageStart = position + page * 128; - - if (pageStart == startPosition) { - System.out.println("# ---clip---"); - } - - for (int i=0; i<125; i += 5) { - int sample1, sample2; - - int start = pageStart + i; -// System.err.println("page="+page+" i="+i+ -// " position="+position+" pageStart="+pageStart+" start="+start); - - sample1 = (data[start] << 2) + (data[start+1] >> 6); - sample2 = ((data[start+1] & 0x3f) << 4) + (data[start+2] >> 4); - System.out.printf("%d %4d %4d %4d\n", bufNumber, t, sample1, sample2); - t++; - - sample1 = ((data[start+2] & 0x0f) << 6) + (data[start+3] >> 2); - sample2 = ((data[start+3] & 3) << 8) + data[start+4]; - System.out.printf("%d %4d %4d %4d\n", bufNumber, t, sample1, sample2); - t++; - } - } - } - - - - private void open() throws PortInUseException, IOException, - UnsupportedCommOperationException { - - if (port != null) { - System.err.println("ERROR: open() called with port="+port); - Thread.dumpStack(); - close(); - } - - if (DEBUG) { - System.err.println(" Opening port..."); - } - - port = (SerialPort)portID.open("OpenRocket",1000); - - port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, - SerialPort.PARITY_NONE); - - port.setInputBufferSize(1); - port.setOutputBufferSize(1); - - is = port.getInputStream(); - os = port.getOutputStream(); - } - - - private void close() { - if (DEBUG) - System.err.println(" Closing port"); - - SerialPort p = port; - port = null; - is = null; - if (p != null) - p.close(); - } - - - - private static int unsign(byte b) { - if (b >= 0) - return b; - else - return 256 + b; - } - - - - - public static void main(String[] arg) throws Exception { - - if (arg.length > 2) { - System.err.println("Illegal arguments."); - return; - } - if (arg.length == 1) { - FileInputStream is = new FileInputStream(arg[0]); - byte[] buffer = new byte[BYTES]; - int n = is.read(buffer); - if (n != BYTES) { - System.err.println("Could read only "+n+" bytes"); - return; - } - - int[] data = new int[BYTES]; - for (int i=0; i - */ - -public class SerialDownload { - private static final boolean DEBUG = false; - - private static final int MAGIC = 666; - - private final CommPortIdentifier portID; - private SerialPort port = null; - private InputStream is = null; - - - - @SuppressWarnings("unchecked") - public static String[] getNames() { - ArrayList list = new ArrayList();; - - Enumeration pids = CommPortIdentifier.getPortIdentifiers(); - - while (pids.hasMoreElements()) { - CommPortIdentifier pid = (CommPortIdentifier) pids.nextElement(); - - if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL) - list.add(pid.getName()); - } - return list.toArray(new String[0]); - } - - - - - - @SuppressWarnings("unchecked") - public SerialDownload(String name) throws IOException { - CommPortIdentifier portID = null; - - Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); - while (portIdentifiers.hasMoreElements()) { - CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); - - if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL && - pid.getName().equals(name)) { - portID = pid; - break; - } - } - - if (portID==null) { - throw new IOException("Port '"+name+"' not found."); - } - this.portID = portID; - } - - - - - - - public void readData() throws IOException, PortInUseException { - long t0 = -1; - long t; - - int previous = MAGIC; - - - try { - open(); - - System.err.println("Ready to read..."); - while (true) { - int c = is.read(); - t = System.nanoTime(); - if (t0 < 0) - t0 = t; - - System.out.printf("%10.6f %d\n", ((double)t-t0)/1000000000.0, c); - - if (previous == MAGIC) { - previous = c; - } else { - System.out.printf("# Altitude: %5d\n", previous*256 + c); - previous = MAGIC; - } - - if (c < 0) - break; - } - - - } catch (UnsupportedCommOperationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } finally { - close(); - } - } - - - - private void open() throws PortInUseException, IOException, - UnsupportedCommOperationException { - - if (port != null) { - System.err.println("ERROR: open() called with port="+port); - Thread.dumpStack(); - close(); - } - - if (DEBUG) { - System.err.println(" Opening port..."); - } - - port = (SerialPort)portID.open("OpenRocket",1000); - - port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, - SerialPort.PARITY_NONE); - - port.setInputBufferSize(1); - port.setOutputBufferSize(1); - - is = port.getInputStream(); - } - - - private void close() { - if (DEBUG) - System.err.println(" Closing port"); - - SerialPort p = port; - port = null; - is = null; - if (p != null) - p.close(); - } - - - - - - public static void main(String[] arg) throws Exception { - - String device = null; - String[] devices = SerialDownload.getNames(); - for (int i=0; i