Added various bits, main reformatted. Added .get to class Servo to allow waiting for rotation before recording information.

Dependencies:   SLCD mbed

Fork of Lab6_Basic by ECE 111 At Oregon State University

Committer:
Dogcatfee
Date:
Sat Nov 19 09:10:23 2016 +0000
Revision:
8:c2f0e524696b
Parent:
6:a64d79286726
Fixed the Motor header;

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
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
Dogcatfee 8:c2f0e524696b 63
ziadeldebri 3:b787aa49b900 64
ziadeldebri 3:b787aa49b900 65
ziadeldebri 3:b787aa49b900 66 }
ziadeldebri 3:b787aa49b900 67
Dogcatfee 8:c2f0e524696b 68 unsigned int Servo::get()
Dogcatfee 8:c2f0e524696b 69 {
Dogcatfee 8:c2f0e524696b 70 value_current = (int)((int)(_feedback*1000)*0.30769230769);
Dogcatfee 8:c2f0e524696b 71 // n = sprintf (buffer, "%d", value_current);
Dogcatfee 8:c2f0e524696b 72 return value_current;
Dogcatfee 8:c2f0e524696b 73 }
ziadeldebri 3:b787aa49b900 74
ziadeldebri 3:b787aa49b900 75 void Servo::check() {
ziadeldebri 3:b787aa49b900 76
Dogcatfee 8:c2f0e524696b 77 value_current = (int)((int)(_feedback*1000)*0.30769230769);
Dogcatfee 8:c2f0e524696b 78 n = sprintf (buffer, "%d", value_current);
ziadeldebri 3:b787aa49b900 79 slcd.clear();
ziadeldebri 3:b787aa49b900 80 slcd.Home();
ziadeldebri 3:b787aa49b900 81 slcd.printf(buffer);
ziadeldebri 3:b787aa49b900 82
ziadeldebri 3:b787aa49b900 83 }
ziadeldebri 3:b787aa49b900 84
ziadeldebri 3:b787aa49b900 85
ziadeldebri 3:b787aa49b900 86