TUDelft-CNS-ATM / bluesky

The open source air traffic simulator
GNU General Public License v3.0
342 stars 239 forks source link

Updating not happening on init_plugin function #453

Closed leonardocfor closed 1 year ago

leonardocfor commented 1 year ago

Hi guys,

I am writing a plugin and I will like to launch the timed_function update to be triggered at the initialization of the Entity class, e.g.

class TestPlugin(Entity):

    """Test plugin"""

    def __init__(self):
        # Initializing parent class bluesky.core Entity
        super().__init__()
        self.update()

   @timed_function(name="TESTPLUGIN", dt=1)
   def update(self):
        """Updating function"""
       print("Updating ...")

This is a snippet of the plugin but the instruction print("Updating ...") is only executed when a command is sent, e.g. creating an aircraft or else and not in the initialization of the plugin. So I was wondering if you can provide an alternative to kickoff the updating process at object initilization.

I appreciate your help. Thank you

Ellislee1 commented 1 year ago

@leonardocfor I came across this and thought I'd have a look at it. I can get updating to run with the following code. The text appears before the Successfully loaded plugin TEST. This also sits in a file ./plugins/test.py. I think as well you need to make sure the function init_plugin() is configured properly. I copied this using the plugins that come with BlueSky to generate this MWE. Hope this helps.

""" BlueSky plugin template. The text you put here will be visible
    in BlueSky as the description of your plugin. """
from bluesky import core, stack, traf  #, settings, navdb, sim, scr, tools

### Initialization function of your plugin. Do not change the name of this
### function, as it is the way BlueSky recognises this file as a plugin.
def init_plugin():
    ''' Plugin initialisation function. '''
    # Instantiate our example entity
    test = Test()

    # Configuration parameters
    config = {
        # The name of your plugin
        'plugin_name':     'TEST',

        # The type of this plugin. For now, only simulation plugins are possible.
        'plugin_type':     'sim',
        }

    # init_plugin() should always return a configuration dict.
    return config

class Test(core.Entity):
    ''' Example new entity object for BlueSky. '''
    def __init__(self):
        super().__init__()
        self.update()

    # Functions that need to be called periodically can be indicated to BlueSky
    # with the timed_function decorator
    @core.timed_function(name='test', dt=5)
    def update(self):
        """Updating function"""
        print("Updating ...")

image

jooste commented 1 year ago

Hi @leonardocfor,

To add to @Ellislee1 's answer: Don't forget to instantiate your Test class (typically in the init_plugin function), and note that periodic functions are only triggered when the simulation is in Operate mode (so not in Hold, which is what you have when you start up BlueSky).

leonardocfor commented 1 year ago

Hi @Ellislee1 and @jooste,

Thank you for your prompt reply. I confirm that the update function is executed upon the class initialization. However, to your comment @jooste, how can I force the simulation to go to Operate mode? i.e. without having to send a command or else to kick off the simulation. I ask this because we need to allow constant checking of a remote API in the update function.

jooste commented 1 year ago

You could do the following:

from bluesky import sim

def init_plugin():
    ...
    sim.op()