diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.gitignore b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.travis.yml b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.travis.yml new file mode 100644 index 0000000..7c486f1 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.travis.yml @@ -0,0 +1,67 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < https://docs.platformio.org/page/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < https://docs.platformio.org/page/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < https://docs.platformio.org/page/userguide/cmd_ci.html > +# +# +# Please choose one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# - platformio update +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to be used as a library with examples. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# - platformio update +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.vscode/extensions.json b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.vscode/extensions.json new file mode 100644 index 0000000..272828b --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.vscode/settings.json b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.vscode/settings.json new file mode 100644 index 0000000..b4af8b4 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "terminal.integrated.env.windows": { + "PATH": "C:\\Users\\lored\\.platformio\\penv\\Scripts;C:\\Users\\lored\\.platformio\\penv;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\Calibre2\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\Git\\cmd;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\lored\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\lored\\AppData\\Local\\atom\\bin;C:\\Users\\lored\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\lored\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\Calibre2\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\Git\\cmd;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\lored\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\lored\\AppData\\Local\\atom\\bin;C:\\Users\\lored\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\lored\\AppData\\Local\\Programs\\Microsoft VS Code\\bin", + "PLATFORMIO_CALLER": "vscode" + } +} \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/include/README b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/lib/README b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/platformio.ini b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/platformio.ini new file mode 100644 index 0000000..47c4e89 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/platformio.ini @@ -0,0 +1,15 @@ +;PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:ATmega88P] +platform = atmelavr +board = ATmega88P +framework = arduino + diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/LightChrono.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/LightChrono.cpp new file mode 100644 index 0000000..14b719b --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/LightChrono.cpp @@ -0,0 +1,67 @@ +/* + * Lightweight chronometer class. + * Simple chronometer/stopwatch class that counts the time passed since started. + * + * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com + * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info + * + * The MIT License (MIT) + * + * 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. + */ +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif +#include "LightChrono.h" + +LightChrono::LightChrono() +{ + restart(); +} + +void LightChrono::start() { restart(); } + +void LightChrono::restart() +{ + _startTime = millis(); +} + +LightChrono::chrono_t LightChrono::elapsed() const { + return (millis() - _startTime); +} + +bool LightChrono::hasPassed(LightChrono::chrono_t timeout) const +{ + return (elapsed() >= timeout); +} + +bool LightChrono::hasPassed(LightChrono::chrono_t timeout, bool restartIfPassed) { + if (hasPassed(timeout)) { + if (restartIfPassed) + restart(); + return true; + } + else { + return false; + } +} + + diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/LightChrono.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/LightChrono.h new file mode 100644 index 0000000..1060a3b --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/LightChrono.h @@ -0,0 +1,72 @@ +/* + * Lightweight chronometer class. + * Simple chronometer/stopwatch class that counts the time passed since started. + * + * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com + * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info + * + * The MIT License (MIT) + * + * 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 LIGHT_CHRONO_H_ +#define LIGHT_CHRONO_H_ + +/* + * Example code: + * + * LightChrono myLightChrono; // chronometer automatically starts at creation + * // ... + * myLightChrono.restart(); // you can start (restart) it later + * while (!myLightChrono.hasPassed(2000)) // a 2000 ms loop + * Serial.println(myLightChrono.elapsed()); // current time + * // do something + * // ... + */ +class LightChrono +{ +public: +#if defined(ARDUINO_ARC32_TOOLS) + typedef uint64_t chrono_t; +#else + typedef unsigned long chrono_t; +#endif + +private: + chrono_t _startTime; // keeps track of start time (in milliseconds) + +public: + /// Constructor. + LightChrono(); + + // Starts/restarts the chronometer. + void start(); + void restart(); + + /// Returns the elapsed time since start (in milliseconds). + chrono_t elapsed() const; + + /// Returns true iff elapsed time has passed given timeout. + bool hasPassed(chrono_t timeout) const; + bool hasPassed(chrono_t timeout, bool restartIfPassed); +}; + +#endif + + diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/Pyr0_Piezo_Sensor_v2.x.x.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/Pyr0_Piezo_Sensor_v2.x.x.cpp new file mode 100644 index 0000000..b70c59c --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/Pyr0_Piezo_Sensor_v2.x.x.cpp @@ -0,0 +1,144 @@ +/* + Piezoelectric Z-Axis sensor using AtMega88/168/328 (AtMega 48 doesnt have enough memory for this version) + + This sketch reads a piezo element to detect a touch of the printer's nozzle to the bed. + The sense pin is tied to an interrupt, which is pulled high by internal pullup resistor. + When the piezo touches the bed, the amplification circuit will draw the interrupt pin low + and the atmega will output a pulse based on the programmed trigger duration + + * PD2 INT0 (Piezo In 'D2') + * D7 PCINT23 (Trigger OUT 'D7') + * PC0 ADC0 (Voltage Reference Check 'A0') + * PC1 ADC1 (Sensitivity Adjustment Check 'A1') + * PD4 PCINT20 (Error feedback LED 'D4') + * PB6 PCINT6 (Voltage Adjustment Resistor 0 'D20') + * PB7 PCINT7 (Voltage Adjustment Resistor 1 'D21') + * PD5 T1 (Voltage Adjustment Resistor 2 'D5') + * PD6 PCINT22 (Voltage Adjustment Resistor 3 'D6') + * PB1 OC1A (Comparator VRef PWM Out 'D9') + * PD3 OC2B (Voltage Follower VRef PWM Out 'D3') + + Schematics for this project can be found here: https://github.com/pyr0ball/pyr0piezo/tree/master/docs/Schematics + + For Arduino IDE use MCUdude MiniCore: https://mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json + + + created 2/18/2019 + by Alan "pyr0ball" Weinstock + + This code is in the public domain. +*/ + +/* To set the below parameters using serial input, use the following: + +To change trigger active duration: TRG_D [integer for milliseconds] +To change gain factor: GAIN_F [integer for gain state - see note*] +To change ADC hysteresis value: HYST [integer] +To change sensor input pullup vRef low threshold: VADJ [float value] +To change comparator trigger high threshold: VCOMP [float value] + + +These commands should be wrapped in this format: + + +Examples: + <~ set gain factor to index 3 (6x) + <~ set the vref floor to 2.35V + +*Note for Gain Factor: +The gain STATE is representative of these values: +0 = 3x +1 = 3.5x +2 = 4.33x +3 = 6x +4 = 11x +*/ + +/*------------------------------------------------------------*/ + +// Debug output toggle. Uncomment to enable +#define DEBUG true + +/* Debug output verbose mode will continuously output sensor readings + rather than waiting for user input */ +//#define VERBOSE true + +// Headers, variables, and functions +#include "LightChrono.h" +#include "pP_pins.h" +#include "pP_config.h" +#include "pP_volatile.h" +#include "pP_function.h" +#include "pP_serial.h" + +// i2c input toggle. Uncomment to enable +//#define I2C_INPUT true + +void setup() { + pinMode(TRG_OUT, OUTPUT); // declare the Trigger as as OUTPUT + pinMode(ERR_LED, OUTPUT); + pinMode(Z_TRG, INPUT_PULLUP); // declare z-sense input with pullup + pinMode(V_FOLLOW_PIN, INPUT); + pinMode(VCOMP_SENSE_PIN, INPUT); + pinMode(GADJ_R0, INPUT); // declare input to set high impedance + pinMode(GADJ_R1, INPUT); // declare input to set high impedance + pinMode(GADJ_R2, INPUT); // declare input to set high impedance + pinMode(GADJ_R3, INPUT); // declare input to set high impedance + Serial.begin(9600); + + attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING); + + Serial.println("Initializing Pyr0-Piezo Sensor..."); +} + +/*------------------------------------------------*/ + +void loop() { + if (mainLoop.hasPassed(LOOP_DUR)) { + mainLoop.restart(); + // Blink LED's on init + if (BlinkCount > 0) { + BlinkState = !BlinkState; + digitalWrite(ERR_LED, BlinkState); + digitalWrite(TRG_OUT, BlinkState); + --BlinkCount; + } + + // Get Serial Input + serialInput(); + + // Set any new parameters from serial input + updateParams(); + + // Set the amplification gain factor + adjustGain(); + + // Check voltage of first and second stages and compare against thresholds + adjustVin(); + VComp = analogRead(VCOMP_SENSE_PIN); + VAdj = analogRead(V_FOLLOW_PIN); + + // Voltage Follower adjustment + if (VLast > Hyst || VLast < -Hyst) { + adjustFollow(); + } + + // Voltage Comparator adjustment + if (VLast > Hyst || VLast < -Hyst) { + adjustComp(); + } + + // Alert the user that auto-calibration is ongoing + calibrateAlert(); + + // Check for error state + checkError(); + + // Reply with status + serialReply(); + + // Sets trigger output state to false after completing loop + //digitalWrite(TRG_OUT, HIGH); + sensorHReading = 0; + } +} diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_config.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_config.h new file mode 100644 index 0000000..d93a69e --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_config.h @@ -0,0 +1,41 @@ +// Configurable settings: + +#if !(defined(GAIN_FACTOR)) + int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x +#endif + +#ifndef senseThrs + #define senseThrs 1450 +#endif + +#ifndef compThrs + #define compThrs 2850 +#endif + +#ifndef InitCount + #define InitCount 6 // Number of times to blink the LED on start +#endif + +#if !(defined(LOOP_DUR)) + int LOOP_DUR = 50; // duration of time between ADC checks and other loop functions +#endif + +#if !(defined(TRG_DUR)) + int TRG_DUR = 20; // duration of the Z-axis pulse sent, in ms +#endif + +#if !(defined(Hyst)) + int Hyst = 20; // Hysteresis value for ADC measurements +#endif + +#if !(defined(voldMeterConstant)) + long voltMeterConstant = 1125300L; // For fine tuning input voltage sense +#endif + +#ifdef I2C_INPUT + #if !(defined(pP_i2c_address)) + byte pP_i2c_address = 0xa0; // I2C Bus Address + #endif +#endif + + diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_function.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_function.h new file mode 100644 index 0000000..7cd6cd8 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_function.h @@ -0,0 +1,147 @@ +/* + pyr0-piezo functions library + Created by Alan "pyr0ball" Weinstock 6/26/2019 +*/ + +void digitalWriteFast(uint8_t pin, uint8_t x) { + if (pin / 8) { // pin >= 8 + PORTB ^= (-x ^ PORTB) & (1 << (pin % 8)); + } + else { + PORTD ^= (-x ^ PORTD) & (1 << (pin % 8)); + } +} + +/*------------------------------------------------*/ + +void pulse() { + digitalWriteFast(TRG_OUT, LOW); + sensorHReading = 1; + #ifdef DEBUG + Serial.println("Trig!"); + #endif + delay(TRG_DUR); + digitalWriteFast(TRG_OUT, HIGH); +} + +/*------------------------------------------------*/ + +long readVcc() { + // Read 1.1V reference against AVcc + // set the reference to Vcc and the measurement to the internal 1.1V reference + #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); + #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + ADMUX = _BV(MUX5) | _BV(MUX0); + #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + ADMUX = _BV(MUX3) | _BV(MUX2); + #else + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); + #endif + + delay(2); // Wait for Vref to settle + ADCSRA |= _BV(ADSC); // Start conversion + while (bit_is_set(ADCSRA,ADSC)); // measuring + + uint8_t low = ADCL; // must read ADCL first - it then locks ADCH + uint8_t high = ADCH; // unlocks both + + long result = (high<<8) | low; + + result = voltMeterConstant / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 + return result; // Vcc in millivolts +} + +/*------------------------------------------------*/ + + void adjustVin() { + VOld = Vin; + Vin = readVcc(), DEC; + senseLong = senseThrs * 1024L; + compLong = compThrs * 1024L; + senseInt = (long long) senseLong / Vin; + compInt = (long long) compLong / Vin; + senseInt = (int) senseInt; + compInt = (int) compInt; + } + +/*------------------------------------------------*/ + + void adjustFollow() { + /* Compares diffs of threshold vs read value + if positive, adjusts the follower to within + the range set above*/ + ADJ_FOLLOW = (senseInt / 4); + + // Analog output (PWM) of duty cycle + analogWrite(V_FOL_PWM, ADJ_FOLLOW); +} + +/*------------------------------------------------*/ + +void adjustComp() { + ADJ_COMP = (compInt / 4); + + analogWrite(VCOMP_PWM, ADJ_COMP); +} + +/*------------------------------------------------*/ + +void calibrateAlert() { + VLast = VOld - Vin; + if (VLast > Hyst || VLast < -Hyst ) { + ERR_STATE = 1; + } +} + +/*------------------------------------------------*/ + +void adjustGain() { + + if (GAIN_FACTOR == 0) { + pinMode(GADJ_R3, INPUT); + pinMode(GADJ_R2, INPUT); + pinMode(GADJ_R1, INPUT); + pinMode(GADJ_R0, INPUT); + ERR_STATE = 0; + } + else if (GAIN_FACTOR > 0) { + pinMode(GADJ_R3, OUTPUT); + digitalWrite(GADJ_R3, LOW); + pinMode(GADJ_R2, INPUT); + pinMode(GADJ_R1, INPUT); + pinMode(GADJ_R0, INPUT); + ERR_STATE = 0; + } + else if (GAIN_FACTOR > 1) { + pinMode(GADJ_R2, OUTPUT); + digitalWrite(GADJ_R2, LOW); + pinMode(GADJ_R1, INPUT); + pinMode(GADJ_R0, INPUT); + ERR_STATE = 0; + } + else if (GAIN_FACTOR > 2) { + pinMode(GADJ_R1, OUTPUT); + digitalWrite(GADJ_R1, LOW); + pinMode(GADJ_R0, INPUT); + ERR_STATE = 0; + } + else if (GAIN_FACTOR > 3) { + pinMode(GADJ_R0, OUTPUT); + digitalWrite(GADJ_R0, LOW); + ERR_STATE = 0; + } +} + +/*------------------------------------------------*/ + +void checkError () { + if (ERR_STATE == 1) { + digitalWrite(ERR_LED, BlinkState); + BlinkState = !BlinkState; + } + else if (ERR_STATE == 0) { + BlinkState = LOW; + digitalWrite(ERR_LED, BlinkState); + } +} diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_i2c.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_i2c.cpp new file mode 100644 index 0000000..ccc093d --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_i2c.cpp @@ -0,0 +1,65 @@ +#ifdef I2C_INPUT + +#include +#include "pP_config.h" +#include "pP_i2c.h" +#include + +pP_i2c::pP_i2c(){ + +} + +void pP_i2c::init() { + Wire.begin(pP_i2c_address); +} + +void pP_i2c::i2cInput(int bytesReceived) { + for (int a = 0; a < bytesReceived; a++) { + if (a < maxBytes) { + cmdRcvd[a] = Wire.read(); + } + else { + longRcvd[a] = Wire.read(); + } + } + if (bytesReceived == 1 && (cmdRcvd[0] < regMapSize)) { + return; + } + if (bytesReceived == 1 && (cmdRcvd[0] >= regMapSize)) { + cmdRcvd[0] = 0x00; + return; + } + switch (cmdRcvd[0]) { + case 0x00: + senseInt = (long) cmdRcvd[1]; + return; + break; + case 0x01: + compInt = (long) cmdRcvd[1]; + return; + break; + case 0x02: + GAIN_FACTOR = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x03: + Hyst = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x04: + LOOP_DUR = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x05: + TRG_DUR = (uint8_t) cmdRcvd[1]; + return; + break; + case 0x06: + voltMeterConstant = longRcvd[0]*65536+longRcvd[1]*256+longRcvd[2]; + return; + break; + default: + return; + } +} +#endif diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_i2c.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_i2c.h new file mode 100644 index 0000000..6e49724 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_i2c.h @@ -0,0 +1,35 @@ +#ifndef _pP_i2c_h_ +#define _pP_i2c_h_ +#ifdef I2C_INPUT + + +#define senseInt_Offset 0x00 // Integer of sense threshold in millivolts +#define compInt_Offset 0x01 // Integer of comparator threshold in millivolts +#define gainFactor_Offset 0x02 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x +#define hysteresis_Offset 0x03 // Hysteresis value for ADC measurements +#define loopDuration_Offset 0x04 // duration of time between ADC checks and other loop functions +#define triggerDuration_Offset 0x05 // duration of the Z-axis pulse sent, in ms +#define voltMeterLong_Offset 0x06 // For fine-tuning the input volt master + +/*-------------------------Variables------------------------*/ +#define regMapSize 7 +uint8_t maxBytes = 2; +#define longBytes 4 +byte regMap[regMapSize]; +byte regMapTemp[regMapSize]; +byte cmdRcvd[maxBytes]; +byte longRcvd[longBytes]; + + +/*------------------------------------------------*/ + +class pP_i2c { + public: + pP_i2c(uint8_t address=pP_i2c_address); + + void init(); + void i2cInput(int bytesReceived); +}; + +#endif +#endif diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_pins.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_pins.h new file mode 100644 index 0000000..6de03c2 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_pins.h @@ -0,0 +1,32 @@ +/* pyr0-piezo pins configuration file + +Default pins (based on Rev.2.x.xPCB layout) + + * PD2 INT0 (Piezo In 'D2') + * D7 PCINT23 (Trigger OUT 'D7') + * PC0 ADC0 (Voltage Reference Check 'A0') + * PC1 ADC1 (Sensitivity Adjustment Check 'A1') + * PD4 PCINT20 (Error feedback LED 'D4') + * PB6 PCINT6 (Voltage Adjustment Resistor 0 'D20') + * PB7 PCINT7 (Voltage Adjustment Resistor 1 'D21') + * PD5 T1 (Voltage Adjustment Resistor 2 'D5') + * PD6 PCINT22 (Voltage Adjustment Resistor 3 'D6') + * PB1 OC1A (Comparator VRef PWM Out 'D9') + * PD3 OC2B (Voltage Follower VRef PWM Out 'D3') +*/ + +// Analog Pin Assignments +#define V_FOLLOW_PIN A0 // Sense pin to check Voltage Follower stage +#define VCOMP_SENSE_PIN A1 // Sense pin to check comparator stage voltage + +// Digital Pin Assignments +#define TRG_OUT 7 // LED and Z-Min trigger output connected to digital pin 7 +//#define TRG_OUT 13 // For testing on Atmega328/2560, Output is moved to onboard LED pin +#define Z_TRG 2 // the piezo is connected to INT0 / digital pin 2 +#define ERR_LED 4 // LED will blink if optimal voltage range cannot be achieved +#define GADJ_R0 20 // Auto-adjust ladder pin assignments +#define GADJ_R1 21 // " +#define GADJ_R2 5 // " +#define GADJ_R3 6 // " +#define V_FOL_PWM 3 // PWM analog output pin for voltage follower adjustment +#define VCOMP_PWM 9 // PWM analog output pin for comparator adjustment \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_serial.h new file mode 100644 index 0000000..4adca89 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_serial.h @@ -0,0 +1,211 @@ + +/*------------------------------------------------*/ + +void parseData() { + + // split the data into its parts + + char * strtokIndx; // this is used by strtok() as an index + + strtokIndx = strtok(inputBuffer,","); // get the first part - the string + strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn + + strtokIndx = strtok(NULL, ","); // this continues where the previous call left off + serialInt = atoi(strtokIndx); // convert this part to an integer + +} +/*------------------------------------------------*/ + +void identifyMarkers() { + + char x = Serial.read(); +// char y = Wire.read(); + + if (x == endMarker) { + readInProgress = false; + serialIncoming = true; + inputBuffer[bytesRecvd] = 0; + parseData(); + } + + else if(readInProgress) { + inputBuffer[bytesRecvd] = x; + bytesRecvd ++; + if (bytesRecvd == buffSize) { + bytesRecvd = buffSize - 1; + } + } + + else if (x == startMarker) { + bytesRecvd = 0; + readInProgress = true; + } + #ifdef I2C + if (y == endMarker) { + readInProgress = false; + serialIncoming = true; + inputBuffer[bytesRecvd] = 0; + parseData(); + } + + if(readInProgress) { + inputBuffer[bytesRecvd] = y; + bytesRecvd ++; + if (bytesRecvd == buffSize) { + bytesRecvd = buffSize - 1; + } + } + + if (y == startMarker) { + bytesRecvd = 0; + readInProgress = true; + } + #endif +} + +/*------------------------------------------------*/ + +void updateTrigDuration() { + if (serialInt >= 0) { + TRG_DUR = serialInt; + } +} +/*------------------------------------------------*/ + +void updateGainFactor() { + if (serialInt >= 0) { + GAIN_FACTOR = serialInt; + } +} +/*------------------------------------------------*/ + +void updateVComp() { + if (serialInt >= 0) { + compInt = serialInt; + //senseInt = compInt; // syncing these params til #24 is fixed + } +} +/*------------------------------------------------*/ + +void updateVAdj() { + if (serialInt >= 0) { + senseInt = serialInt; + //compInt = senseInt; // syncing these params til #24 is fixed + } +} +/*------------------------------------------------*/ + +void updateHysteresis() { + if (serialInt >= 0) { + Hyst = serialInt; + } +} +/*------------------------------------------------*/ + +void updateParams() { + if (strcmp(serialMessageIn, "TRG_D") == 0) { + updateTrigDuration(); + } + else if (strcmp(serialMessageIn, "GAIN_F") == 0) { + updateGainFactor(); + } + else if (strcmp(serialMessageIn, "VCOMP") == 0) { + updateVComp(); + } + else if (strcmp(serialMessageIn, "VADJ") == 0) { + updateVAdj(); + } + else if (strcmp(serialMessageIn, "HYST") == 0) { + updateHysteresis(); + } + else if (strcmp(serialMessageIn, "HELP") == 0) { + Serial.println("To change trigger active duration: TRG_D [integer for milliseconds]"); + Serial.println("To change gain factor: GAIN_F [integer for gain state - see note*]"); + Serial.println("To change ADC hysteresis value: HYST [integer]"); + Serial.println("To change sensor input pullup vRef low threshold: VADJ [float value]"); + Serial.println("To change comparator trigger high threshold: VCOMP [float value]"); + Serial.println(""); + Serial.println("These commands should be wrapped in this format:"); + Serial.println(""); + Serial.println(""); + Serial.println("Examples:"); + Serial.println(" <~ set gain factor to index 3 (6x)"); + Serial.println(" <~ set the vref floor to 2.35V"); + parseData(); + } +} + +/*------------------------------------------------*/ + +void serialInput() { + + // receive data from Serial and save it into inputBuffer + + if(Serial.available() > 0) { + + // the order of these IF clauses is significant + identifyMarkers(); + + } +} + +/*------------------------------------------------*/ + +void serialReply() { + #ifndef VERBOSE + if (serialIncoming) { + serialIncoming = false; + #endif + #ifdef DEBUG + Serial.print("Vcc:"); + Serial.println(Vin); + Serial.print("Comp Sense:"); + Serial.print(VComp); + Serial.print(" "); + Serial.print("Comparator State:"); + Serial.print(ADJ_COMP); + Serial.print(" "); + Serial.println(compInt); + + Serial.print("Amp Sense:"); + Serial.print(VAdj); + Serial.print(" "); + Serial.print("Follower State:"); + Serial.print(ADJ_FOLLOW); + Serial.print(" "); + Serial.println(senseInt); + + Serial.print("Gain Factor:"); + Serial.print(GAIN_FACTOR); + switch (GAIN_FACTOR) { + case 0: + Serial.println(" 3x"); + break; + case 1: + Serial.println(" 3.5x"); + break; + case 2: + Serial.println(" 4.33x"); + break; + case 3: + Serial.println(" 6x"); + break; + case 4: + Serial.println(" 11x"); + break; + default: + Serial.println(" INVALID"); + break; + } + + #endif + + Serial.print("Delay:"); + Serial.println(TRG_DUR); + Serial.print("Error State:"); + Serial.println(ERR_STATE); + Serial.println("------------------"); + #ifndef VERBOSE + } + #endif +} diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_volatile.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_volatile.h new file mode 100644 index 0000000..e266170 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_volatile.h @@ -0,0 +1,48 @@ +// these variables will change on their own. Do not edit ANYTHING below this line +volatile int sensorHReading = 0; // variable to store the value read from the sensor pin +volatile int ADJ_FOLLOW = 0; // Variable for Follower adjustment +volatile int ADJ_COMP = 0; // Variable for Comparator adjustment +volatile int ERR_STATE = 0; + +int Vin = 5000; // input reference voltage in millivolts (multiply V by 1000) +int VOld = 5000; // Variable to store previous cycle's Vin +int VLast = 0; + +// Convert threshold values based on the input voltage + +long senseLong = senseThrs * 1024L; +long compLong = compThrs * 1024L; +long senseInt; +long compInt; + +// Voltage Comparator Adjustment parameters +int VComp = 0; +int diffCompL = VComp - compInt; +int diffCompH = compInt - VComp; + +// Voltage Follower Adjustment parameters +int VAdj = 0; +int diffAdjL = VAdj - senseInt; +int diffAdjH = senseInt - VAdj; + +// Error blink parameters + +int BlinkState = LOW; +int BlinkCount = InitCount * 2; // Multiply Blink count by 2 to handle toggle state + +// Serial Input Parsing Variables +#define buffSize 40 +char inputBuffer[buffSize]; +#define startMarker '<' +#define endMarker '>' +byte bytesRecvd = 0; +bool readInProgress = false; +bool serialIncoming = false; +char serialMessageIn[buffSize] = {0}; +int serialInt = 0; + +//#define LOW 0 +//#define HIGH 1 + +// Task scheduler instances +LightChrono mainLoop; diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/test/README b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html