Skyward boardcore
Loading...
Searching...
No Matches
Stepper.h
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Authors: Alberto Nidasio, Emilio Corigliano
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
26#include <interfaces-impl/gpio_impl.h>
27#include <interfaces/delays.h>
28#include <utils/TestUtils/MockGpioPin.h>
29
30#include "StepperData.h"
31
32namespace Boardcore
33{
34
36{
37public:
39 {
42 };
43
56 Stepper(
57 miosix::GpioPin stepPin, miosix::GpioPin directionPin, float speed = 1,
58 float stepAngle = 1.8, bool revertDirection = false,
59 uint16_t microStep = 1,
61 miosix::GpioPin enablePin = MockGpioPin());
62
63 void enable();
64
65 void disable();
66
72 virtual void setSpeed(float speed);
73
80 virtual void setMicroStepping(uint16_t microStep);
81
85 void zeroPosition(float degrees = 0);
86
90 virtual void move(int16_t steps);
91
95 void moveDeg(float degrees);
96
103 virtual void setPosition(int16_t steps);
104
111 void setPositionDeg(float degrees);
112
113 int16_t getCurrentPosition();
114
119 virtual float getCurrentDegPosition();
120
124 bool isEnabled();
125
130
131protected:
136 void setDirection();
137
138 enum Direction : int8_t
139 {
140 CLOCKWISE = 1, // To move the stepper clockwise seeing it from the top
141 // of the stepper
142 COUNTER_CLOCKWISE = -1, // To move the stepper counter-clockwise seeing
143 // it from the top of the stepper
144 };
145
146 // This class is not copyable!
147 Stepper& operator=(const Stepper&) = delete;
148 Stepper(const Stepper& p) = delete;
149
150 miosix::GpioPin stepPin;
151 miosix::GpioPin directionPin;
152 float speed; // [rev/s]
153 float stepAngle; // [deg/step]
154 bool enabled = false;
156 uint16_t microStep;
158 miosix::GpioPin enablePin;
159
160 Direction currentDirection; // Direction of the stepper
161 float currentPositionDeg = 0; // Absolute position counter [degrees]
162};
163
164inline void Stepper::setSpeed(float speed) { this->speed = speed; }
165
166inline void Stepper::setMicroStepping(uint16_t microStep)
167{
168 this->microStep = microStep;
169}
170
171inline void Stepper::zeroPosition(float degrees)
172{
173 currentPositionDeg = degrees;
174}
175
176inline void Stepper::moveDeg(float degrees)
177{
178 move(degrees * microStep / stepAngle);
179}
180
181inline void Stepper::setPosition(int16_t steps)
182{
184}
185
186inline void Stepper::setPositionDeg(float position)
187{
188 moveDeg(position - getCurrentDegPosition());
189}
190
192{
194}
195
197
198} // namespace Boardcore
miosix::GpioPin directionPin
Definition Stepper.h:151
StepperData getState(float moveDeg)
Returns the current position and the current timestamp.
Definition Stepper.cpp:151
float currentPositionDeg
Definition Stepper.h:161
virtual float getCurrentDegPosition()
Returns the current absolute position of the stepper in degrees [deg].
Definition Stepper.h:196
virtual void setSpeed(float speed)
Changes the stepper motor speed.
Definition Stepper.h:164
miosix::GpioPin stepPin
Definition Stepper.h:150
void setDirection()
Sets the directionPin to the right value to go in the direction stored in currentDirection.
Definition Stepper.cpp:63
virtual void setPosition(int16_t steps)
Set the position of the stepper motor.
Definition Stepper.h:181
Stepper(const Stepper &p)=delete
virtual void move(int16_t steps)
Move the stepper motor by the specified amount of steps.
Definition Stepper.cpp:106
PinConfiguration pinConfig
Definition Stepper.h:157
void zeroPosition(float degrees=0)
Overrides the driver's internal position counter.
Definition Stepper.h:171
Direction currentDirection
Definition Stepper.h:160
Stepper & operator=(const Stepper &)=delete
uint16_t microStep
Definition Stepper.h:156
void moveDeg(float degrees)
Move the stepper motor by the specified amount of degrees.
Definition Stepper.h:176
int16_t getCurrentPosition()
Definition Stepper.h:191
virtual void setMicroStepping(uint16_t microStep)
Set the motor driver micro stepping configuration.
Definition Stepper.h:166
miosix::GpioPin enablePin
Definition Stepper.h:158
void setPositionDeg(float degrees)
Set the position of the stepper motor.
Definition Stepper.h:186
@ COMMON_CATHODE
All - signals connected to Gnd.
@ COMMON_ANODE
All + signals connected to Vdd (3v3)
Stepper(miosix::GpioPin stepPin, miosix::GpioPin directionPin, float speed=1, float stepAngle=1.8, bool revertDirection=false, uint16_t microStep=1, PinConfiguration pinConfiguration=PinConfiguration::COMMON_CATHODE, miosix::GpioPin enablePin=MockGpioPin())
Construct a new Stepper object.
Definition Stepper.cpp:27
bool isEnabled()
Returns whether the stepper is enabled or not.
Definition Stepper.cpp:149
This file includes all the types the logdecoder script will decode.