Servo file under construction.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Blaze513
Date:
Tue Aug 31 04:37:07 2010 +0000
Commit message:

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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.cpp	Tue Aug 31 04:37:07 2010 +0000
@@ -0,0 +1,181 @@
+#include "Servo.h"
+
+Servo::Servo(PinName sgnl) : SignalLine(sgnl), Period(0.02),
+    UpperRange(0.045), Center(0.075), LowerRange(-0.045), Position(0)
+{
+    SignalLine.period(0.02);
+        //set period to most common value;
+    SignalLine.write(0.075);
+        //set position to most common center
+}
+
+void Servo::position(float ratio) 
+{
+    Position = ratio;
+    if (ratio > 1)
+    {
+        ratio = 1;
+    }
+    else if (ratio < -1)
+    {
+        ratio = -1;
+    }
+        //saturate position to the range boundaries
+    if (ratio >= 0)
+    {
+        SignalLine.write((ratio * UpperRange) + Center);
+    }
+    else 
+    {
+        SignalLine.write((ratio * LowerRange) + Center);
+    }
+        //set normalized position (-1 to 1)
+}
+
+float Servo::position()
+{
+    float ratio = SignalLine.read() - Center;
+    if (ratio > UpperRange)
+    {
+        ratio = UpperRange;
+    }
+    else if (ratio < LowerRange)
+    {
+        ratio = LowerRange;
+    }
+        //saturate position to the range boundaries
+    if(ratio >= 0)
+    {
+        return ratio / UpperRange;
+    }
+    else 
+    {
+        return ratio / LowerRange;
+    }
+        //return normalized position
+}
+
+void Servo::maximum(float seconds) 
+{
+    UpperRange = (seconds / Period) - Center;
+    position(Position);
+}
+
+void Servo::maximum_ms(int milliseconds) 
+{
+    UpperRange = ((milliseconds / 1000) / Period) - Center;
+    position(Position);
+}
+
+void Servo::maximum_us(int microseconds) 
+{
+    UpperRange = ((microseconds / 1000000) / Period) - Center;
+    position(Position);
+}
+
+void Servo::center(float seconds)
+{
+    Center = seconds / Period;
+    position(Position);
+}
+
+void Servo::center_ms(int milliseconds)
+{
+    Center = (milliseconds / 1000) / Period;
+    position(Position);
+}
+
+void Servo::center_us(int microseconds)
+{
+    Center = (microseconds / 1000000) / Period;
+    position(Position);
+}
+
+void Servo::minimum(float seconds)
+{
+    LowerRange = (seconds / Period) - Center;
+    position(Position);
+}
+
+void Servo::minimum_ms(int milliseconds)
+{
+    LowerRange = ((milliseconds / 1000) / Period) - Center;
+    position(Position);
+}
+
+void Servo::minimum_us(int microseconds)
+{
+    LowerRange = ((microseconds / 1000000) / Period) - Center;
+    position(Position);
+}
+
+void Servo::period(float seconds)
+{
+    Period = seconds;
+    SignalLine.period(seconds);
+}
+
+void Servo::period_ms(int milliseconds)
+{
+    Period = milliseconds / 1000;
+    SignalLine.period_ms(milliseconds);
+}
+
+void Servo::period_us(int microseconds)
+{
+    Period = microseconds / 1000000;
+    SignalLine.period_us(microseconds);
+}
+
+void Servo::pulsewidth(float seconds)
+{
+    Position = (seconds / Period) - Center;
+    if (Position >=0)
+    {
+        Position /= UpperRange;
+    }
+    else
+    {
+        Position /= LowerRange * -1;
+    }
+    SignalLine.pulsewidth(seconds);
+}
+
+void Servo::pulsewidth_ms(int milliseconds)
+{
+    Position = ((milliseconds / 1000) / Period) - Center;
+    if (Position >=0)
+    {
+        Position /= UpperRange;
+    }
+    else
+    {
+        Position /= LowerRange * -1;
+    }
+    SignalLine.pulsewidth_ms(milliseconds);
+}
+
+void Servo::pulsewidth_us(int microseconds)
+{
+    Position = ((microseconds / 1000000) / Period) - Center;
+    if (Position >=0)
+    {
+        Position /= UpperRange;
+    }
+    else
+    {
+        Position /= LowerRange * -1;
+    }
+    SignalLine.pulsewidth_us(microseconds);
+}
+
+float Servo::operator =(float assignment)
+{
+    position(assignment);
+    return assignment;
+}
+
+Servo::operator float()
+{
+    return position();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.h	Tue Aug 31 04:37:07 2010 +0000
@@ -0,0 +1,48 @@
+#include "stdint.h"
+#include "mbed.h"
+
+class Servo
+{
+    private:
+        PwmOut SignalLine;
+
+        float Period;
+        float UpperRange;
+        float Center;
+        float LowerRange;
+        float Position;
+
+    public:
+        Servo(PinName sgnl);
+            //constructor sets common servo defaults
+        void position(float ratio);
+            //set normalized servo position (-1 to 1), default = 0
+        float position();
+            //get normalized servo position (-1 to 1)
+        void maximum(float seconds);
+        void maximum_ms(int milliseconds);
+        void maximum_us(int microseconds);
+            //set maximum deflection pulse width, default = 2400 us
+        void center(float seconds);
+        void center_ms(int milliseconds);
+        void center_us(int microseconds);
+            //set center deflection pulse width, default = 1500 us
+        void minimum(float seconds);
+        void minimum_ms(int milliseconds);
+        void minimum_us(int microseconds);
+            //set minimum deflection pulse width, default = 600 us
+        void period(float seconds);
+        void period_ms(int milliseconds);
+        void period_us(int microseconds);
+            //set the period, default = 20000 us
+        void pulsewidth(float seconds);
+        void pulsewidth_ms(int milliseconds);
+        void pulsewidth_us(int microseconds);
+            //set the pulse width, default = 1500 us
+        float operator =(float assignment);
+            //shorthand for position setting;
+            //ex: "ServoObj = 0.5;" will set servo deflection to +0.5
+        operator float();
+            //shorthand for position reading;
+            // ex: "float check = ServoObj;" will get the current servo deflection
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Aug 31 04:37:07 2010 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+
+int main() {
+    while(1) {
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Aug 31 04:37:07 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da