Hortau / Mbed 2 deprecated Tensiometer_Simulator_waterbench

Dependencies:   mbed

Revision:
7:8698d17a0168
Parent:
5:1eb90dace1c7
Child:
8:e82e5b78dbbd
--- a/I2CSlaveComm.cpp	Thu Nov 01 20:50:28 2018 +0000
+++ b/I2CSlaveComm.cpp	Fri Nov 23 14:29:57 2018 +0000
@@ -4,28 +4,22 @@
 I2CSlaveCustom slave(D4, D7);
 I2CSlaveCustom slave2(D14, D15); //use another I2C to emulate the adc
 
+AnalogIn relay(A0);
+
 Ticker command_ticker;
 Ticker flow_ticker;
 
 Serial          PcUart(USBTX, USBRX);
 
-int modeswitch = 1;
-int saveswitch = 0;
-float counter = 0;
-
-int I2Cswitch = 1;
+float startvalue = 2;
+float currentvalue = 2;
 
-float risetime = 600;
-float falltime = 30;
-float plateautime = 5;
-float steptime = 0.5;
+float risestepvalue = 0.2;
+float fallstepvalue = 0.2;
+float steptime = 1;
 
-float highvalue = 6;
-float lowvalue = -1;
-
-float stepvalue = steptime * (highvalue - lowvalue) / risetime;
-
-float tension = lowvalue;
+bool isloopdone = 0;
+bool pauseswitch = 1;
 
 unsigned char PointOnAddress = 0;
 
@@ -180,40 +174,55 @@
     slave2.address(0x90);
 }
 
-void cycle() 
+void setparameters()
 {
-    if(modeswitch == 3 )
+    printf("Setting parameters\n");
+    
+    do
     {
-        if(saveswitch == 2)
-        {
-            printf("Tension fall begins\n");
-        }
-        else if (saveswitch == 1)
-        {
-            printf("Tension rise begins\n");
-        }
+        printf("Enter starting tension in kPa\n");
+        scanf("%s", buffer);
+        
+        if(atof(buffer) == 0 && strcmp(buffer,"0") != 0)       
+            printf("Error: invalid input\n");
+        if(atof(buffer) < -1 || atof(buffer) > 10)
+            printf("Error: tension out of range [-1:10]\n");
+    } while(atof(buffer) == 0 && strcmp(buffer,"0") != 0 || atof(buffer) < -1 || atof(buffer) > 10);
         
-        counter = 0;
-        modeswitch = saveswitch; 
+    startvalue = atof(buffer);    
+        
+    do
+    {
+        printf("Enter step time in seconds\n");
+        scanf("%s", buffer);
+        
+        if(atof(buffer) <= 0)
+            printf("Error: invalid input\n");
+    } while(atof(buffer) <= 0);
 
-    }
-    else
+    steptime = atof(buffer);
+    
+    do
     {
-        if(modeswitch == 1)
-        {
-            printf("High plateau begins\n");
-            stepvalue = steptime * (lowvalue - highvalue) / falltime;
-            saveswitch = 2;
-            modeswitch = 3;
-        }
-        else if (modeswitch == 2)
-        {
-            printf("Low plateau begins\n");
-            stepvalue = steptime * (highvalue - lowvalue) / risetime;
-            saveswitch = 1;
-            modeswitch = 3;
-        }
-    }
+        printf("Enter falling step value in kPa\n");
+        scanf("%s", buffer);
+        
+        if(atof(buffer) <= 0)
+            printf("Error: invalid input\n");
+    } while(atof(buffer) <= 0);
+
+    fallstepvalue = atof(buffer);
+    
+        do
+    {
+        printf("Enter rising step value in kPa\n");
+        scanf("%s", buffer);
+        
+        if(atof(buffer) <= 0)
+            printf("Error: invalid input\n");
+    } while(atof(buffer) <= 0);
+
+    risestepvalue = atof(buffer);
 }
 
 void commandselect()
