ESP_IOT v2.5
IOT ESP Coding
PTStepperClass.cpp
Go to the documentation of this file.
1//! \link PTStepperClass
2// PTStepperClass.cpp
3//
4//
5// Created by Scott Moody on 3/16/25.
6//
7
8#include "PTStepperClass.h"
9
10//! for now .. use the PTStepper.h
11//#include "PTStepper.h"
12
13// from Dispense it calls getFeederType_mainModule()
14#include "StepperModule.h"
15
16/******************stepper declarations******************************/
21
23{
24 SerialDebug.printf("PTStepperClass init %s\n", config);
25}
26
27//
28
29#ifdef BOARD
30////Put all the pins in an array to make them easy to work with
32 12, //BLUE end of the Blue/Yellow motor coil RED is ground
33 14, //PINK end of the Pink/Orange motor coil
34 27, //YELLOW end of the Blue/Yellow motor coil
35 26 //ORANGE end of the Pink/Orange motor coil
36};
37
38#else
39
40
41#define NEW_M5_STEPPER
42#ifdef NEW_M5_STEPPER
43// M5Atom GPIO pins connected to ULN2003 IN1–IN4
44#define IN1 22
45#define IN2 19
46#define IN3 23
47#define IN4 33
48////Put all the pins in an array to make them easy to work with
50 IN1, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
51 IN2, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
52 IN3, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
53 IN4 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
54};
55#else
56////Put all the pins in an array to make them easy to work with
58 13, //IN1 on the ULN2003 Board, BLUE end of the Blue/Yellow motor coil
59 12, //IN2 on the ULN2003 Board, PINK end of the Pink/Orange motor coil
60 14, //IN3 on the ULN2003 Board, YELLOW end of the Blue/Yellow motor coil
61 27 //IN4 on the ULN2003 Board, ORANGE end of the Pink/Orange motor coil
62};
63#endif
64
65#endif //BOARD
66
67//Define the full step sequence.
68//With the pin (coil) states as an array of arrays
71 {HIGH,HIGH,LOW,LOW},
72 {LOW,HIGH,HIGH,LOW},
73 {LOW,LOW,HIGH,HIGH},
74 {HIGH,LOW,LOW,HIGH}
75};
76
77//Keeps track of the current step.
78//We'll use a zero based index.
80//int _cycleCnt = 0;
82
83//Keeps track of the current direction
84//Relative to the face of the motor.
85//Clockwise (true) or Counterclockwise(false)
86//We'll default to clockwise
88
89
90void step_PTStepperClass(int steps[][4], int stepCount);
91/**************** end stepper declarations *******************************************************************/
92
93/***************** begin stepper actions *******************************************************************/
94
95/************* Set all motor pins off which turns off the motor ************************************************/
97 for (int pin = 0; pin < 4; pin++) {
98 pinMode(_pins_PTStepperClass[pin], OUTPUT);
99 digitalWrite(_pins_PTStepperClass[pin], LOW);
100 }
101
102}
103
104//! calculate the steps etc
106{
108 //!converts degrees to motor cycles
110
111 // Define number of steps per rotation:
112 // Calculate how many micro-steps to complete one feed sb 130 for Uno or 512 for Mini
113
114#define USE_STEPPER_ANGLE_FORMULA
115#ifdef USE_STEPPER_ANGLE_FORMULA
116#else
117 // Uno makes 16 steps per revolution
119 {
121 }
122 // Mini makes 4 steps per revolution but reverses
125 // Tumbler accepts a variable number of degrees
127 {
128 //!converts degrees to motor cycles
130 }
131 // Fall thru handling *** new information from https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/
132 else
134#endif
135 SerialDebug.printf("PTStepperClass type=%d, angle = %f, STEPS = %d\n",getFeederType_mainModule(), stepperAngle, _targetSteps_PTStepperClass);
136
137 //! 6.14.2024 put this back as the if-else was goofed up.
138 /**************** Fudge Factor code to correct registration *********************
139 ** This code is added to make the stepper keep registration in the long run. **
140 ** it ran for 20 hours with a stepper angle of 15 and registarion was perfect.**
141 ********************************************************************************/
143 case(0):
146 break;
147 case(1):
150 break;
151 case(2):
154 break;
155 }
156
157 _lastType_PTStepperClass = getFeederType_mainModule(); // let's not do this again until needed
158
159 //!see if the motor direction is clockwise == true
161 SerialDebug.printf("MOTOR_DIRECTION_clockwise_PTStepperClass = %d\n", _clockwise_PTStepperClass);
162
163}
164
165//Prepare motor controller
167{
168 clearPins_PTStepperClass(); // Go turn all motor pins off
169 //SerialDebug.print("ourSteps after ");
170 //SerialDebug.print(ourSteps);
171 //SerialDebug.println(" ");
172
173
174 //! 5.3.25 trying to figure out the PIN use
175 registerPinUse_mainModule(IN1, "IN1", "PTStepperClass", false);
176 registerPinUse_mainModule(IN2, "IN2", "PTStepperClass", false);
177 registerPinUse_mainModule(IN3, "IN3", "PTStepperClass", false);
178 registerPinUse_mainModule(IN4, "IN4", "PTStepperClass", false);
179}
180
181void step_PTStepperClass(int steps[][4], int stepCount)
182{
183 //Then we can figure out what our current step within the sequence from the overall current step
184 //and the number of steps in the sequence
185 //SerialDebug.print("current step ");
186 //SerialDebug.println(currentStep);
187 int currentStepInSequence = _currentStep_PTStepperClass % stepCount;
188
189 //Figure out which step to use. If clock wise, it is the same is the current step
190 //if not clockwise, we fire them in the reverse order...
191 int directionStep = _clockwise_PTStepperClass ? currentStepInSequence : (stepCount - 1) - currentStepInSequence;
192
193 //Set the four pins to their proper state for the current step in the sequence,
194 //and for the current direction
195 for (int pin = 0; pin < 4; pin++) {
196 digitalWrite(_pins_PTStepperClass[pin], steps[directionStep][pin]);
197 }
198}
199
200//! cycle
202{
203 //Get a local reference to the number of steps in the sequence
204 //And call the step method to advance the motor in the proper direction
205
206 //Full Step
207 int stepCount = _fullStepCount_PTStepperClass;
209
210 // Increment the program field tracking the current step we are on
212
213 // If targetSteps has been specified, and we have reached
214 // that number of steps, reset the currentStep, and reverse directions
217 clearPins_PTStepperClass(); // Turn off motor
218 }
219 else if (_targetSteps_PTStepperClass == 0 && _currentStep_PTStepperClass == stepCount) {
220 // don't reverse direction, just reset the currentStep to 0
221 // resetting this will prevent currentStep from
222 // eventually overflowing the int variable it is stored in.
225 }
226
227 //2 milliseconds seems to be about the shortest delay that is usable. Anything
228 //lower and the motor starts to freeze.
229 delay(2);
230}
231
232//This will advance the stepper clockwise once by the angle specified in SetupStepper. Example 16 pockets in UNO is 22.5 degrees
235 { // Not assigned yet, is zero or is different type
236 whoAreWe_PTStepperClass(); // go figure who we are, Mini or Uno and calculate steps needed
237 SerialDebug.print("Feeder type currently is ");
238 SerialDebug.println(getFeederType_mainModule());
239 }
240 SerialDebug.println("**************** PTStepperClass::Starting Stepper *******************");
241
242 //! 5.15.25 try the async CLICK
243 //! click call
244 //! click call 5.26.25 SYNC version
246
248 // Step one unit in forward
251 }
252 _cycleCounter_PTStepperClass = 0; // Reset when done
253 SerialDebug.println("**************** PTStepperClass::Ending Stepper *************");
255
256
258 SerialDebug.println("**************** PTStepperClass::Starting Return *******************");
259 _clockwise_PTStepperClass = !_clockwise_PTStepperClass; // Time to go backwards
260 while (_cycleCounter_PTStepperClass < _targetSteps_PTStepperClass - 5) { //Don't quite go all the way back
261 // Step one unit backwards
264 }
266 _cycleCounter_PTStepperClass = 0; // Reset when done
267 _clockwise_PTStepperClass = !_clockwise_PTStepperClass; // next time go forward
268 SerialDebug.println("**************** PTStepperClass::Ending Return ***********");
269
270 }
271
272}
273//! stops motor
275{
276
277}
278//!loop the PTStepper (so timer can run)
280{
281
282}
void main_dispatchSyncCommand(int syncCallCommand)
the main sync command (no parameters yet)
void registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
int getFeederType_mainModule()
get the feeder type (Sepper 1,2,3 ...)
Definition: MainModule.cpp:361
#define SYNC_CLICK_SOUND
Definition: MainModule.h:258
void cycle_PTStepperClass()
cycle
void step_PTStepperClass(int steps[][4], int stepCount)
#define IN4
int _ourSteps_PTStepperClass(0)
PTStepperClass
int _currentStep_PTStepperClass
int _fudgeFactor_PTStepperClass
int _pins_PTStepperClass[]
int _fullStepCount_PTStepperClass
int _fullSteps_PTStepperClass[][4]
void whoAreWe_PTStepperClass()
calculate the steps etc
int _targetSteps_PTStepperClass
#define IN1
int _cycleCounter_PTStepperClass
bool _clockwise_PTStepperClass
void clearPins_PTStepperClass()
#define IN2
int _lastType_PTStepperClass
#define IN3
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 STEPPER_IS_TUMBLER
#define STEPPER_IS_UNO
#define STEPPER_IS_MINI
#define PREFERENCE_STEPPER_ANGLE_FLOAT_SETTING
#define PREFERENCE_STEPPER_CLOCKWISE_MOTOR_DIRECTION_SETTING
#define MINI
Definition: StepperModule.h:48
An mostly virtual class.
void start_MotorStepper()
starts the PTStepper
PTStepperClass(char *config)
constructor
void loop_MotorStepper()
setup the PTStepper
void setup_MotorStepper()
setup the PTStepper
void stop_MotorStepper()
stops motor