Skyward boardcore
Loading...
Searching...
No Matches
StepperPWM.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 "StepperPWM.h"
23
24namespace Boardcore
25{
26
27StepperPWM::StepperPWM(CountedPWM& pwm, miosix::GpioPin stepPin,
28 miosix::GpioPin directionPin, float speed,
29 float stepAngle, bool revertDirection,
30 uint16_t microStep, PinConfiguration pinConfiguration,
31 miosix::GpioPin enablePin)
32 : Stepper(stepPin, directionPin, speed, stepAngle, revertDirection,
33 microStep, pinConfiguration, enablePin),
34 pwm(pwm)
35{
36 if (this->speed < 0)
37 this->speed = 0;
38
41
42 // Start with the motor disabled
43 disable();
44}
45
46void StepperPWM::setSpeed(float speed)
47{
48 this->speed = speed;
50}
51
52void StepperPWM::setMicroStepping(uint16_t microStep)
53{
54 // Resize the pulses to generate and the current pulses generated to use the
55 // new microsteps (keeping constant the angle of movement)
56 pwm.updateTargetCount(pwm.getTargetCount() * this->microStep / microStep);
57 pwm.updateCurrentCount(pwm.getCurrentCount() * this->microStep / microStep);
58
61}
62
63void StepperPWM::move(int16_t steps)
64{
65 if (!enabled || speed == 0 || steps == 0)
66 return;
67
68 // First update currentPositionDeg. This method corrects the initial
69 // position saved with the current state of the CountTimer counter register,
70 // so that we account only for the pulses actually generated.
72
73 if (steps > 0)
74 {
75 // Go forward
78
79 // Move
80 pwm.generatePulses(steps);
81 }
82 else
83 {
84 // Go backwards
87
88 // Move
89 pwm.generatePulses(-steps);
90 }
91}
92
98
99} // namespace Boardcore
This class generates a PWM signal for a chosen number of pulses.
Definition CountedPWM.h:60
uint16_t getTargetCount()
Allows to get the target periods count while the timers are generating the signal.
Definition CountedPWM.h:178
void updateCurrentCount(uint16_t count)
Allows to update the current periods count while the timers are generating the signal.
Definition CountedPWM.h:183
void generatePulses(uint16_t pulses)
Triggers the generation of a specific number of PWM periods.
void updateTargetCount(uint16_t pulses)
Allows to update the target periods count while the timers are generating the signal.
Definition CountedPWM.h:173
void setFrequency(unsigned int pulseFrequency)
uint16_t getCurrentCount()
Returns the number of periods counted until now.
Definition CountedPWM.h:188
float currentPositionDeg
Definition Stepper.h:161
void setDirection()
Sets the directionPin to the right value to go in the direction stored in currentDirection.
Definition Stepper.cpp:63
Direction currentDirection
Definition Stepper.h:160
uint16_t microStep
Definition Stepper.h:156
virtual void setMicroStepping(uint16_t microStep)
Set the motor driver micro stepping configuration.
Definition Stepper.h:166
StepperPWM(CountedPWM &pwm, 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 StepperPWM object.
float getCurrentDegPosition() override
Returns the current angle of the stepper.
void move(int16_t steps) override
Move the stepper motor by the specified amount of steps.
void setMicroStepping(uint16_t microStep) override
Set the motor driver micro stepping configuration.
void setSpeed(float speed) override
Changes the stepper motor speed.
This file includes all the types the logdecoder script will decode.