diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..b385d69 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +poc-java \ No newline at end of file diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..74f8e78 --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,542 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
\ No newline at end of file diff --git a/.idea/libraries/Gradle___IOBoard_0_1_.xml b/.idea/libraries/Gradle___IOBoard_0_1_.xml new file mode 100644 index 0000000..6d943ff --- /dev/null +++ b/.idea/libraries/Gradle___IOBoard_0_1_.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/poc-java_main.iml b/.idea/modules/poc-java_main.iml index 3dfc202..09dd5d9 100644 --- a/.idea/modules/poc-java_main.iml +++ b/.idea/modules/poc-java_main.iml @@ -1,6 +1,6 @@ - + @@ -13,6 +13,7 @@ + diff --git a/.idea/modules/poc-java_test.iml b/.idea/modules/poc-java_test.iml index 04909b9..c619f46 100644 --- a/.idea/modules/poc-java_test.iml +++ b/.idea/modules/poc-java_test.iml @@ -1,6 +1,6 @@ - + @@ -14,6 +14,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index dc0ff23..845f9ef 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,14 @@ - + + + + + + + + @@ -17,6 +24,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -108,8 +135,41 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -118,128 +178,155 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -270,73 +357,6 @@ - - - - - - - - - - - - Displays the components produced by root project 'poc-java'. [incubating]<p><i>Task group: help<i> - Assembles and tests this project and all projects that depend on it.<p><i>Task group: build<i> - Displays the sub-projects of root project 'poc-java'.<p><i>Task group: help<i> - Generates IDEA project files (IML, IPR, IWS)<p><i>Task group: IDE<i> - Assembles main classes.<p><i>Task group: build<i> - <i>Task group: other<i> - Displays all buildscript dependencies declared in root project 'poc-java'.<p><i>Task group: help<i> - Generates Gradle wrapper files. [incubating]<p><i>Task group: Build Setup<i> - <i>Task group: other<i> - Assembles test classes.<p><i>Task group: build<i> - Generates Javadoc API documentation for the main source code.<p><i>Task group: documentation<i> - <i>Task group: other<i> - Assembles a jar archive containing the main classes.<p><i>Task group: build<i> - Displays the configuration model of root project 'poc-java'. [incubating]<p><i>Task group: help<i> - Generates IDEA module files (IML)<p><i>Task group: other<i> - Processes main resources.<p><i>Task group: other<i> - Displays the tasks runnable from root project 'poc-java'.<p><i>Task group: help<i> - Initializes a new Gradle build. [incubating]<p><i>Task group: Build Setup<i> - Cleans IDEA project files (IML, IPR)<p><i>Task group: IDE<i> - Generates an IDEA workspace file (IWS)<p><i>Task group: other<i> - Runs the unit tests.<p><i>Task group: verification<i> - Compiles main Java source.<p><i>Task group: other<i> - Displays the insight into a specific dependency in root project 'poc-java'.<p><i>Task group: help<i> - Runs all checks.<p><i>Task group: verification<i> - Assembles the outputs of this project.<p><i>Task group: build<i> - Deletes the build directory.<p><i>Task group: build<i> - Compiles test Java source.<p><i>Task group: other<i> - <i>Task group: other<i> - Displays all dependencies declared in root project 'poc-java'.<p><i>Task group: help<i> - Processes test resources.<p><i>Task group: other<i> - Displays a help message.<p><i>Task group: help<i> - <i>Task group: other<i> - Assembles and tests this project.<p><i>Task group: build<i> - Installs the 'archives' artifacts into the local Maven repository.<p><i>Task group: other<i> - Assembles and tests this project and all projects it depends on.<p><i>Task group: build<i> - <i>Task group: other<i> - <i>Task group: other<i> - Generates IDEA project file (IPR)<p><i>Task group: other<i> - Displays the properties of root project 'poc-java'.<p><i>Task group: help<i> - <i>Task group: other<i> - Configuration for archive artifacts. - Dependencies for source set 'main'. - Compile classpath for source set 'main'. - Compile dependencies for source set 'main'. - Configuration for default artifacts. - Runtime dependencies for source set 'main'. - Dependencies for source set 'test'. - Compile classpath for source set 'test'. - Compile dependencies for source set 'test'. - Runtime dependencies for source set 'test'. - - - - - - @@ -737,18 +770,21 @@ @@ -782,13 +818,24 @@ - + + - + + + + + + @@ -798,7 +845,7 @@ - @@ -812,7 +859,7 @@ - @@ -820,25 +867,44 @@ - + + + + + + - + + + + - - + + - @@ -1064,6 +1130,26 @@ + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1281,7 +1286,124 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1289,9 +1411,7 @@ - - - + @@ -1313,7 +1433,6 @@ - @@ -1321,7 +1440,6 @@ - @@ -1332,13 +1450,7 @@ - - - - - - - + @@ -1347,13 +1459,7 @@ - - - - - - - + @@ -1361,29 +1467,11 @@ - - - - - - - - - - - - - - + + - - - - - - - + @@ -1402,13 +1490,6 @@ - - - - - - - @@ -1460,13 +1541,6 @@ - - - - - - - @@ -1478,15 +1552,7 @@ - - - - - - - - - + @@ -1494,9 +1560,7 @@ - - - + @@ -1504,51 +1568,82 @@ + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - + + @@ -1556,70 +1651,195 @@ + + + + + + + - + - - + + - + - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + @@ -1686,6 +1906,18 @@ + + + 1.8 + + + + + Maven: com.googlecode.json-simple:json-simple:1.1.1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9b77ceb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM resin/rpi-raspbian:jessie +# FROM ubuntu + +# Install dependencies +RUN apt-get update && apt-get install -y \ + git-core \ + build-essential \ + gcc \ + oracle-java8-jdk\ + curl\ + wget\ + --no-install-recommends && \ + rm -rf /var/lib/apt/lists/* + +RUN git clone git://git.drogon.net/wiringPi +RUN cd wiringPi && ./build +RUN curl -s get.pi4j.com | bash +COPY build/libs/poc-java-all-1.0-SNAPSHOT.jar /root +COPY rpi_startup.sh /root + +# Define working directory +WORKDIR /data +VOLUME /data + +CMD ["/root/rpi_startup.sh"] \ No newline at end of file diff --git a/build.gradle b/build.gradle index 389df61..c09159e 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,10 @@ targetCompatibility = 1.8 repositories { maven { url "http://repo.maven.apache.org/maven2" } + flatDir { + dirs 'libs' + } + } dependencies { @@ -20,6 +24,7 @@ dependencies { compile group: 'com.googlecode.json-simple', name: 'json-simple', version:'1.1.1' compile group: 'org.mongodb', name: 'mongodb-driver', version:'3.4.2' compile group: 'de.vandermeer', name: 'asciitable', version:'0.2.5' + compile name: 'IOBoard-0.1' testCompile group: 'junit', name: 'junit', version:'3.8.1' } diff --git a/libs/IOBoard-0.1.jar b/libs/IOBoard-0.1.jar new file mode 100644 index 0000000..a9654d2 Binary files /dev/null and b/libs/IOBoard-0.1.jar differ diff --git a/libs/IOBoard-RPi-0.1.jar b/libs/IOBoard-RPi-0.1.jar new file mode 100644 index 0000000..e6aa6f8 Binary files /dev/null and b/libs/IOBoard-RPi-0.1.jar differ diff --git a/rpi_startup.sh b/rpi_startup.sh new file mode 100644 index 0000000..68504ee --- /dev/null +++ b/rpi_startup.sh @@ -0,0 +1 @@ +java -classpath .:classes:/opt/pi4j/lib/'*':/root/poc-java-all-1.0-SNAPSHOT.jar com.henrypump.poc.POC localhost \ No newline at end of file diff --git a/src/main/java/com/henrypump/poc/AnalogIn.java b/src/main/java/com/henrypump/poc/AnalogIn.java index 3536937..e94c8d4 100644 --- a/src/main/java/com/henrypump/poc/AnalogIn.java +++ b/src/main/java/com/henrypump/poc/AnalogIn.java @@ -10,6 +10,7 @@ import mraa.mraa; import mraa.Platform; import mraa.Result; + import static java.lang.Thread.sleep; @@ -23,6 +24,7 @@ public class AnalogIn { private double euMax; private double[] history = new double[100]; + AnalogIn(int channel, double rawMin, double rawMax, double euMin, double euMax){ this.channel = channel; if (channel != 99) { diff --git a/src/main/java/com/henrypump/poc/IOControl.java b/src/main/java/com/henrypump/poc/IOControl.java index 16200d1..48f6ae8 100644 --- a/src/main/java/com/henrypump/poc/IOControl.java +++ b/src/main/java/com/henrypump/poc/IOControl.java @@ -1,5 +1,9 @@ package com.henrypump.poc; +import com.henrypump.io.DigitalIn; +import com.henrypump.io.DigitalOut; +import com.henrypump.io.MuxSetup; + /** * Created by patrickjmcd on 2/10/17. */ @@ -15,14 +19,14 @@ public class IOControl implements Runnable { IOControl(POC poc){ this.poc = poc; // IO - startBtn = new DigitalIn(99); - stopBtn = new DigitalIn(99); - led2 = new DigitalOut(2, 0); - led3 = new DigitalOut(3, 0); - led4 = new DigitalOut(4, 0); - led5 = new DigitalOut(5, 0); - runningIndicator = new DigitalOut(6,0); - runCommand = new DigitalOut(7,0); + startBtn = new DigitalIn(1); + stopBtn = new DigitalIn(2); + led2 = new DigitalOut(3); + led3 = new DigitalOut(4); + led4 = new DigitalOut(5); + led5 = new DigitalOut(6); + runningIndicator = new DigitalOut(2); + runCommand = new DigitalOut(1); } private void startWell(){ @@ -40,20 +44,20 @@ public class IOControl implements Runnable { } public void allOutputsOff(){ - led2.write(0,true); - led3.write(0,true); - led4.write(0,true); - led5.write(0,true); - runningIndicator.write(0, true); - runCommand.write(0,true); + led2.write(0); + led3.write(0); + led4.write(0); + led5.write(0); + runningIndicator.write(0); + runCommand.write(0); } public void run() { System.out.println("Running the IOControl Loop"); for (;;) { - if (startBtn.read() == 1) startWell(); - if (stopBtn.read() == 1) stopWell(); - if (startBtn.read() == 1 && stopBtn.read() == 1) { + if (startBtn.read(poc.thisWell.ioMux) == 1) startWell(); + if (stopBtn.read(poc.thisWell.ioMux) == 1) stopWell(); + if (startBtn.read(poc.thisWell.ioMux) == 1 && stopBtn.read(poc.thisWell.ioMux) == 1) { exitPOC(); } diff --git a/src/main/java/com/henrypump/poc/POC.java b/src/main/java/com/henrypump/poc/POC.java index 55cb7b5..216040b 100644 --- a/src/main/java/com/henrypump/poc/POC.java +++ b/src/main/java/com/henrypump/poc/POC.java @@ -15,16 +15,16 @@ public class POC implements Runnable{ POC(String dbHostname){ ioEnabled = true; - thisWell = new Well(dbHostname, 0, 1, 7); + thisWell = new Well(dbHostname, 1, 2); thisWell.getWellSetup(); } POC(String simFileName, boolean ioEnabled, String dbHostname){ this.ioEnabled = ioEnabled; if (this.ioEnabled) { - thisWell = new Well(dbHostname, simFileName,0, 1, 7); + thisWell = new Well(dbHostname, simFileName,1, 2); } else { - thisWell = new Well(dbHostname, simFileName,99, 99, 99); + thisWell = new Well(dbHostname, simFileName,99, 99); } thisWell.getWellSetup(); } diff --git a/src/main/java/com/henrypump/poc/Well.java b/src/main/java/com/henrypump/poc/Well.java index 5bab135..4541930 100644 --- a/src/main/java/com/henrypump/poc/Well.java +++ b/src/main/java/com/henrypump/poc/Well.java @@ -1,6 +1,8 @@ package com.henrypump.poc; +import com.henrypump.io.AnalogIn; +import com.henrypump.io.MuxSetup; import de.vandermeer.asciitable.v2.RenderedTable; import de.vandermeer.asciitable.v2.V2_AsciiTable; import de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer; @@ -16,12 +18,16 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.nio.file.*; +import java.time.Duration; +import java.time.Instant; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import static java.lang.Math.*; + + /** * Created by patrickjmcd on 1/31/17. */ @@ -34,6 +40,7 @@ public class Well { /* IO */ private AnalogIn inclinometer; private AnalogIn loadCell; + MuxSetup ioMux = new MuxSetup(); private double currentSurfacePosition; private double currentSurfaceLoad; private double currentDownholePosition; @@ -201,20 +208,20 @@ public class Well { private long minutesSinceTimerStart_last = 0; - Well(String dbHostname, int inclinometerChannel, int loadCellChannel, int runCommandChannel){ + Well(String dbHostname, int inclinometerChannel, int loadCellChannel){ this.wellName = wellName; db = new Database(dbHostname); strokesLifetime = db.getLastStrokeNum() + 1; currentCard = new Card(strokesLifetime); - inclinometer = new AnalogIn(inclinometerChannel, 0, 1020, 0, 145); - loadCell = new AnalogIn(loadCellChannel, 0, 1020, 0, 50000); + inclinometer = new AnalogIn(inclinometerChannel, 0, 65535, 0, 145); + loadCell = new AnalogIn(loadCellChannel, 0, 65535, 0, 50000); initializeMeasurements(); initializeSetpoints(); db.newRunStatus("Boot", "io"); } - Well(String dbHostname, String simFileName, int inclinometerChannel, int loadCellChannel, int runCommandChannel){ + Well(String dbHostname, String simFileName, int inclinometerChannel, int loadCellChannel){ this.wellName = wellName; sim = new Simulation(simFileName); db = new Database(dbHostname); @@ -228,6 +235,32 @@ public class Well { db.newRunStatus("Boot", "commandline"); } + private int[] calibrateInclinometer(){ + System.out.println("Calibrating inclinometer..."); + Instant startTime = Instant.now(); + int maxRaw = Integer.MIN_VALUE; + int minRaw = Integer.MAX_VALUE; + while (Duration.between(startTime, Instant.now()).toMillis() < 30000){ + inclinometer.read(ioMux); + if (inclinometer.rawValue > maxRaw){ + maxRaw = inclinometer.rawValue; + } + + if (inclinometer.rawValue < minRaw){ + minRaw = inclinometer.rawValue; + } + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + int minMax[] = new int[]{minRaw, maxRaw}; + System.out.println("Inclinometer Min: " + minRaw + ", Inclinometer Max: " + maxRaw); + return minMax; + }; + private void initializeSetpoints(){ /** * intializeSetpoints @@ -833,6 +866,8 @@ public class Well { strokesSinceStart = 0; pumpStartTime = ZonedDateTime.now(); db.newRunStatus(getRunStatusString(), initiator); + int[] inclMinMax = calibrateInclinometer(); + inclinometer = new AnalogIn(inclinometer.channel, inclMinMax[0], inclMinMax[1], inclinometer.euMin, inclinometer.euMax); } } @@ -1492,8 +1527,8 @@ public class Well { public void eval(){ checkSafeties(); - currentSurfacePosition = inclinometer.readScaled(); - currentSurfaceLoad = loadCell.readScaled(); + currentSurfacePosition = inclinometer.read(ioMux); + currentSurfaceLoad = loadCell.read(ioMux); LPStatus lastPoint = calc(currentSurfacePosition, currentSurfaceLoad); if (lastPoint.getStatus() == GOOD_STATUS){ currentDownholePosition = lastPoint.getPosition(); @@ -1556,8 +1591,8 @@ public class Well { public void eval(int simPoint){ checkSafeties(); - currentSurfacePosition = inclinometer.readScaledSim(sim.getPositionAtIndex(simPoint)); - currentSurfaceLoad = loadCell.readScaledSim(sim.getLoadAtIndex(simPoint)); + currentSurfacePosition = inclinometer.readSim(sim.getPositionAtIndex(simPoint)); + currentSurfaceLoad = loadCell.readSim(sim.getLoadAtIndex(simPoint)); LPStatus lastPoint = calc(currentSurfacePosition, currentSurfaceLoad); if (lastPoint.getStatus() == GOOD_STATUS){ @@ -1650,7 +1685,7 @@ public class Well { public static void main( String[] args ){ - Well thisWell = new Well(args[1], 99, 99, 99); + Well thisWell = new Well(args[1], 99, 99); thisWell.getWellSetup(); thisWell.printTapers(); }