Major updates of software structure
- new classes ChargeController and AnalogValue introduced - BuckConverter renamed back to HalfBridge - charging profile settings via struct
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/* mbed library for ADC readings
|
||||
* Copyright (c) 2017 Martin Jäger (www.libre.solar)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "AnalogValue.h"
|
||||
|
||||
float AnalogValue::_vcc = 3.3;
|
||||
|
||||
AnalogValue::AnalogValue(PinName pin, float multiplier, const int num_readings, float filter_constant) :
|
||||
_adc(pin), _num_readings(num_readings)
|
||||
{
|
||||
_multiplier = multiplier;
|
||||
update(); // initialize value without filter
|
||||
_filter_constant = filter_constant;
|
||||
}
|
||||
|
||||
void AnalogValue::update()
|
||||
{
|
||||
float tmp = adc_read_avg(_adc, _num_readings) * _vcc * _multiplier;
|
||||
_value = (1.0 - _filter_constant) * tmp + _filter_constant * _value;
|
||||
}
|
||||
|
||||
float AnalogValue::adc_read_avg(AnalogIn& input, int num_readings)
|
||||
{
|
||||
// averaging: use read_u16 for faster calculation
|
||||
unsigned int sum_adc_readings = 0;
|
||||
for (int i = 0; i < num_readings; i++) {
|
||||
sum_adc_readings += input.read_u16(); // normalized to 0xFFFF
|
||||
}
|
||||
return (float) (sum_adc_readings / num_readings) / (float)0xFFFF;
|
||||
}
|
||||
|
||||
float AnalogValue::read()
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
|
||||
void AnalogValue::update_reference_voltage(AnalogIn& adc_ref, float ref_voltage, int num_readings)
|
||||
{
|
||||
_vcc = ref_voltage / adc_read_avg(adc_ref, num_readings);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/* mbed library for ADC readings
|
||||
* Copyright (c) 2017 Martin Jäger (www.libre.solar)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
class AnalogValue
|
||||
{
|
||||
public:
|
||||
|
||||
/** Create an AnalogValue object
|
||||
*
|
||||
* @param pin ADC input pin
|
||||
* @param multiplier Factor to calculate desired output (e.g. for voltage dividers, gain, etc.)
|
||||
* @param num_readings Number of ADC readings to be averaged
|
||||
* @param filter_constant Low pass filter constant (0.0 to 1.0, 0.0 for no filtering)
|
||||
*/
|
||||
AnalogValue(PinName pin, float multiplier, const int num_readings = 8, float filter_constant = 0.0);
|
||||
|
||||
/** Updates ADC reading and applies low-pass filter. Should be called with a regular time interval
|
||||
*/
|
||||
void update();
|
||||
|
||||
/** Charger state machine update, should be called exactly once per second
|
||||
*
|
||||
* @param adc_ref AnalogIn for the pin with the reference voltage attached
|
||||
* @param ref_voltage Rated reference voltage
|
||||
* @param num_readings Number of ADC readings to be averaged
|
||||
*/
|
||||
static void update_reference_voltage(AnalogIn& adc_ref, float ref_voltage, int num_readings = 8);
|
||||
|
||||
/** Read the last updated (and filtered) ADC result (incl. multiplier)
|
||||
*
|
||||
* @returns
|
||||
* Saved ADC result with multiplier applied
|
||||
*/
|
||||
float read();
|
||||
|
||||
/** An operator shorthand for read()
|
||||
*
|
||||
* The float() operator can be used as a shorthand for read() to simplify common code sequences
|
||||
*
|
||||
* Example:
|
||||
* @code
|
||||
* float x = volume.read();
|
||||
* float x = volume;
|
||||
*
|
||||
* if(volume.read() > 0.25) { ... }
|
||||
* if(volume > 0.25) { ... }
|
||||
* @endcode
|
||||
*/
|
||||
operator float() {
|
||||
return read();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/** Read out ADC results and apply averaging and/or low pass filter
|
||||
*
|
||||
* @param input AnalogIn for the pin to be read
|
||||
* @param num_readings Number of ADC readings to be averaged
|
||||
*
|
||||
* @returns
|
||||
* Averaged and filtered ADC result
|
||||
*/
|
||||
static float adc_read_avg(AnalogIn& input, int num_readings);
|
||||
|
||||
static float _vcc; // actual VCC of ADC
|
||||
|
||||
AnalogIn _adc;
|
||||
float _multiplier;
|
||||
float _value;
|
||||
|
||||
const int _num_readings;
|
||||
float _filter_constant;
|
||||
};
|
||||
Reference in New Issue
Block a user