usr_lysis.c 9.1 KB

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