usr_lysis.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * usr_gpio.c
  3. *
  4. * Created on: 2022年4月11日
  5. * Author: amos
  6. */
  7. #include "onechip_include_header.h"
  8. #define MIN_TEMPERATUARE 5
  9. #define MAX_TEMPERATUARE 120
  10. #define COOL_WORK_THD 5
  11. #define HEAT_WORK_THD 2.5
  12. #define COOL_TEST_TIME 30
  13. #define HEAT_TEST_TIME 3000
  14. #define COOL_TEST_PWR -50
  15. #define HEAT_TEST_PWR 50
  16. #define THERM_ADC_CHN 5
  17. #define OVER_HEAT_EDGE 0.5
  18. #define HEAT_STABLE_TIME 300
  19. #define MAX_REMOVE_GAP 100
  20. #define EER_NOEXIST_TIME_GAP 500
  21. #define HEAT_ARRIVE_EDGE 0.5
  22. #define GOTO_IDLE_GAP 3000
  23. #define HEATCHECK_OUT 85
  24. #define CARD_STDCHANGE_TIME 100
  25. int32_t tupeExist = 0;
  26. extern void light_band_set(uint8_t mode_select, uint16_t speed_ms, uint32_t rgb_color);
  27. volatile uint32_t g_u32AdcIntFlag; //adc simple finish flag
  28. uint32_t Read_Tupe(void) {
  29. return (HAL_GPIO_ReadPin(MINI7_V2_TUBE_SW_GPIO_Port,MINI7_V2_TUBE_SW_Pin)) ? 0 : 1; // 检查PF5引脚值
  30. }
  31. void updatParm_lysis(void) {
  32. sysinf.lysis.offset = sysinf.lysis_offset;
  33. sysinf.lysis.overHeat = sysinf.lysis_overHeat;
  34. sysinf.lysis.overHeatTime = sysinf.lysis_overHeatTime;
  35. sysinf.lysis.pid.Kp = sysinf.lysis_Kp;
  36. sysinf.lysis.pid.Ki = sysinf.lysis_Ki ;
  37. sysinf.lysis.pid.Kd = sysinf.lysis_Kd;
  38. sysinf.lysis.idletemp = sysinf.lysis_idleTemp;
  39. sysinf.lysis.setgaol = sysinf.lysis_Goal;
  40. sysinf.lysis.worktime = sysinf.lysis_HeatTime * 10;
  41. }
  42. /**
  43. * @brief Motor_Init
  44. *
  45. * @param init key and optic input
  46. *
  47. * @return 0正常,1不能加热,2,不能制冷,3 传感器异常
  48. *
  49. * @details 上电,通过设置加热制冷,判断温度变化。
  50. */
  51. uint32_t Therm_Check(void) {
  52. float firstTemp,heattemp;
  53. firstTemp = getADC_Chanel_value(1);
  54. if(firstTemp < MIN_TEMPERATUARE || firstTemp > MAX_TEMPERATUARE){
  55. com_pwm_Set(sysinf.lysis.pwdId,100);
  56. return THERMOSTAT_SENSER_EER;
  57. }
  58. com_pwm_Set(sysinf.lysis.pwdId,HEAT_TEST_PWR);
  59. HAL_Delay(HEAT_TEST_TIME);
  60. heattemp = getADC_Chanel_value(1);
  61. com_pwm_Set(sysinf.lysis.pwdId,0);
  62. if((heattemp - firstTemp) < HEAT_WORK_THD){
  63. return THERMOSTAT_HEAT_EER;
  64. }
  65. return THERMOSTAT_NOR;
  66. }
  67. /**
  68. * @brief set_lysis
  69. *
  70. * @param goal 目标温度
  71. *
  72. * @return 无
  73. *
  74. * @details 设置目标温度。
  75. */
  76. void set_lysis(float goal) {
  77. sysinf.lysis.pid.goal = goal;
  78. if(goal > (sysinf.lysis.overHeat + sysinf.lysis.pid.realtemp )){
  79. sysinf.lysis.pid_st = PID_OVERHEAT;
  80. sysinf.lysis.pid.goal += sysinf.lysis.overHeat;
  81. }else{
  82. sysinf.lysis.pid_st = PID_ADJ;
  83. }
  84. sysinf.lysis.pid.integral = 0;
  85. sysinf.lysis.pid_stable = 0;
  86. }
  87. void Close_therm(float goal) {
  88. sysinf.lysis.pid_st = PID_IDLE;
  89. sysinf.lysis.pid.integral = 0;
  90. sysinf.lysis.pid_stable = 0;
  91. sysinf.lysis.pid.goal = 0;
  92. com_pwm_Set(sysinf.lysis.pwdId,0);
  93. }
  94. /**
  95. * @brief Therm_work
  96. *
  97. * @param adjflag 调节标记
  98. *
  99. *
  100. *
  101. * @details 温控器调节。
  102. */
  103. uint32_t Pid_work(void) {
  104. static uint32_t chrConter = 0;
  105. lysis_ctr_def *ptherm = &sysinf.lysis;
  106. float temp = getADC_Chanel_value(1);
  107. ptherm->pid.realtemp = temp + ptherm->offset;
  108. switch(ptherm->pid_st){
  109. case PID_OVERHEAT:{
  110. PIDRealize(&ptherm->pid);
  111. com_pwm_Set(ptherm->pwdId,ptherm->pid.variate);
  112. if (ptherm->pid.realtemp > (ptherm->pid.goal - OVER_HEAT_EDGE)){
  113. ptherm->pid_st = PID_OVERHEATLAST;
  114. chrConter = 0;
  115. }
  116. }break;
  117. case PID_OVERHEATLAST:{
  118. chrConter ++;
  119. if (chrConter > ptherm->overHeatTime){
  120. ptherm->pid_st = PID_ADJ;
  121. /*PID功率值维持不变*/
  122. ptherm->pid.integral = (ptherm->overHeat*ptherm->pid.Kp)/ptherm->pid.Ki;
  123. ptherm->pid.integral += (ptherm->pid.variate)/ptherm->pid.Ki;
  124. ptherm->pid.goal -= ptherm->overHeat;
  125. chrConter = 0;
  126. }
  127. }break;
  128. case PID_ADJ:{
  129. PIDRealize(&ptherm->pid);
  130. if ((ptherm->pid.realtemp > (ptherm->pid.goal - OVER_HEAT_EDGE)) && (ptherm->pid.realtemp < (ptherm->pid.goal + OVER_HEAT_EDGE))){
  131. if(chrConter < HEAT_STABLE_TIME){
  132. chrConter ++;
  133. }
  134. else{
  135. /*上报加热稳定*/
  136. ptherm->pid_stable = 1;
  137. }
  138. }else{
  139. if(chrConter){chrConter--;}
  140. else{ptherm->pid_stable = 0;}
  141. }
  142. }break;
  143. default:
  144. ptherm->pid_st = PID_IDLE;
  145. ptherm->pid.variate = 0;
  146. break;
  147. }
  148. com_pwm_Set(sysinf.lysis.pwdId,ptherm->pid.variate);
  149. // com_pwm_Set(sysinf.lysis.pwdId,80);
  150. return 0;
  151. }
  152. uint32_t get_lysis_st(float *temp,unsigned int *st,float *pwm) {
  153. *temp = sysinf.lysis.pid.realtemp;
  154. *st = sysinf.lysis.thermostat_st;
  155. *pwm = sysinf.lysis.pid.variate;
  156. return 0;
  157. }
  158. uint32_t lysis_work(void) {
  159. float temp;
  160. static uint32_t last10ms;
  161. static uint32_t cardStChang;
  162. static float firstTemp;
  163. lysis_ctr_def *ptherm = &sysinf.lysis;
  164. temp = getADC_Chanel_value(1);
  165. if(temp < MIN_TEMPERATUARE || temp > MAX_TEMPERATUARE){
  166. error_set(ERROR_TEMPSENSOR_EER);
  167. updateLysisState(SYS_STATE_ERROR_HW);
  168. ptherm->thermostat_st = THERMOSTAT_ERROR;
  169. ptherm->firstin = true;
  170. Close_therm(0);
  171. }
  172. ptherm->pid.realtemp = temp + ptherm->offset;
  173. switch(ptherm->thermostat_st){
  174. case THERMOSTAT_PWRON:{
  175. if(ptherm->firstin){
  176. ptherm->firstin = false;
  177. updateLysisState(SYS_STATE_PREPARE);sysinf.lysisState = SYS_STATE_PREPARE;
  178. firstTemp = getADC_Chanel_value(1);
  179. set_lysis(sysinf.lysis.setgaol);
  180. last10ms = sysinf.conter10ms;
  181. }else{
  182. float heattemp = getADC_Chanel_value(1);
  183. if((heattemp - firstTemp) > HEAT_WORK_THD || heattemp >HEATCHECK_OUT){
  184. ptherm->thermostat_st = THERMOSTAT_WARM;
  185. ptherm->firstin = true;
  186. ptherm->pid_st = PID_IDLE;
  187. }
  188. if((sysinf.conter10ms-last10ms)> HEAT_TEST_TIME){
  189. /*超时判断*/
  190. ptherm->thermostat_st = THERMOSTAT_ERROR;
  191. updateLysisState(SYS_STATE_ERROR_HW);
  192. error_set(ERROR_HEATFUC_EER);
  193. ptherm->firstin = true;
  194. }
  195. }
  196. }break;
  197. case THERMOSTAT_NOTICE:{
  198. if(ptherm->firstin){
  199. /*上电或者预热状态存在管管,停止加热*/
  200. updateLysisState(SYS_STATE_ERROR_HM);
  201. set_lysis( 0);
  202. ptherm->firstin = false;
  203. }else{
  204. if(Read_Tupe() == 0){
  205. /*管管移除,进入到预热状态*/
  206. ptherm->thermostat_st = THERMOSTAT_WARM;
  207. error_clear();
  208. ptherm->firstin = true;
  209. }
  210. }
  211. }break;
  212. case THERMOSTAT_WARM:{
  213. if(ptherm->firstin){
  214. updateLysisState(SYS_STATE_PREPARE);
  215. ptherm->firstin = false;
  216. if(ptherm->idletemp < 0.1){
  217. /*预热参数配置为0,无需要预热*/
  218. ptherm->thermostat_st = THERMOSTAT_IDLE;
  219. ptherm->firstin = true;
  220. }else{
  221. set_lysis(ptherm->idletemp);
  222. }
  223. }else{
  224. /*预热到位,进入空闲状态*/
  225. if(ptherm->pid.realtemp > (ptherm->idletemp - HEAT_ARRIVE_EDGE)){
  226. ptherm->thermostat_st = THERMOSTAT_IDLE;
  227. ptherm->firstin = true;
  228. }
  229. // if(Read_Tupe()){
  230. // /*预热插管管,进入提示状态*/
  231. // ptherm->thermostat_st = THERMOSTAT_NOTICE;
  232. // ptherm->firstin = TRUE;
  233. // }
  234. }
  235. }break;
  236. case THERMOSTAT_IDLE:{
  237. if(ptherm->firstin){
  238. // updateLysisState(SYS_STATE_READY);
  239. ptherm->firstin = false;
  240. Close_therm(0);
  241. ptherm->recGoalFlag = 0;
  242. firstTemp = getADC_Chanel_value(1);
  243. updateLysisState(SYS_STATE_CHECK);
  244. cardStChang = 0;
  245. }else{
  246. if(Read_Tupe()==1){
  247. if(((sysinf.conter10ms - last10ms)>CARD_STDCHANGE_TIME)&&cardStChang){
  248. set_lysis(ptherm->setgaol);
  249. ptherm->recGoalFlag = 0;
  250. firstTemp = getADC_Chanel_value(1);
  251. ptherm->thermostat_st = THERMOSTAT_INSERT;
  252. ptherm->firstin = 1;
  253. }
  254. }else{
  255. last10ms = sysinf.conter10ms;
  256. cardStChang = 1;
  257. }
  258. }
  259. }break;
  260. case THERMOSTAT_INSERT:{
  261. if(ptherm->firstin){
  262. ptherm->firstin = false;
  263. updateLysisState(SYS_STATE_RUNING);
  264. last10ms = sysinf.conter10ms;
  265. }else{
  266. /*加热异常判断*/
  267. if(((ptherm->pid.realtemp - firstTemp) < HEAT_WORK_THD)&&
  268. ((sysinf.conter10ms-last10ms)> HEAT_TEST_TIME)&&((ptherm->pid.goal - firstTemp) > 10.0)){
  269. /*目标温差大于10,加热启动30s之内没升温2.5° 超时判断*/
  270. ptherm->thermostat_st = THERMOSTAT_ERROR;
  271. updateLysisState(SYS_STATE_ERROR_HW);
  272. error_set(ERROR_HEATFUC_EER);
  273. ptherm->firstin = true;
  274. }
  275. if((ptherm->pid.realtemp + 5) >ptherm->setgaol ) {
  276. ptherm->thermostat_st = THERMOSTAT_WORK;
  277. ptherm->firstin = true;
  278. }
  279. }
  280. }break;
  281. case THERMOSTAT_WORK:{
  282. if(ptherm->firstin){
  283. updateLysisState(SYS_STATE_RUNING);
  284. ptherm->firstin = false;
  285. ptherm->pid_stable_time = 0;
  286. }else{
  287. // if(Read_Tupe()){
  288. // last10ms = conter10ms;
  289. // }else{
  290. // if((conter10ms-last10ms)> MAX_REMOVE_GAP){
  291. // Close_therm(0);
  292. // ptherm->eerst = THERMOSTAT_TUBENOEXIST_EER;
  293. // ptherm->thermostat_st = THERMOSTAT_ERROR;
  294. // error_set(ERROR_TUPE_MOVE);
  295. // last10ms = conter10ms;
  296. // ptherm->firstin = TRUE;
  297. // }
  298. // }
  299. // if(ptherm->pid_stable)
  300. {
  301. ptherm->pid_stable_time ++;
  302. if(ptherm->pid_stable_time > ptherm->worktime){
  303. ptherm->thermostat_st = THERMOSTAT_END;
  304. ptherm->firstin = true;
  305. }
  306. }
  307. }
  308. }break;
  309. case THERMOSTAT_END:{
  310. if(ptherm->firstin){
  311. updateLysisState(SYS_STATE_WORKEND);
  312. ptherm->firstin = false;
  313. Close_therm(0);
  314. beep_set(BEEP_WORKEND);
  315. last10ms = sysinf.conter10ms;
  316. cardStChang = 0;
  317. }else{
  318. if(Read_Tupe() == 0){
  319. /*管管移除,进入到预热状态*/
  320. if(((sysinf.conter10ms - last10ms)>CARD_STDCHANGE_TIME)&&cardStChang){
  321. ptherm->thermostat_st = THERMOSTAT_IDLE;
  322. ptherm->firstin = true;
  323. }
  324. }else{
  325. last10ms = sysinf.conter10ms;
  326. cardStChang = 1;
  327. }
  328. }
  329. }break;
  330. case THERMOSTAT_ERROR:{
  331. if(ptherm->firstin){
  332. ptherm->firstin = false;
  333. Close_therm(0);
  334. }
  335. }break;
  336. }
  337. Pid_work();
  338. return 0;
  339. }
  340. uint32_t Close_Therm_work(void) {
  341. lysis_ctr_def *ptherm = &sysinf.lysis;
  342. ptherm->thermostat_st = THERMOSTAT_END;
  343. ptherm->firstin = true;
  344. return 0;
  345. }