rusefi-1/firmware/controllers/system/dc_motor.cpp

66 lines
1.4 KiB
C++
Raw Normal View History

/**
* @file DcMotor.cpp
* @brief DC motor controller
*
* @date Dec 22, 2018
* @author Matthew Kennedy
*/
2019-03-29 06:11:13 -07:00
#include "dc_motor.h"
2019-05-04 21:42:50 -07:00
TwoPinDcMotor::TwoPinDcMotor(SimplePwm* enable, SimplePwm* dir1, SimplePwm* dir2)
: m_enable(enable)
, m_dir1(dir1)
, m_dir2(dir2)
{
}
bool TwoPinDcMotor::isOpenDirection() const {
2019-05-04 21:42:50 -07:00
return m_value >= 0;
2019-03-01 20:09:33 -08:00
}
float TwoPinDcMotor::Get() const {
2019-05-04 21:42:50 -07:00
return m_value;
}
2019-02-10 16:13:04 -08:00
/**
* @param duty value between -1.0 and 1.0
*/
bool TwoPinDcMotor::Set(float duty)
{
2019-05-04 21:42:50 -07:00
m_value = duty;
bool isPositive = duty > 0;
2019-05-04 21:42:50 -07:00
if(!isPositive)
{
duty = -duty;
}
2019-05-04 21:42:50 -07:00
2019-02-10 16:13:04 -08:00
// below here 'duty' is a not negative
// Clamp to 100%
if(duty > 1.0f)
{
duty = 1.0f;
}
// Disable for very small duty
else if (duty < 0.01f)
{
duty = 0.0f;
}
2019-05-04 21:42:50 -07:00
// If we're in two pin mode, force 100%, else use this pin to PWM
2019-05-04 21:53:24 -07:00
float enableDuty = m_type == ControlType::PwmEnablePin ? duty : 1;
2019-04-12 22:03:12 -07:00
2019-05-04 21:53:24 -07:00
// Direction pins get 100% duty unless we're in PwmDirectionPins mode
float dirDuty = m_type == ControlType::PwmDirectionPins ? duty : 1;
2019-05-04 21:42:50 -07:00
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;
}