Pajić Enil, Jakić Irma

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Enil4StepMotor.h Source File

Enil4StepMotor.h

00001 #ifndef E_Motor_H
00002 #define E_Motor_H
00003 #include "mbed.h"
00004 /* CopyRight (C) 2014, by Enil Pajić */
00005 class Enil4StepMotor
00006     {
00007         int eD, eS, eINS[8];
00008         bool eCW, eAtt;
00009         BusOut eIN;
00010         Ticker T;
00011         int Normalize (int S, bool P = true) {if (S < 0) S = -S; S %= 360; double X = S; if (P) X = (512. / 360.) * double (S); return X;}
00012         void Rotate (bool CW) {for (int a = CW ? 7 : 0; CW ? a >= 0 : a < 8; CW ? --a : ++a) eIN = eINS[a], wait_us (eS);/*wait_ms (eS / 10.);*/}
00013         void RotateHelper () {Rotate (eCW);}
00014     public:
00015         Enil4StepMotor (PinName IN1, PinName IN2, PinName IN3, PinName IN4, int MSpeed = 500) : 
00016             eD (0), eS(500), eCW (true), eAtt (false), eIN (IN1, IN2, IN3, IN4)
00017             {
00018                 int X[8] = {0x1, 0x3, 0x2, 0x6, 0x4, 0xC, 0x8, 0x9};
00019                 for (int a = 0; a < 8; ++a) eINS[a] = X[a];
00020                 Speed (MSpeed);
00021             }
00022         bool ClockWiseDirection () const {return eCW;}
00023         bool &ClockWiseDirection () {return eCW;}
00024         int Degrees () const {return eD;}
00025         int Speed () const {return eS;}
00026         void Speed (int S) {if (S <= 0) S = 1; if (S > 999) S = 999; eS = 1000 - S;}
00027         void RotateRelative (int Step, bool ClockWise)
00028             {
00029                 eD = Normalize (Step, false);
00030                 Step = Normalize (Step);
00031                 while (Step--) Rotate (ClockWise);
00032             }
00033         void RotateAbsolute (int Step)
00034             {
00035                 bool P = eCW;   
00036                 Step = Normalize (Step, false);
00037                 int X = Step - eD; if (X < 0) eCW = !eCW, X = -X;
00038                 RotateRelative (X, eCW);
00039                 eCW = P;
00040             }
00041         void Start () {if (!eAtt) eAtt = true, T.attach (this, &Enil4StepMotor::RotateHelper, 0.0001);} //Regulirati 1-999 (ms|us). 0.00001, 0.005, 0.01
00042         void Stop () {if (eAtt) eAtt = false, T.detach();}
00043         bool Running () const {return eAtt;}
00044     };
00045 #endif  //E_Motor_H