Simply creates a servo object from a motor object, to allow the control of the angle.

Dependencies:   mbed

Fork of Lab5_Basic by ziad eldebri

Committer:
dogcatfee
Date:
Fri Nov 03 15:04:26 2017 -0700
Revision:
9:e2909d7f36b8
Parent:
3:b787aa49b900
Child:
12:988fb6ffae63
Fix

Who changed what in which revision?

UserRevisionLine numberNew 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
dogcatfee 9:e2909d7f36b8 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 3:b787aa49b900 16
ziadeldebri 3:b787aa49b900 17 }
ziadeldebri 3:b787aa49b900 18 /* input is angle intger from 0 - 180
ziadeldebri 3:b787aa49b900 19 *
ziadeldebri 3:b787aa49b900 20 */
ziadeldebri 3:b787aa49b900 21 void Servo::set(int degree) {
ziadeldebri 3:b787aa49b900 22 if(degree > 90){
ziadeldebri 3:b787aa49b900 23 value_target = degree; // set the value target as the input of the function
ziadeldebri 3:b787aa49b900 24 tick.attach(this,&Servo::move,0.001); // set the ticker to call move every 0.001 sec
ziadeldebri 3:b787aa49b900 25 }
ziadeldebri 3:b787aa49b900 26 else{
dogcatfee 9:e2909d7f36b8 27 value_target = degree; // set the value target as the input of the function
ziadeldebri 3:b787aa49b900 28 tick.attach(this,&Servo::move,0.001);
ziadeldebri 3:b787aa49b900 29 }
ziadeldebri 3:b787aa49b900 30
ziadeldebri 3:b787aa49b900 31 }//End of Move
ziadeldebri 3:b787aa49b900 32
ziadeldebri 3:b787aa49b900 33 void Servo::move() {
ziadeldebri 3:b787aa49b900 34
dogcatfee 9:e2909d7f36b8 35 //value_current = (int)((int)(_feedback*1000)*0.30769230769);
dogcatfee 9:e2909d7f36b8 36 value_current = (int)((int)(_feedback*100));
dogcatfee 9:e2909d7f36b8 37
ziadeldebri 3:b787aa49b900 38
ziadeldebri 3:b787aa49b900 39 if(value_target > value_current){
ziadeldebri 3:b787aa49b900 40 _motor.Direction(LEFT);
ziadeldebri 3:b787aa49b900 41 // _motor.Speed(10);
ziadeldebri 3:b787aa49b900 42 //wait(0.1);
ziadeldebri 3:b787aa49b900 43 n = sprintf (buffer, "%d", value_current);
ziadeldebri 3:b787aa49b900 44 slcd.clear();
ziadeldebri 3:b787aa49b900 45 slcd.Home();
ziadeldebri 3:b787aa49b900 46 slcd.printf(buffer);
ziadeldebri 3:b787aa49b900 47 }
ziadeldebri 3:b787aa49b900 48 else if(value_target < value_current){
ziadeldebri 3:b787aa49b900 49 _motor.Direction(RIGHT);
ziadeldebri 3:b787aa49b900 50 // _motor.Speed(counter);
ziadeldebri 3:b787aa49b900 51 n = sprintf (buffer, "%d", value_current);
ziadeldebri 3:b787aa49b900 52 slcd.clear();
ziadeldebri 3:b787aa49b900 53 slcd.Home();
ziadeldebri 3:b787aa49b900 54 slcd.printf(buffer);
ziadeldebri 3:b787aa49b900 55 }
ziadeldebri 3:b787aa49b900 56 else if((value_target == value_current))
ziadeldebri 3:b787aa49b900 57 {
ziadeldebri 3:b787aa49b900 58 tick.detach();
ziadeldebri 3:b787aa49b900 59 _motor.Stop();
ziadeldebri 3:b787aa49b900 60 n = sprintf (buffer, "%d", value_current);
ziadeldebri 3:b787aa49b900 61 slcd.clear();
ziadeldebri 3:b787aa49b900 62 slcd.Home();
ziadeldebri 3:b787aa49b900 63 slcd.printf(buffer);
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
ziadeldebri 3:b787aa49b900 72 void Servo::check() {
ziadeldebri 3:b787aa49b900 73
ziadeldebri 3:b787aa49b900 74 value_current = (int)((int)(_feedback*1000)*0.30769230769);
ziadeldebri 3:b787aa49b900 75 n = sprintf (buffer, "%d", value_current);
ziadeldebri 3:b787aa49b900 76 slcd.clear();
ziadeldebri 3:b787aa49b900 77 slcd.Home();
ziadeldebri 3:b787aa49b900 78 slcd.printf(buffer);
dogcatfee 9:e2909d7f36b8 79
dogcatfee 9:e2909d7f36b8 80
ziadeldebri 3:b787aa49b900 81
ziadeldebri 3:b787aa49b900 82 }
ziadeldebri 3:b787aa49b900 83
ziadeldebri 3:b787aa49b900 84
ziadeldebri 3:b787aa49b900 85