Granulo Eldar Fahrudin Brbutović

Dependencies:   mbed sMotor

Committer:
tim003
Date:
Mon May 12 10:01:47 2014 +0000
Revision:
0:f2c718b5cb18
LV9-PAI-Grupa2-Tim003-Zadatak2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tim003 0:f2c718b5cb18 1 #include "mbed.h"
tim003 0:f2c718b5cb18 2 #include "sMotor.h"
tim003 0:f2c718b5cb18 3
tim003 0:f2c718b5cb18 4 #include <string>
tim003 0:f2c718b5cb18 5
tim003 0:f2c718b5cb18 6
tim003 0:f2c718b5cb18 7 #define PIN1 dp13
tim003 0:f2c718b5cb18 8 #define PIN2 dp11
tim003 0:f2c718b5cb18 9 #define PIN3 dp10
tim003 0:f2c718b5cb18 10 #define PIN4 dp9
tim003 0:f2c718b5cb18 11
tim003 0:f2c718b5cb18 12 Serial pc(USBTX, USBRX); //tx, rx
tim003 0:f2c718b5cb18 13 sMotor stepper_motor(PIN1, PIN2, PIN3, PIN4);
tim003 0:f2c718b5cb18 14
tim003 0:f2c718b5cb18 15 int step_speed = 1200 ; // set default motor speed
tim003 0:f2c718b5cb18 16 int numsteps = 512;
tim003 0:f2c718b5cb18 17 int oldsteps = 0;
tim003 0:f2c718b5cb18 18
tim003 0:f2c718b5cb18 19 Ticker rotator;
tim003 0:f2c718b5cb18 20 bool cw = true, working = true;
tim003 0:f2c718b5cb18 21
tim003 0:f2c718b5cb18 22 int speed =1200;
tim003 0:f2c718b5cb18 23
tim003 0:f2c718b5cb18 24 string buffer = "";
tim003 0:f2c718b5cb18 25
tim003 0:f2c718b5cb18 26 bool is_digit(char c) {
tim003 0:f2c718b5cb18 27 return c >= '0' && c <= '9';
tim003 0:f2c718b5cb18 28 }
tim003 0:f2c718b5cb18 29
tim003 0:f2c718b5cb18 30 void wrong_comm() {
tim003 0:f2c718b5cb18 31 pc.printf("Wrong command\n");
tim003 0:f2c718b5cb18 32 buffer = "";
tim003 0:f2c718b5cb18 33 }
tim003 0:f2c718b5cb18 34
tim003 0:f2c718b5cb18 35 void rotate() {
tim003 0:f2c718b5cb18 36 if(working) {
tim003 0:f2c718b5cb18 37 cw ? stepper_motor.step(numsteps, 0, speed) :
tim003 0:f2c718b5cb18 38 stepper_motor.step(numsteps, 1, speed);
tim003 0:f2c718b5cb18 39 }
tim003 0:f2c718b5cb18 40 }
tim003 0:f2c718b5cb18 41
tim003 0:f2c718b5cb18 42 int main() {
tim003 0:f2c718b5cb18 43
tim003 0:f2c718b5cb18 44 rotator.attach(&rotate, .001);
tim003 0:f2c718b5cb18 45
tim003 0:f2c718b5cb18 46 /*
tim003 0:f2c718b5cb18 47 PROTOCOL:
tim003 0:f2c718b5cb18 48
tim003 0:f2c718b5cb18 49 Axxx - set angle to xxx (out of 360)
tim003 0:f2c718b5cb18 50 Sxxx - set motor speed to xxx
tim003 0:f2c718b5cb18 51 W - toggle turn motor on/off
tim003 0:f2c718b5cb18 52 C - set cw/ccw rotation direction
tim003 0:f2c718b5cb18 53 */
tim003 0:f2c718b5cb18 54
tim003 0:f2c718b5cb18 55 while(true) {
tim003 0:f2c718b5cb18 56 buffer.push_back(pc.getc());
tim003 0:f2c718b5cb18 57 if(buffer.length() == 4) {
tim003 0:f2c718b5cb18 58 if(!is_digit(buffer[1]) || !is_digit(buffer[2]) || !is_digit(buffer[3]))
tim003 0:f2c718b5cb18 59 wrong_comm();
tim003 0:f2c718b5cb18 60 int value = buffer[3] + buffer[2] * 10 + buffer[1] * 100;
tim003 0:f2c718b5cb18 61 switch(buffer[0]) {
tim003 0:f2c718b5cb18 62 case 'A':
tim003 0:f2c718b5cb18 63 oldsteps = numsteps;
tim003 0:f2c718b5cb18 64 numsteps = value;
tim003 0:f2c718b5cb18 65 if(numsteps > oldsteps)
tim003 0:f2c718b5cb18 66 stepper_motor.step(numsteps, 1, speed);
tim003 0:f2c718b5cb18 67 else if(numsteps < oldsteps) stepper_motor.step(numsteps, 0, speed);
tim003 0:f2c718b5cb18 68 buffer = "";
tim003 0:f2c718b5cb18 69 break;
tim003 0:f2c718b5cb18 70
tim003 0:f2c718b5cb18 71 case 'S':
tim003 0:f2c718b5cb18 72 speed = value;
tim003 0:f2c718b5cb18 73 buffer = "";
tim003 0:f2c718b5cb18 74
tim003 0:f2c718b5cb18 75 break;
tim003 0:f2c718b5cb18 76
tim003 0:f2c718b5cb18 77 default:
tim003 0:f2c718b5cb18 78 wrong_comm();
tim003 0:f2c718b5cb18 79 }
tim003 0:f2c718b5cb18 80 } else if(buffer.length() == 1 && buffer[0] == 'W')
tim003 0:f2c718b5cb18 81 working = !working;
tim003 0:f2c718b5cb18 82 else if(buffer.length() == 1 && buffer[0] == 'C')
tim003 0:f2c718b5cb18 83 cw = !cw;
tim003 0:f2c718b5cb18 84 // stepper_motor.pins = 3;
tim003 0:f2c718b5cb18 85 }
tim003 0:f2c718b5cb18 86
tim003 0:f2c718b5cb18 87
tim003 0:f2c718b5cb18 88 }