Not restarting

I got the netconsole working, so I can now see the output while the code is running.

I am using the “simple” example as a starting point, but the restart functionality is not working properly. When I hit the button for restart, I see the exception output on the netconsole, but the code is not restarted, it acts like it is just hung up.

Is there something special I need to do to make the code restart once an exception is raised?

The example is outdated, the boot.py no longer reloads the code when an uncaught exception occurs. You can type ‘reboot’ at the NetConsole to reset the robot.

Ah. Ok. Now that I re-read the 2012.1 announcement I see the note about restarting.

So, if I change the RuntimeError exception to SystemRestart, then the interpreter should be restarted – or at least it should try to. There may be other issues around that, but for testing it may be usable.

Is that the same as pressing the reset button on the cRio or is it faster?

I haven’t had a chance to test this on the robot, but given the warnings around the SystemRestart exception (“This is still not all that reliable…”), I wonder if using Python’s own reload() would be better?

This works on my local machine…any reason not to do something like this? Also, anyone have an idea how to improve it?

robot.py:

from my_exceptions import RestartException
import real_code

while True:
	try:
		reload(real_code)
		real_code.go()
	except RestartException:
		print "Reloading and restarting..."
		pass

my_exceptions.py:

class RestartException(Exception): pass

real_code.py:

from my_exceptions import RestartException

import sys,os,re,time

# decide if we need to reload based on some signal, in this case a file existing 
which we delete
# on the robot, this would be a joystick check
def check_restart():
	if os.path.exists("do_reload"):
		os.unlink("do_reload")
		raise RestartException

def go():
	while True:
		check_restart()
		print "Test."
		time.sleep(1)

With the above, if I have it running in one window, edit real_code.py, then create “do_reload”, the real_code.py is reloaded and restarted at the next tick.