Skip to content

Commit 1ba9260

Browse files
authored
Merge pull request #80 from twinters007/periodic
Add robotPeriodic and fix feedback in auto
2 parents f338fc3 + 3fca404 commit 1ba9260

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

magicbot/magicrobot.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,19 @@ def disabledPeriodic(self):
209209
self.logger.warning("Default MagicRobot.disabledPeriodic() method... Overload me!")
210210
func.firstRun = False
211211

212+
def robotPeriodic(self):
213+
"""
214+
Periodic code for all modes should go here.
215+
216+
` Users must override this method to utilize it
217+
but it is not required.
218+
219+
This function gets called last in each mode.
220+
You may use it for any code you need to run
221+
during all modes of the robot (e.g NetworkTables updates)
222+
"""
223+
pass
224+
212225
def onException(self, forceReport=False):
213226
'''
214227
This function must *only* be called when an unexpected exception
@@ -304,10 +317,9 @@ def autonomous(self):
304317
self._on_mode_enable_components()
305318

306319
self._automodes.run(self.control_loop_wait_time,
307-
self._execute_components,
320+
(self._execute_components, self._update_feedback, self.robotPeriodic),
308321
self.onException)
309322

310-
self._update_feedback()
311323
self._on_mode_disable_components()
312324

313325

@@ -343,6 +355,7 @@ def disabled(self):
343355
self.onException()
344356

345357
self._update_feedback()
358+
self.robotPeriodic()
346359
delay.wait()
347360

348361
def operatorControl(self):
@@ -380,6 +393,7 @@ def operatorControl(self):
380393

381394
self._execute_components()
382395
self._update_feedback()
396+
self.robotPeriodic()
383397

384398
delay.wait()
385399

robotpy_ext/autonomous/selector.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ def run(self, control_loop_wait_time=0.020, iter_fn=None, on_exception=None):
196196
197197
This function will NOT exit until autonomous mode has ended. If
198198
you need to execute code in all autonomous modes, pass a function
199-
as the ``iter_fn`` parameter, and it will be called once per
200-
autonomous mode iteration.
199+
or list of functions as the ``iter_fn`` parameter, and they will be
200+
called once per autonomous mode iteration.
201201
202202
:param control_loop_wait_time: Amount of time between iterations
203203
:param iter_fn: Called at the end of every iteration while
@@ -236,7 +236,11 @@ def run(self, control_loop_wait_time=0.020, iter_fn=None, on_exception=None):
236236
except:
237237
on_exception()
238238

239-
iter_fn()
239+
if isinstance(iter_fn, (list, tuple)):
240+
for fn in iter_fn:
241+
fn()
242+
else:
243+
iter_fn()
240244

241245
delay.wait()
242246

0 commit comments

Comments
 (0)