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 //! the motorStepper can be nil
165 //_whichMotorStepper = NULL;
166 //! 5.2.25 default to HDriver..
167 //_whichMotorStepper = _HDriverStepperClass;
168
169 //! 7.31.25 use default..
170 //! 10.26.25 No Power day .. wind storm
171 //! if NULL then do't have a default..
172 //_whichMotorStepper = _L9110S_DCStepperClass;
173#endif
174 }
175
176#define SETUP_FIRST_TIME
177 //!TODO: task on the pins .. to the definition
178 //!and make it work backward compatible..
179#ifdef SETUP_FIRST_TIME
182#endif
183
184}
185//! 4.1.25 April Fools day. Mt Peak hike (162 bmp strange spike)
186//! get the identity of the SenasorPlug
187//! nil if non
189{
190 char *identity = NULL;
192 {
193 identity = _whichMotorStepper->classIdentity();
194 }
195 return identity;
196}
197//TODO: this isn't directly called.. but called from the main processsClientCommand(String cmd)
198////Keep ProcessClientCmd short to let the callback run. instead change the feeder state flag
200 SerialLots.printf("***** ProcessClientCmd(%c) from client*****\n", cmd);
201
202 if ((cmd == 0x00) || (cmd == 's') || (cmd == 'c'))
203 {
204 SerialLots.println("Setting FeedState = SINGLE_FEED");
205 _feedState = SINGLE_FEED; // Gen 3 Pet Tutor used 0x00 to feed so 0x00 is to make it backward compatible 's' is the new version
206 //not used .. for awhile.. So let's stop writing to EPROM all the time.
207 }
208 else if (cmd == 'a')
209 {
210 SerialLots.println("Setting FeedState = AUTO_FEED");
212
213 }
214 else if (cmd == 'j')
215 {
216 SerialLots.println("Setting FeedState = JACKPOT_FEED");
218
219 }
220 else if (cmd == 'B')
221 {
222 SerialLots.println("1.Setting buzzStatus = BUZZON");
223
224 //set the flag, then buz.. (or don't care, and the value is stored in mainModule.. TODO: maybe that should cache the result)
225#ifdef USE_UI_MODULE
226 //make a buzz sound..
229#endif
230
231 }
232 else if (cmd == 'b')
233 {
234 SerialLots.println("2.Setting buzzStatus = BUZZOFF");
235#ifdef USE_UI_MODULE
237#endif
238 }
239
240 //!NOTE: There are other commands, but these are only the ones that require a action.
241 //!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.
242}
243//void FeederStateMachine() {
244
245//!the main loop fro the StepperModule. This used be called FeederStateMachine()
247{
248 //! 3.24.25 warm rain all day.. no ski
249 //! call the loop (which might have a timer to stop a motor)
252 // FeedState is NO LONGER public and can be set by BLE client via the GATT for the characteristic
253
254 switch (_feedState)
255 {
256 case SINGLE_FEED:
257 {
258#ifdef USE_UI_MODULE
260#endif
261
262#pragma mark USE 2FEED FLAG
263 //! 9.2.25 back from LA, Nice here. Sunday off to Europe
264 //! try 2 feeds right now...
265 //! 9.3.25 Dead movie..
266 //! get the preference
268 int numFeeds = 1;
269 if (feed2times)
270 numFeeds = 2;
271 for (int i=0; i< numFeeds; i++)
272 {
273 SerialDebug.printf("SINGLE Feed %d of %d\n", i+1,numFeeds); //this should write a 0x01 for feed ack
274
275 //! uses current motor class instance
278
279 //! 9.4.25 seems the DCMotor is still running when it get's here.. and the next loop doesn't do anything...
280 //! so maybe a delay (if a DCMotor) -- if this works, it would be a class-wide function (get delay amount?)
281
282 //! cancel the feed..
283 _feedState = FEED_STOPPED; //this will cancel Continous feed if it is set
284
285 //! 5.3.24 Issue #332 if TUMBLER .. reverse direction
287 {
288 //! 8.2.24 have the auto a mode as well.. for older Tumblers. Default is ON (so we can change the old ones),
289 //! message: {"set":"autoMotorDirection","val":"true"}
291 if (autoMotorDirection)
292 {
293 // reverse direction, the Q command
294 //! note: reboot not needed as the next time a feed happens, it reads this value
295 // motor direction == (reverse)
297 currentDirection = !currentDirection;
299 }
300 }
301#ifdef NOT_WORKING
302 //! 9.4.25
303 //! NOTE: THis isn't working .. since the delay is already running ..
304 //! we need to get ahold of the stop_MotorStepper..
305 //! try the delay, mainly for the DCMotor
306 int delayAmount = _whichMotorStepper->delayAmountBetweenMotor();
307 //SerialTemp.printf("delayAmount = %d\n", delayAmount);
308 if (i > 0 && delayAmount > 0)
309 {
310 SerialDebug.printf("Delay %d\n", delayAmount);
311 delay(delayAmount);
312 }
313#endif
314 }
315 break;
316 }
317
318 //NOT USED..
319 case JACKPOT_FEED: //note: the only difference in this case is the FEED_STOPPED does NOT get set
320 {
321 if (_feedCount > 0) {
322 unsigned long feedCurrentMillis = millis();
323 if (feedCurrentMillis - feedPreviousMillis >= FEED_INTERVAL) { // changed this to check a timer to see when to fire the feeder
324#ifdef USE_UI_MODULE
325 blinkLED_UIModule(); // Moved 021622 WJL
326#endif
327
330
331 SerialDebug.println("JACKPOT Feed");
332 _feedCount--;
333 feedPreviousMillis = feedCurrentMillis;
334 }
335 }
336 else {
337 _feedCount = _feedsPerJackpot; //reset counter after all feeds for Jackpot completed
339 }
340 break;
341 }
342 case FEED_STOPPED:
343 {
344 //SerialDebug.println("Feed Stopped -- ");
345 //do nothing
346 break;
347 }
348 default:
349 {
351 break;
352 }
353 }
354}
355
int getFeederType_mainModule()
get the feeder type (Sepper 1,2,3 ...)
Definition: MainModule.cpp:649
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.