Rudder program

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
LukeMar
Date:
Wed Nov 28 15:17:04 2018 +0000
Parent:
0:8ed926c84e2f
Commit message:
RUdder version of sailbot RC code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 8ed926c84e2f -r 9e727d54e80c main.cpp
--- a/main.cpp	Wed Nov 28 14:53:46 2018 +0000
+++ b/main.cpp	Wed Nov 28 15:17:04 2018 +0000
@@ -30,65 +30,65 @@
 int j;
 float now;
 //***Stepper Motor universal variables***
-AnalogIn   ain(p18);
-PwmOut  motor( p22 );
-DigitalOut   dir( p21 );
-DigitalOut    I(p23);
-DigitalOut slp(p30); //sleep
-float d_ang = 180.0;
-float pos = 180.0;
-float RC_O;
+AnalogIn   r_ain(p18);
+PwmOut  rudder( p22 );
+DigitalOut   r_dir( p21 );
+DigitalOut    r_I(p23);
+DigitalOut r_slp(p30); //sleep
+float r_ang = 180.0;
+float r_pos = 180.0;
+float RC_1;
 
 //**get position**
-float posi(float in);
+float posr();
 
 
 int main(void)
 {
 
-    I=0;
-    motor.period(.001);
-    motor.pulsewidth(0);
+    r_I=0;
+    rudder.period(.001);
+    rudder.pulsewidth(0);
     // loop
     while(1) {
         //now = rtos::Kernel::get_ms_count();
         //***JUST ADDED stepper code
 
         if (rx.valid) {
-            RC_O = rx.channel[0];
+            RC_1 = rx.channel[0];
             pc.printf(" rc: %f\n",rx.channel[0]);
         } else {
             pc.printf(" invalid\r\n");
-            slp = 0;
+            r_slp = 0;
         }
         //39.19
-        d_ang = (RC_O/6.77)+39.19;
+        r_ang = (RC_1/6.77)+39.19;
 
-        pos = (ain-.108)/.002466;
+        r_pos = (r_ain-.108)/.002466;
         //pc.printf(" %.3f\n",);
-        if((pos > (d_ang-3.0)) && (pos < (d_ang+3.0))) {
-            motor.pulsewidth(0);
-            slp = 0;
+        if((r_pos > (r_ang-3.0)) && (r_pos < (r_ang+3.0))) {
+            rudder.pulsewidth(0);
+            r_slp = 0;
         }
-        if( (pos > (d_ang+3.0)) && pos < 270.0) {
-            slp = 1;
-            while(pos > d_ang) {
-                dir = 1; //left??
-                motor.pulsewidth(.0005);
-                pc.printf("pos: %.3f\n",pos);
-                pos = posi(ain);
+        if( (r_pos > (r_ang+3.0)) && r_pos < 270.0) {
+            r_slp = 1;
+            while(r_pos > r_ang) {
+                r_dir = 1; //left??
+                rudder.pulsewidth(.0005);
+                pc.printf("pos: %.3f\n",r_pos);
+                r_pos = posr();
             }//while pos
-            motor.pulsewidth(0);
+            rudder.pulsewidth(0);
         }//if pos
-        if((pos < (d_ang-3.0)) && pos > 90.0) {
-            slp = 1;
-            while(pos < d_ang) {
-                dir = 0; //right??
-                motor.pulsewidth(.0005);
-                pc.printf("pos: %.3f\n",pos);
-                pos = posi(ain);
+        if((r_pos < (r_ang-3.0)) && r_pos > 90.0) {
+            r_slp = 1;
+            while(r_pos < r_ang) {
+                r_dir = 0; //right??
+                rudder.pulsewidth(.0005);
+                pc.printf("pos: %.3f\n",r_pos);
+                r_pos = posr();
             }//while pos
-            motor.pulsewidth(0);
+            rudder.pulsewidth(0);
         }
         //***END OF JUST ADDED
         ThisThread::sleep_until(121);
@@ -97,13 +97,13 @@
 
 
 
-float posi(float in)
+float posr()
 {
-    float p1;
-    float p2;
-    float p3;
-    p1 = (ain-.108)/.002466;
-    p2 = (ain-.108)/.002466;
-    p3 = (ain-.108)/.002466;
-    return (p1+p2+p3)/3.0;
+    float r1;
+    float r2;
+    float r3;
+    r1 = (r_ain-.108)/.002466;
+    r2 = (r_ain-.108)/.002466;
+    r3 = (r_ain-.108)/.002466;
+    return (r1+r2+r3)/3.0;
 }