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.
FOC/foc.cpp@50:3bda887997f0, 2021-11-04 (annotated)
- Committer:
- wyc136
- Date:
- Thu Nov 04 08:16:21 2021 +0000
- Revision:
- 50:3bda887997f0
- Parent:
- 44:efcde0af8390
WYC ADD PID CONTROL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 37:c0f352d6e8e3 | 1 | |
benkatz | 22:60276ba87ac6 | 2 | #include "foc.h" |
benkatz | 26:2b865c00d7e9 | 3 | using namespace FastMath; |
wyc136 | 50:3bda887997f0 | 4 | float pre_p_des1; |
benkatz | 22:60276ba87ac6 | 5 | |
wyc136 | 50:3bda887997f0 | 6 | float v1_error; |
wyc136 | 50:3bda887997f0 | 7 | float v_mech_error; |
wyc136 | 50:3bda887997f0 | 8 | |
wyc136 | 50:3bda887997f0 | 9 | float pre_v_des1; |
wyc136 | 50:3bda887997f0 | 10 | float pre_dtheta_mech1; |
wyc136 | 50:3bda887997f0 | 11 | float torque_ref; |
benkatz | 22:60276ba87ac6 | 12 | |
benkatz | 22:60276ba87ac6 | 13 | void abc( float theta, float d, float q, float *a, float *b, float *c){ |
benkatz | 25:f5741040c4bb | 14 | /// Inverse DQ0 Transform /// |
benkatz | 22:60276ba87ac6 | 15 | ///Phase current amplitude = lengh of dq vector/// |
benkatz | 22:60276ba87ac6 | 16 | ///i.e. iq = 1, id = 0, peak phase current of 1/// |
benkatz | 37:c0f352d6e8e3 | 17 | float cf = FastCos(theta); |
benkatz | 37:c0f352d6e8e3 | 18 | float sf = FastSin(theta); |
benkatz | 37:c0f352d6e8e3 | 19 | |
benkatz | 37:c0f352d6e8e3 | 20 | *a = cf*d - sf*q; // Faster Inverse DQ0 transform |
benkatz | 37:c0f352d6e8e3 | 21 | *b = (0.86602540378f*sf-.5f*cf)*d - (-0.86602540378f*cf-.5f*sf)*q; |
benkatz | 37:c0f352d6e8e3 | 22 | *c = (-0.86602540378f*sf-.5f*cf)*d - (0.86602540378f*cf-.5f*sf)*q; |
benkatz | 22:60276ba87ac6 | 23 | } |
benkatz | 22:60276ba87ac6 | 24 | |
benkatz | 26:2b865c00d7e9 | 25 | |
benkatz | 22:60276ba87ac6 | 26 | void dq0(float theta, float a, float b, float c, float *d, float *q){ |
benkatz | 25:f5741040c4bb | 27 | /// DQ0 Transform /// |
benkatz | 22:60276ba87ac6 | 28 | ///Phase current amplitude = lengh of dq vector/// |
benkatz | 22:60276ba87ac6 | 29 | ///i.e. iq = 1, id = 0, peak phase current of 1/// |
benkatz | 22:60276ba87ac6 | 30 | |
benkatz | 37:c0f352d6e8e3 | 31 | float cf = FastCos(theta); |
benkatz | 37:c0f352d6e8e3 | 32 | float sf = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 33 | |
benkatz | 37:c0f352d6e8e3 | 34 | *d = 0.6666667f*(cf*a + (0.86602540378f*sf-.5f*cf)*b + (-0.86602540378f*sf-.5f*cf)*c); ///Faster DQ0 Transform |
benkatz | 37:c0f352d6e8e3 | 35 | *q = 0.6666667f*(-sf*a - (-0.86602540378f*cf-.5f*sf)*b - (0.86602540378f*cf-.5f*sf)*c); |
benkatz | 37:c0f352d6e8e3 | 36 | |
benkatz | 22:60276ba87ac6 | 37 | } |
benkatz | 22:60276ba87ac6 | 38 | |
benkatz | 22:60276ba87ac6 | 39 | void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){ |
benkatz | 25:f5741040c4bb | 40 | /// Space Vector Modulation /// |
benkatz | 25:f5741040c4bb | 41 | /// u,v,w amplitude = v_bus for full modulation depth /// |
benkatz | 22:60276ba87ac6 | 42 | |
benkatz | 22:60276ba87ac6 | 43 | float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))/2.0f; |
benkatz | 31:61eb6ae28215 | 44 | *dtc_u = fminf(fmaxf(((u -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 31:61eb6ae28215 | 45 | *dtc_v = fminf(fmaxf(((v -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 31:61eb6ae28215 | 46 | *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 22:60276ba87ac6 | 47 | |
benkatz | 22:60276ba87ac6 | 48 | } |
benkatz | 22:60276ba87ac6 | 49 | |
benkatz | 25:f5741040c4bb | 50 | void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors |
benkatz | 22:60276ba87ac6 | 51 | int adc1_offset = 0; |
benkatz | 22:60276ba87ac6 | 52 | int adc2_offset = 0; |
benkatz | 22:60276ba87ac6 | 53 | int n = 1024; |
benkatz | 25:f5741040c4bb | 54 | for (int i = 0; i<n; i++){ // Average n samples of the ADC |
benkatz | 27:501fee691e0e | 55 | TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles |
benkatz | 27:501fee691e0e | 56 | TIM1->CCR2 = (PWM_ARR>>1)*(1.0f); |
benkatz | 27:501fee691e0e | 57 | TIM1->CCR1 = (PWM_ARR>>1)*(1.0f); |
benkatz | 26:2b865c00d7e9 | 58 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 59 | wait(.001); |
benkatz | 22:60276ba87ac6 | 60 | adc2_offset += ADC2->DR; |
benkatz | 22:60276ba87ac6 | 61 | adc1_offset += ADC1->DR; |
benkatz | 22:60276ba87ac6 | 62 | } |
benkatz | 22:60276ba87ac6 | 63 | *offset_1 = adc1_offset/n; |
benkatz | 22:60276ba87ac6 | 64 | *offset_2 = adc2_offset/n; |
benkatz | 22:60276ba87ac6 | 65 | } |
benkatz | 22:60276ba87ac6 | 66 | |
benkatz | 22:60276ba87ac6 | 67 | void reset_foc(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 68 | TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 69 | TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 70 | TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 71 | controller->i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 72 | controller->i_q_ref = 0; |
benkatz | 28:8c7e29f719c5 | 73 | controller->i_d = 0; |
benkatz | 28:8c7e29f719c5 | 74 | controller->i_q = 0; |
benkatz | 37:c0f352d6e8e3 | 75 | controller->i_q_filt = 0; |
benkatz | 22:60276ba87ac6 | 76 | controller->q_int = 0; |
benkatz | 22:60276ba87ac6 | 77 | controller->d_int = 0; |
benkatz | 28:8c7e29f719c5 | 78 | controller->v_q = 0; |
benkatz | 28:8c7e29f719c5 | 79 | controller->v_d = 0; |
wyc136 | 50:3bda887997f0 | 80 | //**************WYC ADD*******************2021/11/4************// |
wyc136 | 50:3bda887997f0 | 81 | controller->integral_prev = 0; |
wyc136 | 50:3bda887997f0 | 82 | controller->output_prev = 0; |
wyc136 | 50:3bda887997f0 | 83 | controller->error_prev = 0; |
wyc136 | 50:3bda887997f0 | 84 | controller->timestamp_prev = 0; |
wyc136 | 50:3bda887997f0 | 85 | controller->error = 0; |
wyc136 | 50:3bda887997f0 | 86 | //**************WYC END*******************2021/11/4************// |
benkatz | 22:60276ba87ac6 | 87 | } |
benkatz | 22:60276ba87ac6 | 88 | |
benkatz | 22:60276ba87ac6 | 89 | |
benkatz | 37:c0f352d6e8e3 | 90 | void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta){ |
benkatz | 37:c0f352d6e8e3 | 91 | /// Observer Prediction /// |
benkatz | 37:c0f352d6e8e3 | 92 | observer->i_d_est += DT*(observer->i_d_dot); |
benkatz | 37:c0f352d6e8e3 | 93 | observer->i_q_est += DT*(observer->i_q_dot); |
benkatz | 37:c0f352d6e8e3 | 94 | |
benkatz | 25:f5741040c4bb | 95 | /// Commutation Loop /// |
benkatz | 25:f5741040c4bb | 96 | controller->loop_count ++; |
benkatz | 25:f5741040c4bb | 97 | if(PHASE_ORDER){ // Check current sensor ordering |
benkatz | 25:f5741040c4bb | 98 | controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings |
benkatz | 22:60276ba87ac6 | 99 | controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 100 | } |
benkatz | 22:60276ba87ac6 | 101 | else{ |
benkatz | 25:f5741040c4bb | 102 | controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 103 | controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); |
benkatz | 22:60276ba87ac6 | 104 | } |
benkatz | 26:2b865c00d7e9 | 105 | controller->i_a = -controller->i_b - controller->i_c; |
benkatz | 22:60276ba87ac6 | 106 | |
benkatz | 26:2b865c00d7e9 | 107 | float s = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 108 | float c = FastCos(theta); |
benkatz | 44:efcde0af8390 | 109 | dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents |
benkatz | 44:efcde0af8390 | 110 | //controller->i_d = 0.6666667f*(c*controller->i_a + (0.86602540378f*s-.5f*c)*controller->i_b + (-0.86602540378f*s-.5f*c)*controller->i_c); ///Faster DQ0 Transform |
benkatz | 44:efcde0af8390 | 111 | //controller->i_q = 0.6666667f*(-s*controller->i_a - (-0.86602540378f*c-.5f*s)*controller->i_b - (0.86602540378f*c-.5f*s)*controller->i_c); |
benkatz | 37:c0f352d6e8e3 | 112 | |
benkatz | 37:c0f352d6e8e3 | 113 | controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q; |
benkatz | 37:c0f352d6e8e3 | 114 | observer->i_d_m = controller->i_d; |
benkatz | 37:c0f352d6e8e3 | 115 | observer->i_q_m = controller->i_q; |
benkatz | 37:c0f352d6e8e3 | 116 | |
benkatz | 37:c0f352d6e8e3 | 117 | observer->e_d = observer->i_d_m - observer->i_d_est; |
benkatz | 37:c0f352d6e8e3 | 118 | observer->e_q = observer->i_q_m - observer->i_q_est; |
benkatz | 37:c0f352d6e8e3 | 119 | observer->e_d_int += observer->e_d; |
benkatz | 37:c0f352d6e8e3 | 120 | observer->e_q_int += observer->e_q; |
benkatz | 37:c0f352d6e8e3 | 121 | |
benkatz | 37:c0f352d6e8e3 | 122 | observer->i_d_est += K_O*observer->e_d + .001f*observer->e_d_int; |
benkatz | 37:c0f352d6e8e3 | 123 | observer->i_q_est += K_O*observer->e_q + .001f*observer->e_q_int; |
benkatz | 37:c0f352d6e8e3 | 124 | |
benkatz | 37:c0f352d6e8e3 | 125 | |
benkatz | 38:67e4e1453a4b | 126 | float scog12 = FastSin(12.0f*theta); |
benkatz | 38:67e4e1453a4b | 127 | float scog1 = s; |
benkatz | 38:67e4e1453a4b | 128 | float cogging_current = 0.25f*scog1 - 0.3f*scog12; |
benkatz | 25:f5741040c4bb | 129 | |
benkatz | 25:f5741040c4bb | 130 | /// PI Controller /// |
benkatz | 22:60276ba87ac6 | 131 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 38:67e4e1453a4b | 132 | float i_q_error = controller->i_q_ref - controller->i_q + cogging_current; |
benkatz | 37:c0f352d6e8e3 | 133 | |
benkatz | 37:c0f352d6e8e3 | 134 | float v_d_ff = 2.0f*(controller->i_d_ref*R_PHASE - controller->dtheta_elec*L_Q*controller->i_q_ref); //feed-forward voltages |
benkatz | 37:c0f352d6e8e3 | 135 | float v_q_ff = 2.0f*(controller->i_q_ref*R_PHASE + controller->dtheta_elec*(L_D*controller->i_d_ref + WB)); |
benkatz | 37:c0f352d6e8e3 | 136 | |
benkatz | 22:60276ba87ac6 | 137 | controller->d_int += i_d_error; |
benkatz | 22:60276ba87ac6 | 138 | controller->q_int += i_q_error; |
benkatz | 22:60276ba87ac6 | 139 | |
benkatz | 22:60276ba87ac6 | 140 | //v_d_ff = 0; |
benkatz | 22:60276ba87ac6 | 141 | //v_q_ff = 0; |
benkatz | 22:60276ba87ac6 | 142 | |
benkatz | 31:61eb6ae28215 | 143 | limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_SCALE*I_BW*KI_Q)); // Limit integrators to prevent windup |
benkatz | 31:61eb6ae28215 | 144 | controller->v_d = K_SCALE*I_BW*i_d_error + K_SCALE*I_BW*KI_D*controller->d_int;// + v_d_ff; |
benkatz | 31:61eb6ae28215 | 145 | controller->v_q = K_SCALE*I_BW*i_q_error + K_SCALE*I_BW*KI_Q*controller->q_int;// + v_q_ff; |
benkatz | 22:60276ba87ac6 | 146 | |
benkatz | 34:51647c6c500d | 147 | //controller->v_q = 4.0f; |
benkatz | 28:8c7e29f719c5 | 148 | //controller->v_d = 0.0f; |
benkatz | 28:8c7e29f719c5 | 149 | |
benkatz | 22:60276ba87ac6 | 150 | //controller->v_d = v_d_ff; |
benkatz | 22:60276ba87ac6 | 151 | //controller->v_q = v_q_ff; |
benkatz | 22:60276ba87ac6 | 152 | |
benkatz | 44:efcde0af8390 | 153 | limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus |
benkatz | 44:efcde0af8390 | 154 | abc(controller->theta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages |
benkatz | 26:2b865c00d7e9 | 155 | |
benkatz | 44:efcde0af8390 | 156 | //controller->v_u = c*controller->v_d - s*controller->v_q; // Faster Inverse DQ0 transform |
benkatz | 44:efcde0af8390 | 157 | //controller->v_v = (0.86602540378f*s-.5f*c)*controller->v_d - (-0.86602540378f*c-.5f*s)*controller->v_q; |
benkatz | 44:efcde0af8390 | 158 | //controller->v_w = (-0.86602540378f*s-.5f*c)*controller->v_d - (0.86602540378f*c-.5f*s)*controller->v_q; |
benkatz | 22:60276ba87ac6 | 159 | svm(controller->v_bus, controller->v_u, controller->v_v, controller->v_w, &controller->dtc_u, &controller->dtc_v, &controller->dtc_w); //space vector modulation |
benkatz | 22:60276ba87ac6 | 160 | |
benkatz | 37:c0f352d6e8e3 | 161 | observer->i_d_dot = 0.5f*(controller->v_d - 2.0f*(observer->i_d_est*R_PHASE - controller->dtheta_elec*L_Q*observer->i_q_est))/L_D; //feed-forward voltage |
benkatz | 37:c0f352d6e8e3 | 162 | observer->i_q_dot = 0.5f*(controller->v_q - 2.0f*(observer->i_q_est*R_PHASE + controller->dtheta_elec*(L_D*observer->i_d_est + WB)))/L_Q; |
benkatz | 22:60276ba87ac6 | 163 | |
benkatz | 25:f5741040c4bb | 164 | if(PHASE_ORDER){ // Check which phase order to use, |
benkatz | 31:61eb6ae28215 | 165 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles |
benkatz | 31:61eb6ae28215 | 166 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 31:61eb6ae28215 | 167 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 168 | } |
benkatz | 22:60276ba87ac6 | 169 | else{ |
benkatz | 32:ccac5da77844 | 170 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); |
benkatz | 32:ccac5da77844 | 171 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 32:ccac5da77844 | 172 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 173 | } |
benkatz | 25:f5741040c4bb | 174 | |
benkatz | 25:f5741040c4bb | 175 | controller->theta_elec = theta; //For some reason putting this at the front breaks thins |
benkatz | 22:60276ba87ac6 | 176 | |
benkatz | 22:60276ba87ac6 | 177 | |
benkatz | 31:61eb6ae28215 | 178 | if(controller->loop_count >400){ |
benkatz | 22:60276ba87ac6 | 179 | //controller->i_q_ref = -controller->i_q_ref; |
benkatz | 26:2b865c00d7e9 | 180 | controller->loop_count = 0; |
benkatz | 22:60276ba87ac6 | 181 | |
benkatz | 28:8c7e29f719c5 | 182 | //printf("%.2f %.2f %.2f\n\r", controller->i_a, controller->i_b, controller->i_c); |
benkatz | 31:61eb6ae28215 | 183 | //printf("%f\n\r", controller->dtheta_mech*GR); |
benkatz | 22:60276ba87ac6 | 184 | //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c); |
benkatz | 22:60276ba87ac6 | 185 | //pc.printf("%f %f\n\r", controller->i_d, controller->i_q); |
benkatz | 22:60276ba87ac6 | 186 | //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw); |
benkatz | 26:2b865c00d7e9 | 187 | } |
benkatz | 22:60276ba87ac6 | 188 | } |
benkatz | 26:2b865c00d7e9 | 189 | |
benkatz | 26:2b865c00d7e9 | 190 | |
benkatz | 26:2b865c00d7e9 | 191 | void torque_control(ControllerStruct *controller){ |
wyc136 | 50:3bda887997f0 | 192 | v1_error = controller->v_des - pre_p_des1; |
wyc136 | 50:3bda887997f0 | 193 | v_mech_error = controller->dtheta_mech - pre_dtheta_mech1; |
wyc136 | 50:3bda887997f0 | 194 | //if(controller->p_des !=pre_p_des1){ |
wyc136 | 50:3bda887997f0 | 195 | torque_ref = controller->kp*(controller->p_des - controller->theta_mech) + controller->t_ff + controller->kd*(controller->v_des - controller->dtheta_mech); |
wyc136 | 50:3bda887997f0 | 196 | //} |
wyc136 | 50:3bda887997f0 | 197 | //else{ |
wyc136 | 50:3bda887997f0 | 198 | // torque_ref = controller->kp*(controller->v_des - controller->dtheta_mech) +controller->kd*(v1_error - v_mech_error); |
wyc136 | 50:3bda887997f0 | 199 | // } |
benkatz | 26:2b865c00d7e9 | 200 | //float torque_ref = -.1*(controller->p_des - controller->theta_mech); |
benkatz | 26:2b865c00d7e9 | 201 | controller->i_q_ref = torque_ref/KT_OUT; |
benkatz | 37:c0f352d6e8e3 | 202 | controller->i_d_ref = 0.0f; |
wyc136 | 50:3bda887997f0 | 203 | |
wyc136 | 50:3bda887997f0 | 204 | pre_p_des1 = controller->p_des; |
wyc136 | 50:3bda887997f0 | 205 | pre_v_des1 = controller->v_des; |
wyc136 | 50:3bda887997f0 | 206 | pre_dtheta_mech1 = controller->dtheta_mech; |
wyc136 | 50:3bda887997f0 | 207 | } |
benkatz | 26:2b865c00d7e9 | 208 | |
benkatz | 26:2b865c00d7e9 | 209 | |
benkatz | 22:60276ba87ac6 | 210 | /* |
benkatz | 22:60276ba87ac6 | 211 | void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){ |
benkatz | 22:60276ba87ac6 | 212 | |
benkatz | 22:60276ba87ac6 | 213 | } |
wyc136 | 50:3bda887997f0 | 214 | */ |
wyc136 | 50:3bda887997f0 | 215 | /**********************************WYC 2021.10.22*****************************************/ |
wyc136 | 50:3bda887997f0 | 216 | float PID_operator (ControllerStruct *controller){ |
wyc136 | 50:3bda887997f0 | 217 | //printf("piding"); |
wyc136 | 50:3bda887997f0 | 218 | // calculate the time from the last call |
wyc136 | 50:3bda887997f0 | 219 | unsigned long timestamp_now = controller->loop_count; //check the loop_count |
wyc136 | 50:3bda887997f0 | 220 | float Ts = (timestamp_now - controller->timestamp_prev) * DT; |
wyc136 | 50:3bda887997f0 | 221 | //printf("ts:%f\n",Ts); |
wyc136 | 50:3bda887997f0 | 222 | |
wyc136 | 50:3bda887997f0 | 223 | // quick fix for strange cases (micros overflow) |
wyc136 | 50:3bda887997f0 | 224 | if(Ts <= 0 || Ts > 0.5f) Ts = 0.001; |
wyc136 | 50:3bda887997f0 | 225 | |
wyc136 | 50:3bda887997f0 | 226 | // u(s) = (P + I/s + Ds)e(s) |
wyc136 | 50:3bda887997f0 | 227 | // Discrete implementations |
wyc136 | 50:3bda887997f0 | 228 | // proportional part |
wyc136 | 50:3bda887997f0 | 229 | // u_p = P *e(k) |
wyc136 | 50:3bda887997f0 | 230 | float error = controller->error; |
wyc136 | 50:3bda887997f0 | 231 | float proportional = controller->kp * error; |
wyc136 | 50:3bda887997f0 | 232 | // Tustin transform of the integral part |
wyc136 | 50:3bda887997f0 | 233 | // u_ik = u_ik_1 + I*Ts/2*(ek + ek_1) |
wyc136 | 50:3bda887997f0 | 234 | float integral = controller->integral_prev + controller->ki*Ts*0.5f*(error + controller->error_prev); |
wyc136 | 50:3bda887997f0 | 235 | // antiwindup - limit the output |
wyc136 | 50:3bda887997f0 | 236 | limit(&integral, -(controller->LIMIT), controller->LIMIT); |
wyc136 | 50:3bda887997f0 | 237 | // Discrete derivation |
wyc136 | 50:3bda887997f0 | 238 | // u_dk = D(ek - ek_1)/Ts |
wyc136 | 50:3bda887997f0 | 239 | float derivative = controller->kd*(error - controller->error_prev)/Ts; |
wyc136 | 50:3bda887997f0 | 240 | |
wyc136 | 50:3bda887997f0 | 241 | // sum all the components |
wyc136 | 50:3bda887997f0 | 242 | float output = proportional + integral + derivative; |
wyc136 | 50:3bda887997f0 | 243 | // antiwindup - limit the output variable |
wyc136 | 50:3bda887997f0 | 244 | |
wyc136 | 50:3bda887997f0 | 245 | //limit(&output, -(controller->LIMIT), controller->LIMIT); |
wyc136 | 50:3bda887997f0 | 246 | |
wyc136 | 50:3bda887997f0 | 247 | /* |
wyc136 | 50:3bda887997f0 | 248 | // if output ramp defined |
wyc136 | 50:3bda887997f0 | 249 | if(output_ramp > 0){ |
wyc136 | 50:3bda887997f0 | 250 | // limit the acceleration by ramping the output |
wyc136 | 50:3bda887997f0 | 251 | float output_rate = (output - output_prev)/Ts; |
wyc136 | 50:3bda887997f0 | 252 | if (output_rate > output_ramp) |
wyc136 | 50:3bda887997f0 | 253 | output = output_prev + output_ramp*Ts; |
wyc136 | 50:3bda887997f0 | 254 | else if (output_rate < -output_ramp) |
wyc136 | 50:3bda887997f0 | 255 | output = output_prev - output_ramp*Ts; |
wyc136 | 50:3bda887997f0 | 256 | } |
wyc136 | 50:3bda887997f0 | 257 | */ |
wyc136 | 50:3bda887997f0 | 258 | // saving for the next pass |
wyc136 | 50:3bda887997f0 | 259 | controller->integral_prev = integral; |
wyc136 | 50:3bda887997f0 | 260 | controller->output_prev = output; |
wyc136 | 50:3bda887997f0 | 261 | controller->error_prev = error; |
wyc136 | 50:3bda887997f0 | 262 | controller->timestamp_prev = timestamp_now; |
wyc136 | 50:3bda887997f0 | 263 | return output; |
wyc136 | 50:3bda887997f0 | 264 | } |
wyc136 | 50:3bda887997f0 | 265 | /*********WYC ADD 2021.10.22**********/ |
wyc136 | 50:3bda887997f0 | 266 | |
wyc136 | 50:3bda887997f0 | 267 | void velocity_control(ControllerStruct *controller){ |
wyc136 | 50:3bda887997f0 | 268 | |
wyc136 | 50:3bda887997f0 | 269 | controller->ki = controller->kd; |
wyc136 | 50:3bda887997f0 | 270 | controller->kd = 0; |
wyc136 | 50:3bda887997f0 | 271 | controller->error = (controller->v_des - controller->dtheta_mech); |
wyc136 | 50:3bda887997f0 | 272 | |
wyc136 | 50:3bda887997f0 | 273 | float current_sp = PID_operator (controller); |
wyc136 | 50:3bda887997f0 | 274 | controller->i_q_ref = current_sp; //wyc 2021.07.26 compared with the program in mbed,old motor with no "-"in the front of "torque_des" |
wyc136 | 50:3bda887997f0 | 275 | //printf("iq:%f\n",controller->i_q_des); |
wyc136 | 50:3bda887997f0 | 276 | controller->i_d_ref = 0.0f; |
wyc136 | 50:3bda887997f0 | 277 | |
wyc136 | 50:3bda887997f0 | 278 | } |
wyc136 | 50:3bda887997f0 | 279 | /*********WYC ADD 2021.10.22**********/ |