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.

107 lines
3.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include "frequency_sweep_service.h"
  2. #include "zes8p5066lib/basic.h"
  3. #include "zes8p5066lib/systicket.h"
  4. //
  5. #include "kalmanFilter.h"
  6. /***********************************************************************************************************************
  7. * ====================================================THIS_MODULE==================================================== *
  8. ***********************************************************************************************************************/
  9. struct {
  10. float power_table[250];
  11. uint16_t startfreq;
  12. uint16_t endfreq;
  13. uint16_t step;
  14. bool is_schedule;
  15. bool firstloop;
  16. uint16_t nowfreq;
  17. uint32_t startticket;
  18. uint32_t dutyns;
  19. } this;
  20. static KFP KFPConfig = KALMAN_FILTER_PARA;
  21. /***********************************************************************************************************************
  22. * ====================================================PowerTable===================================================== *
  23. ***********************************************************************************************************************/
  24. static void mf_setpower(uint16_t freq, float power) {
  25. uint16_t index = (freq - this.startfreq) / this.step;
  26. if (index >= ZARR_SIZE(this.power_table)) return;
  27. this.power_table[index] = power;
  28. }
  29. static float mf_getpower(uint16_t freq) {
  30. uint16_t index = (freq - this.startfreq) / this.step;
  31. if (index >= ZARR_SIZE(this.power_table)) return 0;
  32. if (index > (this.endfreq - this.startfreq) / this.step) {
  33. return 0;
  34. }
  35. return this.power_table[index];
  36. }
  37. static float mf_get_ozone_power() {
  38. float powersum = 0;
  39. for (size_t i = 0; i < 10; i++) {
  40. powersum += port_adc_get_ozone_generator_power();
  41. }
  42. return powersum / 10;
  43. }
  44. /***********************************************************************************************************************
  45. * ======================================================Extern======================================================= *
  46. ***********************************************************************************************************************/
  47. void frequency_sweep_start(uint32_t startfreq, uint32_t step, uint32_t endfreq, uint16_t dutyns) {
  48. this.startfreq = startfreq;
  49. this.endfreq = endfreq;
  50. this.step = step;
  51. this.dutyns = dutyns;
  52. this.firstloop = true;
  53. this.is_schedule = true;
  54. this.startticket = systicket_get_now_ms();
  55. this.nowfreq = this.startfreq;
  56. printf("cls\n");
  57. }
  58. void frequency_sweep_stop() { this.is_schedule = false; }
  59. bool frequency_sweep_is_finished() { return !this.is_schedule; }
  60. float frequency_sweep_get_power(uint16_t freq) { return mf_getpower(freq); }
  61. void frequency_sweep_schedule() {
  62. if (!this.is_schedule) {
  63. return;
  64. }
  65. /// loop
  66. if (systicket_haspassedms(this.startticket) > 1) {
  67. /**
  68. * @brief
  69. */
  70. port_ozone_pwm_set_duty(this.nowfreq, this.dutyns);
  71. port_ozone_pwm_start();
  72. systicket_delay_ms(3);
  73. /**
  74. * @brief
  75. */
  76. float power = mf_get_ozone_power();
  77. if (this.firstloop) {
  78. KFPConfig.LastP = power;
  79. }
  80. float afterfileter = kalmanFilter(&KFPConfig, power);
  81. mf_setpower(this.nowfreq, afterfileter);
  82. printf("%d,%f,%f\n", this.nowfreq, afterfileter, power);
  83. /**
  84. * @brief
  85. */
  86. this.nowfreq += this.step;
  87. if (this.nowfreq > this.endfreq) {
  88. this.is_schedule = false;
  89. }
  90. port_ozone_pwm_stop();
  91. //
  92. this.firstloop = false;
  93. this.startticket = systicket_get_now_ms();
  94. }
  95. }