K22F Power Conditioner
Dependencies: NetworkServices W5500Interface
main.cpp
- Committer:
- amccartney
- Date:
- 2017-06-13
- Revision:
- 1:f857c10c3cd2
- Parent:
- 0:9d0945119ba4
- Child:
- 2:ab7e8b9c7903
File content as of revision 1:f857c10c3cd2:
#include "mbed.h" #include "config.h" #define READBUFFERSIZE (32) RawSerial pc(USBTX, USBRX); DigitalIn sw2(SW2); DigitalIn sw3(SW3); InterruptIn zeroCross(A2);//Arduino A2 Timer halfCycle; Ticker freqGen; Ticker sampler; DigitalOut do1(D7);//Arduino D7 DigitalOut do2(D6);//Arduino D6 AnalogIn Vrms(A0); AnalogIn Arms(A1); //LocalFileSystem local("local"); //FlashIAP flash; int flip = 0; int halfPeriod = 16600; float freq = 60; float period = 0; double setFreq = 50.00; double periodFreqGen; float Vsample[32]; float Asample[32]; int sample = 0; int sampleMax = 0; float Vhold[32]; float Ahold[32]; int i; int j; int cycleFlag = 0; double VsumSq = 0; double Vmean = 0; double Vroot = 0; double AsumSq = 0; double Amean = 0; double Aroot = 0; char tempBuffer[20]; char buffer[20]; char k = 0; char c; void rxCallback() { do1 = 1; c = pc.getc(); do1 = 0; tempBuffer[k] = c; k++; do1 = 1; if ( c == '\n' ) { k = 0; strncpy(buffer,tempBuffer,sizeof(tempBuffer)); memset(tempBuffer,0,sizeof(tempBuffer)); } //pc.printf("\n%s\n", &buffer); //echo back to terminal do1 = 0; } void sampleTimer() { do2 = 1; Asample[sample] = ( 1 / 0.34286 ) * 3.3 * Arms.read(); Vsample[sample] = 64.7519 * 3.3 * Vrms.read(); sample++; if ( sample == 33 ) //In case there is no zero cross signal { sample = 0; cycleFlag = 1; } do2 = 0; } void startMeasFreq() { halfCycle.start(); sampler.detach(); sampler.attach(&sampleTimer, (periodFreqGen / 32) ); for (j = 0; j < 32; j++) { Vhold[j] = Vsample[j]; Ahold[j] = Asample[j]; } cycleFlag = 1; sampleMax = sample; sample = 0; //Reset the sample counter } void stopMeasFreq() { halfPeriod = ( halfPeriod + halfCycle.read_us() ) /2; halfCycle.stop(); halfCycle.reset(); } int main() { float VA; int PercentLoad = 0; //NVIC_SetPriority(PORTC_IRQn, 2); pc.baud(115200); pc.printf("\n\nFRDM-K22F Power Conditioner.\n\n"); do1 = 0; do2 = 0; while(pc.readable()) { //char e = pc.getc(); } memset(buffer,0,sizeof(buffer)); wait(2.0); periodFreqGen = ( ( 1 / freq ) ); //pc.printf("periodFreqGen: %f\n",periodFreqGen); sampler.attach(&sampleTimer, (periodFreqGen / 32) ); zeroCross.fall(&startMeasFreq); zeroCross.rise(&stopMeasFreq); pc.attach(&rxCallback,pc.RxIrq);//Serial::RxIrq); while(1) { period = halfPeriod * 2; freq = 0.5 * ( 1000000 / halfPeriod ); if (cycleFlag == 1) { for (i=0; i<32; i++) { VsumSq = VsumSq + (Vhold[i] * Vhold[i]); AsumSq = AsumSq + (Ahold[i] * Ahold[i]); } Vmean = VsumSq / 32; Amean = AsumSq / 32; Vroot = ( Vroot + sqrt(Vmean) ) / 2; Aroot = ( Aroot + sqrt(Amean) ) / 2; VA = Vroot * Aroot; PercentLoad = (int)(100 * ( VA / NOMVA )); VsumSq = 0; AsumSq = 0; cycleFlag = 0; } if (strncmp (buffer,"PID.FORMAT",10) == 0) { pc.printf("PID.FORMAT=PROT;VER;UID;BAT;BTM;INP;OUT;ALM;TST;SET;PDU;SYS;OEM;BUZ\n"); memset(buffer,0,sizeof(buffer)); } else if (strncmp (buffer,"PID",3) == 0) { pc.printf("PID=CUSPP;1.13;1;0;0;0;1;0;0;0;0;1;0;0\n"); memset(buffer,0,sizeof(buffer)); } else if (strncmp (buffer,"UID",3) == 0) { pc.printf("UID=AMETEK-POWERVAR;%s;%s;ABC;;PowerCond;%s;20150715\n", &MODEL, &MODEL, &SERIALNO); memset(buffer,0,sizeof(buffer)); } else if (strncmp (buffer,"SYS.FORMAT",10) == 0) { pc.printf("SYS.FORMAT=TOPLGY.N;INVOLT.N;INFRQ.N;INPHS.N;OUTVLT.N;OUTFRQ.N;OUTVA.N;OUTPWR.N;OUTPHS.N;HITEMP.W;OUTQTY.N;OVRLOD.W\n"); memset(buffer,0,sizeof(buffer)); } else if (strncmp (buffer,"SYS",3) == 0) { pc.printf("SYS=0;%d;%d;1;%d;%d.%d;%d;1;35;1;100\n", NOMV, NOMF, NOMV, NOMF, NOMVA, NOMVA); memset(buffer,0,sizeof(buffer)); } else if (strncmp (buffer,"OUT.FORMAT",10) == 0) { pc.printf("OUT.FORMAT=SOURCE.N;FREQ.N;VOLT.N;AMP.N;PWR.N;PERCNT.N\n"); memset(buffer,0,sizeof(buffer)); } else if (strncmp (buffer,"OUT",3) == 0) { pc.printf("OUT=0;%3.1f;%3.1f;%3.1f:%.0f;%d\n", freq, Vroot, Aroot, VA, PercentLoad); memset(buffer,0,sizeof(buffer)); } else if ((buffer[0]) != 0) { pc.printf("?\n"); memset(buffer,0,sizeof(buffer)); } wait(1.0); } }