VR-Link API Documentation for HLA 1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Talk Example

Talk is a minimal VR-Link application.

The program simulates the flight of an F18 aircraft. The program begins by sending a fire PDU or HLA fire interaction. Then, the F18 flies north for 10 seconds, updating its position by sending DIS entity state PDUs or HLA attribute updates.

This example is an extremely simplified version of the F18 example. It publishes a stripped down aircraft. The published data can be viewed by the Listen example.

This example builds and runs in DIS, HLA1.3, HLA1516 and HLA1516e, because we use the VR-Link Protocol Independent API.

Connecting to An Exercise

Like the listen-only example, this program creates a DtExerciseConn to provide an interface to the RTI or DIS network.

Managing Entities

We define the entity type the F18 will use.

To be visible to other applications in the exercise, each locally-simulated entity requires a DtEntityPublisher. The entity publisher manages the generation of messages for this particular entity. It provides an entity state repository where you can set state values, and a tick() function, which causes state information to be sent to the network if necessary.

It sets up a pointer to the entity state repository, then creates a topographic view on that repository. This lets us set the entity's positional data using topographic coordinates, rather than the default geocentric coordinates. (The coordinates are hard-coded for this example.) We show an example of storing a non-positional state value in the repository.

Sending Interactions

The example sends an interaction. You can send the interaction using the exercise connection's sendStamped() function.

You can use sendStamped() to send state updates, but it is preferable to let the entity publisher send state updates for you, using data in the entity state repository.

Sending State Messages

The main loop executes twenty times per second for ten seconds. As in the listen-only example, this program sets VR-Link simulation time at the start of each iteration.

While this program's main purpose is to demonstrate the sending of data to the network, it is not a true send-only application. Incoming data is also processed with the drainInput() call. This call is required for HLA, because this is where we tick the RTI.

The program updates the F18's positional data in its entity state repositoryentity state repository and ticks the entity publisher to send the updated data onto the network. It sets topographic coordinates through a view, which VR-Link converts to geocentric coordinates.

Then, the only remaining tasks are to increment the F18's position, increment the simulation time, and sleep until it is time to begin the next iteration.


/****************************************************************************
* Copyright (c) 2014 VT MAK
* All rights reserved.
****************************************************************************/
#include <vl/topoView.h>
#include <iostream>
void DtRtiShutdownHandler(const char * label, void* finished)
{
//This callback may occur multiple times, since it gets called
//every time we make an RTI call with an error. So, we mark this
//as finished and make sure that we only signal once.
int* finishedInt = (int*)finished;
if (*finishedInt)
{
DtWarn << "Shutting down: " << label << std::endl;
}
*finishedInt = 0; //Order a shutdown;
}
int main( int argc, char* argv[] )
{
// Used for error handling
DtINIT_MINIDUMPER( "Talk" );
try
{
// Create a connection to the exercise or federation execution.
DtVrlApplicationInitializer appInit( argc, argv, "VR-Link Talk" );
#if DtDIS
// Change some defaults. Please note that this is DIS specific code.
// Notice how this is the only line of DIS specific code in this example,
// everything else is using the protocol independent API.
appInit.setUseAsynchIO(true);
appInit.setDisVersionToSend(7);
#else
appInit.setFedFileName(DtDefaultRpr2Fom);
appInit.setExecName("MAK-RPR-2.0");
appInit.setRprFomVersion(2.0);
#endif
// Process the command line. This will actually set values in the
// DtVrlApplicationInitializer class and should be called before
// creating the DtExerciseConn instance.
appInit.parseCmdLine();
// The DtExerciseConn instance is perhaps the most important class
// in any VR-Link exercise. Think of it as the hub which holds
// everything else together. Technically this instance is what's
// responsible for connecting this federate to another federate via
// a network, or an RTI. After exConn is created, and if no error
// has occurred this simulator will be a live federate.
DtExerciseConn exConn(appInit, &status);
if (status != 0)
{
std::cout << "Error creating exercise connection." << std::endl;
return -1;
}
// Create an entity publisher for the entity we are simulating.
DtEntityPublisher entityPub(f18Type, &exConn,
// Hold on to the entity's 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);
// We can use the ESR to set state.
esr->setMarkingText("VR-Link");
topoView.setOrientation(DtTaitBryan(0.0, 0.0, 0.0));
// Initialize VR-Link time.
DtClock* clock = exConn.clock();
DtVector position(0, 0, -100);
DtVector32 velocity(20, 0, 0);
//Shutdown handler in order to smoothly end if the HLA exercise shuts down
int forever = 1;
#ifdef DtHLA
exConn. addRtiErrorCb(&DtRtiShutdownHandler, &forever);
#endif
// Send a Fire Interaction.
fire.setAttackerId(entityPub.globalId());
exConn.sendStamped(fire);
// Main loop
DtTime dt = 0.05;
DtTime simTime = 0;
clock->init();
while (forever && simTime <= 10.0)
{
// Tell VR-Link the current value of simulation time.
clock->setSimTime(simTime);
// Process any incoming messages.
exConn.drainInput();
// Set the current position information.
topoView.setLocation(position);
topoView.setVelocity(velocity);
// Call tick, which insures that any data that needs to be
// updated is sent.
entityPub.tick();
// Set up for next iteration.
position[0] += velocity[0] * dt;
simTime += dt;
// Wait till real time equals simulation time of next step.
DtSleep(simTime - clock->elapsedRealTime());
}
}
DtCATCH_AND_WARN(std::cout);
return 0;
}

Document ID: Generated on Tue Mar 1 02:56:17 EST 2016 from SVN revision 162687
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)