usr_lysis.c 9.2 KB

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