hej

Dependents:   RoboticHackathon

Files at this revision

API Documentation at this revision

Comitter:
iLyngklip
Date:
Sat Apr 05 07:56:43 2014 +0000
Commit message:
dillerdasker

Changed in this revision

SG90.cpp Show annotated file Show diff for this revision Revisions of this file
SG90.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SG90.cpp	Sat Apr 05 07:56:43 2014 +0000
@@ -0,0 +1,114 @@
+#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;  
+        } 
+    
+   }    
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SG90.h	Sat Apr 05 07:56:43 2014 +0000
@@ -0,0 +1,24 @@
+#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