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