diff --git a/POCpp.xcodeproj/project.pbxproj b/POCpp.xcodeproj/project.pbxproj index d7d2825..1ee8834 100644 --- a/POCpp.xcodeproj/project.pbxproj +++ b/POCpp.xcodeproj/project.pbxproj @@ -23,7 +23,6 @@ C568AEFC1F13EA5000524D66 /* LPPair.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C568AEEA1F13EA5000524D66 /* LPPair.cpp */; }; C568AEFD1F13EA5000524D66 /* LPStatus.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C568AEEC1F13EA5000524D66 /* LPStatus.cpp */; }; C568AEFE1F13EA5000524D66 /* Measurement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C568AEEE1F13EA5000524D66 /* Measurement.cpp */; }; - C568AEFF1F13EA5000524D66 /* MuxSetup.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C568AEF01F13EA5000524D66 /* MuxSetup.cpp */; }; C568AF001F13EA5000524D66 /* Well.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C568AEF21F13EA5000524D66 /* Well.cpp */; }; C568AF011F13EA5000524D66 /* WellTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C568AEF41F13EA5000524D66 /* WellTest.cpp */; }; /* End PBXBuildFile section */ @@ -72,8 +71,6 @@ C568AEED1F13EA5000524D66 /* LPStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LPStatus.h; path = src/LPStatus.h; sourceTree = ""; }; C568AEEE1F13EA5000524D66 /* Measurement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Measurement.cpp; path = src/Measurement.cpp; sourceTree = ""; }; C568AEEF1F13EA5000524D66 /* Measurement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Measurement.h; path = src/Measurement.h; sourceTree = ""; }; - C568AEF01F13EA5000524D66 /* MuxSetup.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MuxSetup.cpp; path = src/MuxSetup.cpp; sourceTree = ""; }; - C568AEF11F13EA5000524D66 /* MuxSetup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MuxSetup.h; path = src/MuxSetup.h; sourceTree = ""; }; C568AEF21F13EA5000524D66 /* Well.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Well.cpp; path = src/Well.cpp; sourceTree = ""; }; C568AEF31F13EA5000524D66 /* Well.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Well.h; path = src/Well.h; sourceTree = ""; }; C568AEF41F13EA5000524D66 /* WellTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = WellTest.cpp; path = src/WellTest.cpp; sourceTree = ""; }; @@ -154,8 +151,6 @@ C568AEED1F13EA5000524D66 /* LPStatus.h */, C568AEEE1F13EA5000524D66 /* Measurement.cpp */, C568AEEF1F13EA5000524D66 /* Measurement.h */, - C568AEF01F13EA5000524D66 /* MuxSetup.cpp */, - C568AEF11F13EA5000524D66 /* MuxSetup.h */, C568AEF21F13EA5000524D66 /* Well.cpp */, C568AEF31F13EA5000524D66 /* Well.h */, C568AEF41F13EA5000524D66 /* WellTest.cpp */, @@ -225,7 +220,6 @@ files = ( C568AEF61F13EA5000524D66 /* AnalogIn.cpp in Sources */, C568AEFE1F13EA5000524D66 /* Measurement.cpp in Sources */, - C568AEFF1F13EA5000524D66 /* MuxSetup.cpp in Sources */, C568AEF81F13EA5000524D66 /* Database.cpp in Sources */, C568AEFA1F13EA5000524D66 /* DigitalOut.cpp in Sources */, C568AEF71F13EA5000524D66 /* Card.cpp in Sources */, diff --git a/POCpp.xcodeproj/project.xcworkspace/xcuserdata/patrickjmcd.xcuserdatad/UserInterfaceState.xcuserstate b/POCpp.xcodeproj/project.xcworkspace/xcuserdata/patrickjmcd.xcuserdatad/UserInterfaceState.xcuserstate index 1b898c0..669bd96 100644 Binary files a/POCpp.xcodeproj/project.xcworkspace/xcuserdata/patrickjmcd.xcuserdatad/UserInterfaceState.xcuserstate and b/POCpp.xcodeproj/project.xcworkspace/xcuserdata/patrickjmcd.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/src/AnalogIn.cpp b/src/AnalogIn.cpp index f553530..13a1d89 100644 --- a/src/AnalogIn.cpp +++ b/src/AnalogIn.cpp @@ -10,12 +10,7 @@ AnalogIn::AnalogIn(int channel, double rawMin, double rawMax, double euMin, double euMax) { // TODO Auto-generated constructor stub this->channel = channel; - if (channel != 99){ - channelMux[0] = MUX_VALUES[channel][0]; - channelMux[1] = MUX_VALUES[channel][1]; - channelMux[2] = MUX_VALUES[channel][2]; - } - badReads = 0; + this->rawMax = rawMax; this->rawMin = rawMin; this->euMax = euMax; @@ -52,10 +47,6 @@ int AnalogIn::getRawValue(){ return rawValue; } -long AnalogIn::getBadReads(){ - return badReads; -} - double AnalogIn::setValue(int value){ rawValue = value; double pv = m * rawValue + b; @@ -66,25 +57,6 @@ double AnalogIn::setValue(int value){ return pv; } -double AnalogIn::read(MuxSetup *mux){ - mux->set(channelMux[0], channelMux[1], channelMux[2]); - int rawIn = mux->readAnalog(); - if (rawIn != -1){ - badReads = 0; - return setValue(rawIn); - } else { - badReads++; - } - if (badReads > 10){ - std::cout <<"Too many bad reads" << std::endl; - -// mux->preAnalogWrite(); -// mux->postAnalogWrite(); - } - return lastValue; - -} - void AnalogIn::setRawScaling(int rawMin, int rawMax){ this->rawMin = rawMin; this->rawMax = rawMax; @@ -93,23 +65,3 @@ void AnalogIn::setRawScaling(int rawMin, int rawMax){ AnalogIn::~AnalogIn() { // TODO Auto-generated destructor stub } - -int test_AnalogIn(){ - MuxSetup mux; - double a1val, a2val; - - AnalogIn aI1(1, 32560, 65535, 0, 100); - AnalogIn aI2(2, 0, 65535, 0, 50000); - - for(int i = 0; i< 50; i++){ - a1val = aI1.read(&mux); - a2val = aI2.read(&mux); - - std::cout << "Input 1: " << a1val << ", badReads: " << aI1.getBadReads() << std::endl; - std::cout << "Input 2: " << a2val << ", badReads: " << aI2.getBadReads() << std::endl; - std::cout << "--" << std::endl; - } - - return 1; -} - diff --git a/src/AnalogIn.h b/src/AnalogIn.h index 646f4dd..8733288 100644 --- a/src/AnalogIn.h +++ b/src/AnalogIn.h @@ -8,7 +8,6 @@ #ifndef ANALOGIN_H_ #define ANALOGIN_H_ -#include "MuxSetup.h" #include #include @@ -17,11 +16,8 @@ public: AnalogIn(int ch, double rawMinI, double rawMaxI, double euMinI, double euMaxI); int getRawValue(); double getLastValue(); - long getBadReads(); double getHistory(int index); double setValue(int value); - double read(MuxSetup *mux); - double readSim(double simRaw); void setRawScaling(int rawMin, int rawMax); virtual ~AnalogIn(); @@ -31,9 +27,7 @@ private: double lastValue; double rawMin, rawMax, euMin, euMax; double m, b; - int channelMux[3]; double history[100]; - long badReads; }; #endif /* ANALOGIN_H_ */ diff --git a/src/DigitalIn.cpp b/src/DigitalIn.cpp index dffff6b..b4926de 100644 --- a/src/DigitalIn.cpp +++ b/src/DigitalIn.cpp @@ -10,14 +10,10 @@ DigitalIn::DigitalIn(int channel) { this->channel = channel; value = -1; - channelMux[0] = MUX_VALUES[channel][0]; - channelMux[1] = MUX_VALUES[channel][1]; - channelMux[2] = MUX_VALUES[channel][2]; } -int DigitalIn::read(MuxSetup *mux){ - mux->set(channelMux[0], channelMux[1], channelMux[2]); - value = mux->readDigital(); +int DigitalIn::setValue(int val){ + value = val; return value; } @@ -25,36 +21,3 @@ DigitalIn::~DigitalIn() { // TODO Auto-generated destructor stub } -int test(){ - MuxSetup mux; - - DigitalIn dI1(1); - DigitalIn dI2(2); - DigitalIn dI3(3); - DigitalIn dI4(4); - DigitalIn dI5(5); - DigitalIn dI6(6); - DigitalIn dI7(7); - DigitalIn dI8(8); - - int d1v = dI1.read(&mux); - int d2v = dI2.read(&mux); - int d3v = dI3.read(&mux); - int d4v = dI4.read(&mux); - int d5v = dI5.read(&mux); - int d6v = dI6.read(&mux); - int d7v = dI7.read(&mux); - int d8v = dI8.read(&mux); - - std::cout << "DigIn 1: " << d1v << std::endl; - std::cout << "DigIn 2: " << d2v << std::endl; - std::cout << "DigIn 3: " << d3v << std::endl; - std::cout << "DigIn 4: " << d4v << std::endl; - std::cout << "DigIn 5: " << d5v << std::endl; - std::cout << "DigIn 6: " << d6v << std::endl; - std::cout << "DigIn 7: " << d7v << std::endl; - std::cout << "DigIn 8: " << d8v << std::endl; - - return 1; -} - diff --git a/src/DigitalIn.h b/src/DigitalIn.h index b3689e2..38d0e46 100644 --- a/src/DigitalIn.h +++ b/src/DigitalIn.h @@ -8,17 +8,14 @@ #ifndef DIGITALIN_H_ #define DIGITALIN_H_ -#include "MuxSetup.h" - class DigitalIn { private: int channel; int value; - int channelMux[3]; public: DigitalIn(int channel); - int read(MuxSetup *mux); + int setValue(int val); virtual ~DigitalIn(); }; diff --git a/src/DigitalOut.cpp b/src/DigitalOut.cpp index cefa387..806ea11 100644 --- a/src/DigitalOut.cpp +++ b/src/DigitalOut.cpp @@ -39,53 +39,17 @@ DigitalOut::DigitalOut(int chan) { // std::cout << "Cannot setup channel " << chan; break; } - pin = new mraa::Gpio(channel); - pin->dir(mraa::DIR_OUT); + value = 0; } void DigitalOut::write(int value){ - pin->write(value); + this->value = value; } DigitalOut::~DigitalOut() { // TODO Auto-generated destructor stub - pin->write(0); -} - -int test_DigitalOut(){ - DigitalOut dO1(1); - DigitalOut dO2(2); - DigitalOut dO3(3); - DigitalOut dO4(4); - DigitalOut dO5(5); - DigitalOut dO6(6); - - dO1.write(1); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO2.write(1); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO3.write(1); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO4.write(1); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO5.write(1); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO6.write(1); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO6.write(0); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO5.write(0); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO4.write(0); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO3.write(0); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO2.write(0); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - dO1.write(0); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } + diff --git a/src/DigitalOut.h b/src/DigitalOut.h index 17b5852..0f24cf0 100644 --- a/src/DigitalOut.h +++ b/src/DigitalOut.h @@ -8,7 +8,6 @@ #ifndef DIGITALOUT_H_ #define DIGITALOUT_H_ -#include "mraa.hpp" #include #include #include @@ -17,7 +16,7 @@ class DigitalOut { private: int channel; - mraa::Gpio* pin; + int value; public: DigitalOut(int chan); diff --git a/src/MuxSetup.cpp b/src/MuxSetup.cpp deleted file mode 100644 index d62b337..0000000 --- a/src/MuxSetup.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - * MuxSetup.cpp - * - * Created on: Jun 27, 2017 - * Author: patrickjmcd - */ - -#include "MuxSetup.h" - -int PIN_MUX_1 = 21; -int PIN_MUX_2 = 22; -int PIN_MUX_3 = 23; -int PIN_DIGITAL_READ = 24; -int PIN_ANOUT_TRIGGER = 4; - -int SPI_CHANNEL = 0; - -MuxSetup::MuxSetup() { - // TODO Auto-generated constructor stub - - mux1value = 0; - mux2value = 0; - mux3value = 0; - - pinMux1 = new mraa::Gpio(PIN_MUX_1); - mraa::Result response = pinMux1->dir(mraa::DIR_OUT); - - pinMux2 = new mraa::Gpio(PIN_MUX_2); - response = pinMux2->dir(mraa::DIR_OUT); - - pinMux3 = new mraa::Gpio(PIN_MUX_3); - response = pinMux3->dir(mraa::DIR_OUT); - - pinDigitalRead = new mraa::Gpio(PIN_DIGITAL_READ); - response = pinDigitalRead->dir(mraa::DIR_OUT); - - pinAnOutTrigger = new mraa::Gpio(PIN_ANOUT_TRIGGER); - response = pinAnOutTrigger->dir(mraa::DIR_OUT); - - - - spi = new mraa::Spi(SPI_CHANNEL); - - pinAnOutTrigger->write(1); - -} - -int MuxSetup::apply(){ - pinMux1->write(mux1value); - pinMux2->write(mux2value); - pinMux3->write(mux3value); - - return mux1value + mux2value * 2 + mux3value * 4; -} - -int MuxSetup::set(int v1, int v2, int v3){ - - mux1value = v1; - mux2value = v2; - mux3value = v3; - return apply(); -} - -int MuxSetup::readDigital(){ - return pinDigitalRead->read(); -} - -int MuxSetup::readAnalog(){ - - uint8_t zero[] = {0x00, 0x00, 0x000}; - uint8_t* recv = spi->write(zero, 3); - int a = recv[0]; - int b = recv[1]; - int c = recv[2]; - std::cout << a << " - " << b << " - " << c << std::endl; - - if (c == 13){ - int x = a * 256; - x = x + b; - return x; - } else { - return -1; - } -} - -void MuxSetup::preAnalogWrite(){ - pinAnOutTrigger->write(0); -} - -void MuxSetup::postAnalogWrite(){ - pinAnOutTrigger->write(1); -} - -MuxSetup::~MuxSetup() { - // TODO Auto-generated destructor stub -} - - - diff --git a/src/MuxSetup.h b/src/MuxSetup.h deleted file mode 100644 index d2baf45..0000000 --- a/src/MuxSetup.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * MuxSetup.h - * - * Created on: Jun 27, 2017 - * Author: patrickjmcd - */ - -#ifndef MUXSETUP_H_ -#define MUXSETUP_H_ - -#include "mraa.hpp" - - -#include - - -const int MUX_VALUES[9][3] = { - {0,0,0}, - {0,0,0}, - {1,0,0}, - {0,1,0}, - {1,1,0}, - {0,0,1}, - {1,0,1}, - {0,1,1}, - {1,1,1}, -}; - -class MuxSetup { -public: - - MuxSetup(); - int set(int v1, int v2, int v3); - void preAnalogWrite(); - void postAnalogWrite(); - int readAnalog(); - int readDigital(); - int writeDigital(int val); - virtual ~MuxSetup(); - - -private: - int apply(); - int mux1value, mux2value, mux3value; - mraa::Gpio* pinMux1; - mraa::Gpio* pinMux2; - mraa::Gpio* pinMux3; - mraa::Gpio* pinDigitalRead; - mraa::Gpio* pinAnOutTrigger; - mraa::Spi* spi; - -}; - -#endif /* MUXSETUP_H_ */ diff --git a/src/Well.cpp b/src/Well.cpp index cf246b4..fa2e0c4 100644 --- a/src/Well.cpp +++ b/src/Well.cpp @@ -747,22 +747,43 @@ double lookupRodWeightPerFoot(double i_ym, double i_diam) { return wtPerFt; }; +bool fileExists (const std::string& name) { + std::ifstream f(name.c_str()); + return f.good(); +} + void Well::getWellSetup(){ std::string jsonFilename = "wellSetup.json"; + std::string renamedFilename = "wellSetup.json.bak"; std::ifstream i(jsonFilename); json jsonData; i >> jsonData; - try { - wellName = jsonData["wellName"]; - dt = jsonData["dt"]; - setPumpDiameter(jsonData["pumpDiameter"]); - fluidGradient = jsonData["fluidGradient"]; - setTubingInnerDiameter(jsonData["tubingID"]); - setTubingOuterDiameter(jsonData["tubingOD"]); - tubingAnchorDepth = jsonData["] - + if (fileExists(jsonFilename)){ + + try { + wellName = jsonData["wellName"]; + dt = jsonData["dt"]; + setPumpDiameter(jsonData["pumpDiameter"]); + fluidGradient = jsonData["fluidGradient"]; + setTubingInnerDiameter(jsonData["tubingID"]); + setTubingOuterDiameter(jsonData["tubingOD"]); + + auto tapers = jsonData["tapers"]; + + for (int i = 0; i < tapers.size(); i++){ + setRodLength(i, tapers[i]["length"]); + setRodDiameter(i, tapers[i]["diameter"]); + setRodYM(i, tapers[i]["material"]); + setDampingFactor(i, tapers[i]["dampingFactor"]); + } + rename(jsonFilename.c_str(), renamedFilename.c_str()); + // updateTapers(true); + + } catch (std::exception){ + std::cout << "Error in JSON" << std::endl; + }; } @@ -771,36 +792,7 @@ void Well::getWellSetup(){ try { - Object newTubingAnchorDepth = well.get("tubingAnchorDepth"); - if (newTubingAnchorDepth != null) tubingAnchorDepth = (Double) newTubingAnchorDepth; - - Object newStructuralRating = well.get("structuralRating"); - if (newStructuralRating != null) structuralRating = (Double) newStructuralRating; - - Object newStuffingBoxFriction = well.get("stuffingBoxFriction"); - if (newStuffingBoxFriction != null) setSbfriction((Double) newStuffingBoxFriction); - - Object newTubingHeadPressure = well.get("tubingOD"); - if (newTubingHeadPressure != null) setTubingOD((Double) newTubingHeadPressure); - - JSONArray tapers = (JSONArray) well.get("tapers"); - numTapers = tapers.size(); - for (int i = 0; i < numTapers; i++){ - int currentTaperNum = i + 1; - JSONObject taperObj = (JSONObject) tapers.get(i); - - Object newLength = taperObj.get("length"); - if (newLength != null) setRodLength(currentTaperNum, (Double) newLength); - - Object newDiameter = taperObj.get("diameter"); - if (newDiameter != null) setRodDiameter(currentTaperNum, (Double) newDiameter); - - Object newMaterial = taperObj.get("material"); - if (newMaterial != null) setRodYM(currentTaperNum, (String) newMaterial); - - Object newDampingFactor = taperObj.get("dampingFactor"); - if (newDampingFactor != null) setDampingFactor(currentTaperNum, (Double) newDampingFactor); - } + Path fromFile = Paths.get(jsonFilename); Path toFile = Paths.get(jsonFilename + ".bak");