rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
dc_motor.cpp
Go to the documentation of this file.
1/**
2 * @file DcMotor.cpp
3 * @brief DC motor controller
4 *
5 * @date Dec 22, 2018
6 * @author Matthew Kennedy
7 */
8
9#include "pch.h"
10
11#include "dc_motor.h"
12
14 : m_disable(&disablePin)
15{
16 disable("init");
17}
18
19void TwoPinDcMotor::configure(IPwm& enable, IPwm& dir1, IPwm& dir2, bool isInverted) {
21 m_dir1 = &dir1;
22 m_dir2 = &dir2;
23 m_isInverted = isInverted;
24}
25
27 if (m_disable) {
28 m_disable->setValue(false);
29 }
30
31 m_msg = nullptr;
32}
33
34void TwoPinDcMotor::disable(const char *msg) {
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}
43
45 return m_value >= 0;
46}
47
48float TwoPinDcMotor::get() const {
49 return m_value;
50}
51
52/**
53 * @param duty value between -1.0 and 1.0
54 */
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}
const char * msg() const
Definition dc_motor.h:40
const char * m_msg
Definition dc_motor.h:45
Single output pin reference and state.
Definition efi_output.h:49
void setValue(const char *msg, int logicValue, bool isForce=false)
Definition efi_gpio.cpp:604
virtual SensorResult get() const =0
TwoPinDcMotor(OutputPin &disable)
Definition dc_motor.cpp:13
IPwm * m_dir1
Definition dc_motor.h:82
OutputPin *const m_disable
Definition dc_motor.h:84
bool m_isInverted
Definition dc_motor.h:86
float get() const override
Get the current motor duty cycle.
Definition dc_motor.cpp:48
void disable(const char *msg) override
Definition dc_motor.cpp:34
void configure(IPwm &enable, IPwm &dir1, IPwm &dir2, bool isInverted)
Definition dc_motor.cpp:19
IPwm * m_dir2
Definition dc_motor.h:83
ControlType m_type
Definition dc_motor.h:88
IPwm * m_enable
Definition dc_motor.h:81
virtual bool set(float duty) override
Definition dc_motor.cpp:55
float m_value
Definition dc_motor.h:85
void enable() override
Definition dc_motor.cpp:26
bool isOpenDirection() const override
Definition dc_motor.cpp:44
static void enable(const char *param)
Definition settings.cpp:441
virtual void setSimplePwmDutyCycle(float dutyCycle)=0
static float duty