Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Classic_PID iC_MU mbed-rtos mbed
TiltVelocityLoop.cpp@2:dc684c402296, 2015-05-27 (annotated)
- Committer:
- acodd
- Date:
- Wed May 27 07:13:54 2015 +0000
- Revision:
- 2:dc684c402296
- Parent:
- 0:7ce0bc67f60f
- Child:
- 3:f8a5c1cee1fa
First version of a full control loop for the T3 head
;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ms523 | 0:7ce0bc67f60f | 1 | #include "mbed.h" |
| ms523 | 0:7ce0bc67f60f | 2 | #include "iC_MU.h" |
| ms523 | 0:7ce0bc67f60f | 3 | #include "rtos.h" |
| ms523 | 0:7ce0bc67f60f | 4 | #include "Classic_PID.h" |
| ms523 | 0:7ce0bc67f60f | 5 | |
| ms523 | 0:7ce0bc67f60f | 6 | // Define limits for zero crossing |
| ms523 | 0:7ce0bc67f60f | 7 | // These values should allow operation upto 3750 RPM |
| ms523 | 0:7ce0bc67f60f | 8 | #define bits 18 // The number of bits we want to use |
| ms523 | 0:7ce0bc67f60f | 9 | #define OneTurn (1<<bits) // 262144 counts per rev |
| ms523 | 0:7ce0bc67f60f | 10 | #define Lower (1<<(bits-5)) // 8192 counts = 11.25 degrees |
| ms523 | 0:7ce0bc67f60f | 11 | #define Upper OneTurn - Lower // 262144 - 8192 = 253952 |
| ms523 | 0:7ce0bc67f60f | 12 | |
| acodd | 2:dc684c402296 | 13 | extern Serial pc; |
| ms523 | 0:7ce0bc67f60f | 14 | extern iC_MU tilt_ic_mu; |
| acodd | 2:dc684c402296 | 15 | extern iC_MU TiltPos; |
| ms523 | 0:7ce0bc67f60f | 16 | extern PwmOut Tilt_Motor_PWM; |
| ms523 | 0:7ce0bc67f60f | 17 | extern DigitalOut Tilt_Motor_Direction; |
| ms523 | 0:7ce0bc67f60f | 18 | extern Classic_PID TiltVelocityPID; |
| acodd | 2:dc684c402296 | 19 | extern float Demand_Count_Rate; |
| acodd | 2:dc684c402296 | 20 | extern float Tilt_motor_max_count_rate; |
| acodd | 2:dc684c402296 | 21 | extern float Actual_Motor_Speed; |
| acodd | 2:dc684c402296 | 22 | |
| acodd | 2:dc684c402296 | 23 | extern float T_Position; // True Tilt Position (Degrees) |
| acodd | 2:dc684c402296 | 24 | extern float T_sf; |
| acodd | 2:dc684c402296 | 25 | |
| ms523 | 0:7ce0bc67f60f | 26 | |
| ms523 | 0:7ce0bc67f60f | 27 | int LastTiltPosition = 0; |
| acodd | 2:dc684c402296 | 28 | int Position = 0; |
| acodd | 2:dc684c402296 | 29 | int Velocity = 0; |
| acodd | 2:dc684c402296 | 30 | float Duty_Cycle = 0.0; |
| acodd | 2:dc684c402296 | 31 | |
| acodd | 2:dc684c402296 | 32 | |
| acodd | 2:dc684c402296 | 33 | // S_Ramp Fade values |
| acodd | 2:dc684c402296 | 34 | float MaxSpeed = 60.00; // 5 Deg/s, 1250 RPM |
| acodd | 2:dc684c402296 | 35 | float Tilt_motor_max_count_rate = 5461; //encoder counts / ms |
| acodd | 2:dc684c402296 | 36 | |
| acodd | 2:dc684c402296 | 37 | float T_Position = 0; // True Tilt Position (Degrees) |
| acodd | 2:dc684c402296 | 38 | float T_sf = 1456.355; // counts per degree |
| acodd | 2:dc684c402296 | 39 | |
| acodd | 2:dc684c402296 | 40 | // Main servo loop |
| acodd | 2:dc684c402296 | 41 | int DoMove = 0; |
| acodd | 2:dc684c402296 | 42 | const float LOOPs = 0.001; |
| acodd | 2:dc684c402296 | 43 | float D = 10; // Fade distance |
| acodd | 2:dc684c402296 | 44 | float T = 15; // Fade time |
| acodd | 2:dc684c402296 | 45 | float dir = 1; //direction flag |
| acodd | 2:dc684c402296 | 46 | float ta; // The actual value used after sanity checks |
| acodd | 2:dc684c402296 | 47 | float ts; // The actual value used after sanity checks(S ramping) |
| acodd | 2:dc684c402296 | 48 | float tsfade = 0.5; // segment time for S ramped fade |
| acodd | 2:dc684c402296 | 49 | float tscut = 0.2; // segment time for S ramped cut |
| acodd | 2:dc684c402296 | 50 | float j; // jerk value for fade |
| acodd | 2:dc684c402296 | 51 | float aj; // accel value when S ramping |
| acodd | 2:dc684c402296 | 52 | float Vp; // Top speed for the move Deg/s @ load (125:1 Ratio to motor) |
| acodd | 2:dc684c402296 | 53 | float Vs; // Speed step increment |
| acodd | 2:dc684c402296 | 54 | float Da; // Accel distance |
| acodd | 2:dc684c402296 | 55 | float Ds; // Distance convered at steady speed |
| acodd | 2:dc684c402296 | 56 | float s; // Profiler internal demand speed (always positive) |
| acodd | 2:dc684c402296 | 57 | float sout; // Demand as applied by the Vff term |
| acodd | 2:dc684c402296 | 58 | float s_profile; // output demand speed to postion loop + or -) |
| acodd | 2:dc684c402296 | 59 | float P; // Profiler Demand postion |
| acodd | 2:dc684c402296 | 60 | float fadetime; // this will retain the current fade time |
| acodd | 2:dc684c402296 | 61 | float Error; // Current position vs the profiler position |
| acodd | 2:dc684c402296 | 62 | float Vff = 1; // Velocity feedforward term - a value of 1 sends 100% profiler speed demand to motor |
| acodd | 2:dc684c402296 | 63 | float T_Kp = 8; // This is is multiplied by the position error and added to the motor demand |
| acodd | 2:dc684c402296 | 64 | float Prop; // The demand created by the Kp and error calculation |
| acodd | 2:dc684c402296 | 65 | float demand = 0; // The value sento to the motor to make it move |
| acodd | 2:dc684c402296 | 66 | float P_vel; |
| acodd | 2:dc684c402296 | 67 | float Va; // mid speed point |
| acodd | 2:dc684c402296 | 68 | float as; // acceleration value during linear accel stage |
| acodd | 2:dc684c402296 | 69 | float Vj; // Speed at bottom intersection |
| acodd | 2:dc684c402296 | 70 | float Vjp; // Speed at top intersection |
| acodd | 2:dc684c402296 | 71 | float c; // constant for up ramp y=mx+c |
| acodd | 2:dc684c402296 | 72 | float b; // constant for down ramp y = mx+b |
| acodd | 2:dc684c402296 | 73 | float real_time; |
| acodd | 2:dc684c402296 | 74 | float fade_tilt; |
| acodd | 2:dc684c402296 | 75 | float joy_tilt; |
| acodd | 2:dc684c402296 | 76 | float fade_time=10; |
| acodd | 2:dc684c402296 | 77 | extern bool joystick; |
| acodd | 2:dc684c402296 | 78 | extern float Time; |
| acodd | 2:dc684c402296 | 79 | |
| acodd | 2:dc684c402296 | 80 | float T_Joy = 0.0; |
| acodd | 2:dc684c402296 | 81 | |
| ms523 | 0:7ce0bc67f60f | 82 | |
| ms523 | 0:7ce0bc67f60f | 83 | void TiltVelocityLoop(void const *args) |
| ms523 | 0:7ce0bc67f60f | 84 | { |
| acodd | 2:dc684c402296 | 85 | T_Position = 360 - (TiltPos.ReadPOSITION()/T_sf); // the 3D printed unit counts the opposite way to the aluminium unit. |
| acodd | 2:dc684c402296 | 86 | |
| acodd | 2:dc684c402296 | 87 | if (joystick){ |
| acodd | 2:dc684c402296 | 88 | P = T_Position; |
| acodd | 2:dc684c402296 | 89 | } |
| acodd | 2:dc684c402296 | 90 | |
| acodd | 2:dc684c402296 | 91 | Position = tilt_ic_mu.ReadPOSITION() >> (19 - bits);// Read the current position from the iC-MU and bitshift to reduce noise |
| acodd | 2:dc684c402296 | 92 | Velocity = Position - LastTiltPosition; // Calculate change in position (i.e. Velocity) |
| acodd | 2:dc684c402296 | 93 | Actual_Motor_Speed = Velocity; |
| acodd | 2:dc684c402296 | 94 | |
| ms523 | 0:7ce0bc67f60f | 95 | |
| ms523 | 0:7ce0bc67f60f | 96 | // Check to see if we have gone past the index point |
| ms523 | 0:7ce0bc67f60f | 97 | if(Position < Lower & LastTiltPosition > Upper) { // We have gone over the index point in 1 direction |
| ms523 | 0:7ce0bc67f60f | 98 | Velocity += OneTurn; |
| ms523 | 0:7ce0bc67f60f | 99 | } else if(Position > Upper & LastTiltPosition < Lower) {// We have gone over the index point in the other direction |
| ms523 | 0:7ce0bc67f60f | 100 | Velocity -= OneTurn; |
| acodd | 2:dc684c402296 | 101 | } |
| ms523 | 0:7ce0bc67f60f | 102 | LastTiltPosition = Position; // Update new position from next time |
| ms523 | 0:7ce0bc67f60f | 103 | |
| acodd | 2:dc684c402296 | 104 | TiltVelocityPID.setProcessValue(Velocity); |
| acodd | 2:dc684c402296 | 105 | |
| acodd | 2:dc684c402296 | 106 | if (DoMove == 1) { |
| acodd | 2:dc684c402296 | 107 | if ((fadetime < ts) & (s < Vp)) { |
| acodd | 2:dc684c402296 | 108 | //led2 = 0; |
| acodd | 2:dc684c402296 | 109 | s = (j/2)*fadetime*fadetime; //bottom parabola |
| acodd | 2:dc684c402296 | 110 | fadetime = fadetime + LOOPs; // This provides the base time for the fade sequence |
| acodd | 2:dc684c402296 | 111 | } else if ((fadetime >= ts) & (fadetime <(2*ts))) { |
| acodd | 2:dc684c402296 | 112 | s = (as*fadetime)+c; //steady accel stage |
| acodd | 2:dc684c402296 | 113 | fadetime = fadetime + LOOPs; |
| acodd | 2:dc684c402296 | 114 | } else if ((fadetime >= (2*ts)) & (fadetime <(3*ts))) { |
| acodd | 2:dc684c402296 | 115 | s = (-(j/2)*(fadetime-(3*ts))*(fadetime-(3*ts))) + Vp; // Top parabola |
| acodd | 2:dc684c402296 | 116 | fadetime = fadetime + LOOPs; |
| acodd | 2:dc684c402296 | 117 | } else if ((fadetime >= (3*ts)) & (fadetime <(T-(3*ts)))) { |
| acodd | 2:dc684c402296 | 118 | s = Vp; // Steady Speed Stage |
| acodd | 2:dc684c402296 | 119 | fadetime = fadetime + LOOPs; |
| acodd | 2:dc684c402296 | 120 | } else if ((fadetime >= (T-(3*ts))) & (fadetime <(T-(2*ts)))) { |
| acodd | 2:dc684c402296 | 121 | s = (-(j/2)*(fadetime-(T-(3*ts)))*(fadetime-(T-(3*ts)))) + Vp; // Top parabola down |
| acodd | 2:dc684c402296 | 122 | fadetime = fadetime + LOOPs; |
| acodd | 2:dc684c402296 | 123 | } else if ((fadetime >= (T-ts-ts)) & (fadetime < (T-ts))) { |
| acodd | 2:dc684c402296 | 124 | s = -as*(fadetime - T) + c; //steady decel stage |
| acodd | 2:dc684c402296 | 125 | fadetime = fadetime + LOOPs; |
| acodd | 2:dc684c402296 | 126 | } else if ((fadetime >= (T-ts)) & (s < Vp) & (fadetime <= T)) { |
| acodd | 2:dc684c402296 | 127 | //led2 = 1; |
| acodd | 2:dc684c402296 | 128 | s = (j/2)*(T-fadetime)*(T-fadetime); //bottom parabola to end |
| acodd | 2:dc684c402296 | 129 | fadetime = fadetime + LOOPs; |
| acodd | 2:dc684c402296 | 130 | } else if (fadetime >= T) { |
| acodd | 2:dc684c402296 | 131 | s=0; |
| acodd | 2:dc684c402296 | 132 | //led2 = 0; |
| acodd | 2:dc684c402296 | 133 | DoMove = 0; |
| acodd | 2:dc684c402296 | 134 | TiltVelocityPID.setSetPoint(0); |
| acodd | 2:dc684c402296 | 135 | } else { |
| acodd | 2:dc684c402296 | 136 | fadetime = fadetime + LOOPs; // for TBC reason this is needed! |
| acodd | 2:dc684c402296 | 137 | } |
| acodd | 2:dc684c402296 | 138 | if (DoMove==1) { |
| acodd | 2:dc684c402296 | 139 | // compute the new position demand: |
| acodd | 2:dc684c402296 | 140 | s_profile = s * dir; |
| acodd | 2:dc684c402296 | 141 | P = P + (s_profile * LOOPs); |
| acodd | 2:dc684c402296 | 142 | real_time = ((T - fadetime) * 1000); |
| acodd | 2:dc684c402296 | 143 | |
| acodd | 2:dc684c402296 | 144 | sout = s_profile * Vff; //Apply velocity feedforward term |
| acodd | 2:dc684c402296 | 145 | Error = (P - T_Position); // Position Error |
| acodd | 2:dc684c402296 | 146 | Prop = T_Kp * Error; // Calculate proportional gain element |
| acodd | 2:dc684c402296 | 147 | demand = sout + Prop; // Sum the result of Vff and Kp to the demand |
| acodd | 2:dc684c402296 | 148 | //This demand represents degrees/s @ the output shaft. |
| acodd | 2:dc684c402296 | 149 | // Ratio is 125:1. 5461 couns/ms = 60 Deg/s @ output |
| acodd | 2:dc684c402296 | 150 | // scalefactor is approx 91 |
| acodd | 2:dc684c402296 | 151 | P_vel = demand * 72.8; |
| acodd | 2:dc684c402296 | 152 | TiltVelocityPID.setSetPoint(P_vel); |
| acodd | 2:dc684c402296 | 153 | //.printf("\n\r %f, %f, %f, %f, %f",Time, s_profile, P_vel, T_Position, Error); |
| acodd | 2:dc684c402296 | 154 | //me = Time + LOOPs; |
| acodd | 2:dc684c402296 | 155 | } |
| acodd | 2:dc684c402296 | 156 | } else { |
| acodd | 2:dc684c402296 | 157 | |
| acodd | 2:dc684c402296 | 158 | if(!joystick) { |
| acodd | 2:dc684c402296 | 159 | |
| acodd | 2:dc684c402296 | 160 | P = P + (T_Joy * LOOPs); |
| acodd | 2:dc684c402296 | 161 | sout = T_Joy * Vff; //Apply velocity feedforward term |
| acodd | 2:dc684c402296 | 162 | Error = (P - T_Position); // Position Error |
| acodd | 2:dc684c402296 | 163 | Prop = T_Kp * Error; // Calculate proportional gain element |
| acodd | 2:dc684c402296 | 164 | demand = sout + Prop; // Sum the result of Vff and Kp to the demand |
| acodd | 2:dc684c402296 | 165 | //This demand represents degrees/s @ the output shaft. |
| acodd | 2:dc684c402296 | 166 | // Ratio is 125:1. 5461 couns/ms = 60 Deg/s @ output |
| acodd | 2:dc684c402296 | 167 | // scalefactor is approx 91 |
| acodd | 2:dc684c402296 | 168 | P_vel = demand * 72.8; |
| acodd | 2:dc684c402296 | 169 | TiltVelocityPID.setSetPoint(P_vel); |
| acodd | 2:dc684c402296 | 170 | } |
| acodd | 2:dc684c402296 | 171 | |
| acodd | 2:dc684c402296 | 172 | } |
| acodd | 2:dc684c402296 | 173 | |
| acodd | 2:dc684c402296 | 174 | Duty_Cycle = TiltVelocityPID.compute_ff()/Tilt_motor_max_count_rate; |
| ms523 | 0:7ce0bc67f60f | 175 | |
| ms523 | 0:7ce0bc67f60f | 176 | if(Duty_Cycle < 0) { |
| acodd | 2:dc684c402296 | 177 | Tilt_Motor_Direction = 1; |
| ms523 | 0:7ce0bc67f60f | 178 | Tilt_Motor_PWM = 1 - (Duty_Cycle * -1.0); |
| ms523 | 0:7ce0bc67f60f | 179 | } else { |
| acodd | 2:dc684c402296 | 180 | Tilt_Motor_Direction = 0; |
| ms523 | 0:7ce0bc67f60f | 181 | Tilt_Motor_PWM = 1 - Duty_Cycle; |
| ms523 | 0:7ce0bc67f60f | 182 | } |
| acodd | 2:dc684c402296 | 183 | } |
| acodd | 2:dc684c402296 | 184 | |
| acodd | 2:dc684c402296 | 185 | void Profile() // For S ramped movement using Servo for S ramping |
| acodd | 2:dc684c402296 | 186 | { |
| acodd | 2:dc684c402296 | 187 | if ((fade_tilt >=0) & (fade_tilt <= 359)) { |
| acodd | 2:dc684c402296 | 188 | D = fade_tilt - T_Position; // Calculate distance to move |
| acodd | 2:dc684c402296 | 189 | } else { |
| acodd | 2:dc684c402296 | 190 | D = 0; |
| acodd | 2:dc684c402296 | 191 | abort(); // leave this function |
| acodd | 2:dc684c402296 | 192 | // add an error event handler here |
| acodd | 2:dc684c402296 | 193 | } |
| acodd | 2:dc684c402296 | 194 | |
| acodd | 2:dc684c402296 | 195 | if (D <= 0) { |
| acodd | 2:dc684c402296 | 196 | dir = -1; |
| acodd | 2:dc684c402296 | 197 | D = abs(D); |
| acodd | 2:dc684c402296 | 198 | } else { |
| acodd | 2:dc684c402296 | 199 | dir = 1; |
| acodd | 2:dc684c402296 | 200 | } |
| acodd | 2:dc684c402296 | 201 | |
| acodd | 2:dc684c402296 | 202 | if (fade_time <= (6*tsfade + 0.2)) { |
| acodd | 2:dc684c402296 | 203 | ts = tscut; |
| acodd | 2:dc684c402296 | 204 | T = fade_time; |
| acodd | 2:dc684c402296 | 205 | } else { |
| acodd | 2:dc684c402296 | 206 | ts = tsfade; |
| acodd | 2:dc684c402296 | 207 | T = fade_time; |
| acodd | 2:dc684c402296 | 208 | } |
| acodd | 2:dc684c402296 | 209 | if (fade_time <= (6*tscut+0.2)) { |
| acodd | 2:dc684c402296 | 210 | T = 6*tscut + 0.2; //min fade fime |
| acodd | 2:dc684c402296 | 211 | } |
| acodd | 2:dc684c402296 | 212 | |
| acodd | 2:dc684c402296 | 213 | Vp = D / (T-(3*ts)); // Equation 1 |
| acodd | 2:dc684c402296 | 214 | if (Vp > MaxSpeed) { //Check for maximum speed condition |
| acodd | 2:dc684c402296 | 215 | Vp = MaxSpeed; //Do the fade as fast as possible |
| acodd | 2:dc684c402296 | 216 | T = (D + (Vp * (3*ts)))/Vp; |
| acodd | 2:dc684c402296 | 217 | } |
| acodd | 2:dc684c402296 | 218 | |
| acodd | 2:dc684c402296 | 219 | // New version based on S-Ramping Doc - V2 |
| acodd | 2:dc684c402296 | 220 | |
| acodd | 2:dc684c402296 | 221 | j = Vp / (2*ts*ts); |
| acodd | 2:dc684c402296 | 222 | as = j * ts; |
| acodd | 2:dc684c402296 | 223 | c = -(Vp / 4); |
| acodd | 2:dc684c402296 | 224 | s = 0; |
| acodd | 2:dc684c402296 | 225 | fadetime = 0; |
| acodd | 2:dc684c402296 | 226 | // Time = 0; |
| acodd | 2:dc684c402296 | 227 | P = T_Position; |
| acodd | 2:dc684c402296 | 228 | |
| acodd | 2:dc684c402296 | 229 | } |
| acodd | 2:dc684c402296 | 230 | |
| acodd | 2:dc684c402296 | 231 |