ESP_IOT v2.5
IOT ESP Coding
PTStepper.cpp
Go to the documentation of this file.
1//! \link PTStepper
2
3//
4//
5//
6//
7
8#include "PTStepper.h"
9
10// from Dispense it calls getFeederType()
11#include "StepperModule.h"
12
13#ifdef ESP_M5
14#else
15/******************stepper declarations******************************/
16int _ourSteps(0);
18int _lastType = 0;
20//
21
22#ifdef BOARD
23////Put all the pins in an array to make them easy to work with
24int _pins[]{
25 12, //BLUE end of the Blue/Yellow motor coil RED is ground
26 14, //PINK end of the Pink/Orange motor coil
27 27, //YELLOW end of the Blue/Yellow motor coil
28 26 //ORANGE end of the Pink/Orange motor coil
29};
30
31#else
32
33#ifdef PROTO
34////Put all the pins in an array to make them easy to work with
35int _pins[]{
36 27, //BLUE end of the Blue/Yellow motor coil RED is ground
37 14, //PINK end of the Pink/Orange motor coil
38 12, //YELLOW end of the Blue/Yellow motor coil
39 13 //ORANGE end of the Pink/Orange motor coil
40};
41
42#else
43
44
45////Put all the pins in an array to make them easy to work with
46// PROTOtype board is same as no BOARD
47int _pins[]{
48 13, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
49 12, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
50 14, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
51 27 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
52};
53#endif //PROTO
54#endif //BOARD
55
56//Define the full step sequence.
57//With the pin (coil) states as an array of arrays
59int _fullSteps[][4] = {
60 {HIGH,HIGH,LOW,LOW},
61 {LOW,HIGH,HIGH,LOW},
62 {LOW,LOW,HIGH,HIGH},
63 {HIGH,LOW,LOW,HIGH}
64};
65
66//Keeps track of the current step.
67//We'll use a zero based index.
69//int _cycleCnt = 0;
71
72//Keeps track of the current direction
73//Relative to the face of the motor.
74//Clockwise (true) or Counterclockwise(false)
75//We'll default to clockwise
76bool _clockwise = true;
77
78void setup_PTStepper();
79void cycle_PTStepper();
80void step(int steps[][4], int stepCount);
81/**************** end stepper declarations *******************************************************************/
82
83/***************** begin stepper actions *******************************************************************/
84
85/************* Set all motor pins off which turns off the motor ************************************************/
86void clearPins() {
87 for (int pin = 0; pin < 4; pin++) {
88 pinMode(_pins[pin], OUTPUT);
89 digitalWrite(_pins[pin], LOW);
90 }
91
92}
93void whoAreWe() {
94 // Define number of steps per rotation:
95 // Calculate how many micro-steps to complete one feed sb 130 for Uno or 512 for Mini
96
97 if (getFeederType() == UNO) // Uno makes 16 steps per revolution
98 _targetSteps = 128;
99 else if (getFeederType() == MINI) // Mini makes 4 steps per revolution but reverses
100 _targetSteps = 512;
101 else if (getFeederType() == TUMBLER) // Tumbler accepts a variable number of degrees
102 _targetSteps = (ceil) ( getPreferenceFloat_mainModule(PREFERENCE_STEPPER_ANGLE_FLOAT_SETTING) / 0.17665361523586); //converts degrees to motor cycles
103 switch(_fudgeFactor) {
104 case(0):
106 _fudgeFactor = 1;
107 break;
108 case(1):
110 _fudgeFactor = 2;
111 break;
112 case(2):
114 _fudgeFactor = 0;
115 break;
116 }
117 SerialDebug.printf("STEPPER STEPS = %d\n", _targetSteps);
118 else
119 _targetSteps = 2038; // Fall thru handling *** new information from https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/
120
121
122 _lastType = getFeederType(); // let's not do this again until needed
123
124 //!see if the motor direction is clockwise == true
126 SerialDebug.printf("MOTOR_DIRECTION_CLOCKWISE = %d\n", _clockwise);
127
128}
129
130//Prepare motor controller
132 clearPins(); // Go turn all motor pins off
133 //SerialDebug.print("ourSteps after ");
134 //SerialDebug.print(ourSteps);
135 //SerialDebug.println(" ");
136}
137
138void step(int steps[][4], int stepCount) {
139 //Then we can figure out what our current step within the sequence from the overall current step
140 //and the number of steps in the sequence
141 //SerialDebug.print("current step ");
142 //SerialDebug.println(currentStep);
143 int currentStepInSequence = _currentStep % stepCount;
144
145 //Figure out which step to use. If clock wise, it is the same is the current step
146 //if not clockwise, we fire them in the reverse order...
147 int directionStep = _clockwise ? currentStepInSequence : (stepCount - 1) - currentStepInSequence;
148
149 //Set the four pins to their proper state for the current step in the sequence,
150 //and for the current direction
151 for (int pin = 0; pin < 4; pin++) {
152 digitalWrite(_pins[pin], steps[directionStep][pin]);
153 }
154}
155
157 //Get a local reference to the number of steps in the sequence
158 //And call the step method to advance the motor in the proper direction
159
160 //Full Step
161 int stepCount = _fullStepCount;
163
164 // Increment the program field tracking the current step we are on
165 _currentStep++;
166
167 // If targetSteps has been specified, and we have reached
168 // that number of steps, reset the currentStep, and reverse directions
169 if (_targetSteps != 0 && _currentStep == _targetSteps) {
170 _currentStep = 0;
171 clearPins(); // Turn off motor
172 }
173 else if (_targetSteps == 0 && _currentStep == stepCount) {
174 // don't reverse direction, just reset the currentStep to 0
175 // resetting this will prevent currentStep from
176 // eventually overflowing the int variable it is stored in.
177 _currentStep = 0;
178 clearPins();
179 }
180
181 //2 milliseconds seems to be about the shortest delay that is usable. Anything
182 //lower and the motor starts to freeze.
183 delay(2);
184}
185
186//This will advance the stepper clockwise once by the angle specified in SetupStepper. Example 16 pockets in UNO is 22.5 degrees
188 if (_ourSteps == 0 || getFeederType() != _lastType)
189 { // Not assigned yet, is zero or is different type
190 whoAreWe(); // go figure who we are, Mini or Uno and calculate steps needed
191 SerialDebug.print("Feeder type currently is ");
192 SerialDebug.println(getFeederType());
193 }
194 SerialDebug.println("**************** Starting Stepper *******************");
195
196 while (_cycleCounter < _targetSteps) {
197 // Step one unit in forward
200 }
201 _cycleCounter = 0; // Reset when done
202 SerialDebug.println("**************** Ending Stepper *************");
203 clearPins();
204
205
206 if (getFeederType() == MINI) {
207 SerialDebug.println("**************** Starting Return *******************");
208 _clockwise = !_clockwise; // Time to go backwards
209 while (_cycleCounter < _targetSteps - 5) { //Don't quite go all the way back
210 // Step one unit backwards
213 }
214 clearPins();
215 _cycleCounter = 0; // Reset when done
216 _clockwise = !_clockwise; // next time go forward
217 SerialDebug.println("**************** Emding Return ***********");
218
219 }
220
221}
222
223#endif //ESP_M5
void step(int steps[][4], int stepCount)
Definition: PTStepper.cpp:138
void clearPins()
Definition: PTStepper.cpp:86
void cycle_PTStepper()
Definition: PTStepper.cpp:156
int _currentStep
Definition: PTStepper.cpp:68
void whoAreWe()
Definition: PTStepper.cpp:93
int _fullStepCount
Definition: PTStepper.cpp:58
int _pins[]
Definition: PTStepper.cpp:24
int _lastType
Definition: PTStepper.cpp:18
void start_PTStepper()
starts the PTStepper
Definition: PTStepper.cpp:187
int _fudgeFactor
Definition: PTStepper.cpp:19
int _fullSteps[][4]
Definition: PTStepper.cpp:59
int _targetSteps
Definition: PTStepper.cpp:17
int _cycleCounter
Definition: PTStepper.cpp:70
int _ourSteps(0)
PTStepper
bool _clockwise
Definition: PTStepper.cpp:76
void setup_PTStepper()
setup the PTStepper
Definition: PTStepper.cpp:131
float getPreferenceFloat_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
#define PREFERENCE_STEPPER_ANGLE_FLOAT_SETTING
#define PREFERENCE_STEPPER_CLOCKWISE_MOTOR_DIRECTION_SETTING
int getFeederType()
retreives the feeder type (versus grabbing a global variable)
#define MINI
Definition: StepperModule.h:51
#define TUMBLER
Definition: StepperModule.h:52
#define UNO
Definition: StepperModule.h:50