diff --git a/firmware/main/XGZP.cpp b/firmware/main/XGZP.cpp new file mode 100644 index 0000000..a001ec7 --- /dev/null +++ b/firmware/main/XGZP.cpp @@ -0,0 +1,152 @@ +/* + Copyright 2019 stickbreaker@github + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + */ + +#include "XGZP.h" +#include + +#define I2C_ERROR_OK 0 + +XGZP::XGZP(void): + _devID(0) +{} + +bool XGZP::begin(uint8_t devID) { + _devID=0; + Wire.begin(); + Wire.beginTransmission(devID); + Wire.write(0xA5); // Sys Config Register + Wire.endTransmission(); + uint8_t count = Wire.requestFrom(devID, (uint8_t) 1); + if (count == 1) { + uint8_t reg = Wire.read(); + Serial.printf(" Read System Configuration register = 0x%02X\n", reg); + + Wire.beginTransmission(devID); + Wire.write(0xA5); // sys configuration register + // DAC ON, Single Conversion, Vout=Fixed by Vext*1.5, Vext=3.6V, Calibrated Output, Diag=on + Wire.write(0xFD); // + uint8_t err = Wire.endTransmission(); + if(err != 0){ + Serial.printf("Writing to register 0xA5 value 0xFD failed = %d\n", err); + return false; // fail + } + + bool ready = false; + uint32_t tick = millis(); // timout + while (!ready && (millis() - tick < 1000)) { + Wire.beginTransmission(devID); + ready = I2C_ERROR_OK == Wire.endTransmission(); + } + if (ready) { + Wire.beginTransmission(devID); + Wire.write(0xA5); + Wire.write(0x0A); + err = Wire.endTransmission(); + if(err != 0){ + Serial.printf("Writing to register 0xA5 value 0x0A failed = %d\n", err); + return false; // fail + } + } + else { + Serial.println("Timeout, Sensor did not respond after first config set."); + return false; + } + } + else { + Serial.println("Reading from 0xA5 failed"); + return false; + } + _devID = devID; + return true; +} + +bool XGZP::read(float * reading) { + if ((! _devID ) || ( *reading == NULL )) { + Serial.println("Not initialized or no return value location, returning"); + if (*reading != NULL) *reading = NAN; + return false; + } + + float XGZPC_Value = 0; + bool ready = false; + uint32_t tick = millis(); + while (!ready && (millis() - tick < 1000)) { // Wait upto 1sec for conversion to complete + Wire.beginTransmission(_devID); + Wire.write(0x02); // status register + uint8_t err = Wire.endTransmission(); + if (err == I2C_ERROR_OK) { + uint8_t count = Wire.requestFrom(_devID, (uint8_t) 1); // get status byte + if (count==1) { // got data + uint8_t status = Wire.read(); + ready = (status & 0x01) == 0x01; // data ready! + } + } + } + + if (ready) { + Wire.beginTransmission(_devID); + Wire.write(0x06); // data register + uint8_t err = Wire.endTransmission(); + if (err == I2C_ERROR_OK) { + uint8_t count = Wire.requestFrom(_devID, (uint8_t) 3); // get data + if (count == 3) { // got data + XGZPC_Value = Wire.read() * 65536.0 + Wire.read() * 256.0 + Wire.read(); + XGZPC_Value = XGZPC_Value / 8.0; + Serial.print("Pa Value: "); + Serial.println(XGZPC_Value); + + XGZPC_Value = XGZPC_Value / 6895.0; + + *reading = XGZPC_Value; + Wire.beginTransmission(_devID); // start next sample? + Wire.write(0x30); + Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate + uint8_t err = Wire.endTransmission(); + if (err != 0) { + Serial.printf(" next Sample start failed? i2cError = %d\n", err); + return true; // data valid, next sample will be a problem. + } + else { + return true; + } + } + else{ + Serial.println("Read data failed"); + return false; + } + } + else { + Serial.println("setData address failed"); + return false; + } + } + else{ + Wire.beginTransmission(_devID); // start next sample? + Wire.write(0x30); + Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate + uint8_t err = Wire.endTransmission(); + if (err != 0) { + Serial.printf(" Next Sample start failed? i2cError = %d\n", err); + } + return false; + } +} diff --git a/firmware/main/XGZP.h b/firmware/main/XGZP.h new file mode 100644 index 0000000..532790d --- /dev/null +++ b/firmware/main/XGZP.h @@ -0,0 +1,39 @@ +/* + Copyright 2019 stickbreaker@github + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to + deal in the Software without restriction, including without limitation the + rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + sell copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. +*/ + +#ifndef XGZP_h +#define XGZP_h + +#include + +class XGZP { + +public: + XGZP(); + + bool begin( uint8_t devID = 0x6D ); + bool read( float * reading); +private: + uint8_t _devID; + +}; +#endif diff --git a/firmware/main/hardware.cpp b/firmware/main/hardware.cpp index 0dfed2a..3807d3c 100644 --- a/firmware/main/hardware.cpp +++ b/firmware/main/hardware.cpp @@ -2,6 +2,7 @@ #include #include "hardware.h" +#include "XGZP.h" bool simulating = false; double simulatedPressure = 0.0; @@ -11,6 +12,18 @@ double pressureChangeRate = 0.0; double deflateOffsetMultiplier = 0.0; double inflateOffsetMultiplier = 0.0; double offsetMultiplier = 0.0; +float value = 1; + +XGZP sensor; + +void initHardware() { + Serial.println("Initializing hardware..."); + + if(!sensor.begin()){ + Serial.printf("Error initializing Sensor\n Locking up."); + while(1); + } +} void initSimulation() { simulating = true; @@ -39,8 +52,8 @@ void measurePressure(double &pressureValue) { adjusted *= offsetMultiplier; pressureValue = 0.99 * pressureValue + 0.01 * adjusted; } else { - int sensorValue = analogRead(PRESSURE_SENSOR_PIN); - double adjusted = 0.098 * sensorValue - 16.56 + 3.58; + sensor.read(&value); + double adjusted = 1.368 * value - 6.4; pressureValue = 0.99 * pressureValue + 0.01 * adjusted; } } @@ -51,8 +64,8 @@ void resamplePressure(double &pressureValue) { adjusted *= offsetMultiplier; pressureValue = adjusted; } else { - int sensorValue = analogRead(PRESSURE_SENSOR_PIN); - double adjusted = 0.098 * sensorValue - 16.56 + 3.58; + sensor.read(&value); + double adjusted = 1.368 * value - 6.4; pressureValue = adjusted; } } diff --git a/firmware/main/hardware.h b/firmware/main/hardware.h index b19ead5..f6980f6 100644 --- a/firmware/main/hardware.h +++ b/firmware/main/hardware.h @@ -15,6 +15,7 @@ extern double simulatedPressure; void initSimulation(); +void initHardware(); void tickSimulation(); void measurePressure(double &pressureValue); void resamplePressure(double &pressureValue); diff --git a/firmware/main/main.ino b/firmware/main/main.ino index da394fd..a86690b 100644 --- a/firmware/main/main.ino +++ b/firmware/main/main.ino @@ -5,7 +5,7 @@ #include "hardware.h" -#define SIMULATE 1 +//#define SIMULATE 1 #define DEBUG 0 #define TIMEOUT_TIME 7000 @@ -110,6 +110,8 @@ void setup() initSimulation(); #endif + initHardware(); + resamplePressure(pressureValue); } diff --git a/firmware/pressure/pressure.ino b/firmware/pressure_analog/pressure_analog.ino similarity index 100% rename from firmware/pressure/pressure.ino rename to firmware/pressure_analog/pressure_analog.ino diff --git a/firmware/pressure_digital/XGZP.cpp b/firmware/pressure_digital/XGZP.cpp new file mode 100644 index 0000000..a001ec7 --- /dev/null +++ b/firmware/pressure_digital/XGZP.cpp @@ -0,0 +1,152 @@ +/* + Copyright 2019 stickbreaker@github + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + */ + +#include "XGZP.h" +#include + +#define I2C_ERROR_OK 0 + +XGZP::XGZP(void): + _devID(0) +{} + +bool XGZP::begin(uint8_t devID) { + _devID=0; + Wire.begin(); + Wire.beginTransmission(devID); + Wire.write(0xA5); // Sys Config Register + Wire.endTransmission(); + uint8_t count = Wire.requestFrom(devID, (uint8_t) 1); + if (count == 1) { + uint8_t reg = Wire.read(); + Serial.printf(" Read System Configuration register = 0x%02X\n", reg); + + Wire.beginTransmission(devID); + Wire.write(0xA5); // sys configuration register + // DAC ON, Single Conversion, Vout=Fixed by Vext*1.5, Vext=3.6V, Calibrated Output, Diag=on + Wire.write(0xFD); // + uint8_t err = Wire.endTransmission(); + if(err != 0){ + Serial.printf("Writing to register 0xA5 value 0xFD failed = %d\n", err); + return false; // fail + } + + bool ready = false; + uint32_t tick = millis(); // timout + while (!ready && (millis() - tick < 1000)) { + Wire.beginTransmission(devID); + ready = I2C_ERROR_OK == Wire.endTransmission(); + } + if (ready) { + Wire.beginTransmission(devID); + Wire.write(0xA5); + Wire.write(0x0A); + err = Wire.endTransmission(); + if(err != 0){ + Serial.printf("Writing to register 0xA5 value 0x0A failed = %d\n", err); + return false; // fail + } + } + else { + Serial.println("Timeout, Sensor did not respond after first config set."); + return false; + } + } + else { + Serial.println("Reading from 0xA5 failed"); + return false; + } + _devID = devID; + return true; +} + +bool XGZP::read(float * reading) { + if ((! _devID ) || ( *reading == NULL )) { + Serial.println("Not initialized or no return value location, returning"); + if (*reading != NULL) *reading = NAN; + return false; + } + + float XGZPC_Value = 0; + bool ready = false; + uint32_t tick = millis(); + while (!ready && (millis() - tick < 1000)) { // Wait upto 1sec for conversion to complete + Wire.beginTransmission(_devID); + Wire.write(0x02); // status register + uint8_t err = Wire.endTransmission(); + if (err == I2C_ERROR_OK) { + uint8_t count = Wire.requestFrom(_devID, (uint8_t) 1); // get status byte + if (count==1) { // got data + uint8_t status = Wire.read(); + ready = (status & 0x01) == 0x01; // data ready! + } + } + } + + if (ready) { + Wire.beginTransmission(_devID); + Wire.write(0x06); // data register + uint8_t err = Wire.endTransmission(); + if (err == I2C_ERROR_OK) { + uint8_t count = Wire.requestFrom(_devID, (uint8_t) 3); // get data + if (count == 3) { // got data + XGZPC_Value = Wire.read() * 65536.0 + Wire.read() * 256.0 + Wire.read(); + XGZPC_Value = XGZPC_Value / 8.0; + Serial.print("Pa Value: "); + Serial.println(XGZPC_Value); + + XGZPC_Value = XGZPC_Value / 6895.0; + + *reading = XGZPC_Value; + Wire.beginTransmission(_devID); // start next sample? + Wire.write(0x30); + Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate + uint8_t err = Wire.endTransmission(); + if (err != 0) { + Serial.printf(" next Sample start failed? i2cError = %d\n", err); + return true; // data valid, next sample will be a problem. + } + else { + return true; + } + } + else{ + Serial.println("Read data failed"); + return false; + } + } + else { + Serial.println("setData address failed"); + return false; + } + } + else{ + Wire.beginTransmission(_devID); // start next sample? + Wire.write(0x30); + Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate + uint8_t err = Wire.endTransmission(); + if (err != 0) { + Serial.printf(" Next Sample start failed? i2cError = %d\n", err); + } + return false; + } +} diff --git a/firmware/pressure_digital/XGZP.h b/firmware/pressure_digital/XGZP.h new file mode 100644 index 0000000..532790d --- /dev/null +++ b/firmware/pressure_digital/XGZP.h @@ -0,0 +1,39 @@ +/* + Copyright 2019 stickbreaker@github + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to + deal in the Software without restriction, including without limitation the + rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + sell copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. +*/ + +#ifndef XGZP_h +#define XGZP_h + +#include + +class XGZP { + +public: + XGZP(); + + bool begin( uint8_t devID = 0x6D ); + bool read( float * reading); +private: + uint8_t _devID; + +}; +#endif diff --git a/firmware/pressure_digital/pressure_digital.ino b/firmware/pressure_digital/pressure_digital.ino new file mode 100644 index 0000000..b24c18e --- /dev/null +++ b/firmware/pressure_digital/pressure_digital.ino @@ -0,0 +1,28 @@ +#include +#include + +#include "XGZP.h" + +XGZP sensor; + +float value = 1; + +void setup() { + Serial.begin(9600); + + if(!sensor.begin()){ + Serial.printf("Error initializing Sensor\n Locking up."); + while(1); + } +} + +void loop() { + sensor.read(&value); + + float corrected = 1.368 * value - 6.4; + + Serial.print(corrected); + Serial.println(" PSI"); + + delay(250); +}