davesnowdon / naoutil

A project containing useful utility code for NAO developers (mostly in python). Includes support for i18n and JSON serialisation of arbitrary classes
GNU Lesser General Public License v3.0
9 stars 6 forks source link

Memory Callbacks #20

Closed dnajd closed 11 years ago

dnajd commented 11 years ago

I was trying to use the memory callbacks and even with the README.md example you have:

 def myEventCallback(dataName, value, message):
      nao.naoscript.get(32)
 memory.subscribeToEvent('RightBumperPressed', myEventCallback)
 raw_input("Press ENTER to stop subscribing to RightBumperPressed\n")
 memory.unsubscribeToEvent('RightBumperPressed')

I get this error:

memory.subscribeToEvent('RightBumperPressed', myEventCallback) Exception TypeError: "in method 'modulegetName', argument 1 of type 'AL::module '" in <bound method _SubscriberModule.del of <naoutil.memory._SubscriberModule; >> ignored Traceback (most recent call last): File "", line 1, in File "/home/dnajd/development/personal/FluentNao/src/main/python/naoutil/memory.py", line 58, in subscribeToEvent _SubscriberModule().subscribeToEvent(dataName, callback) File "/home/dnajd/development/personal/FluentNao/src/main/python/naoutil/memory.py", line 20, in getinstance instance = cls() # Keep a strong ref until it is returned File "/home/dnajd/development/personal/FluentNao/src/main/python/naoutil/memory.py", line 28, in init ALModule.init(self) File "/home/dnajd/development/personal/FluentNao/src/main/python/naoutil/init.py", line 31, in init _ALModule.init(self, self.moduleName) File "/home/dnajd/development/opensource/pynaoqi-python-2.7-naoqi-1.14-linux64/naoqi.py", line 133, in init inaoqi.module.init(self, param) File "/home/dnajd/development/opensource/pynaoqi-python-2.7-naoqi-1.14-linux64/inaoqi.py", line 217, in init this = _inaoqi.new_module(_args) RuntimeError: module::module Could not create a module, as there is no current broker in Python's world.

dnajd commented 11 years ago

So I didn't notice that this memory module example states

"Run inside a behavior environment (ie. choregraphe box) or inside a self created broker."

This makes it more difficult to use in naoscript or in a interactive python shell I think. Can we just pass naoenv to this and have it work off that?

AxelVoitier commented 11 years ago

In an interactive python shell I can understand. Although with naoutil.broker module it is not that difficult anymore. Run your code like:

with naoutil.broker.create('naoscript'):
    your_code()

or

naoscript_broker = naoutil.broker.Broker('naoscript')
your_code()
naoscript_broker.shutdown()

But in naoscript I don't see why it's a problem? You kind of have to run your naoscript executer somewhere. If it is executed in a behaviour (like the .crg file available in naoscript project page) it should work, no? How are you running your naoscript executer then?

Passing a naoenv will do nothing. Naoenv does not ensure it runs in a broker or a behaviour actally.

davesnowdon commented 11 years ago

I'm going to close this issue since, as Axel says it's trivial to create a broker.

AxelVoitier commented 11 years ago

I just saw you actually solved the issue yourself here: https://github.com/dnajd/FluentNao/blob/master/bootstrap.py#L10

:)

Two comments though: