| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "battor_sample_converter.h" | 5 #include "battor_sample_converter.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 namespace battor { | 9 namespace battor { |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 baseline_current_ += ToUnitfulVoltage(sample.current_raw); | 44 baseline_current_ += ToUnitfulVoltage(sample.current_raw); |
| 45 baseline_voltage_ += ToUnitfulVoltage(sample.voltage_raw); | 45 baseline_voltage_ += ToUnitfulVoltage(sample.voltage_raw); |
| 46 } | 46 } |
| 47 | 47 |
| 48 baseline_current_ /= calibration_frame.size(); | 48 baseline_current_ /= calibration_frame.size(); |
| 49 baseline_voltage_ /= calibration_frame.size(); | 49 baseline_voltage_ /= calibration_frame.size(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 BattOrSampleConverter::~BattOrSampleConverter() {} | 52 BattOrSampleConverter::~BattOrSampleConverter() {} |
| 53 | 53 |
| 54 BattOrSample BattOrSampleConverter::ToSample( | 54 BattOrSample BattOrSampleConverter::ToSample(const RawBattOrSample& sample, |
| 55 const RawBattOrSample& sample) const { | 55 size_t sample_number) const { |
| 56 // Subtract out the baseline current and voltage that the BattOr reads even | 56 // Subtract out the baseline current and voltage that the BattOr reads even |
| 57 // when it's not attached to anything. | 57 // when it's not attached to anything. |
| 58 double current = ToUnitfulVoltage(sample.current_raw) - baseline_current_; | 58 double current = ToUnitfulVoltage(sample.current_raw) - baseline_current_; |
| 59 double voltage = ToUnitfulVoltage(sample.voltage_raw) - baseline_voltage_; | 59 double voltage = ToUnitfulVoltage(sample.voltage_raw) - baseline_voltage_; |
| 60 | 60 |
| 61 // The BattOr has to amplify the voltage so that it's on a similar scale as | 61 // The BattOr has to amplify the voltage so that it's on a similar scale as |
| 62 // the reference voltage. This is done in the circuit using resistors (with | 62 // the reference voltage. This is done in the circuit using resistors (with |
| 63 // known resistances r2 and r3). Here we undo that amplification. | 63 // known resistances r2 and r3). Here we undo that amplification. |
| 64 double voltage_divider = eeprom_.r3 / (eeprom_.r2 + eeprom_.r3); | 64 double voltage_divider = eeprom_.r3 / (eeprom_.r2 + eeprom_.r3); |
| 65 voltage /= voltage_divider; | 65 voltage /= voltage_divider; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 79 // we can get the current by dividing this voltage by the resistance. | 79 // we can get the current by dividing this voltage by the resistance. |
| 80 current /= eeprom_.r1; | 80 current /= eeprom_.r1; |
| 81 | 81 |
| 82 // Convert to milliamps. | 82 // Convert to milliamps. |
| 83 current *= 1000; | 83 current *= 1000; |
| 84 | 84 |
| 85 // Each BattOr is individually factory-calibrated. Apply these calibrations. | 85 // Each BattOr is individually factory-calibrated. Apply these calibrations. |
| 86 current -= eeprom_.low_gain_correction_offset; | 86 current -= eeprom_.low_gain_correction_offset; |
| 87 current /= eeprom_.low_gain_correction_factor; | 87 current /= eeprom_.low_gain_correction_factor; |
| 88 | 88 |
| 89 return BattOrSample{voltage, current}; | 89 double time_ms = double(sample_number) / eeprom_.sd_sample_rate * 1000; |
| 90 |
| 91 return BattOrSample{time_ms, voltage, current}; |
| 90 } | 92 } |
| 91 | 93 |
| 92 } // namespace battor | 94 } // namespace battor |
| OLD | NEW |