tim010 tim010
/
lv9_grupa5_tim010_zad2
Muris Nuhodzic Amela Spica
main.cpp@0:e6ee6ef61c70, 2014-05-15 (annotated)
- Committer:
- tim010
- Date:
- Thu May 15 13:58:37 2014 +0000
- Revision:
- 0:e6ee6ef61c70
Muris Nuhodzic; Amela Spica
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tim010 | 0:e6ee6ef61c70 | 1 | #include "mbed.h" |
tim010 | 0:e6ee6ef61c70 | 2 | #include "sMotor.h" |
tim010 | 0:e6ee6ef61c70 | 3 | |
tim010 | 0:e6ee6ef61c70 | 4 | |
tim010 | 0:e6ee6ef61c70 | 5 | Serial pc(USBTX, USBRX); |
tim010 | 0:e6ee6ef61c70 | 6 | sMotor motor(dp9, dp10, dp11, dp13); // creates new stepper motor: IN1, IN2, IN3, IN4 |
tim010 | 0:e6ee6ef61c70 | 7 | |
tim010 | 0:e6ee6ef61c70 | 8 | int step_speed = 1200 ; // set default motor speed |
tim010 | 0:e6ee6ef61c70 | 9 | int numstep = 512 ; // defines full turn of 360 degree |
tim010 | 0:e6ee6ef61c70 | 10 | int direction = 0; //0 for right, 1 for left |
tim010 | 0:e6ee6ef61c70 | 11 | bool upaljen = false; |
tim010 | 0:e6ee6ef61c70 | 12 | char c; |
tim010 | 0:e6ee6ef61c70 | 13 | Ticker t; |
tim010 | 0:e6ee6ef61c70 | 14 | |
tim010 | 0:e6ee6ef61c70 | 15 | |
tim010 | 0:e6ee6ef61c70 | 16 | void meni() |
tim010 | 0:e6ee6ef61c70 | 17 | { |
tim010 | 0:e6ee6ef61c70 | 18 | pc.printf("Default Speed: %d, press: \n\r",step_speed); |
tim010 | 0:e6ee6ef61c70 | 19 | pc.printf("1- postavljanje u proizvoljan polozaj\n\r"); |
tim010 | 0:e6ee6ef61c70 | 20 | pc.printf("2- promjena smjera kretanja motora\n\r"); |
tim010 | 0:e6ee6ef61c70 | 21 | pc.printf("3- promjena brzine\n\r"); |
tim010 | 0:e6ee6ef61c70 | 22 | pc.printf("4- pokretanje/zaustavljanje motora\n\r"); |
tim010 | 0:e6ee6ef61c70 | 23 | } |
tim010 | 0:e6ee6ef61c70 | 24 | |
tim010 | 0:e6ee6ef61c70 | 25 | void F() |
tim010 | 0:e6ee6ef61c70 | 26 | { |
tim010 | 0:e6ee6ef61c70 | 27 | motor.step(numstep,direction,step_speed); |
tim010 | 0:e6ee6ef61c70 | 28 | } |
tim010 | 0:e6ee6ef61c70 | 29 | void f1() |
tim010 | 0:e6ee6ef61c70 | 30 | { |
tim010 | 0:e6ee6ef61c70 | 31 | |
tim010 | 0:e6ee6ef61c70 | 32 | } |
tim010 | 0:e6ee6ef61c70 | 33 | int main() |
tim010 | 0:e6ee6ef61c70 | 34 | { |
tim010 | 0:e6ee6ef61c70 | 35 | meni(); |
tim010 | 0:e6ee6ef61c70 | 36 | while (1) { |
tim010 | 0:e6ee6ef61c70 | 37 | if (pc.readable()) { // checks for serial |
tim010 | 0:e6ee6ef61c70 | 38 | |
tim010 | 0:e6ee6ef61c70 | 39 | c=pc.getc(); |
tim010 | 0:e6ee6ef61c70 | 40 | if (c == '1') { |
tim010 | 0:e6ee6ef61c70 | 41 | if(!upaljen) { |
tim010 | 0:e6ee6ef61c70 | 42 | pc.printf("\n Motor je ugasen, upali motor \n"); |
tim010 | 0:e6ee6ef61c70 | 43 | meni(); |
tim010 | 0:e6ee6ef61c70 | 44 | continue; |
tim010 | 0:e6ee6ef61c70 | 45 | } |
tim010 | 0:e6ee6ef61c70 | 46 | int ugao; |
tim010 | 0:e6ee6ef61c70 | 47 | pc.printf("\n Unesi ugao: \n\r"); |
tim010 | 0:e6ee6ef61c70 | 48 | pc.scanf("%d", &ugao); |
tim010 | 0:e6ee6ef61c70 | 49 | if (ugao > 0 ) |
tim010 | 0:e6ee6ef61c70 | 50 | motor.step(numstep / 360 * (ugao % 360), direction, step_speed); // number of steps, direction, speed |
tim010 | 0:e6ee6ef61c70 | 51 | else |
tim010 | 0:e6ee6ef61c70 | 52 | motor.step(numstep / 360 * (ugao % 360), 1 - direction, step_speed); // number of steps, direction, speed |
tim010 | 0:e6ee6ef61c70 | 53 | meni();if(upaljen) motor.step(numstep, direction, step_speed); |
tim010 | 0:e6ee6ef61c70 | 54 | } |
tim010 | 0:e6ee6ef61c70 | 55 | |
tim010 | 0:e6ee6ef61c70 | 56 | if (c =='2') { |
tim010 | 0:e6ee6ef61c70 | 57 | direction = 1 - direction; |
tim010 | 0:e6ee6ef61c70 | 58 | pc.printf("\n Smjer je promjenjen\n"); |
tim010 | 0:e6ee6ef61c70 | 59 | meni();if(upaljen) motor.step(numstep, direction, step_speed); |
tim010 | 0:e6ee6ef61c70 | 60 | } |
tim010 | 0:e6ee6ef61c70 | 61 | if ( c =='3') { |
tim010 | 0:e6ee6ef61c70 | 62 | pc.printf("\n trenutna brzina: %d\n\r", step_speed); |
tim010 | 0:e6ee6ef61c70 | 63 | pc.printf("\n Unesi novu brzinu: \n\r"); |
tim010 | 0:e6ee6ef61c70 | 64 | pc.scanf("%d",&step_speed); |
tim010 | 0:e6ee6ef61c70 | 65 | if(upaljen) motor.step(numstep, direction, step_speed); |
tim010 | 0:e6ee6ef61c70 | 66 | meni();if(upaljen) motor.step(numstep, direction, step_speed); |
tim010 | 0:e6ee6ef61c70 | 67 | } |
tim010 | 0:e6ee6ef61c70 | 68 | |
tim010 | 0:e6ee6ef61c70 | 69 | if (c =='4') { |
tim010 | 0:e6ee6ef61c70 | 70 | if(upaljen == false) { |
tim010 | 0:e6ee6ef61c70 | 71 | upaljen = true; |
tim010 | 0:e6ee6ef61c70 | 72 | motor.step(numstep, direction, step_speed); |
tim010 | 0:e6ee6ef61c70 | 73 | pc.printf("\n motor je upaljen \n"); |
tim010 | 0:e6ee6ef61c70 | 74 | } else { |
tim010 | 0:e6ee6ef61c70 | 75 | upaljen = false; |
tim010 | 0:e6ee6ef61c70 | 76 | motor.step(numstep, direction, 0); |
tim010 | 0:e6ee6ef61c70 | 77 | pc.printf("\n motor je ugasen \n"); |
tim010 | 0:e6ee6ef61c70 | 78 | } |
tim010 | 0:e6ee6ef61c70 | 79 | meni();if(upaljen) motor.step(numstep, direction, step_speed); |
tim010 | 0:e6ee6ef61c70 | 80 | } |
tim010 | 0:e6ee6ef61c70 | 81 | } |
tim010 | 0:e6ee6ef61c70 | 82 | } |
tim010 | 0:e6ee6ef61c70 | 83 | } |