servom

Dependents:   servomotore progetto8 progetto9 progetto10 ... more

Files at this revision

API Documentation at this revision

Comitter:
faraonemena
Date:
Thu Mar 28 15:52:51 2019 +0000
Parent:
0:a62b163b1dbb
Commit message:
servom

Changed in this revision

SG90.cpp Show diff for this revision Revisions of this file
SG90.h 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
diff -r a62b163b1dbb -r de53826667ce SG90.cpp
--- a/SG90.cpp	Sat Apr 05 07:56:43 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-#include "mbed.h"
-#include "SG90.h"
-
-    
-//Initialize the position variables to neutral
-
-servo::servo() : pwm1(PTA5), pwm2(PTA12), pwm3(PTA4), pwm4(PTA5) 
-    {
-      
-      init();  
-    }
-
-
-
-void servo::init() //Initialize all servo pwm's to a periode of 20ms
-    {
-        s1=s2=s3=s4=1400; //Initialize the position variables to neutral
-        pwm1.period_ms(20);
-        pwm1.pulsewidth_us(s1);
-        pwm2.period_ms(20);
-        pwm2.pulsewidth_us(s2);
-        pwm3.period_ms(20);
-        pwm3.pulsewidth_us(s3);
-        pwm4.period_ms(20);
-        pwm4.pulsewidth_us(s4);
-    }
-
-void servo::right(int a)    //Lower the pulsewidth to make the servo turn clockwise
-    {
-        switch (a)
-        {
-            case 1:
-                if (s1 >= 540) s1 = (s1 - 20); 
-                pwm1.pulsewidth_us(s1); 
-                break;
-                
-            case 2:
-                if (s2 >= 540)s2 =s2 -20;
-                pwm2.pulsewidth_us(s2);
-                break;
-                
-            case 3:
-                if (s3 >= 540)s3 = s3 -20;
-                pwm3.pulsewidth_us(s3);
-                break;
-                
-            case 4:
-                if (s4 >= 540)s4 = s4 -20;
-                pwm4.pulsewidth_us(s4);
-                break;
-                
-            default :
-                break;  
-        } 
-    }
-           
-void servo::left(int a)          //Raise the pulsewidth to make the servo turn counter clockwise
-    {
-         switch (a)
-        {
-            case 1:
-                if (s1 <=2300) s1 = s1 + 20; 
-                pwm1.pulsewidth_us(s1); 
-                break;
-                
-            case 2:
-                if (s2 <=2300)s2 =s2 +20;
-                pwm2.pulsewidth_us(s2);
-                break;
-                
-            case 3:
-                if (s2 <=2300)s3 = s3 +20;
-                pwm3.pulsewidth_us(s3);
-                break;
-                
-            case 4:
-                if (s2 <=2300)s4 = s4 +20;
-                pwm4.pulsewidth_us(s4);
-                break;
-                
-            default :
-                break;  
-        } 
-    }
-void servo::position(int a, int p) //Position control of the servor, a is the servo and p is the position in degrees
-    {
-          int pw;
-          pw = 500 + (p*10); 
-          switch (a)
-        {
-            case 1:
-                pwm1.pulsewidth_us(pw); 
-                break;
-                
-            case 2:
-                
-                pwm2.pulsewidth_us(pw);
-                break;
-                
-            case 3:
-                
-                pwm3.pulsewidth_us(pw);
-                break;
-                
-            case 4:
-                
-                pwm4.pulsewidth_us(pw);
-                break;
-                
-            default :
-                break;  
-        } 
-    
-   }    
diff -r a62b163b1dbb -r de53826667ce SG90.h
--- a/SG90.h	Sat Apr 05 07:56:43 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-#ifndef SG90_H
-#define SG90_H
-#include "mbed.h"
-#include "PwmOut.h"
-
-class servo {
-    
-public:
-
-PwmOut pwm1, pwm2, pwm3, pwm4;
-servo();
-
-
-void init();
-void right(int);
-void left(int);
-void position(int, int);
-
-int s1, s2, s3, s4;
-
-
-
-};
-#endif
\ No newline at end of file
diff -r a62b163b1dbb -r de53826667ce Servo.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.cpp	Thu Mar 28 15:52:51 2019 +0000
@@ -0,0 +1,42 @@
+/*
+
+Copyright (c) 2012-2014 RedBearLab
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+and associated documentation files (the "Software"), to deal in the Software without restriction, 
+including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
+subject to the following conditions:
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
+PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
+FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+*/
+
+#include "../Servo/Servo.h"
+
+Servo::Servo(PinName pin) : _servo(pin)
+{
+    _servo.period_ms(20);
+}
+
+Servo::~Servo(void)
+{
+    
+}
+
+void Servo::write(unsigned char degree)
+{
+    convert(degree);
+    _servo.pulsewidth_us(pulse);
+}
+
+void Servo::convert(unsigned char degree)
+{
+    // 0~180 degree correspond to 500~2500
+    pulse = degree * 11 + 500;
+}
\ No newline at end of file
diff -r a62b163b1dbb -r de53826667ce Servo.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.h	Thu Mar 28 15:52:51 2019 +0000
@@ -0,0 +1,40 @@
+/*
+
+Copyright (c) 2012-2014 RedBearLab
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+and associated documentation files (the "Software"), to deal in the Software without restriction, 
+including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
+subject to the following conditions:
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
+PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
+FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+*/
+
+#ifndef _SERVO_H
+#define _SERVO_H
+
+#include "mbed.h"
+
+class Servo
+{
+public:
+    Servo(PinName pin);
+    ~Servo(void);
+    
+    void write(unsigned char degree);
+
+private:
+    void convert(unsigned char degree);
+    
+    PwmOut _servo;
+    unsigned int pulse;
+};
+
+#endif
\ No newline at end of file