Fixes for oscillating PWM output

- Added hysteresis input for UART/I2C
- Fixed example to show correct command (VADJ vs VADJH)
- added TRG_DUR to the interrupt function
- Migrated math for 8-10bit ADC to a different function
- Organized/added debug serial output
- fixed line begin whitespace
This commit is contained in:
pyr0ball 2019-04-22 15:32:26 -07:00
parent 5ff215c0da
commit 5a6d5ca7db

View file

@ -33,10 +33,10 @@
To change trigger active duration: TRG_D [integer for milliseconds]
To change gain factor: GAIN_F [integer for gain state - see note*]
To change sensor input pullup vRef high threshold: VADJH [float value]
To change sensor input pullup vRef low threshold: VADJL [float value]
To change comparator trigger high threshold: VCOMPH [float value]
To change comparator trigger high threshold: VCOMPL [float value]
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:
<CMD, INT, FLOAT>
@ -45,7 +45,7 @@ You must include the unused variable for each instance.
Examples:
<GAIN_F, 3, 0.00>
<VADJH, 0, 2.35>
<VADJ, 0, 2.35>
*Note for Gain Factor:
The gain STATE is representative of these values:
@ -62,13 +62,13 @@ The gain STATE is representative of these values:
int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
int InitCount = 6; // Number of times to blink the LED on start
int TRG_DUR = 120; // duration of the Z-axis pulse sent, in ms
#define senseThrs 2.15
#define senseThrs 2.45
//float senseHighThrs = 2.35; // Upper threshold of Voltage Follower before adjustment
//float senseLowThrs = 1.8; // Lower threshold of Voltage Follower before adjustment
#define compThrs 2.75
#define compThrs 3.15
//float compHighThrs = 2.75; // Upper threshold of Comparator before adjustment
//float compLowThrs = 2.54; // Lower threshold of Comparator before adjustment
int Hyst = 20; // Hysteresis value for ADC measurements
int Hyst = 17; // Hysteresis value for ADC measurements
int Vin = 5; // input reference voltage
// Analog Pin Assignments
@ -163,6 +163,7 @@ void setup() {
void pulse() {
digitalWrite(TRG_OUT, LOW);
sensorHReading = 1;
delay(TRG_DUR);
}
/*------------------------------------------------*/
@ -172,10 +173,10 @@ void adjustFollow() {
if positive, adjusts the follower to within
the range set above*/
if (diffAdjL > 0.0) {
ADJ_FOLLOW += (diffAdjL / 4) ;
ADJ_FOLLOW += diffAdjL;
}
if (diffAdjH > 0.0) {
ADJ_FOLLOW -= (diffAdjH / 4);
ADJ_FOLLOW -= diffAdjH;
}
// Analog output (PWM) of duty cycle
@ -186,11 +187,11 @@ void adjustFollow() {
void adjustComp() {
if (diffCompL > 0.0) {
ADJ_COMP += (diffCompL / 4);
ADJ_COMP += diffCompL;
}
if (diffCompH > 0.0) {
ADJ_COMP -= (diffCompH / 4);
ADJ_COMP -= diffCompH;
}
analogWrite(VCOMP_PWM, ADJ_COMP);
@ -376,6 +377,9 @@ void updateParams() {
//if (strcmp(serialMessageIn, "VADJL") == 0) {
// updateVAdjL();
//}
if (strcmp(serialMessageIn, "HYST") == 0) {
updateHysteresis();
}
}
/*------------------------------------------------*/
@ -435,23 +439,45 @@ void updateVAdjL() {
}
*------------------------------------------------*/
void updateHysteresis() {
if (serialInt >= 0) {
Hyst = serialInt;
}
}
/*------------------------------------------------*/
void serialReply() {
if (serialIncoming) {
serialIncoming = false;
Serial.print("PZ Status:");
Serial.println(sensorHReading);
Serial.print("Voltage Reference:");
Serial.print("Comp Reference:");
Serial.print(VComp);
Serial.print(" ");
//Serial.println(VCompRef,2);
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.println(vAdjRead,2);
Serial.print("Comparator State:");
Serial.println(ADJ_COMP);
Serial.print("Follower State:");
Serial.println(ADJ_FOLLOW);
Serial.print(ADJ_FOLLOW);
Serial.print(" ");
Serial.println(senseInt);
Serial.print("Diff");
Serial.print(" ");
Serial.print(diffAdjL);
Serial.print(" ");
Serial.println(diffAdjH);
Serial.print("Delay:");
Serial.println(TRG_DUR);
Serial.print("Error State:");
@ -488,15 +514,15 @@ void loop() {
// Check voltage of first and second stages and compare against thresholds
VComp = analogRead(VCOMP_SENSE_PIN);
diffCompL = (VComp - compInt) - Hyst;
diffCompH = (compInt - VComp) - Hyst;
diffCompL = ((VComp - compInt) / 4) - Hyst;
diffCompH = ((compInt - VComp) / 4) - Hyst;
//diffCompL = VComp - compLowInt;
//diffCompH = compHighInt - VComp;
//VCompRef = (VComp * 5) / 1024;
VAdj = analogRead(V_FOLLOW_PIN);
diffAdjL = (VAdj - senseInt) - Hyst;
diffAdjH = (senseInt - VAdj) - Hyst;
diffAdjL = ((VAdj - senseInt) / 4) - Hyst;
diffAdjH = ((senseInt - VAdj) / 4) - Hyst;
//diffAdjL = VAdj - senseLowInt;
//diffAdjH = senseHighInt - VAdj;
//vAdjRead = (VAdj * 5) / 1024;