evc_comm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "evc_comm.h"
  2. #include "evc.h"
  3. #include "evc_sm.h"
  4. #include "plt.h"
  5. void evc_comm_dac(int idx)
  6. {
  7. struct evc_t *dev = &evc[idx];
  8. struct comm_t *comm = &dev->comm;
  9. int chanidx = comm->chanidx;
  10. int addr = comm->adr;
  11. int ret = 0;
  12. if (comm_get_state(comm) != COMMST_NORMAL)
  13. {
  14. return;
  15. }
  16. evc_dev_control_strategy_pack_temp(idx); // 控制帧每一轮都要发送
  17. // static int count = 0;
  18. // if (chan_socketcan_rxrb_num_items(chanidx) <= 0)
  19. // {
  20. // count++;
  21. // if (count >= 600) // 10 分钟超时
  22. // {
  23. // log_dbg("evc_comm_dac, idx:%d, there is no data for %d loop, can comm err", idx, count);
  24. // count = 0;
  25. // comm_set_state(comm, COMMST_ERR);
  26. // return;
  27. // }
  28. // if(count % 60 == 0)
  29. // {
  30. // log_dbg("evc_comm_dac, idx:%d, there is no data for %d loop", idx, count);
  31. // }
  32. // return;
  33. // }
  34. // else
  35. // {
  36. // count = 0; // reset, 直到真的满10次
  37. // }
  38. comm_start_cal_dac_timing(comm);
  39. chan_socketcan_ringbuffer_element_t e;
  40. static chan_socketcan_ringbuffer_element_t e_cache[128];
  41. int cache_count = 0;
  42. // log_dbg("evc_comm_dac, idx:%d", chanidx);
  43. chan_lock(chanidx);
  44. while (chan_socketcan_rxrb_num_items(chanidx) > 0)
  45. {
  46. chan_socketcan_rxrb_dequeue(chanidx, &e);
  47. // log_dbg("evc_comm_dac, idx:%d, canid:0x%x", chanidx, (e.frame.can_id & (~CAN_EFF_FLAG)));
  48. if ((e.frame.can_id & (~CAN_EFF_FLAG)) == 0x18008040) // 帧二,读取所有值
  49. {
  50. // log_dbg("evc_comm_dac, frame2");
  51. dev->runMode = e.frame.data[0] & 0x03;
  52. if (dev->runMode == 0x03)
  53. {
  54. strcpy(dev->runModeStr, "Self Circulation");
  55. }
  56. else if (dev->runMode == 0x02)
  57. {
  58. strcpy(dev->runModeStr, "Heating");
  59. }
  60. else if (dev->runMode == 0x01)
  61. {
  62. strcpy(dev->runModeStr, "Cooling");
  63. }
  64. else if (dev->runMode == 0x00)
  65. {
  66. strcpy(dev->runModeStr, "Stop");
  67. }
  68. dev->out_water_temp = e.frame.data[1] - 40;
  69. dev->in_water_temp = e.frame.data[2] - 40;
  70. dev->ev_temp = e.frame.data[3] - 40;
  71. dev->in_water_press = e.frame.data[4];
  72. dev->out_water_press = e.frame.data[5];
  73. dev->err_code = e.frame.data[7] & 0x3f;
  74. dev->err_lvl = (e.frame.data[7] & 0xC0) >> 6;
  75. if (dev->err_code != 0)
  76. {
  77. // log_dbg("evc has error, errCode : %d, errLevel :%d", dev->err_code, dev->err_lvl);
  78. if (dev->err_lvl == 1 || dev->err_lvl == 2) // 就是报1/2都要检查设备了,有些不用强行动作的不用动作
  79. {
  80. sm_set_state(&dev->sm, SMST_ERR, EVCERR_READY_COMMERR);
  81. }
  82. }
  83. }
  84. else if ((e.frame.can_id & (~CAN_EFF_FLAG)) == 0x18018040) // 帧三,读取所有状态
  85. {
  86. // log_dbg("evc_comm_dac, frame3");
  87. dev->comp_state = (e.frame.data[0] & 0x01) != 0 ? 1 : 0;
  88. dev->comp_heat_state = (e.frame.data[0] & 0x02) != 0 ? 1 : 0;
  89. dev->elec_heat_state = (e.frame.data[0] & 0x04) != 0 ? 1 : 0;
  90. dev->pump_state = (e.frame.data[0] & 0x08) != 0 ? 1 : 0;
  91. dev->fan1_state = (e.frame.data[0] & 0x10) != 0 ? 1 : 0;
  92. dev->fan2_state = (e.frame.data[0] & 0x20) != 0 ? 1 : 0;
  93. dev->fan3_state = (e.frame.data[0] & 0x40) != 0 ? 1 : 0;
  94. dev->comp_rpm = e.frame.data[1] * 100;
  95. dev->pump_rpm = e.frame.data[2] * 100;
  96. }
  97. else if ((e.frame.can_id & (~CAN_EFF_FLAG)) == 0x18068040) // 帧四,读取所有状态
  98. {
  99. // log_dbg("evc_comm_dac, frame4");
  100. dev->in_out_water_press_sensor_err = (e.frame.data[0] & 0x01) != 0 ? 1 : 0;
  101. dev->ov_err = (e.frame.data[0] & 0x02) != 0 ? 1 : 0;
  102. dev->uv_err = (e.frame.data[0] & 0x04) != 0 ? 1 : 0;
  103. dev->lack_water_err = (e.frame.data[0] & 0x08) != 0 ? 1 : 0;
  104. dev->pump_err = (e.frame.data[0] & 0x10) != 0 ? 1 : 0;
  105. dev->pump_err_lock = (e.frame.data[0] & 0x20) != 0 ? 1 : 0;
  106. }
  107. else if ((e.frame.can_id & (~CAN_EFF_FLAG)) == 0x18078040) // 帧五,读取所有状态
  108. {
  109. // log_dbg("evc_comm_dac, frame5");
  110. dev->out_water_press_sensor_err = (e.frame.data[0] & 0x01) != 0 ? 1 : 0;
  111. dev->in_water_press_sensor_err = (e.frame.data[0] & 0x02) != 0 ? 1 : 0;
  112. dev->up_sensor_err = (e.frame.data[0] & 0x04) != 0 ? 1 : 0;
  113. dev->out_water_temp_sensor_err = (e.frame.data[0] & 0x08) != 0 ? 1 : 0;
  114. dev->sys_low_pressure_err = (e.frame.data[0] & 0x10) != 0 ? 1 : 0;
  115. dev->sys_high_pressure_switch_err = (e.frame.data[0] & 0x20) != 0 ? 1 : 0;
  116. dev->out_air_over_temp_err = (e.frame.data[0] & 0x40) != 0 ? 1 : 0;
  117. dev->can_comm_err = (e.frame.data[0] & 0x80) != 0 ? 1 : 0;
  118. dev->in_air_temp_sensor_err = (e.frame.data[1] & 0x01) != 0 ? 1 : 0;
  119. dev->cooling_sys_err = (e.frame.data[1] & 0x02) != 0 ? 1 : 0;
  120. dev->in_air_over_temp_err = (e.frame.data[1] & 0x04) != 0 ? 1 : 0;
  121. dev->comp_VFD_ov_err = (e.frame.data[1] & 0x08) != 0 ? 1 : 0;
  122. dev->comp_VFD_uv_err = (e.frame.data[1] & 0x10) != 0 ? 1 : 0;
  123. dev->comp_VFD_oc_err = (e.frame.data[1] & 0x20) != 0 ? 1 : 0;
  124. dev->comp_VFD_ot_err = (e.frame.data[1] & 0x40) != 0 ? 1 : 0;
  125. dev->comp_VFD_comm_err = (e.frame.data[1] & 0x80) != 0 ? 1 : 0;
  126. dev->comp_VFD_miss_phrase_err = (e.frame.data[2] & 0x01) != 0 ? 1 : 0;
  127. dev->comp_VFD_others_err = (e.frame.data[2] & 0x02) != 0 ? 1 : 0;
  128. dev->pump_press_diff_low_err = (e.frame.data[2] & 0x04) != 0 ? 1 : 0;
  129. dev->out_air_over_temp_lock = (e.frame.data[2] & 0x08) != 0 ? 1 : 0;
  130. dev->sys_high_pressure_lock = (e.frame.data[2] & 0x10) != 0 ? 1 : 0;
  131. dev->sys_low_pressure_lock = (e.frame.data[2] & 0x20) != 0 ? 1 : 0;
  132. dev->comp_VFD_over_current_lock = (e.frame.data[2] & 0x40) != 0 ? 1 : 0;
  133. dev->comp_VFD_over_voltage_lock = (e.frame.data[2] & 0x80) != 0 ? 1 : 0;
  134. dev->comp_VFD_under_voltage_lock = (e.frame.data[3] & 0x01) != 0 ? 1 : 0;
  135. dev->comp_VFD_over_temp_lock = (e.frame.data[3] & 0x02) != 0 ? 1 : 0;
  136. dev->comp_VFD_miss_phrase_lock = (e.frame.data[3] & 0x04) != 0 ? 1 : 0;
  137. dev->comp_VFD_others_lock = (e.frame.data[3] & 0x08) != 0 ? 1 : 0;
  138. }
  139. else if ((e.frame.can_id & (~CAN_EFF_FLAG)) == 0x18088040) // 帧六,读取所有状态
  140. {
  141. // log_dbg("evc_comm_dac, frame6");
  142. dev->high_press_sensor_err = (e.frame.data[0] & 0x01) != 0 ? 1 : 0;
  143. dev->env_temp_sensor_err = (e.frame.data[0] & 0x02) != 0 ? 1 : 0;
  144. dev->in_water_temp_sensor_err = (e.frame.data[0] & 0x04) != 0 ? 1 : 0;
  145. dev->out_air_temp_sensor_err = (e.frame.data[0] & 0x08) != 0 ? 1 : 0;
  146. dev->condensate_sensor_err = (e.frame.data[0] & 0x10) != 0 ? 1 : 0;
  147. dev->unexpected_power_off_err = (e.frame.data[0] & 0x20) != 0 ? 1 : 0;
  148. dev->out_water_low_temp_err = (e.frame.data[0] & 0x40) != 0 ? 1 : 0;
  149. dev->out_water_high_temp_err = (e.frame.data[0] & 0x80) != 0 ? 1 : 0;
  150. dev->out_water_over_press_err = (e.frame.data[1] & 0x01) != 0 ? 1 : 0;
  151. }
  152. else
  153. {
  154. e_cache[cache_count++] = e;
  155. }
  156. }
  157. chan_socketcan_rxrb_queue_arr(chanidx, e_cache, cache_count); // 重新入队
  158. chan_unlock(chanidx);
  159. comm_stop_cal_dac_timing(comm);
  160. }
  161. int evc_dev_control_strategy_pack_temp(int idx)
  162. {
  163. chan_socketcan_ringbuffer_element_t e;
  164. int chanidx = evc[idx].comm.chanidx;
  165. static int cool_set = -1;
  166. static int heat_set = -1;
  167. // if (pack_get_state() != SMST_ERR && pack_get_celltmax() >= 25 || ac_get_state(1) != SMST_ERR && evc[idx].out_water_temp > 24) // 25 + 50% * 2
  168. if (pack_get_celltmax() >= 25) // 25 + 50% * 2
  169. {
  170. if (cool_set == -1)
  171. {
  172. cool_set = evc[idx].out_water_temp;
  173. if (ctn_get_state() == SMST_DHG || ctn_get_state() == SMST_CHG)
  174. {
  175. cool_set -= 4;
  176. }
  177. if (cool_set <= 16)
  178. cool_set = 16; // 保证最低温度在 15 以上,保护电芯
  179. }
  180. // log_dbg("evc_dev_control_strategy_pack_temp, idx:%d, temp:%d, cooling, cool set :%d", idx, pack_get_celltmax(), cool_set);
  181. e.frame.can_id = 0x04904000;
  182. e.frame.can_id |= CAN_EFF_FLAG;
  183. e.frame.can_dlc = 8;
  184. e.frame.data[0] = 0x01; // 制冷
  185. e.frame.data[1] = 0;
  186. e.frame.data[2] = 0;
  187. e.frame.data[3] = 0;
  188. e.frame.data[4] = cool_set + 40;
  189. e.frame.data[5] = 0;
  190. e.frame.data[6] = 0;
  191. e.frame.data[7] = 0;
  192. chan_lock(chanidx);
  193. chan_socketcan_txrb_queue(chanidx, e);
  194. chan_unlock(chanidx);
  195. }
  196. else if (pack_get_celltmin() <= 16)
  197. {
  198. if (heat_set == -1)
  199. {
  200. heat_set = evc[idx].out_water_temp;
  201. if (heat_set >= 18)
  202. heat_set = 18;
  203. }
  204. // log_dbg("evc_dev_control_strategy_pack_temp, idx:%d, temp:%d, heating, heat set %d", idx, pack_get_celltmin(), heat_set);
  205. e.frame.can_id = 0x04904000;
  206. e.frame.can_id |= CAN_EFF_FLAG;
  207. e.frame.can_dlc = 8;
  208. e.frame.data[0] = 0x02; // 制热
  209. e.frame.data[1] = 0;
  210. e.frame.data[2] = 0;
  211. e.frame.data[3] = 0;
  212. e.frame.data[4] = heat_set + 40; // 制热点尽量高
  213. e.frame.data[5] = 0;
  214. e.frame.data[6] = 0;
  215. e.frame.data[7] = 0;
  216. chan_lock(chanidx);
  217. chan_socketcan_txrb_queue(chanidx, e);
  218. chan_unlock(chanidx);
  219. }
  220. else
  221. {
  222. cool_set = -1;
  223. heat_set = -1;
  224. // log_dbg("evc_dev_control_strategy_pack_temp, idx:%d,cell temp max:%d, min:%d stop", idx, pack_get_celltmax(), pack_get_celltmin());
  225. // evc_dev_control_keep_can_communication(idx);
  226. // evc_set_dev_stop(idx);
  227. // log_dbg("evc_dev_control_strategy_pack_temp, idx:%d,cell temp max:%d, min:%d self circulation", idx, pack_get_celltmax(), pack_get_celltmin());
  228. evc_set_dev_self_circulation(idx);
  229. }
  230. // evc[idx].cool_set = cool_set;
  231. // evc[idx].heat_set = heat_set;
  232. }
  233. // 发送控制帧,不出现通信失败
  234. int evc_dev_control_keep_can_communication(int idx)
  235. {
  236. chan_socketcan_ringbuffer_element_t e;
  237. int chanidx = evc[idx].comm.chanidx;
  238. e.frame.can_id = 0x04904000;
  239. e.frame.can_id |= CAN_EFF_FLAG;
  240. e.frame.can_dlc = 8;
  241. e.frame.data[0] = 1;
  242. e.frame.data[1] = 0;
  243. e.frame.data[2] = 0;
  244. e.frame.data[3] = 0;
  245. e.frame.data[4] = 40; // 范围是 5~35,0会在范围外,超过范围就会设置失败,不会改变模式
  246. e.frame.data[5] = 0;
  247. e.frame.data[6] = 0;
  248. e.frame.data[7] = 0;
  249. chan_lock(chanidx);
  250. chan_socketcan_txrb_queue(chanidx, e);
  251. chan_unlock(chanidx);
  252. return 0;
  253. }
  254. int evc_set_dev_stop(int idx)
  255. {
  256. chan_socketcan_ringbuffer_element_t e;
  257. int chanidx = evc[idx].comm.chanidx;
  258. e.frame.can_id = 0x04904000;
  259. e.frame.can_id |= CAN_EFF_FLAG;
  260. e.frame.can_dlc = 8;
  261. e.frame.data[0] = 0; // 1.2-1.1, 3 自循环
  262. e.frame.data[1] = 0;
  263. e.frame.data[2] = 0;
  264. e.frame.data[3] = 0;
  265. e.frame.data[4] = 0;
  266. e.frame.data[5] = 0;
  267. e.frame.data[6] = 0;
  268. e.frame.data[7] = 0;
  269. chan_lock(chanidx);
  270. chan_socketcan_txrb_queue(chanidx, e);
  271. chan_unlock(chanidx);
  272. return 0;
  273. }
  274. int evc_set_dev_self_circulation(int idx)
  275. {
  276. chan_socketcan_ringbuffer_element_t e;
  277. int chanidx = evc[idx].comm.chanidx;
  278. e.frame.can_id = 0x04904000;
  279. e.frame.can_id |= CAN_EFF_FLAG;
  280. e.frame.can_dlc = 8;
  281. e.frame.data[0] = 3; // 1.2-1.1, 3 自循环
  282. e.frame.data[1] = 0;
  283. e.frame.data[2] = 0;
  284. e.frame.data[3] = 0;
  285. e.frame.data[4] = 0;
  286. e.frame.data[5] = 0;
  287. e.frame.data[6] = 0;
  288. e.frame.data[7] = 0;
  289. chan_lock(chanidx);
  290. chan_socketcan_txrb_queue(chanidx, e);
  291. chan_unlock(chanidx);
  292. return 0;
  293. }
  294. int evc_comm_init(int idx)
  295. {
  296. struct evc_t *dev = &evc[idx];
  297. struct comm_t *comm = &dev->comm;
  298. comm_set_state(comm, COMMST_ERR);
  299. return 0;
  300. }
  301. int evc_comm_reset(int idx)
  302. {
  303. struct evc_t *dev = &evc[idx];
  304. struct comm_t *comm = &dev->comm;
  305. comm_set_state(comm, COMMST_NORMAL);
  306. return 0;
  307. }