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.

130 lines
4.1KB

  1. // Copyright 2015 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // CV reader.
  28. #ifndef MARBLES_CV_READER_H_
  29. #define MARBLES_CV_READER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "marbles/drivers/adc.h"
  32. #include "marbles/cv_reader_channel.h"
  33. #include "marbles/settings.h"
  34. namespace marbles {
  35. class CvReader {
  36. public:
  37. CvReader() { }
  38. ~CvReader() { }
  39. void Init(CalibrationData* calibration_data);
  40. inline bool ready_for_calibration() const {
  41. return true;
  42. }
  43. inline void CalibrateRateC1() {
  44. cv_c1_[0] = channel_[ADC_CHANNEL_T_RATE].unscaled_cv_lp();
  45. }
  46. inline void CalibrateRateC3() {
  47. cv_c3_[0] = channel_[ADC_CHANNEL_T_RATE].unscaled_cv_lp();
  48. }
  49. inline void CalibrateSpreadC1() {
  50. cv_c1_[1] = 0.5f * channel_[ADC_CHANNEL_X_SPREAD].unscaled_cv_lp() + \
  51. 0.5f * channel_[ADC_CHANNEL_X_SPREAD_2].unscaled_cv_lp();
  52. }
  53. inline bool CalibrateSpreadC3() {
  54. cv_c3_[1] = 0.5f * channel_[ADC_CHANNEL_X_SPREAD].unscaled_cv_lp() + \
  55. 0.5f * channel_[ADC_CHANNEL_X_SPREAD_2].unscaled_cv_lp();
  56. for (int i = 0; i < 2; ++i) {
  57. float c3 = cv_c3_[i]; // 0.2
  58. float c1 = cv_c1_[i]; // 0.4
  59. float delta = c3 - c1;
  60. float target_scale = i == 0 ? 24.0f : 0.4f;
  61. float target_offset = i == 0 ? 12.0f : 0.2f;
  62. if (delta > -0.3f && delta < -0.1f) {
  63. int channel = i == 0 ? ADC_CHANNEL_T_RATE : ADC_CHANNEL_X_SPREAD;
  64. calibration_data_->adc_scale[channel] = target_scale / (c3 - c1);
  65. calibration_data_->adc_offset[channel] = target_offset - \
  66. calibration_data_->adc_scale[channel] * c1;
  67. } else {
  68. return false;
  69. }
  70. }
  71. calibration_data_->adc_scale[ADC_CHANNEL_X_SPREAD_2] = calibration_data_->adc_scale[ADC_CHANNEL_X_SPREAD];
  72. calibration_data_->adc_offset[ADC_CHANNEL_X_SPREAD_2] = calibration_data_->adc_offset[ADC_CHANNEL_X_SPREAD];
  73. return true;
  74. }
  75. inline void CalibrateOffsets() {
  76. for (size_t i = 0; i < ADC_CHANNEL_LAST; ++i) {
  77. if (i != ADC_CHANNEL_T_RATE && i != ADC_CHANNEL_X_SPREAD) {
  78. calibration_data_->adc_offset[i] = \
  79. 2.0f * channel_[i].unscaled_cv_lp();
  80. }
  81. }
  82. }
  83. inline uint8_t adc_value(int index) const {
  84. return adc_.value(index) >> 8;
  85. }
  86. void Copy(uint16_t* output);
  87. void Process(const uint16_t* values, float* output);
  88. inline const CvReaderChannel& channel(size_t index) {
  89. return channel_[index];
  90. }
  91. inline CvReaderChannel* mutable_channel(size_t index) {
  92. return &channel_[index];
  93. }
  94. inline void set_attenuverter(int index, float value) {
  95. attenuverter_[index] = value;
  96. }
  97. private:
  98. Adc adc_;
  99. CalibrationData* calibration_data_;
  100. float cv_c1_[2];
  101. float cv_c3_[2];
  102. CvReaderChannel channel_[ADC_CHANNEL_LAST];
  103. float attenuverter_[ADC_CHANNEL_LAST];
  104. static const CvReaderChannel::Settings channel_settings_[ADC_CHANNEL_LAST];
  105. DISALLOW_COPY_AND_ASSIGN(CvReader);
  106. };
  107. } // namespace marbles
  108. #endif // MARBLES_CV_READER_H_