Biblioteka za upravljanje bipolarnim step motorom
bmotor.cpp@0:ccc31f6eba34, 2017-02-08 (annotated)
- Committer:
- mario_meh
- Date:
- Wed Feb 08 08:36:14 2017 +0000
- Revision:
- 0:ccc31f6eba34
zadnje izmjene pred predaju seminarskog zadatka
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mario_meh | 0:ccc31f6eba34 | 1 | #include "mbed.h" |
mario_meh | 0:ccc31f6eba34 | 2 | #include "bmotor.h" |
mario_meh | 0:ccc31f6eba34 | 3 | |
mario_meh | 0:ccc31f6eba34 | 4 | int pola[8][6] = {{1,0,1,0,1,1}, {0,0,0,0,1,1}, {1,1,0,0,1,1}, {1,1,0,0,0,0}, {1,1,0,1,0,1}, {0,0,0,1,0,1}, {1,0,1,1,0,1}, {1,0,1,0,0,0}}; |
mario_meh | 0:ccc31f6eba34 | 5 | int brzina = 2400; |
mario_meh | 0:ccc31f6eba34 | 6 | volatile int kreni = 0; |
mario_meh | 0:ccc31f6eba34 | 7 | volatile int _gore = 8; |
mario_meh | 0:ccc31f6eba34 | 8 | volatile int _dolje = 0; |
mario_meh | 0:ccc31f6eba34 | 9 | int okretaja = 520; |
mario_meh | 0:ccc31f6eba34 | 10 | |
mario_meh | 0:ccc31f6eba34 | 11 | Bipolar::Bipolar() : ENA(PTB9),IN1(PTC1),IN2(PTC2),IN3(PTE21),IN4(PTE20),ENB(PTB10) |
mario_meh | 0:ccc31f6eba34 | 12 | |
mario_meh | 0:ccc31f6eba34 | 13 | { |
mario_meh | 0:ccc31f6eba34 | 14 | startSM(); |
mario_meh | 0:ccc31f6eba34 | 15 | b_t.start(); |
mario_meh | 0:ccc31f6eba34 | 16 | b_ticker.attach_us(this, &Bipolar::ciklusSM, brzina); |
mario_meh | 0:ccc31f6eba34 | 17 | } |
mario_meh | 0:ccc31f6eba34 | 18 | |
mario_meh | 0:ccc31f6eba34 | 19 | void Bipolar::goreDolje(int y) { |
mario_meh | 0:ccc31f6eba34 | 20 | static uint8_t i = 0; |
mario_meh | 0:ccc31f6eba34 | 21 | if(b_t.read_us() > brzina*0.5) { |
mario_meh | 0:ccc31f6eba34 | 22 | |
mario_meh | 0:ccc31f6eba34 | 23 | switch(i) { |
mario_meh | 0:ccc31f6eba34 | 24 | case 0: |
mario_meh | 0:ccc31f6eba34 | 25 | ENA = pola[y][0]; |
mario_meh | 0:ccc31f6eba34 | 26 | i++; |
mario_meh | 0:ccc31f6eba34 | 27 | case 1: |
mario_meh | 0:ccc31f6eba34 | 28 | IN1 = pola[y][1]; |
mario_meh | 0:ccc31f6eba34 | 29 | i++; |
mario_meh | 0:ccc31f6eba34 | 30 | case 2: |
mario_meh | 0:ccc31f6eba34 | 31 | IN2 = pola[y][2]; |
mario_meh | 0:ccc31f6eba34 | 32 | i++; |
mario_meh | 0:ccc31f6eba34 | 33 | case 3: |
mario_meh | 0:ccc31f6eba34 | 34 | IN3 = pola[y][3]; |
mario_meh | 0:ccc31f6eba34 | 35 | i++; |
mario_meh | 0:ccc31f6eba34 | 36 | case 4: |
mario_meh | 0:ccc31f6eba34 | 37 | IN4 = pola[y][4]; |
mario_meh | 0:ccc31f6eba34 | 38 | i++; |
mario_meh | 0:ccc31f6eba34 | 39 | case 5: |
mario_meh | 0:ccc31f6eba34 | 40 | ENB = pola[y][5]; |
mario_meh | 0:ccc31f6eba34 | 41 | i = 0; |
mario_meh | 0:ccc31f6eba34 | 42 | default: |
mario_meh | 0:ccc31f6eba34 | 43 | i = 0; |
mario_meh | 0:ccc31f6eba34 | 44 | } |
mario_meh | 0:ccc31f6eba34 | 45 | b_t.reset(); |
mario_meh | 0:ccc31f6eba34 | 46 | } |
mario_meh | 0:ccc31f6eba34 | 47 | } |
mario_meh | 0:ccc31f6eba34 | 48 | |
mario_meh | 0:ccc31f6eba34 | 49 | void Bipolar::potez() { |
mario_meh | 0:ccc31f6eba34 | 50 | if(b_poz.p_gore == 1) { |
mario_meh | 0:ccc31f6eba34 | 51 | goreDolje(--_gore); |
mario_meh | 0:ccc31f6eba34 | 52 | if(_gore == 0) { |
mario_meh | 0:ccc31f6eba34 | 53 | _gore = 8; |
mario_meh | 0:ccc31f6eba34 | 54 | } |
mario_meh | 0:ccc31f6eba34 | 55 | } |
mario_meh | 0:ccc31f6eba34 | 56 | if(b_poz.p_dolje == 1) { |
mario_meh | 0:ccc31f6eba34 | 57 | goreDolje(_dolje++); |
mario_meh | 0:ccc31f6eba34 | 58 | if(_dolje == 8) { |
mario_meh | 0:ccc31f6eba34 | 59 | _dolje = 0; |
mario_meh | 0:ccc31f6eba34 | 60 | } |
mario_meh | 0:ccc31f6eba34 | 61 | } |
mario_meh | 0:ccc31f6eba34 | 62 | } |
mario_meh | 0:ccc31f6eba34 | 63 | |
mario_meh | 0:ccc31f6eba34 | 64 | void Bipolar::ciklusSM() { |
mario_meh | 0:ccc31f6eba34 | 65 | startSM(); |
mario_meh | 0:ccc31f6eba34 | 66 | for(int a = 0; a < okretaja; a++) { |
mario_meh | 0:ccc31f6eba34 | 67 | potez(); |
mario_meh | 0:ccc31f6eba34 | 68 | if(a == (okretaja - 1)) { |
mario_meh | 0:ccc31f6eba34 | 69 | b_poz.done = true; |
mario_meh | 0:ccc31f6eba34 | 70 | } |
mario_meh | 0:ccc31f6eba34 | 71 | } |
mario_meh | 0:ccc31f6eba34 | 72 | stopSM(); |
mario_meh | 0:ccc31f6eba34 | 73 | kreni = 0; |
mario_meh | 0:ccc31f6eba34 | 74 | } |
mario_meh | 0:ccc31f6eba34 | 75 | |
mario_meh | 0:ccc31f6eba34 | 76 | void Bipolar::startSM() { |
mario_meh | 0:ccc31f6eba34 | 77 | ENA = 0; |
mario_meh | 0:ccc31f6eba34 | 78 | IN1 = 0; |
mario_meh | 0:ccc31f6eba34 | 79 | IN2 = 0; |
mario_meh | 0:ccc31f6eba34 | 80 | IN3 = 0; |
mario_meh | 0:ccc31f6eba34 | 81 | IN4 = 0; |
mario_meh | 0:ccc31f6eba34 | 82 | ENB = 0; |
mario_meh | 0:ccc31f6eba34 | 83 | } |
mario_meh | 0:ccc31f6eba34 | 84 | |
mario_meh | 0:ccc31f6eba34 | 85 | void Bipolar::stopSM() { |
mario_meh | 0:ccc31f6eba34 | 86 | ENA = 1; |
mario_meh | 0:ccc31f6eba34 | 87 | IN1 = 1; |
mario_meh | 0:ccc31f6eba34 | 88 | IN2 = 1; |
mario_meh | 0:ccc31f6eba34 | 89 | IN3 = 1; |
mario_meh | 0:ccc31f6eba34 | 90 | IN4 = 1; |
mario_meh | 0:ccc31f6eba34 | 91 | ENB = 1; |
mario_meh | 0:ccc31f6eba34 | 92 | } |