From 3b3092346016cf3fcfd0f0d36304f9ca0e998d1e Mon Sep 17 00:00:00 2001 From: bepo23 Date: Fri, 29 May 2009 06:24:45 +0000 Subject: [PATCH] Changed every System.out.print[ln] to Log.write, thus logging can be changed depending on the used platform. git-svn-id: svn://svn.code.sf.net/p/wiigee/code/trunk@73 c7eff9ee-dd40-0410-8832-91a4d88773cf --- src/device/Device.java | 9 ++++--- src/device/Wiimote.java | 7 ++--- src/device/WiimoteStreamer.java | 5 ++-- src/logic/Classifier.java | 2 +- src/logic/GestureModel.java | 9 ++++--- src/logic/HMM.java | 17 ++++++------ src/logic/PreciseHMM.java | 19 ++++++------- src/logic/ProcessingUnit.java | 5 ++-- src/logic/Quantizer.java | 29 ++++++++++---------- src/logic/TriggeredProcessingUnit.java | 37 +++++++++++++------------- 10 files changed, 74 insertions(+), 65 deletions(-) diff --git a/src/device/Device.java b/src/device/Device.java index 575d52e..b0a4ec7 100644 --- a/src/device/Device.java +++ b/src/device/Device.java @@ -33,6 +33,7 @@ import filter.DirectionalEquivalenceFilter; import filter.Filter; import filter.IdleStateFilter; import filter.MotionDetectFilter; +import util.Log; public class Device { @@ -52,7 +53,7 @@ public class Device { // Listeners, receive generated events protected Vector devicelistener = new Vector(); - protected ProcessingUnit processingunit = new TriggeredProcessingUnit(); + protected ProcessingUnit processingunit = new ContinuousProcessingUnit();//new TriggeredProcessingUnit(); public Device() { this.addFilter(new IdleStateFilter()); @@ -67,7 +68,7 @@ public class Device { */ public void addFilter(Filter filter) { this.filters.add(filter); - System.out.println("Filter added..."); + Log.write("Filter added..."); } /** @@ -88,7 +89,7 @@ public class Device { */ public void addDeviceListener(DeviceListener listener) { this.devicelistener.add(listener); - System.out.println("WiimoteListener added..."); + Log.write("WiimoteListener added..."); } /** @@ -98,7 +99,7 @@ public class Device { */ public void addGestureListener(GestureListener listener) { this.processingunit.addGestureListener(listener); - System.out.println("GestureListener added..."); + Log.write("GestureListener added..."); } public int getRecognitionButton() { diff --git a/src/device/Wiimote.java b/src/device/Wiimote.java index 9ffb5ff..0fea970 100755 --- a/src/device/Wiimote.java +++ b/src/device/Wiimote.java @@ -29,6 +29,7 @@ import java.util.Random; import javax.bluetooth.L2CAPConnection; import javax.microedition.io.Connector; import event.*; +import util.Log; /** * @author Benjamin 'BePo' Poppinga @@ -70,7 +71,7 @@ public class Wiimote extends Device { // LED encoded as byte byte ledencoding; - + // control connection, send commands to wiimote private L2CAPConnection controlCon; @@ -125,9 +126,9 @@ public class Wiimote extends Device { try { this.controlCon.close(); this.receiveCon.close(); - System.out.println("Disconnected wiimote."); + Log.write("Disconnected wiimote."); } catch(Exception e) { - System.out.println("Failure during disconnect of wiimote."); + Log.write("Failure during disconnect of wiimote."); } } diff --git a/src/device/WiimoteStreamer.java b/src/device/WiimoteStreamer.java index d0e1a69..c382a97 100755 --- a/src/device/WiimoteStreamer.java +++ b/src/device/WiimoteStreamer.java @@ -31,6 +31,7 @@ import javax.bluetooth.L2CAPConnection; import event.ButtonPressedEvent; import event.ButtonReleasedEvent; +import util.Log; /** * This class listens to data sended by the wiimote and generates specific @@ -86,7 +87,7 @@ public class WiimoteStreamer extends Thread { this.x1 = b[11] & 0xFF; this.y1 = b[12] & 0xFF; this.z1 = b[13] & 0xFF; - System.out.println("Autocalibration successful!"); + Log.write("Autocalibration successful!"); continue; } @@ -258,7 +259,7 @@ public class WiimoteStreamer extends Thread { } // while(running) } catch (IOException e) { - System.out.println("Streamer: Connection to Wiimote lost."); + Log.write("Streamer: Connection to Wiimote lost."); this.running = false; } } diff --git a/src/logic/Classifier.java b/src/logic/Classifier.java index 9b32558..3dfc612 100644 --- a/src/logic/Classifier.java +++ b/src/logic/Classifier.java @@ -44,7 +44,7 @@ public class Classifier { * @param g gesture to classify */ public int classifyGesture(Gesture g) { - //System.out.println("Recognizing gesture..."); + //Log.write("Recognizing gesture..."); // Wert im Nenner berechnen, nach Bayes double sum = 0; diff --git a/src/logic/GestureModel.java b/src/logic/GestureModel.java index 5150886..cb9238e 100755 --- a/src/logic/GestureModel.java +++ b/src/logic/GestureModel.java @@ -26,6 +26,7 @@ package logic; import java.util.Vector; import event.AccelerationEvent; +import util.Log; /** * This Class units a Quantizer-Component and an Model-Component. @@ -129,7 +130,7 @@ public class GestureModel { * For debug purposes or very technical interested people. :) */ public void printMap() { - System.out.println("Gesture Quantizer-Map:"); + Log.write("Gesture Quantizer-Map:"); this.quantizer.printMap(); } @@ -138,9 +139,9 @@ public class GestureModel { * @return */ public void print() { - System.out.println("HMM-Print:"); + Log.write("HMM-Print:"); this.markovmodell.print(); - System.out.println("Quanzizer-Print:"); + Log.write("Quanzizer-Print:"); this.quantizer.printMap(); } @@ -182,7 +183,7 @@ public class GestureModel { public void setDefaultProbability(double prob) { this.defaultprobability = prob; - System.out.println("def-prob. set to = "+this.defaultprobability); + Log.write("def-prob. set to = "+this.defaultprobability); } public Quantizer getQuantizer() { diff --git a/src/logic/HMM.java b/src/logic/HMM.java index bd117f7..33ff28f 100755 --- a/src/logic/HMM.java +++ b/src/logic/HMM.java @@ -23,8 +23,9 @@ */ package logic; -import java.text.*; +import java.text.DecimalFormat; import java.util.Vector; +import util.Log; /** * This is a Hidden Markov Model implementation which internally provides @@ -272,20 +273,20 @@ public class HMM { fmt.setMinimumFractionDigits(5); fmt.setMaximumFractionDigits(5); for (int i = 0; i < numStates; i++) - System.out.println("pi(" + i + ") = " + fmt.format(pi[i])); - System.out.println(); + Log.write("pi(" + i + ") = " + fmt.format(pi[i])); + Log.write(""); for (int i = 0; i < numStates; i++) { for (int j = 0; j < numStates; j++) - System.out.print("a(" + i + "," + j + ") = " + Log.write("a(" + i + "," + j + ") = " + fmt.format(a[i][j]) + " "); - System.out.println(); + Log.write(""); } - System.out.println(); + Log.write(""); for (int i = 0; i < numStates; i++) { for (int k = 0; k < numObservations; k++) - System.out.print("b(" + i + "," + k + ") = " + Log.write("b(" + i + "," + k + ") = " + fmt.format(b[i][k]) + " "); - System.out.println(); + Log.write(""); } } diff --git a/src/logic/PreciseHMM.java b/src/logic/PreciseHMM.java index ca912fb..21dfcb2 100755 --- a/src/logic/PreciseHMM.java +++ b/src/logic/PreciseHMM.java @@ -26,6 +26,7 @@ package logic; import java.text.*; import java.util.Vector; import java.lang.Math; +import util.Log; /** * This is a Hidden Markov Model implementation which internally provides @@ -345,10 +346,10 @@ public class PreciseHMM { } } - //System.out.println("log p = "+lp); + //Log.write("log p = "+lp); //return lp; // we now have log10(p) calculated, transform to p. - System.out.println("prob = "+Math.exp(lp)); + Log.write("prob = "+Math.exp(lp)); return Math.exp(lp); //return Math.pow(10, lp); } @@ -413,20 +414,20 @@ public class PreciseHMM { fmt.setMinimumFractionDigits(10); fmt.setMaximumFractionDigits(10); for (int i = 0; i < numStates; i++) - System.out.println("pi(" + i + ") = " + fmt.format(pi[i])); - System.out.println(); + Log.write("pi(" + i + ") = " + fmt.format(pi[i])); + Log.write(""); for (int i = 0; i < numStates; i++) { for (int j = 0; j < numStates; j++) - System.out.print("a(" + i + "," + j + ") = " + Log.write("a(" + i + "," + j + ") = " + fmt.format(a[i][j]) + " "); - System.out.println(); + Log.write(""); } - System.out.println(); + Log.write(""); for (int i = 0; i < numStates; i++) { for (int k = 0; k < sigmaSize; k++) - System.out.print("b(" + i + "," + k + ") = " + Log.write("b(" + i + "," + k + ") = " + fmt.format(b[i][k]) + " "); - System.out.println(); + Log.write(""); } } diff --git a/src/logic/ProcessingUnit.java b/src/logic/ProcessingUnit.java index 210c3b0..23652bc 100644 --- a/src/logic/ProcessingUnit.java +++ b/src/logic/ProcessingUnit.java @@ -12,6 +12,7 @@ import event.InfraredEvent; import event.MotionStartEvent; import event.MotionStopEvent; import event.StateEvent; +import util.Log; public abstract class ProcessingUnit implements DeviceListener { @@ -69,9 +70,9 @@ public abstract class ProcessingUnit implements DeviceListener { public void reset() { if(this.classifier.getCountOfGestures()>0) { this.classifier.clear(); - System.out.println("### Model reset ###"); + Log.write("### Model reset ###"); } else { - System.out.println("There doesn't exist any data to reset."); + Log.write("There doesn't exist any data to reset."); } } diff --git a/src/logic/Quantizer.java b/src/logic/Quantizer.java index f7a6094..1e20432 100755 --- a/src/logic/Quantizer.java +++ b/src/logic/Quantizer.java @@ -26,6 +26,7 @@ package logic; import java.util.Vector; import event.AccelerationEvent; +import util.Log; /** * This class implements a quantization component. In this case a @@ -77,7 +78,7 @@ public class Quantizer { double pi = Math.PI; this.radius = (gesture.getMaxAcceleration() + gesture .getMinAcceleration()) / 2; - System.out.println("Using radius: " + this.radius); + Log.write("Using radius: " + this.radius); // x , z , y if (!this.maptrained) { @@ -135,11 +136,11 @@ public class Quantizer { if (nenner > 1) { // nur wenn der nenner>0 oder >1??? ist muss // was // geaendert werden - // System.out.println("Setze neuen Centeroid!"); + // Log.write("Setze neuen Centeroid!"); this.map[i] = new double[] {(zaehlerX / (double) nenner), (zaehlerY / (double) nenner), (zaehlerZ / (double) nenner) }; - // System.out.println("Centeroid: "+i+": "+newcenteroid[0]+":"+newcenteroid[1]); + // Log.write("Centeroid: "+i+": "+newcenteroid[0]+":"+newcenteroid[1]); } } // new centeroids @@ -148,7 +149,7 @@ public class Quantizer { // Debug: Printout groups /* * for (int i = 0; i < n; i++) { for (int j = 0; j < this.data.size(); - * j++) { System.out.print(g[i][j] + "|"); } System.out.println(""); } + * j++) { Log.write(g[i][j] + "|"); } Log.write(""); } */ } @@ -181,9 +182,9 @@ public class Quantizer { vector[2] = ref[2] - curr[2]; d[i][j] = Math.sqrt((vector[0] * vector[0]) + (vector[1] * vector[1]) + (vector[2] * vector[2])); - // System.out.print(d[i][j] + "|"); + // Log.write(d[i][j] + "|"); } - // System.out.println(""); + // Log.write(""); } // look, to which group a value belongs @@ -203,8 +204,8 @@ public class Quantizer { // Debug output /* * for (int i = 0; i < groups.length; i++) { // zeilen for (int j = 0; j - * < groups[i].length; j++) { System.out.print(groups[i][j] + "|"); } - * System.out.println(""); } + * < groups[i].length; j++) { Log.write(groups[i][j] + "|"); } + * Log.write(""); } */ return groups; @@ -222,12 +223,12 @@ public class Quantizer { int[][] groups = this.deriveGroups(gesture); Vector sequence = new Vector(); - // System.out.print("Visible symbol sequence: "); + // Log.write("Visible symbol sequence: "); for (int j = 0; j < groups[0].length; j++) { // spalten for (int i = 0; i < groups.length; i++) { // zeilen if (groups[i][j] == 1) { - // System.out.print(" "+ i); + // Log.write(" "+ i); sequence.add(i); break; } @@ -241,10 +242,10 @@ public class Quantizer { // better resolution than copying the old value a few time. while (sequence.size() < this.numStates) { sequence.add(sequence.elementAt(sequence.size() - 1)); - // System.out.print(" "+sequence.elementAt(sequence.size()-1)); + // Log.write(" "+sequence.elementAt(sequence.size()-1)); } - // System.out.println(""); + // Log.write(""); int[] out = new int[sequence.size()]; for (int i = 0; i < sequence.size(); i++) { @@ -259,9 +260,9 @@ public class Quantizer { * interests. */ public void printMap() { - System.out.println("Centeroids:"); + Log.write("Centeroids:"); for (int i = 0; i < this.map.length; i++) { - System.out.println(i + ". :" + this.map[i][0] + ":" + Log.write(i + ". :" + this.map[i][0] + ":" + this.map[i][1] + ":" + this.map[i][2]); } } diff --git a/src/logic/TriggeredProcessingUnit.java b/src/logic/TriggeredProcessingUnit.java index 89c53bf..dc8b9ac 100644 --- a/src/logic/TriggeredProcessingUnit.java +++ b/src/logic/TriggeredProcessingUnit.java @@ -26,6 +26,7 @@ package logic; import java.util.Vector; import event.*; +import util.Log; /** * This class analyzes the WiimoteAccelerationEvents emitted from a Wiimote @@ -99,7 +100,7 @@ public class TriggeredProcessingUnit extends ProcessingUnit { // TrainButton = record a gesture for learning if((!this.analyzing && !this.learning) && event.isTrainInitEvent()) { - System.out.println("Training started!"); + Log.write("Training started!"); this.learning=true; this.fireStateEvent(1); } @@ -107,7 +108,7 @@ public class TriggeredProcessingUnit extends ProcessingUnit { // RecognitionButton = record a gesture for recognition if((!this.analyzing && !this.learning) && event.isRecognitionInitEvent()) { - System.out.println("Recognition started!"); + Log.write("Recognition started!"); this.analyzing=true; this.fireStateEvent(2); } @@ -118,7 +119,7 @@ public class TriggeredProcessingUnit extends ProcessingUnit { event.isCloseGestureInitEvent()) { if(this.trainsequence.size()>0) { - System.out.println("Training the model with "+this.trainsequence.size()+" gestures..."); + Log.write("Training the model with "+this.trainsequence.size()+" gestures..."); this.fireStateEvent(1); this.learning=true; @@ -130,7 +131,7 @@ public class TriggeredProcessingUnit extends ProcessingUnit { this.trainsequence=new Vector(); this.learning=false; } else { - System.out.println("There is nothing to do. Please record some gestures first."); + Log.write("There is nothing to do. Please record some gestures first."); } } } @@ -138,44 +139,44 @@ public class TriggeredProcessingUnit extends ProcessingUnit { public void handleStopEvent(ActionStopEvent event) { if(this.learning) { // button release and state=learning, stops learning if(this.current.getCountOfData()>0) { - System.out.println("Finished recording (training)..."); - System.out.println("Data: "+this.current.getCountOfData()); + Log.write("Finished recording (training)..."); + Log.write("Data: "+this.current.getCountOfData()); Gesture gesture = new Gesture(this.current); this.trainsequence.add(gesture); this.current=new Gesture(); this.learning=false; } else { - System.out.println("There is no data."); - System.out.println("Please train the gesture again."); + Log.write("There is no data."); + Log.write("Please train the gesture again."); this.learning=false; // ? } } else if(this.analyzing) { // button release and state=analyzing, stops analyzing if(this.current.getCountOfData()>0) { - System.out.println("Finished recording (recognition)..."); - System.out.println("Compare gesture with "+this.classifier.getCountOfGestures()+" other gestures."); + Log.write("Finished recording (recognition)..."); + Log.write("Compare gesture with "+this.classifier.getCountOfGestures()+" other gestures."); Gesture gesture = new Gesture(this.current); int recognized = this.classifier.classifyGesture(gesture); if(recognized!=-1) { double recogprob = this.classifier.getLastProbability(); this.fireGestureEvent(recognized, recogprob); - System.out.println("######"); - System.out.println("Gesture No. "+recognized+" recognized: "+recogprob); - System.out.println("######"); + Log.write("######"); + Log.write("Gesture No. "+recognized+" recognized: "+recogprob); + Log.write("######"); } else { this.fireStateEvent(0); - System.out.println("######"); - System.out.println("No gesture recognized."); - System.out.println("######"); + Log.write("######"); + Log.write("No gesture recognized."); + Log.write("######"); } this.current=new Gesture(); this.analyzing=false; } else { - System.out.println("There is no data."); - System.out.println("Please recognize the gesture again."); + Log.write("There is no data."); + Log.write("Please recognize the gesture again."); this.analyzing=false; // ? } }