Test Program

Dependencies:   Classic_PID iC_MU mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
acodd
Date:
Wed May 27 10:07:39 2015 +0000
Parent:
2:dc684c402296
Child:
4:4dafa4113982
Commit message:
Tidy up to variables

Changed in this revision

ServiceKeyboard.cpp Show annotated file Show diff for this revision Revisions of this file
TiltVelocityLoop.cpp 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
--- a/ServiceKeyboard.cpp	Wed May 27 07:13:54 2015 +0000
+++ b/ServiceKeyboard.cpp	Wed May 27 10:07:39 2015 +0000
@@ -53,7 +53,7 @@
                 // Stop both axes
                 //PanVelocityPID.setSetPoint(0);
                 //TiltVelocityPID.setSetPoint(0);
-                pc.printf("\n\r Under PC Control");
+                pc.printf("\n\r Under Full Profiler Control");
                 pc.printf("\n\r");
             }
             break;
@@ -189,3 +189,4 @@
 
 
 
+
--- a/TiltVelocityLoop.cpp	Wed May 27 07:13:54 2015 +0000
+++ b/TiltVelocityLoop.cpp	Wed May 27 10:07:39 2015 +0000
@@ -21,25 +21,28 @@
 extern float Actual_Motor_Speed;
 
 extern float T_Position; // True Tilt Position (Degrees)
-extern float T_sf;
+extern float T_Encoder_sf;
 
 
-int LastTiltPosition = 0;
-int Position = 0;
+int Last_M_Position = 0;
+int M_Position = 0;
 int Velocity = 0;
 float Duty_Cycle = 0.0;
 
-
-// S_Ramp Fade values
+// System Configuration:
 float MaxSpeed = 60.00; // 5 Deg/s, 1250 RPM
 float Tilt_motor_max_count_rate = 5461; //encoder counts / ms
-
 float T_Position = 0; // True Tilt Position (Degrees)
-float T_sf = 1456.355;  // counts per degree
+float T_Encoder_sf = 1456.355;  // counts per degree
+float T_Gear_ratio = 125;  // 125:1
+float M_Encoder = 262144; // counts/rev
+float Motor_sf = ((1 * T_Gear_ratio) * (M_Encoder / 360))/1000 ;  // This is the scale factor from deg/s at payload to motor counts/ms
+bool M_Encoder_front = true; // or 0 depending on the encoder configuration on the motor (facing to motor, or facing away from motor)
 
 // Main servo loop
 int DoMove = 0;
 const float LOOPs = 0.001;
+// S_Ramp Fade values
 float D = 10; // Fade distance
 float T = 15; // Fade time
 float dir = 1; //direction flag
