2018-12-22 20:57:28 -08:00
|
|
|
/**
|
2018-12-23 18:10:30 -08:00
|
|
|
* @file DcMotor.cpp
|
|
|
|
* @brief DC motor controller
|
|
|
|
*
|
|
|
|
* @date Dec 22, 2018
|
|
|
|
* @author Matthew Kennedy
|
2018-12-22 20:57:28 -08:00
|
|
|
*/
|
2018-12-23 18:10:30 -08:00
|
|
|
|
2018-12-22 20:57:28 -08:00
|
|
|
#include "DcMotor.h"
|
|
|
|
|
|
|
|
TwoPinDcMotor::TwoPinDcMotor(SimplePwm* pwm, OutputPin* dir1, OutputPin* dir2)
|
2018-12-23 18:10:30 -08:00
|
|
|
: m_pwm(pwm)
|
|
|
|
, m_dir1(dir1)
|
2018-12-22 20:57:28 -08:00
|
|
|
, m_dir2(dir2)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-23 18:10:30 -08:00
|
|
|
bool TwoPinDcMotor::Set(float duty)
|
2018-12-22 20:57:28 -08:00
|
|
|
{
|
|
|
|
bool dir;
|
|
|
|
|
|
|
|
if(duty < 0)
|
|
|
|
{
|
|
|
|
dir = false;
|
|
|
|
duty = -duty;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dir = true;
|
|
|
|
}
|
|
|
|
|
2018-12-23 18:10:30 -08:00
|
|
|
// Clamp to 100%
|
|
|
|
if(duty > 1.0f)
|
2018-12-22 20:57:28 -08:00
|
|
|
{
|
2018-12-23 18:10:30 -08:00
|
|
|
duty = 1.0f;
|
2018-12-22 20:57:28 -08:00
|
|
|
}
|
|
|
|
// Disable for very small duty
|
|
|
|
else if (duty < 0.01f)
|
|
|
|
{
|
|
|
|
duty = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(duty < 0.01f)
|
|
|
|
{
|
|
|
|
m_dir1->setValue(false);
|
|
|
|
m_dir2->setValue(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_dir1->setValue(dir);
|
|
|
|
m_dir2->setValue(!dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pwm->setSimplePwmDutyCycle(duty);
|
2018-12-23 18:10:30 -08:00
|
|
|
|
|
|
|
// This motor has no fault detection, so always return false (indicate success).
|
|
|
|
return false;
|
2018-12-22 20:57:28 -08:00
|
|
|
}
|