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