Basic DC motor control test, rpm feedback by simple impulse signal, PID speed control.

Dependencies:   FastPWM mbed FastIO MODSERIAL

Revision:
5:ec4d6e435822
Parent:
4:7cb8986200a7
Child:
6:cc38171e6a4b
--- a/main.cpp	Mon Mar 26 13:00:32 2018 +0000
+++ b/main.cpp	Tue Mar 27 16:11:44 2018 +0000
@@ -6,10 +6,13 @@
 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 64 
 #include "MODSERIAL.h"
 
-static const PinName IMPULSE_SENSOR_PIN = PinName(10);
+#define IMPULSE_SENSOR_R_PIN (PA_9)
+#define PWM_OUT_R_PIN (PA_6)
 
-FastPWM mypwm(PWM_OUT);
-FastIn<IMPULSE_SENSOR_PIN> pinImpulseSensorIn;
+FastPWM mypwm(PWM_OUT_R_PIN);
+
+FastIn<IMPULSE_SENSOR_R_PIN> pinImpulseSensorIn;
+FastIn<USER_BUTTON> pinUserButtonIn;
 
 DigitalOut myled(LED1);
 
@@ -21,73 +24,103 @@
 //------------------------------------
 MODSERIAL pcLink(SERIAL_TX, SERIAL_RX);
 
-static const us_timestamp_t  periodImpSens  = 100000UL;
-static const us_timestamp_t  periodLEDBlink = 1000000UL;
-static const us_timestamp_t  periodPWMWrite = 10000000UL;
+static const us_timestamp_t  periodImpSens  = 250000;   // 250 msec
+static const us_timestamp_t  periodLEDBlink = 100000;   // 100 msec
+static const us_timestamp_t  periodPWMWrite = 5000000;  // 5 sec
 
-static us_timestamp_t  tStampImpSens  = 0UL;
-static us_timestamp_t  tStampLEDBlink = 0UL;
-static us_timestamp_t  tStampPWMWrite = 0UL;
+static us_timestamp_t  tStampImpSens  = 0;
+static us_timestamp_t  tStampLEDBlink = 0;
+static us_timestamp_t  tStampPWMWrite = 0;
+static us_timestamp_t  tStamp = 0;
 
 static unsigned int uiImpSens = 0U;
+static unsigned int uiImpSensTemp = 0U;
+static int          iImpSensLastState = 0;
+static double       dPwmDuty = 0.0;
 
-int main() {
-    unsigned int uiImpSensTemp = 0U;
-    int iImpSensLastState = 0;
+static void setup(void);
+static void tskImpSens(void);
+static void tskLEDBlink(void);
+static void tskPWMWrite(void);
+static void tskBackground(void);
+
+static inline void DO_TASK(us_timestamp_t tskPeriod, us_timestamp_t &tskTimer, us_timestamp_t timeStamp, void (*tskFunction)(void))
+{
+    if (tskPeriod < (timeStamp - tskTimer))
+    {
+        tskTimer = timeStamp;
+        (*tskFunction)();
+    }
+}
+
+static inline void BACKGROUND(void (*tskFunction)(void))
+{
+    (*tskFunction)();
+}
 
+
+int main(void)
+{
+    setup();
+       
+    while(1)
+    {
+        tStamp = myTimer.read_high_resolution_us();
+ 
+        DO_TASK(periodLEDBlink, tStampLEDBlink, tStamp, &tskLEDBlink);
+        DO_TASK(periodPWMWrite, tStampPWMWrite, tStamp, &tskPWMWrite);
+        DO_TASK(periodImpSens,  tStampImpSens,  tStamp, &tskImpSens);
+
+        BACKGROUND(&tskBackground);
+    }
+}
+
+
+void setup(void)
+{
     pcLink.baud(115200);
     pcLink.format(8, SerialBase::None, 1);
     
-    mypwm.period_us(100);
-    mypwm.write(0.0);
+    mypwm.period_us(2000);
+    mypwm.write(0.5);
   
     myTimer.start();
-        
-    while(1) {
-        double dPwmDuty = 0.0;
-        
-        us_timestamp_t  tStamp;
+}
+
+void tskImpSens(void)
+{
+    uiImpSens = uiImpSensTemp;
+    uiImpSensTemp = 0U;
 
-        tStamp = myTimer.read_high_resolution_us();
-        
-        if (periodLEDBlink < (tStamp - tStampLEDBlink))
-        {
-            tStampLEDBlink = tStamp;
+    pcLink.printf("IMP: %u imp.\r", uiImpSens);
+}
 
-            myled = !myled;
-        }
-        
-        if (periodImpSens < (tStamp - tStampImpSens))
-        {
-            tStampImpSens = tStamp;
-
-            uiImpSens = uiImpSensTemp;
-            uiImpSensTemp = 0U;
+void tskLEDBlink(void)
+{
+    myled = !myled;
+}
 
-            pcLink.printf("IMP: %u imp.\n", uiImpSens);
-        }
-        
-        if (periodPWMWrite < (tStamp - tStampPWMWrite))
-        {
-            tStampPWMWrite = tStamp;
-
-            mypwm.write(dPwmDuty);
-            pcLink.printf("PWM: %.2f %%\n", mypwm.read() * 100);
-    
-            dPwmDuty += 0.1;
+void tskPWMWrite(void)
+{
+    dPwmDuty = dPwmDuty + 0.1;
             
-            if (1.0 < dPwmDuty)
-            {
-                dPwmDuty = 0.0;
-            }
-        }
-        
-        // Impulse sensor
-        int iTemp = pinImpulseSensorIn.read();
-        if (iTemp != iImpSensLastState)
-        {
-            iImpSensLastState = iTemp;
-            uiImpSensTemp++;
-        }
+    if (1.0 < dPwmDuty)
+    {
+        dPwmDuty = 0.1;
     }
+
+    mypwm.write(dPwmDuty);
+
+    pcLink.printf("\r\nPWM: %.2f %%\r\n", mypwm.read() * 100);
 }
+
+void tskBackground(void)
+{
+    // Impulse sensor - pulse counting
+    int iTemp = pinImpulseSensorIn.read();
+    if (iTemp != iImpSensLastState)
+    {
+        iImpSensLastState = iTemp;
+        uiImpSensTemp++;
+    }
+}
\ No newline at end of file