This commit is contained in:
Matthew Kennedy 2021-02-23 11:57:14 -10:00 committed by GitHub
parent f8437c80b5
commit 350a047ead
4 changed files with 14 additions and 43 deletions

View File

@ -95,21 +95,11 @@ LECalculator::LECalculator() {
} }
void LECalculator::reset() { void LECalculator::reset() {
m_program = nullptr;
stack.reset(); stack.reset();
currentCalculationLogPosition = 0; currentCalculationLogPosition = 0;
memset(calcLogAction, 0, sizeof(calcLogAction)); memset(calcLogAction, 0, sizeof(calcLogAction));
} }
void LECalculator::reset(LEElement *element) {
reset();
setProgram(element);
}
void LECalculator::setProgram(LEElement* program) {
m_program = program;
}
bool float2bool(float v) { bool float2bool(float v) {
return v != 0; return v != 0;
} }
@ -269,24 +259,13 @@ FsioResult LECalculator::processElement(const LEElement *element DECLARE_ENGINE_
} }
} }
float LECalculator::getValue2(float selfValue, LEElement *fistElementInList DECLARE_ENGINE_PARAMETER_SUFFIX) { float LECalculator::evaluate(float selfValue, const LEElement* element DECLARE_ENGINE_PARAMETER_SUFFIX) {
reset(fistElementInList); if (!element) {
return getValue(selfValue PASS_ENGINE_PARAMETER_SUFFIX);
}
bool LECalculator::isEmpty() const {
return !m_program;
}
float LECalculator::getValue(float selfValue DECLARE_ENGINE_PARAMETER_SUFFIX) {
if (isEmpty()) {
warning(CUSTOM_NO_FSIO, "no FSIO code"); warning(CUSTOM_NO_FSIO, "no FSIO code");
return NAN; return NAN;
} }
const LEElement* element = m_program; reset();
stack.reset();
int counter = 0; int counter = 0;

View File

@ -130,12 +130,8 @@ typedef FLStack<float, MAX_STACK_DEPTH> calc_stack_t;
class LECalculator { class LECalculator {
public: public:
LECalculator(); LECalculator();
float getValue(float selfValue DECLARE_ENGINE_PARAMETER_SUFFIX); float evaluate(float selfValue, const LEElement* element DECLARE_ENGINE_PARAMETER_SUFFIX);
float getValue2(float selfValue, LEElement *fistElementInList DECLARE_ENGINE_PARAMETER_SUFFIX);
bool isEmpty() const;
void reset(); void reset();
void reset(LEElement *element);
// Log history of calculation actions for debugging // Log history of calculation actions for debugging
le_action_e calcLogAction[MAX_CALC_LOG]; le_action_e calcLogAction[MAX_CALC_LOG];
@ -143,14 +139,10 @@ public:
int currentCalculationLogPosition; int currentCalculationLogPosition;
private: private:
void setProgram(LEElement* program);
void push(le_action_e action, float value); void push(le_action_e action, float value);
FsioResult processElement(const LEElement* element DECLARE_ENGINE_PARAMETER_SUFFIX); FsioResult processElement(const LEElement* element DECLARE_ENGINE_PARAMETER_SUFFIX);
float pop(le_action_e action); float pop(le_action_e action);
LEElement* m_program = nullptr;
calc_stack_t stack; calc_stack_t stack;
}; };

View File

@ -337,7 +337,7 @@ float getFsioOutputValue(int index DECLARE_ENGINE_PARAMETER_SUFFIX) {
warning(CUSTOM_NO_FSIO, "no FSIO for #%d %s", index + 1, hwPortname(CONFIG(fsioOutputPins)[index])); warning(CUSTOM_NO_FSIO, "no FSIO for #%d %s", index + 1, hwPortname(CONFIG(fsioOutputPins)[index]));
return NAN; return NAN;
} else { } else {
return calc.getValue2(engine->fsioState.fsioLastValue[index], state.fsioLogics[index] PASS_ENGINE_PARAMETER_SUFFIX); return calc.evaluate(engine->fsioState.fsioLastValue[index], state.fsioLogics[index] PASS_ENGINE_PARAMETER_SUFFIX);
} }
} }
@ -404,7 +404,7 @@ static void setPinState(const char * msg, OutputPin *pin, LEElement *element DEC
if (!element) { if (!element) {
warning(CUSTOM_FSIO_INVALID_EXPRESSION, "invalid expression for %s", msg); warning(CUSTOM_FSIO_INVALID_EXPRESSION, "invalid expression for %s", msg);
} else { } else {
int value = (int)calc.getValue2(pin->getLogicValue(), element PASS_ENGINE_PARAMETER_SUFFIX); int value = (int)calc.evaluate(pin->getLogicValue(), element PASS_ENGINE_PARAMETER_SUFFIX);
if (pin->isInitialized() && value != pin->getLogicValue()) { if (pin->isInitialized() && value != pin->getLogicValue()) {
for (int i = 0;i < calc.currentCalculationLogPosition;i++) { for (int i = 0;i < calc.currentCalculationLogPosition;i++) {
@ -446,7 +446,7 @@ static bool updateValueOrWarning(int humanIndex, const char *msg, float *value D
return false; return false;
} else { } else {
float beforeValue = *value; float beforeValue = *value;
*value = calc.getValue2(beforeValue, element PASS_ENGINE_PARAMETER_SUFFIX); *value = calc.evaluate(beforeValue, element PASS_ENGINE_PARAMETER_SUFFIX);
// floating '==' comparison without EPS seems fine here // floating '==' comparison without EPS seems fine here
return (beforeValue != *value); return (beforeValue != *value);
} }
@ -666,7 +666,7 @@ static void rpnEval(char *line) {
if (e == NULL) { if (e == NULL) {
scheduleMsg(logger, "parsing failed"); scheduleMsg(logger, "parsing failed");
} else { } else {
float result = evalCalc.getValue2(0, e PASS_ENGINE_PARAMETER_SUFFIX); float result = evalCalc.evaluate(0, e PASS_ENGINE_PARAMETER_SUFFIX);
scheduleMsg(logger, "Evaluate result: %.2f", result); scheduleMsg(logger, "Evaluate result: %.2f", result);
} }
#endif #endif

View File

@ -117,7 +117,7 @@ static void testExpression2(float selfValue, const char *line, float expected, E
EXPAND_Engine; EXPAND_Engine;
ASSERT_NEAR(expected, c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX), EPS4D) << line; ASSERT_NEAR(expected, c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX), EPS4D) << line;
} }
static void testExpression2(float selfValue, const char *line, float expected, const std::unordered_map<SensorType, float>& sensorVals = {}) { static void testExpression2(float selfValue, const char *line, float expected, const std::unordered_map<SensorType, float>& sensorVals = {}) {
@ -144,20 +144,20 @@ TEST(fsio, testHysteresisSelf) {
double selfValue = 0; double selfValue = 0;
engine->fsioState.mockRpm = 0; engine->fsioState.mockRpm = 0;
selfValue = c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); selfValue = c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX);
ASSERT_EQ(0, selfValue); ASSERT_EQ(0, selfValue);
engine->fsioState.mockRpm = 430; engine->fsioState.mockRpm = 430;
selfValue = c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); selfValue = c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX);
// OFF since not ON yet // OFF since not ON yet
ASSERT_EQ(0, selfValue); ASSERT_EQ(0, selfValue);
engine->fsioState.mockRpm = 460; engine->fsioState.mockRpm = 460;
selfValue = c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); selfValue = c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX);
ASSERT_EQ(1, selfValue); ASSERT_EQ(1, selfValue);
engine->fsioState.mockRpm = 430; engine->fsioState.mockRpm = 430;
selfValue = c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); selfValue = c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX);
// OFF since was ON yet // OFF since was ON yet
ASSERT_EQ(1, selfValue); ASSERT_EQ(1, selfValue);
} }
@ -241,7 +241,7 @@ TEST(fsio, testLogicExpressions) {
LEElement * element = pool.parseExpression("fan NOT coolant 90 > AND fan coolant 85 > AND OR"); LEElement * element = pool.parseExpression("fan NOT coolant 90 > AND fan coolant 85 > AND OR");
ASSERT_TRUE(element != NULL) << "Not NULL expected"; ASSERT_TRUE(element != NULL) << "Not NULL expected";
LECalculator c; LECalculator c;
ASSERT_EQ( 1, c.getValue2(0, element PASS_ENGINE_PARAMETER_SUFFIX)) << "that expression"; ASSERT_EQ( 1, c.evaluate(0, element PASS_ENGINE_PARAMETER_SUFFIX)) << "that expression";
ASSERT_EQ(12, c.currentCalculationLogPosition); ASSERT_EQ(12, c.currentCalculationLogPosition);
ASSERT_EQ(102, c.calcLogAction[0]); ASSERT_EQ(102, c.calcLogAction[0]);