refactoring

This commit is contained in:
rusefi 2020-04-08 21:23:49 -04:00
parent 2c423052c4
commit 914855d262
2 changed files with 27 additions and 18 deletions

View File

@ -213,15 +213,19 @@ static void cjUpdateAnalogValues() {
} }
void cjCalibrate(void) { void cjCalibrate(void) {
globalInstance.cjIdentify(); globalInstance.calibrate();
}
void CJ125::calibrate(void) {
cjIdentify();
scheduleMsg(logger, "cj125: Starting calibration..."); scheduleMsg(logger, "cj125: Starting calibration...");
globalInstance.cjSetMode(CJ125_MODE_CALIBRATION); cjSetMode(CJ125_MODE_CALIBRATION);
int init1 = cjReadRegister(INIT_REG1_RD); int init1 = cjReadRegister(INIT_REG1_RD);
// check if our command has been accepted // check if our command has been accepted
if (init1 != CJ125_INIT1_CALBRT) { if (init1 != CJ125_INIT1_CALBRT) {
scheduleMsg(logger, "cj125: Calibration error (init1=0x%02x)! Failed!", init1); scheduleMsg(logger, "cj125: Calibration error (init1=0x%02x)! Failed!", init1);
globalInstance.cjSetMode(CJ125_MODE_NORMAL_17); cjSetMode(CJ125_MODE_NORMAL_17);
return; return;
} }
#if EFI_PROD_CODE #if EFI_PROD_CODE
@ -229,8 +233,8 @@ void cjCalibrate(void) {
// wait for the start of the calibration // wait for the start of the calibration
chThdSleepMilliseconds(CJ125_CALIBRATION_DELAY); chThdSleepMilliseconds(CJ125_CALIBRATION_DELAY);
#endif /* EFI_PROD_CODE */ #endif /* EFI_PROD_CODE */
globalInstance.vUaCal = 0.0f; vUaCal = 0.0f;
globalInstance.vUrCal = 0.0f; vUrCal = 0.0f;
// wait for some more ADC samples // wait for some more ADC samples
for (int i = 0; i < CJ125_CALIBRATE_NUM_SAMPLES; i++) { for (int i = 0; i < CJ125_CALIBRATE_NUM_SAMPLES; i++) {
cjUpdateAnalogValues(); cjUpdateAnalogValues();
@ -242,33 +246,33 @@ void cjCalibrate(void) {
} }
#endif /* EFI_TUNER_STUDIO */ #endif /* EFI_TUNER_STUDIO */
globalInstance.vUaCal += globalInstance.vUa; vUaCal += vUa;
globalInstance.vUrCal += globalInstance.vUr; vUrCal += vUr;
} }
// find average // find average
globalInstance.vUaCal /= (float)CJ125_CALIBRATE_NUM_SAMPLES; vUaCal /= (float)CJ125_CALIBRATE_NUM_SAMPLES;
globalInstance.vUrCal /= (float)CJ125_CALIBRATE_NUM_SAMPLES; vUrCal /= (float)CJ125_CALIBRATE_NUM_SAMPLES;
// restore normal mode // restore normal mode
globalInstance.cjSetMode(CJ125_MODE_NORMAL_17); cjSetMode(CJ125_MODE_NORMAL_17);
#if EFI_PROD_CODE #if EFI_PROD_CODE
// todo: testing solution // todo: testing solution
chThdSleepMilliseconds(CJ125_CALIBRATION_DELAY); chThdSleepMilliseconds(CJ125_CALIBRATION_DELAY);
#endif #endif
// check if everything is ok // check if everything is ok
globalInstance.diag = cjReadRegister(DIAG_REG_RD); diag = cjReadRegister(DIAG_REG_RD);
cjUpdateAnalogValues(); cjUpdateAnalogValues();
cjPrintData(); cjPrintData();
// store new calibration data // store new calibration data
uint32_t storedLambda = get16bitFromVoltage(globalInstance.vUaCal); uint32_t storedLambda = get16bitFromVoltage(vUaCal);
uint32_t storedHeater = get16bitFromVoltage(globalInstance.vUrCal); uint32_t storedHeater = get16bitFromVoltage(vUrCal);
scheduleMsg(logger, "cj125: Done! Saving calibration data (%d %d).", storedLambda, storedHeater); scheduleMsg(logger, "cj125: Done! Saving calibration data (%d %d).", storedLambda, storedHeater);
#if EFI_PROD_CODE #if EFI_PROD_CODE
backupRamSave(BACKUP_CJ125_CALIBRATION_LAMBDA, storedLambda); backupRamSave(BACKUP_CJ125_CALIBRATION_LAMBDA, storedLambda);
backupRamSave(BACKUP_CJ125_CALIBRATION_HEATER, storedHeater); backupRamSave(BACKUP_CJ125_CALIBRATION_HEATER, storedHeater);
#endif /* EFI_PROD_CODE */ #endif /* EFI_PROD_CODE */
globalInstance.state = CJ125_IDLE; state = CJ125_IDLE;
} }
static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) { static void cjStart(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
@ -289,7 +293,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) {
cjCalibrate(); globalInstance.calibrate();
} 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);
@ -373,7 +377,7 @@ static bool cj125periodic(CJ125 *instance DECLARE_ENGINE_PARAMETER_SUFFIX) {
} }
if (instance->state == CJ125_CALIBRATION) { if (instance->state == CJ125_CALIBRATION) {
cjCalibrate(); globalInstance.calibrate();
// 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

@ -54,8 +54,8 @@ public:
class CJ125 { class CJ125 {
public: public:
CJ125(); CJ125();
Cj125SpiStream *spi = NULL; Cj125SpiStream *spi = nullptr;
Logging *logger = NULL; Logging *logger = nullptr;
SimplePwm wboHeaterControl; SimplePwm wboHeaterControl;
@ -77,11 +77,15 @@ public:
volatile float lambda = 1.0f; volatile float lambda = 1.0f;
// Current values // Current values
// lambda
volatile float vUa = 0.0f; volatile float vUa = 0.0f;
// heater
volatile float vUr = 0.0f; volatile float vUr = 0.0f;
// Calibration values // Calibration values
// lambda
volatile float vUaCal = 0.0f; volatile float vUaCal = 0.0f;
// header
volatile float vUrCal = 0.0f; volatile float vUrCal = 0.0f;
OutputPin wboHeaterPin; OutputPin wboHeaterPin;
@ -98,6 +102,7 @@ public:
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(void);
void calibrate();
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);