rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
gppwm_channel.cpp
Go to the documentation of this file.
1
2#include "pch.h"
3
4#include "gppwm_channel.h"
5
6#include "table_helper.h"
8
9float GppwmChannel::setOutput(float result) {
10 // Not init yet, nothing to do.
11 if (!m_config) {
12 return result;
13 }
14
15 if (m_usePwm) {
16 efiAssert(ObdCode::OBD_PCM_Processor_Fault, m_usePwm, "m_usePwm null", 0);
17 m_pwm->setSimplePwmDutyCycle(clampF(0, result / 100.0f, 1));
18
19 return result;
20 } else {
21 efiAssert(ObdCode::OBD_PCM_Processor_Fault, m_output, "m_output null", 0);
23 firmwareError(ObdCode::CUSTOM_ERR_6122, "You can't have off below %d greater than on above %d",
26 }
27 // Apply hysteresis with provided values
28 if (m_state && result < m_config->offBelowDuty) {
29 m_state = false;
30 } else if (!m_state && result > m_config->onAboveDuty) {
31 m_state = true;
32 }
33
35
36 // Return the actual output value with hysteresis
37 return m_state ? 100 : 0;
38 }
39}
40
41void GppwmChannel::init(bool usePwm, IPwm* pwm, OutputPin* outputPin, const ValueProvider3D* table, const gppwm_channel* p_config) {
42 m_usePwm = usePwm;
43 m_pwm = pwm;
44 m_output = outputPin;
45 m_table = table;
46 m_config = p_config;
47}
48
49/*PUBLIC_API_WEAK*/ expected<float> boardOverrideGppwm(size_t index) {
50 UNUSED(index);
51 return unexpected;
52}
53
55 expected<float> xAxisValue = readGppwmChannel(m_config->rpmAxis);
56 expected<float> yAxisValue = readGppwmChannel(m_config->loadAxis);
57
58 GppwmResult result { (float)m_config->dutyIfError, xAxisValue.value_or(0), yAxisValue.value_or(0) };
59 expected<float> override = boardOverrideGppwm(index);
60 if (override) {
61 result.Result = override.Value;
62 return result;
63 }
64
65 // If we couldn't get load axis value, fall back on error value
66 if (!xAxisValue || !yAxisValue) {
67 return result;
68 }
69
70 float resultVal = m_table->getValue(xAxisValue.Value, yAxisValue.Value);
71
72 if (std::isnan(result.Result)) {
73 return result;
74 }
75
76 result.Result = resultVal;
77 return result;
78}
79
81 // Without a config, nothing to do.
82 if (!m_config) {
83 return {};
84 }
85
86 auto output = getOutput(index);
87 output.Result = setOutput(output.Result);
88
89 return output;
90}
GppwmResult getOutput(size_t index) const
float setOutput(float result)
OutputPin * m_output
void init(bool usePwm, IPwm *pwm, OutputPin *outputPin, const ValueProvider3D *table, const gppwm_channel *config)
const gppwm_channel * m_config
GppwmResult update(size_t index)
const ValueProvider3D * m_table
Single output pin reference and state.
Definition efi_output.h:49
void setValue(const char *msg, int logicValue, bool isForce=false)
Definition efi_gpio.cpp:604
virtual float getValue(float xColumn, float yRow) const =0
void firmwareError(ObdCode code, const char *fmt,...)
expected< float > boardOverrideGppwm(size_t index)
expected< float > readGppwmChannel(gppwm_channel_e channel)
UNUSED(samplingTimeSeconds)
@ OBD_PCM_Processor_Fault
@ CUSTOM_ERR_6122
virtual void setSimplePwmDutyCycle(float dutyCycle)=0