fixed unary operators for adjustment logic

- Switched unary operators to correct syntax (+= vs =+) Should fix oscillation noted in https://github.com/pyr0ball/pyr0piezo/issues/24
- Added software hysteresis to the adjustment function. Referenced in https://github.com/pyr0ball/pyr0piezo/issues/24
- Fixed notes to show the correct gain factors
This commit is contained in:
pyr0ball 2019-04-21 23:02:16 -07:00
parent 856ecafad2
commit 95ea64a822

View file

@ -56,10 +56,10 @@ The gain STATE is representative of these values:
4 = 11x 4 = 11x
*/ */
#include <Wire.h> //#include <Wire.h>
// Set variables for working parameters // Set variables for working parameters
int GAIN_FACTOR = 2; // Gain adjustment factor. 0=2x, 1=2.5x, 2=3.33x, 3=5x, 4=10x 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 InitCount = 6; // Number of times to blink the LED on start
int TRG_DUR = 120; // duration of the Z-axis pulse sent, in ms int TRG_DUR = 120; // duration of the Z-axis pulse sent, in ms
float senseThrs = 2.15; float senseThrs = 2.15;
@ -68,6 +68,7 @@ float senseThrs = 2.15;
float compThrs = 2.75; float compThrs = 2.75;
//float compHighThrs = 2.75; // Upper threshold of Comparator before adjustment //float compHighThrs = 2.75; // Upper threshold of Comparator before adjustment
//float compLowThrs = 2.54; // Lower threshold of Comparator before adjustment //float compLowThrs = 2.54; // Lower threshold of Comparator before adjustment
int Hyst = 20; // Hysteresis value for ADC measurements
int Vin = 5; // input reference voltage int Vin = 5; // input reference voltage
// Analog Pin Assignments // Analog Pin Assignments
@ -171,10 +172,10 @@ void adjustFollow() {
if positive, adjusts the follower to within if positive, adjusts the follower to within
the range set above*/ the range set above*/
if (diffAdjL > 0.0) { if (diffAdjL > 0.0) {
ADJ_FOLLOW =+ (diffAdjL / 4); ADJ_FOLLOW += (diffAdjL / 4) ;
} }
if (diffAdjH > 0.0) { if (diffAdjH > 0.0) {
ADJ_FOLLOW =- (diffAdjH / 4); ADJ_FOLLOW -= (diffAdjH / 4);
} }
// Analog output (PWM) of duty cycle // Analog output (PWM) of duty cycle
@ -185,11 +186,11 @@ void adjustFollow() {
void adjustComp() { void adjustComp() {
if (diffCompL > 0.0) { if (diffCompL > 0.0) {
ADJ_COMP =+ (diffCompL / 4); ADJ_COMP += (diffCompL / 4);
} }
if (diffCompH > 0.0) { if (diffCompH > 0.0) {
ADJ_COMP =- (diffCompH / 4); ADJ_COMP -= (diffCompH / 4);
} }
analogWrite(VCOMP_PWM, ADJ_COMP); analogWrite(VCOMP_PWM, ADJ_COMP);
@ -269,7 +270,7 @@ void serialInput() {
/*------------------------------------------------*/ /*------------------------------------------------*/
void i2cInput() { /* void i2cInput() {
// receive data from Serial and save it into inputBuffer // receive data from Serial and save it into inputBuffer
@ -279,13 +280,13 @@ void i2cInput() {
i2cReply(); i2cReply();
} }
} }
*/
/*------------------------------------------------*/ /*------------------------------------------------*/
void identifyMarkers() { void identifyMarkers() {
char x = Serial.read(); char x = Serial.read();
char y = Wire.read(); // char y = Wire.read();
if (x == endMarker) { if (x == endMarker) {
readInProgress = false; readInProgress = false;
@ -307,7 +308,7 @@ void identifyMarkers() {
readInProgress = true; readInProgress = true;
} }
if (y == endMarker) { /* if (y == endMarker) {
readInProgress = false; readInProgress = false;
serialIncoming = true; serialIncoming = true;
inputBuffer[bytesRecvd] = 0; inputBuffer[bytesRecvd] = 0;
@ -326,6 +327,7 @@ void identifyMarkers() {
bytesRecvd = 0; bytesRecvd = 0;
readInProgress = true; readInProgress = true;
} }
*/
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
@ -458,12 +460,13 @@ void serialReply() {
} }
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
/*
void i2cReply() { void i2cReply() {
if (serialIncoming) { if (serialIncoming) {
Wire.write("OK"); Wire.write("OK");
} }
} }
*/
/*------------------------------------------------*/ /*------------------------------------------------*/
void loop() { void loop() {
@ -485,15 +488,15 @@ void loop() {
// Check voltage of first and second stages and compare against thresholds // Check voltage of first and second stages and compare against thresholds
VComp = analogRead(VCOMP_SENSE_PIN); VComp = analogRead(VCOMP_SENSE_PIN);
diffCompL = VComp - compInt; diffCompL = (VComp - compInt) - Hyst;
diffCompH = compInt - VComp; diffCompH = (compInt - VComp) - Hyst;
//diffCompL = VComp - compLowInt; //diffCompL = VComp - compLowInt;
//diffCompH = compHighInt - VComp; //diffCompH = compHighInt - VComp;
VCompRef = (VComp * 5) / 1024; VCompRef = (VComp * 5) / 1024;
VAdj = analogRead(V_FOLLOW_PIN); VAdj = analogRead(V_FOLLOW_PIN);
diffAdjL = VAdj - senseInt; diffAdjL = (VAdj - senseInt) - Hyst;
diffAdjH = senseInt - VAdj; diffAdjH = (senseInt - VAdj) - Hyst;
//diffAdjL = VAdj - senseLowInt; //diffAdjL = VAdj - senseLowInt;
//diffAdjH = senseHighInt - VAdj; //diffAdjH = senseHighInt - VAdj;
vAdjRead = (VAdj * 5) / 1024; vAdjRead = (VAdj * 5) / 1024;