fix ETB PWM (#2870)

* iwpm

* dc motor has configure

* consume

* format

* use fake PWM to save one SimplePwm instance

* comments

* not sure why I changed that

* init the pins otherwise

* typo

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2021-08-18 22:33:33 -07:00 committed by GitHub
parent a65448ce80
commit 50889671c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 29 deletions

View File

@ -12,6 +12,17 @@
#include "dc_motors.h"
#include "dc_motor.h"
// Simple wrapper to use an OutputPin as "PWM" that can only do 0 or 1
struct PwmWrapper : public IPwm {
OutputPin& m_pin;
PwmWrapper(OutputPin& pin) : m_pin(pin) { }
void setSimplePwmDutyCycle(float dutyCycle) override {
m_pin.setValue(dutyCycle > 0.5f);
}
};
class DcHardware {
private:
OutputPin m_pinEnable;
@ -19,9 +30,12 @@ private:
OutputPin m_pinDir2;
OutputPin m_disablePin;
SimplePwm m_pwmEnable;
SimplePwm m_pwmDir1;
SimplePwm m_pwmDir2;
PwmWrapper wrappedEnable{m_pinEnable};
PwmWrapper wrappedDir1{m_pinDir1};
PwmWrapper wrappedDir2{m_pinDir2};
SimplePwm m_pwm1;
SimplePwm m_pwm2;
public:
DcHardware() : dcMotor(m_disablePin) {}
@ -29,9 +43,8 @@ public:
TwoPinDcMotor dcMotor;
void setFrequency(int frequency) {
m_pwmEnable.setFrequency(frequency);
m_pwmDir1.setFrequency(frequency);
m_pwmDir2.setFrequency(frequency);
m_pwm1.setFrequency(frequency);
m_pwm2.setFrequency(frequency);
}
void start(bool useTwoWires,
@ -50,34 +63,46 @@ public:
// Clamp to >100hz
int clampedFrequency = maxI(100, frequency);
if (useTwoWires) {
m_pinEnable.initPin("ETB Enable", pinEnable);
// no need to complicate event queue with ETB PWM in unit tests
#if ! EFI_UNIT_TEST
startSimplePwmHard(&m_pwmEnable, "ETB Enable",
executor,
pinEnable,
&m_pinEnable,
clampedFrequency,
0
);
startSimplePwmHard(&m_pwm1, "ETB Dir 1",
executor,
pinDir1,
&m_pinDir1,
clampedFrequency,
0
);
startSimplePwmHard(&m_pwmDir1, "ETB Dir 1",
executor,
pinDir1,
&m_pinDir1,
clampedFrequency,
0
);
startSimplePwmHard(&m_pwm2, "ETB Dir 2",
executor,
pinDir2,
&m_pinDir2,
clampedFrequency,
0
);
#endif // EFI_UNIT_TEST
startSimplePwmHard(&m_pwmDir2, "ETB Dir 2",
executor,
pinDir2,
&m_pinDir2,
clampedFrequency,
0
);
#endif /* EFI_UNIT_TEST */
dcMotor.configure(wrappedEnable, m_pwm1, m_pwm2);
} else {
m_pinDir1.initPin("ETB Dir 1", pinDir1);
m_pinDir2.initPin("ETB Dir 2", pinDir2);
dcMotor.configure(m_pwmEnable, m_pwmDir1, m_pwmDir2);
// no need to complicate event queue with ETB PWM in unit tests
#if ! EFI_UNIT_TEST
startSimplePwmHard(&m_pwm1, "ETB Enable",
executor,
pinEnable,
&m_pinEnable,
clampedFrequency,
0
);
#endif // EFI_UNIT_TEST
dcMotor.configure(m_pwm1, wrappedDir1, wrappedDir2);
}
}
};