You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

435 lines
14 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <stdbool.h> //定义布尔
  2. #include <string.h>
  3. #include "board.h"
  4. //
  5. #include "zes8p5066lib/basic.h"
  6. #include "zes8p5066lib/gpio.h"
  7. #include "zes8p5066lib/key.h"
  8. #include "zes8p5066lib/systicket.h"
  9. #include "zes8p5066lib/uart0.h"
  10. //
  11. #include "service/light_control_service.h"
  12. #include "service/thisdevice.h"
  13. #include "test.h"
  14. #include "zsimple_timer/zsimple_timer.h"
  15. /***********************************************************************************************************************
  16. * =========================================================================================================== *
  17. ***********************************************************************************************************************/
  18. void onkey(zkey_t* key, zkey_state_t key_state);
  19. /***********************************************************************************************************************
  20. * =========================================================================================================== *
  21. ***********************************************************************************************************************/
  22. static zkey_t s_keys[] = {
  23. ZKEY_INIT("powerkey", port_gpio_get_power_key_state), //电源按键
  24. ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
  25. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
  26. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
  27. };
  28. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  29. static zsimple_timer_t zsimple_timer_mem[10]; //最多同时存在10个定时器
  30. zsimple_timer_t* debuglighttimer;
  31. /***********************************************************************************************************************
  32. * =============================================================================================================== *
  33. ***********************************************************************************************************************/
  34. static uint32_t compute_countdown_num(int countdowns) {
  35. if (countdowns) return countdowns / kconst_countdown_step_s + !!(countdowns % kconst_countdown_step_s);
  36. }
  37. static void shutdwon() {
  38. printf("power off\n");
  39. thisDevice.poweron = false;
  40. port_fan_set(false);
  41. //@TODO:关闭臭氧
  42. }
  43. static void increase_and_assign_countdonwnum() {
  44. if (thisDevice.countdonwnum == 4) {
  45. thisDevice.countdonwnum = 0;
  46. thisDevice.countdonwnum_s = 0;
  47. thisDevice.countdonw_setting_num = 0;
  48. lcs_active_input(knone_active);
  49. } else {
  50. thisDevice.countdonwnum_s = (thisDevice.countdonwnum + 1) * kconst_countdown_step_s;
  51. thisDevice.countdonwnum = thisDevice.countdonwnum + 1;
  52. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  53. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  54. }
  55. }
  56. static void set_countdown(int countdownnum) {
  57. thisDevice.countdonwnum = countdownnum;
  58. thisDevice.countdonwnum_s = thisDevice.countdonwnum * kconst_countdown_step_s;
  59. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  60. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  61. }
  62. static void onkey(zkey_t* key, zkey_state_t key_state) {
  63. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  64. printf("on %s \n", key->name);
  65. if (!thisDevice.poweron) {
  66. printf("power on\n");
  67. thisDevice.poweron = true;
  68. thisDevice.level = klevel1;
  69. thisDevice.mode = knormal;
  70. //设置灯光效果
  71. port_fan_set(true);
  72. //@TODO:启动臭氧
  73. } else {
  74. shutdwon();
  75. }
  76. return;
  77. }
  78. // levelkey
  79. if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  80. if (!thisDevice.poweron) return;
  81. printf("on %s \n", key->name);
  82. /**
  83. * @brief
  84. */
  85. if (!lcs_input_is_active(kchange_level_input)) {
  86. lcs_active_input(kchange_level_input);
  87. return;
  88. }
  89. if (thisDevice.level == klevel1) {
  90. printf("changet level to level2\n");
  91. thisDevice.level = klevel2;
  92. //更改臭氧状态
  93. } else if (thisDevice.level == klevel2) {
  94. printf("changet level to level1\n");
  95. thisDevice.level = klevel1;
  96. //更改臭氧状态
  97. }
  98. lcs_active_input(kchange_level_input);
  99. return;
  100. }
  101. // timerkey
  102. if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  103. if (!thisDevice.poweron) return;
  104. printf("on %s \n", key->name);
  105. if (!lcs_input_is_active(kchange_countdonw_time_input)) {
  106. lcs_active_input(kchange_countdonw_time_input);
  107. if (thisDevice.mode != ktimingMode || thisDevice.countdonwnum_s == 0) {
  108. set_countdown(1);
  109. thisDevice.mode = ktimingMode;
  110. }
  111. return;
  112. }
  113. lcs_active_input(kchange_countdonw_time_input);
  114. increase_and_assign_countdonwnum();
  115. }
  116. if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  117. if (!thisDevice.poweron) return;
  118. printf("on %s \n", key->name);
  119. if (!lcs_input_is_active(kchange_intermittentmode_time_input)) {
  120. lcs_active_input(kchange_intermittentmode_time_input);
  121. if (thisDevice.mode != kintermittentMode || thisDevice.countdonwnum_s == 0) {
  122. set_countdown(1);
  123. thisDevice.mode = kintermittentMode;
  124. }
  125. return;
  126. }
  127. lcs_active_input(kchange_intermittentmode_time_input);
  128. increase_and_assign_countdonwnum();
  129. }
  130. }
  131. void compute_countdown() {
  132. if /* */ (thisDevice.mode == knormal) {
  133. thisDevice.countdonwnum = 0;
  134. thisDevice.countdonwnum_s = 0;
  135. } else if (thisDevice.mode == ktimingMode || thisDevice.mode == kintermittentMode) {
  136. if (thisDevice.countdonwnum_s != 0) {
  137. thisDevice.countdonwnum_s = //
  138. thisDevice.countdonw_setting_num * kconst_countdown_step_s - systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000;
  139. thisDevice.countdonwnum = compute_countdown_num(thisDevice.countdonwnum_s);
  140. }
  141. }
  142. }
  143. void process_countdwonevent() {
  144. //计算countdown
  145. compute_countdown();
  146. /**
  147. * @brief
  148. */
  149. if (thisDevice.mode == ktimingMode) {
  150. //定时自动关机
  151. if (thisDevice.poweron) {
  152. if (thisDevice.countdonwnum_s == 0) {
  153. shutdwon();
  154. }
  155. }
  156. } else if (thisDevice.mode == kintermittentMode) {
  157. /**
  158. * @brief
  159. * ,,,,,
  160. */
  161. if (!thisDevice.intermittentMode_idle) {
  162. if (thisDevice.countdonwnum_s == 0) {
  163. //切换状态到待机状态
  164. // TODO:关闭臭氧
  165. thisDevice.intermittentMode_idle = true;
  166. port_fan_set(false);
  167. }
  168. } else {
  169. if (systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000 > INTERMITTENTMODE_PERIOD_S) {
  170. thisDevice.intermittentMode_idle = false;
  171. set_countdown(thisDevice.countdonw_setting_num);
  172. //启动设备
  173. port_fan_set(true);
  174. }
  175. }
  176. }
  177. };
  178. void do_debug_light_state(zsimple_timer_t* handler) {
  179. static uint8_t debug_led_state = 1;
  180. debug_led_state = !debug_led_state;
  181. port_debug_set(debug_led_state);
  182. }
  183. float get_ozone_power() {
  184. float powersum = 0;
  185. for (size_t i = 0; i < 5; i++) {
  186. powersum += port_adc_get_ozone_generator_power();
  187. }
  188. return powersum / 5;
  189. }
  190. #if 0
  191. typedef struct {
  192. float LastP; //上次估算协方差 初始化值为0.02
  193. float Now_P; //当前估算协方差 初始化值为0
  194. float out; //卡尔曼滤波器输出 初始化值为0
  195. float Kg; //卡尔曼增益 初始化值为0
  196. float Q; //过程噪声协方差 初始化值为0.001
  197. float R; //观测噪声协方差 初始化值为0.543
  198. } KFP; // Kalman Filter parameter
  199. // 2. 以高度为例 定义卡尔曼结构体并初始化参数
  200. KFP KFP_height = {0.02, 0, 0, 0, 0.03, 0.543};
  201. /**
  202. *
  203. *@param KFP *kfp
  204. * float input
  205. *@return
  206. */
  207. float kalmanFilter(KFP* kfp, float input) {
  208. //预测协方差方程:k时刻系统估算协方差 = k-1时刻的系统协方差 + 过程噪声协方差
  209. kfp->Now_P = kfp->LastP + kfp->Q;
  210. //卡尔曼增益方程:卡尔曼增益 = k时刻系统估算协方差 / (k时刻系统估算协方差 + 观测噪声协方差)
  211. kfp->Kg = kfp->Now_P / (kfp->Now_P + kfp->R);
  212. //更新最优值方程:k时刻状态变量的最优值 = 状态变量的预测值 + 卡尔曼增益 * (测量值 - 状态变量的预测值)
  213. kfp->out = kfp->out + kfp->Kg * (input - kfp->out); //因为这一次的预测值就是上一次的输出值
  214. //更新协方差方程: 本次的系统协方差付给 kfp->LastP 威下一次运算准备。
  215. kfp->LastP = (1 - kfp->Kg) * kfp->Now_P;
  216. return kfp->out;
  217. }
  218. float derivative(float now) {
  219. static float last = 0;
  220. float diff = now - last;
  221. last = now;
  222. return diff;
  223. }
  224. void printf_ozone_freq_and_power_table() {
  225. /**
  226. * @brief
  227. */
  228. float startfreq = 10 * 1000;
  229. float stepfreq = 100;
  230. float nowfreq = startfreq;
  231. port_ozone_pwm_set_duty(nowfreq, 1000);
  232. port_ozone_pwm_start();
  233. systicket_delay_ms(10);
  234. bool first = true;
  235. while (true) {
  236. port_ozone_pwm_set_duty(nowfreq, 3000);
  237. systicket_delay_ms(2);
  238. float power = get_ozone_power();
  239. // printf("%f %f\n", nowfreq, freq);
  240. // printf("{freq power:%f,%f}\r\n", nowfreq, power);
  241. // printf("{freq power:%f,%f}\r\n", nowfreq, power);
  242. if (first) {
  243. // kalmanFilter(&KFP_height, power);
  244. KFP_height.LastP = power;
  245. first = false;
  246. }
  247. float afterfilter = kalmanFilter(&KFP_height, power);
  248. printf("%f,%f,%f,%f\r\n", nowfreq, power, afterfilter,derivative(afterfilter)*10);
  249. // printf("{power:%f}\r\n", power);
  250. nowfreq += stepfreq;
  251. if (nowfreq >= 40 * 1000) break;
  252. }
  253. port_ozone_pwm_set_duty(33000, 3000);
  254. port_fan_set(true);
  255. while (true) {
  256. }
  257. port_ozone_pwm_stop();
  258. while (true) {
  259. systicket_delay_ms(2);
  260. float power = get_ozone_power();
  261. float afterfilter = kalmanFilter(&KFP_height, power);
  262. nowfreq+=50;
  263. printf("%f,%f,%f\r\n", nowfreq, power, afterfilter);
  264. }
  265. }
  266. #endif
  267. #if 0
  268. float kalmanFilter1(float input) {
  269. static KFP kfp = {0.02, 0, 0, 0, 0.03, 0.543};
  270. return kalmanFilter(&kfp, input);
  271. }
  272. float kalmanFilter2(float input) {
  273. static KFP kfp = {0.02, 0, 0, 0, 0.03, 0.543};
  274. return kalmanFilter(&kfp, input);
  275. }
  276. float kalmanFilter3(float input) {
  277. static KFP kfp = {0.02, 0, 0, 0, 0.03, 0.543};
  278. return kalmanFilter(&kfp, input);
  279. }
  280. float kalmanFilter4(float input) {
  281. static KFP kfp = {0.02, 0, 0, 0, 0.03, 0.543};
  282. return kalmanFilter(&kfp, input);
  283. }
  284. float set_pwm_and_get_power(uint32_t freq, uint32_t dutyns) {
  285. port_ozone_pwm_set_duty(freq, dutyns);
  286. systicket_delay_ms(2);
  287. float power1 = get_ozone_power();
  288. return power1;
  289. }
  290. void printf_ozone_freq_and_power_table() {
  291. /**
  292. * @brief
  293. */
  294. uint32_t startfreq = 10 * 1000;
  295. uint32_t stepfreq = 100;
  296. uint32_t nowfreq = startfreq;
  297. port_ozone_pwm_set_duty(nowfreq, 1000);
  298. // port_ozone_pwm_stop();
  299. port_ozone_pwm_start();
  300. systicket_delay_ms(10);
  301. bool first = true;
  302. while (true) {
  303. // printf("%d,%f,%f,%f,%f\r\n", nowfreq, //
  304. // /*kalmanFilter1*/(set_pwm_and_get_power(nowfreq, 2000)), //
  305. // /*kalmanFilter2*/(set_pwm_and_get_power(nowfreq, 4000)), //
  306. // /*kalmanFilter3*/(set_pwm_and_get_power(nowfreq, 6000)), //
  307. // /*kalmanFilter4*/(set_pwm_and_get_power(nowfreq, 8000)) //
  308. // );
  309. // printf("{power:%f}\r\n", power);
  310. nowfreq += stepfreq;
  311. if (nowfreq >= 40 * 1000) break;
  312. }
  313. port_ozone_pwm_set_duty(33000, 3500);
  314. port_fan_set(true);
  315. while (true) {
  316. }
  317. }
  318. #endif
  319. // void printf_ozone_freq_and_power_table() {
  320. // /**
  321. // * @brief 打印臭氧发生器和频率之间的关系
  322. // */
  323. // float startfreq = 20 * 1000;
  324. // float stepfreq = 50;
  325. // float nowfreq = startfreq;
  326. // // port_ozone_pwm_set_duty(nowfreq, 1000);
  327. // // port_ozone_pwm_start();
  328. // // systicket_delay_ms(10);
  329. // while (true) {
  330. // // printf("{power:%f}\r\n", nowfreq);
  331. // printf("%d,%d\r\n", (int)nowfreq,(int)nowfreq);
  332. // systicket_delay_ms(100);
  333. // nowfreq += stepfreq;
  334. // // if (nowfreq >= 40 * 1000) break;
  335. // }
  336. // port_ozone_pwm_stop();
  337. // }
  338. void init_all_subdevice_state() {
  339. port_debug_set(false);
  340. port_fan_set(false);
  341. port_led0_set(false);
  342. port_led1_set(false);
  343. port_led2_set(false);
  344. port_led3_set(false);
  345. port_led_r_set(false);
  346. port_led_g_set(false);
  347. port_led_b_set(false);
  348. }
  349. int main(void) {
  350. SystemInit(); //配置系统时钟
  351. DeviceClockAllEnable(); //打开所有外设时钟
  352. systicket_init();
  353. /*系统初始化*/
  354. zgpio_init_all_gpio(); //
  355. port_init(); //
  356. init_all_subdevice_state(); //
  357. printf("==========OZONE_GENERATOR==========\n"); //
  358. printf("= manufactor: iflytop\n"); //
  359. printf("= version : %s\n", VERSION); //
  360. printf("=\n"); //
  361. /*组件初始化*/
  362. zsimple_timer_module_init(zsimple_timer_mem, ZARR_SIZE(zsimple_timer_mem), systicket_get_now_ms); //定时器模块初始化
  363. zkey_init(&key_module); //按键初始化
  364. debuglighttimer = zsimple_timer_alloc();
  365. zsimple_timer_trigger_static(debuglighttimer, 300, INFINITE_TIMES /*触发次数:*/, true, do_debug_light_state);
  366. /**
  367. * @brief
  368. * 20k起步50hz100ms计算功率
  369. */
  370. printf_ozone_freq_and_power_table();
  371. while (true) {
  372. //按键扫描逻辑
  373. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  374. END();
  375. zsimple_timer_schedule();
  376. lcs_shcedule();
  377. process_countdwonevent();
  378. // printf("countddonw %d\n", thisDevice.countdonwnum_s);
  379. }
  380. }