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

Files at this revision

API Documentation at this revision

Comitter:
dogcatfee
Date:
Fri Nov 03 19:28:37 2017 -0700
Parent:
11:2bb1624d324b
Commit message:
Cleanup, comment, position fixes.

Changed in this revision

Motor.cpp Show annotated file Show diff for this revision Revisions of this file
Servo.cpp Show annotated file Show diff for this revision Revisions of this file
Servo.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Motor.cpp	Fri Nov 03 22:06:05 2017 +0000
+++ b/Motor.cpp	Fri Nov 03 19:28:37 2017 -0700
@@ -5,9 +5,9 @@
 
 Motor::Motor(PinName Positive, PinName Negative,PinName Speed):
         _positive(Positive), _negative(Negative), _speed(Speed) {
-     _speed.period(0.03f);      // 
-     _speed.write(0.45f); 
-}   
+     _speed.period(0.03f);      //
+     _speed.write(0.45f);
+}
      /* Input 1 or 2.
      *  1 : Postive VCC Negtaive GND, 2 : Postive GND Negtaive VCC.
      */
@@ -16,23 +16,23 @@
      _positive = 1;
      _negative = 0;
     }
-    else if(move == 2){   
+    else if(move == 2){
      _positive = 0;
      _negative = 1;
         }
     }//End of Direction
-   
+
      /* No input.
-     *  1 : Postive GND Negtaive GND  
+     *  1 : Postive GND Negtaive GND
      */
 void Motor::Stop() {
      _positive = 0;
      _negative = 0;
     }//End of Stop
     /* input is the .
-     *  1 : Postive GND Negtaive GND  
+     *  1 : Postive GND Negtaive GND
      */
 void Motor::Speed(int motor_speed) {
    float percantage = motor_speed/100;
-   _speed.write(percantage); 
-    }//End of Stop
\ No newline at end of file
+   _speed.write(percantage);
+    }//End of Stop
--- a/Servo.cpp	Fri Nov 03 22:06:05 2017 +0000
+++ b/Servo.cpp	Fri Nov 03 19:28:37 2017 -0700
@@ -1,85 +1,121 @@
- #include "mbed.h"
- #include "Servo.h"
- // gloabl varevlbe used only inseide the class 
+#include "mbed.h"
+#include "Servo.h"
+// gloabl varevlbe used only inseide the class
 int value_current;
 int value_target;
 float value_target_raw;
 Ticker tick;
 SLCD slcd;
 int n;
-int counter;
 char buffer[50];
-
+unsigned int counter;
 
 Servo::Servo(PinName analog_input,PinName Positive, PinName Negative,PinName Speed):
