measure_current.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Xytronic LF-1600
  3. * Current measurement routines
  4. *
  5. * Copyright (c) 2015-2017 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "measure_current.h"
  22. #include "measure.h"
  23. #include "timer.h"
  24. #include "scale.h"
  25. #include "controller_current.h"
  26. #include "filter.h"
  27. #include "debug_uart.h"
  28. #include <string.h>
  29. /* Low pass filter time. */
  30. #define MEASCURR_FILTER_SHIFT 4
  31. struct meascurr_context {
  32. bool initialized;
  33. fixpt_t prev_feedback;
  34. struct lp_filter_u16 filter;
  35. int16_t old_filter_report_value;
  36. };
  37. static struct meascurr_context meascurr;
  38. void meascurr_filter_reset(void)
  39. {
  40. lp_filter_u16_reset(&meascurr.filter);
  41. }
  42. uint16_t meascurr_filter_handler(uint16_t raw_adc);
  43. uint16_t meascurr_filter_handler(uint16_t raw_adc)
  44. {
  45. uint16_t filtered_adc;
  46. /* Run a simple low pass filter. */
  47. if (meascurr.initialized) {
  48. filtered_adc = lp_filter_u16_run(&meascurr.filter, raw_adc,
  49. MEASCURR_FILTER_SHIFT);
  50. } else {
  51. lp_filter_u16_set(&meascurr.filter, raw_adc);
  52. filtered_adc = raw_adc;
  53. }
  54. filtered_adc = min(filtered_adc, MEASURE_MAX_RESULT);
  55. debug_report_int16(DEBUG_PFX1("fc"), &meascurr.old_filter_report_value,
  56. (int16_t)filtered_adc);
  57. return filtered_adc;
  58. }
  59. void meascurr_result_handler(fixpt_t measured_phys_value,
  60. enum measure_plausibility plaus);
  61. void meascurr_result_handler(fixpt_t measured_phys_value,
  62. enum measure_plausibility plaus)
  63. {
  64. uint8_t emergency_flags;
  65. /* Set/reset emergency status. */
  66. emergency_flags = contrcurr_get_emerg();
  67. if (plaus == MEAS_PLAUSIBLE)
  68. emergency_flags &= (uint8_t)~CONTRCURR_EMERG_UNPLAUS_FEEDBACK;
  69. else if (plaus == MEAS_PLAUS_TIMEOUT)
  70. emergency_flags |= CONTRCURR_EMERG_UNPLAUS_FEEDBACK;
  71. contrcurr_set_emerg(emergency_flags);
  72. /* Set the controller feedback. */
  73. if (plaus == MEAS_PLAUSIBLE) {
  74. meascurr.prev_feedback = measured_phys_value;
  75. contrcurr_set_feedback(measured_phys_value);
  76. } else if (plaus == MEAS_NOT_PLAUSIBLE) {
  77. /* Just run the controller with the previous feedback. */
  78. contrcurr_set_feedback(meascurr.prev_feedback);
  79. }
  80. meascurr.initialized = true;
  81. }
  82. extern
  83. const struct measure_config __flash meascurr_config;
  84. const struct measure_config __flash meascurr_config = {
  85. .name = "mc",
  86. .mux = MEAS_MUX_ADC2,
  87. .did = MEAS_DID_ADC2,
  88. .ps = MEAS_PS_64,
  89. .ref = MEAS_REF_AREF,
  90. .averaging_timeout_ms = 5,
  91. .scale_raw_lo = 0,
  92. .scale_raw_hi = 65,
  93. .scale_phys_lo = FLOAT_TO_FIXPT(AMPERE(0)),
  94. .scale_phys_hi = FLOAT_TO_FIXPT(AMPERE(1.1)),
  95. .plaus_neglim = FLOAT_TO_FIXPT(CONTRCURR_NEGLIM),
  96. .plaus_poslim = FLOAT_TO_FIXPT(CONTRCURR_POSLIM),
  97. .plaus_timeout_ms = 3000,
  98. };
  99. void meascurr_init(void)
  100. {
  101. meascurr.initialized = false;
  102. }