From 1d098f184b58a3e85e43869e83ca12cdbbb6497c Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Wed, 26 Jun 2019 10:40:02 -0700 Subject: [PATCH] fixed some ordering causing definition issues - started on https://github.com/pyr0ball/pyr0piezo/issues/30 --- .../Pyr0_Piezo_Sensor_v2.x.x.ino | 77 ++++++++---- .../{pP_functions.cpp => pP_functions.h} | 113 ++++++++---------- .../{pP_i2c.cpp => pP_i2c.h} | 0 .../Pyr0_Piezo_Sensor_v2.x.x/pP_serial.cpp | 56 --------- .../Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h | 54 +++++++++ 5 files changed, 153 insertions(+), 147 deletions(-) rename firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/{pP_functions.cpp => pP_functions.h} (79%) rename firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/{pP_i2c.cpp => pP_i2c.h} (100%) delete mode 100644 firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.cpp create mode 100644 firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino index 3ee495d..3c0143f 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino @@ -56,27 +56,10 @@ The gain STATE is representative of these values: 4 = 11x */ -// Debug output toggle. Uncomment to enable -#define DEBUG true - -#include "pP_serial.cpp" - -// i2c input toggle. Uncomment to enable -//#define I2C true -#ifdef I2C - #include - #include "pP_i2c.cpp" -#endif - -// Headers, variables, and functions -#include "pP_pins.h" -#include "pP_volatile.h" -#include "pP_functions.cpp" - // Configurable settings: int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x #define InitCount 6 // Number of times to blink the LED on start -int LOOP_DUR = 100; // duration of time between ADC checks and other loop functions +int LOOP_DUR = 50; // duration of time between ADC checks and other loop functions int TRG_DUR = 20; // duration of the Z-axis pulse sent, in ms #define senseThrs 1.85 #define compThrs 2.54 @@ -84,6 +67,46 @@ int TRG_DUR = 20; // duration of the Z-axis pulse sent, in ms int Hyst = 20; // Hysteresis value for ADC measurements #define Vin 5 // input reference voltage +/*------------------------------------------------------------*/ + +// Debug output toggle. Uncomment to enable +#define DEBUG true + +// Headers, variables, and functions +#include "pP_pins.h" +#include "pP_volatile.h" +#include "pP_functions.h" +#include "pP_serial.h" + +// i2c input toggle. Uncomment to enable +//#define I2C true +#ifdef I2C + #include "pP_i2c.h" +#endif + +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 break pull to ground + pinMode(GADJ_R1, INPUT); // declare input to break pull to ground + pinMode(GADJ_R2, INPUT); // declare input to break pull to ground + pinMode(GADJ_R3, INPUT); // declare input to break pull to ground + Serial.begin(9600); + + // Uncomment the following lines to use PCInt pins instead of hardware interrupt + //#include + //attachPCINT(digitalPinToPCINT(Z_TRG), pulse, FALLING); + + // Uncomment the followoing line to use hardware interrupt pin + attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING); + + Serial.println("Initializing Pyr0-Piezo Sensor..."); + +} + /*------------------------------------------------*/ @@ -106,22 +129,26 @@ void loop() { // Check voltage of first and second stages and compare against thresholds VComp = analogRead(VCOMP_SENSE_PIN); - diffCompL = ((VComp - compInt) / 4) - Hyst; - diffCompH = ((compInt - VComp) / 4) - Hyst; + diffCompL = (VComp - compInt) - Hyst; + diffCompH = (compInt - VComp) - Hyst; VAdj = analogRead(V_FOLLOW_PIN); - diffAdjL = ((VAdj - senseInt) / 4) - Hyst; - diffAdjH = ((senseInt - VAdj) / 4) - Hyst; + diffAdjL = (VAdj - senseInt) - Hyst; + diffAdjH = (senseInt - VAdj) - Hyst; // Set the amplification gain factor adjustGain(); // Voltage Follower adjustment - adjustFollow(); - + if (diffAdjL > 0 || diffAdjH > 0) { + adjustFollow(); + } + // Voltage Comparator adjustment - adjustComp(); + if (diffCompL > 0 || diffCompH > 0) { + adjustComp(); + } // Alert the user that auto-calibration is ongoing calibrateAlert(); diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_functions.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_functions.h similarity index 79% rename from firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_functions.cpp rename to firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_functions.h index b7c7038..4c14c42 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_functions.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_functions.h @@ -1,25 +1,3 @@ -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 break pull to ground - pinMode(GADJ_R1, INPUT); // declare input to break pull to ground - pinMode(GADJ_R2, INPUT); // declare input to break pull to ground - pinMode(GADJ_R3, INPUT); // declare input to break pull to ground - Serial.begin(9600); - - // Uncomment the following lines to use PCInt pins instead of hardware interrupt - //#include - //attachPCINT(digitalPinToPCINT(Z_TRG), pulse, FALLING); - - // Uncomment the followoing line to use hardware interrupt pin - attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING); - - Serial.println("Initializing Pyr0-Piezo Sensor..."); - -} /*------------------------------------------------*/ @@ -36,11 +14,11 @@ void adjustFollow() { /* Compares diffs of threshold vs read value if positive, adjusts the follower to within the range set above*/ - if (diffAdjL > 0.0) { - ADJ_FOLLOW += diffAdjL; + if (diffAdjL > 0) { + ADJ_FOLLOW += diffAdjL / 12; } - if (diffAdjH > 0.0) { - ADJ_FOLLOW -= diffAdjH; + if (diffAdjH > 0) { + ADJ_FOLLOW -= diffAdjH / 12; } // Analog output (PWM) of duty cycle @@ -50,11 +28,11 @@ void adjustFollow() { /*------------------------------------------------*/ void adjustComp() { - if (diffCompL > 0.0) { - ADJ_COMP += diffCompL; + if (diffCompL > 0) { + ADJ_COMP += diffCompL / 12; } - if (diffCompH > 0.0) { - ADJ_COMP -= diffCompH; + if (diffCompH > 0) { + ADJ_COMP -= diffCompH / 12; } analogWrite(VCOMP_PWM, ADJ_COMP); @@ -120,6 +98,24 @@ void checkError () { /*------------------------------------------------*/ +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 + + strtokIndx = strtok(NULL, ","); + serialFloat = atof(strtokIndx); // convert this part to a float + +} +/*------------------------------------------------*/ + void identifyMarkers() { char x = Serial.read(); @@ -169,41 +165,6 @@ void identifyMarkers() { /*------------------------------------------------*/ -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 - - strtokIndx = strtok(NULL, ","); - serialFloat = atof(strtokIndx); // convert this part to a float - -} - -/*------------------------------------------------*/ - -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(); - } -} -/*------------------------------------------------*/ - void updateTrigDuration() { if (serialInt >= 0) { TRG_DUR = serialInt; @@ -239,4 +200,24 @@ void updateHysteresis() { Hyst = serialInt; } } -/*------------------------------------------------*/ \ No newline at end of file +/*------------------------------------------------*/ + +/*------------------------------------------------*/ + +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(); + } +} + + +/*------------------------------------------------*/ diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.h similarity index 100% rename from firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.cpp rename to firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_i2c.h diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.cpp deleted file mode 100644 index 2fe76af..0000000 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/*------------------------------------------------*/ - -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() { - if (serialIncoming) { - serialIncoming = false; - #ifdef DEBUG - Serial.print("Comp Reference:"); - Serial.print(VComp); - Serial.print(" "); - Serial.print("Comparator State:"); - Serial.print(ADJ_COMP); - Serial.print(" "); - Serial.println(compInt); - - Serial.print("Diff"); - Serial.print(" "); - Serial.print(diffCompL); - Serial.print(" "); - Serial.println(diffCompH); - - Serial.print("Amp Sense:"); - Serial.print(VAdj); - Serial.print(" "); - Serial.print("Follower State:"); - Serial.print(ADJ_FOLLOW); - Serial.print(" "); - Serial.println(senseInt); - - Serial.print("Diff"); - Serial.print(" "); - Serial.print(diffAdjL); - Serial.print(" "); - Serial.println(diffAdjH); - #endif - - Serial.print("Delay:"); - Serial.println(TRG_DUR); - Serial.print("Error State:"); - Serial.println(ERR_STATE); - Serial.println("------------------"); - } -} diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h new file mode 100644 index 0000000..1761435 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h @@ -0,0 +1,54 @@ +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() { + if (serialIncoming) { + serialIncoming = false; + #ifdef DEBUG + Serial.print("Comp Reference:"); + Serial.print(VComp); + Serial.print(" "); + Serial.print("Comparator State:"); + Serial.print(ADJ_COMP); + Serial.print(" "); + Serial.println(compInt); + + Serial.print("Diff"); + Serial.print(" "); + Serial.print(diffCompL); + Serial.print(" "); + Serial.println(diffCompH); + + Serial.print("Amp Sense:"); + Serial.print(VAdj); + Serial.print(" "); + Serial.print("Follower State:"); + Serial.print(ADJ_FOLLOW); + Serial.print(" "); + Serial.println(senseInt); + + Serial.print("Diff"); + Serial.print(" "); + Serial.print(diffAdjL); + Serial.print(" "); + Serial.println(diffAdjH); + #endif + + Serial.print("Delay:"); + Serial.println(TRG_DUR); + Serial.print("Error State:"); + Serial.println(ERR_STATE); + Serial.println("------------------"); + } +}