![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Emrah Dautbegović Edis Kunić
main.cpp@0:d1b1742b324c, 2014-05-15 (annotated)
- Committer:
- tim008
- Date:
- Thu May 15 18:13:30 2014 +0000
- Revision:
- 0:d1b1742b324c
Lv9-Pai-grupa7-tim008-zad2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tim008 | 0:d1b1742b324c | 1 | #include "mbed.h" |
tim008 | 0:d1b1742b324c | 2 | #include "sMotor.h" |
tim008 | 0:d1b1742b324c | 3 | //motor.step(numstep,smjer,speed); |
tim008 | 0:d1b1742b324c | 4 | int smjer = 0; |
tim008 | 0:d1b1742b324c | 5 | bool radi = true; |
tim008 | 0:d1b1742b324c | 6 | Serial pc(USBTX, USBRX); |
tim008 | 0:d1b1742b324c | 7 | sMotor motor(dp9, dp10, dp11, dp13); // creates new stepper motor: IN1, IN2, IN3, IN4 |
tim008 | 0:d1b1742b324c | 8 | int speed = 900 ; // set default motor speed |
tim008 | 0:d1b1742b324c | 9 | float numstep = 512 ; //broj koraka potrebnih da motor napravi ugao od 360 stepeni |
tim008 | 0:d1b1742b324c | 10 | |
tim008 | 0:d1b1742b324c | 11 | void objasni() { |
tim008 | 0:d1b1742b324c | 12 | pc.printf("\n NAREDBE: "); |
tim008 | 0:d1b1742b324c | 13 | pc.printf("\nA-postavi osovinu motora(0-360) stepeni"); |
tim008 | 0:d1b1742b324c | 14 | pc.printf("\nB-promijeni smjer kretanja motora"); |
tim008 | 0:d1b1742b324c | 15 | pc.printf("\nC-promijeni brzinu kretanja motora"); |
tim008 | 0:d1b1742b324c | 16 | pc.printf("\nD-pokreni/zaustavi motor"); |
tim008 | 0:d1b1742b324c | 17 | } |
tim008 | 0:d1b1742b324c | 18 | |
tim008 | 0:d1b1742b324c | 19 | char ocitajNaredbu() { |
tim008 | 0:d1b1742b324c | 20 | char naredba; |
tim008 | 0:d1b1742b324c | 21 | pc.printf("\nUnesi naredbu: "); |
tim008 | 0:d1b1742b324c | 22 | naredba=pc.getc(); |
tim008 | 0:d1b1742b324c | 23 | while(naredba != 'A' && naredba != 'B' && naredba != 'C' && naredba != 'D') { |
tim008 | 0:d1b1742b324c | 24 | pc.printf("\nUnesi naredbu: "); |
tim008 | 0:d1b1742b324c | 25 | naredba=pc.getc(); |
tim008 | 0:d1b1742b324c | 26 | } |
tim008 | 0:d1b1742b324c | 27 | return naredba; |
tim008 | 0:d1b1742b324c | 28 | } |
tim008 | 0:d1b1742b324c | 29 | |
tim008 | 0:d1b1742b324c | 30 | void postaviUgaoOsovine() { |
tim008 | 0:d1b1742b324c | 31 | int ugao; |
tim008 | 0:d1b1742b324c | 32 | pc.printf("\nUnesi ugao(stepeni): "); |
tim008 | 0:d1b1742b324c | 33 | pc.scanf("%d", &ugao); |
tim008 | 0:d1b1742b324c | 34 | numstep = (int)((512./360.)*(ugao%360)); |
tim008 | 0:d1b1742b324c | 35 | if(ugao>0){ |
tim008 | 0:d1b1742b324c | 36 | if(radi==true) |
tim008 | 0:d1b1742b324c | 37 | motor.step(numstep,smjer,speed); |
tim008 | 0:d1b1742b324c | 38 | } |
tim008 | 0:d1b1742b324c | 39 | else { |
tim008 | 0:d1b1742b324c | 40 | smjer = 1-smjer; |
tim008 | 0:d1b1742b324c | 41 | if(radi==true) |
tim008 | 0:d1b1742b324c | 42 | motor.step(numstep,smjer,speed); |
tim008 | 0:d1b1742b324c | 43 | } |
tim008 | 0:d1b1742b324c | 44 | } |
tim008 | 0:d1b1742b324c | 45 | |
tim008 | 0:d1b1742b324c | 46 | void promijeniSmjer() { |
tim008 | 0:d1b1742b324c | 47 | smjer = 1-smjer; |
tim008 | 0:d1b1742b324c | 48 | if(radi==true) |
tim008 | 0:d1b1742b324c | 49 | motor.step(numstep,smjer,speed); |
tim008 | 0:d1b1742b324c | 50 | } |
tim008 | 0:d1b1742b324c | 51 | |
tim008 | 0:d1b1742b324c | 52 | void promijeniBrzinu() { |
tim008 | 0:d1b1742b324c | 53 | pc.printf("\nUnesi brzinu: "); |
tim008 | 0:d1b1742b324c | 54 | pc.scanf("%d", &speed); |
tim008 | 0:d1b1742b324c | 55 | if(radi==true) |
tim008 | 0:d1b1742b324c | 56 | motor.step(numstep,smjer,speed); |
tim008 | 0:d1b1742b324c | 57 | } |
tim008 | 0:d1b1742b324c | 58 | |
tim008 | 0:d1b1742b324c | 59 | void pali_gasi() { |
tim008 | 0:d1b1742b324c | 60 | if(radi) pc.printf("\nMotor je upaljen i krece se brzinom %d.", speed); |
tim008 | 0:d1b1742b324c | 61 | else pc.printf("\nMotor je ugasen."); |
tim008 | 0:d1b1742b324c | 62 | } |
tim008 | 0:d1b1742b324c | 63 | |
tim008 | 0:d1b1742b324c | 64 | void startOrStop() { |
tim008 | 0:d1b1742b324c | 65 | if(radi == true) { |
tim008 | 0:d1b1742b324c | 66 | radi = false; |
tim008 | 0:d1b1742b324c | 67 | speed = 0; |
tim008 | 0:d1b1742b324c | 68 | } |
tim008 | 0:d1b1742b324c | 69 | else { |
tim008 | 0:d1b1742b324c | 70 | radi = true; |
tim008 | 0:d1b1742b324c | 71 | speed = 900; |
tim008 | 0:d1b1742b324c | 72 | } |
tim008 | 0:d1b1742b324c | 73 | motor.step(numstep,smjer,speed); |
tim008 | 0:d1b1742b324c | 74 | } |
tim008 | 0:d1b1742b324c | 75 | |
tim008 | 0:d1b1742b324c | 76 | int main() { |
tim008 | 0:d1b1742b324c | 77 | char naredba; |
tim008 | 0:d1b1742b324c | 78 | motor.step(numstep,smjer,speed); |
tim008 | 0:d1b1742b324c | 79 | while(1) { |
tim008 | 0:d1b1742b324c | 80 | objasni(); |
tim008 | 0:d1b1742b324c | 81 | pali_gasi(); |
tim008 | 0:d1b1742b324c | 82 | naredba = ocitajNaredbu(); |
tim008 | 0:d1b1742b324c | 83 | if(naredba == 'A') postaviUgaoOsovine(); |
tim008 | 0:d1b1742b324c | 84 | else if(naredba == 'B') promijeniSmjer(); |
tim008 | 0:d1b1742b324c | 85 | else if(naredba == 'C') promijeniBrzinu(); |
tim008 | 0:d1b1742b324c | 86 | else if(naredba == 'D') startOrStop(); |
tim008 | 0:d1b1742b324c | 87 | wait(1); |
tim008 | 0:d1b1742b324c | 88 | } |
tim008 | 0:d1b1742b324c | 89 | } |