Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StepperMotor.h
Go to the documentation of this file.
1 /*
2  * StepperMotor.h Created on: 13 Jun 2014
3  * Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
4  * Made available for the book "Exploring BeagleBone"
5  * See: www.exploringbeaglebone.com
6  * Licensed under the EUPL V.1.1
7  *
8  * This Software is provided to You under the terms of the European
9  * Union Public License (the "EUPL") version 1.1 as published by the
10  * European Union. Any use of this Software, other than as authorized
11  * under this License is strictly prohibited (to the extent such use
12  * is covered by a right of the copyright holder of this Software).
13  *
14  * This Software is provided under the License on an "AS IS" basis and
15  * without warranties of any kind concerning the Software, including
16  * without limitation merchantability, fitness for a particular purpose,
17  * absence of defects or errors, accuracy, and non-infringement of
18  * intellectual property rights other than copyright. This disclaimer
19  * of warranty is an essential part of the License and a condition for
20  * the grant of any rights to this Software.
21  *
22  * For more details, see http://www.derekmolloy.ie/
23  */
24 
25 #ifndef STEPPERMOTOR_H_
26 #define STEPPERMOTOR_H_
27 #include "../gpio/GPIO.h"
28 
29 namespace exploringBB {
30 
36 class StepperMotor {
37 
38 public:
41 
42 private:
43  // The GPIO pins MS1, MS2 (Microstepping options), STEP (The low->high step)
44  // SLP (Sleep - active low) and DIR (Direction)
45  GPIO *gpio_MS1, *gpio_MS2, *gpio_STEP, *gpio_SLP, *gpio_DIR;
46  unsigned int uSecDelay;
47  DIRECTION direction;
48  int delayFactor; // keep constant rpm even with microstepping
49  STEP_MODE stepMode;
50  float speed;
51  int stepsPerRevolution;
52  bool asleep;
53  void init(int speedRPM, int stepsPerRevolution);
54 
55 public:
56  StepperMotor(GPIO *gpio_MS1, GPIO *gpio_MS2, GPIO *gpio_STEP, GPIO *gpio_SLP,
57  GPIO *gpio_DIR, int speedRPM = 60, int stepsPerRevolution = 200);
58  StepperMotor(int gpio_MS1, int gpio_MS2, int gpio_STEP, int gpio_SLP,
59  int gpio_DIR, int speedRPM = 60, int stepsPerRevolution = 200);
60 
61  virtual void step();
62  virtual void step(int numberOfSteps);
63  virtual int threadedStepForDuration(int numberOfSteps, int duration_ms);
64  virtual void threadedStepCancel() { this->threadRunning = false; }
65  virtual void rotate(float degrees);
66  virtual void setDirection(DIRECTION direction);
67  virtual DIRECTION getDirection() { return this->direction; }
68  virtual void reverseDirection();
69  virtual void setStepMode(STEP_MODE mode);
70  virtual STEP_MODE getStepMode() { return stepMode; }
71  virtual void setSpeed(float rpm);
72  virtual float getSpeed() { return speed; }
73  virtual void setStepsPerRevolution(int steps) { stepsPerRevolution = steps; }
74  virtual int getStepsPerRevolution() { return stepsPerRevolution; }
75  virtual void sleep();
76  virtual void wake();
77  virtual bool isAsleep() { return asleep; }
78  virtual ~StepperMotor();
79 
80 private:
81  bool threadRunning;
82  pthread_t thread;
83  CallbackType callbackFunction;
84  int threadedStepPeriod, threadedStepNumber;
85  friend void* threadedStep(void *value);
86 };
87 
88 void* threadedStep(void *value);
89 
90 } /* namespace exploringBB */
91 
92 #endif /* STEPPERMOTOR_H_ */
Definition: StepperMotor.h:39
int(* CallbackType)(int)
Definition: GPIO.h:40
virtual void setStepsPerRevolution(int steps)
Definition: StepperMotor.h:73
virtual void setSpeed(float rpm)
Definition: StepperMotor.cpp:108
virtual STEP_MODE getStepMode()
Definition: StepperMotor.h:70
Definition: StepperMotor.h:40
GPIO class for input and output functionality on a single GPIO pin.
Definition: GPIO.h:46
virtual DIRECTION getDirection()
Definition: StepperMotor.h:67
virtual int getStepsPerRevolution()
Definition: StepperMotor.h:74
virtual void setStepMode(STEP_MODE mode)
Definition: StepperMotor.cpp:82
virtual void wake()
Definition: StepperMotor.cpp:171
virtual int threadedStepForDuration(int numberOfSteps, int duration_ms)
Definition: StepperMotor.cpp:133
DIRECTION
Definition: StepperMotor.h:40
virtual bool isAsleep()
Definition: StepperMotor.h:77
Definition: StepperMotor.h:40
A class to control a stepper motor using a motor driver board, such as the Easy Driver board...
Definition: StepperMotor.h:36
virtual void reverseDirection()
Definition: StepperMotor.cpp:151
virtual void step()
Definition: StepperMotor.cpp:127
Definition: StepperMotor.h:39
virtual void rotate(float degrees)
Definition: StepperMotor.cpp:158
Definition: StepperMotor.h:39
Definition: BusDevice.cpp:27
virtual ~StepperMotor()
Definition: StepperMotor.cpp:176
friend void * threadedStep(void *value)
Definition: StepperMotor.cpp:179
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
STEP_MODE
Definition: StepperMotor.h:39
virtual void threadedStepCancel()
Definition: StepperMotor.h:64
Definition: StepperMotor.h:39
virtual void setDirection(DIRECTION direction)
Definition: StepperMotor.cpp:145
virtual void sleep()
Definition: StepperMotor.cpp:166
virtual float getSpeed()
Definition: StepperMotor.h:72
void * threadedStep(void *value)
Definition: StepperMotor.cpp:179