Upravljanje unipolasnim motorom

umotor.cpp

Committer:
mario_meh
Date:
2017-01-30
Revision:
1:9cd4fd37eee0
Parent:
0:a3d5b06d790a
Child:
2:b769f13c7523

File content as of revision 1:9cd4fd37eee0:

/** Projektiranje ugradbenih računalnih sustava
* Interakcija sa linijom
* @author: mario_meh
* @code
#include "mbed.h"
#include "umotor.h"

* int main() {
*     Steper *bipolar = new Steper();
*     while(1) {
*         bipolar->u_motor();
*    }
* }
* @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;
/** Unipolarni motor radi u half-step modu */
const uint8_t schrittfolge[] = {0b1001, 0b1010, 0b0110, 0b0101};  // Vollschritt zwei Phasen
/** 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)

{
    
    //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;
    
} 
/** <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;
    }
    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 (pbtwoPressed) {
            pbtwoPressed = false;
            // Do actions required when button two is pressed.
            fallL = !fallL;
            toggler.detach();
            
            //void motorStall();
        } 
    }
}