mirror of https://github.com/rusefi/rusefi-1.git
Fix gppwm properly (#1487)
* directly control output in onoff mode * test fixing
This commit is contained in:
parent
19bb1b6850
commit
b906d12268
|
@ -43,7 +43,7 @@ void initGpPwm(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
||||||
|
|
||||||
// Finally configure the channel
|
// Finally configure the channel
|
||||||
INJECT_ENGINE_REFERENCE(&channels[i]);
|
INJECT_ENGINE_REFERENCE(&channels[i]);
|
||||||
channels[i].init(usePwm, &outputs[i], tables[i], &cfg);
|
channels[i].init(usePwm, &outputs[i], &pins[i], tables[i], &cfg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,11 +34,15 @@ expected<float> readGppwmChannel(gppwm_channel_e channel DECLARE_ENGINE_PARAMETE
|
||||||
|
|
||||||
void GppwmChannel::setOutput(float result) {
|
void GppwmChannel::setOutput(float result) {
|
||||||
// Not init yet, nothing to do.
|
// Not init yet, nothing to do.
|
||||||
if (!m_pwm || !m_config) {
|
if (!m_config) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_usePwm) {
|
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");
|
||||||
// Apply hysteresis with provided values
|
// Apply hysteresis with provided values
|
||||||
if (m_state && result < m_config->offBelowDuty) {
|
if (m_state && result < m_config->offBelowDuty) {
|
||||||
m_state = false;
|
m_state = false;
|
||||||
|
@ -46,15 +50,14 @@ void GppwmChannel::setOutput(float result) {
|
||||||
m_state = true;
|
m_state = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = m_state ? 100 : 0;
|
m_output->setValue(m_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_pwm->setSimplePwmDutyCycle(clampF(0, result / 100.0f, 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GppwmChannel::init(bool usePwm, SimplePwm* pwm, const ValueProvider3D* table, const gppwm_channel* config) {
|
void GppwmChannel::init(bool usePwm, SimplePwm* pwm, OutputPin* outputPin, const ValueProvider3D* table, const gppwm_channel* config) {
|
||||||
m_usePwm = usePwm;
|
m_usePwm = usePwm;
|
||||||
m_pwm = pwm;
|
m_pwm = pwm;
|
||||||
|
m_output = outputPin;
|
||||||
m_table = table;
|
m_table = table;
|
||||||
m_config = config;
|
m_config = config;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ class GppwmChannel {
|
||||||
public:
|
public:
|
||||||
DECLARE_ENGINE_PTR;
|
DECLARE_ENGINE_PTR;
|
||||||
|
|
||||||
void init(bool usePwm, SimplePwm* pwm, const ValueProvider3D* table, const gppwm_channel* config);
|
void init(bool usePwm, SimplePwm* pwm, OutputPin* outputPin, const ValueProvider3D* table, const gppwm_channel* config);
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -24,5 +24,6 @@ private:
|
||||||
const gppwm_channel* m_config = nullptr;
|
const gppwm_channel* m_config = nullptr;
|
||||||
bool m_usePwm = false;
|
bool m_usePwm = false;
|
||||||
SimplePwm* m_pwm = nullptr;
|
SimplePwm* m_pwm = nullptr;
|
||||||
|
OutputPin* m_output = nullptr;
|
||||||
const ValueProvider3D* m_table = nullptr;
|
const ValueProvider3D* m_table = nullptr;
|
||||||
};
|
};
|
||||||
|
|
|
@ -23,6 +23,13 @@ void turnAllPinsOff(void);
|
||||||
#define turnAllPinsOff() {}
|
#define turnAllPinsOff() {}
|
||||||
#endif /* EFI_GPIO_HARDWARE */
|
#endif /* EFI_GPIO_HARDWARE */
|
||||||
|
|
||||||
|
// Used if you want a function to be virtual only for unit testing purposes
|
||||||
|
#if EFI_UNIT_TEST
|
||||||
|
#define TEST_VIRTUAL virtual
|
||||||
|
#else
|
||||||
|
#define TEST_VIRTUAL
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
/**
|
/**
|
||||||
* @brief Single output pin reference and state
|
* @brief Single output pin reference and state
|
||||||
|
@ -48,7 +55,7 @@ public:
|
||||||
bool isInitialized();
|
bool isInitialized();
|
||||||
|
|
||||||
bool getAndSet(int logicValue);
|
bool getAndSet(int logicValue);
|
||||||
void setValue(int logicValue);
|
TEST_VIRTUAL void setValue(int logicValue);
|
||||||
void toggle();
|
void toggle();
|
||||||
bool getLogicValue() const;
|
bool getLogicValue() const;
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,11 @@ public:
|
||||||
MOCK_METHOD(void, setSimplePwmDutyCycle, (float dutyCycle), (override));
|
MOCK_METHOD(void, setSimplePwmDutyCycle, (float dutyCycle), (override));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class MockOutputPin : public OutputPin {
|
||||||
|
public:
|
||||||
|
MOCK_METHOD(void, setValue, (int value), (override));
|
||||||
|
};
|
||||||
|
|
||||||
class MockExecutor : public TestExecutor {
|
class MockExecutor : public TestExecutor {
|
||||||
public:
|
public:
|
||||||
MOCK_METHOD(void, scheduleByTimestamp, (scheduling_s *scheduling, efitimeus_t timeUs, action_s action), (override));
|
MOCK_METHOD(void, scheduleByTimestamp, (scheduling_s *scheduling, efitimeus_t timeUs, action_s action), (override));
|
||||||
|
|
|
@ -26,7 +26,7 @@ TEST(GpPwm, OutputWithPwm) {
|
||||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(1.0f));
|
EXPECT_CALL(pwm, setSimplePwmDutyCycle(1.0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
ch.init(true, &pwm, nullptr, &cfg);
|
ch.init(true, &pwm, nullptr, nullptr, &cfg);
|
||||||
|
|
||||||
// Set the output - should set directly to PWM
|
// Set the output - should set directly to PWM
|
||||||
ch.setOutput(25.0f);
|
ch.setOutput(25.0f);
|
||||||
|
@ -44,19 +44,23 @@ TEST(GpPwm, OutputOnOff) {
|
||||||
cfg.onAboveDuty = 50;
|
cfg.onAboveDuty = 50;
|
||||||
cfg.offBelowDuty = 40;
|
cfg.offBelowDuty = 40;
|
||||||
|
|
||||||
MockPwm pwm;
|
MockOutputPin pin;
|
||||||
|
|
||||||
{
|
{
|
||||||
InSequence i;
|
InSequence i;
|
||||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(0.0f));
|
|
||||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(1.0f));
|
// Rising edge test
|
||||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(1.0f));
|
EXPECT_CALL(pin, setValue(0));
|
||||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(1.0f));
|
EXPECT_CALL(pin, setValue(1));
|
||||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(0.0f));
|
EXPECT_CALL(pin, setValue(1));
|
||||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(0.0f));
|
|
||||||
|
// Falling edge test
|
||||||
|
EXPECT_CALL(pin, setValue(1));
|
||||||
|
EXPECT_CALL(pin, setValue(0));
|
||||||
|
EXPECT_CALL(pin, setValue(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
ch.init(false, &pwm, nullptr, &cfg);
|
ch.init(false, nullptr, &pin, nullptr, &cfg);
|
||||||
|
|
||||||
// Test rising edge - these should output 0, 1, 1
|
// Test rising edge - these should output 0, 1, 1
|
||||||
ch.setOutput(49.0f);
|
ch.setOutput(49.0f);
|
||||||
|
@ -86,7 +90,7 @@ TEST(GpPwm, GetOutput) {
|
||||||
return tps;
|
return tps;
|
||||||
});
|
});
|
||||||
|
|
||||||
ch.init(false, nullptr, &table, &cfg);
|
ch.init(false, nullptr, nullptr, &table, &cfg);
|
||||||
|
|
||||||
Sensor::resetAllMocks();
|
Sensor::resetAllMocks();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue