rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Attributes
TwoPinDcMotor Class Reference

Represents a DC motor controller (H-bridge) with some combination of PWM and on/off control pins. 2024: what does 'TwoPin' class name mean? is that just a historic artifact? More...

#include <dc_motor.h>

Inheritance diagram for TwoPinDcMotor:
Inheritance graph
[legend]
Collaboration diagram for TwoPinDcMotor:
Collaboration graph
[legend]

Public Types

enum class  ControlType { PwmDirectionPins , PwmEnablePin }
 

Public Member Functions

 TwoPinDcMotor (OutputPin &disable)
 
void configure (IPwm &enable, IPwm &dir1, IPwm &dir2, bool isInverted)
 
virtual bool set (float duty) override
 
float get () const override
 Get the current motor duty cycle.
 
bool isOpenDirection () const override
 
void enable () override
 
void disable (const char *msg) override
 
void setType (ControlType type)
 
- Public Member Functions inherited from DcMotor
const charmsg () const
 

Private Attributes

IPwmm_enable = nullptr
 
IPwmm_dir1 = nullptr
 
IPwmm_dir2 = nullptr
 
OutputPin *const m_disable
 
float m_value = 0
 
bool m_isInverted = false
 
ControlType m_type = ControlType::PwmDirectionPins
 

Additional Inherited Members

- Protected Attributes inherited from DcMotor
const charm_msg = nullptr
 

Detailed Description

Represents a DC motor controller (H-bridge) with some combination of PWM and on/off control pins. 2024: what does 'TwoPin' class name mean? is that just a historic artifact?

Definition at line 55 of file dc_motor.h.

Member Enumeration Documentation

◆ ControlType

enum class TwoPinDcMotor::ControlType
strong
Enumerator
PwmDirectionPins 

For example TLE7209 - two control wires: PWM on both wires - one to open, another to close

PwmEnablePin 

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, turn the motor one direction (positive duty), and = 01 should turn the other way (negative duty).

For example VNH2SP30 - three control wires: PWM on 'enable' PIN, two binary pins for direction

TLE9201 with two wire control also uses this mode PWM on one pin, open/close using one binary direction pin, second direction pin unused

Definition at line 58 of file dc_motor.h.

59 {
60 /**
61 * For example TLE7209 - two control wires:
62 * PWM on both wires - one to open, another to close
63 */
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 */
78 };

Constructor & Destructor Documentation

◆ TwoPinDcMotor()

TwoPinDcMotor::TwoPinDcMotor ( OutputPin disable)
Parameters
enableIPwm driver for enable pin, for PWM speed control.
dir1Enable 1 or direction 1 pin. Gets set high to rotate forward.
dir2Enable 2 or direction 2 pin. Gets set high to rotate backward.

Definition at line 13 of file dc_motor.cpp.

14 : m_disable(&disablePin)
15{
16 disable("init");
17}
OutputPin *const m_disable
Definition dc_motor.h:84
void disable(const char *msg) override
Definition dc_motor.cpp:34
Here is the call graph for this function:

Member Function Documentation

◆ configure()

void TwoPinDcMotor::configure ( IPwm enable,
IPwm dir1,
IPwm dir2,
bool  isInverted 
)

Definition at line 19 of file dc_motor.cpp.

19 {
21 m_dir1 = &dir1;
22 m_dir2 = &dir2;
23 m_isInverted = isInverted;
24}
IPwm * m_dir1
Definition dc_motor.h:82
bool m_isInverted
Definition dc_motor.h:86
IPwm * m_dir2
Definition dc_motor.h:83
IPwm * m_enable
Definition dc_motor.h:81
void enable() override
Definition dc_motor.cpp:26

Referenced by DcHardware::start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disable()

void TwoPinDcMotor::disable ( const char msg)
overridevirtual

Implements DcMotor.

Definition at line 34 of file dc_motor.cpp.

34 {
35 m_msg = msg;
36 if (m_disable) {
37 m_disable->setValue(true);
38 }
39
40 // Also set the duty to zero
41 set(0);
42}
const char * msg() const
Definition dc_motor.h:40
const char * m_msg
Definition dc_motor.h:45
void setValue(const char *msg, int logicValue, bool isForce=false)
Definition efi_gpio.cpp:604
virtual bool set(float duty) override
Definition dc_motor.cpp:55

Referenced by TwoPinDcMotor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ enable()

void TwoPinDcMotor::enable ( )
overridevirtual

Implements DcMotor.

Definition at line 26 of file dc_motor.cpp.

