servo

Dependents:   followCar script_voor_project

Fork of Servo by Simon Ford

Files at this revision

API Documentation at this revision

Comitter:
backman
Date:
Fri Dec 02 13:03:33 2016 +0000
Parent:
3:36b69a7ced07
Commit message:
servo final;

Changed in this revision

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
diff -r 36b69a7ced07 -r b9fe7af92cd5 Servo.cpp
--- a/Servo.cpp	Thu Sep 02 17:34:43 2010 +0000
+++ b/Servo.cpp	Fri Dec 02 13:03:33 2016 +0000
@@ -24,51 +24,64 @@
 #include "Servo.h"
 #include "mbed.h"
 
-static float clamp(float value, float min, float max) {
-    if(value < min) {
-        return min;
-    } else if(value > max) {
-        return max;
-    } else {
-        return value;
-    }
-}
 
 Servo::Servo(PinName pin) : _pwm(pin) {
-    calibrate();
-    write(0.5);
-}
-
-void Servo::write(float percent) {
-    float offset = _range * 2.0 * (percent - 0.5);
-    _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
-    _p = clamp(percent, 0.0, 1.0);
+    
+    _pwm.period_ms(20);
+    wait(0.5);
 }
 
-void Servo::position(float degrees) {
-    float offset = _range * (degrees / _degrees);
-    _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
-}
+
+
+/*
+
 
-void Servo::calibrate(float range, float degrees) {
-    _range = range;
-    _degrees = degrees;
-}
+  
+    while(1){  
+       
+       p.pulsewidth(0.0010);
+       wait(1);
+       
+        p.pulsewidth((0.0016+0.0009)/2);
+       wait(1);
+       
+       p.pulsewidth(0.0015);
+       wait(1);
+       
+   
+   
+     
+  }
 
-float Servo::read() {
-    return _p;
-}
+*/
+
+
+
 
-Servo& Servo::operator= (float percent) { 
-    write(percent);
-    return *this;
-}
+
+
+void Servo::angle(float ang){
+    
+// l45->0.0010  1000us
+// r45->0.0015  1500us 
+
+
+//0.0010         0.0015
+//  -45        0          45
+         
+//  m= 250/45 =5.556
 
-Servo& Servo::operator= (Servo& rhs) {
-    write(rhs.read());
-    return *this;
-}
+//ang*x=    
 
-Servo::operator float() {
-    return read();
-}
+    _pwm.pulsewidth_us(ang*5.556+1250);    
+        
+    
+    
+    
+    
+    
+    
+    
+    
+    
+}
\ No newline at end of file
diff -r 36b69a7ced07 -r b9fe7af92cd5 Servo.h
--- a/Servo.h	Thu Sep 02 17:34:43 2010 +0000
+++ b/Servo.h	Fri Dec 02 13:03:33 2016 +0000
@@ -25,30 +25,6 @@
 
 #include "mbed.h"
 
-/** Servo control class, based on a PwmOut
- *
- * Example:
- * @code
- * // Continuously sweep the servo through it's full range
- * #include "mbed.h"
- * #include "Servo.h"
- * 
- * Servo myservo(p21);
- * 
- * int main() {
- *     while(1) {
- *         for(int i=0; i<100; i++) {
- *             myservo = i/100.0;
- *             wait(0.01);
- *         }
- *         for(int i=100; i>0; i--) {
- *             myservo = i/100.0;
- *             wait(0.01);
- *         }
- *     }
- * }
- * @endcode
- */
 class Servo {
 
 public:
@@ -62,37 +38,12 @@
      *
      * @param percent A normalised number 0.0-1.0 to represent the full range.
      */
-    void write(float percent);
-    
-    /**  Read the servo motors current position
-     *
-     * @param returns A normalised number 0.0-1.0  representing the full range.
-     */
-    float read();
-    
-    /** Set the servo position
-     *
-     * @param degrees Servo position in degrees
-     */
-    void position(float degrees);
-    
-    /**  Allows calibration of the range and angles for a particular servo
-     *
-     * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
-     * @param degrees Angle from centre to maximum/minimum position in degrees
-     */
-    void calibrate(float range = 0.0005, float degrees = 45.0); 
-        
-    /**  Shorthand for the write and read functions */
-    Servo& operator= (float percent);
-    Servo& operator= (Servo& rhs);
-    operator float();
+    void angle(float ang);
+   
 
 protected:
     PwmOut _pwm;
-    float _range;
-    float _degrees;
-    float _p;
+    
 };
 
 #endif