PIM.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "PIM.h"
  2. static boolean_T FirstRun_PIM;
  3. #define pimd_L_rls 101
  4. void PIM_Init(void)
  5. {
  6. FirstRun_PIM = true;
  7. }
  8. void PIM(void)
  9. {
  10. static uint8_T ihd_st_workStat_Delay;
  11. uint16_T i;
  12. uint16_T j;
  13. static uint16_T pimn_V_volt[pimd_L_rls];
  14. static int16_T pimn_I_curr[pimd_L_rls];
  15. static real_T pimn_V_ocv;
  16. static real_T pimn_R_ohm;
  17. static real_T pimn_R_polar;
  18. static real_T pimn_F_polar;
  19. static uint16_T pimn_N_ctn;
  20. static real_T pimn_M_P[4][4];
  21. static real_T theta[4];
  22. real_T P[4][4];
  23. real_T A[4];
  24. real_T K[4];
  25. real_T S[4];
  26. real_T temp;
  27. //重置
  28. if (ihd_st_workStat_Delay != 1 && ihd_st_workStat == 1)
  29. {
  30. FirstRun_PIM = true;
  31. }
  32. //限定记忆长度
  33. if (FirstRun_PIM)
  34. {
  35. for (i = 0; i < pimd_L_rls; i++)
  36. {
  37. pimn_V_volt[i] = sfmv_V_cellU[0];
  38. pimn_I_curr[i] = sfmd_I_curr;
  39. }
  40. }
  41. else
  42. {
  43. for (i = 0; i < pimd_L_rls - 1; i++)
  44. {
  45. pimn_V_volt[i] = pimn_V_volt[i+1];
  46. pimn_I_curr[i] = pimn_I_curr[i+1];
  47. }
  48. pimn_V_volt[i] = sfmv_V_cellU[0];
  49. pimn_I_curr[i] = sfmd_I_curr;
  50. }
  51. //初值
  52. if (FirstRun_PIM)
  53. {
  54. pimn_M_P[0][0] = 10; pimn_M_P[0][1] = 0; pimn_M_P[0][2] = 0; pimn_M_P[0][3] = 0;
  55. pimn_M_P[1][0] = 0; pimn_M_P[1][1] = 10; pimn_M_P[1][2] = 0; pimn_M_P[1][3] = 0;
  56. pimn_M_P[2][0] = 0; pimn_M_P[2][1] = 0; pimn_M_P[2][2] = 10; pimn_M_P[2][3] = 0;
  57. pimn_M_P[3][0] = 0; pimn_M_P[3][1] = 0; pimn_M_P[3][2] = 0; pimn_M_P[3][3] = 10;
  58. pimn_V_ocv = 3.6;
  59. pimn_R_ohm = 0.002;
  60. pimn_R_polar = 0.001;
  61. pimn_F_polar = 10;
  62. pimn_N_ctn = 0;
  63. }
  64. //
  65. if(ihd_st_workStat_Delay == 1)
  66. {
  67. pimn_N_ctn = pimn_N_ctn + 1;
  68. //输入
  69. A[0] = (real_T)pimn_V_volt[pimd_L_rls - 2] * 0.001;
  70. A[1] = (real_T)pimn_I_curr[pimd_L_rls - 1] * 0.1;
  71. A[2] =-(real_T)pimn_I_curr[pimd_L_rls - 2] * 0.1;
  72. A[3] = 1;
  73. //参数形变
  74. temp = exp(- (pimn_R_ohm + pimn_R_polar)/pimn_R_ohm/pimn_F_polar);
  75. theta[0] = 1- pimn_R_ohm * (1 - temp)/(pimn_R_ohm + pimn_R_polar);
  76. theta[1] = pimn_R_ohm;
  77. theta[2] = pimn_R_ohm * temp;
  78. theta[3] = (1 -theta[0]) * pimn_V_ocv;
  79. //增益矩阵
  80. temp = (A[0] * pimn_M_P[0][0] +A[1] * pimn_M_P[1][0] + A[2] * pimn_M_P[2][0] + A[3] * pimn_M_P[3][0]) * A[0] +
  81. (A[0] * pimn_M_P[0][1] +A[1] * pimn_M_P[1][1] + A[2] * pimn_M_P[2][1] + A[3] * pimn_M_P[3][1]) * A[1] +
  82. (A[0] * pimn_M_P[0][2] +A[1] * pimn_M_P[1][2] + A[2] * pimn_M_P[2][2] + A[3] * pimn_M_P[3][2]) * A[2] +
  83. (A[0] * pimn_M_P[0][3] +A[1] * pimn_M_P[1][3] + A[2] * pimn_M_P[2][3] + A[3] * pimn_M_P[3][3]) * A[3];
  84. for(i = 0;i < 4;i++)
  85. {
  86. K[i] = (pimn_M_P[i][0] * A[0] + pimn_M_P[i][1] * A[1] + pimn_M_P[i][2] * A[2] + pimn_M_P[i][3] * A[3])/(1 + temp);
  87. }
  88. //参数更新
  89. temp = (real_T)pimn_V_volt[pimd_L_rls - 1] * 0.001 - (theta[0] * A[0] + theta[1] * A[1] + theta[2] * A[2] + theta[3] * A[3]);
  90. for(i = 0;i < 4;i++)
  91. {
  92. theta[i] = theta[i] + K[i] * temp;
  93. }
  94. //协方差更新
  95. for(i = 0;i < 4;i++)
  96. {
  97. for(j = 0;j < 4;j++)
  98. {
  99. P[i][j] = pimn_M_P[i][j] - K[i] * (A[0] * pimn_M_P[0][j] + A[1] * pimn_M_P[1][j] + A[2] * pimn_M_P[2][j] + A[3] * pimn_M_P[3][j]);
  100. }
  101. }
  102. //传递下循环
  103. for(i = 0;i < 4;i++)
  104. {
  105. for(j = 0;j < 4;j++)
  106. {
  107. pimn_M_P[i][j] = P[i][j];
  108. }
  109. }
  110. //=======================================================================
  111. if(pimn_N_ctn > pimd_L_rls)
  112. {
  113. //输入
  114. A[0] = (real_T)pimn_V_volt[0] * 0.001;
  115. A[1] = (real_T)pimn_I_curr[1] * 0.1;
  116. A[2] =-(real_T)pimn_I_curr[0] * 0.1;
  117. A[3] = 1;
  118. //增益矩阵
  119. temp = (A[0] * pimn_M_P[0][0] +A[1] * pimn_M_P[1][0] + A[2] * pimn_M_P[2][0] + A[3] * pimn_M_P[3][0]) * A[0] +
  120. (A[0] * pimn_M_P[0][1] +A[1] * pimn_M_P[1][1] + A[2] * pimn_M_P[2][1] + A[3] * pimn_M_P[3][1]) * A[1] +
  121. (A[0] * pimn_M_P[0][2] +A[1] * pimn_M_P[1][2] + A[2] * pimn_M_P[2][2] + A[3] * pimn_M_P[3][2]) * A[2] +
  122. (A[0] * pimn_M_P[0][3] +A[1] * pimn_M_P[1][3] + A[2] * pimn_M_P[2][3] + A[3] * pimn_M_P[3][3]) * A[3];
  123. for(i = 0;i <4;i++)
  124. {
  125. K[i] = (pimn_M_P[i][0] * A[0] + pimn_M_P[i][1] * A[1] + pimn_M_P[i][2] * A[2] + pimn_M_P[i][3] * A[3])/(1 + temp);
  126. }
  127. //协方差更新
  128. for(i = 0;i < 4;i++)
  129. {
  130. for(j = 0;j < 4;j++)
  131. {
  132. P[i][j] = pimn_M_P[i][j] + K[i] * (A[0] * pimn_M_P[0][j] + A[1] * pimn_M_P[1][j] + A[2] * pimn_M_P[2][j] + A[3] * pimn_M_P[3][j]);
  133. }
  134. }
  135. //传递下一循环
  136. for(i = 0;i < 4;i++)
  137. {
  138. for(j = 0;j < 4;j++)
  139. {
  140. pimn_M_P[i][j] = P[i][j];
  141. }
  142. }
  143. }
  144. //参数求解
  145. pimn_V_ocv = theta[3]/(1 - theta[0]);
  146. pimn_R_ohm = theta[1];
  147. pimn_R_polar = (theta[1] - theta[2])/(1 - theta[0]) - theta[1];
  148. pimn_F_polar = theta[2] * theta[1] > 0 ? (pimn_R_ohm + pimn_R_polar)/pimn_R_ohm/ (-log(theta[2]/theta[1])) : (pimn_R_ohm + pimn_R_polar)/pimn_R_ohm;
  149. pimd_V_ocv = (uint16_T)(pimn_V_ocv * 1000);
  150. pimd_R_ohm = (uint16_T)(pimn_R_ohm * 1000 * 1000);
  151. pimd_R_polar = (uint16_T)(pimn_R_polar * 1000 * 1000);
  152. pimd_F_polar = (uint16_T)(pimn_F_polar * 1000);
  153. }
  154. //
  155. ihd_st_workStat_Delay = ihd_st_workStat;
  156. FirstRun_PIM = false;
  157. }