From cd055a687df9f57dc4a0a9ccbf32a4bbc57db996 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 26 Jun 2021 18:42:40 -0700 Subject: [PATCH] dc motor api (#2869) * iwpm * dc motor has configure * consume * format * spelling --- firmware/controllers/actuators/dc_motors.cpp | 4 +- firmware/controllers/system/dc_motor.cpp | 68 ++++++++++------- firmware/controllers/system/dc_motor.h | 76 ++++++++++--------- .../system/timer/pwm_generator_logic.h | 8 +- 4 files changed, 88 insertions(+), 68 deletions(-) diff --git a/firmware/controllers/actuators/dc_motors.cpp b/firmware/controllers/actuators/dc_motors.cpp index 95dfd69804..fd4c75f1c1 100644 --- a/firmware/controllers/actuators/dc_motors.cpp +++ b/firmware/controllers/actuators/dc_motors.cpp @@ -31,7 +31,7 @@ private: SimplePwm m_pwmDir2; public: - DcHardware() : dcMotor(&m_pwmEnable, &m_pwmDir1, &m_pwmDir2, &m_disablePin) {} + DcHardware() : dcMotor(m_disablePin) {} TwoPinDcMotor dcMotor; @@ -83,6 +83,8 @@ public: 0 ); #endif /* EFI_UNIT_TEST */ + + dcMotor.configure(m_pwmEnable, m_pwmDir1, m_pwmDir2); } }; diff --git a/firmware/controllers/system/dc_motor.cpp b/firmware/controllers/system/dc_motor.cpp index af9832f713..e3780161f7 100644 --- a/firmware/controllers/system/dc_motor.cpp +++ b/firmware/controllers/system/dc_motor.cpp @@ -10,15 +10,18 @@ #include "efi_gpio.h" #include "pwm_generator_logic.h" -TwoPinDcMotor::TwoPinDcMotor(SimplePwm* enable, SimplePwm* dir1, SimplePwm* dir2, OutputPin* disablePin) - : m_enable(enable) - , m_dir1(dir1) - , m_dir2(dir2) - , m_disable(disablePin) +TwoPinDcMotor::TwoPinDcMotor(OutputPin& disablePin) + : m_disable(&disablePin) { disable(); } +void TwoPinDcMotor::configure(IPwm& enable, IPwm& dir1, IPwm& dir2) { + m_enable = &enable; + m_dir1 = &dir1; + m_dir2 = &dir2; +} + void TwoPinDcMotor::enable() { if (m_disable) { m_disable->setValue(false); @@ -49,34 +52,43 @@ bool TwoPinDcMotor::set(float duty) { m_value = duty; - bool isPositive = duty > 0; + // If not init, don't try to set + if (!m_dir1 || !m_dir2 || !m_enable) { + if (m_disable) { + m_disable->setValue(true); + } - if (!isPositive) { - duty = -duty; - } + return false; + } - // below here 'duty' is a not negative + bool isPositive = duty > 0; - // Clamp to 100% - if (duty > 1.0f) { - duty = 1.0f; - } - // Disable for very small duty - else if (duty < 0.01f) - { - duty = 0.0f; - } + if (!isPositive) { + duty = -duty; + } - // If we're in two pin mode, force 100%, else use this pin to PWM - float enableDuty = m_type == ControlType::PwmEnablePin ? duty : 1; + // below here 'duty' is a not negative - // Direction pins get 100% duty unless we're in PwmDirectionPins mode - float dirDuty = m_type == ControlType::PwmDirectionPins ? duty : 1; + // Clamp to 100% + if (duty > 1.0f) { + duty = 1.0f; + } + // Disable for very small duty + else if (duty < 0.01f) + { + duty = 0.0f; + } - m_enable->setSimplePwmDutyCycle(enableDuty); - m_dir1->setSimplePwmDutyCycle(isPositive ? dirDuty : 0); - m_dir2->setSimplePwmDutyCycle(isPositive ? 0 : dirDuty); + // If we're in two pin mode, force 100%, else use this pin to PWM + float enableDuty = m_type == ControlType::PwmEnablePin ? duty : 1; - // This motor has no fault detection, so always return false (indicate success). - return false; + // Direction pins get 100% duty unless we're in PwmDirectionPins mode + float dirDuty = m_type == ControlType::PwmDirectionPins ? duty : 1; + + m_enable->setSimplePwmDutyCycle(enableDuty); + m_dir1->setSimplePwmDutyCycle(isPositive ? dirDuty : 0); + m_dir2->setSimplePwmDutyCycle(isPositive ? 0 : dirDuty); + + // This motor has no fault detection, so always return false (indicate success). + return false; } diff --git a/firmware/controllers/system/dc_motor.h b/firmware/controllers/system/dc_motor.h index fdf6272d28..8d0f66488e 100644 --- a/firmware/controllers/system/dc_motor.h +++ b/firmware/controllers/system/dc_motor.h @@ -19,26 +19,26 @@ class DcMotor { public: - /** - * @brief Sets the motor duty cycle. - * @param duty +1.0f represents full power forward, and -1.0f represents full power backward. - * @return True if any fault was detected driving the motor, and false if successful. - */ - virtual bool set(float duty) = 0; + /** + * @brief Sets the motor duty cycle. + * @param duty +1.0f represents full power forward, and -1.0f represents full power backward. + * @return True if any fault was detected driving the motor, and false if successful. + */ + virtual bool set(float duty) = 0; - /** - * @brief Get the current motor duty cycle. - * @return The current duty cycle setting. +1.0f represents full power forward, and -1.0f represents full power backward. - */ - virtual float get() const = 0; + /** + * @brief Get the current motor duty cycle. + * @return The current duty cycle setting. +1.0f represents full power forward, and -1.0f represents full power backward. + */ + virtual float get() const = 0; virtual void disable() = 0; virtual void enable() = 0; - virtual bool isOpenDirection() const = 0; + virtual bool isOpenDirection() const = 0; }; -class SimplePwm; +struct IPwm; class OutputPin; /** @@ -48,13 +48,13 @@ class OutputPin; class TwoPinDcMotor : public DcMotor { public: - enum class ControlType - { - /** - * For example TLE7209 - two control wires: - * PWM on both wires - one to open, another to close - */ - PwmDirectionPins, + enum class ControlType + { + /** + * For example TLE7209 - two control wires: + * PWM on both wires - one to open, another to close + */ + PwmDirectionPins, /** * The control/enable pin is used for PWM and disable, and the two direction pins are used * to set the polarity of each half of the H bridge. setting {dir1,dir2} = 10 should, @@ -67,31 +67,33 @@ public: * TLE9201 with two wire control also uses this mode * PWM on one pin, open/close using one binary direction pin, second direction pin unused */ - PwmEnablePin, - }; + PwmEnablePin, + }; private: - SimplePwm* const m_enable; - SimplePwm* const m_dir1; - SimplePwm* const m_dir2; + IPwm* m_enable; + IPwm* m_dir1; + IPwm* m_dir2; OutputPin* const m_disable; - float m_value = 0; + float m_value = 0; - ControlType m_type = ControlType::PwmDirectionPins; + ControlType m_type = ControlType::PwmDirectionPins; public: - /** - * @param enable SimplePwm driver for enable pin, for PWM speed control. - * @param dir1 Enable 1 or direction 1 pin. Gets set high to rotate forward. - * @param dir2 Enable 2 or direction 2 pin. Gets set high to rotate backward. - */ - TwoPinDcMotor(SimplePwm* enable, SimplePwm* dir1, SimplePwm* dir2, OutputPin* disable); + /** + * @param enable IPwm driver for enable pin, for PWM speed control. + * @param dir1 Enable 1 or direction 1 pin. Gets set high to rotate forward. + * @param dir2 Enable 2 or direction 2 pin. Gets set high to rotate backward. + */ + TwoPinDcMotor(OutputPin& disable); - virtual bool set(float duty) override; - float get() const override; - bool isOpenDirection() const override; + void configure(IPwm& enable, IPwm& dir1, IPwm& dir2); + + virtual bool set(float duty) override; + float get() const override; + bool isOpenDirection() const override; void enable() override; void disable() override; - void setType(ControlType type) { m_type = type; } + void setType(ControlType type) { m_type = type; } }; diff --git a/firmware/controllers/system/timer/pwm_generator_logic.h b/firmware/controllers/system/timer/pwm_generator_logic.h index 21cf222c78..850fc756a3 100644 --- a/firmware/controllers/system/timer/pwm_generator_logic.h +++ b/firmware/controllers/system/timer/pwm_generator_logic.h @@ -117,11 +117,15 @@ private: struct hardware_pwm; -class SimplePwm : public PwmConfig { +struct IPwm { + virtual void setSimplePwmDutyCycle(float dutyCycle) = 0; +}; + +class SimplePwm : public PwmConfig, public IPwm { public: SimplePwm(); explicit SimplePwm(const char *name); - virtual void setSimplePwmDutyCycle(float dutyCycle); + void setSimplePwmDutyCycle(float dutyCycle) override; pin_state_t pinStates[2]; SingleChannelStateSequence sr[1]; float _switchTimes[2];