usr_lysis.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * usr_gpio.c
  3. *
  4. * Created on: 2022年4月11日
  5. * Author: amos
  6. */
  7. #include "onechip_include_header.h"
  8. extern uint32_t conter100ms;
  9. extern uint32_t conter10ms;
  10. #define MIN_TEMPERATUARE 5
  11. #define MAX_TEMPERATUARE 120
  12. #define COOL_WORK_THD 5
  13. #define HEAT_WORK_THD 2.5
  14. #define COOL_TEST_TIME 30
  15. #define HEAT_TEST_TIME 6000
  16. #define COOL_TEST_PWR -50
  17. #define HEAT_TEST_PWR 50
  18. #define THERM_ADC_CHN 5
  19. #define OVER_HEAT_EDGE 0.5
  20. #define HEAT_STABLE_TIME 300
  21. #define MAX_REMOVE_GAP 100
  22. #define EER_NOEXIST_TIME_GAP 500
  23. #define HEAT_ARRIVE_EDGE 0.5
  24. #define GOTO_IDLE_GAP 3000
  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. 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. lysis_ctr_def *ptherm = &sysinf.lysis;
  157. temp = getADC_Chanel_value(1);
  158. ptherm->pid.realtemp = temp + ptherm->offset;
  159. switch(ptherm->thermostat_st){
  160. case THERMOSTAT_PWRON:{
  161. static float firstTemp;
  162. if(ptherm->firstin){
  163. ptherm->firstin = false;
  164. updateLysisState(SYS_STATE_PREPARE);sysinf.lysisState = SYS_STATE_PREPARE;
  165. firstTemp = getADC_Chanel_value(1);
  166. if(firstTemp < MIN_TEMPERATUARE || firstTemp > MAX_TEMPERATUARE){
  167. error_set(ERROR_TEMPSENSOR_EER);
  168. ptherm->thermostat_st = THERMOSTAT_ERROR;
  169. ptherm->firstin = true;
  170. }else{
  171. set_lysis(firstTemp+30);
  172. last10ms = conter10ms;
  173. }
  174. }else{
  175. float heattemp = getADC_Chanel_value(1);
  176. if((heattemp - firstTemp) > HEAT_WORK_THD){
  177. ptherm->thermostat_st = THERMOSTAT_WARM;
  178. ptherm->firstin = true;
  179. ptherm->pid_st = PID_IDLE;
  180. }
  181. if((conter10ms-last10ms)> HEAT_TEST_TIME){
  182. /*超时判断*/
  183. ptherm->thermostat_st = THERMOSTAT_ERROR;
  184. updateLysisState(SYS_STATE_ERROR_HW);
  185. error_set(ERROR_HEATFUC_EER);
  186. ptherm->firstin = true;
  187. }
  188. }
  189. }break;
  190. case THERMOSTAT_NOTICE:{
  191. if(ptherm->firstin){
  192. /*上电或者预热状态存在管管,停止加热*/
  193. updateLysisState(SYS_STATE_ERROR_HM);
  194. set_lysis( 0);
  195. ptherm->firstin = false;
  196. }else{
  197. if(Read_Tupe() == 0){
  198. /*管管移除,进入到预热状态*/
  199. ptherm->thermostat_st = THERMOSTAT_WARM;
  200. error_clear();
  201. ptherm->firstin = true;
  202. }
  203. }
  204. }break;
  205. case THERMOSTAT_WARM:{
  206. if(ptherm->firstin){
  207. updateLysisState(SYS_STATE_PREPARE);
  208. ptherm->firstin = false;
  209. if(ptherm->idletemp < 0.1){
  210. /*预热参数配置为0,无需要预热*/
  211. ptherm->thermostat_st = THERMOSTAT_IDLE;
  212. ptherm->firstin = true;
  213. }else{
  214. set_lysis(ptherm->idletemp);
  215. }
  216. }else{
  217. /*预热到位,进入空闲状态*/
  218. if(ptherm->pid.realtemp > (ptherm->idletemp - HEAT_ARRIVE_EDGE)){
  219. ptherm->thermostat_st = THERMOSTAT_IDLE;
  220. ptherm->firstin = true;
  221. }
  222. // if(Read_Tupe()){
  223. // /*预热插管管,进入提示状态*/
  224. // ptherm->thermostat_st = THERMOSTAT_NOTICE;
  225. // ptherm->firstin = TRUE;
  226. // }
  227. }
  228. }break;
  229. case THERMOSTAT_IDLE:{
  230. if(ptherm->firstin){
  231. updateLysisState(SYS_STATE_READY);
  232. ptherm->firstin = false;
  233. Close_therm(0);
  234. ptherm->recGoalFlag = 0;
  235. }else{
  236. if(ptherm->recGoalFlag){
  237. set_lysis(ptherm->pid.goal);
  238. ptherm->recGoalFlag = 0;
  239. }
  240. if(ptherm->pid_stable){
  241. ptherm->thermostat_st = THERMOSTAT_INSERT;
  242. ptherm->firstin = true;
  243. }
  244. // if(Read_Tupe()){
  245. // /*预热插管管,进入提示状态*/
  246. // ptherm->thermostat_st = THERMOSTAT_NOTICE;
  247. // ptherm->firstin = TRUE;
  248. // }
  249. }
  250. }break;
  251. case THERMOSTAT_INSERT:{
  252. if(ptherm->firstin){
  253. updateLysisState(SYS_STATE_CHECK);
  254. ptherm->firstin = false;
  255. }else{
  256. if(Read_Tupe()){
  257. ptherm->thermostat_st = THERMOSTAT_WORK;
  258. ptherm->firstin = true;
  259. }
  260. }
  261. }break;
  262. case THERMOSTAT_WORK:{
  263. if(ptherm->firstin){
  264. updateLysisState(SYS_STATE_RUNING);
  265. ptherm->firstin = false;
  266. ptherm->pid_stable_time = 0;
  267. }else{
  268. // if(Read_Tupe()){
  269. // last10ms = conter10ms;
  270. // }else{
  271. // if((conter10ms-last10ms)> MAX_REMOVE_GAP){
  272. // Close_therm(0);
  273. // ptherm->eerst = THERMOSTAT_TUBENOEXIST_EER;
  274. // ptherm->thermostat_st = THERMOSTAT_ERROR;
  275. // error_set(ERROR_TUPE_MOVE);
  276. // last10ms = conter10ms;
  277. // ptherm->firstin = TRUE;
  278. // }
  279. // }
  280. // if(ptherm->pid_stable)
  281. {
  282. ptherm->pid_stable_time ++;
  283. if(ptherm->pid_stable_time > ptherm->worktime){
  284. ptherm->thermostat_st = THERMOSTAT_END;
  285. ptherm->firstin = true;
  286. }
  287. }
  288. }
  289. }break;
  290. case THERMOSTAT_END:{
  291. if(ptherm->firstin){
  292. updateLysisState(SYS_STATE_WORKEND);
  293. ptherm->firstin = false;
  294. Close_therm(0);
  295. last10ms = conter10ms;
  296. }else{
  297. if((conter10ms-last10ms)> GOTO_IDLE_GAP){
  298. // if(Read_Tupe() == 0){
  299. /*管管移除,进入到预热状态*/
  300. ptherm->thermostat_st = THERMOSTAT_WARM;
  301. ptherm->firstin = true;
  302. }
  303. }
  304. }break;
  305. case THERMOSTAT_ERROR:{
  306. if(ptherm->firstin){
  307. ptherm->firstin = false;
  308. Close_therm(0);
  309. }else{
  310. if(ptherm->eerst == THERMOSTAT_TUBENOEXIST_EER){
  311. if((conter10ms-last10ms)> EER_NOEXIST_TIME_GAP){
  312. ptherm->thermostat_st = THERMOSTAT_IDLE;
  313. error_clear();
  314. ptherm->firstin = true;
  315. }
  316. }
  317. }
  318. }break;
  319. }
  320. Pid_work();
  321. return 0;
  322. }
  323. uint32_t Close_Therm_work(void) {
  324. lysis_ctr_def *ptherm = &sysinf.lysis;
  325. ptherm->thermostat_st = THERMOSTAT_END;
  326. ptherm->firstin = true;
  327. return 0;
  328. }