Skip to content

Commit

Permalink
[#145] fix: Dis-/charged Energy is NOT Charge*Voltage
Browse files Browse the repository at this point in the history
  • Loading branch information
johslarsen committed Jan 25, 2016
1 parent dc28641 commit 3a2a162
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
66 changes: 47 additions & 19 deletions src/core/AnalogInputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "atomic.h"
#include "Balancer.h"

#define ANALOG_INPUTS_E_OUT_dt_FACTOR 50
#define ANALOG_INPUTS_E_OUT_DIVIDER 100

#define ANALOG_INPUTS_ADC_MEASUREMENTS_COUNT (ANALOG_INPUTS_ADC_ROUND_MAX_COUNT*ANALOG_INPUTS_ADC_BURST_COUNT)

Expand Down Expand Up @@ -72,6 +74,8 @@ namespace AnalogInputs {
bool enable_deltaVoutMax_;

uint32_t i_charge_;
uint32_t i_Eout_;
uint8_t i_Eout_dt_;

void _resetAvr();
void _resetDeltaAvr();
Expand Down Expand Up @@ -243,6 +247,8 @@ void AnalogInputs::resetAccumulatedMeasurements()
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
i_charge_ = 0;
i_Eout_ = 0;
i_Eout_dt_ = ANALOG_INPUTS_E_OUT_dt_FACTOR;
}
resetMeasurement();
resetDelta();
Expand Down Expand Up @@ -360,26 +366,54 @@ void AnalogInputs::printRealValue(Name name, uint8_t dig)
lcdPrintAnalog(x, dig, t);
}

uint16_t AnalogInputs::getCharge()
static inline uint32_t toHoursBasis(uint32_t accumulator) {
uint32_t retu = accumulator;
retu /= 1000000/TIMER_INTERRUPT_PERIOD_MICROSECONDS
* 3600/TIMER_SLOW_INTERRUPT_INTERVAL;
return retu;
}

AnalogInputs::ValueType AnalogInputs::getCharge()
{
//check units
STATIC_ASSERT(ANALOG_AMP(1.0) == ANALOG_CHARGE(1.0));

uint32_t retu;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
retu = i_charge_;
}
#if TIMER_INTERRUPT_PERIOD_MICROSECONDS == 500
retu /= 1000000/TIMER_INTERRUPT_PERIOD_MICROSECONDS/16;
retu /= 3600/TIMER_SLOW_INTERRUPT_INTERVAL*16;
#else
#warning "TIMER_INTERRUPT_PERIOD_MICROSECONDS != 500"
retu /= 1000000/TIMER_INTERRUPT_PERIOD_MICROSECONDS;
retu /= 3600/TIMER_SLOW_INTERRUPT_INTERVAL;
#endif
return retu;
return toHoursBasis(retu);
}


AnalogInputs::ValueType AnalogInputs::getEout()
{
uint32_t retu;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
retu = i_Eout_;
}

//check units
STATIC_ASSERT(uint32_t(ANALOG_AMP(1.0))*ANALOG_VOLT(1.0)
/ (ANALOG_INPUTS_E_OUT_DIVIDER*ANALOG_INPUTS_E_OUT_dt_FACTOR)
== 2 * ANALOG_WATTH(1.0));
retu /= 2;

return toHoursBasis(retu);
}

void AnalogInputs::doSlowInterrupt()
{
i_charge_ += getIout();

if(--i_Eout_dt_ == 0) {
i_Eout_dt_ = ANALOG_INPUTS_E_OUT_dt_FACTOR;

uint32_t P = getIout();
P *= getVout();
uint32_t E_since_previous_measurement = P / ANALOG_INPUTS_E_OUT_DIVIDER;
i_Eout_ += E_since_previous_measurement;
}
}

// finalize Measurement
Expand Down Expand Up @@ -540,26 +574,20 @@ void AnalogInputs::finalizeFullVirtualMeasurement()
setReal(VobInfo, obInfo);

AnalogInputs::ValueType IoutValue = 0;
AnalogInputs::ValueType CoutValue = getCharge();
if(Discharger::isPowerOn()) {
IoutValue = getRealValue(Idischarge);
} else if (SMPS::isPowerOn()) {
IoutValue = getRealValue(Ismps);
}

setReal(Iout, IoutValue);
setReal(Cout, CoutValue);

uint32_t P = IoutValue;
P *= out;
P /= 10000;
setReal(Pout, P);

//TODO: rewrite
uint32_t E = CoutValue;
E *= out;
E /= 10000;
setReal(Eout, E);
setReal(Iout, IoutValue);
setReal(Cout, getCharge());
setReal(Eout, getEout());
}

void AnalogInputs::setReal(Name name, ValueType real)
Expand Down
3 changes: 2 additions & 1 deletion src/core/AnalogInputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ namespace AnalogInputs {
ValueType getIout();
ValueType getDeltaLastT();
ValueType getDeltaCount();
uint16_t getCharge();
ValueType getCharge();
ValueType getEout();
void enableDeltaVoutMax(bool enable);

uint16_t getConnectedBalancePorts();
Expand Down

0 comments on commit 3a2a162

Please sign in to comment.