better state messages

This commit is contained in:
rusefi 2020-05-01 18:52:06 -04:00
parent 8be6bd5c97
commit c39662b5c6
4 changed files with 95 additions and 55 deletions

View File

@ -170,31 +170,27 @@ static uint32_t get16bitFromVoltage(float v) {
return (uint32_t)(v * CJ125_VOLTAGE_TO_16BIT_FACTOR); return (uint32_t)(v * CJ125_VOLTAGE_TO_16BIT_FACTOR);
} }
static void cjPrintState() { static const char * getCjState(cj125_state_e stateCode) {
scheduleMsg(logger, "cj125: state=%d diag=0x%x (vUa=%.3f vUr=%.3f) (vUaCal=%.3f vUrCal=%.3f)", switch (stateCode) {
globalInstance.state, globalInstance.diag, case CJ125_INIT:
globalInstance.vUa, globalInstance.vUr, return "INIT";
globalInstance.vUaCal, globalInstance.vUrCal); case CJ125_IDLE:
return "IDLE";
scheduleMsg(logger, "cj125 P=%f I=%f D=%f", case CJ125_CALIBRATION:
globalInstance.heaterPidConfig.pFactor, return "CALIBRATION";
globalInstance.heaterPidConfig.iFactor, case CJ125_PREHEAT:
globalInstance.heaterPidConfig.dFactor); return "PREHEAT";
} case CJ125_HEAT_UP:
return "HEAT UP";
static void cjInfo() { case CJ125_READY:
cjPrintState(); return "READY";
#if HAL_USE_SPI case CJ125_OVERHEAT:
printSpiConfig(logger, "cj125", CONFIG(cj125SpiDevice)); return "OVERHEAT";
#endif /* HAL_USE_SPI */ case CJ125_ERROR:
} return "ERROR";
default:
static void cjPrintData() { return "UNKNOWN";
#if ! EFI_UNIT_TEST
if (engineConfiguration->isCJ125Verbose) {
cjPrintState();
} }
#endif /* EFI_UNIT_TEST */
} }
static void cjPrintErrorCode(cj125_error_e errCode) { static void cjPrintErrorCode(cj125_error_e errCode) {
@ -222,6 +218,39 @@ static void cjPrintErrorCode(cj125_error_e errCode) {
scheduleMsg(logger, "cj125 ERROR: %s.", errString); scheduleMsg(logger, "cj125 ERROR: %s.", errString);
} }
static void cjPrintState() {
scheduleMsg(logger, "cj125: state=%s %s diag=0x%x (vUa=%.3f vUr=%.3f) (vUaCal=%.3f vUrCal=%.3f)",
getCjState(globalInstance.state), globalInstance.diag,
globalInstance.vUa, globalInstance.vUr,
globalInstance.vUaCal, globalInstance.vUrCal);
globalInstance.printDiag();
if (globalInstance.state == CJ125_ERROR) {
cjPrintErrorCode(globalInstance.errorCode);
}
scheduleMsg(logger, "cj125 P=%f I=%f D=%f",
globalInstance.heaterPidConfig.pFactor,
globalInstance.heaterPidConfig.iFactor,
globalInstance.heaterPidConfig.dFactor);
}
static void cjInfo() {
cjPrintState();
#if HAL_USE_SPI
printSpiConfig(logger, "cj125", CONFIG(cj125SpiDevice));
#endif /* HAL_USE_SPI */
}
static void cjPrintData() {
#if ! EFI_UNIT_TEST
if (engineConfiguration->isCJ125Verbose) {
cjPrintState();
}
#endif /* EFI_UNIT_TEST */
}
class RealSpi : public Cj125SpiStream { class RealSpi : public Cj125SpiStream {
public: public:
uint8_t ReadRegister(uint8_t reg) override { uint8_t ReadRegister(uint8_t reg) override {
@ -249,12 +278,14 @@ static void cjUpdateAnalogValues() {
#endif /* EFI_PROD_CODE */ #endif /* EFI_PROD_CODE */
} }
#if ! EFI_UNIT_TEST
void cjCalibrate(void) { void cjCalibrate(void) {
globalInstance.calibrate(); globalInstance.calibrate(PASS_ENGINE_PARAMETER_SIGNATURE);
} }
#endif /* EFI_UNIT_TEST */
void CJ125::calibrate(void) { void CJ125::calibrate(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
cjIdentify(); cjIdentify(PASS_ENGINE_PARAMETER_SIGNATURE);
scheduleMsg(logger, "cj125: Starting calibration..."); scheduleMsg(logger, "cj125: Starting calibration...");
cjSetMode(CJ125_MODE_CALIBRATION); cjSetMode(CJ125_MODE_CALIBRATION);
@ -318,7 +349,7 @@ static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
return; return;
} }
globalInstance.cjIdentify(); globalInstance.cjIdentify(PASS_ENGINE_PARAMETER_SIGNATURE);
// Load calibration values // Load calibration values
#if EFI_PROD_CODE #if EFI_PROD_CODE
@ -330,7 +361,7 @@ static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
#endif #endif
// if no calibration, try to calibrate now and store new values // if no calibration, try to calibrate now and store new values
if (storedLambda == 0 || storedHeater == 0) { if (storedLambda == 0 || storedHeater == 0) {
globalInstance.calibrate(); globalInstance.calibrate(PASS_ENGINE_PARAMETER_SIGNATURE);
} else { } else {
scheduleMsg(logger, "cj125: Loading stored calibration data (%d %d)", storedLambda, storedHeater); scheduleMsg(logger, "cj125: Loading stored calibration data (%d %d)", storedLambda, storedHeater);
globalInstance.vUaCal = getVoltageFrom16bit(storedLambda); globalInstance.vUaCal = getVoltageFrom16bit(storedLambda);
@ -395,7 +426,7 @@ static bool cj125periodic(CJ125 *instance DECLARE_ENGINE_PARAMETER_SUFFIX) {
} }
if (instance->state == CJ125_CALIBRATION) { if (instance->state == CJ125_CALIBRATION) {
globalInstance.calibrate(); globalInstance.calibrate(PASS_ENGINE_PARAMETER_SIGNATURE);
// Start normal operation // Start normal operation
instance->state = CJ125_INIT; instance->state = CJ125_INIT;
globalInstance.cjSetMode(CJ125_MODE_NORMAL_17); globalInstance.cjSetMode(CJ125_MODE_NORMAL_17);

View File

@ -61,6 +61,21 @@ static void printDiagCode(Logging * logging, const char *msg, int code, const ch
case 2: case 2:
scheduleMsg(logging, "%s Short to Vbatt", msg); scheduleMsg(logging, "%s Short to Vbatt", msg);
return; return;
case 3:
scheduleMsg(logging, "%s LOOKS GOOD", msg);
return;
}
}
void CJ125::printDiag() {
if (diag == CJ125_DIAG_NORM) {
scheduleMsg(logger, "cj125: diag Looks great!");
} else {
scheduleMsg(logger, "cj125: diag NOT GOOD");
printDiagCode(logger, "VM", diag, LOW_VOLTAGE);
printDiagCode(logger, "UN", diag >> 2, LOW_VOLTAGE);
printDiagCode(logger, "IA", diag >> 4, LOW_VOLTAGE);
printDiagCode(logger, "HR", diag >> 6, "open load");
} }
} }
@ -68,7 +83,7 @@ static void printDiagCode(Logging * logging, const char *msg, int code, const ch
* @return true in case of positive SPI identification * @return true in case of positive SPI identification
* false in case of unexpected SPI response * false in case of unexpected SPI response
*/ */
bool CJ125::cjIdentify(void) { bool CJ125::cjIdentify(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
efiAssert(OBD_PCM_Processor_Fault, spi!= NULL, "No SPI pointer", false); efiAssert(OBD_PCM_Processor_Fault, spi!= NULL, "No SPI pointer", false);
// read Ident register // read Ident register
int ident = spi->ReadRegister(IDENT_REG_RD) & CJ125_IDENT_MASK; int ident = spi->ReadRegister(IDENT_REG_RD) & CJ125_IDENT_MASK;
@ -84,24 +99,15 @@ bool CJ125::cjIdentify(void) {
scheduleMsg(logger, "cj125: Check ident=0x%x diag=0x%x init1=0x%x init2=0x%x", ident, diag, init1, init2); scheduleMsg(logger, "cj125: Check ident=0x%x diag=0x%x init1=0x%x init2=0x%x", ident, diag, init1, init2);
if (ident != CJ125_IDENT) { if (ident != CJ125_IDENT) {
scheduleMsg(logger, "cj125: Error! Wrong ident! Cannot communicate with CJ125!"); scheduleMsg(logger, "cj125: Error! Wrong ident! Cannot communicate with CJ125!");
errorCode = CJ125_ERROR_WRONG_IDENT; setError(CJ125_ERROR_WRONG_IDENT PASS_ENGINE_PARAMETER_SUFFIX);
state = CJ125_ERROR;
return false; return false;
} }
if (init1 != CJ125_INIT1_NORMAL_17 || init2 != CJ125_INIT2_DIAG) { if (init1 != CJ125_INIT1_NORMAL_17 || init2 != CJ125_INIT2_DIAG) {
scheduleMsg(logger, "cj125: Error! Cannot set init registers! Cannot communicate with CJ125!"); scheduleMsg(logger, "cj125: Error! Cannot set init registers! Cannot communicate with CJ125!");
errorCode = CJ125_ERROR_WRONG_INIT; setError(CJ125_ERROR_WRONG_IDENT PASS_ENGINE_PARAMETER_SUFFIX);
state = CJ125_ERROR;
return false; return false;
} }
if (diag == CJ125_DIAG_NORM) { printDiag();
scheduleMsg(logger, "cj125: Looks great!");
} else {
printDiagCode(logger, "VM", diag, LOW_VOLTAGE);
printDiagCode(logger, "UN", diag >> 2, LOW_VOLTAGE);
printDiagCode(logger, "IA", diag >> 4, LOW_VOLTAGE);
printDiagCode(logger, "HR", diag >> 6, "open load");
}
return true; return true;
} }

View File

@ -18,14 +18,14 @@ typedef enum {
} cj125_sensor_type_e; } cj125_sensor_type_e;
typedef enum { typedef enum {
CJ125_INIT, CJ125_INIT = 0,
CJ125_IDLE, CJ125_IDLE = 1,
CJ125_CALIBRATION, CJ125_CALIBRATION = 2,
CJ125_PREHEAT, CJ125_PREHEAT = 3,
CJ125_HEAT_UP, CJ125_HEAT_UP = 4,
CJ125_READY, CJ125_READY = 5,
CJ125_OVERHEAT, CJ125_OVERHEAT = 6,
CJ125_ERROR, CJ125_ERROR = 7,
} cj125_state_e; } cj125_state_e;
@ -101,8 +101,9 @@ public:
void SetHeater(float value DECLARE_ENGINE_PARAMETER_SUFFIX); void SetHeater(float value DECLARE_ENGINE_PARAMETER_SUFFIX);
void SetIdleHeater(DECLARE_ENGINE_PARAMETER_SIGNATURE); void SetIdleHeater(DECLARE_ENGINE_PARAMETER_SIGNATURE);
void StartHeaterControl(pwm_gen_callback *stateChangeCallback DECLARE_ENGINE_PARAMETER_SUFFIX); void StartHeaterControl(pwm_gen_callback *stateChangeCallback DECLARE_ENGINE_PARAMETER_SUFFIX);
bool cjIdentify(void); bool cjIdentify(DECLARE_ENGINE_PARAMETER_SIGNATURE);
void calibrate(); void printDiag();
void calibrate(DECLARE_ENGINE_PARAMETER_SIGNATURE);
void cjSetMode(cj125_mode_e m); void cjSetMode(cj125_mode_e m);
bool isValidState() const; bool isValidState() const;
void cjInitPid(DECLARE_ENGINE_PARAMETER_SIGNATURE); void cjInitPid(DECLARE_ENGINE_PARAMETER_SIGNATURE);

View File

@ -39,7 +39,7 @@ TEST(testCJ125, testInitialState) {
EXPECT_CALL(mock, ReadRegister(INIT_REG2_RD)).Times(1).WillOnce(Return(CJ125_INIT2_DIAG)); EXPECT_CALL(mock, ReadRegister(INIT_REG2_RD)).Times(1).WillOnce(Return(CJ125_INIT2_DIAG));
EXPECT_CALL(mock, ReadRegister(DIAG_REG_RD)).Times(1); EXPECT_CALL(mock, ReadRegister(DIAG_REG_RD)).Times(1);
cj.cjIdentify(); cj.cjIdentify(PASS_ENGINE_PARAMETER_SIGNATURE);
// validate that error state was not set // validate that error state was not set
ASSERT_EQ(cj.state, CJ125_INIT); ASSERT_EQ(cj.state, CJ125_INIT);
} }
@ -53,7 +53,9 @@ TEST(testCJ125, testFailedIdentify) {
TestSpi mock; TestSpi mock;
cj.spi = &mock; cj.spi = &mock;
cj.cjIdentify(); WITH_ENGINE_TEST_HELPER(FORD_ASPIRE_1996);
cj.cjIdentify(PASS_ENGINE_PARAMETER_SIGNATURE);
ASSERT_EQ(cj.errorCode, CJ125_ERROR_WRONG_IDENT); ASSERT_EQ(cj.errorCode, CJ125_ERROR_WRONG_IDENT);
ASSERT_EQ(cj.state, CJ125_ERROR); ASSERT_EQ(cj.state, CJ125_ERROR);
} }