26 {
27 if (m_disable) {
28 m_disable->setValue(false);
29 }
30
31 m_msg = nullptr;
32}

Referenced by configure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get()

float TwoPinDcMotor::get ( ) const
overridevirtual

Get the current motor duty cycle.

Returns
The current duty cycle setting. +1.0f represents full power forward, and -1.0f represents full power backward.

Implements DcMotor.

Definition at line 48 of file dc_motor.cpp.

48 {
49 return m_value;
50}
float m_value
Definition dc_motor.h:85

Referenced by showDcMotorInfo(), and updateTunerStudioState().

Here is the caller graph for this function:

◆ isOpenDirection()

bool TwoPinDcMotor::isOpenDirection ( ) const
overridevirtual

Implements DcMotor.

Definition at line 44 of file dc_motor.cpp.

44 {
45 return m_value >= 0;
46}

Referenced by showDcMotorInfo().

Here is the caller graph for this function:

◆ set()

bool TwoPinDcMotor::set ( float  duty)
overridevirtual
Parameters
dutyvalue between -1.0 and 1.0

Implements DcMotor.

Definition at line 55 of file dc_motor.cpp.

56{
57 m_value = duty;
58
59 // For low voltage, voltageRatio will be >1 to boost duty so that motor current stays the same
60 // At high voltage, the inverse will be true to keep behavior always the same.
61 float voltageRatio = 14 / Sensor::get(SensorType::BatteryVoltage).value_or(14);
62 duty *= voltageRatio;
63
64 // If not init, don't try to set
65 if (!m_dir1 || !m_dir2 || !m_enable) {
66 if (m_disable) {
67 m_disable->setValue(true);
68 }
69
70 return false;
71 }
72
73 bool isPositive = duty > 0;
74
75 if (!isPositive) {
76 duty = -duty;
77 }
78
79 // below here 'duty' is a not negative
80
81 // Clamp to 100%
82 if (duty > 1.0f) {
83 duty = 1.0f;
84 }
85 // Disable for very small duty
86 else if (duty < 0.01f)
87 {
88 duty = 0.0f;
89 }
90
91 // If we're in two pin mode, force 100%, else use this pin to PWM
92 float enableDuty = m_type == ControlType::PwmEnablePin ? duty : 1;
93
94 // Direction pins get 100% duty unless we're in PwmDirectionPins mode
95 float dirDuty = m_type == ControlType::PwmDirectionPins ? duty : 1;
96
98 float recipDuty = 0;
99 if (m_isInverted) {
100 dirDuty = 1.0f - dirDuty;
101 recipDuty = 1.0f;
102 }
103
104 m_dir1->setSimplePwmDutyCycle(isPositive ? dirDuty : recipDuty);
105 m_dir2->setSimplePwmDutyCycle(isPositive ? recipDuty : dirDuty);
106
107 // This motor has no fault detection, so always return false (indicate success).
108 return false;
109}
virtual SensorResult get() const =0
ControlType m_type
Definition dc_motor.h:88
virtual void setSimplePwmDutyCycle(float dutyCycle)=0
static float duty

Referenced by disable(), and setDcMotorDuty().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setType()

void TwoPinDcMotor::setType ( ControlType  type)
inline

Definition at line 106 of file dc_motor.h.

106{ m_type = type; }

Referenced by DcHardware::start().

Here is the caller graph for this function:

Field Documentation

◆ m_dir1

IPwm* TwoPinDcMotor::m_dir1 = nullptr
private

Definition at line 82 of file dc_motor.h.

Referenced by configure(), and set().

◆ m_dir2

IPwm* TwoPinDcMotor::m_dir2 = nullptr
private

Definition at line 83 of file dc_motor.h.

Referenced by configure(), and set().

◆ m_disable

OutputPin* const TwoPinDcMotor::m_disable
private

Definition at line 84 of file dc_motor.h.

Referenced by disable(), enable(), and set().

◆ m_enable

IPwm* TwoPinDcMotor::m_enable = nullptr
private

Definition at line 81 of file dc_motor.h.

Referenced by configure(), and set().

◆ m_isInverted

bool TwoPinDcMotor::m_isInverted = false
private

Definition at line 86 of file dc_motor.h.

Referenced by configure(), and set().

◆ m_type

ControlType TwoPinDcMotor::m_type = ControlType::PwmDirectionPins
private

Definition at line 88 of file dc_motor.h.

Referenced by set(), and setType().

◆ m_value

float TwoPinDcMotor::m_value = 0
private

Definition at line 85 of file dc_motor.h.

Referenced by get(), isOpenDirection(), and set().


The documentation for this class was generated from the following files: