WebLVC 1.8 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DDS Talk/Listen Example

The talkListenDds example is two separate applications: one which publishes a simple DDS entity, and one which listens for published DDS entities.

This is a standalone example that can be run independently from WebLVC, and serves to illustrate the procedures involved in publishing and reflecting DDS objects. These applications can be used in conjuction with the DDS broker for testing or development.


talk.cxx

/****************************************************************************
* Copyright (c) 2016 MAK Technologies, Inc.
* All rights reserved.
****************************************************************************/
#include <vl/entityStateRepository.h>
#include <vl/topoView.h>
#include <vlutil/vlProcessControl.h>
#include <vlutil/vlMiniDumper.h>
#include <vlutil/vlKeyboard.h>
#include <iostream>
void updateLocationOrientation( DtTime t, DtTopoView& view );
using namespace MAKVrExchange::DtDdsBroker;
int main( int argc, char* argv[] )
{
DtINIT_MINIDUMPER( "DDS Talk" );
try
{
// Get the partition name from the command line.
DtString partitionName = "VR-Exchange";
if ( argc > 1 )
{
partitionName = argv[1];
}
// Set the partition name in the initializer.
appInit.setPartitionName( partitionName );
// 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.
DtDdsDomainConnection exConn( appInit );
// Initialize VR-Link time.
DtClock* clock = exConn.clock();
// Entity type.
DtEntityType type( "1:2:225:50:4:1:0" );
// Instantiate an entity publisher.
DtEntityPublisher publisher( &exConn, "Predator_UAV" );
DtEntityStateRepository* entSr = publisher.esr();
entSr->setGlobalId( DtEntityIdentifier(1,2,3) );
entSr->setEntityId( DtEntityIdentifier(1,2,3) );
entSr->setMarkingText( "Predator_UAV" );
entSr->setForceId( DtForceFriendly );
entSr->setEntityType( type );
entSr->setGuise( type );
// Create a topo view for updating the entity's location, and initialize it.
DtTopoView topoView( entSr, DtDeg2Rad(35.699760), DtDeg2Rad(-121.326577) );
updateLocationOrientation( clock->elapsedRealTime(), topoView );
while ( true )
{
// Check if user hit 'q' to quit.
char* keyPtr = DtPollBlockingInputLine();
if ( keyPtr && ( *keyPtr == 'q' || *keyPtr == 'Q' ) )
{
break;
}
// Tell VR-Link the current value of simulation time.
clock->setSimTime( clock->elapsedRealTime() );
// Update the aircraft's location and orientation.
updateLocationOrientation( clock->elapsedRealTime(), topoView );
// Process any incoming messages.
exConn.drainInput();
// Tick the publisher.
publisher.tick();
// Sleep till next iteration.
DtSleep( 0.1 );
}
}
DtCATCH_AND_WARN( std::cout );
return 0;
}
void updateLocationOrientation( DtTime t, DtTopoView& view )
{
// Orbit constants.
const float radius = 200; // Radius of the orbit.
const float period = 20.0; // Time to do a full orbit.
const float speed = M_2PI*radius/period; // Approx speed the aircraft is moving.
// Intermediate variables.
float angle = t*M_2PI/period; // Angle around orbit.
float advAngle = angle + M_PI_2; // Angle + Pi/2 around orbit.
// Update the location/velocity/orientation.
view.setLocation( DtVector64(200*cos(angle),-200*sin(angle),-1230.0) );
view.setVelocity( DtVector32(speed*cos(advAngle),speed*sin(advAngle),-1230.0) );
view.setOrientation( DtTaitBryan(-advAngle,0,DtDeg2Rad(-15)) );
}

listen.cxx

/****************************************************************************
* Copyright (c) 2016 MAK Technologies, Inc.
* All rights reserved.
****************************************************************************/
#include <vl/entityStateRepository.h>
#include <vlutil/vlProcessControl.h>
#include <vlutil/vlMiniDumper.h>
#include <vlutil/vlKeyboard.h>
#include <iostream>
using namespace MAKVrExchange::DtDdsBroker;
void discoverObject( void* refObj, void* /*usr*/ );
void updateObject( void* refObj, void* /*usr*/ );
void removeObject( void* refObj, void* /*usr*/ );
int main( int argc, char* argv[] )
{
DtINIT_MINIDUMPER( "DDS Listen" );
try
{
// Get the partition name from the command line.
DtString partitionName = "VR-Exchange";
if ( argc > 1 )
{
partitionName = argv[1];
}
// Set the partition name in the initializer.
appInit.setPartitionName( partitionName );
// 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.
DtDdsDomainConnection exConn( appInit );
// Subscribe for object discovery and removal.
exConn.addEntityAdditionCallback( discoverObject, 0 );
exConn.addEntityRemovalCallback( removeObject, 0 );
// Initialize VR-Link time.
DtClock* clock = exConn.clock();
while ( true )
{
// Check if user hit 'q' to quit.
char* keyPtr = DtPollBlockingInputLine();
if ( keyPtr && ( *keyPtr == 'q' || *keyPtr == 'Q' ) )
{
break;
}
// Tell VR-Link the current value of simulation time.
clock->setSimTime( clock->elapsedRealTime() );
// Process any incoming messages.
exConn.drainInput();
// Sleep till next iteration.
DtSleep( 0.1 );
}
}
DtCATCH_AND_WARN( std::cout );
return 0;
}
void discoverObject( void* refObj, void* /*usr*/ )
{
DtReflectedEntity* entity = (DtReflectedEntity*) refObj;
std::cout << "Discovered new Entity:" << std::endl;
std::cout << *entity->esr() << std::endl;
std::cout << std::endl;
// Subscribe to further updates to the object.
entity->addPostUpdateCallback( updateObject, 0 );
}
void updateObject( void* refObj, void* /*usr*/ )
{
DtReflectedEntity* entity = (DtReflectedEntity*) refObj;
std::cout << "Received Entity Update:" << std::endl;
std::cout << *entity->esr() << std::endl;
std::cout << std::endl;
}
void removeObject( void* refObj, void* /*usr*/ )
{
DtReflectedEntity* entity = (DtReflectedEntity*) refObj;
std::cout << "Entity Removed:" << std::endl;
std::cout << *entity->esr() << std::endl;
std::cout << std::endl;
}

Document ID: Generated on Wed Mar 12 15:05:17 EDT 2025 from SVN revision 274272
Copyright © 2005-2025 MAK Technologies. All Rights Reserved (www.mak.com)