Skyward boardcore
Loading...
Searching...
No Matches
Stepper.cpp
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#include "Stepper.h"
23
24namespace Boardcore
25{
26
27Stepper::Stepper(miosix::GpioPin stepPin, miosix::GpioPin directionPin,
28 float speed, float stepAngle, bool revertDirection,
29 uint16_t microStep, PinConfiguration pinConfiguration,
30 miosix::GpioPin enablePin)
31 : stepPin(stepPin), directionPin(directionPin), speed(speed),
32 stepAngle(stepAngle), revertDirection(revertDirection),
33 microStep(microStep), pinConfig(pinConfiguration), enablePin(enablePin),
34 currentDirection(Direction::CLOCKWISE)
35{
36 if (this->speed < 0)
37 this->speed = 0;
38
40
41 // Start with the motor disabled
42 disable();
43}
44
46{
48 enablePin.low();
49 else
50 enablePin.high();
51 enabled = true;
52}
53
55{
57 enablePin.high();
58 else
59 enablePin.low();
60 enabled = false;
61}
62
64{
65 // Following the connections written in the stepper-driver datasheet for
66 // moving the stepper clockwise we have that:
67 // directionPin high: stepper turns clockwise;
68 // directionPin low: stepper turns counterclockwise;
69 //
70 // The revertDirection flag is used just for accounting for an inverted
71 // polarity in the configuration.
73 {
74 // To set the stepper-driver to turn CLOCKWISE we have to set the pin
75 // high in common cathode mode and low in common anode mode (the
76 // resulting potential difference should lead to a high logic value)
77 if ((!revertDirection &&
80 {
81 directionPin.high();
82 }
83 else
84 {
85 directionPin.low();
86 }
87 }
88 else
89 {
90 // To set the stepper-driver to turn COUNTER-CLOCKWISE we have to set
91 // the pin low in common cathode mode and high in common anode mode (the
92 // resulting potential difference should lead to a low logic value)
93 if ((!revertDirection &&
96 {
97 directionPin.low();
98 }
99 else
100 {
101 directionPin.high();
102 }
103 }
104}
105
106void Stepper::move(int16_t steps)
107{
108 if (!enabled || speed == 0 || steps == 0)
109 return;
110
111 unsigned int halfStepDelay = 1e6 / (speed * 360 / stepAngle * microStep);
112 int16_t stepsAbs;
113 if (steps > 0)
114 {
115 // Go forward
117 setDirection();
118 stepsAbs = steps;
119 }
120 else
121 {
122 // Go backwards
124 setDirection();
125 stepsAbs = -steps;
126 }
127
128 miosix::delayUs(halfStepDelay);
129
130 for (int i = 0; i < stepsAbs; i++)
131 {
133 stepPin.high();
134 else
135 stepPin.low();
136
137 miosix::delayUs(halfStepDelay);
138
140 stepPin.low();
141 else
142 stepPin.high();
143 miosix::delayUs(halfStepDelay);
144 }
145
147}
148
149bool Stepper::isEnabled() { return enabled; }
150
156
157} // 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
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 move(int16_t steps)
Move the stepper motor by the specified amount of steps.
Definition Stepper.cpp:106
PinConfiguration pinConfig
Definition Stepper.h:157
Direction currentDirection
Definition Stepper.h:160
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
virtual void setMicroStepping(uint16_t microStep)
Set the motor driver micro stepping configuration.
Definition Stepper.h:166
miosix::GpioPin enablePin
Definition Stepper.h:158
@ 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
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.