2020-04-26 11:06:28 -07:00
|
|
|
|
2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
2020-04-26 11:06:28 -07:00
|
|
|
#include "gppwm_channel.h"
|
|
|
|
|
|
|
|
#include "table_helper.h"
|
|
|
|
#include "expected.h"
|
|
|
|
|
|
|
|
expected<float> readGppwmChannel(gppwm_channel_e channel DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
|
|
|
switch (channel) {
|
|
|
|
case GPPWM_Tps:
|
|
|
|
return Sensor::get(SensorType::Tps1);
|
2021-03-29 05:57:37 -07:00
|
|
|
case GPPWM_Map:
|
2020-12-30 05:43:49 -08:00
|
|
|
return Sensor::get(SensorType::Map);
|
2020-04-26 11:06:28 -07:00
|
|
|
case GPPWM_Clt:
|
|
|
|
return Sensor::get(SensorType::Clt);
|
|
|
|
case GPPWM_Iat:
|
|
|
|
return Sensor::get(SensorType::Iat);
|
2020-07-23 02:51:08 -07:00
|
|
|
case GPPWM_FuelLoad:
|
|
|
|
return getFuelingLoad(PASS_ENGINE_PARAMETER_SIGNATURE);
|
|
|
|
case GPPWM_IgnLoad:
|
|
|
|
return getIgnitionLoad(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2021-03-29 05:57:37 -07:00
|
|
|
case GPPWM_AuxTemp1:
|
|
|
|
return Sensor::get(SensorType::AuxTemp1);
|
|
|
|
case GPPWM_AuxTemp2:
|
|
|
|
return Sensor::get(SensorType::AuxTemp2);
|
2021-04-14 22:08:34 -07:00
|
|
|
case GPPWM_Zero:
|
|
|
|
return 0;
|
2021-05-28 00:38:45 -07:00
|
|
|
case GPPWM_AccelPedal:
|
|
|
|
return Sensor::get(SensorType::AcceleratorPedal);
|
2021-07-31 18:20:19 -07:00
|
|
|
case GPPWM_Vbatt:
|
|
|
|
return Sensor::get(SensorType::BatteryVoltage);
|
2020-04-26 11:06:28 -07:00
|
|
|
}
|
2021-03-29 05:57:37 -07:00
|
|
|
|
|
|
|
return unexpected;
|
2020-04-26 11:06:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void GppwmChannel::setOutput(float result) {
|
|
|
|
// Not init yet, nothing to do.
|
2020-06-11 17:43:26 -07:00
|
|
|
if (!m_config) {
|
2020-04-26 11:06:28 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-06-11 17:43:26 -07:00
|
|
|
|
|
|
|
if (m_usePwm) {
|
|
|
|
efiAssertVoid(OBD_PCM_Processor_Fault, m_usePwm, "m_usePwm null");
|
|
|
|
m_pwm->setSimplePwmDutyCycle(clampF(0, result / 100.0f, 1));
|
|
|
|
} else {
|
|
|
|
efiAssertVoid(OBD_PCM_Processor_Fault, m_output, "m_output null");
|
2021-09-06 04:58:17 -07:00
|
|
|
if (m_config->offBelowDuty > m_config->onAboveDuty) {
|
|
|
|
firmwareError(CUSTOM_ERR_6122, "You can't have off below %d greater than on above %d",
|
2021-09-05 17:16:57 -07:00
|
|
|
m_config->offBelowDuty,
|
2021-09-05 17:18:14 -07:00
|
|
|
m_config->onAboveDuty);
|
2021-09-05 17:16:57 -07:00
|
|
|
}
|
2020-04-26 11:06:28 -07:00
|
|
|
// Apply hysteresis with provided values
|
|
|
|
if (m_state && result < m_config->offBelowDuty) {
|
|
|
|
m_state = false;
|
|
|
|
} else if (!m_state && result > m_config->onAboveDuty) {
|
|
|
|
m_state = true;
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:43:26 -07:00
|
|
|
m_output->setValue(m_state);
|
2020-04-26 11:06:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 06:21:18 -07:00
|
|
|
void GppwmChannel::init(bool usePwm, IPwm* pwm, OutputPin* outputPin, const ValueProvider3D* table, const gppwm_channel* config) {
|
2020-04-26 11:06:28 -07:00
|
|
|
m_usePwm = usePwm;
|
|
|
|
m_pwm = pwm;
|
2020-06-11 17:43:26 -07:00
|
|
|
m_output = outputPin;
|
2020-04-26 11:06:28 -07:00
|
|
|
m_table = table;
|
|
|
|
m_config = config;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:44:44 -07:00
|
|
|
percent_t GppwmChannel::getOutput() const {
|
2020-04-26 11:06:28 -07:00
|
|
|
expected<float> loadAxisValue = readGppwmChannel(m_config->loadAxis PASS_ENGINE_PARAMETER_SUFFIX);
|
|
|
|
|
|
|
|
// If we couldn't get load axis value, fall back on error value
|
|
|
|
if (!loadAxisValue) {
|
|
|
|
return m_config->dutyIfError;
|
|
|
|
}
|
|
|
|
|
|
|
|
float rpm = GET_RPM();
|
|
|
|
|
|
|
|
float result = m_table->getValue(rpm / RPM_1_BYTE_PACKING_MULT, loadAxisValue.Value);
|
|
|
|
|
|
|
|
if (cisnan(result)) {
|
|
|
|
return m_config->dutyIfError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-21 19:49:38 -07:00
|
|
|
float GppwmChannel::update() {
|
2020-04-26 11:06:28 -07:00
|
|
|
// Without a config, nothing to do.
|
|
|
|
if (!m_config) {
|
2020-10-21 19:49:38 -07:00
|
|
|
return 0;
|
2020-04-26 11:06:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
float output = getOutput();
|
|
|
|
setOutput(output);
|
2020-10-21 19:49:38 -07:00
|
|
|
return output;
|
2020-04-26 11:06:28 -07:00
|
|
|
}
|