VR-Link API Documentation for HLA 1.3
HLA Time Regulating Talk Example

This program is a time regulating version of TalkRPR.

It gets an initial time when time regulation is enabled and then advances for about 10 time units. Movement of the entity is not even. It Sends one interaction. Time regulation is turned off when the federate is ready to leave, this will allow time constrained federates to advance at that point. It is possible, and usually desirable to move time forward as the simulation progresses however we choose not to here.


//********************************************************************
// Copyright (c) 2006 MaK Technologies, Inc.
// All rights reserved.
//********************************************************************
#ifdef DtHLA

#include <vl/exerciseConn.h>
#include <vl/exConnInit.h>
#include <vl/topoView.h>
#include <vlutil/vlProcessControl.h>
#include <vlutil/vlKeyboard.h>
#include <vlpi/EntityTypes.h>
#include <vl/entityPub.h>
#include <vl/entitySR.h>
#include <vl/fireInter.h>
#include <vl/vrlFedAmb.h>

#if DtHLA_1516
   #if DtHLA_1516_EVOLVED
      #include <RTI/time/HLAfloat64Time.h>
      #include <RTI/time/HLAfloat64Interval.h>
   #else
      #include <RTI/logicalTimeImpl.h>
      #include <RTI/logicalTimeIntervalImpl.h>
   #endif
#else
   #include <fedtime.hh>
#endif


int keybrdTick();
#if DtHLA_1516
void timeAdvanceRequestCb(const RTI::LogicalTime &, void *);
void timeRegulationEnabledCb(const RTI::LogicalTime &, void *);
#else
void timeAdvanceRequestCb(const RTI::FedTime &, void *);
void timeRegulationEnabledCb(const RTI::FedTime &, void *);
#endif


// Globals
bool GlobalTimeRegOn(false);
bool GlobalTimeMoved(false);

#if DtHLA_1516
   #if DtHLA_1516_EVOLVED
      HLAfloat64Time GlobalFedTime;
      const HLAfloat64Interval LookAhead(2.0);
      const HLAfloat64Interval FedTimeAdvance(0.5);
   #else
      LogicalTimeImpl GlobalFedTime;
      const LogicalTimeIntervalImpl LookAhead(2.0);
      const LogicalTimeIntervalImpl FedTimeAdvance(0.5);
   #endif
#else
RTIfedTime GlobalFedTime;
const RTIfedTime LookAhead(2.0);
const RTIfedTime FedTimeAdvance(0.5);
#endif


