fixed some ordering causing definition issues
- started on https://github.com/pyr0ball/pyr0piezo/issues/30
This commit is contained in:
parent
4b2186aaf7
commit
1d098f184b
5 changed files with 153 additions and 147 deletions
|
|
@ -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 <Wire.h>
|
||||
#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 <PinChangeInterrupt.h>
|
||||
//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();
|
||||
|
|
|
|||
|
|
@ -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 <PinChangeInterrupt.h>
|
||||
//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;
|
||||
}
|
||||
}
|
||||
/*------------------------------------------------*/
|
||||
/*------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------*/
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------*/
|
||||
|
|
@ -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("------------------");
|
||||
}
|
||||
}
|
||||
54
firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h
Normal file
54
firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h
Normal file
|
|
@ -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("------------------");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue