Muris Nuhodžić Edis Kunić

Dependencies:   mbed sMotor

Committer:
tim010
Date:
Wed Jun 04 22:05:15 2014 +0000
Revision:
1:d75b1ae184ea
Parent:
0:0b1211077b9a
promjena

Who changed what in which revision?

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