Upravljanje unipolasnim motorom

Revision:
2:b769f13c7523
Parent:
1:9cd4fd37eee0
--- a/umotor.cpp	Mon Jan 30 00:22:04 2017 +0000
+++ b/umotor.cpp	Wed Feb 08 01:22:42 2017 +0000
@@ -1,147 +1,99 @@
 /** Projektiranje ugradbenih računalnih sustava
-* Interakcija sa linijom
+* Default metoda za unipolarni steper motor
 * @author: mario_meh
 * @code
-#include "mbed.h"
-#include "umotor.h"
-
-* int main() {
-*     Steper *bipolar = new Steper();
-*     while(1) {
-*         bipolar->u_motor();
-*    }
-* }
+* #include "mbed.h"
+* #include "umotor.h"
+*
+*
+* Steper *unipolar = new Steper();    
+* unipolar->u_poz.u_krenuo = 1;  // Treba biti 1(0)
+* unipolar->u_poz.u_stao = 0;    // Treba biti 0(1)
+* unipolar->Brzina(4000);        // < brzina = brze i krace
+* unipolar->UEnable();           // Pokreni umotor 
+*
 * @endcode
 */
 #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;
+#define OKRETAJA 4000
+#define STEPER_STEP 0.5
+
+int step;
+int broji;
+int u_okretaja = OKRETAJA;
+float za_step = STEPER_STEP;
 /** Unipolarni motor radi u half-step modu */
-const uint8_t schrittfolge[] = {0b1001, 0b1010, 0b0110, 0b0101};  // Vollschritt zwei Phasen
+const uint8_t u_zavojnica[8][4] = {{1,0,0,1}, {1,0,0,0}, {1,1,0,0}, {0,1,0,0}, {0,1,1,0}, {0,0,1,0}, {0,0,1,1}, {0,0,0,1}};
 /** Konstruktor 
 * inicijalizacija je: Steper <naziv> = new Steper;
 */
-Steper::Steper() : coil1(PTD2), coil2(PTD0), coil3(PTD5), coil4(PTA13),
-                    ledSec(PTB18), riseL(PTD6), fallL(PTD7), pbone(PTA5), pbtwo(PTA4)
+Steper::Steper() : coil1(PTC12), coil2(PTC13), coil3(PTC16), coil4(PTA13)
 
 {
     
-    //toggler.attach(this, &Steper::timer_isr, 0.001);
-    
 }
 
 bool pbonePressed = false;
-bool pbtwoPressed = false;
+bool ready = true;
 
-void Steper::stepper_step() 
+void Steper::stepper_step(int broji) 
 {
-    
-    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;
-    
+    static uint8_t i = 0;
+    if(u_t.read_us() > u_okretaja*za_step) {
+        switch(i) {
+            case 0:
+                coil1 = u_zavojnica[broji][0];
+                i++;
+            case 1:
+                coil2 = u_zavojnica[broji][1];
+                i++;
+            case 2:
+                coil3 = u_zavojnica[broji][2];
+                i++;
+            case 3:
+                coil4 = u_zavojnica[broji][3];
+                i++;
+            default:
+                i = 0;
+        }
+        u_t.reset();
+    }    
 } 
-/** <timer_isr()> zove stepper_steo() */
+
 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;
+    step++;
+    if(step <= u_okretaja) {
+        u_motor();
     }
-    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;
-    /** Imam flag za 2 tipkala */
-    pbone.fall(this, &Steper::pboneCallback);
-    pbtwo.fall(this, &Steper::pbtwoCallback);
-    /** Započni timer da bi mogao zvati prekid <timer_isr> */
-    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(u_poz.u_krenuo == 1) {
+        stepper_step(broji++);
+        if(broji == 8) {
+            broji = 0;
         }
-        if (pbtwoPressed) {
-            pbtwoPressed = false;
-            // Do actions required when button two is pressed.
-            fallL = !fallL;
-            toggler.detach();
-            
-            //void motorStall();
-        } 
+    }
+    if(u_poz.u_stao) {
+        u_ticker.detach();
+    }
+}
+
+int Steper::Brzina(int okretaja) {
+    if(okretaja < OKRETAJA) {
+        u_okretaja = okretaja;
+        za_step = (float)((STEPER_STEP*1000)/OKRETAJA);
     }
+    if(okretaja >= OKRETAJA) {
+        u_okretaja = okretaja;
+        za_step = (float)((STEPER_STEP*1000)/OKRETAJA);
+    }
+    return u_okretaja;
+}
+
+void Steper::UEnable() {
+    u_t.start();
+    u_ticker.attach_us(this, &Steper::timer_isr, u_okretaja);
 }
\ No newline at end of file