PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
stepper.h
Go to the documentation of this file.
1 
26 #pragma once
27 
28 #include <PropWare/gpio/pin.h>
29 
30 namespace PropWare {
31 
35 class Stepper {
36  public:
37  enum class Step {
38  HALF,
39  ONE,
40  ONE_AND_A_HALF,
41  TWO,
42  TWO_AND_A_HALF,
43  THREE,
44  THREE_AND_A_HALF,
45  FOUR
46  };
47 
51  static const unsigned int DEFAULT_DELAY = 10000;
52  static const Step DEFAULT_START_STEP = Step::HALF;
53 
54  public:
64  Stepper (const Pin::Mask phase1Mask, const Pin::Mask phase2Mask, const Pin::Mask phase3Mask,
65  const Pin::Mask phase4Mask, const Step start = DEFAULT_START_STEP)
66  : m_currentStep(start),
67  m_phase1(phase1Mask, Pin::Dir::OUT),
68  m_phase2(phase2Mask, Pin::Dir::OUT),
69  m_phase3(phase3Mask, Pin::Dir::OUT),
70  m_phase4(phase4Mask, Pin::Dir::OUT) {
71  this->rotate();
72  }
73 
79  void step_to (const Stepper::Step step) {
80  this->m_currentStep = step;
81  this->rotate();
82  }
83 
88  Step get_current_step () const {
89  return this->m_currentStep;
90  }
91 
101  bool full_power_hold (const bool direction, const unsigned int usDelay = DEFAULT_DELAY) {
102  const bool movementNeeded = static_cast<bool>(static_cast<uint32_t>(this->m_currentStep) % 2);
103  if (movementNeeded) {
104  if (direction)
105  this->half_forward(1, usDelay);
106  else
107  this->half_reverse(1, usDelay);
108  }
109  return movementNeeded;
110  }
111 
121  bool half_power_hold (const bool direction, const unsigned int usDelay = DEFAULT_DELAY) {
122  const bool movementNeeded = !static_cast<bool>(static_cast<uint32_t>(this->m_currentStep) % 2);
123  if (movementNeeded) {
124  if (direction)
125  this->half_forward(1, usDelay);
126  else
127  this->half_reverse(1, usDelay);
128  }
129  return movementNeeded;
130  }
131 
138  void step_forward (unsigned int steps, const unsigned int usDelay = DEFAULT_DELAY) {
139  while (steps--) {
140  const unsigned int stepNumber = static_cast<unsigned int>(this->m_currentStep) + 2;
141  this->m_currentStep = static_cast<Step>(stepNumber % 8);
142  this->rotate();
143  waitcnt(usDelay * MICROSECOND + CNT);
144  }
145  }
146 
153  void half_forward (unsigned int halfSteps, const unsigned int usDelay = DEFAULT_DELAY) {
154  while (halfSteps--) {
155  const unsigned int stepNumber = static_cast<unsigned int>(this->m_currentStep) + 1;
156  this->m_currentStep = static_cast<Step>(stepNumber % 8);
157  this->rotate();
158  waitcnt(usDelay * MICROSECOND + CNT);
159  }
160  }
161 
168  void step_reverse (unsigned int steps, const unsigned int usDelay = DEFAULT_DELAY) {
169  while (steps--) {
170  const unsigned int stepNumber = static_cast<unsigned int>(this->m_currentStep) + 6;
171  this->m_currentStep = static_cast<Step>(stepNumber % 8);
172  this->rotate();
173  waitcnt(usDelay * MICROSECOND + CNT);
174  }
175  }
176 
183  void half_reverse (unsigned int halfSteps, const unsigned int usDelay = DEFAULT_DELAY) {
184  while (halfSteps--) {
185  const unsigned int stepNumber = static_cast<unsigned int>(this->m_currentStep) + 7;
186  this->m_currentStep = static_cast<Step>(stepNumber % 8);
187  this->rotate();
188  waitcnt(usDelay * MICROSECOND + CNT);
189  }
190  }
191 
192  private:
196  virtual void rotate () {
197  switch (this->m_currentStep) {
198  case Step::HALF:
199  this->m_phase1.set();
200  this->m_phase2.clear();
201  this->m_phase3.clear();
202  this->m_phase4.set();
203  break;
204  case Step::ONE:
205  this->m_phase1.set();
206  this->m_phase2.clear();
207  this->m_phase3.clear();
208  this->m_phase4.clear();
209  break;
210  case Step::ONE_AND_A_HALF:
211  this->m_phase1.set();
212  this->m_phase2.set();
213  this->m_phase3.clear();
214  this->m_phase4.clear();
215  break;
216  case Step::TWO:
217  this->m_phase1.clear();
218  this->m_phase2.set();
219  this->m_phase3.clear();
220  this->m_phase4.clear();
221  break;
222  case Step::TWO_AND_A_HALF:
223  this->m_phase1.clear();
224  this->m_phase2.set();
225  this->m_phase3.set();
226  this->m_phase4.clear();
227  break;
228  case Step::THREE:
229  this->m_phase1.clear();
230  this->m_phase2.clear();
231  this->m_phase3.set();
232  this->m_phase4.clear();
233  break;
234  case Step::THREE_AND_A_HALF:
235  this->m_phase1.clear();
236  this->m_phase2.clear();
237  this->m_phase3.set();
238  this->m_phase4.set();
239  break;
240  case Step::FOUR:
241  this->m_phase1.clear();
242  this->m_phase2.clear();
243  this->m_phase3.clear();
244  this->m_phase4.set();
245  break;
246  }
247  }
248 
249  private:
250  Step m_currentStep;
251  Pin m_phase1;
252  Pin m_phase2;
253  Pin m_phase3;
254  Pin m_phase4;
255 };
256 
257 }
PropWare::Stepper::half_reverse
void half_reverse(unsigned int halfSteps, const unsigned int usDelay=DEFAULT_DELAY)
Move the motor backward by a specified number of half-steps.
Definition: stepper.h:183
PropWare::Stepper
4-pin bipolar and 5-pin unipolar stepper motor driver
Definition: stepper.h:35
PropWare::Stepper::half_power_hold
bool half_power_hold(const bool direction, const unsigned int usDelay=DEFAULT_DELAY)
Step the motor to the nearest full-step to achieve a half-power hold.
Definition: stepper.h:121
PropWare::Port::Mask
Mask
Definition: port.h:43
PropWare::Stepper::step_to
void step_to(const Stepper::Step step)
Move the motor directly to the specified step.
Definition: stepper.h:79
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::Stepper::Stepper
Stepper(const Pin::Mask phase1Mask, const Pin::Mask phase2Mask, const Pin::Mask phase3Mask, const Pin::Mask phase4Mask, const Step start=DEFAULT_START_STEP)
Constructor.
Definition: stepper.h:64
PropWare::Port::set
void set() const
Set selected output port high (set all pins to 1)
Definition: port.h:226
PropWare::Stepper::step_forward
void step_forward(unsigned int steps, const unsigned int usDelay=DEFAULT_DELAY)
Move the motor forward by a specified number of steps.
Definition: stepper.h:138
PropWare::Stepper::full_power_hold
bool full_power_hold(const bool direction, const unsigned int usDelay=DEFAULT_DELAY)
Step the motor to the nearest half-step to achieve a full-power hold.
Definition: stepper.h:101
PropWare::Stepper::get_current_step
Step get_current_step() const
Get the current step.
Definition: stepper.h:88
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
PropWare::Stepper::DEFAULT_DELAY
static const unsigned int DEFAULT_DELAY
Default delay in microseconds used between each step.
Definition: stepper.h:51
PropWare::Stepper::half_forward
void half_forward(unsigned int halfSteps, const unsigned int usDelay=DEFAULT_DELAY)
Move the motor forward the a specified number of half-steps.
Definition: stepper.h:153
pin.h
PropWare::Stepper::step_reverse
void step_reverse(unsigned int steps, const unsigned int usDelay=DEFAULT_DELAY)
Move the motor backward by a specified number of steps.
Definition: stepper.h:168
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Port::clear
void clear() const
Clear selected output port (set it to 0)
Definition: port.h:249