upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Public Member Functions

API for the Stepper Motor. More...

Detailed Description

This module defines the Stepper Motor interface. It is compatible with stepper motor drivers that use 2 pins to control the motor, like an Easy Driver from Brian Schmalz or the STR driver series from Applied Motion. It can also control an enable pin if one is available and connected.

The implementation is synchronous and thus blocking while the stepper motor is in motion. However it is possible to send the commands via threading and the performance of the library will be very good given a low CPU load. On a busy system though you will notice some jitter especially at higher speeds. It is possible to reduce this effect to some extent by using smoothing and/or microstepping on stepper drivers that support such features.

stepmotor.jpg


EasyDriver Sensor image provided by SparkFun* under CC BY 2.0.

upm::StepMotor sensor(2, 3);
while (doWork) {
cout << "1 Revolution forward and back at 60 rpm" << endl;
sensor.setSpeed(60);
sensor.stepForward(200);
upm_delay_us(1000000);
sensor.stepBackward(200);
upm_delay_us(1000000);
cout << "1 Revolution forward and back at 150 rpm" << endl;
sensor.setSpeed(150);
sensor.stepForward(200);
upm_delay_us(1000000);
sensor.stepBackward(200);
upm_delay_us(1000000);
cout << "1 Revolution forward and back at 300 rpm" << endl;
sensor.setSpeed(300);
sensor.stepForward(200);
upm_delay_us(1000000);
sensor.stepBackward(200);
upm_delay_us(1000000);
}

Public Member Functions

 StepMotor (int dirPin, int stePin, int steps=200, int enPin=-1)
 
 ~StepMotor ()
 
void enable (bool flag)
 
void setSpeed (int speed)
 
mraa::Result step (int ticks)
 
mraa::Result stepForward (int ticks)
 
mraa::Result stepBackward (int ticks)
 
void setPosition (int pos)
 
int getPosition ()
 
int getStep ()
 

Constructor & Destructor Documentation

StepMotor ( int  dirPin,
int  stePin,
int  steps = 200,
int  enPin = -1 
)

Instantiates a StepMotor object.

Parameters
dirPinDirection GPIO pin
stePinStepper pulse GPIO pin
stepsNumber of steps per revolution (Default 200)
enPinEnable pin if connected (Optional)

Here is the call graph for this function:

~StepMotor ( )

StepMotor object destructor.

Member Function Documentation

void enable ( bool  flag)

Can be used to enable/disable the stepper driver if an enable pin is available and connected. Check your data sheet as some drivers might have the enable logic inverted.

Parameters
flagtrue to enable or false to disable

Here is the caller graph for this function:

void setSpeed ( int  speed)

Sets the rotation speed in rpm. Default 60 rpm.

Parameters
speedRotation speed in rpm

Here is the caller graph for this function:

mraa::Result step ( int  ticks)

Rotates the motor by the specified number of steps. Positive values rotate clockwise and negative values rotate counter-clockwise.

Parameters
ticksNumber of steps the motor moves

Here is the call graph for this function:

mraa::Result stepForward ( int  ticks)

Rotates the motor forward (clockwise).

Parameters
ticksNumber of steps the motor moves

Here is the caller graph for this function:

mraa::Result stepBackward ( int  ticks)

Rotates the motor backward (counter-clockwise).

Parameters
ticksNumber of steps the motor moves

Here is the caller graph for this function:

void setPosition ( int  pos)

Sets the current position. Useful if the motor is not at 0 when the driver is initialized.

Parameters
posCurrent position

Here is the caller graph for this function:

int getPosition ( )

Gets the current position. This is cumulative and the result of all the step commands sent to the motor.

Returns
Stepper's position.
int getStep ( )

Gets the current step. This is relative to one revolution.

Returns
Current step, ranges from 0 to number of steps per revolution.

The documentation for this class was generated from the following files: