[Firebug-cvs] fireboard/beta/fireworks/tools/java/net/sf/firebug/DataCollection DataCollect.java,1.2
Brought to you by:
doolin
From: Karthik D. <da...@us...> - 2005-08-29 18:52:37
|
Update of /cvsroot/firebug/fireboard/beta/fireworks/tools/java/net/sf/firebug/DataCollection In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11552/net/sf/firebug/DataCollection Modified Files: DataCollect.java Log Message: Working pressure and temperature calibration Index: DataCollect.java =================================================================== RCS file: /cvsroot/firebug/fireboard/beta/fireworks/tools/java/net/sf/firebug/DataCollection/DataCollect.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** DataCollect.java 29 Aug 2005 00:25:27 -0000 1.2 --- DataCollect.java 29 Aug 2005 18:52:25 -0000 1.3 *************** *** 1,43 **** - /* - * Author: Mike Chen <mik...@cs...> - * Inception Date: October 22th, 2000 - * - * This software is copyrighted by Mike Chen and the Regents of - * the University of California. The following terms apply to all - * files associated with the software unless explicitly disclaimed in - * individual files. - * - * The authors hereby grant permission to use this software without - * fee or royalty for any non-commercial purpose. The authors also - * grant permission to redistribute this software, provided this - * copyright and a copy of this license (for reference) are retained - * in all distributed copies. - * - * For commercial use of this software, contact the authors. - * - * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY - * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES - * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY - * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE - * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE - * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR - * MODIFICATIONS. - * - * Copyright (c) 2005 Moteiv Corporation - * All rights reserved. - * - * This file is distributed under the terms in the attached MOTEIV-LICENSE - * file. If you do not find these files, copies can be found at - * http://www.moteiv.com/MOTEIV-LICENSE.txt and by emailing in...@mo.... - * - * Modified September 11, 2004 by Rob Szewczyk <in...@mo...> - * to display readings from the contrib/moteiv/apps/PressureTest application - */ - //============================================================================== //=== DataCollect.java ============================================== --- 1,2 ---- *************** *** 56,59 **** --- 15,19 ---- * @author <A HREF="http://www.cs.berkeley.edu/~mikechen/">Mike Chen</A> * (<A HREF="mailto:mik...@cs...">mik...@cs...</A>) + * customized for data collection applicatoin by Karthik Dantu (da...@us...) * @since 1.1.6 * *************** *** 62,70 **** ! public class DataCollect implements MessageListener, Runnable { ! //========================================================================= //=== CONSTANTS ======================================================= ! private static int MSG_SIZE = 36; // 4 header bytes, 30 msg bytes, 2 crc // bytes; 2 strength bytes are not --- 22,30 ---- ! public class DataCollect implements MessageListener { //, Runnable { ! //========================================================================= //=== CONSTANTS ======================================================= ! private static int MSG_SIZE = 36; // 4 header bytes, 30 msg bytes, 2 crc // bytes; 2 strength bytes are not *************** *** 72,84 **** //========================================================================= //=== PRIVATE VARIABLES ================================================ ! static final int SENSORREADING = 0; static final int CALIBRATION = 1; String strAddr; int nPort; MoteIF dataCollectStub; - MoteIF calibrateStub; List dlist, clist; public static double pressure_mbar(int raw_pressure, int raw_temperature, int[] calibration) { --- 32,51 ---- //========================================================================= //=== PRIVATE VARIABLES ================================================ ! static final int SENSORREADING = 0; static final int CALIBRATION = 1; + static final boolean DEBUG = false; + String strAddr; int nPort; MoteIF dataCollectStub; List dlist, clist; + + private static void prt(String s) { + if(DEBUG) { + System.out.println(s); + } + } public static double pressure_mbar(int raw_pressure, int raw_temperature, int[] calibration) { *************** *** 96,104 **** c5 += calibration[1] >> 6; c6 = calibration[1] & 0x3F; ! int ut1 = 8*c5+20224; int dt = raw_temperature - ut1; double temp = 200 + dt*(c6+50)/(Math.pow(2,10)); ! double off = c2*4 + ((c4-512)*dt)/(Math.pow(2,12)); double sens = c1 + (c3*dt)/(Math.pow(2,10)) + 24576; --- 63,72 ---- c5 += calibration[1] >> 6; c6 = calibration[1] & 0x3F; ! ! prt("Calibration constants in pressure_mbar are "+c1+" "+c2+" "+c3+" "+c4+" "+c5+" "+c6+" "); int ut1 = 8*c5+20224; int dt = raw_temperature - ut1; double temp = 200 + dt*(c6+50)/(Math.pow(2,10)); ! double off = c2*4 + ((c4-512)*dt)/(Math.pow(2,12)); double sens = c1 + (c3*dt)/(Math.pow(2,10)) + 24576; *************** *** 106,114 **** (Math.pow(2,14)) - off; double p = x*100/(Math.pow(2,5)) + 250*100; ! //System.out.println("Press (mbar) : " + p/100); ! //System.out.println("Press (inHg) : " + p/(100*33.864)); return (p/100); } ! public static double pressure_inHg(int raw_pressure, int raw_temperature, int[] calibration) { int c1,c2,c3,c4,c5,c6; --- 74,82 ---- (Math.pow(2,14)) - off; double p = x*100/(Math.pow(2,5)) + 250*100; ! //prt("Press (mbar) : " + p/100); ! //prt("Press (inHg) : " + p/(100*33.864)); return (p/100); } ! public static double pressure_inHg(int raw_pressure, int raw_temperature, int[] calibration) { int c1,c2,c3,c4,c5,c6; *************** *** 125,132 **** c5 += calibration[1] >> 6; c6 = calibration[1] & 0x3F; int ut1 = 8*c5+20224; int dt = raw_temperature - ut1; ! double off = c2*4 + ((c4-512)*dt)/(Math.pow(2,12)); double sens = c1 + (c3*dt)/(Math.pow(2,10)) + 24576; --- 93,102 ---- c5 += calibration[1] >> 6; c6 = calibration[1] & 0x3F; + + prt("Calibration constants in pressure_inHg are "+c1+" "+c2+" "+c3+" "+c4+" "+c5+" "+c6+" "); int ut1 = 8*c5+20224; int dt = raw_temperature - ut1; ! double off = c2*4 + ((c4-512)*dt)/(Math.pow(2,12)); double sens = c1 + (c3*dt)/(Math.pow(2,10)) + 24576; *************** *** 136,140 **** return (p/(100*33.864)); } ! public static double pressure_temp(int raw_temperature, int[] calibration) { int c1,c2,c3,c4,c5,c6; --- 106,110 ---- return (p/(100*33.864)); } ! public static double pressure_temp(int raw_temperature, int[] calibration) { int c1,c2,c3,c4,c5,c6; *************** *** 151,155 **** c5 += calibration[1] >> 6; c6 = calibration[1] & 0x3F; ! int ut1 = 8*c5+20224; int dt = raw_temperature - ut1; --- 121,126 ---- c5 += calibration[1] >> 6; c6 = calibration[1] & 0x3F; ! ! prt("Calibration constants in pressure_temp are "+c1+" "+c2+" "+c3+" "+c4+" "+c5+" "+c6+" "); int ut1 = 8*c5+20224; int dt = raw_temperature - ut1; *************** *** 157,161 **** return (temp/10); } ! public DataCollect() { dlist = new LinkedList(); --- 128,132 ---- return (temp/10); } ! public DataCollect() { dlist = new LinkedList(); *************** *** 172,191 **** } } ! public void packetReceived(byte[] packet) { } ! ! public static void main(String args[]) { ! DataCollect reader = new DataCollect(); ! Thread th = new Thread(reader); ! th.start(); ! } public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { --- 143,167 ---- } } ! public void packetReceived(byte[] packet) { } ! ! public static void main(String args[]) { ! ! prt("In program"); DataCollect reader = new DataCollect(); ! prt("AM Type for Data collection is " + DataCollectionMsg.AM_TYPE); ! ! // Thread th = new Thread(reader); ! // th.start(); ! } + /* public void run() { while (true) { try { + prt("In thread.run"); Thread.sleep(500); } catch (InterruptedException e) { *************** *** 194,243 **** } } public void messageReceived(int to, Message m) { ! System.out.println("Message Received with AM Type " + m.amType()); if(m.amType() == DataCollectionMsg.AM_TYPE) { MultihopMsg msg = new MultihopMsg(m.dataGet()); ! this.packetReceived(msg); } } ! ! public void packetReceived(MultihopMsg m) { int index=0; DataCollectionMsg msg = new DataCollectionMsg(m.dataGet(),m.offset_data(0)); - System.out.println("Type is " + msg.get_type()); if(msg.get_type() == SENSORREADING) { System.out.println("Sensor reading"); if(get_data(msg.get_seq_no(), (long)msg.get_src()) == 0) { ! System.out.println("not duplicate with seq no " + msg.get_seq_no()); /* Not a duplicate */ if((index = get_calibration(msg.get_seq_no(), (long)msg.get_src())) == 0) { /* No calibration data received yet. So, add to the list */ ! System.out.println("Added to list"); dlist.add(msg); } else { dlist.add(msg); ! System.out.println("Sending to print"); printData(index, get_data(msg.get_seq_no(), (long)msg.get_src())); } } } ! else if(msg.get_type() == CALIBRATION) { System.out.println("Calibration"); ! CalibrationMsg cMsg = new CalibrationMsg(m.dataGet(),m.offset_data(0)); if(get_calibration(cMsg.get_seq_no(), (long)cMsg.get_src()) == 0) { /* Not a duplicate */ ! System.out.println("not duplicate with seq no " + msg.get_seq_no()); if((index = get_data(cMsg.get_seq_no(), (long)cMsg.get_src())) == 0) { /* No calibration data received yet. So, add to the list */ ! System.out.println("Added to list"); clist.add(cMsg); } else { clist.add(cMsg); ! System.out.println("Send to print"); printData(get_calibration(cMsg.get_seq_no(), (long)cMsg.get_src()), index); } --- 170,234 ---- } } + */ public void messageReceived(int to, Message m) { ! // prt("Message Received with AM Type " + m.amType()); if(m.amType() == DataCollectionMsg.AM_TYPE) { MultihopMsg msg = new MultihopMsg(m.dataGet()); ! this.pReceived(msg); } } ! ! public void pReceived(MultihopMsg m) { int index=0; + int i; + DataCollectionMsg msg = new DataCollectionMsg(m.dataGet(),m.offset_data(0)); if(msg.get_type() == SENSORREADING) { + prt("1. Type is " + msg.get_type()); System.out.println("Sensor reading"); + if(get_data(msg.get_seq_no(), (long)msg.get_src()) == 0) { ! prt("not duplicate with seq no " + msg.get_seq_no()); /* Not a duplicate */ if((index = get_calibration(msg.get_seq_no(), (long)msg.get_src())) == 0) { /* No calibration data received yet. So, add to the list */ ! prt("Added to list"); dlist.add(msg); } else { dlist.add(msg); ! prt("Sending to print"); printData(index, get_data(msg.get_seq_no(), (long)msg.get_src())); } } } ! ! /* Ideally, should have a root structure with type and two child structures DataCollection and Calibration. ! This way, we can use the root structure to cast to for the outer check condition. This is a quick hack */ ! CalibrationMsg cMsg = new CalibrationMsg(m.dataGet(), m.offset_data(0)); ! if(cMsg.get_type() == CALIBRATION) { ! prt("2. Type is " + msg.get_type()); ! // System.out.println("Data received is " + DataCollect.byteArrayToHexString(m.dataGet())); ! prt("Calibration Data is : "); ! int cal[] = cMsg.get_calibration(); ! for(i=0;i<4;i++) ! prt(" " +cal[i]); ! prt(""); System.out.println("Calibration"); ! // CalibrationMsg cMsg = new CalibrationMsg(m.dataGet(),m.offset_data(0)); if(get_calibration(cMsg.get_seq_no(), (long)cMsg.get_src()) == 0) { /* Not a duplicate */ ! prt("not duplicate with seq no " + msg.get_seq_no()); if((index = get_data(cMsg.get_seq_no(), (long)cMsg.get_src())) == 0) { /* No calibration data received yet. So, add to the list */ ! prt("Added to list"); clist.add(cMsg); } else { clist.add(cMsg); ! prt("Send to print"); printData(get_calibration(cMsg.get_seq_no(), (long)cMsg.get_src()), index); } *************** *** 245,254 **** } } ! void printData(int cindex, int dindex) { ! System.out.println("In print data"); DataCollectionMsg dMsg = (DataCollectionMsg) dlist.get(dindex); CalibrationMsg cMsg = (CalibrationMsg) clist.get(cindex); - System.out.println("--reading--"); System.out.println("Source : "+ dMsg.get_src()); System.out.println("Sequence Number: "+ dMsg.get_seq_no()); --- 236,244 ---- } } ! void printData(int cindex, int dindex) { ! prt("In print data"); DataCollectionMsg dMsg = (DataCollectionMsg) dlist.get(dindex); CalibrationMsg cMsg = (CalibrationMsg) clist.get(cindex); System.out.println("Source : "+ dMsg.get_src()); System.out.println("Sequence Number: "+ dMsg.get_seq_no()); *************** *** 256,266 **** System.out.println("Raw humidity : "+ dMsg.get_humidity()); System.out.println("Raw voltage : "+ dMsg.get_ivolt()); System.out.println("Temperature : "+pressure_temp(dMsg.get_temperature(), cMsg.get_calibration())); System.out.println("Pressure : "+pressure_mbar(dMsg.get_pressure(), dMsg.get_temperature(), cMsg.get_calibration())); ! /* Delete the printed data */ dlist.remove(dindex); clist.remove(cindex); ! System.out.println("\n\nSize of dlist:"+dlist.size() +" and clist:"+clist.size()+" \n\n"); } --- 246,259 ---- System.out.println("Raw humidity : "+ dMsg.get_humidity()); System.out.println("Raw voltage : "+ dMsg.get_ivolt()); + System.out.println("Raw Pressure : "+ dMsg.get_pressure()); + + System.out.println("Raw calibration readings "+DataCollect.byteArrayToHexString(DataCollect.intToByteArray(cMsg.get_calibration()))); System.out.println("Temperature : "+pressure_temp(dMsg.get_temperature(), cMsg.get_calibration())); System.out.println("Pressure : "+pressure_mbar(dMsg.get_pressure(), dMsg.get_temperature(), cMsg.get_calibration())); ! /* Delete the printed data */ dlist.remove(dindex); clist.remove(cindex); ! System.out.println("\n\nSize of dlist:"+dlist.size() +" and clist:"+clist.size()+" \n\n"); } *************** *** 270,277 **** ListIterator it=dlist.listIterator(); int index=0; ! while(it.hasNext()) { DataCollectionMsg msg = (DataCollectionMsg) it.next(); ! if(msg.get_seq_no() == seqno && msg.get_src() == src) { found=true; --- 263,270 ---- ListIterator it=dlist.listIterator(); int index=0; ! while(it.hasNext()) { DataCollectionMsg msg = (DataCollectionMsg) it.next(); ! if(msg.get_seq_no() == seqno && msg.get_src() == src) { found=true; *************** *** 282,286 **** } } ! if(!found) { index=0; --- 275,279 ---- } } ! if(!found) { index=0; *************** *** 289,301 **** return index; } ! private int get_calibration(long seqno, long src) { boolean found=false; ListIterator it=clist.listIterator(); int index=0; ! while(it.hasNext()) { CalibrationMsg msg = (CalibrationMsg) it.next(); ! if(msg.get_seq_no() == seqno && msg.get_src() == src) { found=true; --- 282,294 ---- return index; } ! private int get_calibration(long seqno, long src) { boolean found=false; ListIterator it=clist.listIterator(); int index=0; ! while(it.hasNext()) { CalibrationMsg msg = (CalibrationMsg) it.next(); ! if(msg.get_seq_no() == seqno && msg.get_src() == src) { found=true; *************** *** 306,310 **** } } ! if(!found) { index=0; --- 299,303 ---- } } ! if(!found) { index=0; *************** *** 313,316 **** --- 306,372 ---- return index; } + + /** + * Convert a byte[] array to readable string format. This makes the "hex" + readable! + * @return result String buffer in String format + * @param in byte[] buffer to convert to string format + */ + + static String byteArrayToHexString(byte in[]) { + + byte ch = 0x00; + int i = 0; + + if (in == null || in.length <= 0) + return null; + + String pseudo[] = {"0", "1", "2", + "3", "4", "5", "6", "7", "8", + "9", "A", "B", "C", "D", "E", + "F"}; + + StringBuffer out = new StringBuffer(in.length * 2); + + while (i < in.length) { + ch = (byte) (in[i] & 0xF0); // Strip off high nibble + + ch = (byte) (ch >>> 4); + // shift the bits down + + ch = (byte) (ch & 0x0F); + // must do this is high order bit is on! + + out.append(pseudo[ (int) ch]); // convert the nibble to a String Character + ch = (byte) (in[i] & 0x0F); // Strip off low nibble + out.append(pseudo[ (int) ch]); // convert the nibble to a String Character + i++; + } + + String rslt = new String(out); + + return rslt; + + } + + static private byte[] intToByteArray (final int integer) { + int byteNum = (40 - Integer.numberOfLeadingZeros (integer < 0 ? ~integer : integer)) / 8; + byte[] byteArray = new byte[4]; + + for (int n = 0; n < byteNum; n++) + byteArray[3 - n] = (byte) (integer >>> (n * 8)); + + return (byteArray); + } + + static private byte[] intToByteArray (int[] intArray) { + int i; + byte[] byteArray = new byte[4*intArray.length]; + + for(i=0;i<intArray.length; i++) + System.arraycopy(intToByteArray (intArray[i]), 0, byteArray, i*4, 4); + + return byteArray; + } } |