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