Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:610dc7763656
- Parent:
- 0:ce79b04fc2ca
- Child:
- 2:f2583b56777e
--- a/main.cpp Thu Jul 16 21:37:20 2015 +0000 +++ b/main.cpp Thu Jul 23 18:39:24 2015 +0000 @@ -1,8 +1,10 @@ #include "mbed.h" +//#include "rtos.h" + //Declare hardware inputs Timer timer; AnalogIn potSet(A0); -AnalogIn pressure(A1); +AnalogIn pressureDAQZO(A1); PwmOut pumpStep(D8); DigitalOut dir(D9); DigitalOut visDir(LED2); @@ -11,15 +13,22 @@ PwmOut b(LED_BLUE); //Declare globals -unsigned long lastTime; -float inputTemperature, outputCmd, iTerm; -float kp=2; +char bufferIn[10]; +char bufferOut[8]; +char hexCCIn[2]; +char hexDDDDIn[4]; +int decCCIn; +int decDDDDIn; + +unsigned long lastTime; //start it at 0, so at least one run of computePressureCommand is guaranteed +float outputCmd, iTerm; +float kp=2/9.9; float ki=0; -float setPoint; +float setPoint=0.000f; int sampleTime = 250; //1/4 sec float outMin=-1.0; float outMax=1.0; -float smallChange=0.01; +float smallChange=0.01*9.9/5; float oldOutputCmd; //dont need bool runPump=false; @@ -42,10 +51,11 @@ b=1; } + int hexToDec(char hex[]) { int decValue = strtol(hex, NULL, 16); - + return decValue; } @@ -65,63 +75,138 @@ void computePressureCommand() { - int i; - unsigned int now = timer.read_ms(); + unsigned int now = timer.read_ms(); int nSamplesToAverage=10; int timeChange = (now - lastTime); if(timeChange>=sampleTime) { - - inputTemperature=0; - for (i=0; i<nSamplesToAverage; i++) { - inputTemperature = inputTemperature+pressure; + + float avePressureDAQZO=0; + for (int i=0; i<nSamplesToAverage; i++) { + avePressureDAQZO = avePressureDAQZO+pressureDAQZO; } - inputTemperature=inputTemperature/(double)(nSamplesToAverage); + avePressureDAQZO=avePressureDAQZO/(double)(nSamplesToAverage); //setPoint = potSet; - - float error = setPoint - inputTemperature; - + float inputPressure=avePressureDAQZO*9.9; + + float error = setPoint - inputPressure; + iTerm+= (ki * error); if(iTerm > outMax) iTerm= outMax; else if(iTerm < outMin) iTerm= outMin; - + outputCmd = kp * error + iTerm; if(outputCmd > outMax) outputCmd = outMax; else if(outputCmd < outMin) outputCmd = outMin; - + visDir=0; if (outputCmd < 0) { dir=1; visDir=1; outputCmd = outputCmd * -1.0; - } - else dir=0; - + } else dir=0; + if (abs(error) > smallChange) { pumpStep.write(0.50f); // turn back on - pumpStep.period_us((1.0/outputCmd)*200); + pumpStep.period_us((1.0/outputCmd)*200); + } else pumpStep.write(0.0f); //shut off steps for too small change + + + char signSetPoint='+'; + if(setPoint<0) { + signSetPoint='-'; } - else pumpStep.write(0.0f); //shut off steps for too small change - - printf("%f, %f \n\r ",setPoint, inputTemperature); - //printf("%f, %f, %f, %f, %f, %f \n\r ",setPoint, inputTemperature, outputCmd, kp*error, iTerm, (-0.5-dir.read())); - + char signinputVoltage='+'; + if(setPoint<0) { + signinputVoltage='-'; + } + + + pc.printf("%s%c%2.3f%s%c%2.3f%s","#SP",signSetPoint,abs(setPoint),"MP",signinputVoltage,abs(inputPressure),"SS^"); + //printf("%f, %f, %f, %f, %f, %f \n\r ",setPoint, inputVoltage, outputCmd, kp*error, iTerm, (-0.5-dir.read())); + ledConfirmSent(); oldOutputCmd=outputCmd; //don't need lastTime = now; /*Remember time for next time*/ - }//end if timeChange + }//end if timeChange + }//end computePressureVoltage +void runWhenNewSerialIn() +{ + + + + bufferOut[0]=bufferIn[0]; + + for(int i=0; i<7; i++) { // duplicate bytes 0 to 9 for now; + bufferOut[i]=bufferIn[i]; + } + bufferOut[7]='^';//IMPORTANT! + + + //get hexCCIn + for(int i=0; i<2; i++) { + hexCCIn[i]=bufferIn[i+1]; + } + //pc.printf("hexCCIn= is %s\r\n", hexCCIn); + + for(int i=0; i<4; i++) { + hexDDDDIn[i]=bufferIn[i+3]; + } + + //pc.printf("hello test= is %s\r\n", test); + + //int value = strtol(hexDDDDIn, NULL, 16); + decCCIn = hexToDec(hexCCIn); + decDDDDIn = hexToDec(hexDDDDIn); + //int decDDDDIn = hexToDec("0000"); + + //Make sure incoming checksum pans out: + + //Do action based on CC + + //pc.printf("hexCCIn= is %c%c\r\n", hexCCIn[0],hexCCIn[1]); + //pc.printf("hexDDDDIn= is %c%c%c%c\r\n", hexDDDDIn[0],hexDDDDIn[1],hexDDDDIn[2],hexDDDDIn[3]); + //pc.printf("decCCIn= is %d\r\n", decCCIn); + //pc.printf("decDDDDIn= is %d\r\n", decDDDDIn); + if (decCCIn==1) { //control built in LED + //PwmOut led(LED_RED); //set to red just for now, change later if nec. + //Get desired intensity + double ledIntensity=(double)(hexToDecSub(bufferIn, 4, 5)); + if (bufferIn[3]=='0') { //red + //PwmOut r(LED_RED); + r=(255-ledIntensity)/255; + } else if(bufferIn[3]=='1') { + //PwmOut g(LED_GREEN); + g=(255-ledIntensity)/255; + } else if (bufferIn[3]=='2') { + //PwmOut led(LED_BLUE); + b=(255-ledIntensity)/255; + } + + pc.printf("%s", bufferOut); + + } else if(decCCIn==27) { //CC=1b, Enable pump + pumpStep.write(0.50f); // set fixed duty cycle to 50% (hold position?) + ledConfirmSent(); + runPump=true; + //pc.printf("%s\n", bufferOut); //No output SP & MP instead via computePressureCommand + ledConfirmSent(); + + } else if(decCCIn==28) { //CC=1c, Define new pressure setPoint + setPoint=((double)(decDDDDIn))/100; //converted fron hex above + + } else if (decCCIn==29) {//CC=1d, Deactivate pump + pumpStep.write(0.0f); + ledConfirmSent(); + runPump=false; + pc.printf("%s", bufferOut); + ledConfirmSent(); + } +} int main() { - - //int testInt=0; - - char bufferIn[10]; - char bufferOut[8]; - char hexCCIn[2]; - char hexDDDDIn[4]; - r.period(0.001f); g.period(0.001f); b.period(0.001f); @@ -129,93 +214,24 @@ r=1; g=1; b=1; + + //pc.printf("\r\n"); //Important + timer.start(); - timer.start(); while(1) { scanf("%10s", bufferIn); - //pc.printf("bufferIn= is %s\r\n", bufferIn); - //pc.printf("bufferIn= is %c%c\r\n", bufferIn[0],bufferIn[1]); - - //Turn built in LED blue (at half intensity) to confirm command recieved ledConfirmReceive(); - - bufferOut[0]=bufferIn[0]; - - for(int i=0; i<7; i++) { // duplicate bytes 0 to 9 for now; - bufferOut[i]=bufferIn[i]; - } - bufferOut[7]='^'; - - - //get hexCCIn - for(int i=0; i<2; i++) { - hexCCIn[i]=bufferIn[i+1]; - } - //pc.printf("hexCCIn= is %s\r\n", hexCCIn); - - for(int i=0; i<4; i++) { - hexDDDDIn[i]=bufferIn[i+3]; - } - - //pc.printf("hello test= is %s\r\n", test); - - //int value = strtol(hexDDDDIn, NULL, 16); - int decCCIn = hexToDec(hexCCIn); - int decDDDDIn = hexToDec(hexDDDDIn); - //int decDDDDIn = hexToDec("0000"); - - //Make sure incoming checksum pans out: - - //Do action based on CC - - //pc.printf("hexCCIn= is %c%c\r\n", hexCCIn[0],hexCCIn[1]); - pc.printf("hexDDDDIn= is %c%c%c%c\r\n", hexDDDDIn[0],hexDDDDIn[1],hexDDDDIn[2],hexDDDDIn[3]); - //pc.printf("decCCIn= is %d\r\n", decCCIn); - pc.printf("decDDDDIn= is %d\r\n", decDDDDIn); - if (decCCIn==1) { //control built in LED - //PwmOut led(LED_RED); //set to red just for now, change later if nec. - //Get desired intensity - double ledIntensity=(double)(hexToDecSub(bufferIn, 4, 5)); - if (bufferIn[3]=='0') { //red - //PwmOut r(LED_RED); - r=(255-ledIntensity)/255; - } else if(bufferIn[3]=='1') { - //PwmOut g(LED_GREEN); - g=(255-ledIntensity)/255; - } else if (bufferIn[3]=='2') { - //PwmOut led(LED_BLUE); - b=(255-ledIntensity)/255; - } - //led.period(0.001f); - //led=(255-ledIntensity)/255; - //led=1.000; - - //end built in LED control - //pc.printf("%s\n", bufferOut); - - }else if(decCCIn==27) { //CC=1b, Enable pump - pumpStep.write(0.50f); // set fixed duty cycle to 50% (hold position?) - ledConfirmSent(); - runPump=true; - - }else if(decCCIn==28) { //CC=1c, Define new pressure setPoint - setPoint=((double)(decDDDDIn))/100; //converted fron hex above - - } else if (decCCIn==29) {//CC=1d, Deactivate pump - pumpStep.write(0.0f); - ledConfirmSent(); - runPump=false; - - } + runWhenNewSerialIn(); //Always keep running computePressureCommand if requred - computePressureCommand(); - + if(runPump) { + computePressureCommand(); + } //pc.printf("NEW value of '%s' is %ld\n", hexDDDDIn, decDDDDIn); //Turn built in LED green (at half intensity) to confirm reponse sent back - + } } \ No newline at end of file