Sami Kanderian / Mbed 2 deprecated TestAnalogInPins

Dependencies:   mbed

Revision:
0:ce79b04fc2ca
Child:
1:610dc7763656
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 16 21:37:20 2015 +0000
@@ -0,0 +1,221 @@
+#include "mbed.h"
+//Declare hardware inputs
+Timer timer;
+AnalogIn potSet(A0);
+AnalogIn pressure(A1);
+PwmOut pumpStep(D8);
+DigitalOut dir(D9);
+DigitalOut visDir(LED2);
+PwmOut r(LED_RED);
+PwmOut g(LED_GREEN);
+PwmOut b(LED_BLUE);
+
+//Declare globals
+unsigned long lastTime;
+float inputTemperature, outputCmd, iTerm;
+float kp=2;
+float ki=0;
+float setPoint;
+int sampleTime = 250; //1/4 sec
+float outMin=-1.0;
+float outMax=1.0;
+float smallChange=0.01;
+float oldOutputCmd; //dont need
+bool runPump=false;
+
+//------------------------------------
+// Hyperterminal configuration 9600 bauds, 8N
+//------------------------------------
+Serial pc(USBTX, USBRX);
+
+void ledConfirmReceive()
+{
+    r=1;
+    g=1;
+    b=0.9;
+}
+
+void ledConfirmSent()
+{
+    r=1;
+    g=0.9;
+    b=1;
+}
+
+int hexToDec(char hex[])
+{
+    int decValue = strtol(hex, NULL, 16);
+    
+    return decValue;
+
+}
+
+int hexToDecSub(char hex[], int st, int ed)
+{
+    int n=ed-st+1;
+    char hexSubset[n];
+    for(int i=st; i<ed+1; i++) {
+        hexSubset[i-st]=hex[i];
+    }
+
+    int decValue = strtol(hexSubset, NULL, 16);
+    return decValue;
+
+}
+
+void computePressureCommand()
+{
+    int i;
+    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;
+        }
+        inputTemperature=inputTemperature/(double)(nSamplesToAverage);
+        //setPoint = potSet;
+        
+        float error = setPoint - inputTemperature;
+       
+        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;
+                
+        if (abs(error) > smallChange) {
+            pumpStep.write(0.50f); // turn back on
+            pumpStep.period_us((1.0/outputCmd)*200); 
+        }
+        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()));
+        
+        oldOutputCmd=outputCmd; //don't need
+        lastTime = now; /*Remember time for next time*/
+    }//end if timeChange    
+}//end computePressureVoltage
+
+
+
+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);
+
+    r=1;
+    g=1;
+    b=1;
+    
+    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;
+
+        }
+
+        //Always keep running computePressureCommand if requred
+        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