ESP_IOT v2.5
IOT ESP Coding
StepperModule.cpp
Go to the documentation of this file.
1/**
2* \link StepperModule
3*/
4
5#include "StepperModule.h"
6
7
8//! 3.16.25 using a class version (thisi is for ESP_32 and ESP_M5 now)
9
10#include "PTStepperClass.h"
11#include "DCMotorStepperClass.h"
13//! 3.24.25 warm raining day
14#include "HDriverStepperClass.h"
15//! 3.31..25
17//! 5.18.25 45 years Mt St Hellens Eruption
19
20//! 8.13.25 Trying Stepper motor with HDriver (other was rather loud and grating)
22
23
24//! instances of the PTStepeprClass
25//! 1
27//! 2
29//! 3
31//! 4
32//! 3.31.25 try Servo ..
34//! 5
35//! 5.18.25 45 years Mt St Hellens Eruption
37//! 6
38//! 8.13.25 Trying Stepper motor with HDriver (other was rather loud and grating)
40
41//! MAKE SURE THIS IS UPDATED...
42#define NUM_STEPPER_CLASS 6
43
44//! use this one...
46
47//! 3.31.25 create array of plugs
49
51{
52 int whichStepperClasssIndex = 0;
53 SerialDebug.println("setup_StepperClasses");
54
55 //! instances of the PTStepeprClass
56 //! NOTE: These are the "identity' so that is also the "message" sent around. So SELF defining.
57 _DCMotorStepperClass = new DCMotorStepperClass((char*)"DCMotorStepperClass");
58 _motorSteppers[whichStepperClasssIndex++] = _DCMotorStepperClass;
59
60 _PTStepperClass = new PTStepperClass((char*)"PTStepperClass");
61 _motorSteppers[whichStepperClasssIndex++] = _PTStepperClass;
62
63 _HDriverStepperClass = new HDriverStepperClass((char*)"HDriverStepperClass");
64 _motorSteppers[whichStepperClasssIndex++] = _HDriverStepperClass;
65
66 //! 3.31.25 try Servo ..
67 _SG90ServoStepperClass = new SG90ServoStepperClass((char*)"SG90ServoStepperClass");
68 _motorSteppers[whichStepperClasssIndex++] = _SG90ServoStepperClass;
69
70 //! 5.18.25 45 years Mt St Hellens Eruption
71 _L9110S_DCStepperClass = new L9110S_DCStepperClass((char*)"L9110S_DCStepperClass");
72 _motorSteppers[whichStepperClasssIndex++] = _L9110S_DCStepperClass;
73
74 //! 8.13.25 Trying Stepper motor with HDriver (other was rather loud and grating)
75 _ULN2003_StepperClass = new ULN2003_StepperClass((char*)"ULN2003_StepperClass");
76 _motorSteppers[whichStepperClasssIndex++] = _ULN2003_StepperClass;
77
78 //! just check if we haven't goofed with NUM_STEPPER_CLASS not being the actual number whichStepperClasssIndex
79 if (whichStepperClasssIndex != NUM_STEPPER_CLASS)
80 {
81 SerialError.printf("stepperIndex %d not %d\n", whichStepperClasssIndex, NUM_STEPPER_CLASS);
82 }
83 //!TODO: create an array of plug options .. then call their "identity" method
84 SerialDebug.println("Done setup_StepperClasses");
85}
86
87//! called to set a preference (which will be an identifier and a string, which can be converted to a number or boolean)
88void stepperModule_savePreference(int preferenceID, String preferenceValue);
89
90/*feederState*/
91#define FEED_STOPPED 0
92#define SINGLE_FEED 1
93#define AUTO_FEED 2
94#define JACKPOT_FEED 3
95
97int _feedsPerJackpot = 3; //this will be set by user at some point
98int _feedCount = _feedsPerJackpot; //init to max feeds per Jackpot
99
100#define FEED_INTERVAL 1000 // make this a variable determined by user input and stored in JSON
101unsigned long feedPreviousMillis = 0;
102
103//proteced
104// someone else the "savePreferenceBoolean_mainModule(PREFERENCE_STEPPER_BUZZER_VALUE) knows where to save it..
105
106//!returns the buzzer status
108{
110 return buzzStatus;
111}
112
113//! retreives the feeder type (versus grabbing a global variable)
115{
117}
118
119//!called on setup()
121{
122
124 //! 3.28.24 feed on startup..
125#ifdef FEED_ON_STARTUP
127#endif
128
129 //! 3.16.25 use the class ..
131
132 //! NOTE: this is only re-evaluated on reboot .. for now
133 //! 3.16.25 for now this is compile option ..
134 //! but no reason the same build cannot support it..
135 //! sensor kind (see the M5AtomSensor .. as example)
137 SerialDebug.printf("sensorPlug = %s\n", sensorPlug);
138 _whichMotorStepper = NULL;
139
140 //! find it dynamically
141 //! 3.31.25
142 for (int i=0; i< NUM_STEPPER_CLASS; i++)
143 {
144 if (!_motorSteppers[i])
145 {
146 SerialDebug.printf("sensorPlug[%d] == null\n", i);
147 continue;
148 }
149 if (strcmp(sensorPlug,_motorSteppers[i]->classIdentity())==0)
150 {
152 SerialDebug.printf("Match motorSteppers[%d] = %s\n", i, sensorPlug);
153 break;
154 }
155 }
156 //! if nothing found .. use default
158 {
159#ifdef ESP_32
160 //! DEFAULT .. if (strcmp(sensorPlug,"PTStepperClass")==0)
161 //! use this one...
163#else
164 SerialDebug.printf("**** Using default PTStepper since null EPROM value ***\n");
165 //! 7.26.26 Tyler and Me .. 1991 Dead, Scar->Fire,Maggie, etc
166 //! this might be the BUG ... it's null it crashes later ... so a constraing is a NON NULL ??
167 //! use same PTStepperClass default
168 // _whichMotorStepper = _PTStepperClass;
169
170 //! the motorStepper can be nil
171 //_whichMotorStepper = NULL;
172 //! 5.2.25 default to HDriver..
173 //_whichMotorStepper = _HDriverStepperClass;
174
175 //! 7.31.25 use default..
176 //! 10.26.25 No Power day .. wind storm
177 //! if NULL then do't have a default..
178 //_whichMotorStepper = _L9110S_DCStepperClass;
179#endif
180 }
181
182#define SETUP_FIRST_TIME
183 //!TODO: task on the pins .. to the definition
184 //!and make it work backward compatible..
185#ifdef SETUP_FIRST_TIME
188#endif
189
190}
191//! 4.1.25 April Fools day. Mt Peak hike (162 bmp strange spike)
192//! get the identity of the SenasorPlug
193//! nil if non
195{
196 char *identity = NULL;
198 {
199 identity = _whichMotorStepper->classIdentity();
200 }
201 return identity;
202}
203//TODO: this isn't directly called.. but called from the main processsClientCommand(String cmd)
204////Keep ProcessClientCmd short to let the callback run. instead change the feeder state flag
206 SerialLots.printf("***** ProcessClientCmd(%c) from client*****\n", cmd);
207
208 if ((cmd == 0x00) || (cmd == 's') || (cmd == 'c'))
209 {
210 SerialLots.println("Setting FeedState = SINGLE_FEED");
211 _feedState = SINGLE_FEED; // Gen 3 Pet Tutor used 0x00 to feed so 0x00 is to make it backward compatible 's' is the new version
212 //not used .. for awhile.. So let's stop writing to EPROM all the time.
213 }
214 else if (cmd == 'a')
215 {
216 SerialLots.println("Setting FeedState = AUTO_FEED");
218
219 }
220 else if (cmd == 'j')
221 {
222 SerialLots.println("Setting FeedState = JACKPOT_FEED");
224
225 }
226 else if (cmd == 'B')
227 {
228 SerialLots.println("1.Setting buzzStatus = BUZZON");
229
230 //set the flag, then buz.. (or don't care, and the value is stored in mainModule.. TODO: maybe that should cache the result)
231#ifdef USE_UI_MODULE
232 //make a buzz sound..
235#endif
236
237 }
238 else if (cmd == 'b')
239 {
240 SerialLots.println("2.Setting buzzStatus = BUZZOFF");
241#ifdef USE_UI_MODULE
243#endif
244 }
245
246 //!NOTE: There are other commands, but these are only the ones that require a action.
247 //!TODO: make these "events" on the setting of those values. eg. register for stepperValue true or false, etc.. They would be invoked from the 'savePreferencesBoolean' call.
248}
249//void FeederStateMachine() {
250
251//!the main loop fro the StepperModule. This used be called FeederStateMachine()
253{
254 //! 3.24.25 warm rain all day.. no ski
255 //! call the loop (which might have a timer to stop a motor)
258 // FeedState is NO LONGER public and can be set by BLE client via the GATT for the characteristic
259
260 switch (_feedState)
261 {
262 case SINGLE_FEED:
263 {
264#ifdef USE_UI_MODULE
266#endif
267
268#pragma mark USE 2FEED FLAG
269 //! 9.2.25 back from LA, Nice here. Sunday off to Europe
270 //! try 2 feeds right now...
271 //! 9.3.25 Dead movie..
272 //! get the preference
274 int numFeeds = 1;
275 if (feed2times)
276 numFeeds = 2;
277 for (int i=0; i< numFeeds; i++)
278 {
279 SerialDebug.printf("SINGLE Feed %d of %d\n", i+1,numFeeds); //this should write a 0x01 for feed ack
280
281 //! uses current motor class instance
284
285 //! 9.4.25 seems the DCMotor is still running when it get's here.. and the next loop doesn't do anything...
286 //! so maybe a delay (if a DCMotor) -- if this works, it would be a class-wide function (get delay amount?)
287
288 //! cancel the feed..
289 _feedState = FEED_STOPPED; //this will cancel Continous feed if it is set
290
291 //! 5.3.24 Issue #332 if TUMBLER .. reverse direction
293 {
294 //! 8.2.24 have the auto a mode as well.. for older Tumblers. Default is ON (so we can change the old ones),
295 //! message: {"set":"autoMotorDirection","val":"true"}
297 if (autoMotorDirection)
298 {
299 // reverse direction, the Q command
300 //! note: reboot not needed as the next time a feed happens, it reads this value
301 // motor direction == (reverse)
303 currentDirection = !currentDirection;
305 }
306 }
307#ifdef NOT_WORKING
308 //! 9.4.25
309 //! NOTE: THis isn't working .. since the delay is already running ..
310 //! we need to get ahold of the stop_MotorStepper..
311 //! try the delay, mainly for the DCMotor
312 int delayAmount = _whichMotorStepper->delayAmountBetweenMotor();
313 //SerialTemp.printf("delayAmount = %d\n", delayAmount);
314 if (i > 0 && delayAmount > 0)
315 {
316 SerialDebug.printf("Delay %d\n", delayAmount);
317 delay(delayAmount);
318 }
319#endif
320 }
321 break;
322 }
323
324 //NOT USED..
325 case JACKPOT_FEED: //note: the only difference in this case is the FEED_STOPPED does NOT get set
326 {
327 if (_feedCount > 0) {
328 unsigned long feedCurrentMillis = millis();
329 if (feedCurrentMillis - feedPreviousMillis >= FEED_INTERVAL) { // changed this to check a timer to see when to fire the feeder
330#ifdef USE_UI_MODULE
331 blinkLED_UIModule(); // Moved 021622 WJL
332#endif
333
336
337 SerialDebug.println("JACKPOT Feed");
338 _feedCount--;
339 feedPreviousMillis = feedCurrentMillis;
340 }
341 }
342 else {
343 _feedCount = _feedsPerJackpot; //reset counter after all feeds for Jackpot completed
345 }
346 break;
347 }
348 case FEED_STOPPED:
349 {
350 //SerialDebug.println("Feed Stopped -- ");
351 //do nothing
352 break;
353 }
354 default:
355 {
357 break;
358 }
359 }
360}
361
int getFeederType_mainModule()
get the feeder type (Sepper 1,2,3 ...)
Definition: MainModule.cpp:730
void savePreferenceBoolean_mainModule(int preferenceID, boolean flag)
save a boolean preference
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...
char * getPreference_mainModule(int preferenceID)
#define PREFERENCE_SENSOR_PLUGS_SETTING
#define PREFERENCE_STEPPER_BUZZER_VALUE
stepper preferences
#define STEPPER_IS_TUMBLER
#define PREFERENCE_STEPPER_2FEED_SETTING
#define PREFERENCE_STEPPER_AUTO_MOTOR_DIRECTION_SETTING
#define PREFERENCE_STEPPER_CLOCKWISE_MOTOR_DIRECTION_SETTING
#define AUTO_FEED
void setup_StepperClasses()
#define NUM_STEPPER_CLASS
MAKE SURE THIS IS UPDATED...
void loop_StepperModule()
the main loop fro the StepperModule. This used be called FeederStateMachine()
SG90ServoStepperClass * _SG90ServoStepperClass
char * stepperIdentity_StepperModule()
ULN2003_StepperClass * _ULN2003_StepperClass
#define FEED_STOPPED
MotorStepperClassType * _motorSteppers[NUM_STEPPER_CLASS]
3.31.25 create array of plugs
unsigned long feedPreviousMillis
#define JACKPOT_FEED
void setup_StepperModule()
called on setup()
#define SINGLE_FEED
int getFeederType()
retreives the feeder type (versus grabbing a global variable)
void stepperModule_ProcessClientCmdFinal(char cmd)
the Blink the LED - and it will use the latest BUZZER status (so MQTT could set buzzer on....
int _feedsPerJackpot
HDriverStepperClass * _HDriverStepperClass
3
int _feedCount
void stepperModule_savePreference(int preferenceID, String preferenceValue)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
#define FEED_INTERVAL
MotorStepperClassType * _whichMotorStepper
use this one...
int _feedState
L9110S_DCStepperClass * _L9110S_DCStepperClass
boolean getBuzzStatus_StepperModule()
returns the buzzer status
DCMotorStepperClass * _DCMotorStepperClass
3.16.25 using a class version (thisi is for ESP_32 and ESP_M5 now)
PTStepperClass * _PTStepperClass
2
unsigned long millis()
Definition: TinyGPS.cpp:35
void setBuzzerLight_UIModule(boolean onFlag)
UI specific actions.
Definition: UI.cpp:17
void blinkLED_UIModule()
blink the LED
Definition: UI.cpp:23
An concrete class.
An concrete class.
An mostly virtual class.
virtual void loop_MotorStepper()=0
loop the PTStepper (so timer can run)
virtual void setup_MotorStepper()=0
setup the PTStepper
virtual void start_MotorStepper()=0
An concrete class.
An concrete class.