![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Armin, Kenan
motor.cpp@0:3b0e2caa6ad4, 2014-05-12 (annotated)
- Committer:
- tim010
- Date:
- Mon May 12 16:01:59 2014 +0000
- Revision:
- 0:3b0e2caa6ad4
PAI_LV9_Grupa3_Tim010
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tim010 | 0:3b0e2caa6ad4 | 1 | #include "motor.h" |
tim010 | 0:3b0e2caa6ad4 | 2 | #include "mbed.h" |
tim010 | 0:3b0e2caa6ad4 | 3 | |
tim010 | 0:3b0e2caa6ad4 | 4 | int brzinaMotora; // Steper speed |
tim010 | 0:3b0e2caa6ad4 | 5 | |
tim010 | 0:3b0e2caa6ad4 | 6 | motor::motor(PinName A0, PinName A1, PinName A2, PinName A3) : _A0(A0), _A1(A1), _A2(A2), _A3(A3) { // Defenition of motor pins |
tim010 | 0:3b0e2caa6ad4 | 7 | _A0=0; _A1=0; _A2=0; _A3=0; |
tim010 | 0:3b0e2caa6ad4 | 8 | } |
tim010 | 0:3b0e2caa6ad4 | 9 | |
tim010 | 0:3b0e2caa6ad4 | 10 | void motor::postaviPinove(int vr) { |
tim010 | 0:3b0e2caa6ad4 | 11 | switch (vr) { |
tim010 | 0:3b0e2caa6ad4 | 12 | case 0: { _A0=0; _A1=0; _A2=0; _A3=1; } |
tim010 | 0:3b0e2caa6ad4 | 13 | break; |
tim010 | 0:3b0e2caa6ad4 | 14 | case 1: { _A0=0; _A1=0; _A2=1; _A3=1; } |
tim010 | 0:3b0e2caa6ad4 | 15 | break; |
tim010 | 0:3b0e2caa6ad4 | 16 | case 2: { _A0=0; _A1=0; _A2=1; _A3=0; } |
tim010 | 0:3b0e2caa6ad4 | 17 | break; |
tim010 | 0:3b0e2caa6ad4 | 18 | case 3: { _A0=0; _A1=1; _A2=1; _A3=0; } |
tim010 | 0:3b0e2caa6ad4 | 19 | break; |
tim010 | 0:3b0e2caa6ad4 | 20 | case 4: { _A0=0; _A1=1; _A2=0; _A3=0; } |
tim010 | 0:3b0e2caa6ad4 | 21 | break; |
tim010 | 0:3b0e2caa6ad4 | 22 | case 5: { _A0=1; _A1=1; _A2=0; _A3=0; } |
tim010 | 0:3b0e2caa6ad4 | 23 | break; |
tim010 | 0:3b0e2caa6ad4 | 24 | case 6: { _A0=1; _A1=0; _A2=0; _A3=0; } |
tim010 | 0:3b0e2caa6ad4 | 25 | break; |
tim010 | 0:3b0e2caa6ad4 | 26 | case 7: { _A0=1; _A1=0; _A2=0; _A3=1; } |
tim010 | 0:3b0e2caa6ad4 | 27 | break; |
tim010 | 0:3b0e2caa6ad4 | 28 | } |
tim010 | 0:3b0e2caa6ad4 | 29 | wait_us(brzinaMotora); // wait time defines the speed |
tim010 | 0:3b0e2caa6ad4 | 30 | } |
tim010 | 0:3b0e2caa6ad4 | 31 | |
tim010 | 0:3b0e2caa6ad4 | 32 | void motor::binarniBrojac(bool nazad) { |
tim010 | 0:3b0e2caa6ad4 | 33 | if (nazad) |
tim010 | 0:3b0e2caa6ad4 | 34 | for (int i = 7; i >= 0; i--) |
tim010 | 0:3b0e2caa6ad4 | 35 | postaviPinove(i); |
tim010 | 0:3b0e2caa6ad4 | 36 | |
tim010 | 0:3b0e2caa6ad4 | 37 | if (!nazad) |
tim010 | 0:3b0e2caa6ad4 | 38 | for (int i = 0; i <= 7; i++) |
tim010 | 0:3b0e2caa6ad4 | 39 | postaviPinove(i); |
tim010 | 0:3b0e2caa6ad4 | 40 | } |
tim010 | 0:3b0e2caa6ad4 | 41 | |
tim010 | 0:3b0e2caa6ad4 | 42 | void motor::nazad() { // rotate the motor 1 step anticlockwise |
tim010 | 0:3b0e2caa6ad4 | 43 | binarniBrojac(true); |
tim010 | 0:3b0e2caa6ad4 | 44 | } |
tim010 | 0:3b0e2caa6ad4 | 45 | |
tim010 | 0:3b0e2caa6ad4 | 46 | void motor::naprijed() { // rotate the motor 1 step clockwise |
tim010 | 0:3b0e2caa6ad4 | 47 | binarniBrojac(false); |
tim010 | 0:3b0e2caa6ad4 | 48 | } |
tim010 | 0:3b0e2caa6ad4 | 49 | void motor::korak(float broj_koraka, int smjer, int brzina) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200) |
tim010 | 0:3b0e2caa6ad4 | 50 | int count=0; // initalize step count |
tim010 | 0:3b0e2caa6ad4 | 51 | brzinaMotora=brzina; //set motor speed |
tim010 | 0:3b0e2caa6ad4 | 52 | if (smjer==0) // turn clockwise |
tim010 | 0:3b0e2caa6ad4 | 53 | do { |
tim010 | 0:3b0e2caa6ad4 | 54 | naprijed(); |
tim010 | 0:3b0e2caa6ad4 | 55 | count++; |
tim010 | 0:3b0e2caa6ad4 | 56 | } while (count<broj_koraka); // turn number of steps applied |
tim010 | 0:3b0e2caa6ad4 | 57 | else if (smjer==1)// turn anticlockwise |
tim010 | 0:3b0e2caa6ad4 | 58 | do { |
tim010 | 0:3b0e2caa6ad4 | 59 | nazad(); |
tim010 | 0:3b0e2caa6ad4 | 60 | count++; |
tim010 | 0:3b0e2caa6ad4 | 61 | } while (count<broj_koraka);// turn number of steps applied |
tim010 | 0:3b0e2caa6ad4 | 62 | } |