usr_lysis.c 9.6 KB

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