usr_lysis.c 9.1 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. }
  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. com_pwm_Set(sysinf.lysis.pwdId,0);
  89. }
  90. /**
  91. * @brief Therm_work
  92. *
  93. * @param adjflag 调节标记
  94. *
  95. *
  96. *
  97. * @details 温控器调节。
  98. */
  99. uint32_t Pid_work(void) {
  100. static uint32_t chrConter = 0;
  101. lysis_ctr_def *ptherm = &sysinf.lysis;
  102. float temp = getADC_Chanel_value(1);
  103. ptherm->pid.realtemp = temp + ptherm->offset;
  104. switch(ptherm->pid_st){
  105. case PID_OVERHEAT:{
  106. PIDRealize(&ptherm->pid);
  107. com_pwm_Set(ptherm->pwdId,ptherm->pid.variate);
  108. if (ptherm->pid.realtemp > (ptherm->pid.goal - OVER_HEAT_EDGE)){
  109. ptherm->pid_st = PID_OVERHEATLAST;
  110. chrConter = 0;
  111. }
  112. }break;
  113. case PID_OVERHEATLAST:{
  114. chrConter ++;
  115. if (chrConter > ptherm->overHeatTime){
  116. ptherm->pid_st = PID_ADJ;
  117. /*PID功率值维持不变*/
  118. ptherm->pid.integral = (ptherm->overHeat*ptherm->pid.Kp)/ptherm->pid.Ki;
  119. ptherm->pid.integral += (ptherm->pid.variate)/ptherm->pid.Ki;
  120. ptherm->pid.goal -= ptherm->overHeat;
  121. chrConter = 0;
  122. }
  123. }break;
  124. case PID_ADJ:{
  125. PIDRealize(&ptherm->pid);
  126. if ((ptherm->pid.realtemp > (ptherm->pid.goal - OVER_HEAT_EDGE)) && (ptherm->pid.realtemp < (ptherm->pid.goal + OVER_HEAT_EDGE))){
  127. if(chrConter < HEAT_STABLE_TIME){
  128. chrConter ++;
  129. }
  130. else{
  131. /*上报加热稳定*/
  132. ptherm->pid_stable = 1;
  133. }
  134. }else{
  135. if(chrConter){chrConter--;}
  136. else{ptherm->pid_stable = 0;}
  137. }
  138. }break;
  139. default:
  140. ptherm->pid_st = PID_IDLE;
  141. ptherm->pid.variate = 0;
  142. break;
  143. }
  144. com_pwm_Set(sysinf.lysis.pwdId,ptherm->pid.variate);
  145. // com_pwm_Set(sysinf.lysis.pwdId,80);
  146. return 0;
  147. }
  148. uint32_t get_lysis_st(float *temp,unsigned int *st) {
  149. *temp = sysinf.lysis.pid.realtemp;
  150. *st = sysinf.lysis.thermostat_st;
  151. return 0;
  152. }
  153. uint32_t lysis_work(void) {
  154. float temp;
  155. static uint32_t last10ms;
  156. static float firstTemp;
  157. lysis_ctr_def *ptherm = &sysinf.lysis;
  158. temp = getADC_Chanel_value(1);
  159. if(temp < MIN_TEMPERATUARE || temp > MAX_TEMPERATUARE){
  160. error_set(ERROR_TEMPSENSOR_EER);
  161. updateLysisState(SYS_STATE_ERROR_HW);
  162. ptherm->thermostat_st = THERMOSTAT_ERROR;
  163. ptherm->firstin = true;
  164. Close_therm(0);
  165. }
  166. ptherm->pid.realtemp = temp + ptherm->offset;
  167. switch(ptherm->thermostat_st){
  168. case THERMOSTAT_PWRON:{
  169. if(ptherm->firstin){
  170. ptherm->firstin = false;
  171. updateLysisState(SYS_STATE_PREPARE);sysinf.lysisState = SYS_STATE_PREPARE;
  172. firstTemp = getADC_Chanel_value(1);
  173. set_lysis(95);
  174. ptherm->pid.goal = 95;
  175. ptherm->worktime = 3000;
  176. ptherm->lastgaol = 95;
  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. }else{
  241. if(ptherm->recGoalFlag){
  242. set_lysis(ptherm->pid.goal);
  243. ptherm->recGoalFlag = 0;
  244. updateLysisState(SYS_STATE_PREHEAT);
  245. firstTemp = getADC_Chanel_value(1);
  246. last10ms = sysinf.conter10ms;
  247. }
  248. if(ptherm->pid_stable){
  249. ptherm->thermostat_st = THERMOSTAT_INSERT;
  250. ptherm->firstin = true;
  251. }
  252. if(((ptherm->pid.realtemp - firstTemp) < HEAT_WORK_THD)&&
  253. ((sysinf.conter10ms-last10ms)> HEAT_TEST_TIME)&&(firstTemp<HEATCHECK_OUT)){
  254. /*目标温差大于10,加热启动30s之内没升温2.5° 超时判断*/
  255. ptherm->thermostat_st = THERMOSTAT_ERROR;
  256. updateLysisState(SYS_STATE_ERROR_HW);
  257. error_set(ERROR_HEATFUC_EER);
  258. ptherm->firstin = true;
  259. }
  260. }
  261. }break;
  262. case THERMOSTAT_INSERT:{
  263. if(ptherm->firstin){
  264. updateLysisState(SYS_STATE_CHECK);
  265. ptherm->firstin = false;
  266. }else{
  267. if(Read_Tupe()){
  268. ptherm->thermostat_st = THERMOSTAT_WORK;
  269. ptherm->firstin = true;
  270. }
  271. }
  272. }break;
  273. case THERMOSTAT_WORK:{
  274. if(ptherm->firstin){
  275. updateLysisState(SYS_STATE_RUNING);
  276. ptherm->firstin = false;
  277. ptherm->pid_stable_time = 0;
  278. }else{
  279. // if(Read_Tupe()){
  280. // last10ms = conter10ms;
  281. // }else{
  282. // if((conter10ms-last10ms)> MAX_REMOVE_GAP){
  283. // Close_therm(0);
  284. // ptherm->eerst = THERMOSTAT_TUBENOEXIST_EER;
  285. // ptherm->thermostat_st = THERMOSTAT_ERROR;
  286. // error_set(ERROR_TUPE_MOVE);
  287. // last10ms = conter10ms;
  288. // ptherm->firstin = TRUE;
  289. // }
  290. // }
  291. // if(ptherm->pid_stable)
  292. {
  293. ptherm->pid_stable_time ++;
  294. if(ptherm->pid_stable_time > ptherm->worktime){
  295. ptherm->thermostat_st = THERMOSTAT_END;
  296. ptherm->firstin = true;
  297. }
  298. }
  299. }
  300. }break;
  301. case THERMOSTAT_END:{
  302. if(ptherm->firstin){
  303. updateLysisState(SYS_STATE_WORKEND);
  304. ptherm->firstin = false;
  305. Close_therm(0);
  306. beep_set(BEEP_WORKEND);
  307. last10ms = sysinf.conter10ms;
  308. }else{
  309. if((sysinf.conter10ms-last10ms)> GOTO_IDLE_GAP){
  310. // if(Read_Tupe() == 0){
  311. /*管管移除,进入到预热状态*/
  312. ptherm->thermostat_st = THERMOSTAT_IDLE;
  313. ptherm->firstin = true;
  314. }
  315. }
  316. }break;
  317. case THERMOSTAT_ERROR:{
  318. if(ptherm->firstin){
  319. ptherm->firstin = false;
  320. Close_therm(0);
  321. }
  322. }break;
  323. }
  324. Pid_work();
  325. return 0;
  326. }
  327. uint32_t Close_Therm_work(void) {
  328. lysis_ctr_def *ptherm = &sysinf.lysis;
  329. ptherm->thermostat_st = THERMOSTAT_END;
  330. ptherm->firstin = true;
  331. return 0;
  332. }