Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
stepper.hpp
1 /*
2  Title --- motor/stepper.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_MOTOR_STEPPER_HPP_
23 #define _RPI_HW_MOTOR_STEPPER_HPP_
24 
25 #include <vector>
26 
27 #include <rpi-hw/types.hpp>
28 #include <rpi-hw/exception.hpp>
29 #include <rpi-hw/math.hpp>
30 #include <rpi-hw/time.hpp>
31 
32 #include <rpi-hw/iface/base.hpp>
33 #include <rpi-hw/iface/output.hpp>
34 
35 namespace rpihw { // Begin main namespace
36 
37 /*!
38  @namespace rpihw::motor
39  @brief Namespace of the displays.
40 */
41 
42 namespace motor { // Begin motors namespace
43 
44 //! Directions of the rotation.
45 enum Direction {
46 
47  DIRECTION_CW = 1, //!< Clockwise direction.
48  DIRECTION_CCW = -1 //!< Counterclockwise direction.
49 };
50 
51 /*!
52  @class stepper
53  @brief Motor stepper controller.
54 
55  @example motor/28bjy48.cpp
56 */
57 class stepper {
58 
59 public:
60 
61  //@{
62  //! Some moving sequences.
63  static const std::vector< uint8_t > seq_2wire, seq_wave, seq_full, seq_half;
64  //@}
65 
66  /*!
67  @brief Constructor method.
68  @param[in] steps The number of steps per revolution.
69  @param[in] pin0 The GPIO pin connected to the data pin #0 of the stepper controller.
70  @param[in] pin1 The GPIO pin connected to the data pin #1 of the stepper controller.
71  */
72  stepper( size_t steps, uint8_t pin0, uint8_t pin1 );
73 
74  /*!
75  @brief Constructor method.
76  @param[in] steps The number of steps per revolution.
77  @param[in] pin0 The GPIO pin connected to the data pin #0 of the stepper controller.
78  @param[in] pin1 The GPIO pin connected to the data pin #1 of the stepper controller.
79  @param[in] pin2 The GPIO pin connected to the data pin #2 of the stepper controller.
80  @param[in] pin3 The GPIO pin connected to the data pin #3 of the stepper controller.
81  */
82  stepper( size_t steps, uint8_t pin0, uint8_t pin1, uint8_t pin2, uint8_t pin3 );
83 
84  //! Destructor method.
85  virtual ~stepper();
86 
87  /*!
88  @brief Sets the number of steps per revolution.
89  @param[in] steps The number of rotations per minute.
90  */
91  void setSteps( size_t steps );
92 
93  /*!
94  @brief Returns the number of steps per revolution.
95  @return The current number of rotations per minute.
96  */
97  size_t getSteps() const;
98 
99  /*!
100  @brief Sets the moving sequence.
101  @param[in] sequence The moving sequence.
102  */
103  void setSequence( const std::vector< uint8_t > &sequence );
104 
105  /*!
106  @brief Sets the motor speed in rotations per minute (RPMs).
107  @param[in] rpm The number of rotations per minute.
108  */
109  void setSpeed( double rpm );
110 
111  /*!
112  @brief Returns the motor speed in rotations per minute (RPMs).
113  @return The currente number of rotations per minute.
114  */
115  double getSpeed() const;
116 
117  /*!
118  @brief Sets the motor direction.
119  @param[in] direction The motor direction.
120  */
121  void setDirection( Direction direction );
122 
123  /*!
124  @brief Returns the current motor direction.
125  @return The current motor direction.
126  */
127  Direction getDirection() const;
128 
129  /*!
130  @brief Moves the motor one or more steps.
131  @param[in] number Number of steps.
132  */
133  void step( size_t number = 1 );
134 
135  /*!
136  @brief Rotates the motor.
137  @param[in] angle The angle rotation (in degrees).
138  */
139  void rotate( double angle );
140 
141 protected:
142 
143  //! Output interface to the display.
145 
146  //! Moving sequence.
147  std::vector< uint8_t > m_sequence;
148 
149  //! Motor direction.
151 
152  //! Motor speed in rotations per minute (RPMs).
153  double m_rpm;
154 
155  //! Number of steps per revolution.
156  size_t m_steps;
157 
158  //! Steps counter.
159  size_t m_counter;
160 };
161 
162 } // End of motors namespace
163 
164 } // End of main namespace
165 
166 
167 // Include inline methods
168 #include <rpi-hw/motor/stepper-inl.hpp>
169 
170 #endif /* _RPI_HW_MOTOR_STEPPER_HPP_ */
void setSpeed(double rpm)
Sets the motor speed in rotations per minute (RPMs).
Definition: stepper-inl.hpp:44
Output interface.
Definition: output.hpp:43
size_t m_counter
Steps counter.
Definition: stepper.hpp:159
Counterclockwise direction.
Definition: stepper.hpp:48
void setDirection(Direction direction)
Sets the motor direction.
Definition: stepper-inl.hpp:58
void setSequence(const std::vector< uint8_t > &sequence)
Sets the moving sequence.
Direction
Directions of the rotation.
Definition: stepper.hpp:45
Clockwise direction.
Definition: stepper.hpp:47
double m_rpm
Motor speed in rotations per minute (RPMs).
Definition: stepper.hpp:153
static const std::vector< uint8_t > seq_2wire
Some moving sequences.
Definition: stepper.hpp:63
size_t getSteps() const
Returns the number of steps per revolution.
Definition: stepper-inl.hpp:37
void rotate(double angle)
Rotates the motor.
Motor stepper controller.
Definition: stepper.hpp:57
stepper(size_t steps, uint8_t pin0, uint8_t pin1)
Constructor method.
Direction getDirection() const
Returns the current motor direction.
Definition: stepper-inl.hpp:65
size_t m_steps
Number of steps per revolution.
Definition: stepper.hpp:156
virtual ~stepper()
Destructor method.
double getSpeed() const
Returns the motor speed in rotations per minute (RPMs).
Definition: stepper-inl.hpp:51
std::vector< uint8_t > m_sequence
Moving sequence.
Definition: stepper.hpp:147
iface::output * m_motor
Output interface to the display.
Definition: stepper.hpp:144
Direction m_direction
Motor direction.
Definition: stepper.hpp:150
void step(size_t number=1)
Moves the motor one or more steps.
void setSteps(size_t steps)
Sets the number of steps per revolution.
Definition: stepper-inl.hpp:30