-     _feedback(analog_input),_motor(Positive,Negative,Speed) {
-      
-}   
-     /* input is angle intger from 0 - 180 
-     *  
-     */
-void Servo::set(int degree) {
-   if(degree > 90){
-   value_target = degree;   // set the value target as the input of the function
-   tick.attach(this,&Servo::move,0.001); // set the ticker to call move every 0.001 sec
+  _feedback(analog_input), _motor(Positive,Negative,Speed) 
+{
+
 }
-  else{
-   value_target = degree;   // set the value target as the input of the function
-   tick.attach(this,&Servo::move,0.001);   
-      } 
-  
-       }//End of Move
-   
-void Servo::move() {
+/*
+   Degree should be a value from 0 to 180.
+   Degree is really from 0 to 100(percent).
+*/
+void Servo::set(int degree) 
+{
+  /*
+     Sets value_target to the degree value that user wants servo to go to.
+  */
+  value_target = degree;
+  /*
+     Counter to store number of tries for motor to be in position.
+  */
+  counter = 0;
+  /*
+     Add the servo's move function to the ticker, runs every 1ms.
+  */
+  tick.attach(this, &Servo::move, 0.01); // set the ticker to call move every 0.001 sec
+}
 
-//value_current = (int)((int)(_feedback*1000)*0.30769230769);
-value_current = (int)((int)(_feedback*100));
-
+void Servo::move() 
+{
+  /*
+     value_target is destination
+     value_current is read from analog
+     slcd for the LCD display
+  */
 
-if(value_target > value_current){ 
-        _motor.Direction(LEFT);
-      //  _motor.Speed(10);
-        //wait(0.1);
-        n = sprintf (buffer, "%d", value_current);
-        slcd.clear();
-        slcd.Home();  
-        slcd.printf(buffer); 
-       }
-else if(value_target < value_current){
-        _motor.Direction(RIGHT);
-      //  _motor.Speed(counter);
-        n = sprintf (buffer, "%d", value_current);
-        slcd.clear();
-        slcd.Home();  
-        slcd.printf(buffer);
-        }     
-else if((value_target == value_current))
- {
-        tick.detach();
-        _motor.Stop();
-        n = sprintf (buffer, "%d", value_current);
-        slcd.clear();
-        slcd.Home();  
-        slcd.printf(buffer);
-       }
+  /* Read the potentiometer value. */
+  value_current = _feedback*100;
+  /*
+     When target value is higher relative to
+     the value read from the potentiometer
+     rotate to the left (Couter-Clockwise).
+  */
+  if(value_target > value_current)
+  {
+    _motor.Direction(LEFT);
+    counter ++;
+  }
+  /*
+     When target value is lower relative to
+     the value read from the potentiometer
+     rotate to the right.  (Clockwise).
+  */
+  else if(value_target < value_current)
+  {
+    _motor.Direction(RIGHT);
+    counter ++;
+  }
+  /*
+     When the motor has been set 2000 times
+     change the target value to the current
+     value of the analog reading, set the
+     counter to 0. 
 
-      
-
-      }
-      
-      
-   
-void Servo::check() {
-
-  value_current = (int)((int)(_feedback*1000)*0.30769230769);
+     Used to prevent rocking between values
+     when the MBED cannot get the value from
+     the potentiometer.
+  */
+  else if(counter > 2000)
+  {
+    value_target = value_current;
+    counter = 0;
+  }
+  /*
+     If no if statements are true, stop
+     the motor and detatch the tick process.
+  */
+  else
+  {
+    tick.detach();
+    _motor.Stop();
+  }
+  /*
+     Sprintf to copy the current value into,
+     the buffer( n stores status of sprintf()).
+  */
+  n = sprintf (buffer, "%d", value_current);
+  slcd.clear();                /* Clear the SLCD */
+  slcd.Home();              /* Set the decimal point on SLCD */
+  slcd.printf(buffer);   /* Write the buffer to the SLCD */
+}
+/*
+   Check function to write to SLCD,
+   not used.
+*/
+void Servo::check()
+{
+  value_current = _feedback*100;
   n = sprintf (buffer, "%d", value_current);
   slcd.clear();
-  slcd.Home();  
+  slcd.Home();
   slcd.printf(buffer);
-
-
-   
-      }
-            
-      
-      
\ No newline at end of file
+}
+/*
+   Returns current position of the analog dial.
+   0 to 100.
+*/
+unsigned int Servo::get()
+{
+  value_current = _feedback*100;
+  return value_current;
+}
--- a/Servo.h	Fri Nov 03 22:06:05 2017 +0000
+++ b/Servo.h	Fri Nov 03 19:28:37 2017 -0700
@@ -3,7 +3,7 @@
 #include "SLCD.h"
 #ifndef MBED_SERVO_H
 #define MBED_SERVO_H
- 
+
 /** Servo control class, based on a Motor Class object
  *
  * Example:
@@ -13,39 +13,42 @@
  * #include "Servo.h"
  * Motor my_motor(P12,P13P,p11);
  * Servo my_servo(p21,my_motor);
- * 
+ *
  * int main() {
- *     
+ *
  * }
  * @endcode
  */
 class Servo {
- 
+
 public:
     /** Create a servo object.
      *
-     * @param pin AnalogIn pin to be feedback, motor is a Motor object from Motor.h  
+     * @param pin AnalogIn pin to be feedback, motor is a Motor object from Motor.h
      */
     Servo(PinName analog_input,PinName Positive, PinName Negative,PinName Speed);
-    
+
     /** Set the servo position
      *
      * @param angle intger in degrees 0-180 to represent the full range.
      */
     void set(int degree);
-    
+
     /**  Read the servo current position in degrees from 0-180
      *
-     * @return returns the current anlge of the servo   
-     */   
-    
+     * @return returns the current anlge of the servo
+     */
+
      void move(void);
+
+     void check();
+
+     unsigned int get();
+
      
-     void check();
 protected:
     AnalogIn _feedback;
     Motor    _motor;
 };
- 
+
 #endif
- 
\ No newline at end of file
--- a/main.cpp	Fri Nov 03 22:06:05 2017 +0000
+++ b/main.cpp	Fri Nov 03 19:28:37 2017 -0700
@@ -9,25 +9,17 @@
 /*  Description:              */
 /*                                                                                      */
 /****************************************************************************************/
+// Edit ~ Matthew Sessions
 
-//              A3   Digi  Digi  PWM
+//               A    Digi  Digi  PWM
 //Servo my_servo(PTB3,PTE19,PTE18,PTE31);
 Servo my_servo(PTB3,PTB0,PTB1,PTE29);
-Serial pc(USBTX, USBRX); // tx, rx
 
-
-int main() {
-
-while (1) {  
-
-
-my_servo.set(24);
-wait(4); 
-my_servo.set(73);
-wait(4); 
-my_servo.set(98);
-wait(4);
-
-
-    }
+int main()
+{
+      for(int i = 10 ; i < 100; i+=5 )
+      {
+        my_servo.set(i);
+        wait(4);
+      }
 }