BMS_Clear_Error_States.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * BMS_Clear_Error_States.c
  3. *
  4. * Created on: Nov 8, 2016
  5. * Author: le8041
  6. */
  7. #include "BMS_Master.h"
  8. uint32_t BMS_Clear_Error_Buffer(MASTER_CAN0_STRUCT_t* s,uint8_t ErrorClass) {
  9. uint8_t i;
  10. if(ErrorClass == BMS_ERROR_CLASS_1) {
  11. // go though Error buffer 1 and check if Errors still apply
  12. // if no clear Error Flag
  13. for(i=0;i<s->ErrorBuffer.ES1_ErrorCounter;i++) {
  14. if(s->ErrorBuffer.ES1_Error[i].Word & 0x00000001) {
  15. // Error still applies
  16. if(s->ErrorBuffer.ES1_Error[i].Master.SubsystemNr==BMS_ERROR_MASTER_ID) {
  17. // Error is master Error
  18. BMS_Clear_Error_State_Master(s,&(s->ErrorBuffer.ES1_Error[i]),i,ErrorClass);
  19. }
  20. else if(s->ErrorBuffer.ES1_Error[i].Master.SubsystemNr==BMS_ERROR_UI_ID) {
  21. // Error is UI Error
  22. }
  23. else{
  24. // Error is Slave Error
  25. BMS_Clear_Error_State_Slave(s,&(s->ErrorBuffer.ES1_Error[i])) ;
  26. }
  27. }
  28. }
  29. }
  30. else if(ErrorClass == BMS_ERROR_CLASS_2) {
  31. // go though Error buffer 2 and check if Errors still apply
  32. // if no clear Error Flag
  33. for(i=0;i<s->ErrorBuffer.ES2_ErrorCounter;i++) {
  34. if(s->ErrorBuffer.ES2_Error[i].Word & 0x00000001) {
  35. // Error still applies
  36. if(s->ErrorBuffer.ES2_Error[i].Master.SubsystemNr==BMS_ERROR_MASTER_ID) {
  37. // Error is master Error
  38. BMS_Clear_Error_State_Master(s,&(s->ErrorBuffer.ES2_Error[i]),i,ErrorClass);
  39. }
  40. else if(s->ErrorBuffer.ES2_Error[i].Master.SubsystemNr==BMS_ERROR_UI_ID) {
  41. // Error is UI Error
  42. }
  43. else{
  44. // Error is Slave Error
  45. BMS_Clear_Error_State_Slave(s,&(s->ErrorBuffer.ES2_Error[i])) ;
  46. }
  47. }
  48. }
  49. }
  50. else {
  51. // go though Error buffer 3 and check if Errors still apply
  52. // if no clear Error Flag
  53. for(i=0;i<s->ErrorBuffer.ES3_ErrorCounter;i++) {
  54. if(s->ErrorBuffer.ES3_Error[i].Word & 0x00000001) {
  55. // Error still applies
  56. if(s->ErrorBuffer.ES3_Error[i].Master.SubsystemNr==BMS_ERROR_MASTER_ID) {
  57. // Error is master Error
  58. BMS_Clear_Error_State_Master(s,&(s->ErrorBuffer.ES3_Error[i]),i,ErrorClass);
  59. }
  60. else if(s->ErrorBuffer.ES3_Error[i].Master.SubsystemNr==BMS_ERROR_UI_ID) {
  61. // Error is UI Error
  62. }
  63. else{
  64. // Error is Slave Error
  65. }
  66. }
  67. }
  68. }
  69. return TRUE;
  70. }
  71. /*
  72. * @brief check if error still applies, if no return false if yes return true and invalidate error
  73. */
  74. uint32_t BMS_Clear_Error_State_Master(MASTER_CAN0_STRUCT_t* s,BMS_ERROR_STATE_t* error_state,uint8_t Error_nr,uint8_t Error_class) {
  75. uint8_t SlaveSelect;
  76. if(error_state->Master.ErrorCode==BMS_ERROR_STACK_MASTER_UMIN_1) {
  77. // check if Umin1 error still applies
  78. if(s->inverterState.startupPwr >20 ) {
  79. // PV Supplies more than 20W Start recharging battery
  80. // give inverter time to start charging battery
  81. if(Error_class ==BMS_ERROR_CLASS_1) {
  82. // use Error class 1
  83. if(s->ErrorBuffer.ES1_Error_Recovery[Error_nr].recoveryPending== FALSE) {
  84. // not yet trying to recover from Error
  85. s->ErrorBuffer.ES1_Error_Recovery[Error_nr].recoveryPending= TRUE;
  86. s->ErrorBuffer.ES1_Error_Recovery[Error_nr].recoveryTimestamp =Global_1msCounter ;
  87. return TRUE;
  88. }
  89. else {
  90. // System already trying to recover from error
  91. if(s->inverterState.inverterIsCharging == TRUE) {
  92. // system is charging => Error can be completely revoked
  93. error_state->Master.active=0;
  94. s->ErrorBuffer.ES1_Error_Recovery[Error_nr].recoveryPending= FALSE;
  95. return TRUE;
  96. }
  97. else if(s->ErrorBuffer.ES1_Error_Recovery[Error_nr].recoveryPending== TRUE) {
  98. // check if timeout occured
  99. if(s->ErrorBuffer.ES1_Error_Recovery[Error_nr].recoveryTimestamp +BMS_ERROR_RESTORE_TIMEOUT < Global_1msCounter) {
  100. // timeout occured
  101. error_state->Master.active=1;
  102. s->ErrorBuffer.ES1_Error_Recovery[Error_nr].recoveryPending= FALSE;
  103. return FALSE;
  104. }
  105. else {
  106. // no timeout;
  107. return TRUE;
  108. }
  109. }
  110. }
  111. }
  112. else if(Error_class ==BMS_ERROR_CLASS_2) {
  113. // use Error class 2
  114. // up date if umin1 should become error class 2
  115. return FALSE;
  116. }
  117. else {
  118. // Error class 3 has no recovery function
  119. error_state->Master.active=1;
  120. return FALSE;
  121. }
  122. return FALSE;
  123. }
  124. else {
  125. error_state->Master.active=1;
  126. return FALSE;
  127. }
  128. }
  129. else if(error_state->Master.ErrorCode==BMS_ERROR_STACK_MASTER_UMIN_2 ) {
  130. // check if Umin2 error still applies
  131. if(s->inverterState.startupPwr >20 ) {
  132. // PV Supplies more than 20W Start recharging battery
  133. // give inverter time to start charging battery
  134. if(Error_class ==BMS_ERROR_CLASS_2) {
  135. // use Error class 2
  136. if(s->ErrorBuffer.ES2_Error_Recovery[Error_nr].recoveryPending== FALSE) {
  137. // not yet trying to recover from Error
  138. s->ErrorBuffer.ES2_Error_Recovery[Error_nr].recoveryPending= TRUE;
  139. s->ErrorBuffer.ES2_Error_Recovery[Error_nr].recoveryTimestamp =Global_1msCounter ;
  140. return TRUE;
  141. }
  142. else {
  143. // System already trying to recover from error
  144. if(s->inverterState.inverterIsCharging == TRUE) {
  145. // system is charging => Error can be completely revoked
  146. error_state->Master.active=0;
  147. s->ErrorBuffer.ES2_Error_Recovery[Error_nr].recoveryPending= FALSE;
  148. return TRUE;
  149. }
  150. else if(s->ErrorBuffer.ES2_Error_Recovery[Error_nr].recoveryPending== TRUE) {
  151. // check if timeout occured
  152. if(s->ErrorBuffer.ES2_Error_Recovery[Error_nr].recoveryTimestamp +BMS_ERROR_RESTORE_TIMEOUT < Global_1msCounter) {
  153. // timeout occured
  154. error_state->Master.active=1;
  155. s->ErrorBuffer.ES2_Error_Recovery[Error_nr].recoveryPending= FALSE;
  156. return FALSE;
  157. }
  158. else {
  159. // no timeout;
  160. return TRUE;
  161. }
  162. }
  163. }
  164. }
  165. else {
  166. // Error class 3 has no recovery function
  167. error_state->Master.active=1;
  168. return FALSE;
  169. }
  170. return FALSE;
  171. }
  172. }
  173. else if(error_state->Master.ErrorCode==BMS_ERROR_STACK_MASTER_UMIN_3) {
  174. // check if Umin1 error still applies
  175. // error state can only removed from external technician
  176. return FALSE;
  177. }
  178. else if(error_state->Master.ErrorCode==BMS_ERROR_STACK_MASTER_UMAX_2) {
  179. // check if Umax2 error still applies
  180. if(s->maxCellVoltage <= BMS_SOC_UMAX_2) {
  181. // error no longer applies
  182. error_state->Master.active=0;
  183. return TRUE;
  184. }
  185. else {
  186. return FALSE;
  187. }
  188. }
  189. else if(error_state->Master.ErrorCode==BMS_ERROR_STACK_MASTER_I_MAX_DISCHARGE_OP) {
  190. // not clear how to revoke error because relais are open and therefore current is 0A
  191. error_state->Master.active=0;
  192. return TRUE;
  193. }
  194. else if(error_state->Master.ErrorCode==BMS_ERROR_STACK_MASTER_I_MAX_CHARGE_OP) {
  195. // not clear how to revoke error because relais are open and therefore current is 0A
  196. error_state->Master.active=0;
  197. return TRUE;
  198. }
  199. else if( error_state->Master.ErrorCode==BMS_ERROR_STACK_MASTER_EXTERN_CAN_ERROR) {
  200. // simply revoke Error when no more Timeouts are detected
  201. if(s->CAN1_fsmStruct.timeoutCyclesCnt==0) {
  202. error_state->Master.active = 0;
  203. return TRUE;
  204. }
  205. return FALSE;
  206. }
  207. else{
  208. // invalid state
  209. return FALSE;
  210. }
  211. }
  212. /*
  213. * @brief check if error still applies, if no return false if yes return true and invalidate error
  214. */
  215. uint32_t BMS_Clear_Error_State_Slave(MASTER_CAN0_STRUCT_t* s,BMS_ERROR_STATE_t* error_state) {
  216. uint8_t SlaveSelect;
  217. if(error_state->Slave.ErrorType == BMS_ERROR_STACK_SLAVE_CAN_ERROR) {
  218. SlaveSelect=error_state->Slave.SubsystemNr;
  219. if(s->Slave[SlaveSelect].SlaveCanCommuniationError.FailedComCnt==0) {
  220. // connection is good again
  221. error_state->Slave.active=0;
  222. return TRUE;
  223. }
  224. else {
  225. return FALSE;
  226. }
  227. }
  228. else{
  229. // invalid state
  230. return FALSE;
  231. }
  232. }
  233. uint32_t BMS_Clear_Error_init_recovery_struct(MASTER_CAN0_STRUCT_t* s) {
  234. uint8_t i;
  235. for(i=0;i<BMS_ERROR_ERROR_STACK_SIZE;i++) {
  236. s->ErrorBuffer.ES1_Error_Recovery[i].recoveryPending=0;
  237. s->ErrorBuffer.ES1_Error_Recovery[1].recoveryTimestamp=0;
  238. s->ErrorBuffer.ES2_Error_Recovery[i].recoveryPending=0;
  239. s->ErrorBuffer.ES2_Error_Recovery[1].recoveryTimestamp=0;
  240. }
  241. }