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 08:32:04 2016 +0000
Revision:
6:a64d79286726
Parent:
4:b3a93554fedf
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?

UserRevisionLine numberNew contents of line
ziadeldebri 3:b787aa49b900 1 #include "mbed.h"
ziadeldebri 3:b787aa49b900 2 #include "Servo.h"
Dogcatfee 6:a64d79286726 3 //added .get() to approximate position
ziadeldebri 3:b787aa49b900 4 // gloabl varevlbe used only inseide the class
ziadeldebri 3:b787aa49b900 5 int value_current;
ziadeldebri 3:b787aa49b900 6 int value_target;
ziadeldebri 4:b3a93554fedf 7 int sum;
ziadeldebri 3:b787aa49b900 8 float value_target_raw;
ziadeldebri 3:b787aa49b900 9 Ticker tick;
ziadeldebri 3:b787aa49b900 10 SLCD slcd;
ziadeldebri 3:b787aa49b900 11 int n;
ziadeldebri 3:b787aa49b900 12 int counter;
ziadeldebri 3:b787aa49b900 13 char buffer[50];
ziadeldebri 3:b787aa49b900 14
ziadeldebri 3:b787aa49b900 15 Servo::Servo(PinName analog_input,PinName Positive, PinName Negative,PinName Speed):
ziadeldebri 3:b787aa49b900 16 _feedback(analog_input),_motor(Positive,Negative,Speed) {
ziadeldebri 4:b3a93554fedf 17 sum=0;
ziadeldebri 3:b787aa49b900 18
ziadeldebri 3:b787aa49b900 19 }
ziadeldebri 3:b787aa49b900 20 /* input is angle intger from 0 - 180
ziadeldebri 3:b787aa49b900 21 *
ziadeldebri 3:b787aa49b900 22 */
ziadeldebri 3:b787aa49b900 23 void Servo::set(int degree) {
ziadeldebri 3:b787aa49b900 24 if(degree > 90){
ziadeldebri 3:b787aa49b900 25 value_target = degree; // set the value target as the input of the function
ziadeldebri 3:b787aa49b900 26 tick.attach(this,&Servo::move,0.001); // set the ticker to call move every 0.001 sec
ziadeldebri 3:b787aa49b900 27 }
ziadeldebri 3:b787aa49b900 28 else{
ziadeldebri 3:b787aa49b900 29 value_target = degree-15; // set the value target as the input of the function
ziadeldebri 3:b787aa49b900 30 tick.attach(this,&Servo::move,0.001);
ziadeldebri 3:b787aa49b900 31 }
ziadeldebri 3:b787aa49b900 32
ziadeldebri 3:b787aa49b900 33 }//End of Move
ziadeldebri 3:b787aa49b900 34
ziadeldebri 3:b787aa49b900 35 void Servo::move() {
ziadeldebri 3:b787aa49b900 36
ziadeldebri 3:b787aa49b900 37 value_current = (int)((int)(_feedback*1000)*0.30769230769);
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 4:b3a93554fedf 74
ziadeldebri 4:b3a93554fedf 75 for (int j = 0; j < 100; j++){
ziadeldebri 4:b3a93554fedf 76 sum += _feedback.read_u16();
ziadeldebri 4:b3a93554fedf 77 }
ziadeldebri 4:b3a93554fedf 78 value_current = sum / 100;
ziadeldebri 4:b3a93554fedf 79 sum = 0;
ziadeldebri 4:b3a93554fedf 80
ziadeldebri 4:b3a93554fedf 81 n = sprintf (buffer, "%x", value_current);
ziadeldebri 3:b787aa49b900 82 slcd.clear();
ziadeldebri 3:b787aa49b900 83 slcd.Home();
ziadeldebri 3:b787aa49b900 84 slcd.printf(buffer);
ziadeldebri 3:b787aa49b900 85
ziadeldebri 3:b787aa49b900 86 }
ziadeldebri 3:b787aa49b900 87
ziadeldebri 3:b787aa49b900 88
ziadeldebri 3:b787aa49b900 89