Simply creates a servo object from a motor object, to allow the control of the angle. uses the solar panel to look for the brightest spot, then stops.
Dependencies: mbed
Fork of Lab6_Basic by
Servo.cpp@3:b787aa49b900, 2016-09-19 (annotated)
- Committer:
- ziadeldebri
- Date:
- Mon Sep 19 17:06:45 2016 +0000
- Revision:
- 3:b787aa49b900
- Child:
- 4:b3a93554fedf
Scott Test;
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 | 3:b787aa49b900 | 6 | float value_target_raw; |
ziadeldebri | 3:b787aa49b900 | 7 | Ticker tick; |
ziadeldebri | 3:b787aa49b900 | 8 | SLCD slcd; |
ziadeldebri | 3:b787aa49b900 | 9 | int n; |
ziadeldebri | 3:b787aa49b900 | 10 | int counter; |
ziadeldebri | 3:b787aa49b900 | 11 | char buffer[50]; |
ziadeldebri | 3:b787aa49b900 | 12 | |
ziadeldebri | 3:b787aa49b900 | 13 | Servo::Servo(PinName analog_input,PinName Positive, PinName Negative,PinName Speed): |
ziadeldebri | 3:b787aa49b900 | 14 | _feedback(analog_input),_motor(Positive,Negative,Speed) { |
ziadeldebri | 3:b787aa49b900 | 15 | |
ziadeldebri | 3:b787aa49b900 | 16 | } |
ziadeldebri | 3:b787aa49b900 | 17 | /* input is angle intger from 0 - 180 |
ziadeldebri | 3:b787aa49b900 | 18 | * |
ziadeldebri | 3:b787aa49b900 | 19 | */ |
ziadeldebri | 3:b787aa49b900 | 20 | void Servo::set(int degree) { |
ziadeldebri | 3:b787aa49b900 | 21 | if(degree > 90){ |
ziadeldebri | 3:b787aa49b900 | 22 | value_target = degree; // set the value target as the input of the function |
ziadeldebri | 3:b787aa49b900 | 23 | tick.attach(this,&Servo::move,0.001); // set the ticker to call move every 0.001 sec |
ziadeldebri | 3:b787aa49b900 | 24 | } |
ziadeldebri | 3:b787aa49b900 | 25 | else{ |
ziadeldebri | 3:b787aa49b900 | 26 | value_target = degree-15; // set the value target as the input of the function |
ziadeldebri | 3:b787aa49b900 | 27 | tick.attach(this,&Servo::move,0.001); |
ziadeldebri | 3:b787aa49b900 | 28 | } |
ziadeldebri | 3:b787aa49b900 | 29 | |
ziadeldebri | 3:b787aa49b900 | 30 | }//End of Move |
ziadeldebri | 3:b787aa49b900 | 31 | |
ziadeldebri | 3:b787aa49b900 | 32 | void Servo::move() { |
ziadeldebri | 3:b787aa49b900 | 33 | |
ziadeldebri | 3:b787aa49b900 | 34 | value_current = (int)((int)(_feedback*1000)*0.30769230769); |
ziadeldebri | 3:b787aa49b900 | 35 | |
ziadeldebri | 3:b787aa49b900 | 36 | if(value_target > value_current){ |
ziadeldebri | 3:b787aa49b900 | 37 | _motor.Direction(LEFT); |
ziadeldebri | 3:b787aa49b900 | 38 | // _motor.Speed(10); |
ziadeldebri | 3:b787aa49b900 | 39 | //wait(0.1); |
ziadeldebri | 3:b787aa49b900 | 40 | n = sprintf (buffer, "%d", value_current); |
ziadeldebri | 3:b787aa49b900 | 41 | slcd.clear(); |
ziadeldebri | 3:b787aa49b900 | 42 | slcd.Home(); |
ziadeldebri | 3:b787aa49b900 | 43 | slcd.printf(buffer); |
ziadeldebri | 3:b787aa49b900 | 44 | } |
ziadeldebri | 3:b787aa49b900 | 45 | else if(value_target < value_current){ |
ziadeldebri | 3:b787aa49b900 | 46 | _motor.Direction(RIGHT); |
ziadeldebri | 3:b787aa49b900 | 47 | // _motor.Speed(counter); |
ziadeldebri | 3:b787aa49b900 | 48 | n = sprintf (buffer, "%d", value_current); |
ziadeldebri | 3:b787aa49b900 | 49 | slcd.clear(); |
ziadeldebri | 3:b787aa49b900 | 50 | slcd.Home(); |
ziadeldebri | 3:b787aa49b900 | 51 | slcd.printf(buffer); |
ziadeldebri | 3:b787aa49b900 | 52 | } |
ziadeldebri | 3:b787aa49b900 | 53 | else if((value_target == value_current)) |
ziadeldebri | 3:b787aa49b900 | 54 | { |
ziadeldebri | 3:b787aa49b900 | 55 | tick.detach(); |
ziadeldebri | 3:b787aa49b900 | 56 | _motor.Stop(); |
ziadeldebri | 3:b787aa49b900 | 57 | n = sprintf (buffer, "%d", value_current); |
ziadeldebri | 3:b787aa49b900 | 58 | slcd.clear(); |
ziadeldebri | 3:b787aa49b900 | 59 | slcd.Home(); |
ziadeldebri | 3:b787aa49b900 | 60 | slcd.printf(buffer); |
ziadeldebri | 3:b787aa49b900 | 61 | } |
ziadeldebri | 3:b787aa49b900 | 62 | |
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 | void Servo::check() { |
ziadeldebri | 3:b787aa49b900 | 70 | |
ziadeldebri | 3:b787aa49b900 | 71 | value_current = (int)((int)(_feedback*1000)*0.30769230769); |
ziadeldebri | 3:b787aa49b900 | 72 | n = sprintf (buffer, "%d", value_current); |
ziadeldebri | 3:b787aa49b900 | 73 | slcd.clear(); |
ziadeldebri | 3:b787aa49b900 | 74 | slcd.Home(); |
ziadeldebri | 3:b787aa49b900 | 75 | slcd.printf(buffer); |
ziadeldebri | 3:b787aa49b900 | 76 | |
ziadeldebri | 3:b787aa49b900 | 77 | } |
ziadeldebri | 3:b787aa49b900 | 78 | |
ziadeldebri | 3:b787aa49b900 | 79 | |
ziadeldebri | 3:b787aa49b900 | 80 |