Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Friends
exploringBB::StepperMotor Class Reference

A class to control a stepper motor using a motor driver board, such as the Easy Driver board, or compatible. The class uses five GPIOs to control each motor. More...

#include <StepperMotor.h>

Public Types

enum  STEP_MODE { STEP_FULL, STEP_HALF, STEP_QUARTER, STEP_EIGHT }
 
enum  DIRECTION { CLOCKWISE, ANTICLOCKWISE }
 

Public Member Functions

 StepperMotor (GPIO *gpio_MS1, GPIO *gpio_MS2, GPIO *gpio_STEP, GPIO *gpio_SLP, GPIO *gpio_DIR, int speedRPM=60, int stepsPerRevolution=200)
 
 StepperMotor (int gpio_MS1, int gpio_MS2, int gpio_STEP, int gpio_SLP, int gpio_DIR, int speedRPM=60, int stepsPerRevolution=200)
 
virtual void step ()
 
virtual void step (int numberOfSteps)
 
virtual int threadedStepForDuration (int numberOfSteps, int duration_ms)
 
virtual void threadedStepCancel ()
 
virtual void rotate (float degrees)
 
virtual void setDirection (DIRECTION direction)
 
virtual DIRECTION getDirection ()
 
virtual void reverseDirection ()
 
virtual void setStepMode (STEP_MODE mode)
 
virtual STEP_MODE getStepMode ()
 
virtual void setSpeed (float rpm)
 
virtual float getSpeed ()
 
virtual void setStepsPerRevolution (int steps)
 
virtual int getStepsPerRevolution ()
 
virtual void sleep ()
 
virtual void wake ()
 
virtual bool isAsleep ()
 
virtual ~StepperMotor ()
 

Friends

void * threadedStep (void *value)
 

Detailed Description

A class to control a stepper motor using a motor driver board, such as the Easy Driver board, or compatible. The class uses five GPIOs to control each motor.

Member Enumeration Documentation

Enumerator
CLOCKWISE 
ANTICLOCKWISE 
Definition: StepperMotor.h:40
Definition: StepperMotor.h:40
Enumerator
STEP_FULL 
STEP_HALF 
STEP_QUARTER 
STEP_EIGHT 
Definition: StepperMotor.h:39
Definition: StepperMotor.h:39
Definition: StepperMotor.h:39
Definition: StepperMotor.h:39

Constructor & Destructor Documentation

exploringBB::StepperMotor::StepperMotor ( GPIO gpio_MS1,
GPIO gpio_MS2,
GPIO gpio_STEP,
GPIO gpio_SLP,
GPIO gpio_DIR,
int  speedRPM = 60,
int  stepsPerRevolution = 200 
)
34  {
35  this->gpio_MS1 = gpio_MS1;
36  this->gpio_MS2 = gpio_MS2;
37  this->gpio_STEP = gpio_STEP;
38  this->gpio_SLP = gpio_SLP;
39  this->gpio_DIR = gpio_DIR;
40  // the default speed in rpm
41  this->setSpeed(speedRPM);
42  this->stepsPerRevolution = stepsPerRevolution;
43  this->init(speedRPM, stepsPerRevolution);
44 }
virtual void setSpeed(float rpm)
Definition: StepperMotor.cpp:108
exploringBB::StepperMotor::StepperMotor ( int  gpio_MS1,
int  gpio_MS2,
int  gpio_STEP,
int  gpio_SLP,
int  gpio_DIR,
int  speedRPM = 60,
int  stepsPerRevolution = 200 
)
47  {
48  this->gpio_MS1 = new GPIO(gpio_MS1);
49  this->gpio_MS2 = new GPIO(gpio_MS2);
50  this->gpio_STEP = new GPIO(gpio_STEP);
51  this->gpio_SLP = new GPIO(gpio_SLP);
52  this->gpio_DIR = new GPIO(gpio_DIR);
53  this->gpio_MS1->setDirection(GPIO::OUTPUT);
54  this->gpio_MS2->setDirection(GPIO::OUTPUT);
55  this->gpio_STEP->setDirection(GPIO::OUTPUT);
56  this->gpio_SLP->setDirection(GPIO::OUTPUT);
57  this->gpio_DIR->setDirection(GPIO::OUTPUT);
58  this->init(speedRPM, stepsPerRevolution);
59 }
Definition: GPIO.h:48
virtual int setDirection(GPIO::DIRECTION)
Definition: GPIO.cpp:120
exploringBB::StepperMotor::~StepperMotor ( )
virtual
176 {}

