fome-fw/firmware/controllers/system/dc_motor.cpp

86 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"
TwoPinDcMotor::TwoPinDcMotor(SimplePwm* pwm, OutputPin* dir1, OutputPin* dir2)
: m_pwm(pwm)
, m_dir1(dir1)
, m_dir2(dir2)
{
}
2019-03-01 20:09:33 -08:00
bool TwoPinDcMotor::isOpenDirection() {
return isPositiveOrZero;
}
float TwoPinDcMotor::Get() {
return value;
}
void TwoPinDcMotor::BrakeGnd() {
2019-03-03 12:27:49 -08:00
m_dir1->setValue(false);
m_dir2->setValue(false);
}
void TwoPinDcMotor::BrakeVcc() {
m_dir1->setValue(true);
m_dir2->setValue(true);
}
2019-02-10 16:13:04 -08:00
/**
* @param duty value between -1.0 and 1.0
*/
bool TwoPinDcMotor::Set(float duty)
{
2019-03-01 20:09:33 -08:00
this->value = duty;
if(duty < 0)
{
2019-02-10 16:13:04 -08:00
isPositiveOrZero = false;
duty = -duty;
}
else
{
2019-02-10 16:13:04 -08:00
isPositiveOrZero = true;
}
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;
}
if(duty < 0.01f)
{
BrakeGnd();
}
else
{
2019-04-12 22:03:12 -07:00
if (isPositiveOrZero) {
twoWireModeControl = m_dir1;
} else {
twoWireModeControl = m_dir2;
}
2019-02-10 16:13:04 -08:00
m_dir1->setValue(isPositiveOrZero);
m_dir2->setValue(!isPositiveOrZero);
}
m_pwm->setSimplePwmDutyCycle(duty);
// This motor has no fault detection, so always return false (indicate success).
return false;
}