Upravljanje unipolasnim motorom

Revision:
0:a3d5b06d790a
Child:
1:9cd4fd37eee0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/umotor.cpp	Sun Jan 29 23:53:10 2017 +0000
@@ -0,0 +1,130 @@
+#include "mbed.h"
+#include "umotor.h"
+
+volatile uint8_t speed_step = 0;
+uint32_t g_tmr_multiplicator = 5;
+int koraka;
+int available = 1;
+static uint8_t step = 0;
+
+const uint8_t schrittfolge[] = {0b1001, 0b1010, 0b0110, 0b0101};  // Vollschritt zwei Phasen
+
+Steper::Steper() : coil1(PTD2), coil2(PTD0), coil3(PTD5), coil4(PTA13),
+                    ledSec(PTB18), riseL(PTD6), fallL(PTD7), pbone(PTA5), pbtwo(PTA4)
+
+{
+    
+    //toggler.attach(this, &Steper::timer_isr, 0.001);
+    
+}
+
+bool pbonePressed = false;
+bool pbtwoPressed = false;
+
+void Steper::stepper_step() 
+{
+    
+    static uint8_t step = 0;
+    
+    coil1 = 0x01 & (schrittfolge[step] >> 0 );
+    coil2 = 0x01 & (schrittfolge[step] >> 1 );
+    coil3 = 0x01 & (schrittfolge[step] >> 2 );
+    coil4 = 0x01 & (schrittfolge[step] >> 3 );
+    
+    step++;
+    
+    if (step >= ARRAY_SIZE(schrittfolge))
+        step = 0;
+    
+} 
+
+void Steper::timer_isr() {
+    
+    static uint32_t g_tmr_cnt = 0;
+    g_tmr_cnt++;
+    if (g_tmr_cnt >= g_tmr_multiplicator) {
+        stepper_step();
+        g_tmr_cnt = 0;
+    }
+    
+}
+
+void Steper::pboneCallback(void) {
+    if(t.read_ms() > 20) {
+        pbonePressed = true;
+    }
+    t.reset();
+}
+void Steper::pbtwoCallback(void) {
+    if(t.read_ms() > 20) {    
+        pbtwoPressed = true;
+    }
+    t.reset(); 
+}
+void Steper::togglerOff() { ledSec = 0; }
+void Steper::togglerRed() { 
+    ledSec = 1;
+    led.detach();
+    led.attach(this, &Steper::togglerOff, 0.2);
+}
+
+void motorStall() {
+    //toggler.detach();
+}
+
+void Steper::u_motor() {
+    riseL = 1;
+    fallL = 1;
+    ledSec = 1;
+    
+    pbone.fall(this, &Steper::pboneCallback);
+    pbtwo.fall(this, &Steper::pbtwoCallback);
+    
+    t.start();
+    toggler.attach(this, &Steper::timer_isr, 0.001);
+ 
+    while(1) {
+        static uint8_t speed_step = 0;
+        if (pbonePressed) {
+            toggler.attach(this, &Steper::timer_isr, 0.001);
+            pbonePressed = !pbonePressed;
+            led.attach(this, &Steper::togglerRed, 1);
+            // Do actions required when button one is pressed.
+            riseL = !riseL;
+            
+            switch(speed_step){
+              case 0:
+                g_tmr_multiplicator = 5;
+                speed_step++;
+              break;
+              
+              case 1:
+                g_tmr_multiplicator = 10;
+                speed_step++;
+              break;
+              
+              case 2:
+                g_tmr_multiplicator = 20;
+                speed_step++;
+              break;
+              
+              case 3:
+                g_tmr_multiplicator = 50;
+                speed_step = 0;
+              break;
+              
+              default:
+                speed_step = 0;
+              break;
+            }
+        }
+        if (pbtwoPressed) {
+            pbtwoPressed = false;
+            // Do actions required when button two is pressed.
+            fallL = !fallL;
+            toggler.detach();
+            
+            //void motorStall();
+        } 
+    }
+}
\ No newline at end of file