Added various bits, main reformatted. Added .get to class Servo to allow waiting for rotation before recording information.
Fork of Lab6_Basic by
Diff: Motor.cpp
- Revision:
- 6:a64d79286726
- Parent:
- 3:b787aa49b900
- Child:
- 8:c2f0e524696b
--- a/Motor.cpp Fri Nov 18 03:59:01 2016 +0000 +++ b/Motor.cpp Sat Nov 19 08:32:04 2016 +0000 @@ -1,38 +1,86 @@ -#include "mbed.h" -#include "Motor.h" + #include "mbed.h" + #include "Servo.h" + // gloabl varevlbe used only inseide the class +int value_current; +int value_target; +float value_target_raw; +Ticker tick; +SLCD slcd; +int n; +int counter; +char buffer[50]; +Servo::Servo(PinName analog_input,PinName Positive, PinName Negative,PinName Speed): + _feedback(analog_input),_motor(Positive,Negative,Speed) { + +} + /* input is angle intger from 0 - 180 + * + */ +void Servo::set(int degree) { + if(degree > 90){ + value_target = degree; // set the value target as the input of the function + tick.attach(this,&Servo::move,0.001); // set the ticker to call move every 0.001 sec +} + else{ + value_target = degree-15; // set the value target as the input of the function + tick.attach(this,&Servo::move,0.001); + } + + }//End of Move + +void Servo::move() { + +value_current = (int)((int)(_feedback*1000)*0.30769230769); + +if(value_target > value_current){ + _motor.Direction(LEFT); + // _motor.Speed(10); + //wait(0.1); + n = sprintf (buffer, "%d", value_current); + slcd.clear(); + slcd.Home(); + slcd.printf(buffer); + } +else if(value_target < value_current){ + _motor.Direction(RIGHT); + // _motor.Speed(counter); + n = sprintf (buffer, "%d", value_current); + slcd.clear(); + slcd.Home(); + slcd.printf(buffer); + } +else if((value_target == value_current)) + { + tick.detach(); + _motor.Stop(); + n = sprintf (buffer, "%d", value_current); + slcd.clear(); + slcd.Home(); + slcd.printf(buffer); + } -Motor::Motor(PinName Positive, PinName Negative,PinName Speed): - _positive(Positive), _negative(Negative), _speed(Speed) { - _speed.period(0.03f); // - _speed.write(0.45f); -} - /* Input 1 or 2. - * 1 : Postive VCC Negtaive GND, 2 : Postive GND Negtaive VCC. - */ -void Motor::Direction(int move) { - if(move == 1){ - _positive = 1; - _negative = 0; - } - else if(move == 2){ - _positive = 0; - _negative = 1; + + + } + + unsigned int Servo::get() + { + value_current = (int)((int)(_feedback*1000)*0.30769230769); + // n = sprintf (buffer, "%d", value_current); + return value_current; } - }//End of Direction - /* No input. - * 1 : Postive GND Negtaive GND - */ -void Motor::Stop() { - _positive = 0; - _negative = 0; - }//End of Stop - /* input is the . - * 1 : Postive GND Negtaive GND - */ -void Motor::Speed(int motor_speed) { - float percantage = motor_speed/100; - _speed.write(percantage); - }//End of Stop \ No newline at end of file +void Servo::check() { + + value_current = (int)((int)(_feedback*1000)*0.30769230769); + n = sprintf (buffer, "%d", value_current); + slcd.clear(); + slcd.Home(); + slcd.printf(buffer); + + } + + + \ No newline at end of file