@@ -82,107 +85,99 @@
 
 void TiltVelocityLoop(void const *args)
 {
-    T_Position = 360 - (TiltPos.ReadPOSITION()/T_sf); // the 3D printed unit counts the opposite way to the aluminium unit.
-    
-    if (joystick){
+    T_Position = 360 - (TiltPos.ReadPOSITION()/T_Encoder_sf); // the 3D printed unit counts the opposite way to the aluminium unit.
+
+    if (joystick) {
         P = T_Position;
-        }
-    
-    Position = tilt_ic_mu.ReadPOSITION() >> (19 - bits);// Read the current position from the iC-MU and bitshift to reduce noise
-    Velocity = Position - LastTiltPosition;             // Calculate change in position (i.e. Velocity)
+    }
+
+    M_Position = tilt_ic_mu.ReadPOSITION() >> (19 - bits);// Read the current position from the iC-MU and bitshift to reduce noise
+    Velocity = M_Position - Last_M_Position;             // Calculate change in position (i.e. Velocity)
     Actual_Motor_Speed = Velocity;
 
 
     // Check to see if we have gone past the index point
-    if(Position < Lower & LastTiltPosition > Upper) {       // We have gone over the index point in 1 direction
+    if(M_Position < Lower & Last_M_Position > Upper) {       // We have gone over the index point in 1 direction
         Velocity += OneTurn;
-    } else if(Position > Upper & LastTiltPosition < Lower) {// We have gone over the index point in the other direction
+    } else if(M_Position > Upper & Last_M_Position < Lower) {// We have gone over the index point in the other direction
         Velocity -= OneTurn;
     }
-    LastTiltPosition = Position;                            // Update new position from next time
+    Last_M_Position = M_Position;                            // Update new position from next time
 
     TiltVelocityPID.setProcessValue(Velocity);
 
-    if (DoMove == 1) {
-        if ((fadetime < ts) & (s < Vp)) {
-            //led2 = 0;
-            s = (j/2)*fadetime*fadetime;  //bottom parabola
-            fadetime = fadetime + LOOPs; // This provides the base time for the fade sequence
-        } else if ((fadetime >= ts) & (fadetime <(2*ts))) {
-            s = (as*fadetime)+c; //steady accel stage
-            fadetime = fadetime + LOOPs;
-        } else if ((fadetime >= (2*ts)) & (fadetime <(3*ts))) {
-            s = (-(j/2)*(fadetime-(3*ts))*(fadetime-(3*ts))) + Vp; // Top parabola
-            fadetime = fadetime + LOOPs;
-        } else if ((fadetime >= (3*ts)) & (fadetime <(T-(3*ts)))) {
-            s = Vp;  // Steady Speed Stage
-            fadetime = fadetime + LOOPs;
-        } else if ((fadetime >= (T-(3*ts))) & (fadetime <(T-(2*ts)))) {
-            s = (-(j/2)*(fadetime-(T-(3*ts)))*(fadetime-(T-(3*ts)))) + Vp; // Top parabola down
-            fadetime = fadetime + LOOPs;
-        } else if ((fadetime >= (T-ts-ts)) & (fadetime < (T-ts))) {
-            s = -as*(fadetime - T) + c; //steady decel stage
-            fadetime = fadetime + LOOPs;
-        } else if ((fadetime >= (T-ts)) & (s < Vp) & (fadetime <= T)) {
-            //led2 = 1;
-            s = (j/2)*(T-fadetime)*(T-fadetime);  //bottom parabola to end
-            fadetime = fadetime + LOOPs;
-        } else if (fadetime >= T) {
-            s=0;
-            //led2 = 0;
-            DoMove = 0;
-            TiltVelocityPID.setSetPoint(0);
-        } else {
-            fadetime = fadetime + LOOPs; // for TBC reason this is needed!
-        }
-        if (DoMove==1) {
+    if(!joystick) {
+        if (DoMove == 1) {
+            if ((fadetime < ts) & (s < Vp)) {
+                //led2 = 0;
+                s = (j/2)*fadetime*fadetime;  //bottom parabola
+                fadetime = fadetime + LOOPs; // This provides the base time for the fade sequence
+            } else if ((fadetime >= ts) & (fadetime <(2*ts))) {
+                s = (as*fadetime)+c; //steady accel stage
+                fadetime = fadetime + LOOPs;
+            } else if ((fadetime >= (2*ts)) & (fadetime <(3*ts))) {
+                s = (-(j/2)*(fadetime-(3*ts))*(fadetime-(3*ts))) + Vp; // Top parabola
+                fadetime = fadetime + LOOPs;
+            } else if ((fadetime >= (3*ts)) & (fadetime <(T-(3*ts)))) {
+                s = Vp;  // Steady Speed Stage
+                fadetime = fadetime + LOOPs;
+            } else if ((fadetime >= (T-(3*ts))) & (fadetime <(T-(2*ts)))) {
+                s = (-(j/2)*(fadetime-(T-(3*ts)))*(fadetime-(T-(3*ts)))) + Vp; // Top parabola down
+                fadetime = fadetime + LOOPs;
+            } else if ((fadetime >= (T-ts-ts)) & (fadetime < (T-ts))) {
+                s = -as*(fadetime - T) + c; //steady decel stage
+                fadetime = fadetime + LOOPs;
+            } else if ((fadetime >= (T-ts)) & (s < Vp) & (fadetime <= T)) {
+                //led2 = 1;
+                s = (j/2)*(T-fadetime)*(T-fadetime);  //bottom parabola to end
+                fadetime = fadetime + LOOPs;
+            } else if (fadetime >= T) {
+                s=0;
+                //led2 = 0;
+                DoMove = 0;
+                TiltVelocityPID.setSetPoint(0);
+            } else {
+                fadetime = fadetime + LOOPs; // for TBC reason this is needed!
+            }
             // compute the new position demand:
             s_profile = s * dir;
             P = P + (s_profile * LOOPs);
             real_time = ((T - fadetime) * 1000);
+        } else {  // If not fading then respect joystick profiler instead
 
-            sout = s_profile * Vff;  //Apply velocity feedforward term
-            Error = (P - T_Position);    // Position Error
-            Prop = T_Kp * Error;      // Calculate proportional gain element
-            demand = sout + Prop;   // Sum the result of Vff and Kp to the demand
-            //This demand represents degrees/s @ the output shaft.
-            // Ratio is 125:1.  5461 couns/ms = 60 Deg/s @ output
-            // scalefactor is approx 91
-            P_vel = demand * 72.8;
-            TiltVelocityPID.setSetPoint(P_vel);
-            //.printf("\n\r %f,   %f,   %f,   %f, %f",Time, s_profile, P_vel, T_Position, Error);
-            //me = Time + LOOPs;
-        }
-    } else {
+            P = P + (T_Joy * LOOPs);  // Sum total of Joystick profiler right now.
+
+        }  // End of profilers
 
-        if(!joystick) {
-
-            P = P + (T_Joy * LOOPs);
-            sout = T_Joy * Vff;  //Apply velocity feedforward term
-            Error = (P - T_Position);    // Position Error
-            Prop = T_Kp * Error;      // Calculate proportional gain element
-            demand = sout + Prop;   // Sum the result of Vff and Kp to the demand
-            //This demand represents degrees/s @ the output shaft.
-            // Ratio is 125:1.  5461 couns/ms = 60 Deg/s @ output
-            // scalefactor is approx 91
-            P_vel = demand * 72.8;
-            TiltVelocityPID.setSetPoint(P_vel);
-        }
-
-    }
+        sout = s_profile * Vff;  //Apply velocity feedforward term
+        Error = (P - T_Position);    // Position Error
+        Prop = T_Kp * Error;      // Calculate proportional gain element
+        demand = sout + Prop;   // Sum the result of Vff and Kp to the demand
+        P_vel = demand * Motor_sf; // Convert from load Deg/s into motor encoder counts/ms
+        TiltVelocityPID.setSetPoint(P_vel);
+        //.printf("\n\r %f,   %f,   %f,   %f, %f",Time, s_profile, P_vel, T_Position, Error);
+    }  // End of if !Joystick
 
     Duty_Cycle = TiltVelocityPID.compute_ff()/Tilt_motor_max_count_rate;
 
     if(Duty_Cycle < 0) {
-        Tilt_Motor_Direction = 1;
+        if (M_Encoder_front) {
+            Tilt_Motor_Direction = 1;
+        } else {
+            Tilt_Motor_Direction = 0;
+        }
         Tilt_Motor_PWM = 1 - (Duty_Cycle * -1.0);
     } else {
-        Tilt_Motor_Direction = 0;
+        if (M_Encoder_front) {
+            Tilt_Motor_Direction = 0;
+        } else {
+            Tilt_Motor_Direction = 1;
+        }
         Tilt_Motor_PWM = 1 - Duty_Cycle;
     }
 }
 
-void Profile() // For S ramped movement using Servo for S ramping
+void Profile() // For S ramped movement using Servo for S ramping. Calling here as all it's variables are needed in this arena
 {
     if ((fade_tilt >=0) & (fade_tilt <= 359)) {
         D = fade_tilt - T_Position; // Calculate distance to move
@@ -223,7 +218,7 @@
     c = -(Vp / 4);
     s = 0;
     fadetime = 0;
-   // Time = 0;
+    // Time = 0;
     P = T_Position;
 
 }
--- a/main.cpp	Wed May 27 07:13:54 2015 +0000
+++ b/main.cpp	Wed May 27 10:07:39 2015 +0000
@@ -65,11 +65,11 @@
 bool AutofocusFlag = true;
 bool ManualfocusFlag = false;
 bool Mode = AUTO;
-bool joystick = true;
+bool joystick = false;
 bool scoping = false;
 
 extern int LastPanPosition;
-extern int LastTiltPosition;
+extern int Last_M_Position;
 float Pan_JoyStickDem = 0.8;
 float Tilt_JoyStickDem = 0.8;
 float Demand_Count_Rate = 0.0;
@@ -79,7 +79,7 @@
 extern float Tilt_motor_max_count_rate; //encoder counts / ms
 
 extern float T_Position; // True Tilt Position (Degrees)
-extern float T_sf;  // counts per degree
+extern float T_Encoder_sf;  // counts per degree
 extern int DoMove;
 extern float s_profile;
 extern float P_vel;
@@ -191,7 +191,7 @@
     // Initalise Tilt PID Loop
     TiltVelocityPID.setProcessLimits(Tilt_motor_max_count_rate, (Tilt_motor_max_count_rate*-1));
     TiltVelocityPID.setSetPoint(0);
-    LastTiltPosition = tilt_ic_mu.ReadPOSITION() >> 1;
+    Last_M_Position = tilt_ic_mu.ReadPOSITION() >> 1;
 
     // Initalise Anti-Lock RtosTimer thread
     RtosTimer Anti_Lock_timer(Anti_Lock, osTimerPeriodic);
@@ -199,7 +199,7 @@
 
     pc.baud(921600);
     
-    T_Position = 360 - (TiltPos.ReadPOSITION()/T_sf);    // Prime the system on startup, this is not used once running.
+    T_Position = 360 - (TiltPos.ReadPOSITION()/T_Encoder_sf);    // Prime the system on startup, this is not used once running.
     P = T_Position;                                      // Priming
     pc.printf("\n\r Startup: T_Position = %f, P = %f, \n\r", T_Position, P);
     
@@ -208,7 +208,7 @@
         if(pc.readable()) {
             ServiceKeyboard();
         }
-        //T_Position = TiltPos.ReadPOSITION()/T_sf;
+        //T_Position = TiltPos.ReadPOSITION()/T_Encoder_sf;
         Demand_Count_Rate = TiltVelocityPID.getSetPoint();
 
         if(1==0) {
@@ -219,7 +219,7 @@
          if(scoping) {
             P_Error = T_Position - P;
             pc.printf("\n\r %f, %f, %f, %f, %f", Time, Tilt_JoyStickDem, P_Error, T_Position, P);
-            Time = Time + 0.05;
+            Time = Time + 0.025;
         }
 
        // if(DoMove ==1) {
@@ -228,7 +228,7 @@
 
 
 
-        Thread::wait(50);
+        Thread::wait(25);
         // Update the Zoom and Focus Demands
         //UpdateCamera(Zoom_Joystick.read(),Focus_Pot.read());