@@ -222,64 +231,57 @@
     {
         char command = PcUart.getc();
         switch(command)
-        { 
+        {        
+            case 'p':
+            {
+                if (!pauseswitch)
+                {
+                    pauseswitch = 1;
+                    printf("Paused\n");
+                }
+                else
+                {
+                    pauseswitch = 0;
+                    printf("Resumed\n");
+                }
+                break;
+            }
+        
             case 'w':
             {
-                I2Cswitch = 0;
-                flow_ticker.detach();
-                
-                printf("Setting parameters\n");
+                flow_ticker.detach();                
                 
-                printf("Enter tension high value in kPa\n");
-                scanf("%s", buffer);
-                highvalue = atoi(buffer);
-                
-                printf("Enter tension low value in kPa\n");
-                scanf("%s", buffer);
-                lowvalue = atoi(buffer);
-                
-                printf("Enter tension rise time in seconds\n");
-                scanf("%s", buffer);
-                risetime = atoi(buffer);
+                setparameters();
+                        
+                printf("Resetting cycle\n");
                 
-                printf("Enter tension fall time in seconds\n");
-                scanf("%s", buffer);
-                falltime = atoi(buffer);
-                
-                printf("Enter plateau time in seconds\n");
-                scanf("%s", buffer);
-                plateautime = atoi(buffer);
+                pauseswitch = 1;                
+                currentvalue = startvalue;
+                setTension(currentvalue);
                 
-                printf("Enter step time in seconds\n");
-                scanf("%s", buffer);
-                steptime = atoi(buffer);
-                
-                printf("Resetting cycle\n");
-                counter = 0;
-                tension = lowvalue;
-                stepvalue = steptime * (highvalue - lowvalue) / risetime;
-                modeswitch = 1;
+                printf("Use command 'p' to resume cycling\n");
                 
                 flow_ticker.attach(&flow, steptime);
-                I2Cswitch = 1;
                 break;
             }
             
             case 'i':
             {
                 printf("List of parameter values\n");
-                printf("High tension: %df kPa\n", (int)highvalue);
-                printf("Low tension: %d kPa\n", (int)lowvalue);
-                printf("Cycle rise time: %d seconds\n", (int)risetime);
-                printf("Cycle fall time: %d seconds\n", (int)falltime);
-                printf("Cycle plateau time: %d seconds\n", (int)plateautime);
+                printf("Start value: %01f kPa\n", startvalue);
+                printf("Rising step value: %01f kPa\n", risestepvalue);
+                printf("Falling step value: %01f kPa\n", fallstepvalue);
                 printf("Step time: %d seconds\n", (int)steptime);
-                if(modeswitch == 1)
+
+                if(relay.read() >= 0.1f)
+                    printf("Cycle currently in falling phase\n");
+                else
                     printf("Cycle currently in rising phase\n");
-                else if(modeswitch == 2)
-                    printf("Cycle currently in falling phase\n");
-                else if(modeswitch == 3)
-                    printf("Cycle currently in plateau phase\n");
+                    
+                if(pauseswitch)
+                    printf("Cycle currently in pause\n");
+                else
+                    printf("Cycle currently active\n");
                 break;
             }      
         }
@@ -288,47 +290,58 @@
 
 void flow()
 {
-    if(modeswitch == 3)
-    {
-        counter += steptime;
-        
-        if(counter >= plateautime)
-            cycle();
-    }
+    if(pauseswitch)
+        return;
     else
     {
-        tension += stepvalue;
-        setTension(tension);
-        
-        if(modeswitch == 1)
-            printf("Rising, tension = %f\n", tension);
-        if(modeswitch == 2)
-            printf("Falling, tension = %f\n", tension);
-               
-        if(modeswitch == 1 && tension >= highvalue - 0.001f)
+        if(relay.read() >= 0.1f)
         {
-            tension = highvalue;
-            cycle();
+            if(currentvalue <= 0.201f)
+                currentvalue = currentvalue/2;
+            else
+                currentvalue -= fallstepvalue;
+            setTension(currentvalue);
+            printf("Falling, tension = %f\n", currentvalue);
         }
-        else if(modeswitch == 2 && tension <= lowvalue + 0.001f)
+    
+        else
         {
-            tension = lowvalue;
-            cycle();
-        }
+            currentvalue += risestepvalue;
+            setTension(currentvalue);
+            printf("Rising, tension = %f\n", currentvalue);
+        }        
     }
 }
 
 int main()
 {
-    printf("Initiating\n");
+   do
+    {
+        printf("Set parameters? (y/n)\n");
+        scanf("%s", buffer);
+        
+        if(strcmp(buffer,"y") == 0 || strcmp(buffer,"Y") == 0)
+        {       
+            setparameters();
+            isloopdone = 1;
+        }
+        else if(strcmp(buffer,"n") == 0 || strcmp(buffer,"N") == 0)
+        {
+            printf("Default parameters maintained\n");
+            isloopdone = 1;
+        }
+        else
+            printf("Error: invalid input\n");
+    } while(isloopdone != 1); 
+    
+    printf("Beginning simulation\n");
+    printf("Use command 'p' to begin cycling\n");
+    
     InitI2CSlaveComm();
     flow_ticker.attach(&flow, steptime);
     command_ticker.attach(&commandselect, 1);
     while(1)
     {
-        if(I2Cswitch == 1)
-        {
             I2CSlaveProcess();
-        }
     }
 }
\ No newline at end of file