Member Function Documentation

virtual DIRECTION exploringBB::StepperMotor::getDirection ( )
inlinevirtual
67 { return this->direction; }
virtual float exploringBB::StepperMotor::getSpeed ( )
inlinevirtual
72 { return speed; }
virtual STEP_MODE exploringBB::StepperMotor::getStepMode ( )
inlinevirtual
70 { return stepMode; }
virtual int exploringBB::StepperMotor::getStepsPerRevolution ( )
inlinevirtual
74 { return stepsPerRevolution; }
virtual bool exploringBB::StepperMotor::isAsleep ( )
inlinevirtual
77 { return asleep; }
void exploringBB::StepperMotor::reverseDirection ( )
virtual
151  {
152  if(this->direction==CLOCKWISE){
154  }
155  else this->setDirection(CLOCKWISE);
156 }
Definition: StepperMotor.h:40
Definition: StepperMotor.h:40
virtual void setDirection(DIRECTION direction)
Definition: StepperMotor.cpp:145
void exploringBB::StepperMotor::rotate ( float  degrees)
virtual
158  {
159  float degreesPerStep = 360.0f/getStepsPerRevolution();
160  int numberOfSteps = floor(((this->delayFactor*degrees)/degreesPerStep)+0.5);
161  //cout << "The number of steps is " << numberOfSteps << endl;
162  //cout << "The delay factor is " << delayFactor << endl;
163  step(numberOfSteps);
164 }
virtual int getStepsPerRevolution()
Definition: StepperMotor.h:74
virtual void step()
Definition: StepperMotor.cpp:127
void exploringBB::StepperMotor::setDirection ( DIRECTION  direction)
virtual
145  {
146  this->direction = direction;
147  if(this->direction==CLOCKWISE) this->gpio_DIR->setValue(GPIO::HIGH);
148  else this->gpio_DIR->setValue(GPIO::LOW);
149 }
Definition: StepperMotor.h:40
Definition: GPIO.h:49
Definition: GPIO.h:49
virtual int setValue(GPIO::VALUE)
Definition: GPIO.cpp:130
void exploringBB::StepperMotor::setSpeed ( float  rpm)
virtual
108  {
109  this->speed = rpm;
110  float delayPerSec = (60/rpm)/stepsPerRevolution; // delay per step in seconds
111  this->uSecDelay = (int)(delayPerSec * 1000 * 1000); // in microseconds
112 }
void exploringBB::StepperMotor::setStepMode ( STEP_MODE  mode)
virtual
82  {
83  this->stepMode = mode;
84  switch(stepMode){
85  case STEP_FULL:
86  this->gpio_MS1->setValue(GPIO::LOW);
87  this->gpio_MS2->setValue(GPIO::LOW);
88  this->delayFactor = 1;
89  break;
90  case STEP_HALF:
91  this->gpio_MS1->setValue(GPIO::HIGH);
92  this->gpio_MS2->setValue(GPIO::LOW);
93  this->delayFactor = 2;
94  break;
95  case STEP_QUARTER:
96  this->gpio_MS1->setValue(GPIO::LOW);
97  this->gpio_MS2->setValue(GPIO::HIGH);
98  this->delayFactor = 4;
99  break;
100  case STEP_EIGHT:
101  this->gpio_MS1->setValue(GPIO::HIGH);
102  this->gpio_MS2->setValue(GPIO::HIGH);
103  this->delayFactor = 8;
104  break;
105  }
106 }
Definition: StepperMotor.h:39
Definition: GPIO.h:49
Definition: GPIO.h:49
Definition: StepperMotor.h:39
Definition: StepperMotor.h:39
virtual int setValue(GPIO::VALUE)
Definition: GPIO.cpp:130
Definition: StepperMotor.h:39
virtual void exploringBB::StepperMotor::setStepsPerRevolution ( int  steps)
inlinevirtual
73 { stepsPerRevolution = steps; }
void exploringBB::StepperMotor::sleep ( )
virtual
166  {
167  this->asleep = true;
168  this->gpio_SLP->setValue(GPIO::LOW);
169 }
Definition: GPIO.h:49
virtual int setValue(GPIO::VALUE)
Definition: GPIO.cpp:130
void exploringBB::StepperMotor::step ( )
virtual
127  {
128  this->gpio_STEP->setValue(GPIO::LOW);
129  this->gpio_STEP->setValue(GPIO::HIGH);
130 }
Definition: GPIO.h:49
Definition: GPIO.h:49
virtual int setValue(GPIO::VALUE)
Definition: GPIO.cpp:130
void exploringBB::StepperMotor::step ( int  numberOfSteps)
virtual
114  {
115  //cout << "Doing "<< numberOfSteps << " steps and going to sleep for " << uSecDelay/delayFactor << "uS\n";
116  int sleepDelay = uSecDelay/delayFactor;
117  if(numberOfSteps<0) {
118  this->reverseDirection();
119  numberOfSteps = -numberOfSteps;
120  }
121  for(int i=0; i<numberOfSteps; i++){
122  this->step();
123  usleep(sleepDelay);
124  }
125 }
virtual void reverseDirection()
Definition: StepperMotor.cpp:151
virtual void step()
Definition: StepperMotor.cpp:127
virtual void exploringBB::StepperMotor::threadedStepCancel ( )
inlinevirtual
64 { this->threadRunning = false; }
int exploringBB::StepperMotor::threadedStepForDuration ( int  numberOfSteps,
int  duration_ms 
)
virtual
133  {
134  this->threadedStepNumber = numberOfSteps;
135  this->threadedStepPeriod = duration_ms/numberOfSteps;
136  this->threadRunning = true;
137  if(pthread_create(&this->thread, NULL, &threadedStep, static_cast<void*>(this))){
138  perror("StepperMotor: Failed to create the stepping thread");
139  this->threadRunning = false;
140  return -1;
141  }
142  return 0;
143 }
friend void * threadedStep(void *value)
Definition: StepperMotor.cpp:179
void exploringBB::StepperMotor::wake ( )
virtual
171  {
172  this->asleep = false;
173  this->gpio_SLP->setValue(GPIO::HIGH);
174 }
Definition: GPIO.h:49
virtual int setValue(GPIO::VALUE)
Definition: GPIO.cpp:130

Friends And Related Function Documentation

void* threadedStep ( void *  value)
friend
179  {
180  StepperMotor *stepper = static_cast<StepperMotor*>(value);
181  while(stepper->threadRunning){
182  stepper->step();
183  usleep(stepper->threadedStepPeriod * 1000); // convert from ms to us
184  if(stepper->threadedStepNumber>0) stepper->threadedStepNumber--;
185  if(stepper->threadedStepNumber==0) stepper->threadRunning = false;
186  }
187  return 0;
188 }
StepperMotor(GPIO *gpio_MS1, GPIO *gpio_MS2, GPIO *gpio_STEP, GPIO *gpio_SLP, GPIO *gpio_DIR, int speedRPM=60, int stepsPerRevolution=200)
Definition: StepperMotor.cpp:33

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