stepmotor: made some small API changes for consistency and usability

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Mihai Tudor Panu
2015-12-22 15:20:30 -08:00
parent 091bd9b381
commit 4dc679af70
6 changed files with 56 additions and 45 deletions

View File

@ -34,10 +34,13 @@ using namespace upm;
using namespace std;
StepMotor::StepMotor (int dirPin, int stePin, int steps, int enPin)
: m_dirPinCtx(dirPin), m_stePinCtx(stePin), m_enPinCtx(0), m_steps(steps) {
: m_dirPinCtx(dirPin),
m_stePinCtx(stePin),
m_enPinCtx(0),
m_steps(steps) {
m_name = "StepMotor";
setSpeed(60);
setStep(0);
setPosition(0);
if (m_dirPinCtx.dir(mraa::DIR_OUT) != mraa::SUCCESS) {
throw std::runtime_error(string(__FUNCTION__) +
@ -95,7 +98,7 @@ StepMotor::setSpeed (int speed) {
mraa::Result
StepMotor::step (int ticks) {
if (ticks < 0) {
return stepBackwards(abs(ticks));
return stepBackward(abs(ticks));
} else {
return stepForward(ticks);
}
@ -106,37 +109,37 @@ StepMotor::stepForward (int ticks) {
dirForward();
for (int i = 0; i < ticks; i++) {
move();
if (++m_position >= m_steps) {
m_position = 0;
}
m_position++;
delayus(m_delay - MINPULSE_US - OVERHEAD_US);
}
return mraa::SUCCESS;
}
mraa::Result
StepMotor::stepBackwards (int ticks) {
dirBackwards();
StepMotor::stepBackward (int ticks) {
dirBackward();
for (int i = 0; i < ticks; i++) {
move();
if (--m_position < 0) {
m_position = m_steps - 1;
}
m_position--;
delayus(m_delay - MINPULSE_US - OVERHEAD_US);
}
return mraa::SUCCESS;
}
void
StepMotor::setStep (int step) {
if (step <= m_steps) {
m_position = step;
}
StepMotor::setPosition (int pos) {
m_position = pos;
}
int
StepMotor::getPosition () {
return m_position;
}
int
StepMotor::getStep () {
return m_position;
return m_position < 0 ? m_steps + m_position % m_steps :
m_position % m_steps;
}
void
@ -157,7 +160,7 @@ StepMotor::dirForward () {
}
mraa::Result
StepMotor::dirBackwards () {
StepMotor::dirBackward () {
mraa::Result error = m_dirPinCtx.write(LOW);
if (error != mraa::SUCCESS) {
throw std::runtime_error(string(__FUNCTION__) +

View File

@ -69,7 +69,7 @@ namespace upm {
* and/or microstepping on stepper drivers that support such features.
*
* @image html stepmotor.jpg
* <br><em>ECS1030 Sensor image provided by SparkFun* under
* <br><em>EasyDriver Sensor image provided by SparkFun* under
* <a href=https://creativecommons.org/licenses/by-nc-sa/3.0/>
* CC BY-NC-SA-3.0</a>.</em>
*
@ -128,20 +128,28 @@ class StepMotor {
*
* @param ticks Number of steps the motor moves
*/
mraa::Result stepBackwards (int ticks);
mraa::Result stepBackward (int ticks);
/**
* Sets the current step. Useful if the motor is not at 0 when the
* Sets the current position. Useful if the motor is not at 0 when the
* driver is initialized.
*
* @param step Current shaft position
* @param step Current position
*/
void setStep (int step);
void setPosition (int pos);
/**
* Gets the current step.
* Gets the current position. This is cumulative and the result of all
* the step commands sent to the motor.
*
* @return Current shaft position.
* @return Stepper's position.
*/
int getPosition ();
/**
* Gets the current step. This is relative to one revolution.
*
* @return Current step, ranges from 0 to number of steps per revolution.
*/
int getStep ();
@ -157,7 +165,7 @@ class StepMotor {
int m_position;
mraa::Result dirForward ();
mraa::Result dirBackwards ();
mraa::Result dirBackward ();
void move ();
void delayus (int us);
};