| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | /** | |||
| 2 | * @file dc_motor.h | |||
| 3 | * | |||
| 4 | * ETB user documentation at https://github.com/rusefi/rusefi/wiki/HOWTO_electronic_throttle_body | |||
| 5 | * | |||
| 6 | * | |||
| 7 | * @date Dec 22, 2018 | |||
| 8 | * @author Matthew Kennedy, (c) 2018 | |||
| 9 | */ | |||
| 10 | ||||
| 11 | #pragma once | |||
| 12 | ||||
| 13 | /** | |||
| 14 | * @brief Brushed or brushless DC motor interface | |||
| 15 | * | |||
| 16 | * Represents a DC motor (brushed or brushless) that provides simple | |||
| 17 | * torque/power/current/duty cycle control, but not accurate absolute position control. | |||
| 18 | */ | |||
| 19 | class DcMotor | |||
| 20 | { | |||
| 21 | public: | |||
| 22 | /** | |||
| 23 | * @brief Sets the motor duty cycle. | |||
| 24 | * @param duty +1.0f represents full power forward, and -1.0f represents full power backward. | |||
| 25 | * @return True if any fault was detected driving the motor, and false if successful. | |||
| 26 | */ | |||
| 27 | virtual bool set(float duty) = 0; | |||
| 28 | ||||
| 29 | /** | |||
| 30 | * @brief Get the current motor duty cycle. | |||
| 31 | * @return The current duty cycle setting. +1.0f represents full power forward, and -1.0f represents full power backward. | |||
| 32 | */ | |||
| 33 | virtual float get() const = 0; | |||
| 34 | ||||
| 35 | virtual void disable(const char *msg) = 0; | |||
| 36 | virtual void enable() = 0; | |||
| 37 | ||||
| 38 | virtual bool isOpenDirection() const = 0; | |||
| 39 | ||||
| 40 | 531078 | const char* msg() const { | ||
| 41 | 531078 | return m_msg; | ||
| 42 | } | |||
| 43 | ||||
| 44 | protected: | |||
| 45 | const char* m_msg = nullptr; | |||
| 46 | }; | |||
| 47 | ||||
| 48 | struct IPwm; | |||
| 49 | class OutputPin; | |||
| 50 | ||||
| 51 | /** | |||
| 52 | * @brief Represents a DC motor controller (H-bridge) with some combination of PWM and on/off control pins. | |||
| 53 | * 2024: what does 'TwoPin' class name mean? is that just a historic artifact? | |||
| 54 | */ | |||
| 55 | class TwoPinDcMotor : public DcMotor | |||
| 56 | { | |||
| 57 | public: | |||
| 58 | enum class ControlType | |||
| 59 | { | |||
| 60 | /** | |||
| 61 | * For example TLE7209 - two control wires: | |||
| 62 | * PWM on both wires - one to open, another to close | |||
| 63 | */ | |||
| 64 | PwmDirectionPins, | |||
| 65 | /** | |||
| 66 | * The control/enable pin is used for PWM and disable, and the two direction pins are used | |||
| 67 | * to set the polarity of each half of the H bridge. setting {dir1,dir2} = 10 should, | |||
| 68 | * turn the motor one direction (positive duty), and = 01 should turn the other way (negative | |||
| 69 | * duty). | |||
| 70 | * | |||
| 71 | * For example VNH2SP30 - three control wires: | |||
| 72 | * PWM on 'enable' PIN, two binary pins for direction | |||
| 73 | * | |||
| 74 | * TLE9201 with two wire control also uses this mode | |||
| 75 | * PWM on one pin, open/close using one binary direction pin, second direction pin unused | |||
| 76 | */ | |||
| 77 | PwmEnablePin, | |||
| 78 | }; | |||
| 79 | ||||
| 80 | private: | |||
| 81 | IPwm* m_enable = nullptr; | |||
| 82 | IPwm* m_dir1 = nullptr; | |||
| 83 | IPwm* m_dir2 = nullptr; | |||
| 84 | OutputPin* const m_disable; | |||
| 85 | float m_value = 0; | |||
| 86 | bool m_isInverted = false; | |||
| 87 | ||||
| 88 | ControlType m_type = ControlType::PwmDirectionPins; | |||
| 89 | public: | |||
| 90 | /** | |||
| 91 | * @param enable IPwm driver for enable pin, for PWM speed control. | |||
| 92 | * @param dir1 Enable 1 or direction 1 pin. Gets set high to rotate forward. | |||
| 93 | * @param dir2 Enable 2 or direction 2 pin. Gets set high to rotate backward. | |||
| 94 | */ | |||
| 95 | TwoPinDcMotor(OutputPin& disable); | |||
| 96 | ||||
| 97 | void configure(IPwm& enable, IPwm& dir1, IPwm& dir2, bool isInverted); | |||
| 98 | ||||
| 99 | virtual bool set(float duty) override; | |||
| 100 | float get() const override; | |||
| 101 | bool isOpenDirection() const override; | |||
| 102 | ||||
| 103 | void enable() override; | |||
| 104 | void disable(const char *msg) override; | |||
| 105 | ||||
| 106 | 6 | void setType(ControlType type) { m_type = type; } | ||
| 107 | }; | |||
| 108 |