usr_lysis.c 8.9 KB

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