int main(int argc, char **argv)
{

   DtVrlApplicationInitializer appInit(argc, argv, "VR-Link timeRegulator");

   appInit.parseCmdLine();
   DtExerciseConn exConn(appInit);

#if DtHLA_1516
   exConn.rtiAmb()->enableTimeRegulation(LookAhead);
#else
   exConn.rtiAmb()->enableTimeRegulation(GlobalFedTime,
                                         LookAhead);
#endif

   // Tell the Exercise Connection that all our messages should be
   // sent TSO. If we want to send a message non TSO, this can be turned on
   // and off throughout the life of the application.
   exConn.setSendFedTime(true);

   // Register for 
   // 1) time advance grants, to let us move time forward.
   exConn.fedAmb()->addTimeAdvanceGrantCb(timeAdvanceRequestCb, 0);
   // 2) time regulation turning on, because we cant start simulating until
   // it goes on.
   exConn.fedAmb()->addTimeRegulationEnabledCb(timeRegulationEnabledCb, 0);
   
   // We have requested Time regulation, as we don't know what time
   // will begin as, we need to wait before moving forward.
   while(!GlobalTimeRegOn)  
   {
      // Wait and query the RTI
      exConn.drainInput();
      DtSleep(0.05);
   }


   DtVector position(0, 0, 0);
   DtVector velocity(10, 0, 0);


   DtEntityType *f18Type = new DtEntityType(DtPlatform, DtPlatformDomainAir,
                              DtUnitedStates, DtFighter, DtF18, 0, 0);
      
   // Create an entity publisher for the entity we are simulating.
   DtEntityPublisher *entityPub = new DtEntityPublisher(*f18Type, &exConn,
                DtDrDrmRvw, DtForceFriendly,
                DtEntityPublisher::guiseSameAsType()); 

   // Hold on to its state repository, where we can set data.
   DtEntityStateRepository *esr = entityPub->entityStateRep();

   // Create a topographic view on the state repository, so we 
   // can set position information in topographic coordinates.
   double refLatitude  = DtDeg2Rad(  35.699760);
   double refLongitude = DtDeg2Rad(-121.326577);
   DtTopoView topoView(esr, refLatitude, refLongitude);
   topoView.setLocation(position);
   topoView.setVelocity(velocity);

   // We can use the ESR to set state.
   esr->setMarkingText("VR-Link");
#if DtHLA_1516
   DtTime dt = FedTimeAdvance.getInterval();
#else
   DtTime dt = FedTimeAdvance.getTime();
#endif

   // messages we send out will be tagged with the Sim Time. 
   // with TSO messages as a time regulator we make a contract
   // to send no messages earlier than globalFedTime + Lookahead.
   // Lookahead may by 0 however, this imposes stress on the RTI and
   // should be avoided.
#if DtHLA_1516
   DtTime simTime = GlobalFedTime.getTime() + LookAhead.getInterval() + dt;  
#else
   DtTime simTime = GlobalFedTime.getTime() + LookAhead.getTime() + dt;  
#endif

   // Initialize VR-Link time.
   exConn.clock()->setSimTime(simTime);  


   // Send a Fire Interaction.

   DtInfo << "\nSending Interaction at global Time: " 
       << (GlobalFedTime.getTime() + 
#if DtHLA_1516
       LookAhead.getInterval()
#else
       LookAhead.getTime()
#endif
       ) << "\n\n";


   DtFireInteraction fire;
   fire.setAttackerId(entityPub->globalId()); 
   exConn.sendStamped(fire);


   int updateCount = 0;

   DtTime lastTime = simTime + 10.0;
   

   while (simTime <= lastTime)
   {
      if (keybrdTick() == -1)
         break;

      if (GlobalTimeMoved)
      {
         GlobalTimeMoved = false;

         // Tell VR-Link the current value of simulation time.
         exConn.clock()->setSimTime(simTime);

         position[0] += velocity[0] * dt;
         position[1] = simTime;
         position[2] = updateCount++; 

         topoView.setLocation(position);
         topoView.setVelocity(velocity);

         entityPub->tick();

         // Print the position.
         // Since it returns a DtString, we need to force it to const char*
         // with a cast.
         // Print the fed time:

         DtInfo << " Fed Time:" << GlobalFedTime.getTime() 
            << " Sim Time:" << simTime 
            << " Pos: %s " 
            << topoView.location().string() << '\n'
            << "--------" << std::endl;

         simTime     += dt;

#if DtHLA_1516
#if DtHLA_1516_EVOLVED
         HLAfloat64Time reqTime;
#else
         LogicalTimeImpl reqTime;
#endif
         reqTime = GlobalFedTime.getTime() + FedTimeAdvance.getInterval();
         exConn.rtiAmb()->timeAdvanceRequest(reqTime);
         DtInfo << "requesting time: " 
            << (GlobalFedTime.getTime() + FedTimeAdvance.getInterval())
            << std::endl;
#else
         exConn.rtiAmb()->timeAdvanceRequest(GlobalFedTime + FedTimeAdvance);
         DtInfo << "requesting time: " 
            << (GlobalFedTime.getTime() + FedTimeAdvance.getTime())
            << std::endl;
#endif


      }

      
      // Suspend for longer and longer periods of time
      // but if we join late lets not make it obnoxious
      exConn.drainInput();
      DtSleep(((int)simTime%10) + 1);

   }  
   
   // Delete these items before you turn off time regulation. If
   // time reg goes off (remember, it takes affect right away) then
   // the deleteObject message will not have a time stamp and may be 
   // delivered BEFORE the discoverObject is issued.
   delete f18Type;
   delete entityPub; 

   
   // Disable time regulation. The RTI should be able to figure this out
   // but its nice to be polite.
   exConn.rtiAmb()->disableTimeRegulation();
 
 
      // Drain Input to work around a potential bug in the RTI where 
      // resigning from the Exercise will delete all objects with TSO
   exConn.drainInput();

  

   return 0;
}
    

int keybrdTick()
{
   char *keyPtr = DtPollInputLine();
   if (keyPtr && (*keyPtr == 'q' || *keyPtr == 'Q'))
      return -1;
   else
      return 0;
}

void timeAdvanceRequestCb(
#if DtHLA_1516
   const RTI::LogicalTime &aTime, 
#else
   const RTI::FedTime &aTime, 
#endif
   void *userData)
{
   GlobalFedTime = aTime;
   GlobalTimeMoved = true;

   DtInfo << "Time Advance Grant to: " 
        << GlobalFedTime.getTime() << std::endl;
   DtInfo.flush();

}

void timeRegulationEnabledCb(
#if DtHLA_1516
   const RTI::LogicalTime &aTime, 
#else
   const RTI::FedTime &aTime, 
#endif
   void *userData)
{
   GlobalFedTime = aTime;
   GlobalTimeRegOn = true;
   GlobalTimeMoved = true;

   DtInfo << "\nTime Regulation Enabled at time: " 
        << GlobalFedTime.getTime() << "\n\n";
   DtInfo.flush();
}


#else    // !DtHLA

#include <iostream>



int main()
{
   std::cout << "Time Management Is only available in HLA" << std::endl;;
   return 0;
}

#endif   // !DtHLA



Document ID: Generated on Mon May 14 08:06:18 EDT 2012 from SVN revision 114750
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)