Added various bits, main reformatted. Added .get to class Servo to allow waiting for rotation before recording information.
Fork of Lab6_Basic by
Motor.cpp@6:a64d79286726, 2016-11-19 (annotated)
- Committer:
- Dogcatfee
- Date:
- Sat Nov 19 08:32:04 2016 +0000
- Revision:
- 6:a64d79286726
- Parent:
- 3:b787aa49b900
- Child:
- 8:c2f0e524696b
Added while/if statement to wait for rotation before recording.; Added .get() to Servo class, returns unsigned int of approximated rotation.; Added const int size to change size of arrays and loops; Added position_array with for fill.; Reformatted main.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dogcatfee | 6:a64d79286726 | 1 | #include "mbed.h" |
Dogcatfee | 6:a64d79286726 | 2 | #include "Servo.h" |
Dogcatfee | 6:a64d79286726 | 3 | // gloabl varevlbe used only inseide the class |
Dogcatfee | 6:a64d79286726 | 4 | int value_current; |
Dogcatfee | 6:a64d79286726 | 5 | int value_target; |
Dogcatfee | 6:a64d79286726 | 6 | float value_target_raw; |
Dogcatfee | 6:a64d79286726 | 7 | Ticker tick; |
Dogcatfee | 6:a64d79286726 | 8 | SLCD slcd; |
Dogcatfee | 6:a64d79286726 | 9 | int n; |
Dogcatfee | 6:a64d79286726 | 10 | int counter; |
Dogcatfee | 6:a64d79286726 | 11 | char buffer[50]; |
ziadeldebri | 3:b787aa49b900 | 12 | |
Dogcatfee | 6:a64d79286726 | 13 | Servo::Servo(PinName analog_input,PinName Positive, PinName Negative,PinName Speed): |
Dogcatfee | 6:a64d79286726 | 14 | _feedback(analog_input),_motor(Positive,Negative,Speed) { |
Dogcatfee | 6:a64d79286726 | 15 | |
Dogcatfee | 6:a64d79286726 | 16 | } |
Dogcatfee | 6:a64d79286726 | 17 | /* input is angle intger from 0 - 180 |
Dogcatfee | 6:a64d79286726 | 18 | * |
Dogcatfee | 6:a64d79286726 | 19 | */ |
Dogcatfee | 6:a64d79286726 | 20 | void Servo::set(int degree) { |
Dogcatfee | 6:a64d79286726 | 21 | if(degree > 90){ |
Dogcatfee | 6:a64d79286726 | 22 | value_target = degree; // set the value target as the input of the function |
Dogcatfee | 6:a64d79286726 | 23 | tick.attach(this,&Servo::move,0.001); // set the ticker to call move every 0.001 sec |
Dogcatfee | 6:a64d79286726 | 24 | } |
Dogcatfee | 6:a64d79286726 | 25 | else{ |
Dogcatfee | 6:a64d79286726 | 26 | value_target = degree-15; // set the value target as the input of the function |
Dogcatfee | 6:a64d79286726 | 27 | tick.attach(this,&Servo::move,0.001); |
Dogcatfee | 6:a64d79286726 | 28 | } |
Dogcatfee | 6:a64d79286726 | 29 | |
Dogcatfee | 6:a64d79286726 | 30 | }//End of Move |
Dogcatfee | 6:a64d79286726 | 31 | |
Dogcatfee | 6:a64d79286726 | 32 | void Servo::move() { |
Dogcatfee | 6:a64d79286726 | 33 | |
Dogcatfee | 6:a64d79286726 | 34 | value_current = (int)((int)(_feedback*1000)*0.30769230769); |
Dogcatfee | 6:a64d79286726 | 35 | |
Dogcatfee | 6:a64d79286726 | 36 | if(value_target > value_current){ |
Dogcatfee | 6:a64d79286726 | 37 | _motor.Direction(LEFT); |
Dogcatfee | 6:a64d79286726 | 38 | // _motor.Speed(10); |
Dogcatfee | 6:a64d79286726 | 39 | //wait(0.1); |
Dogcatfee | 6:a64d79286726 | 40 | n = sprintf (buffer, "%d", value_current); |
Dogcatfee | 6:a64d79286726 | 41 | slcd.clear(); |
Dogcatfee | 6:a64d79286726 | 42 | slcd.Home(); |
Dogcatfee | 6:a64d79286726 | 43 | slcd.printf(buffer); |
Dogcatfee | 6:a64d79286726 | 44 | } |
Dogcatfee | 6:a64d79286726 | 45 | else if(value_target < value_current){ |
Dogcatfee | 6:a64d79286726 | 46 | _motor.Direction(RIGHT); |
Dogcatfee | 6:a64d79286726 | 47 | // _motor.Speed(counter); |
Dogcatfee | 6:a64d79286726 | 48 | n = sprintf (buffer, "%d", value_current); |
Dogcatfee | 6:a64d79286726 | 49 | slcd.clear(); |
Dogcatfee | 6:a64d79286726 | 50 | slcd.Home(); |
Dogcatfee | 6:a64d79286726 | 51 | slcd.printf(buffer); |
Dogcatfee | 6:a64d79286726 | 52 | } |
Dogcatfee | 6:a64d79286726 | 53 | else if((value_target == value_current)) |
Dogcatfee | 6:a64d79286726 | 54 | { |
Dogcatfee | 6:a64d79286726 | 55 | tick.detach(); |
Dogcatfee | 6:a64d79286726 | 56 | _motor.Stop(); |
Dogcatfee | 6:a64d79286726 | 57 | n = sprintf (buffer, "%d", value_current); |
Dogcatfee | 6:a64d79286726 | 58 | slcd.clear(); |
Dogcatfee | 6:a64d79286726 | 59 | slcd.Home(); |
Dogcatfee | 6:a64d79286726 | 60 | slcd.printf(buffer); |
Dogcatfee | 6:a64d79286726 | 61 | } |
ziadeldebri | 3:b787aa49b900 | 62 | |
ziadeldebri | 3:b787aa49b900 | 63 | |
Dogcatfee | 6:a64d79286726 | 64 | |
Dogcatfee | 6:a64d79286726 | 65 | |
Dogcatfee | 6:a64d79286726 | 66 | } |
Dogcatfee | 6:a64d79286726 | 67 | |
Dogcatfee | 6:a64d79286726 | 68 | unsigned int Servo::get() |
Dogcatfee | 6:a64d79286726 | 69 | { |
Dogcatfee | 6:a64d79286726 | 70 | value_current = (int)((int)(_feedback*1000)*0.30769230769); |
Dogcatfee | 6:a64d79286726 | 71 | // n = sprintf (buffer, "%d", value_current); |
Dogcatfee | 6:a64d79286726 | 72 | return value_current; |
ziadeldebri | 3:b787aa49b900 | 73 | } |
ziadeldebri | 3:b787aa49b900 | 74 | |
Dogcatfee | 6:a64d79286726 | 75 | void Servo::check() { |
Dogcatfee | 6:a64d79286726 | 76 | |
Dogcatfee | 6:a64d79286726 | 77 | value_current = (int)((int)(_feedback*1000)*0.30769230769); |
Dogcatfee | 6:a64d79286726 | 78 | n = sprintf (buffer, "%d", value_current); |
Dogcatfee | 6:a64d79286726 | 79 | slcd.clear(); |
Dogcatfee | 6:a64d79286726 | 80 | slcd.Home(); |
Dogcatfee | 6:a64d79286726 | 81 | slcd.printf(buffer); |
Dogcatfee | 6:a64d79286726 | 82 | |
Dogcatfee | 6:a64d79286726 | 83 | } |
Dogcatfee | 6:a64d79286726 | 84 | |
Dogcatfee | 6:a64d79286726 | 85 | |
Dogcatfee | 6:a64d79286726 | 86 |