Combines Raspi and Non-Raspi code
This commit is contained in:
157
Non-RaspberryPi/src/main/java/com/henrypump/io/AnalogIn.java
Normal file
157
Non-RaspberryPi/src/main/java/com/henrypump/io/AnalogIn.java
Normal file
@@ -0,0 +1,157 @@
|
||||
package com.henrypump.io;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by patrickjmcd on 6/14/17.
|
||||
*/
|
||||
public class AnalogIn {
|
||||
|
||||
private int channel;
|
||||
private int rawValue;
|
||||
private double lastValue;
|
||||
private Instant lastStored = Instant.EPOCH;
|
||||
private double rawMax, rawMin, euMax, euMin;
|
||||
private List<Integer> channelMux;
|
||||
private double m, b;
|
||||
private double[] history = new double[100];
|
||||
private long badReads = 0;
|
||||
|
||||
public AnalogIn(int channel, double rawMin, double rawMax, double euMin, double euMax) {
|
||||
this.channel = channel;
|
||||
if (channel != 99) {
|
||||
this.channelMux = MuxSetup.muxValues.get(channel);
|
||||
}
|
||||
this.rawMax = rawMax;
|
||||
this.rawMin = rawMin;
|
||||
this.euMax = euMax;
|
||||
this.euMin = euMin;
|
||||
|
||||
m = (euMax - euMin) / (rawMax - rawMin);
|
||||
b = euMax - m * (rawMax);
|
||||
}
|
||||
|
||||
public int getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public double getRawMax() {
|
||||
return rawMax;
|
||||
}
|
||||
|
||||
public double getRawMin() {
|
||||
return rawMin;
|
||||
}
|
||||
|
||||
public double getEuMax() {
|
||||
return euMax;
|
||||
}
|
||||
|
||||
public double getEuMin() {
|
||||
return euMin;
|
||||
}
|
||||
|
||||
public long getBadReads() {
|
||||
return badReads;
|
||||
}
|
||||
|
||||
public int getRawValue() {
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
public double getLastValue() {
|
||||
return lastValue;
|
||||
}
|
||||
|
||||
public Instant getLastStored() {
|
||||
return lastStored;
|
||||
}
|
||||
|
||||
public double getHistory(int pointIndex) {
|
||||
return history[pointIndex];
|
||||
}
|
||||
|
||||
double setValue(int value) {
|
||||
// System.out.println(value);
|
||||
this.rawValue = value;
|
||||
double pv = m * rawValue + b;
|
||||
lastValue = pv;
|
||||
lastStored = Instant.now();
|
||||
System.arraycopy(history, 0, history,1, history.length - 1);
|
||||
history[0] = lastValue;
|
||||
// System.out.println(value + " --> " + pv);
|
||||
return pv;
|
||||
}
|
||||
|
||||
public double read(MuxSetup mux){
|
||||
mux.set(channelMux.get(0), channelMux.get(1), channelMux.get(2));
|
||||
int rawIn = mux.readAnalog();
|
||||
if (rawIn != -1){
|
||||
badReads = 0;
|
||||
return setValue(rawIn);
|
||||
} else {
|
||||
badReads++;
|
||||
try {
|
||||
Thread.sleep(5);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return lastValue;
|
||||
}
|
||||
|
||||
public double readSim(double simRaw){
|
||||
double pv = ((euMax - euMin)/(rawMax - rawMin)) * simRaw + (euMax - ((euMax - euMin)/(rawMax - rawMin)) * rawMax);
|
||||
lastValue = pv;
|
||||
System.arraycopy(history, 0, history,1, history.length - 1);
|
||||
history[0] = lastValue;
|
||||
return pv;
|
||||
}
|
||||
|
||||
public HashMap<String, Long> getStatus(){
|
||||
HashMap<String, Long> status;
|
||||
status = new HashMap<String, Long>();
|
||||
|
||||
status.put("badReads", badReads);
|
||||
status.put("measurementAge", Duration.between(lastStored, Instant.now()).toMillis());
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
MuxSetup mux = null;
|
||||
Instant now = Instant.now();
|
||||
mux = new MuxSetup();
|
||||
HashMap<String, Long> aI1status;
|
||||
// HashMap<String, Long> aI2status;
|
||||
|
||||
System.out.println("Testing Analog Inputs");
|
||||
|
||||
AnalogIn aI1 = new AnalogIn(1, 32560, 65535, 0, 100);
|
||||
// AnalogIn aI2 = new AnalogIn(2, 0, 65535, 0, 50000);
|
||||
// AnalogIn aI3 = new AnalogIn(3, 0, 65535, 0, 100);
|
||||
// AnalogIn aI4 = new AnalogIn(4, 0, 65535, 0, 100);
|
||||
|
||||
for (int i = 0; i < 500; i++) {
|
||||
|
||||
double a1Val = aI1.read(mux);
|
||||
// double a2Val = aI2.read(mux);
|
||||
aI1status = aI1.getStatus();
|
||||
// aI2status = aI2.getStatus();
|
||||
System.out.println("Input " + aI1.channel + ": " + aI1.lastValue + ", age= " + aI1status.get("measurementAge") + "ms" + ", badReads= " + aI1status.get("badReads"));
|
||||
// System.out.println("Input " + aI2.channel + ": " + aI2.lastValue + ", age= " + aI2status.get("measurementAge") + "ms" + ", badReads= " + aI2status.get("badReads"));
|
||||
// System.out.println("Input " + aI3.channel + ": " + aI3.lastValue + ", age= " + Duration.between(aI3.lastStored, now).toMillis() + "ms");
|
||||
// System.out.println("Input " + aI4.channel + ": " + aI4.lastValue + ", age= " + Duration.between(aI4.lastStored, now).toMillis() + "ms");
|
||||
// System.out.println("--");
|
||||
// System.out.printf("%s,%s%n", aI1.lastValue, aI2.lastValue);
|
||||
|
||||
}
|
||||
// for (int j = 0; j < 100; j++){
|
||||
// System.out.println("History[" + j + "]= " + aI3.getHistory(j));
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user