From 52bd77482e98618483034a513931c50a870b9a47 Mon Sep 17 00:00:00 2001 From: yeom Date: Wed, 31 Oct 2012 06:26:12 +0000 Subject: [PATCH] changes + add two more benchmarks without annotations --- Robust/src/Analysis/SSJava/BuildLattice.java | 3 +- Robust/src/Analysis/SSJava/FlowDownCheck.java | 10 +- .../Analysis/SSJava/LocationInference.java | 47 ++- .../SSJava/EyeTrackingInfer/Classifier.java | 175 ++++++++ .../EyeTrackingInfer/ClassifierTree.java | 193 +++++++++ .../SSJava/EyeTrackingInfer/Counter.java | 15 + .../EyeTrackingInfer/DeviationScanner.java | 171 ++++++++ .../SSJava/EyeTrackingInfer/Dimension.java | 5 + .../EyeTrackingInfer/DummyCaptureDevice.java | 52 +++ .../SSJava/EyeTrackingInfer/EyeDetector.java | 90 +++++ .../SSJava/EyeTrackingInfer/EyePosition.java | 94 +++++ .../EyeTrackingInfer/FaceAndEyePosition.java | 47 +++ .../EyeTrackingInfer/ICaptureDevice.java | 52 +++ .../SSJava/EyeTrackingInfer/Image.java | 50 +++ .../SSJava/EyeTrackingInfer/ImageReader.java | 193 +++++++++ .../EyeTrackingInfer/IntegralImageData.java | 66 +++ .../SSJava/EyeTrackingInfer/LATTICE.java | 3 + .../SSJava/EyeTrackingInfer/LEA.java | 124 ++++++ .../EyeTrackingInfer/LEAImplementation.java | 125 ++++++ .../SSJava/EyeTrackingInfer/LOC.java | 7 + .../SSJava/EyeTrackingInfer/Point.java | 20 + .../SSJava/EyeTrackingInfer/Rectangle2D.java | 36 ++ .../SSJava/EyeTrackingInfer/ScanArea.java | 119 ++++++ .../SSJava/EyeTrackingInfer/makefile | 52 +++ .../SSJava/JavaNatorInfer/MotorControl.java | 318 +++++++++++++++ .../SSJava/JavaNatorInfer/PWMControl.java | 192 +++++++++ .../SSJava/JavaNatorInfer/PWMManager.java | 214 ++++++++++ .../SSJava/JavaNatorInfer/PWMNative.java | 377 ++++++++++++++++++ .../SSJava/JavaNatorInfer/PWMRtsj.java | 322 +++++++++++++++ .../SSJava/JavaNatorInfer/RCBridge.java | 41 ++ .../SSJava/JavaNatorInfer/RobotMain.java | 310 ++++++++++++++ .../SSJava/JavaNatorInfer/StrategyMgr.java | 270 +++++++++++++ .../JavaNatorInfer/TestSensorInput.java | 26 ++ .../Benchmarks/SSJava/JavaNatorInfer/makefile | 52 +++ 34 files changed, 3861 insertions(+), 10 deletions(-) create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Classifier.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ClassifierTree.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Counter.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/DeviationScanner.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Dimension.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/DummyCaptureDevice.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/EyeDetector.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/EyePosition.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/FaceAndEyePosition.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ICaptureDevice.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Image.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ImageReader.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/IntegralImageData.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LATTICE.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LEA.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LEAImplementation.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LOC.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Point.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Rectangle2D.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ScanArea.java create mode 100644 Robust/src/Benchmarks/SSJava/EyeTrackingInfer/makefile create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/MotorControl.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMControl.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMManager.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMNative.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMRtsj.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/RCBridge.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/RobotMain.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/StrategyMgr.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/TestSensorInput.java create mode 100644 Robust/src/Benchmarks/SSJava/JavaNatorInfer/makefile diff --git a/Robust/src/Analysis/SSJava/BuildLattice.java b/Robust/src/Analysis/SSJava/BuildLattice.java index d1c631fd..8c66b13a 100644 --- a/Robust/src/Analysis/SSJava/BuildLattice.java +++ b/Robust/src/Analysis/SSJava/BuildLattice.java @@ -66,8 +66,6 @@ public class BuildLattice { HierarchyGraph inputGraph, LocationSummary locSummary, Map, Set>> mapImSucc) { - HierarchyGraph simpleHierarchyGraph = infer.getSimpleHierarchyGraph(desc); - SSJavaLattice lattice = new SSJavaLattice(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM); @@ -140,6 +138,7 @@ public class BuildLattice { } + inputGraph.removeRedundantEdges(); return lattice; } diff --git a/Robust/src/Analysis/SSJava/FlowDownCheck.java b/Robust/src/Analysis/SSJava/FlowDownCheck.java index 6d22825d..25f6a8e3 100644 --- a/Robust/src/Analysis/SSJava/FlowDownCheck.java +++ b/Robust/src/Analysis/SSJava/FlowDownCheck.java @@ -2172,6 +2172,7 @@ public class FlowDownCheck { // composite location int maxTupleSize = 0; + int minTupleSize = 0; CompositeLocation maxCompLoc = null; Location prevPriorityLoc = null; @@ -2181,6 +2182,9 @@ public class FlowDownCheck { maxTupleSize = compLoc.getSize(); maxCompLoc = compLoc; } + if (minTupleSize == 0 || compLoc.getSize() < minTupleSize) { + minTupleSize = compLoc.getSize(); + } Location priorityLoc = compLoc.get(0); String priorityLocId = priorityLoc.getLocIdentifier(); priorityLocIdentifierSet.add(priorityLocId); @@ -2207,10 +2211,7 @@ public class FlowDownCheck { } SSJavaLattice locOrder = getLatticeByDescriptor(priorityDescriptor); - System.out.println("priorityDescriptor=" + priorityDescriptor); - System.out.println("GLB INPUT=" + priorityLocIdentifierSet); String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet); - System.out.println("GLB OUTPUT="+glbOfPriorityLoc); glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc)); Set compSet = locId2CompLocSet.get(glbOfPriorityLoc); @@ -2221,7 +2222,8 @@ public class FlowDownCheck { // in this case, do not take care about delta // CompositeLocation inputComp = inputSet.iterator().next(); - for (int i = 1; i < maxTupleSize; i++) { + // for (int i = 1; i < maxTupleSize; i++) { + for (int i = 1; i < minTupleSize; i++) { glbCompLoc.addLocation(Location.createTopLocation(maxCompLoc.get(i).getDescriptor())); } } else { diff --git a/Robust/src/Analysis/SSJava/LocationInference.java b/Robust/src/Analysis/SSJava/LocationInference.java index c3440672..6575d4ba 100644 --- a/Robust/src/Analysis/SSJava/LocationInference.java +++ b/Robust/src/Analysis/SSJava/LocationInference.java @@ -560,7 +560,6 @@ public class LocationInference { // // update return flow nodes in the caller CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc(); - System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc); if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis()) && returnLoc.getSize() > 1) { @@ -573,6 +572,20 @@ public class LocationInference { } System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple); callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple); + } else { + // if the return loc set was empty and later pcloc was connected to the return loc + // need to make sure that return loc reflects to this changes. + FlowReturnNode flowReturnNode = callerFlowGraph.getFlowReturnNode(min); + if (flowReturnNode != null && flowReturnNode.getReturnTupleSet().isEmpty()) { + + if (needToUpdateReturnLocHolder(min.getMethod(), flowReturnNode)) { + NTuple baseTuple = mapMethodInvokeNodeToBaseTuple.get(min); + NTuple newReturnTuple = baseTuple.clone(); + flowReturnNode.addTuple(newReturnTuple); + } + + } + } } @@ -584,6 +597,23 @@ public class LocationInference { } + private boolean needToUpdateReturnLocHolder(MethodDescriptor mdCallee, + FlowReturnNode flowReturnNode) { + FlowGraph fg = getFlowGraph(mdCallee); + MethodSummary summary = getMethodSummary(mdCallee); + CompositeLocation returnCompLoc = summary.getRETURNLoc(); + NTuple returnDescTuple = translateToDescTuple(returnCompLoc.getTuple()); + Set incomingNodeToReturnNode = + fg.getIncomingFlowNodeSet(fg.getFlowNode(returnDescTuple)); + for (Iterator iterator = incomingNodeToReturnNode.iterator(); iterator.hasNext();) { + FlowNode inNode = (FlowNode) iterator.next(); + if (inNode.getDescTuple().get(0).equals(mdCallee.getThis())) { + return true; + } + } + return false; + } + private void addMapMethodDescToMethodInvokeNodeSet(MethodInvokeNode min) { MethodDescriptor md = min.getMethod(); if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) { @@ -3149,9 +3179,11 @@ public class LocationInference { // calculate a return location: // the return location type is lower than all parameters and the location of return values MethodSummary methodSummary = getMethodSummary(md); - if (methodSummary.getRETURNLoc() != null) { - return; - } + // if (methodSummary.getRETURNLoc() != null) { + // System.out.println("$HERE?"); + // return; + // } + FlowGraph fg = getFlowGraph(md); Map mapParamToLoc = methodSummary.getMapParamIdxToInferLoc(); Set paramIdxSet = mapParamToLoc.keySet(); @@ -3199,6 +3231,13 @@ public class LocationInference { } + // makes sure that PCLOC is higher than RETURNLOC + CompositeLocation pcLoc = methodSummary.getPCLoc(); + if (!pcLoc.get(0).isTop()) { + NTuple pcLocDescTuple = translateToDescTuple(pcLoc.getTuple()); + fg.addValueFlowEdge(pcLocDescTuple, returnDescTuple); + } + } } diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Classifier.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Classifier.java new file mode 100644 index 00000000..f73ef95b --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Classifier.java @@ -0,0 +1,175 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * + * @author Florian + */ + + +public class Classifier { + + + private ScanArea[] scanAreas; + + + private float[] possibilities_FaceYes; + + private float[] possibilities_FaceNo; + + private int possibilityFaceYes = 0; + + private int possibilityFaceNo = 0; + + public Classifier(int numScanAreas) { + this.scanAreas = new ScanArea[numScanAreas]; + this.possibilities_FaceYes = new float[numScanAreas]; + this.possibilities_FaceNo = new float[numScanAreas]; + } + + public void setScanArea(int idx, ScanArea area) { + scanAreas[idx] = area; + } + + public void setPossibilitiesFaceYes(@DELEGATE float[] arr) { + this.possibilities_FaceYes = arr; + } + + public void setPossibilityFaceYes(int v) { + this.possibilityFaceYes = v; + } + + public void setPossibilitiesFaceNo(@DELEGATE float[] arr) { + this.possibilities_FaceNo = arr; + } + + public void setPossibilityFaceNo(int v) { + this.possibilityFaceNo = v; + } + + /** + * Classifies an images region as face + * + * @param image + * @param scaleFactor + * please be aware of the fact that the scanareas are scaled for use + * with 100x100 px images + * @param translationX + * @param translationY + * @return true if this region was classified as face, else false + */ + + + public boolean classifyFace( IntegralImageData image, + float scaleFactor, int translationX, + int translationY, float borderline) { + + long values[] = new long[scanAreas.length]; + + float avg = 0f; + int avgItems = 0; + for ( int i = 0; i < scanAreas.length; ++i) { + values[i] = 0l; + + values[i] += + image.getIntegralAt(translationX + scanAreas[i].getToX(scaleFactor), translationY + + scanAreas[i].getToY(scaleFactor)); + values[i] += + image.getIntegralAt(translationX + scanAreas[i].getFromX(scaleFactor), translationY + + scanAreas[i].getFromY(scaleFactor)); + + values[i] -= + image.getIntegralAt(translationX + scanAreas[i].getToX(scaleFactor), translationY + + scanAreas[i].getFromY(scaleFactor)); + values[i] -= + image.getIntegralAt(translationX + scanAreas[i].getFromX(scaleFactor), translationY + + scanAreas[i].getToY(scaleFactor)); + + values[i] = (long) (values[i] / ((float) scanAreas[i].getSize(scaleFactor))); + avg = ((avgItems * avg) + values[i]) / (++avgItems); + } + // System.out.println("avg=" + avg); + + // int amountYesNo = this.possibilityFaceNo + this.possibilityFaceYes; + + // calculate the possibilites for face=yes and face=no with naive bayes + // P(Yes | M1 and ... and Mn) = P(Yes) * P(M1 | Yes) * ... * P(Mn | Yes) /xx + // P(No | M1 and ... and Mn) = P(No) * P(M1 | No) * ... * P(Mn | No) / xx + // as we just maximize the args we don't actually calculate the accurate + // possibility + + float isFaceYes = 1.0f;// this.possibilityFaceYes / + // (float)amountYesNo; + float isFaceNo = 1.0f;// this.possibilityFaceNo / + // (float)amountYesNo; + + for ( int i = 0; i < this.scanAreas.length; ++i) { + boolean bright = (values[i] >= avg); + isFaceYes *= (bright ? this.possibilities_FaceYes[i] : 1 - this.possibilities_FaceYes[i]); + isFaceNo *= (bright ? this.possibilities_FaceNo[i] : 1 - this.possibilities_FaceNo[i]); + } + // System.out.println("avg=" + avg + " yes=" + isFaceYes + " no=" + + // isFaceNo); + + return (isFaceYes >= isFaceNo && (isFaceYes / (isFaceYes + isFaceNo)) > borderline); + } + + public ScanArea[] getScanAreas() { + return this.scanAreas; + } + + public int getLearnedFacesYes() { + return this.possibilityFaceYes; + } + + public int getLearnedFacesNo() { + return this.possibilityFaceNo; + } + + public float getPossibility(int scanAreaID, boolean faceYes, boolean bright) { + if (faceYes) { + return (bright ? this.possibilities_FaceYes[scanAreaID] + : 1 - this.possibilities_FaceYes[scanAreaID]); + } else { + return (bright ? this.possibilities_FaceNo[scanAreaID] + : 1 - this.possibilities_FaceNo[scanAreaID]); + } + } + + public int compareTo(Classifier o) { + if (o.getScanAreas().length > this.getScanAreas().length) { + return -1; + } else if (o.getScanAreas().length < this.getScanAreas().length) { + return 1; + } else + return 0; + } + + public String toString() { + + String str = ""; + for ( int i = 0; i < scanAreas.length; i++) { + str += scanAreas[i].toString() + "\n"; + } + + return str; + + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ClassifierTree.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ClassifierTree.java new file mode 100644 index 00000000..14549029 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ClassifierTree.java @@ -0,0 +1,193 @@ +import SSJava.PCLOC; + +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * + * @author Florian + */ + + +public class ClassifierTree { + + + private Classifier classifiers[]; + + public ClassifierTree(int size) { + classifiers = new Classifier[size]; + } + + public void addClassifier( int idx, Classifier c) { + classifiers[idx] = c; + } + + /** + * Locates a face by searching radial starting at the last known position. If + * lastCoordinates are null we simply start in the center of the image. + *

+ * TODO: This method could quite possible be tweaked so that face recognition + * would be much faster + * + * @param image + * the image to process + * @param lastCoordinates + * the last known coordinates or null if unknown + * @return an rectangle representing the actual face position on success or + * null if no face could be detected + */ + + + public Rectangle2D locateFaceRadial( Image smallImage, + Rectangle2D lastCoordinates) { + + IntegralImageData imageData = new IntegralImageData(smallImage); + float originalImageFactor = 1; + + if (lastCoordinates == null) { + // if we don't have a last coordinate we just begin in the center + int smallImageMaxDimension = + Math.min(smallImage.getWidth(), smallImage.getHeight()); + lastCoordinates = + new Rectangle2D((smallImage.getWidth() - smallImageMaxDimension) / 2.0, + (smallImage.getHeight() - smallImageMaxDimension) / 2.0, smallImageMaxDimension, + smallImageMaxDimension); + // System.out.println("lastCoordinates=" + lastCoordinates); + } else { + // first we have to scale the last coodinates back relative to the resized + // image + lastCoordinates = + new Rectangle2D((lastCoordinates.getX() * (1 / originalImageFactor)), + (lastCoordinates.getY() * (1 / originalImageFactor)), + (lastCoordinates.getWidth() * (1 / originalImageFactor)), + (lastCoordinates.getHeight() * (1 / originalImageFactor))); + } + + float startFactor = (float) (lastCoordinates.getWidth() / 100.0f); + + // first we calculate the maximum scale factor for our 200x200 image + float maxScaleFactor = + Math.min(imageData.getWidth() / 100f, imageData.getHeight() / 100f); + // maxScaleFactor = 1.0f; + + // we simply won't recognize faces that are smaller than 40x40 px + float minScaleFactor = 0.5f; + + float maxScaleDifference = + Math.max(Math.abs(maxScaleFactor - startFactor), Math.abs(minScaleFactor - startFactor)); + + // border for faceYes-possibility must be greater that that + float maxBorder = 0.999f; + + int startPosX = (int) lastCoordinates.getX(); + int startPosY = (int) lastCoordinates.getX(); + + int loopidx = 0; + TERMINATE: for ( float factorDiff = 0.0f; Math.abs(factorDiff) <= maxScaleDifference; factorDiff = + (factorDiff + sgn(factorDiff) * 0.1f) * -1 // we alternate between + // negative and positiv + // factors + ) { + + if (++loopidx > 1000) { + return null; + } + + float factor = startFactor + factorDiff; + if (factor > maxScaleFactor || factor < minScaleFactor) + continue; + + // now we calculate the actualDimmension + int actualDimmension = (int) (100 * factor); + int maxX = imageData.getWidth() - actualDimmension; + int maxY = imageData.getHeight() - actualDimmension; + + int maxDiffX = Math.max(Math.abs(startPosX - maxX), startPosX); + int maxDiffY = Math.max(Math.abs(startPosY - maxY), startPosY); + + int xidx = 0; + TERMINATE: for ( float xDiff = 0.1f; Math.abs(xDiff) <= maxDiffX; xDiff = + (xDiff + sgn(xDiff) * 0.5f) * -1) { + + if (++xidx > 1000) { + return null; + } + + int xPos = Math.round((float) (startPosX + xDiff)); + + if (xPos < 0 || xPos > maxX) + continue; + + int yidx = 0; + // yLines: + TERMINATE: for ( float yDiff = 0.1f; Math.abs(yDiff) <= maxDiffY; yDiff = + (yDiff + sgn(yDiff) * 0.5f) * -1) { + + if (++yidx > 1000) { + return null; + } + + int yPos = Math.round(startPosY + yDiff); + if (yPos < 0 || yPos > maxY) + continue; + + // by now we should have a valid coordinate to process which we should + // do now + boolean backToYLines = false; + for ( int idx = 0; idx < classifiers.length; ++idx) { + float borderline = + 0.8f + (idx / (classifiers.length - 1)) * (maxBorder - 0.8f); + if (!classifiers[idx].classifyFace(imageData, factor, xPos, yPos, borderline)) { + backToYLines = true; + break; + // continue yLines; + } + } + + // if we reach here we have a face recognized because our image went + // through all + // classifiers + + if (backToYLines) { + continue; + } + Rectangle2D faceRect = + new Rectangle2D(xPos * originalImageFactor, yPos * originalImageFactor, + actualDimmension * originalImageFactor, actualDimmension * originalImageFactor); + + return faceRect; + + } + + } + + } + + // System.out.println("Time: "+(System.currentTimeMillis()-timeStart)+"ms"); + return null; + + } + + + + private static int sgn( float value) { + return (value < 0 ? -1 : (value > 0 ? +1 : 1)); + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Counter.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Counter.java new file mode 100644 index 00000000..a5a4ac3e --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Counter.java @@ -0,0 +1,15 @@ +public class Counter { + + static int idx = 0; + + @TRUST + static boolean inc() { + idx++; + } + + @TRUST + static int idx() { + return idx; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/DeviationScanner.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/DeviationScanner.java new file mode 100644 index 00000000..4cc353e0 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/DeviationScanner.java @@ -0,0 +1,171 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * No description given. + * + * @author Florian Frankenberger + */ + + +public class DeviationScanner { + + + private EyePosition eyePositions[]; + + // LEFT_UP(+1, -1), UP(0, -1), RIGHT_UP(-1, -1), LEFT(+1, 0), NONE(0, 0), + // RIGHT(-1, 0), LEFT_DOWN( + // +1, +1), DOWN(0, +1), RIGHT_DOWN(-1, +1); + + public static final int LEFT_UP = 0; + public static final int UP = 1; + public static final int RIGHT_UP = 2; + public static final int LEFT = 3; + public static final int NONE = 4; + public static final int RIGHT = 5; + public static final int LEFT_DOWN = 6; + public static final int DOWN = 7; + public static final int RIGHT_DOWN = 8; + + public DeviationScanner() { + eyePositions = new EyePosition[3]; + } + + + public void addEyePosition( EyePosition eyePosition) { + + // for ( int i = 1; i < 3; i++) { + // eyePositions[i - 1] = eyePositions[i]; + // eyePositions[i] = null; + // } + // eyePositions[eyePositions.length - 1] = eyePosition; + + SSJAVA.append(eyePositions, eyePosition); + + } + + // + + + public int scanForDeviation( Rectangle2D faceRect) { + + int deviation = NONE; + + for ( int i = 0; i < 3; i++) { + if (eyePositions[i] == null) { + return deviation; + } + } + + double deviationX = 0; + double deviationY = 0; + + int lastIdx = -1; + for ( int i = 0; i < 3; ++i) { + if (lastIdx != -1) { + deviationX += (eyePositions[i].getX() - eyePositions[lastIdx].getX()); + deviationY += (eyePositions[i].getY() - eyePositions[lastIdx].getY()); + } + lastIdx = i; + } + + final double deviationPercentX = 0.04; + final double deviationPercentY = 0.04; + + deviationX /= faceRect.getWidth(); + deviationY /= faceRect.getWidth(); + + int deviationAbsoluteX = 0; + int deviationAbsoluteY = 0; + if (deviationX > deviationPercentX) + deviationAbsoluteX = 1; + if (deviationX < -deviationPercentX) + deviationAbsoluteX = -1; + if (deviationY > deviationPercentY) + deviationAbsoluteY = 1; + if (deviationY < -deviationPercentY) + deviationAbsoluteY = -1; + + deviation = getDirectionFor(deviationAbsoluteX, deviationAbsoluteY); + + if (deviation != NONE) { + eyePositions = new EyePosition[3]; + } + // System.out.println(String.format("%.2f%% | %.2f%% => %d and %d >>> %s", + // deviationX*100, deviationY*100, deviationAbsoluteX, deviationAbsoluteY, + // deviation.toString())); + + return deviation; + } + + + public int getDirectionFor( int directionX, int directionY) { + + if (directionX == +1 && directionY == -1) { + return LEFT_UP; + } else if (directionX == 0 && directionY == -1) { + return UP; + } else if (directionX == -1 && directionY == -1) { + return RIGHT_UP; + } else if (directionX == +1 && directionY == 0) { + return LEFT; + } else if (directionX == 0 && directionY == 0) { + return NONE; + } else if (directionX == -1 && directionY == 0) { + return RIGHT; + } else if (directionX == +1 && directionY == +1) { + return LEFT_DOWN; + } else if (directionX == 0 && directionY == +1) { + return DOWN; + } else if (directionX == -1 && directionY == +1) { + return RIGHT_DOWN; + } + + return -1; + } + + public void clear() { + System.out.println("CLEAR"); + eyePositions = new EyePosition[3]; + } + + public String toStringDeviation( int dev) { + if (dev == LEFT_UP) { + return "LEFT_UP"; + } else if (dev == UP) { + return "UP"; + } else if (dev == RIGHT_UP) { + return "RIGHT_UP"; + } else if (dev == LEFT) { + return "LEFT"; + } else if (dev == NONE) { + return "NONE"; + } else if (dev == RIGHT) { + return "RIGHT"; + } else if (dev == LEFT_DOWN) { + return "LEFT_DOWN"; + } else if (dev == DOWN) { + return "DOWN"; + } else if (dev == RIGHT_DOWN) { + return "RIGHT_DOWN"; + } + return "ERROR"; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Dimension.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Dimension.java new file mode 100644 index 00000000..c6013eb7 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Dimension.java @@ -0,0 +1,5 @@ +public class Dimension { + public int width; + public int height; + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/DummyCaptureDevice.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/DummyCaptureDevice.java new file mode 100644 index 00000000..699a620d --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/DummyCaptureDevice.java @@ -0,0 +1,52 @@ +/** + * + */ + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import de.darkblue.lea.ifaces.ICaptureDevice; + +/** + * No description given. + * + * @author Florian Frankenberger + */ +public class DummyCaptureDevice implements ICaptureDevice { + + /** + * + */ + public DummyCaptureDevice() { + // TODO Auto-generated constructor stub + } + + /* (non-Javadoc) + * @see de.darkblue.lea.ifaces.ICaptureDevice#close() + */ + @Override + public void close() { + } + + /* (non-Javadoc) + * @see de.darkblue.lea.ifaces.ICaptureDevice#getFrameRate() + */ + @Override + public int getFrameRate() { + return 15; + } + + /* (non-Javadoc) + * @see de.darkblue.lea.ifaces.ICaptureDevice#getImage() + */ + @Override + public BufferedImage getImage() { + BufferedImage image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB); + Graphics2D g2d = (Graphics2D)image.getGraphics(); + g2d.setColor(new Color(255, 255, 255)); + g2d.fillRect(0, 0, 639, 479); + return image; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/EyeDetector.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/EyeDetector.java new file mode 100644 index 00000000..11443ab5 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/EyeDetector.java @@ -0,0 +1,90 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * No description given. + * + * @author Florian Frankenberger + */ + + +class EyeDetector { + + + private int width; + + private int height; + + private int[] pixelBuffer; + + double percent; + + public EyeDetector(Image image, Rectangle2D faceRect) { + + percent = 0.15 * faceRect.getWidth(); + Rectangle2D adjustedFaceRect = + new Rectangle2D(faceRect.getX() + percent, faceRect.getY() + percent, faceRect.getWidth() + - percent, faceRect.getHeight() - 2 * percent); + + width = (int) adjustedFaceRect.getWidth() / 2; + height = (int) adjustedFaceRect.getHeight() / 2; + pixelBuffer = new int[width * height]; + + int startX = (int) adjustedFaceRect.getX(); + int startY = (int) adjustedFaceRect.getY(); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + pixelBuffer[(y * width) + x] = (int) image.getPixel(x + startX, y + startY); + } + } + + } + + + public Point detectEye() { + Point eyePosition = null; + float brightness = 255f; + for ( int y = 0; y < height; ++y) { + for ( int x = 0; x < width; ++x) { + final int position = y * width + x; + final int[] color = + new int[] { (pixelBuffer[position] & 0xFF0000) >> 16, + (pixelBuffer[position] & 0x00FF00) >> 8, pixelBuffer[position] & 0x0000FF }; + // System.out.println("("+x+","+y+")="+color[0]+" "+color[1]+" "+color[2]); + final float acBrightness = getBrightness(color); + + if (acBrightness < brightness) { + eyePosition = new Point(x + (int) percent, y + (int) percent); + brightness = acBrightness; + } + } + } + + return eyePosition; + } + + + private static float getBrightness( int[] color) { + int min = Math.min(Math.min(color[0], color[1]), color[2]); + int max = Math.max(Math.max(color[0], color[1]), color[2]); + + return 0.5f * (max + min); + } +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/EyePosition.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/EyePosition.java new file mode 100644 index 00000000..500b0086 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/EyePosition.java @@ -0,0 +1,94 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * No description given. + * + * @author Florian Frankenberger + */ + + +public class EyePosition { + + private int x; + + private int y; + + private Rectangle2D faceRect; + + public EyePosition(Point p, Rectangle2D faceRect) { + this(p.x, p.y, faceRect); + } + + public EyePosition(int x, int y, Rectangle2D faceRect) { + this.x = x; + this.y = y; + this.faceRect = faceRect; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public String toString() { + return "(" + x + "," + y + ")"; + } + + // public Deviation getDeviation(EyePosition oldEyePosition) { + // if (oldEyePosition == null) return Deviation.NONE; + // + // //first we check if the faceRects are corresponding + // double widthChange = (this.faceRect.getWidth() - + // oldEyePosition.faceRect.getWidth()) / this.faceRect.getWidth(); + // if (widthChange > 0.1) return Deviation.NONE; + // + // int maxDeviationX = (int)Math.round(this.faceRect.getWidth() / 4f); + // int maxDeviationY = (int)Math.round(this.faceRect.getWidth() / 8f); + // int minDeviation = (int)Math.round(this.faceRect.getWidth() / 16f); + // + // int deviationX = Math.abs(x - oldEyePosition.x); + // int directionX = sgn(x - oldEyePosition.x); + // if (deviationX < minDeviation || deviationX > maxDeviationX) directionX = + // 0; + // + // int deviationY = Math.abs(y - oldEyePosition.y); + // int directionY = sgn(y - oldEyePosition.y); + // if (deviationY < minDeviation || deviationY > maxDeviationY) directionY = + // 0; + // + // double deviationXPercent = deviationX / this.faceRect.getWidth(); + // double deviationYPercent = deviationY / this.faceRect.getWidth(); + // + // System.out.println(String.format("devX: %.2f | devY: %.2f", + // deviationXPercent*100f, deviationYPercent*100f)); + // return Deviation.getDirectionFor(directionX, directionY); + // } + + private static int sgn(int i) { + if (i > 0) + return 1; + if (i < 0) + return -1; + return 0; + } +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/FaceAndEyePosition.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/FaceAndEyePosition.java new file mode 100644 index 00000000..cf24488e --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/FaceAndEyePosition.java @@ -0,0 +1,47 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * No description given. + * + * @author Florian Frankenberger + */ + + +public class FaceAndEyePosition { + + + private Rectangle2D facePosition; + + private EyePosition eyePosition; + + public FaceAndEyePosition(Rectangle2D facePosition, EyePosition eyePosition) { + this.facePosition = facePosition; + this.eyePosition = eyePosition; + } + + public Rectangle2D getFacePosition() { + return this.facePosition; + } + + public EyePosition getEyePosition() { + return this.eyePosition; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ICaptureDevice.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ICaptureDevice.java new file mode 100644 index 00000000..f050a978 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ICaptureDevice.java @@ -0,0 +1,52 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + + +import java.awt.image.BufferedImage; + +/** + * Describes a capture device. For now it is only tested + * with images in 640x480 at RGB or YUV color space. + * + * @author Florian Frankenberger + */ +public interface ICaptureDevice { + + /** + * Returns the frame rate of the image source per second + * + * @return the frame rate (e.g. 15 = 15 frames per second) + */ + public int getFrameRate(); + + /** + * Will be called a maximum of getFrameRate()-times in a second and returns + * the actual image of the capture device + * + * @return the actual image of the capture device + */ + public BufferedImage getImage(); + + /** + * LEA calls this when it cleans up. You should put your own cleanup code in here. + */ + public void close(); + + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Image.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Image.java new file mode 100644 index 00000000..d91a7e4a --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Image.java @@ -0,0 +1,50 @@ + + +public class Image { + + + int width; + + int height; + + int pixel[][]; + + public Image(int width, int height) { + this.width = width; + this.height = height; + pixel = new int[width][height]; + } + + public void setPixel(int x, int y, int R, int G, int B) { + pixel[x][y] = (R << 16) | (G << 8) | B; + } + + public int getRed(int x, int y) { + return (pixel[x][y] >> 16) & 0xff; + } + + public int getGreen(int x, int y) { + return (pixel[x][y] >> 8) & 0xff; + } + + public int getBlue(int x, int y) { + return pixel[x][y] & 0xff; + } + + public void setPixel(int x, int y, int p) { + pixel[x][y] = p; + } + + public long getPixel(int x, int y) { + return pixel[x][y]; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ImageReader.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ImageReader.java new file mode 100644 index 00000000..36aa569f --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ImageReader.java @@ -0,0 +1,193 @@ +public class ImageReader { + + static int idx = 0; + + public ImageReader() { + } + + @TRUST + public static Image getImage() { + System.out.println(idx); + Image image = ImageReader.readImage("data/b" + idx + ".bmp"); + idx++; + if (idx > 73) { + return null; + } + return image; + } + + public static Image readImage(String file) { + + FileInputStream fs = new FileInputStream(file); + int bflen = 14; // 14 byte BITMAPFILEHEADER + byte bf[] = new byte[bflen]; + // fs.read(bf,0,bflen); + fs.read(bf); + int bilen = 40; // 40-byte BITMAPINFOHEADER + byte bi[] = new byte[bilen]; + // fs.read(bi,0,bilen); + fs.read(bi); + // Interperet data. + int nsize = + (((int) bf[5] & 0xff) << 24) | (((int) bf[4] & 0xff) << 16) | (((int) bf[3] & 0xff) << 8) + | (int) bf[2] & 0xff; + // System.out.println("File type is :" + (char) bf[0] + (char) bf[1]); + // System.out.println("Size of file is :" + nsize); + int nbisize = + (((int) bi[3] & 0xff) << 24) | (((int) bi[2] & 0xff) << 16) | (((int) bi[1] & 0xff) << 8) + | (int) bi[0] & 0xff; + // System.out.println("Size of bitmapinfoheader is :" + nbisize); + int nwidth = + (((int) bi[7] & 0xff) << 24) | (((int) bi[6] & 0xff) << 16) | (((int) bi[5] & 0xff) << 8) + | (int) bi[4] & 0xff; + // System.out.println("Width is :" + nwidth); + int nheight = + (((int) bi[11] & 0xff) << 24) | (((int) bi[10] & 0xff) << 16) | (((int) bi[9] & 0xff) << 8) + | (int) bi[8] & 0xff; + // System.out.println("Height is :" + nheight); + int nplanes = (((int) bi[13] & 0xff) << 8) | (int) bi[12] & 0xff; + // System.out.println("Planes is :" + nplanes); + int nbitcount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xff; + // System.out.println("BitCount is :" + nbitcount); + // Look for non-zero values to indicate compression + int ncompression = + (((int) bi[19]) << 24) | (((int) bi[18]) << 16) | (((int) bi[17]) << 8) | (int) bi[16]; + // System.out.println("Compression is :" + ncompression); + int nsizeimage = + (((int) bi[23] & 0xff) << 24) | (((int) bi[22] & 0xff) << 16) + | (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xff; + // System.out.println("SizeImage is :" + nsizeimage); + + int nxpm = + (((int) bi[27] & 0xff) << 24) | (((int) bi[26] & 0xff) << 16) + | (((int) bi[25] & 0xff) << 8) | (int) bi[24] & 0xff; + // System.out.println("X-Pixels per meter is :" + nxpm); + int nypm = + (((int) bi[31] & 0xff) << 24) | (((int) bi[30] & 0xff) << 16) + | (((int) bi[29] & 0xff) << 8) | (int) bi[28] & 0xff; + // System.out.println("Y-Pixels per meter is :" + nypm); + int nclrused = + (((int) bi[35] & 0xff) << 24) | (((int) bi[34] & 0xff) << 16) + | (((int) bi[33] & 0xff) << 8) | (int) bi[32] & 0xff; + // System.out.println("Colors used are :" + nclrused); + int nclrimp = + (((int) bi[39] & 0xff) << 24) | (((int) bi[38] & 0xff) << 16) + | (((int) bi[37] & 0xff) << 8) | (int) bi[36] & 0xff; + // System.out.println("Colors important are :" + nclrimp); + + Image image = new Image(nwidth, nheight); + + if (nbitcount == 24) { + // No Palatte data for 24-bit format but scan lines are + // padded out to even 4-byte boundaries. + int npad = (nsizeimage / nheight) - nwidth * 3; + // ndata = new int[(nheight * nwidth) + 4]; + byte brgb[] = new byte[(nwidth + npad) * 3 * nheight]; + // fs.read (brgb, 0, (nwidth + npad) * 3 * nheight); + fs.read(brgb); + int nindex = 0; + int yPos = 0; + for (int j = 0; j < nheight; j++) { + for (int i = 0; i < nwidth; i++) { + // ndata[nwidth * (nheight - j - 1) + i] = + // (255 & 0xff) << 24 | (((int) brgb[nindex + 2] & 0xff) << 16) + // | (((int) brgb[nindex + 1] & 0xff) << 8) | (int) brgb[nindex] & + // 0xff; + int ta = + ((3 * ((int) (brgb[nindex + 2]) & 0xff) + 6 * ((int) brgb[nindex + 1] & 0xff) + ((int) brgb[nindex] & 0xff))) / 10; + + // ndata[nwidth * (nheight - j - 1) + i + 4] = ta; + yPos = nheight - j - 1; + // System.out.println("yPos=" + yPos + " nheight=" + nheight + " j=" + + // j); + // System.out.println("Encoded Color at (" + i + "," + yPos + ")is:" + + // brgb + " (R,G,B)= (" + // + ((int) (brgb[nindex + 2]) & 0xff) + "," + ((int) brgb[nindex + 1] + // & 0xff) + "," + // + ((int) brgb[nindex] & 0xff) + ")" + "cufoff=" + ta); + image.setPixel(i, yPos, ((int) brgb[nindex + 2] & 0xff), ((int) brgb[nindex + 1] & 0xff), + ((int) brgb[nindex] & 0xff)); + nindex += 3; + } + nindex += npad; + } + // image = createImage + // ( new MemoryImageSource (nwidth, nheight, + // ndata, 0, nwidth)); + + } else if (nbitcount == 8) { + // Have to determine the number of colors, the clrsused + // parameter is dominant if it is greater than zero. If + // zero, calculate colors based on bitsperpixel. + int nNumColors = 0; + if (nclrused > 0) { + nNumColors = nclrused; + } else { + nNumColors = (1 & 0xff) << nbitcount; + } + System.out.println("The number of Colors is" + nNumColors); + // Some bitmaps do not have the sizeimage field calculated + // Ferret out these cases and fix 'em. + if (nsizeimage == 0) { + nsizeimage = ((((nwidth * nbitcount) + 31) & 31) >> 3); + nsizeimage *= nheight; + System.out.println("nsizeimage (backup) is" + nsizeimage); + } + // Read the palatte colors. + int npalette[] = new int[nNumColors]; + byte bpalette[] = new byte[nNumColors * 4]; + // fs.read (bpalette, 0, nNumColors*4); + fs.read(bpalette); + int nindex8 = 0; + for (int n = 0; n < nNumColors; n++) { + npalette[n] = + (255 & 0xff) << 24 | (((int) bpalette[nindex8 + 2] & 0xff) << 16) + | (((int) bpalette[nindex8 + 1] & 0xff) << 8) | (int) bpalette[nindex8] & 0xff; + // System.out.println ("Palette Color "+n + // +" is:"+npalette[n]+" (res,R,G,B)= (" +((int)(bpalette[nindex8+3]) & + // 0xff)+"," +((int)(bpalette[nindex8+2]) & 0xff)+"," + // +((int)bpalette[nindex8+1]&0xff)+"," + // +((int)bpalette[nindex8]&0xff)+")"); + + nindex8 += 4; + } + // Read the image data (actually indices into the palette) + // Scan lines are still padded out to even 4-byte + // boundaries. + int npad8 = (nsizeimage / nheight) - nwidth; + // System.out.println("nPad is:" + npad8); + // int ndata8[] = new int[nwidth * nheight]; + // ndata = new int[(nwidth * nheight) + 4]; + byte bdata[] = new byte[(nwidth + npad8) * nheight]; + // fs.read (bdata, 0, (nwidth+npad8)*nheight); + fs.read(bdata); + nindex8 = 0; + for (int j8 = 0; j8 < nheight; j8++) { + for (int i8 = 0; i8 < nwidth; i8++) { + // ndata[nwidth * (nheight - j8 - 1) + i8 + 4] = npalette[((int) + // bdata[nindex8] & 0xff)]; + image.setPixel(i8, j8, npalette[((int) bdata[nindex8] & 0xff)]); + // System.out.println("Encoded Color at (" + i8 + "," + j8 + ")is: " + // + ndata[nwidth * (nheight - j8 - 1) + i8 + 4]); + nindex8++; + } + nindex8 += npad8; + } + // image = createImage ( new MemoryImageSource (nwidth, nheight, + // ndata8, 0, nwidth)); + } else { + System.out.println("Not a 24-bit or 8-bit Windows Bitmap, aborting..."); + // image = (Image)null; + } + fs.close(); + + // ndata[0] = nheight; + // ndata[1] = nwidth; + // ndata[2] = nheight * nwidth; + // ndata[3] = 2; + + return image; + // return ndata; + + } +} \ No newline at end of file diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/IntegralImageData.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/IntegralImageData.java new file mode 100644 index 00000000..d517a373 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/IntegralImageData.java @@ -0,0 +1,66 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * No description given. + * + * @author Florian Frankenberger + */ + + +public class IntegralImageData { + + + private long[][] integral; + + private int width; + + private int hegith; + + // private Dimension dimension; + + public IntegralImageData(Image bufferedImage) { + this.integral = new long[bufferedImage.getWidth()][bufferedImage.getHeight()]; + this.width = bufferedImage.getWidth(); + this.hegith = bufferedImage.getHeight(); + + long[][] s = new long[bufferedImage.getWidth()][bufferedImage.getHeight()]; + for (int y = 0; y < bufferedImage.getHeight(); ++y) { + for (int x = 0; x < bufferedImage.getWidth(); ++x) { + s[x][y] = (y - 1 < 0 ? 0 : s[x][y - 1]) + (bufferedImage.getBlue(x, y) & 0xff); + this.integral[x][y] = (x - 1 < 0 ? 0 : this.integral[x - 1][y]) + s[x][y]; + // System.out.println("integral ("+x+","+y+")="+integral[x][y]); + } + } + + } + + public long getIntegralAt( int x, int y) { + return this.integral[x][y]; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return hegith; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LATTICE.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LATTICE.java new file mode 100644 index 00000000..3cb4b1da --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LATTICE.java @@ -0,0 +1,3 @@ +public @interface LATTICE { + String value(); +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LEA.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LEA.java new file mode 100644 index 00000000..48a15775 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LEA.java @@ -0,0 +1,124 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * This is the main class of LEA. + *

+ * It uses a face detection algorithm to find an a face within the provided + * image(s). Then it searches for the eye in a region where it most likely + * located and traces its position relative to the face and to the last known + * position. The movements are estimated by comparing more than one movement. If + * a movement is distinctly pointing to a direction it is recognized and all + * listeners get notified. + *

+ * The notification is designed as observer pattern. You simply call + * addEyeMovementListener(IEyeMovementListener) to add an + * implementation of IEyeMovementListener to LEA. When a face is + * recognized/lost or whenever an eye movement is detected LEA will call the + * appropriate methods of the listener + *

+ * LEA also needs an image source implementing the ICaptureDevice. + * One image source proxy to the Java Media Framework is included ( + * JMFCaptureDevice). + *

+ * Example (for using LEA with Java Media Framework): + *

+ * + * LEA lea = new LEA(new JMFCaptureDevice(), true); + * + *

+ * This will start LEA with the first available JMF datasource with an extra + * status window showing if face/eye has been detected successfully. Please note + * that face detection needs about 2 seconds to find a face. After detection the + * following face detection is much faster. + * + * @author Florian Frankenberger + */ + + +public class LEA { + + + private LEAImplementation implementation; + + private FaceAndEyePosition lastPositions = new FaceAndEyePosition(null, null); + + private DeviationScanner deviationScanner = new DeviationScanner(); + + public LEA() { + // this.imageProcessor = new + // ImageProcessor(this.captureDevice.getFrameRate()); + implementation = new LEAImplementation(); + } + + /** + * Clears the internal movement buffer. If you just capture some of the eye + * movements you should call this every time you start recording the + * movements. Otherwise you may get notified for movements that took place + * BEFORE you started recording. + */ + public void clear() { + // this.imageProcessor.clearDeviationScanner(); + } + + /** + * @METHOD To test LEA with the first capture device from the + * Java Media Framework just start from here. + * + * @param args + * @throws Exception + */ + public static void main(String[] args) throws Exception { + LEA lea = new LEA(); + lea.doRun(); + } + + + public void doRun() { + + int i = 0; + + SSJAVA: while (true) { + Image image = ImageReader.getImage(); + if (image == null) { + break; + } + processImage(image); + } + + System.out.println("Done."); + + } + + + private void processImage( Image image) { + FaceAndEyePosition positions = implementation.getEyePosition(image); + // if (positions.getEyePosition() != null) { + deviationScanner.addEyePosition(positions.getEyePosition()); + int deviation = + deviationScanner.scanForDeviation(positions.getFacePosition());// positions.getEyePosition().getDeviation(lastPositions.getEyePosition()); + if (deviation != DeviationScanner.NONE) { + System.out.println("deviation=" + deviationScanner.toStringDeviation(deviation)); + // notifyEyeMovementListenerEyeMoved(deviation); + } + // } + lastPositions = positions; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LEAImplementation.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LEAImplementation.java new file mode 100644 index 00000000..998cd69b --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LEAImplementation.java @@ -0,0 +1,125 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * No description given. + * + * @author Florian Frankenberger + */ + + +public class LEAImplementation { + + + private ClassifierTree classifierTree; + + + private Rectangle2D lastRectangle; + + public LEAImplementation() { + this.loadFaceData(); + } + + + + public FaceAndEyePosition getEyePosition( Image image) { + if (image == null) + return null; + Rectangle2D faceRect = + classifierTree.locateFaceRadial(image, lastRectangle); + if (faceRect.getWidth() > image.getWidth() || faceRect.getHeight() > image.getHeight()) { + return null; + } + EyePosition eyePosition = null; + if (faceRect != null) { + lastRectangle = faceRect; + faceRect = null; + Point point = readEyes(image, lastRectangle); + if (point != null) { + eyePosition = new EyePosition(point, lastRectangle); + } + } else { + lastRectangle = null; + } + System.out.println("eyePosition=" + eyePosition); + + return new FaceAndEyePosition(lastRectangle, eyePosition); + } + + + + private Point readEyes( Image image, Rectangle2D rect) { + EyeDetector ed = new EyeDetector(image, rect); + return ed.detectEye(); + } + + public boolean needsCalibration() { + return false; + } + + /** + * This method loads the faceData from a file called facedata.dat which should + * be within the jar-file + */ + private void loadFaceData() { + + FileInputStream inputFile = new FileInputStream("facedata.dat"); + + int numClassifier = Integer.parseInt(inputFile.readLine()); + classifierTree = new ClassifierTree(numClassifier); + for (int c = 0; c < numClassifier; c++) { + + int numArea = Integer.parseInt(inputFile.readLine()); + Classifier classifier = new Classifier(numArea); + // parsing areas + for (int idx = 0; idx < numArea; idx++) { + // 54,54,91,62,296.0 + Point fromPoint = new Point(); + Point toPoint = new Point(); + fromPoint.x = Integer.parseInt(inputFile.readLine()); + fromPoint.y = Integer.parseInt(inputFile.readLine()); + toPoint.x = Integer.parseInt(inputFile.readLine()); + toPoint.y = Integer.parseInt(inputFile.readLine()); + float size = Float.parseFloat(inputFile.readLine()); + ScanArea area = new ScanArea(fromPoint, toPoint, size); + classifier.setScanArea(idx, area); + } + + // parsing possibilities face yes + float array[] = new float[numArea]; + for (int idx = 0; idx < numArea; idx++) { + array[idx] = Float.parseFloat(inputFile.readLine()); + } + classifier.setPossibilitiesFaceYes(array); + + // parsing possibilities face no + array = new float[numArea]; + for (int idx = 0; idx < numArea; idx++) { + array[idx] = Float.parseFloat(inputFile.readLine()); + } + classifier.setPossibilitiesFaceNo(array); + + classifier.setPossibilityFaceYes(Integer.parseInt(inputFile.readLine())); + classifier.setPossibilityFaceNo(Integer.parseInt(inputFile.readLine())); + + classifierTree.addClassifier(c, classifier); + } + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LOC.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LOC.java new file mode 100644 index 00000000..0165516c --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/LOC.java @@ -0,0 +1,7 @@ +public @interface LOC { + String value(); +} + +public @interface RETURNLOC { + String value(); +} \ No newline at end of file diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Point.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Point.java new file mode 100644 index 00000000..beb0545f --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Point.java @@ -0,0 +1,20 @@ + + +public class Point { + + public int x; + public int y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public Point() { + } + + public String toString(){ + return "("+x+","+y+")"; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Rectangle2D.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Rectangle2D.java new file mode 100644 index 00000000..e765e70c --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/Rectangle2D.java @@ -0,0 +1,36 @@ + + +public class Rectangle2D { + + double x; + double y; + double width; + double height; + + public Rectangle2D(double x, double y, double w, double h) { + this.x = x; + this.y = y; + this.width = w; + this.height = h; + } + + public double getX() { + return x; + } + + public double getY() { + return y; + } + + public double getWidth() { + return width; + } + + public double getHeight() { + return height; + } + + public String toString() { + return "(" + x + "," + y + "," + width + "," + height + ")"; + } +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ScanArea.java b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ScanArea.java new file mode 100644 index 00000000..abb581b0 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/ScanArea.java @@ -0,0 +1,119 @@ +/* + * Copyright 2009 (c) Florian Frankenberger (darkblue.de) + * + * This file is part of LEA. + * + * LEA is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * LEA is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with LEA. If not, see . + */ + +/** + * + * @author Florian + */ + + +public class ScanArea { + + + private Point fromPoint; + + private Point toPoint; + + private float size; + + /** + * Imagine you want to classify an image with 100px x 100px what would be the + * scanarea in this kind of image. That size gets automatically scalled to fit + * bigger images + * + * @param fromPoint + * @param toPoint + */ + public ScanArea(Point fromPoint, Point toPoint) { + this.fromPoint = fromPoint; + this.toPoint = toPoint; + + this.size = (this.toPoint.x - this.fromPoint.x) * (this.toPoint.y - this.fromPoint.y); + } + + public ScanArea(Point fromPoint, Point toPoint, float size) { + this.fromPoint = fromPoint; + this.toPoint = toPoint; + this.size = size; + } + + public ScanArea(int fromX, int fromY, int width, int height) { + this(new Point(fromX, fromY), new Point(fromX + width, fromY + height)); + } + + public int getFromX( float scaleFactor) { + return (int) (this.fromPoint.x * scaleFactor); + } + + public int getFromY( float scaleFactor) { + return (int) (this.fromPoint.y * scaleFactor); + } + + public int getToX( float scaleFactor) { + return (int) (this.toPoint.x * scaleFactor); + } + + public int getToY( float scaleFactor) { + return (int) (this.toPoint.y * scaleFactor); + } + + public int getSize( float scaleFactor) { + return (int) (this.size * Math.pow(scaleFactor, 2)); + } + + @Override + public boolean equals(Object o) { + ScanArea other = (ScanArea) o; + + return pointsWithin(other.fromPoint.x, other.toPoint.x, this.fromPoint.x, this.toPoint.x) + && pointsWithin(other.fromPoint.y, other.toPoint.y, this.fromPoint.y, this.toPoint.y); + } + + private static boolean pointsWithin(int pointA1, int pointA2, int pointB1, int pointB2) { + boolean within = false; + within = within || (pointB1 >= pointA1 && pointB1 <= pointA2); + within = within || (pointB2 >= pointA1 && pointB2 <= pointA2); + within = within || (pointA1 >= pointB1 && pointA1 <= pointB2); + within = within || (pointA2 >= pointB1 && pointA2 <= pointB2); + + return within; + } + + // private boolean checkPoints(ScanArea a, ScanArea b) { + // Point[] pointsToCheck = new Point[] { + // a.fromPoint, a.toPoint, + // new Point (a.fromPoint.x, a.toPoint.y), + // new Point (a.toPoint.x, a.fromPoint.y) + // }; + // for (Point point: pointsToCheck) { + // if (point.x >= b.fromPoint.x && point.x <= b.toPoint.x && + // point.y >= b.fromPoint.y && point.y <= b.toPoint.y) return true; + // } + // + // return false; + // } + + public String toString() { + String str = ""; + str += "fromPoint=(" + fromPoint.x + "," + fromPoint.y + ")"; + str += "toPoint=(" + toPoint.x + "," + toPoint.y + ")"; + str += "size=" + size; + return str; + } +} diff --git a/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/makefile b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/makefile new file mode 100644 index 00000000..f42b8fb8 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/EyeTrackingInfer/makefile @@ -0,0 +1,52 @@ +BUILDSCRIPT=../../../buildscript + +PROGRAM=LEA +SOURCE_FILES=LEA.java + +ifndef INV_ERROR_PROB +INV_ERROR_PROB=1000 +endif + +ifndef RANDOMSEED +RANDOMSEED=12345 +endif + +SSJAVA= -ssjava -ssjavainfer -ssjavadebug +BSFLAGS= -32bit -mainclass $(PROGRAM) -heapsize-mb 1350 -nooptimize -debug -garbagestats #-printlinenum #-joptimize +NORMAL= -ssjava-inject-error 0 0 +INJECT_ERROR= -ssjava-inject-error $(INV_ERROR_PROB) $(RANDOMSEED) + + +default: $(PROGRAM)s.bin + +normal: $(PROGRAM)n.bin + +error: $(PROGRAM)e.bin + + +$(PROGRAM)s.bin: $(SOURCE_FILES) makefile + $(BUILDSCRIPT) $(SSJAVA) $(BSFLAGS) -o $(PROGRAM)s -builddir ssj $(SOURCE_FILES) + +$(PROGRAM)n.bin: $(SOURCE_FILES) makefile + $(BUILDSCRIPT) $(NORMAL) $(BSFLAGS) -o $(PROGRAM)n -builddir norm $(SOURCE_FILES) + +$(PROGRAM)e.bin: $(SOURCE_FILES) makefile + $(BUILDSCRIPT) $(SSJAVA) $(INJECT_ERROR) $(BSFLAGS) -o $(PROGRAM)e -builddir injerr $(SOURCE_FILES) + +cleanerror: + rm -f $(PROGRAM)e.bin + rm -rf injerr + +clean: + rm -f $(PROGRAM)s.bin $(PROGRAM)n.bin $(PROGRAM)e.bin + rm -fr ssj norm injerr + rm -f nve-diff.tmp nve-diff-ranges.tmp + rm -f *~ + rm -f *.dot + rm -f *.png + rm -f *.pdf + rm -f aliases.txt + rm -f results*txt + rm -f output.txt + rm -f *log + diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/MotorControl.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/MotorControl.java new file mode 100644 index 00000000..ea7f2f06 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/MotorControl.java @@ -0,0 +1,318 @@ +/* + * PWMRtsj.java + * + * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + */ + +/** + * This class is a wrapper for a PWMControl introduced by porting to SSJava. It + * maintains two key fields motorLeftUpTime and motorRightUpTime and expects + * that the control thread who is running from the other side gets the current + * settings. + */ + + + +public class MotorControl { + + boolean DEBUG = true; + + int motorLeftUpTime = 150; + + int motorRightUpTime = 150; + + int speedFactor; + + int agilityFactor; + + public MotorControl(int speedFactor, int agilityFactor) { + this.speedFactor = speedFactor; + this.agilityFactor = agilityFactor; + } + + // A poor's man ajustimg for the 0 speed which found + // to be 450000 nano seconds. + + private int normalizeTime( int timePosition) { + if ((timePosition <= 50) && (timePosition >= 44)) { + return 45; + } + return timePosition; + } + + /** + * setSpeedSpin - Set speed for the spin case motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedSpinLeft( int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + if (DEBUG) { + System.out.println("setSpeedSpinLeft: input-> " + Integer.toString(timePosition)); + } + int timePos = normalizeTime(timePosition); + + int motorUpTime = (int) (timePos * agilityFactor * speedFactor); + // System.out.println("Left Motor UpTime1: " + + // Integer.toString(motorUpTime)); + // Since the right motor is hanging in reverse position + // the direction should be opposit + // Bug in wait. Can't take 0 nanoseconds + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + + // if (DEBUG) { + // System.out.println("setSpeedSpinLeft: output-> = " + + // Integer.toString(motorUpTime)); + // } + // synchronized (this) { + /* Factor in the speed and agility factors */ + motorLeftUpTime = motorUpTime; + // } + if (DEBUG) { + System.out.println("MotorControl: setSpeedSpinLeft: output-> " + motorLeftUpTime); + } + } + + /** + * setSpeedSpinRight - Set speed for the spin case right motor. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedSpinRight( int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* An input of 50 should result in 0 speed. */ + /* 100 should result in full speed forward */ + /* while 0 should result in full speed backwords */ + if (DEBUG) { + System.out.println("setSpeedSpinRight: input-> " + Integer.toString(timePosition)); + } + int timePos = normalizeTime(timePosition); + int motorUpTime = (int) ((timePos) * agilityFactor * speedFactor); + // Bug in wait. Cant take 0 nanoseconds + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + + // if (DEBUG) { + // System.out.println("setSpeedSpinRight: output-> = " + + // Integer.toString(motorUpTime)); + // } + // synchronized (this) { + /* Factor in the speed and agility factors */ + motorRightUpTime = motorUpTime; + // } + if (DEBUG) { + System.out.println("MotorControl: setSpeedSpinRight: output-> " + motorRightUpTime); + } + } + + /** + * setSpeedTurnM1 - set speed considering agility factor for motor 1 + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedTurnLeft( int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + if (DEBUG) { + System.out.println("setSpeedTurnLeft: input-> " + Integer.toString(timePosition)); + } + int timePosLocal = normalizeTime(timePosition); + int motorUpTime = + (timePosLocal * 100 + ((100 - timePosLocal) * (100 - agilityFactor))) * speedFactor; + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + // if (DEBUG) { + // System.out.println("setSpeedTurnLeft: output-> = " + + // Integer.toString(motorUpTime)); + // } + // synchronized (this) { + /* Factor in the speed and agility factors */ + motorLeftUpTime = motorUpTime; + // } + if (DEBUG) { + System.out.println("MotorControl: setSpeedTurnLeft: output-> " + motorLeftUpTime); + } + } + + /** + * setSpeedTurnM1 - set speed considering agility factor for motor 2 + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedTurnRight( int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + if (DEBUG) { + System.out.println("setSpeedTurnRight: input-> " + Integer.toString(timePosition)); + } + int timePos = normalizeTime(timePosition); + int motorUpTime = + ((timePos * 100) + ((100 - timePos) * (100 - agilityFactor))) * speedFactor; + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + + // synchronized (this) { + /* Factor in the speed and agility factors */ + motorRightUpTime = motorUpTime; + // } + if (DEBUG) { + System.out.println("MotorControl: setSpeedTurnRight: output-> " + motorRightUpTime); + } + } + + /** + * setSpeedLeft - speed control for motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedLeft( int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + if (DEBUG) { + System.out.println("setSpeedLeft: input-> " + Integer.toString(timePosition)); + } + int timePos = normalizeTime(timePosition); + int motorUpTime = (int) (timePos * 100) * speedFactor; + /* + * System.out.println("motorUpTime = " + Integer.toStri + * ng(motorUpTime) + " timePos = " + Integer.toString((int)timePos) + + * " timePosition = " + Integer.toString((int)timePosition) + + * " speedFactor = " + Integer.toString(speedFactor)); + */ + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + + // synchronized (this) { + /* Factor in speedFactor */ + motorLeftUpTime = motorUpTime; + // } + if (DEBUG) { + System.out + .println("MotorContol: setSpeedLeft: output-> " + Integer.toString(motorLeftUpTime)); + } + } + + /** + * setSpeedRight - speed control for motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedRight( int timePosition) { + if (DEBUG) { + System.out.println("setSpeedRight: input-> " + Integer.toString(timePosition)); + } + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + int timePos = normalizeTime(timePosition); + int motorUpTime = (int) (timePos * 100) * speedFactor; + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + // synchronized (this) { + /* Factor in speedFactor */ + motorRightUpTime = motorUpTime; + // } + if (DEBUG) { + System.out.println("MotorControl: setSpeedRight: output-> " + motorRightUpTime); + } + } + + public void setUrgentReverse() { + // synchronized (this) { + motorLeftUpTime = 1; + motorRightUpTime = 1; + // } + if (DEBUG) { + System.out.println("MotorControl: setUrgentReverse: motorLeftUpTime-> " + motorLeftUpTime); + System.out.println("MotorControl: setUrgentReverse: motorRightUpTime-> " + motorRightUpTime); + } + } + + public void setUrgentStraight() { + // synchronized (this) { + motorLeftUpTime = 99; + motorRightUpTime = 99; + if (DEBUG) { + System.out.println("MotorControl: setUrgentStraight: motorLeftUpTime-> " + motorLeftUpTime); + System.out.println("MotorControl: setUrgentStraight: motorRightUpTime-> " + motorRightUpTime); + } + // } + } + + public void justSync() { + // synchronized (this) { + motorLeftUpTime = motorLeftUpTime; + motorRightUpTime = motorRightUpTime; + if (DEBUG) { + System.out.println("MotorControl: justSync: motorLeftUpTime-> " + motorLeftUpTime); + System.out.println("MotorControl: justSync: motorRightUpTime-> " + motorRightUpTime); + } + // } + } + + /** + * Control debug messageing. true - Activate debug messages false - deactivate + * debug messages + */ + public void setDebug(boolean debug) { + DEBUG = debug; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMControl.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMControl.java new file mode 100644 index 00000000..05a1c56a --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMControl.java @@ -0,0 +1,192 @@ +/* + * PWMControl.java + * + * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + */ + +/** + * This class is the super class of all motor control classes. The class + * represent a generic interface to such class. In our case the motor controller + * require for 0 speed 1.5 ms out of 10-20ms cycle and a change between 1 to 2 + * seconds for max reverse and forward speed. These values: 1, 1.5, 2 and 20 ms + * are thus this class specific + * + * @author Michael Gesundheit + * @version 1.0 + */ + +public abstract class PWMControl { + + PWMManager pwmManager; + boolean DEBUG = false; + // boolean DEBUG = true; + static final byte PULSE_HIGH = 1; + static final byte PULSE_LOW = 0; + int highMask; + int m1HighMask; + int m2HighMask; + int lowMask; + int myOwnBit; + int myBitPos; + int m1BitPos; + int m2BitPos; + int speedFactor; + int agilityFactor; + boolean updateTime; + int pulseWidth; + Object obj; + + int motorLeftUpTime = 150; + int motorRightUpTime = 150; + boolean manualMode = false; + + PWMControl(PWMManager pwmMan, int motor1bit, int motor2bit) { + pwmManager = pwmMan; // This is papa + m1BitPos = 0x1 << motor1bit; + m2BitPos = 0x1 << motor2bit; + m1HighMask = 0x1 << motor1bit; + m2HighMask = 0x1 << motor2bit; + lowMask = 0x0; + obj = new Object(); + } + + public void setManualMode() { + if (DEBUG) + System.out.println("PWMContolr: setManualMode... "); +// synchronized (obj) { + if (manualMode == false) { + manualMode = true; + } +// } + } + + public void setAutomatedMode() { + if (DEBUG) + System.out.println("PWMControl: setAutomatedMode... "); +// synchronized (obj) { + if (manualMode == true) { + System.out.println("PWMControl: wake me up... "); + manualMode = false; + // obj.notify(); + System.out.println("PWMControl: wake me up...... tried!!!"); + } +// } + } + + /** + * OutPut value to motor control line + */ + public void outToPortMLeft(byte value) { + /* + * System.out.println("PWMControl " + Integer.toString(myOwnBit) + + * //" bit position = 0x" + Integer.toHexString(myOwnBit) + + * " output value = 0x" + Integer.toHexString(value)); + */ + if (value == PULSE_HIGH) { + pwmManager.writeToPort(m1BitPos, m1HighMask); + } else { + pwmManager.writeToPort(m1BitPos, lowMask); + } + } + + public void outToPortMRight(byte value) { + /* + * System.out.println("PWMControl " + Integer.toString(myOwnBit) + + * //" bit position = 0x" + Integer.toHexString(myOwnBit) + + * " output value = 0x" + Integer.toHexString(value)); + */ + if (value == PULSE_HIGH) { + pwmManager.writeToPort(m2BitPos, m2HighMask); + } else { + pwmManager.writeToPort(m2BitPos, lowMask); + } + } + + /** + * setSpeedSpin - Set speed for the spin case motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedSpinLeft(int timePosition) { + } + + /** + * setSpeedSpinRight - Set speed for the spin case right motor. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedSpinRight(int timePosition) { + } + + /** + * setSpeedTurnM1 - set speed considering agility factor for motor 1 + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedTurnLeft(int timePosition) { + } + + /** + * setSpeedTurnM1 - set speed considering agility factor for motor 2 + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedTurnRight(int timePosition) { + } + + /** + * setSpeedLeft - speed control for motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedLeft(int timePosition) { + } + + /** + * setSpeedRight - speed control for motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedRight(int timePosition) { + } + + public void setSpeedAgilityFactors(int speed, int agility) { +// synchronized (this) { + speedFactor = speed; + agilityFactor = agility; +// } + } + + public void setUrgentReverse() { + } + + public void setUrgentStraight() { + } + + /** + * Control debug messageing. true - Activate debug messages false - deactivate + * debug messages + */ + public void setDebug(boolean debug) { + DEBUG = debug; + } +} diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMManager.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMManager.java new file mode 100644 index 00000000..09f4aeb7 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMManager.java @@ -0,0 +1,214 @@ +/* + * PWMManager.java + * + * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + */ + +/** + * PWMManager - Interface between robot strategy code and the various motors + * PWMControl classes. Agility is a number between 0 to 100 and represent the + * precentage of max speed to be applied to the turn side wheel. On left turn + * the left wheel will receive less speed as % of "speed" value represented by + * agility. + * + * @author Michael Gesundheit + * @version 1.0 + */ + + + +public class PWMManager { + + // private int GPIO_A_OER = 0x09002000; + // private int GPIO_A_OUT = GPIO_A_OER + 4; + // private int GPIO_E_OER = 0x09002400; + // private int GPIO_E_OUT = 0x09002404; + // private byte MOTOR_PORTID_6 = 6; // Bit 2 (...3,2,1,0) + // private byte MOTOR_PORTID_7 = 7; // Bit 3 (...3,2,1,0) + + private int currentRegMask; + + private boolean DEBUG = false; + // private PWMControl pwmControl; + + private RCBridge rcb; + + private int speedFactor; + + private int agilityFactor; + + private int zeroSpeed = 45; + + /** + * Constractor + */ + public PWMManager(String pwmSelection) { + /* + * Instantiate PWM Makers one for each motor. motorPortId is 1 to 8 and is + * corresponding to 8 bits in a byte. + */ + // if (pwmSelection.equals("rtsj")) + // pwmControl = new PWMRtsj(this, MOTOR_PORTID_6, MOTOR_PORTID_7); + // else { + // System.out.println("Selection PWMNative is activated"); + // pwmControl = new PWMNative(this, MOTOR_PORTID_6, MOTOR_PORTID_7); + // System.out.println("Selection PWMNative is activated.... Return"); + // } + + // rcb = new RCBridge(this); + rcb = new RCBridge(); + + /* Enable input bits 0,1 Enable output for the rest */ + // rawJEM.set(GPIO_E_OER, 0x00C0); + } + + public void setManualMode() { + if (DEBUG) + System.out.println("PWMManager: setManualMode...."); + // pwmControl.setManualMode(); + rcb.setManualMode(); + } + + public void setAutomatedMode() { + if (DEBUG) + System.out.println("PWMManager: setAutomatedMode...."); + // pwmControl.setAutomatedMode(); + rcb.setAutomatedMode(); + } + + // public PWMControl getPWMControl() { + // if (DEBUG) + // System.out.println("PWMManager: getPWMControl...."); + // return pwmControl; + // } + + public synchronized void writeToPort(int myBit, int myValue) { + currentRegMask &= ~myBit; // e.g. 0x11110111 + currentRegMask |= myValue; + /* + * // if (DEBUG){ // System.out.println("PWMM: writeToPort: myBit = 0x" + + * Integer.toHexString(myBit) + // " ~myBit = 0x" + + * Integer.toHexString(~myBit) + " myValue = 0x" + // + * Integer.toHexString(myValue) + " currentRegMask = 0x" + // + * Integer.toHexString(currentRegMask)); //} + */ + // rawJEM.set(GPIO_E_OUT, currentRegMask); + } + + /* + * public void stop(){ if(DEBUG) System.out.println("PWMManager: stop...."); + * pwmControl.setSpeedLeft(zeroSpeed); pwmControl.setSpeedRight(zeroSpeed); } + * + * public void search(){ if(DEBUG) + * System.out.println("PWMManager: search...."); pwmControl.setSpeedLeft(70); + * pwmControl.setSpeedRight(50); } + * + * public void straight(){ if(DEBUG) + * System.out.println("PWMManager: strait...."); pwmControl.setSpeedLeft(100); + * pwmControl.setSpeedRight(100); } + * + * public void spinRight(){ if(DEBUG) + * System.out.println("PWMManager: spinRight...."); + * pwmControl.setSpeedSpinLeft(100); pwmControl.setSpeedSpinRight(0); } + * + * public void spinLeft(){ if(DEBUG) + * System.out.println("PWMManager: spinLeft...."); + * pwmControl.setSpeedSpinLeft(0); pwmControl.setSpeedSpinRight(100); } + * + * public void spin180(){ int mod = (rand.nextInt() % 2); + * + * if(DEBUG) System.out.println("PWMManager: spin180...."); if(mod == 1){ + * pwmControl.setSpeedSpinLeft(0); pwmControl.setSpeedSpinRight(100); }else{ + * pwmControl.setSpeedSpinLeft(100); pwmControl.setSpeedSpinRight(0); } } + * + * public void right(){ if(DEBUG) System.out.println("PWMManager: right...."); + * pwmControl.setSpeedTurnLeft(100); pwmControl.setSpeedRight(zeroSpeed); } + * + * public void left(){ if(DEBUG) System.out.println("PWMManager: left...."); + * pwmControl.setSpeedLeft(zeroSpeed); pwmControl.setSpeedTurnRight(100); } + * + * public void bearRight(){ if(DEBUG) + * System.out.println("PWMManager: bearRight...."); + * pwmControl.setSpeedTurnLeft(100); pwmControl.setSpeedTurnRight(60); } + * + * public void bearLeft(){ if(DEBUG) + * System.out.println("PWMManager: bearLeft...."); + * pwmControl.setSpeedTurnLeft(60); pwmControl.setSpeedTurnRight(100); } + * + * public void back(){ if(DEBUG) System.out.println("PWMManager: back...."); + * pwmControl.setSpeedLeft(0); pwmControl.setSpeedRight(0); } + * + * public void backBearLeft(){ if(DEBUG) + * System.out.println("PWMManager: backBearLeft...."); + * pwmControl.setSpeedLeft(30); pwmControl.setSpeedRight(0); } + * + * public void backBearRight(){ if(DEBUG) + * System.out.println("PWMManager: backBearRight...."); + * pwmControl.setSpeedLeft(0); pwmControl.setSpeedRight(30); } + */ + public void resume() { + if (DEBUG) + System.out.println("PWMManager: Reasume..........."); + } + + /** + * setSpeedFactor - set speed factor value + * + * @param speed + * factor + */ + public synchronized void setSpeedFactor(int speedFactor) { + if (DEBUG) + System.out.println("PWMManager: setSpeedFactor...."); + this.speedFactor = speedFactor; + // pwmControl.setSpeedAgilityFactors(speedFactor, agilityFactor); + } + + /** + * setAgilityFactor + * + * @param agility + * factor + */ + public synchronized void setAgilityFactor(int agilityFactor) { + if (DEBUG) + System.out.println("PWMManager: setAgilityFactor...."); + this.agilityFactor = agilityFactor; + // pwmControl.setSpeedAgilityFactors(speedFactor, agilityFactor); + } + + /** + * setSpeedAgilityFactors - set both speed and agility + * + * @param speed + * @param agility + */ + public synchronized void setSpeedAgilityFactors( int speed, int agility) { + if (DEBUG) + System.out.println("PWMManager: setSpeedAgilityFactors...."); + speedFactor = speed; + agilityFactor = agility; + // pwmControl.setSpeedAgilityFactors(speedFactor, agilityFactor); + } + + /** + * Control debug messaging. true - Activate debug messages false - deactivate + * debug messages + */ + public void setDebug(boolean debug) { + DEBUG = debug; + } +} \ No newline at end of file diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMNative.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMNative.java new file mode 100644 index 00000000..4bc2be52 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMNative.java @@ -0,0 +1,377 @@ +/* + * PWMNative.java + * + * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + */ + +//public class PWMNative extends PWMControl implements Runnable, InterruptEventListener { +public class PWMNative extends PWMControl { + + private int PULSE_INTERVAL = 2000; + private int NATIVE_OFFSET = 100; + private Object obj; + private Object tc0Obj; + private Object tc1Obj; + private int pulseUpTime; + + // private TimerCounter tc0; + // private TimerCounter tc1; + // private TimerCounter[] tcSet = new TimerCounter[2]; + + PWMNative(PWMManager pwmMan, int motor1bit, int motor2bit) { + super(pwmMan, motor1bit, motor2bit); + + System.out.println("PWMNative constructor.....Start"); + obj = new Object(); + tc0Obj = new Object(); + tc1Obj = new Object(); + + // TimerCounter.setPrescalerClockSource(TimerCounter.INTERNAL_PERIPHERAL_CLOCK); + // TimerCounter.setPrescalerReloadRegisterValue(375); + // TimerCounter.setPrescalerEnabled(true); + + // tc0 = tcSet[0] = new TimerCounter(0); + // tc0.setMode_IO_Line_A(TimerCounter.TIMER_0_OUTPUT); + // // bring TIMER_0_OUTPUT_DIVIDE_BY_2 out via IO_Line_B + // tc0.setMode_IO_Line_B(TimerCounter.TIMER_0_OUTPUT_DIVIDE_BY_2); + // // In JemBuilder, go to Pin Setup 2 and allocate Port E bit 6 to + // // be Timer IO_Line_B. + // + // // Connect signal lead of servo (usually the white one) to IOE6 on + // // JStamp + // tc0.set_IO_Line_A_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH); + // tc0.set_IO_Line_B_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH); + // tc0.setExternalTimerEnableMode(TimerCounter.TIMER_ENABLED_ONLY_VIA_MTEN_AND_TRIGGER); + // tc0.setReloadRegisterValue(100); + // tc0.setExternalStartTriggerMode(TimerCounter.NO_EXTERNAL_START_TRIGGER); + // tc0.setMasterTimerEnabled(true); + // tc0.setGlobalInterruptEnabled(true); + // tc0.setTimeOutInterruptEnabled(true); + // tc0.setTimerTriggerRegister(false); + // System.out.println("PWMNative: Constructor completed 1...."); + /* + * tc1 = tcSet[1] = new TimerCounter ( 1 ); tc1.setMode_IO_Line_A( + * TimerCounter.TIMER_1_OUTPUT ); //bring TIMER_0_OUTPUT_DIVIDE_BY_2 out via + * IO_Line_B tc1.setMode_IO_Line_B( TimerCounter.TIMER_1_OUTPUT_DIVIDE_BY_2 + * ); //In JemBuilder, go to Pin Setup 2 and allocate Port E bit 6 to //be + * Timer IO_Line_B. + * tc1.set_IO_Line_A_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH); + * tc1.set_IO_Line_B_Polarity(TimerCounter.POLARITY_ACTIVE_STATE_HIGH); + * tc1.setExternalTimerEnableMode + * (TimerCounter.TIMER_ENABLED_ONLY_VIA_MTEN_AND_TRIGGER ); + * tc1.setReloadRegisterValue( 100 ); + * tc1.setExternalStartTriggerMode(TimerCounter.NO_EXTERNAL_START_TRIGGER ); + * tc1.setMasterTimerEnabled( true ); tc1.setGlobalInterruptEnabled( true); + * tc1.setTimeOutInterruptEnabled( true); tc1.setTimerTriggerRegister( false + * ); + */ + /* + * // Add interrupt event listener for GPTC + * InterruptController.addInterruptListener( this, + * InterruptController.GPTC_INTERRUPT ); + * + * // Turn on interrupts InterruptController.enableInterrupt( + * InterruptController.GPTC_INTERRUPT ); + * + * // start all prescaler based timers TimerCounter.setPrescalerEnabled( + * true ); + */ + // t2 = new Thread(this); + // t2.start(); + // t2.setPriority(20); + System.out.println("PWMNative: Constructor return....."); + } + + public void setUpTime(int upTime) { + // synchronized (obj) { + pulseUpTime = upTime; + // } + } + + /* + * public void interruptEvent() { TimerCounter tc; int tcIntrState; + * + * System.out.println("PWMNative: InterruptEvent"); do { for ( int tcNum=0; + * tcNum<2; tcNum++ ) { tc = tcSet[ tcNum ]; + * if(tc.readAndClear_TimeOutInterruptStatus()){ switch(tcNum){ case 0: + * System.out.println("PWMNative: Interrupt case 0"); synchronized(tc0Obj){ + * System.out.println("PWMNative: Interrupt notify 0"); tc0Obj.notify(); } + * break; case 1: System.out.println("PWMNative: Interrupt case 1"); + * synchronized(tc1Obj){ System.out.println("PWMNative: Interrupt notify 1"); + * tc1Obj.notify(); } break; default:; } } } } while ( + * TimerCounter.isGPTCInterruptPending() ); } + */ + public void run() { + // + // System.out.println("PWMNative: run method........"); + // int upTime0 = 150; // 1.5 milli seconds = 0 speed + // int upTime1 = 150; // 1.5 milli seconds = 0 speed + // + // while (true) { + // synchronized (obj) { + // /* + // * System.out.println("PWMNative: Updating Up Times......Left = " + + // * Integer.toString(motorLeftUpTime) + " Right = " + + // * Integer.toString(motorRightUpTime)); + // */ + // upTime0 = motorLeftUpTime; + // upTime1 = motorRightUpTime; + // } + // + // // Timer number 1 + // tc0.setReloadRegisterValue(upTime0); + // outToPortMLeft(PULSE_HIGH); + // tc0.setTimerTriggerRegister(true); + // do { + // } while (tc0.didTimeOutInterruptOccur()); + // outToPortMLeft(PULSE_LOW); + // tc0.setTimerTriggerRegister(false); + // tc0.resetTimeOutInterruptStatus(); + // + // // System.out.println("PWMNative: Big loop long suspend1"); + // try { + // tc0Obj.wait(18, 500000); + // } catch (Exception e) { + // } + // // System.out.println("PWMNative: Big loop long suspend2"); + // + // tc0.setReloadRegisterValue(upTime1); + // outToPortMRight(PULSE_HIGH); + // tc0.setTimerTriggerRegister(true); + // do { + // } while (tc0.didTimeOutInterruptOccur()); + // outToPortMRight(PULSE_LOW); + // tc0.setTimerTriggerRegister(false); + // tc0.resetTimeOutInterruptStatus(); + // + // try { + // tc0Obj.wait(18, 500000); + // } catch (Exception e) { + // } + + /* + * // Timer number 2 tc1.setReloadRegisterValue( upTime1 ); + * tc1.setTimerTriggerRegister( true ); synchronized(tc1Obj){ try{ + * System.out.println("PWMNative: Sleep 3"); tc1Obj.wait(); + * System.out.println("PWMNative: Wake Up 3"); }catch(Exception e){ } } + * tc1.setTimerTriggerRegister( false ); tc1.resetTimeOutInterruptStatus(); + * tc1.setReloadRegisterValue( PULSE_INTERVAL - upTime1 ); //this sets pulse + * interval tc1.setTimerTriggerRegister( true ); synchronized(tc1Obj){ try{ + * System.out.println("PWMNative: Sleep 3"); tc1Obj.wait(); + * System.out.println("PWMNative: Wake Up 3"); }catch(Exception e){ } } + * tc1.setTimerTriggerRegister( false ); tc1.resetTimeOutInterruptStatus(); + */ + // } + } + + public void setManualMode() { + if (DEBUG) + System.out.println("PWMControl: setManualMode... "); + // synchronized (obj) { + if (manualMode == false) { + manualMode = true; + } + // } + } + + public void setAutomatedMode() { + if (DEBUG) + System.out.println("PWMControl: setAutomatedMode... "); + /* + * synchronized(obj){ if(manualMode == true){ + * System.out.println("PWMControl: wake me up... "); obj.notifyAll(); + * manualMode = false; } } + */ + } + + /** + * setSpeedSpin - Set speed for the spin case motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedSpinLeft(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + + if (DEBUG) { + System.out.println("setSpeedSpinLeft: input-> " + Integer.toString(timePosition)); + } + int timePos = timePosition + NATIVE_OFFSET; + int motorUpTime = (int) (timePos * agilityFactor * speedFactor) / 10000; + + if (DEBUG) { + System.out.println("setSpeedSpinLeft: output-> = " + Integer.toString(motorUpTime)); + } + // synchronized (obj) { + /* Factor in the speed and agility factors */ + motorLeftUpTime = motorUpTime; + // } + } + + /** + * setSpeedSpinRight - Set speed for the spin case right motor. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedSpinRight(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* An input of 50 should result in 0 speed. */ + /* 100 should result in full speed forward */ + /* while 0 should result in full speed backwords */ + if (DEBUG) { + System.out.println("setSpeedSpinRight: input-> " + Integer.toString(timePosition)); + } + int timePos = timePosition + NATIVE_OFFSET; + int motorUpTime = (int) ((timePos) * agilityFactor * speedFactor) / 10000; + + if (DEBUG) { + System.out.println("setSpeedSpinRight: output-> = " + Integer.toString(motorUpTime)); + } + // synchronized (obj) { + /* Factor in the speed and agility factors */ + motorRightUpTime = motorUpTime; + // } + } + + /** + * setSpeedTurnM1 - set speed considering agility factor for motor 1 + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedTurnLeft(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + if (DEBUG) { + System.out.println("setSpeedTurnLeft: input-> " + Integer.toString(timePosition)); + } + int timePosLocal = timePosition + NATIVE_OFFSET; + int motorUpTime = + ((timePosLocal * 100 + ((100 - timePosLocal) * (100 - agilityFactor))) * speedFactor) / 10000; + if (DEBUG) { + System.out.println("setSpeedTurnLeft: output-> = " + Integer.toString(motorUpTime)); + } + // synchronized (obj) { + /* Factor in the speed and agility factors */ + motorLeftUpTime = motorUpTime; + // } + } + + /** + * setSpeedTurnM1 - set speed considering agility factor for motor 2 + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedTurnRight(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + if (DEBUG) { + System.out.println("setSpeedTurnRight: input-> " + Integer.toString(timePosition)); + } + int timePos = timePosition + NATIVE_OFFSET; + int motorUpTime = + (((timePos * 100) + ((100 - timePos) * (100 - agilityFactor))) * speedFactor) / 10000; + + if (DEBUG) { + System.out.println("setSpeedTurnRight: output-> " + Integer.toString(motorUpTime)); + } + // synchronized (obj) { + /* Factor in the speed and agility factors */ + motorRightUpTime = motorUpTime; + // } + } + + /** + * setSpeedLeft - speed control for motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedLeft(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + if (DEBUG) { + System.out.println("setSpeedLeft: input-> " + Integer.toString(timePosition)); + } + int timePos = timePosition + NATIVE_OFFSET; + int motorUpTime = (int) ((timePos * 100) * speedFactor) / 10000; + + if (DEBUG) { + System.out.println("setSpeedLeft: output-> " + Integer.toString(motorUpTime)); + } + // synchronized (obj) { + /* Factor in speedFactor */ + motorLeftUpTime = motorUpTime; + // } + } + + /** + * setSpeedRight - speed control for motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedRight(int timePosition) { + if (DEBUG) { + System.out.println("setSpeedRight: input-> " + Integer.toString(timePosition)); + } + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + int timePos = timePosition + NATIVE_OFFSET; + int motorUpTime = (int) (((timePos * 100) * speedFactor) / 10000); + + if (DEBUG) { + System.out.println("setSpeedRight: output-> " + Integer.toString(motorUpTime)); + } + // synchronized (obj) { + /* Factor in speedFactor */ + motorRightUpTime = motorUpTime; + // } + } + + public void setSpeedAgilityFactors(int speed, int agility) { + // synchronized (obj) { + speedFactor = speed; + agilityFactor = agility; + // } + } + + public void setUrgentReverse() { + // synchronized (obj) { + motorLeftUpTime = 1; + motorRightUpTime = 1; + // } + } + + /** + * Control debug messageing. true - Activate debug messages false - deactivate + * debug messages + */ + public void setDebug(boolean debug) { + DEBUG = debug; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMRtsj.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMRtsj.java new file mode 100644 index 00000000..1bb41904 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/PWMRtsj.java @@ -0,0 +1,322 @@ +/* + * PWMRtsj.java + * + * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + */ + +/** + * This class is motor controller specific. In our case the motor controller + * require for 0 speed 1.5 ms out of 10-20ms cycle and a change between 1 to 2 + * seconds for max reverse and forward speed. These values: 1, 1.5, 2 and 20 ms + * are thus this class specific + * + * @author Michael Gesundheit + * @version 1.0 + */ + +public class PWMRtsj extends PWMControl { + + // RelativeTime zeroSpeedDuration; + // RelativeTime timeUpDuration; + // RelativeTime timeDownDuration; + // RelativeTime cycleTime; + boolean updateTime; + int sleepUpDurationNano = 500000; + long sleepUpDurationMilli = 1; + int sleepDownDurationNano = 500000; + long sleepDownDurationMilli = 18; + int pulseWidth; + // Object obj; + + private static final int GPIO_A_OER = 0x09002000; + private static final int GPIO_A_OUT = GPIO_A_OER + 4; + private static final int GPIO_A_IN = GPIO_A_OER + 8; + + /** + * Constructor - Start a standard Java thread. The thread is suspended and + * will wake up evey 18 ms. It will issue a 1-2ms pulse and suspends itself + * again to 18ms. This is to have a total of 20ms or less yet it's the maxim + * possible cycle so as to load the cpu as little as possible. + */ + public PWMRtsj(PWMManager pwmMan, int motor1bit, int motor2bit) { + + super(pwmMan, motor1bit, motor2bit); // This is papa + + motorLeftUpTime = 450000; // Nano seconds + motorRightUpTime = 450000; // Nano seconds + + if (DEBUG) { + System.out.println("PWMRtsj: About to start RoboThread..."); + } + + // t2 = new RoboThread(); + // t2.start(); + // t2.setPriority(10); + } + + // A poor's man ajustimg for the 0 speed which found + // to be 450000 nano seconds. + private int normalizeTime(int timePosition) { + if ((timePosition <= 50) && (timePosition >= 44)) { + return 45; + } + return timePosition; + } + + /** + * setSpeedSpin - Set speed for the spin case motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedSpinLeft(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + if (DEBUG) { + System.out.println("setSpeedSpinLeft: input-> " + Integer.toString(timePosition)); + } + int timePos = normalizeTime(timePosition); + + int motorUpTime = (int) (timePos * agilityFactor * speedFactor); + // System.out.println("Left Motor UpTime1: " + + // Integer.toString(motorUpTime)); + // Since the right motor is hanging in reverse position + // the direction should be opposit + // Bug in wait. Can't take 0 nanoseconds + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + + if (DEBUG) { + System.out.println("setSpeedSpinLeft: output-> = " + Integer.toString(motorUpTime)); + } +// synchronized (this) { + /* Factor in the speed and agility factors */ + motorLeftUpTime = motorUpTime; +// } + } + + /** + * setSpeedSpinRight - Set speed for the spin case right motor. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedSpinRight(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* An input of 50 should result in 0 speed. */ + /* 100 should result in full speed forward */ + /* while 0 should result in full speed backwords */ + if (DEBUG) { + System.out.println("setSpeedSpinRight: input-> " + Integer.toString(timePosition)); + } + int timePos = normalizeTime(timePosition); + + int motorUpTime = (int) ((timePos) * agilityFactor * speedFactor); + // Bug in wait. Cant take 0 nanoseconds + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + + if (DEBUG) { + System.out.println("setSpeedSpinRight: output-> = " + Integer.toString(motorUpTime)); + } +// synchronized (this) { + /* Factor in the speed and agility factors */ + motorRightUpTime = motorUpTime; +// } + } + + /** + * setSpeedTurnM1 - set speed considering agility factor for motor 1 + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedTurnLeft(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + if (DEBUG) { + System.out.println("setSpeedTurnLeft: input-> " + Integer.toString(timePosition)); + } + int timePosLocal = normalizeTime(timePosition); + int motorUpTime = + (timePosLocal * 100 + ((100 - timePosLocal) * (100 - agilityFactor))) * speedFactor; + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + if (DEBUG) { + System.out.println("setSpeedTurnLeft: output-> = " + Integer.toString(motorUpTime)); + } +// synchronized (this) { + /* Factor in the speed and agility factors */ + motorLeftUpTime = motorUpTime; +// } + } + + /** + * setSpeedTurnM1 - set speed considering agility factor for motor 2 + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedTurnRight(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + if (DEBUG) { + System.out.println("setSpeedTurnRight: input-> " + Integer.toString(timePosition)); + } + int timePos = normalizeTime(timePosition); + int motorUpTime = ((timePos * 100) + ((100 - timePos) * (100 - agilityFactor))) * speedFactor; + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + + if (DEBUG) { + System.out.println("setSpeedTurnRight: output-> " + Integer.toString(motorUpTime)); + } +// synchronized (this) { + /* Factor in the speed and agility factors */ + motorRightUpTime = motorUpTime; +// } + } + + /** + * setSpeedLeft - speed control for motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedLeft(int timePosition) { + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + /* 100-input to get reverse polarity for this motor */ + /* since it's mounted in reverse direction to the other motor */ + if (DEBUG) { + System.out.println("setSpeedLeft: input-> " + Integer.toString(timePosition)); + } + int timePos = normalizeTime(timePosition); + int motorUpTime = (int) (timePos * 100) * speedFactor; + /* + * System.out.println("motorUpTime = " + Integer.toString(motorUpTime) + + * " timePos = " + Integer.toString((int)timePos) + " timePosition = " + + * Integer.toString((int)timePosition) + " speedFactor = " + + * Integer.toString(speedFactor)); + */ + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + if (DEBUG) { + System.out.println("setSpeedLeft: output-> " + Integer.toString(motorUpTime)); + } +// synchronized (this) { + /* Factor in speedFactor */ + motorLeftUpTime = motorUpTime; +// } + } + + /** + * setSpeedRight - speed control for motor 1. + * + * @param uptime + * pulse width (time position) + */ + public void setSpeedRight(int timePosition) { + if (DEBUG) { + System.out.println("setSpeedRight: input-> " + Integer.toString(timePosition)); + } + /* Speed comes in as a number between 0 - 100 */ + /* It represents values between 1 to 2 ms */ + int timePos = normalizeTime(timePosition); + int motorUpTime = (int) (timePos * 100) * speedFactor; + if (motorUpTime == 0) { + motorUpTime = 1; + // System.out.println("returning...."); + // return; // ndr + } else if (motorUpTime == 1000000) { + motorUpTime--; + } + if (DEBUG) { + System.out.println("setSpeedRight: output-> " + Integer.toString(motorUpTime)); + } +// synchronized (this) { + /* Factor in speedFactor */ + motorRightUpTime = motorUpTime; +// } + } + + public void setSpeedAgilityFactors(int speed, int agility) { +// synchronized (this) { + speedFactor = speed; + agilityFactor = agility; +// } + } + + public void setUrgentReverse() { +// synchronized (this) { + motorLeftUpTime = 1; + motorRightUpTime = 1; +// } + } + + public void setUrgentStraight() { +// synchronized (this) { + motorLeftUpTime = 99; + motorRightUpTime = 99; +// } + } + + public void justSync() { +// synchronized (this) { + motorLeftUpTime = motorLeftUpTime; + motorRightUpTime = motorRightUpTime; +// } + } + + /** + * Control debug messageing. true - Activate debug messages false - deactivate + * debug messages + */ + public void setDebug(boolean debug) { + DEBUG = debug; + } + +} diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/RCBridge.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/RCBridge.java new file mode 100644 index 00000000..28dfcfcd --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/RCBridge.java @@ -0,0 +1,41 @@ +/* + * PWMControl.java + * + * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + */ + +/** + * Manage the software link between two IO port lines. + *

+ * Copyright: Copyright (c) 2002 + *

+ * + * @author Michael Gesundheit + * @version 1.0 + */ + +public class RCBridge { + + public void setManualMode() { + // rcLleft.activate(); + // rcLright.activate(); + } + + public void setAutomatedMode() { + // rcLleft.deActivate(); + // rcLright.deActivate(); + } +} \ No newline at end of file diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/RobotMain.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/RobotMain.java new file mode 100644 index 00000000..a3d582a8 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/RobotMain.java @@ -0,0 +1,310 @@ +/* + * RobotMain.java + * + * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + */ + +/** + * Robot's Main class - instantiates all required classes and resources. + * + * @author Michael Gesundheit + * @version 1.0 + */ + + +public class RobotMain { + + + private static boolean DEBUG1 = true; + + private static boolean DEBUG = true; + + private static final int OFF_MODE = 1; + private static final int ON_MODE = 2; + private static final int MANUAL_MODE = 1; + private static final int AUTONOMUS_MODE = 2; + private static final int ON_OFF = 128;// 0x80 + private static final int MANUAL_AUTONOMUS = 32; // 0x20 + public static final int LINE_SENSORS = 64; // 0x40 + private static final int SONAR_SENSORS = 96; // 0x60 + public static final int ALL_COMMANDS = 0xe0; + + public static final byte LF_FRONT = 0x1; + public static final byte RT_FRONT = 0x2; + public static final byte RT_SIDE = 0x4; + public static final byte BK_SIDE = 0x8; + public static final byte LF_SIDE = 0x10; + public static final byte ALL_SONARS = 0x1f; + public static final byte LF_RT_FRONT = 0x3; // LF_FRONT | RT_FRONT; + public static final byte LS_LEFT = 0x1; + public static final byte LS_RIGHT = 0x2; + public static final byte LS_REAR = 0x4; + public static final byte LS_LEFT_RIGHT = 0x3; + public static final byte LS_LEFT_REAR = 0x5; + public static final byte LS_RIGHT_REAR = 0x6; + public static final byte LS_ALL = 0x7; + private static final int ALL_DATA = 0x1f; + + + private static PWMManager pwmm; + + private static StrategyMgr strategyMgr; + + + private static int onOffMode = ON_MODE; + + private static int manualAutonomusMode = AUTONOMUS_MODE; + + private static byte lineSensorsMask; + + private static byte sonarSensors; + + private static byte currentCommand; + + private static byte privSonars; + + private static byte privLineSensors; + + private static byte currentSonars; + + public static String pwmSelection; + + /** + * Constructor for the class for the robot main thread. + */ + RobotMain() { + } + + /** + * Processes sonar sensor input. This method is called each time new sonar + * sensor data arrives, and each time that a mode switch occurs between ON/OFF + * and Manual Override. + */ + static void processSonars() { + strategyMgr.processSonars(sonarSensors); + } + + /** + * Processes line sensor input. This method is called each time new line + * sensor data arrives, and each time that a mode switch occurs between ON/OFF + * and Manual Override. + */ + static void processLineSensors() { + strategyMgr.processLineSensors(lineSensorsMask); + resume(); + } + + /** + * Resumes motors as per the last sonar command. + */ + public static void resume() { + processSonars(); + } + + /** + * Extracts sonar sensor data from the adjunct sensor controller and from line + * sensor data from the JStamp line sensor pins. + */ + + private static void processIOCommand() { + + // int data = 0; + // int opCode = 0; + // synchronized (obj1) { + int data = (int) (currentCommand & ALL_DATA); + int opCode = (int) (currentCommand & 0xe0); // ALL_COMMANDS); + // } + + if (DEBUG) { + System.out.println("processIOCommand: Default: opCode = " + Integer.toString((int) opCode) + + " data = " + Integer.toString((int) data)); + } + switch ((int) opCode) { + case ON_OFF: + if (DEBUG) { + System.out.println("processIO: ON_OFF...."); + } + if ((data & 0x1) == 0x1) { + System.out.println("processIO: ON MODE........"); + onOffMode = ON_MODE; + processLineSensors(); + processSonars(); + } else { + System.out.println("processIO: OFF MODE......."); + onOffMode = OFF_MODE; + strategyMgr.stop(); + } + break; + case MANUAL_AUTONOMUS: + if (DEBUG) { + System.out.println("processIO: Setting manual_autonomus mode"); + } + if ((data & 0x1) == 0x1) { + manualAutonomusMode = MANUAL_MODE; + pwmm.setManualMode(); + } else { + manualAutonomusMode = AUTONOMUS_MODE; + pwmm.setAutomatedMode(); + processLineSensors(); + processSonars(); + } + break; + case LINE_SENSORS: + if (DEBUG) { + // System.out.println("processIO: Line Sensors.................." + // + Integer.toBinaryString((int) (data & LS_ALL))); + } + lineSensorsMask = (byte) (data & LS_ALL); + + /* + * Line sensors with '0' data means the robot moved off the edge line, and + * there is nothing to do. + */ + if (((data & LS_ALL) == 0) || ((data & LS_ALL) == privLineSensors)) { + privLineSensors = (byte) (data & LS_ALL); + return; + } + if ((onOffMode == ON_MODE) && (manualAutonomusMode == AUTONOMUS_MODE)) { + if (DEBUG) + System.out.println("processIO: Line Sensors..Process...!!!"); + processLineSensors(); + } + break; + case SONAR_SENSORS: + if (DEBUG) { + // System.out.println("processIO: SONAR_SENORS: bits = ......" + // + Integer.toBinaryString((int) (currentCommand & ALL_SONARS))); + } + currentSonars = (byte) (data & ALL_SONARS); + /* + * No need to synchronized on this variable assignment since all referring + * code is on the same logical thread + */ + sonarSensors = (byte) currentSonars; + if ((onOffMode == ON_MODE) && (manualAutonomusMode == AUTONOMUS_MODE)) { + processSonars(); + } + break; + default: + strategyMgr.stop(); + System.out.println("processIOCommand: Default: opCode = " + Integer.toString((int) opCode) + + " data = " + Integer.toString((int) data)); + } + } + + /** + * Sets the simulation mode on in the IOManager. + */ + // static public void setSimulation() { + // sm.setSimulation(); + // } + + /** + * Resets the simulation mode in the IOManager. + */ + // static public void resetSimulation() { + // sm.resetSimulation(); + // } + + /** + * Saves the current IOManager command byte locally. + */ + static public void setIOManagerCommand(byte[] cmd) { + // synchronized (obj1) { + currentCommand = cmd[0]; + // } + // synchronized (obj) { + try { + // obj.notify(); + } catch (IllegalMonitorStateException ie) { + System.out.println("IllegalMonitorStateException caught trying to notify"); + } + // } + } + + /** + * Sets debug messaging state: true - Activate debug messages false - + * deactivate debug messages + */ + static public void setDebug(boolean debug) { + DEBUG = debug; + } + + /** + * Runs the robot's main thread. + */ + + public static void main( String args[]) { + + TestSensorInput.init(); + boolean active = true; + /** + * RealTime management of the robot behaviour based on sensors and commands + * input. + */ + + /** + * Set the low level PWM interface type, e.g. "rtsj" or "native" (ajile + * library-based). + */ + pwmSelection = "rtsj"; + + System.out.println("PWM Selction is: " + pwmSelection); + + pwmm = new PWMManager(pwmSelection); + + // Pass in the PWMManager for callbacks. + // sm = new IOManager(pwmm); + // ram = new RemoteApplicationManager(pwmm); + MotorControl mc = new MotorControl(100, 100); + strategyMgr = new StrategyMgr(mc); + + /* + * Sets initial values for the speed and agility parameters. Speed and + * agility are arbitrary scale factors for the overall speed and speed of + * turns, respectively. These work with PWM via the native ajile libraries, + * but do not work well with the RTSJ implementation due to limits on the + * granularity of timing for the PWM pulse (quantization). + */ + pwmm.setSpeedAgilityFactors(100, 100); + + /* + * Robot's initial state is "ON" by default. Set this parameter to "OFF" if + * the robot is to be started over the TCP/IP link. + */ + // issueCommand("OFF"); + + SSJAVA: while (active) { + + // if (DEBUG) { + // System.out.println("Main: Waiting for remote commands"); + // } + // try { + // obj.wait(); + // } catch (IllegalMonitorStateException ie) { + // System.out.println("IllegalMonitorStateException caught in main loop"); + // } + currentCommand = TestSensorInput.getCommand(); + if (currentCommand == -1) { + break; + } + System.out.println("currentCommand=" + currentCommand); + processIOCommand(); + } + System.exit(0); + } + +} diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/StrategyMgr.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/StrategyMgr.java new file mode 100644 index 00000000..c46b5fdf --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/StrategyMgr.java @@ -0,0 +1,270 @@ +/* + * StragegyMgr.java + * + * Copyright (c) 1993-2002 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF + * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. + */ + +/** + * StrategyMgr - an isolation class ment for programmers to modify and create + * thier own code for robot strategy. + * + * @author Michael Gesundheit + * @version 1.0 + */ + + +public class StrategyMgr { + + + private MotorControl mc; + private static final int zeroSpeed = 45; + + private Random rand; + + private boolean DEBUGL = true; + // private boolean DEBUGL = false; + + // private boolean DEBUG = true; + + private boolean DEBUG = true; + + /** + * Constructor - Invoke communication to remote application thread + */ + public StrategyMgr(@DELEGATE MotorControl motorControl) { + mc = motorControl; + rand = new Random(); + } + + void processSonars( byte sonarSensors) { + + // 5 sensors. 1,2 are fromnt left and right. + // Sensor 3 is right side, 4 back and 5 is left side. + if ((sonarSensors & 0x1f) == 0) { + // No sensor data may mean dead area between sensors. + // Continue with the last gesture until any sensor + // provide data. + if (DEBUG) { + System.out.println("sonar: NO SONARS!!!!!!!!"); + } + } else if ((sonarSensors & (RobotMain.LF_RT_FRONT)) == (RobotMain.LF_RT_FRONT)) { + if (DEBUG) { + System.out.println("sonar: straight"); + } + straight(); + } else if ((sonarSensors & RobotMain.LF_FRONT) == RobotMain.LF_FRONT) { + if (DEBUG) { + System.out.println("sonar: bearLeft"); + } + bearLeft(); + } else if ((sonarSensors & RobotMain.RT_FRONT) == RobotMain.RT_FRONT) { + if (DEBUG) { + System.out.println("sonar: bearRight"); + } + bearRight(); + } else if ((sonarSensors & RobotMain.RT_SIDE) == RobotMain.RT_SIDE) { + if (DEBUG) { + System.out.println("sonar: right"); + } + spinRight(); + } else if ((sonarSensors & RobotMain.LF_SIDE) == RobotMain.LF_SIDE) { + if (DEBUG) { + System.out.println("sonar: left"); + } + spinLeft(); + } else if ((sonarSensors & RobotMain.BK_SIDE) == RobotMain.BK_SIDE) { + if (DEBUG) { + System.out.println("sonar: spin180"); + } + spin180(); + } + } + + void processLineSensors( byte lineSensorsMask) { + + int count = 0; + + while ((lineSensorsMask & RobotMain.LS_ALL) != 0) { + + if (count > 100) { + // if the robot fail to get out of weird condition wihtin 100 steps, + // terminate while loop for stabilizing behavior. + stop(); + break; + } + count++; + + if ((lineSensorsMask & RobotMain.LS_ALL) == RobotMain.LS_ALL) { + if (DEBUGL) + System.out.println("Line Sensors - ALL"); + stop(); + } else if ((lineSensorsMask & RobotMain.LS_LEFT_RIGHT) == RobotMain.LS_LEFT_RIGHT) { + if (DEBUGL) + System.out.println("Line Sensors - Left & Right"); + back(); + try { + // Thread.sleep(1000); + } catch (InterruptedException ie) { + } + spin180(); + try { + // Thread.sleep(1000); + } catch (InterruptedException ie) { + } + } else if ((lineSensorsMask & RobotMain.LS_LEFT_REAR) == RobotMain.LS_LEFT_REAR) { + if (DEBUGL) + System.out.println("Line Sensors - Left & Rear"); + bearRight(); + try { + // Thread.sleep(1000); + } catch (InterruptedException ie) { + } + } else if ((lineSensorsMask & RobotMain.LS_RIGHT_REAR) == RobotMain.LS_RIGHT_REAR) { + if (DEBUGL) + System.out.println("Line Sensors - Right & Rear"); + bearLeft(); + try { + // Thread.sleep(1000); + } catch (InterruptedException ie) { + } + } else if ((lineSensorsMask & RobotMain.LS_LEFT) == RobotMain.LS_LEFT) { + if (DEBUGL) + System.out.println("Line Sensors - Left"); + backBearRight(); + + try { + // Thread.sleep(1000); + } catch (InterruptedException ie) { + } + } else if ((lineSensorsMask & RobotMain.LS_RIGHT) == RobotMain.LS_RIGHT) { + if (DEBUGL) + System.out.println("Line Sensors - Right"); + backBearLeft(); + try { + // Thread.sleep(1000); + } catch (InterruptedException ie) { + } + } else if ((lineSensorsMask & RobotMain.LS_REAR) == RobotMain.LS_REAR) { + if (DEBUGL) + System.out.println("Line Sensors - Rear"); + straight(); + try { + // Thread.sleep(1000); + } catch (InterruptedException ie) { + } + } + lineSensorsMask = TestSensorInput.getCommand(); + }// while loop + } + + public void stop() { + if (DEBUG) + System.out.println("StrageyMgr: stop...."); + mc.setSpeedLeft(zeroSpeed); + mc.setSpeedRight(zeroSpeed); + } + + public void search() { + if (DEBUG) + System.out.println("StrategyMgr: search...."); + mc.setSpeedLeft(70); + mc.setSpeedRight(50); + } + + public void straight() { + if (DEBUG) + System.out.println("StrategyMgr: strait...."); + mc.setSpeedLeft(100); + mc.setSpeedRight(100); + } + + public void spinRight() { + if (DEBUG) + System.out.println("StrategyMgr: spinRight...."); + mc.setSpeedSpinLeft(100); + mc.setSpeedSpinRight(0); + } + + public void spinLeft() { + if (DEBUG) + System.out.println("StrategyMgr: spinLeft...."); + mc.setSpeedSpinLeft(0); + mc.setSpeedSpinRight(100); + } + + public void spin180() { + int mod = (rand.nextInt() % 2); + + if (DEBUG) + System.out.println("StrategyMgr: spin180...."); + if (mod == 1) { + mc.setSpeedSpinLeft(0); + mc.setSpeedSpinRight(100); + } else { + mc.setSpeedSpinLeft(100); + mc.setSpeedSpinRight(0); + } + } + + public void right() { + if (DEBUG) + System.out.println("StrategyMgr: right...."); + mc.setSpeedTurnLeft(100); + mc.setSpeedRight(zeroSpeed); + } + + public void left() { + if (DEBUG) + System.out.println("StrategyMgr: left...."); + mc.setSpeedLeft(zeroSpeed); + mc.setSpeedTurnRight(100); + } + + public void bearRight() { + if (DEBUG) + System.out.println("StrategyMgr: bearRight...."); + mc.setSpeedTurnLeft(100); + mc.setSpeedTurnRight(60); + } + + public void bearLeft() { + if (DEBUG) + System.out.println("StrategyMgr: bearLeft...."); + mc.setSpeedTurnLeft(60); + mc.setSpeedTurnRight(100); + } + + public void back() { + if (DEBUG) + System.out.println("StrategyMgr: back...."); + mc.setSpeedLeft(0); + mc.setSpeedRight(0); + } + + public void backBearLeft() { + if (DEBUG) + System.out.println("StrategyMgr: backBearLeft...."); + mc.setSpeedLeft(30); + mc.setSpeedRight(0); + } + + public void backBearRight() { + if (DEBUG) + System.out.println("StrategyMgr: backBearRight...."); + mc.setSpeedLeft(0); + mc.setSpeedRight(30); + } +} diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/TestSensorInput.java b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/TestSensorInput.java new file mode 100644 index 00000000..a6b3d378 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/TestSensorInput.java @@ -0,0 +1,26 @@ +public class TestSensorInput { + + private static FileInputStream inputFile; + private static int idx=0; + + @TRUST + public static void init() { + inputFile = new FileInputStream("input.dat"); + } + + @TRUST + public static byte getCommand() { + String in = inputFile.readLine(); + if (in == null) { + return (byte) -1; + } + // DEBUG_OUTPUT(); + return (byte) Integer.parseInt(in); + } + + public static DEBUG_OUTPUT(){ + idx++; + System.out.println(idx); + } + +} diff --git a/Robust/src/Benchmarks/SSJava/JavaNatorInfer/makefile b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/makefile new file mode 100644 index 00000000..99d81779 --- /dev/null +++ b/Robust/src/Benchmarks/SSJava/JavaNatorInfer/makefile @@ -0,0 +1,52 @@ +BUILDSCRIPT=../../../buildscript + +PROGRAM=RobotMain +SOURCE_FILES=RobotMain.java + +ifndef INV_ERROR_PROB +INV_ERROR_PROB=1000 +endif + +ifndef RANDOMSEED +RANDOMSEED=12345 +endif + +SSJAVA= -ssjava -ssjavainfer -ssjavadebug +BSFLAGS= -32bit -mainclass $(PROGRAM) -heapsize-mb 1350 -nooptimize -debug -garbagestats #-printlinenum #-joptimize +NORMAL= -ssjava-inject-error 0 0 +INJECT_ERROR= -ssjava-inject-error $(INV_ERROR_PROB) $(RANDOMSEED) + + +default: $(PROGRAM)s.bin + +normal: $(PROGRAM)n.bin + +error: $(PROGRAM)e.bin + + +$(PROGRAM)s.bin: $(SOURCE_FILES) makefile + $(BUILDSCRIPT) $(SSJAVA) $(BSFLAGS) -o $(PROGRAM)s -builddir ssj $(SOURCE_FILES) + +$(PROGRAM)n.bin: $(SOURCE_FILES) makefile + $(BUILDSCRIPT) $(NORMAL) $(BSFLAGS) -o $(PROGRAM)n -builddir norm $(SOURCE_FILES) + +$(PROGRAM)e.bin: $(SOURCE_FILES) makefile + $(BUILDSCRIPT) $(SSJAVA) $(INJECT_ERROR) $(BSFLAGS) -o $(PROGRAM)e -builddir injerr $(SOURCE_FILES) + +cleanerror: + rm -f $(PROGRAM)e.bin + rm -rf injerr + +clean: + rm -f $(PROGRAM)s.bin $(PROGRAM)n.bin $(PROGRAM)e.bin + rm -fr ssj norm injerr + rm -f nve-diff.tmp nve-diff-ranges.tmp + rm -f *~ + rm -f *.dot + rm -f *.png + rm -f *.pdf + rm -f aliases.txt + rm -f results*txt + rm -f output.txt + rm -f *log + -- 2.34.1