mirror of https://github.com/rusefi/rusefi.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
|
||||
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) {
|
||||
// Not init yet, nothing to do.
|
||||
if (!m_pwm || !m_config) {
|
||||
if (!m_config) {
|
||||
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
|
||||
if (m_state && result < m_config->offBelowDuty) {
|
||||
m_state = false;
|
||||
|
@ -46,15 +50,14 @@ void GppwmChannel::setOutput(float result) {
|
|||
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_pwm = pwm;
|
||||
m_output = outputPin;
|
||||
m_table = table;
|
||||
m_config = config;
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ class GppwmChannel {
|
|||
public:
|
||||
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();
|
||||
|
||||
private:
|
||||
|
@ -24,5 +24,6 @@ private:
|
|||
const gppwm_channel* m_config = nullptr;
|
||||
bool m_usePwm = false;
|
||||
SimplePwm* m_pwm = nullptr;
|
||||
OutputPin* m_output = nullptr;
|
||||
const ValueProvider3D* m_table = nullptr;
|
||||
};
|
||||
|
|
|
@ -23,6 +23,13 @@ void turnAllPinsOff(void);
|
|||
#define turnAllPinsOff() {}
|
||||
#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
|
||||
/**
|
||||
* @brief Single output pin reference and state
|
||||
|
@ -48,7 +55,7 @@ public:
|
|||
bool isInitialized();
|
||||
|
||||
bool getAndSet(int logicValue);
|
||||
void setValue(int logicValue);
|
||||
TEST_VIRTUAL void setValue(int logicValue);
|
||||
void toggle();
|
||||
bool getLogicValue() const;
|
||||
|
||||
|
|
|
@ -41,6 +41,11 @@ public:
|
|||
MOCK_METHOD(void, setSimplePwmDutyCycle, (float dutyCycle), (override));
|
||||
};
|
||||
|
||||
class MockOutputPin : public OutputPin {
|
||||
public:
|
||||
MOCK_METHOD(void, setValue, (int value), (override));
|
||||
};
|
||||
|
||||
class MockExecutor : public TestExecutor {
|
||||
public:
|
||||
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));
|
||||
}
|
||||
|
||||
ch.init(true, &pwm, nullptr, &cfg);
|
||||
ch.init(true, &pwm, nullptr, nullptr, &cfg);
|
||||
|
||||
// Set the output - should set directly to PWM
|
||||
ch.setOutput(25.0f);
|
||||
|
@ -44,19 +44,23 @@ TEST(GpPwm, OutputOnOff) {
|
|||
cfg.onAboveDuty = 50;
|
||||
cfg.offBelowDuty = 40;
|
||||
|
||||
MockPwm pwm;
|
||||
MockOutputPin pin;
|
||||
|
||||
{
|
||||
InSequence i;
|
||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(0.0f));
|
||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(1.0f));
|
||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(1.0f));
|
||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(1.0f));
|
||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(0.0f));
|
||||
EXPECT_CALL(pwm, setSimplePwmDutyCycle(0.0f));
|
||||
|
||||
// Rising edge test
|
||||
EXPECT_CALL(pin, setValue(0));
|
||||
EXPECT_CALL(pin, setValue(1));
|
||||
EXPECT_CALL(pin, setValue(1));
|
||||
|
||||
// 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
|
||||
ch.setOutput(49.0f);
|
||||
|
@ -86,7 +90,7 @@ TEST(GpPwm, GetOutput) {
|
|||
return tps;
|
||||
});
|
||||
|
||||
ch.init(false, nullptr, &table, &cfg);
|
||||
ch.init(false, nullptr, nullptr, &table, &cfg);
|
||||
|
||||
Sensor::resetAllMocks();
|
||||
|
||||
|
|
Loading…
Reference in New Issue