bool AD5933_Class::getGainFactorsSweep(double cResistance, int avgNum, double *gainFactorArr, double *pShiftArr) { int ctrReg = getByte(0x80); if (setCtrMode(STAND_BY, ctrReg) == false) { return false; } if (setCtrMode(INIT_START_FREQ, ctrReg) == false) { return false; } if (setCtrMode(START_FREQ_SWEEP, ctrReg) == false) { return false; } int t1 = 0; int rImag, rReal; double mag; while ( (getStatusReg() & 0x04) != 0x04) // Loop while if the entire sweep in not complete { double tSumMag = 0, tSumPhase = 0; for (int t2 = 0; t2 < avgNum; t2++) { getComplexRawOnce(rReal, rImag); // Only for real and Imag components. tSumMag += getMag(rReal, rImag); tSumPhase += atan2(rImag, rReal); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } } //Serial.println(t1); gainFactorArr[t1] = (tSumMag / avgNum) * cResistance; pShiftArr[t1] = tSumPhase / avgNum; if (setCtrMode(INCR_FREQ, ctrReg) == false) { return false; } t1++; } if (setCtrMode(POWER_DOWN, ctrReg) == false) { return false; } return true; // Succeed! }
bool AD5933_Class::setCtrMode(byte modetoSet) // setting Control Register to change control mode without assuming control register. (0x80) { return setCtrMode(modetoSet, getByte(0x80)); }
double AD5933_Class::getGainFactor(double cResistance, int avgNum, bool retStandBy) // A function to get Gain Factor. It performs one impedance measurement in start frequency. // double cResistance - Calibration Resistor Value // avgNum - number of measurement for averaging. // Returns -1 if error occurs. { int ctrReg = getByte(0x80); // Get the content of Control Register and put it into ctrReg if (setCtrMode(STAND_BY, ctrReg) == false) { #if LOGGING1 printer->println("getGainFactor - Failed to setting Stand By Status!"); #endif return -1; } if (setCtrMode(INIT_START_FREQ, ctrReg) == false) { #if LOGGING1 printer->println("getGainFactor - Failed to setting initialization with starting frequency!"); #endif return -1; } //delay(delayTimeInit); if (setCtrMode(START_FREQ_SWEEP, ctrReg) == false) { #if LOGGING1 printer->println("getGainFactor - Failed to set to start frequency sweeping!"); #endif return -1; } int t1 = 0; double tSum = 0; while (t1 < avgNum) // Until reached pre-defined number for averaging. { tSum += getMagOnce(); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { #if LOGGING1 printer->println("getGainFactor - Failed to set to repeat this frequency!"); #endif return -1; } t1++; } double mag = tSum / (double)avgNum; #if LOGGING2 printer->print("getGainFactor - Gain Factor: "); printer->println(mag * cResistance); #endif if (retStandBy == false) { #if LOGGING3 printer->println("getGainFactor - terminate the function without going into Stand By"); #endif return mag * cResistance; } if ( setCtrMode(STAND_BY, ctrReg) == false) { #if LOGGING1 printer->println("getGainFactor - Failed to set into Stand-By Status"); #endif return -1; } resetAD5933(); // Gain Factor is different from one of the datasheet in this program. Reciprocal Value. return mag * cResistance; }
bool AD5933_Class::getGainFactorTetra(double calResistance, int avgNum, double &gainFactor, double &vPShift, double &iPShift, bool retStandBy){ int ctrReg = getByte(0x80); // Get the content of Control Register and put it into ctrReg if (setCtrMode(STAND_BY, ctrReg) == false){ return false; } if (setCtrMode(INIT_START_FREQ, ctrReg) == false){ return false; } if (setCtrMode(START_FREQ_SWEEP, ctrReg) == false){ return false; } int index = 0, iVoltage = 0, rVoltage = 0, iCurrent = 0, rCurrent = 0; double voltageSum = 0, currentSum = 0, voltagePhaseSum = 0, currentPhaseSum = 0; delay(100); digitalWrite(IV_MUX, LOW); //Toggle current / voltage multiplexer to voltage delay(100); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } // Until reached pre-defined number for averaging. while (index < avgNum) { getComplexRawOnce(rVoltage, iVoltage); voltageSum += getMag(rVoltage, iVoltage); voltagePhaseSum += atan2(iVoltage, rVoltage); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } index++; } delay(100); digitalWrite(IV_MUX, HIGH); // Toggle current / voltage multiplexer to current delay(100); // delay for mux to settle index = 0; // Reset index for current if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } while (index < avgNum) { getComplexRawOnce(rCurrent, iCurrent); currentSum += getMag(rCurrent, iCurrent); currentPhaseSum += atan2(iCurrent, rCurrent); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } index++; } double voltageMag = voltageSum / ((double) avgNum); double currentMag = currentSum / ((double) avgNum); vPShift = voltagePhaseSum / ((double) avgNum); iPShift = currentPhaseSum / ((double) avgNum); if (retStandBy == false){ gainFactor = calResistance * (currentMag / voltageMag); return true; } if ( setCtrMode(STAND_BY, ctrReg) == false){ return false; } resetAD5933(); gainFactor = calResistance * (currentMag / voltageMag); return true; }
bool AD5933_Class::getGainFactorS_TP(double cResistance, int avgNum, double startFreq, double endFreq, double &GF_Init, double &GF_Incr, double &PS_Init, double &PS_Incr) { int ctrReg = getByte(0x80); byte numIncrementBackup = numIncrement; long incrHexBackup = incrHex; setNumofIncrement(1); setIncrement(endFreq - startFreq); if (setCtrMode(STAND_BY, ctrReg) == false) { return false; } if (setCtrMode(INIT_START_FREQ, ctrReg) == false) { return false; } if (setCtrMode(START_FREQ_SWEEP, ctrReg) == false) { return false; } int t1 = 0; int rImag, rReal; double sumMag = 0, sumPhase = 0; for (t1 = 0; t1 < avgNum; t1++) { getComplexRawOnce(rReal, rImag); // Only for real and Imag components. sumMag += getMag(rReal, rImag); sumPhase += atan2(rImag, rReal); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } } GF_Init = (sumMag / avgNum) * cResistance; PS_Init = sumPhase / avgNum; setCtrMode(INCR_FREQ, ctrReg); sumMag = 0; sumPhase = 0; for (t1 = 0; t1 < avgNum; t1++) { getComplexRawOnce(rReal, rImag); // Only for real and Imag components. sumMag += getMag(rReal, rImag); sumPhase += atan2(rImag, rReal); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } } GF_Incr = ((sumMag / avgNum) * cResistance - GF_Init) / numIncrementBackup; PS_Incr = ((sumPhase / avgNum) - PS_Init) / numIncrementBackup; if (setCtrMode(POWER_DOWN, ctrReg) == false) { return false; } setNumofIncrement(numIncrementBackup); setIncrementinHex(incrHexBackup); return true; // Succeed! }
bool AD5933_Class::performFreqSweep(double gainFactor, double *arrSave) // Function to perform frequency Sweep, Just call it once to do it. It automatically do all the step. // double gainFactor - You need to call getGainFactor(double,int) // // double *arrSave - Just put the name of the array to save it. It should have right number of entries to save it. // If not, hidden error will be occur. { int ctrReg = getByte(0x80); // Get the content of Control Register and put it into ctrReg if (setCtrMode(STAND_BY, ctrReg) == false) { #if LOGGING1 printer->println("performFreqSweep - Failed to setting Stand By Status!"); #endif return false; } if (setCtrMode(INIT_START_FREQ, ctrReg) == false) { #if LOGGING1 printer->println("performFreqSweep - Failed to setting initialization with starting frequency!"); #endif return false; } //delay(delayTimeInit); if (setCtrMode(START_FREQ_SWEEP, ctrReg) == false) { #if LOGGING1 printer->println("performFreqSweep - Failed to set to start frequency sweeping!"); #endif return false; } int t1 = 0; while ( (getStatusReg() & 0x04) != 0x04 ) // Loop while if the entire sweep in not complete { //delay(delayTimeInit); arrSave[t1] = gainFactor / getMagOnce(); // Calculated with Gain Factor #if LOGGING1 printer->print("performFreqSweep - arrSave["); printer->print(t1); printer->print("] = "); printer->println(arrSave[t1]); #endif if (setCtrMode(INCR_FREQ, ctrReg) == false) { #if LOGGING1 printer->println("performFreqSweep - Failed to set for increasing frequency!"); #endif return false; } t1++; //getByte(0x80); } if (setCtrMode(POWER_DOWN, ctrReg) == false) { #if LOGGING1 printer->println("performFreqSweep - Completed sweep, but failed to power down"); #endif return false; } return true; // Succeed! }
bool AD5933_Class::setCtrMode(byte modetoSet) { return setCtrMode(modetoSet, getByte(0x80)); }
bool AD5933_Class::getGainFactorsTetraSweep(double cResistance, int avgNum, double *gainFactorArr, double *vShiftArr, double *cShiftArr){ int ctrReg = getByte(0x80); if (setCtrMode(STAND_BY, ctrReg) == false) { return false; } if (setCtrMode(INIT_START_FREQ, ctrReg) == false) { return false; } if (setCtrMode(START_FREQ_SWEEP, ctrReg) == false) { return false; } int arrayIndex = 0; while ( (getStatusReg() & 0x04) != 0x04) { // While the fequency sweep isn't complete ////// int averageIndex = 0, iVoltage = 0, rVoltage = 0, iCurrent = 0, rCurrent = 0; double voltageSum = 0, currentSum = 0, voltagePhaseSum = 0, currentPhaseSum = 0; digitalWrite(IV_MUX, LOW); //Toggle current / voltage multiplexer to voltage delay(10); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } // Until reached pre-defined number for averaging. while (averageIndex < avgNum) { getComplexRawOnce(rVoltage, iVoltage); voltageSum += getMag(rVoltage, iVoltage); voltagePhaseSum += atan2(iVoltage, rVoltage); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } averageIndex++; } digitalWrite(IV_MUX, HIGH); // Toggle current / voltage multiplexer to current delay(10); // delay for mux to settle averageIndex = 0; // Reset index for current if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } while (averageIndex < avgNum) { getComplexRawOnce(rCurrent, iCurrent); currentSum += getMag(rCurrent, iCurrent); currentPhaseSum += atan2(iCurrent, rCurrent); if (setCtrMode(REPEAT_FREQ, ctrReg) == false) { return false; } averageIndex++; } double voltageMag = voltageSum / ((double) avgNum); double currentMag = currentSum / ((double) avgNum); gainFactorArr[arrayIndex] = cResistance * (currentMag / voltageMag); vShiftArr[arrayIndex] = voltagePhaseSum / ((double) avgNum); cShiftArr[arrayIndex] = currentPhaseSum / ((double) avgNum); ////// if (setCtrMode(INCR_FREQ, ctrReg) == false){ return false; } arrayIndex++; } if (setCtrMode(POWER_DOWN, ctrReg) == false){ return false; } return true; // Succeed! }