Receives a measured height of a ping-pong ball from a PC, and uses it to control the PWM of a fan to keep the height as set with the keypad. Information is shown on the LCD

Dependencies:   TextLCD

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Files at this revision

API Documentation at this revision

Comitter:
gunarthon
Date:
Tue Jul 04 16:36:18 2017 +0000
Parent:
40:19d51f6e6800
Child:
42:b69538bba4f9
Commit message:
improved menu navigation

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Jul 02 19:01:07 2017 +0000
+++ b/main.cpp	Tue Jul 04 16:36:18 2017 +0000
@@ -17,6 +17,8 @@
 const double Kp = 0.02;
 const double Ki = 0;
 const double Kd = 0;
+const double configStep = 0.001;
+const double configFastStep = configStep*10;
 
 const int simulationSeconds = 10;
 
@@ -32,7 +34,7 @@
 AnalogIn        buttons(A0);
 
 //Threads
-Thread blueThread;
+Thread lcdThread;
 Thread pidThread;
 Thread communicationThread;
 Thread hmiThread;
@@ -48,21 +50,30 @@
 int menu = menuSETPOINT;
 int parameter = parameterKP;
 int simul = simulNONE;
+bool setPointSimul = false;
 
 void SerialCallback(int);
 
 //=============================Thread Methodes==================================
 void setPointMethode(void)
 {
-    while(1)
+    while(1) //toggle every 20 seconds between 2 setPoint values
     {
-        pidController->setSetPoint(0.2);
-        Thread::wait(20000);   
-        pidController->setSetPoint(0.8);
-        Thread::wait(20000);   
+        while(!setPointSimul) Thread::wait(2000);
+            
+        if(setPointSimul)
+        {
+            pidController->setSetPoint(0.2);
+            Thread::wait(20000);
+            if(setPointSimul)
+            {
+                pidController->setSetPoint(0.8);
+                Thread::wait(20000);  
+            }
+        } 
     }
 }
-void BlueMethode(void)
+void LcdMethode(void)
 {
     while(true)
     {
@@ -99,7 +110,12 @@
         {
             lcd.cls();
             if(simul == simulNONE)
-                lcd.printf("u:STEP dwn:RAMP");
+            {
+                if(setPointSimul)
+                    lcd.printf("u:STEP * d:RAMP");
+                else
+                    lcd.printf("u:STEP _ d:RAMP");
+            }
             else
                 lcd.printf("set  $in$   out");
             lcd.locate(0,1);
@@ -143,25 +159,14 @@
     serial.format(8, SerialBase::None, 1);
     double input = 0;
     
-    //event_callback_t functionpointer;
-    //functionpointer.attach(&SerialCallback);
-    
     while(true)
     {
-        //serial.read(&input, 1, functionpointer);
         input = double(serial.getc()+(serial.getc()<<8)) / heightResolution;
-        //lcd.cls();
-        //lcd.printf("%d", int(input));
         message_t *message = mpool.alloc();
         message->input = input;
         messageQueue.put(message);
     }   
 }
-/*
-void SerialCallback(int)
-{
-   //ut = 
-}*/
 
 unsigned readButtons()
 {
@@ -190,14 +195,17 @@
 {
     
     unsigned button = btnNONE;
+    bool fastChange = false;
     while(true)
     {
         
-        //while(button == btnNONE)
-        //{
+        button = readButtons();
+        while(button == btnNONE)
+        {
             button = readButtons();
-          //  Thread::wait(10);
-        //}
+            Thread::wait(10);
+            fastChange = false;
+        }
                
         double prevSetPoint = pidController->getSetPoint();
         double newSetPoint = prevSetPoint;
@@ -210,13 +218,13 @@
         else if(menu == menuSETPOINT)
         {
             if(button == btnUP)
-                newSetPoint += 0.001;
+                newSetPoint += fastChange ? configFastStep : configStep;
             else if(button == btnDOWN)
-                newSetPoint -= 0.001;
+                newSetPoint -= fastChange ? configFastStep : configStep;
             else if(button == btnLEFT)
-                newSetPoint -= 0.01;
+                newSetPoint -= fastChange ? 10*configFastStep : 10*configStep;
             else if(button == btnRIGHT)
-                newSetPoint += 0.01;
+                newSetPoint += fastChange ? 10*configFastStep : 10*configStep;
                 
             pidController->setSetPoint(newSetPoint);
         }
@@ -225,20 +233,20 @@
             if(button == btnUP)
             {
                 if(parameter == parameterKP)
-                    pidController->addKp(0.001);
+                    pidController->addKp(fastChange ? configFastStep : configStep);
                 else if(parameter == parameterKI)
-                    pidController->addKi(0.001);
+                    pidController->addKi(fastChange ? configFastStep : configStep);
                 else if(parameter == parameterKD)
-                    pidController->addKd(0.001);
+                    pidController->addKd(fastChange ? configFastStep : configStep);
             }
             else if(button == btnDOWN)
             {
                 if(parameter == parameterKP)
-                    pidController->addKp(-0.001);
+                    pidController->addKp(fastChange ? -configFastStep : -configStep);
                 else if(parameter == parameterKI)
-                    pidController->addKi(-0.001);
+                    pidController->addKi(fastChange ? -configFastStep : -configStep);
                 else if(parameter == parameterKD)
-                    pidController->addKd(-0.001);
+                    pidController->addKd(fastChange ? -configFastStep : -configStep);
             }
             else if(button == btnLEFT)
                 parameter = (parameter+1) % parameterSIZE;
@@ -276,20 +284,21 @@
                 }
                 simul = simulNONE;
             }
-            else if(button == btnLEFT) //ramp
-            {
-                setPointThread.start(setPointMethode);
-            }
+            else if(button == btnLEFT)
+                setPointSimul = true;
             else if(button == btnRIGHT)
-                setPointThread.terminate();
+                setPointSimul = false;
             
         }
-        
-        /*while(button != btnNONE)
+        unsigned repeatCount = 0;
+        while(button != btnNONE && !fastChange)
         {
+            repeatCount++;
             button = readButtons();
             Thread::wait(10);
-        }*/
+            if(repeatCount > 50)
+                fastChange = true;
+        }
         Thread::wait(100);
     }   
 }
@@ -307,10 +316,11 @@
     pidController = new Pid(Kp, Ki, Kd);
     pidController->setSetPoint(setPoint);
     
-    blueThread.start(BlueMethode);
+    lcdThread.start(LcdMethode);
     pidThread.start(PidMethode);
     communicationThread.start(CommunicationMethode);
     hmiThread.start(hmiMethode);
+    setPointThread.start(setPointMethode);
     
     while (true) {
         Thread::wait(1100);