Skip to content

Commit

Permalink
[hla] Add a configurable timestep parameter
Browse files Browse the repository at this point in the history
This parameter makes only sense if the simulation does not use blender
internal physics.
  • Loading branch information
Arnaud Degroote committed May 13, 2015
1 parent 3d0a25c commit e3ca3f1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
8 changes: 7 additions & 1 deletion doc/morse/user/advanced_tutorials/hla_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ Now, we need to connect this stuff to the HLA world. We use the
env = Environment('empty')
env.configure_stream_manager(
'hla',
fom = 'Test.fed', name = 'Morse', federation = 'Test', sync_point = 'Init', time_sync = True)
fom = 'Test.fed', name = 'Morse', federation = 'Test', sync_point
= 'Init', time_sync = True, timestep = 1.0)
ground = bpymorse.get_object('Ground')
ground.scale = [255.0, 55.0, 0.0065]
Expand All @@ -99,6 +100,11 @@ Now, we need to connect this stuff to the HLA world. We use the
The parameters in ``configure_stream_manager`` are really important, see
:doc:`the hla middleware documentation <../middlewares/hla>` for a complete description.

.. note::

You can play with the timestep value to see how it interacts with other
simulators

.. note::

The ``ground`` and ``env`` configuration here is not very important, but
Expand Down
3 changes: 3 additions & 0 deletions doc/morse/user/middlewares/hla.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ The following variables permit to configure HLA behaviour:
pressing :kbd:`Enter`. The default is False.
- **time_sync**: Optional : a boolean indicating if the simulation is in
'Best-effort' mode or synchronised by HLA. The default value is False.
- **timestep**: Optional : a float indicating how much logic time elapse each
game loop. **Do not set it if you use physics from Blender (or sensors
relying on Blender time !**.


You need to pass them to the ``configure_stream_manager`` method in the
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorials/tutorial_hla.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

env.configure_stream_manager(
'hla',
fom = 'Test.fed', name = 'Morse', federation = 'Test', sync_point = 'Init', time_sync = True)
fom = 'Test.fed', name = 'Morse', federation = 'Test', sync_point = 'Init', time_sync = True, timestep = 1.0)

ground = bpymorse.get_object('Ground')
ground.scale = [255.0, 55.0, 0.0065]
Expand Down
23 changes: 14 additions & 9 deletions src/morse/middleware/hla_datastream.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from morse.core import blenderapi

class MorseBaseAmbassador(rti.FederateAmbassador):
def __init__(self, rtia, federation, time_sync):
def __init__(self, rtia, federation, time_sync, timestep):
self._rtia = rtia
self.federation = federation
self._time_sync = time_sync
Expand All @@ -21,24 +21,27 @@ def __init__(self, rtia, federation, time_sync):

self._attributes_values = {} # obj_name -> { attr_handle -> value }

self.timestep = timestep

def initialize_time_regulation(self):
self.logical_time = self._rtia.queryFederateTime()
logger.debug("federation %s time %f" % (self.federation, self.logical_time))
logger.debug("federation %s time %f timestep %f" %
(self.federation, self.logical_time, self.timestep))

self.constraint_enabled = False
self.regulator_enabled = False
self.granted = False
self.lookahead = 1.0
self.lookahead = self.timestep

self._rtia.enableTimeConstrained()
self._rtia.enableTimeRegulation(self.logical_time, self.lookahead)
self._rtia.enableTimeRegulation(self.logical_time, self.timestep)
while not (self.constraint_enabled and self.regulator_enabled):
self._rtia.tick(0, self.lookahead)

def advance_time(self):
if self._time_sync:
self.granted = False
self._rtia.timeAdvanceRequest(self.logical_time + self.lookahead)
self._rtia.timeAdvanceRequest(self.logical_time + self.timestep)
while not self.granted:
self._rtia.tick(0, self.lookahead)
else:
Expand Down Expand Up @@ -112,7 +115,7 @@ def get_attributes(self, obj_name):
def update_attribute(self, obj_handle, value):
if self._time_sync:
self._rtia.updateAttributeValues(obj_handle, value, "morse_update",
self.logical_time + self.lookahead)
self.logical_time + self.timestep)
else:
self._rtia.updateAttributeValues(obj_handle, value, "morse_update")

Expand Down Expand Up @@ -160,7 +163,8 @@ def federationSynchronized(self, label):


class HLABaseNode:
def __init__(self, klass, fom, node_name, federation, sync_point, sync_register, time_sync):
def __init__(self, klass, fom, node_name, federation, sync_point,
sync_register, time_sync, timestep):
"""
Initializes HLA (connection to RTIg, FOM file, publish robots...)
"""
Expand Down Expand Up @@ -188,7 +192,7 @@ def __init__(self, klass, fom, node_name, federation, sync_point, sync_register,
"Please check the '.fed' file syntax.")
raise
logger.debug("Creating MorseAmbassador...")
self.morse_ambassador = klass(self.rtia, federation, time_sync)
self.morse_ambassador = klass(self.rtia, federation, time_sync, timestep)
try:
self.rtia.joinFederationExecution(node_name,
federation, self.morse_ambassador)
Expand Down Expand Up @@ -253,9 +257,10 @@ def __init__(self, args, kwargs):
sync_point = kwargs.get("sync_point", None)
sync_register = kwargs.get("sync_register", False)
time_sync = kwargs.get("time_sync", False)
timestep = kwargs.get("timestep", 1.0 / blenderapi.getfrequency())

self.node = HLABaseNode(MorseBaseAmbassador, fom, node_name,
federation, sync_point, sync_register, time_sync)
federation, sync_point, sync_register, time_sync, timestep)
except KeyError as error:
logger.error("One of [fom, name, federation] attribute is not configured: "
"Cannot create HLADatastreamManager")
Expand Down
2 changes: 1 addition & 1 deletion src/morse/multinode/hla.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def initialize(self):
os.environ["CERTI_TCP_PORT"] = str(self.port)

self.node = HLABaseNode(MorseAmbassador, self.fom, self.node_name,
self.federation, None, self.time_sync)
self.federation, None, self.time_sync, 1.0)

self.node.morse_ambassador.initialize()

Expand Down

0 comments on commit e3ca3f1

Please sign in to comment.