VR-Forces 4.0.4 Class Documentation
Sensor Component

This subclass of DtSimComponent will check to see if there are any emitter systems in the area that could sense a fixed wing.


VR-Forces provides a component class, DtSimComponent (simCmpnt.h), so we can derive our radar sensor from this component. Anytime you create a new component, you have to provide the following member items in your new class:

The string returned by the type() function is the key into the component factory that VR-Forces uses to instantiate new components. See compTypes.h for the set of string types for VR-Forces component classes.
Implementing the Radar Warning Receiver Class
The radar warning receiver class, DtSimpleRadarWarningReceiver (simpleRadarWarnRx.h), needs to detect the emissions and notify any interested controllers that it has detected one or more radars. In this example it merely checks for anything sending out emissions within a 5 Km radius. The class has a VR-Link DtReflectedEmitterSystem- List data member, myReflectedEmitterList, which it uses to gain knowledge of all entities generating emission PDUs over the network.
If it finds any, it extracts the host ID of the emitter and places that host ID on an emitter list port. The radar warning receiver uses the existing DtEntityListOutputPort class to communicate the identity of the set of radars it has detected in the current tick. It will ultimately be connected to the radar warning response controller that will receive the data on the emitter list port (a list of detected radars) and use that information to drive its behavior.
Adding a Sensor Component
As a provider of data (the list of detected emitters), the DtSimpleRadarWarningReceiver has a DtEntityListPort data member, myEmitterListPort. It is responsible for creating and setting values on the port. Each tick, it does the following:

 void DtSimpleRadarWarningReceiver::tick()
 {
    if(!entity())
    {
       return;
    }
    if(!myEmitterListPort)
    {
       return;
    }
    if (myEmitterListPort->attemptToActivate())
    {
       myEmitterListPort->emptyList();
       DtReflectedEmitterSystem* emitterSystem;
       for(emitterSystem = myReflectedEmitterList->first(); emitterSystem;
             emitterSystem = emitterSystem->next())
       {
          DtGlobalObjectDesignator emitterSystemHostId =
               emitterSystem->emitterSysRep()->hostId();
          if(emitterSystemHostId == myGlobalId)
          {
             continue;
          }
          DtVrfObject* emitterEntity = mySimManager->vrfObjectManager()->
                  lookupVrfObjectByGlobalId(emitterSystemHostId);
          if(!emitterEntity)
          {
             continue;
          }
          DtListItem beamItem = emitterSystem->emitterSysRep()->
                    beamList()->first();
          DtEmitterBeamRepository *beamSR = NULL;
          bool found = false;
          while (beamItem && !found)
          {
             beamSR = (DtEmitterBeamRepository) beamItem->data();
             if (beamSR->ERP() > 0)
             {
                double rangeSquaredToEmitter = DtDistSqr(
                     entity()->vrfState()->localPosition(),
                     emitterEntity->vrfState()->localPosition());
                if(rangeSquaredToEmitter < 25000000.0)
                {
                   myEmitterListPort->addEntity(emitterEntity->objectIdentifier());
                   found = true;
                }
             }
             beamItem = beamItem->next();
          }
       }
    }
 }
 
Header file:

/*******************************************************************************
** Copyright (c) 2003 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
/*******************************************************************************
** $RCSfile: simpleRadarWarnRx.h,v $ $Revision: 1.9 $ $State: Exp $
*******************************************************************************/

#ifndef DtSimpleRadarWarningReceiver_H_
#define DtSimpleRadarWarningReceiver_H_

#include "vrfobjcore/simCmpnt.h"
#include <vl/globalObjDes.h>

//class DtSimpleRadarWarningReceiver:
//
//Instances of DtSimpleRadarWarningReceiver are an example of a simple radar
//warning reciever.  For the purposes of example this receiver determines
//that it has detected a remote radar if a given remote emitter system
//is located within a given distance from the entity configured with the 
//reciever.  The list of radar emitters sensed this tick is generated as 
//output.

class DtReflectedEmitterSystemList;
class DtEntityListOutputPort;
class DtRadarWarningReceiverDescriptor;

class DtSimpleRadarWarningReceiver : public DtSimComponent
{
public:
   //constructor
   DtSimpleRadarWarningReceiver(const DtString & name, DtVrfObject* owner,
      DtSimManager * simManager, DtComponentDescriptor* desc = NULL,
      DtReaderWriterRegistry * parentRegistry = 0);

   //destructor
   virtual ~DtSimpleRadarWarningReceiver();

   //Calls createRelfectedEmitterList() and createEmitterListPort().
   virtual bool init();

   //Called by init().  Creates the VR-Link list of all reflected emitter
   //objects in the simulation.
   virtual bool createReflectedEmitterList();

   //This returns a string that uniquely identifies this type of component.
   //(must be unique with respect to the component types defined in compTypes.h
   //as well as any additional user-defined components).  Returns the string
   //"simple-radar-warning-receiver".
   virtual DtString type();

   //This is the entity we're detecting for.  We override because we
   //want to grab and cache the global object designator.
   virtual void setEntity(DtVrfObject * entity);

   //Detects emitters and places them on our port.
   virtual void tick();

   //Returns the list of emitters we've detected.
   virtual DtEntityListOutputPort* emitterListPort() const;

   //Returns a newly created instance of this class.  We need to provide
   //this function so that we can register this class with the component
   //factory.
   static DtSimComponent * creator(const DtString & name, DtVrfObject* owner,
      DtSimManager * simManager, DtComponentDescriptor* desc,
      DtReaderWriterRegistry * parentRegistry);

protected:
   
   //Overridden to create myEmitterListPort
   virtual bool createPorts();

protected:
   //Default constructor; should not be called.  Here because the compiler
   //will generate an unprotected one otherwise.
   DtSimpleRadarWarningReceiver();

   //copy constructor, not implemented
   DtSimpleRadarWarningReceiver(const DtSimpleRadarWarningReceiver& orig);

   //assignment operator, not implemented
   DtSimpleRadarWarningReceiver& operator=(
      const DtSimpleRadarWarningReceiver& orig);

protected:

   //List of all remote emitter systems.
   DtReflectedEmitterSystemList* myReflectedEmitterList;

   //Port used to communicate the list of detected emitter systems to
   //other components.
   DtEntityListOutputPort* myEmitterListPort;

   //The id of the entity this receiver is mounted on.
   DtGlobalObjectDesignator myGlobalId;

   //Pointer to the descriptor that holds the initialization parameters for this component.
   //This pointer is const because all instances of this component share a single
   //descriptor instance, so they should not be writing data back to it.
   const DtRadarWarningReceiverDescriptor* myDescriptor;
};

#endif


Implementation:

/*******************************************************************************
** Copyright (c) 2003 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
/*******************************************************************************
** $RCSfile: simpleRadarWarnRx.cxx,v $ $Revision: 1.11 $ $State: Exp $
*******************************************************************************/

#include "simpleRadarWarnRx.h"
#include <vlutil/vlPrint.h>
#include <vl/refEmitLst.h>
#include "vrfobjcore/entListIOPort.h"
#include "vrfcore/simMgr.h"
#include "vrfobjcore/vrfObject.h"
#include "vrfobjcore/vrfObjMgr.h"
#include "radarWarningReceiverDesc.h"

DtSimpleRadarWarningReceiver::DtSimpleRadarWarningReceiver(const DtString& name,
   DtVrfObject* owner, DtSimManager * simManager, DtComponentDescriptor* desc,
   DtReaderWriterRegistry * parentRegistry) :
   DtSimComponent(name, owner, simManager, desc, parentRegistry),
   myReflectedEmitterList(NULL),
   myEmitterListPort(NULL),
#if DtHLA
   myGlobalId(DtString::nullString())
#else
   myGlobalId(DtEntityIdentifier::nullId())
#endif
   , myDescriptor(0)
{
   //Initialize myDescriptor from the descriptor passed into the constructor.
   //Warn the user if the OPE or sysdef file was configured with the incorrect
   //descriptor for this component.
   myDescriptor = dynamic_cast<const DtRadarWarningReceiverDescriptor*>(desc);
   if (!myDescriptor)
   {
      objectConsoleError() << "DtSimpleRadarWarningReceiver requires descriptor of type " <<
         DtRadarWarningReceiverDescriptorType << " but found type " << desc->type() << std::endl;
   }
}

DtSimpleRadarWarningReceiver::~DtSimpleRadarWarningReceiver()
{
   if (myReflectedEmitterList)
   {
      delete myReflectedEmitterList;
      myReflectedEmitterList = NULL;
   }

   if (myEmitterListPort)
   {
      delete myEmitterListPort;
      myEmitterListPort = NULL;
   }
}

bool DtSimpleRadarWarningReceiver::init()
{
   if (!DtSimComponent::init())
   {
      return false;
   }

   if (!createReflectedEmitterList())
   {
      return false;
   }

   return true;
}

bool DtSimpleRadarWarningReceiver::createPorts()
{
   if (myEmitterListPort)
   {
      delete myEmitterListPort;
      myEmitterListPort = NULL;
   }

   //Create the sensor's output port.
   myEmitterListPort = new DtEntityListOutputPort("emitter-list");
   myEmitterListPort->init();
   addOutputPort(myEmitterListPort);
   
   return DtSimComponent::createPorts();
}

bool DtSimpleRadarWarningReceiver::createReflectedEmitterList()
{
   //Safety check - this should never happen
   if (!mySimManager)
   {
      DtWarn("Can't create reflected emitter list - no sim manager!\n");

      return false;
   }

   DtExerciseConn* exerciseConn = mySimManager->exerciseConn();

   //Safety check - this should never happen   
   if (!exerciseConn)
   {
      DtWarn("Can't create reflected emitter list - no exercise connection!\n");

      return false;
   }

   if (myReflectedEmitterList)
   {
      delete myReflectedEmitterList;
      myReflectedEmitterList = NULL;
   }

   myReflectedEmitterList = new DtReflectedEmitterSystemList(exerciseConn);

   return true;
}

//This returns a string that uniquely identifies this type of component.
//(must be unique with respect to the component types defined in compTypes.h
 //as well as any additional user-defined components).
DtString DtSimpleRadarWarningReceiver::type()
{
   return DtString("simple-radar-warning-receiver");
}

//This is the entity we're detecting for.  We override because we
//want to grab and cache the global object designator.
void DtSimpleRadarWarningReceiver::setEntity(DtVrfObject * entity)
{
   DtSimComponent::setEntity(entity);

   if (entity)
   {
      myGlobalId = entity->globalId();
   }
}

//Detects emitters and places them on our port.
void DtSimpleRadarWarningReceiver::tick()
{
   //Check to see if the simulation is paused
   if (dT() == 0.)
   {
      return;
   }

   if (!entity())
   {
      return;
   }

   //Safety check - this should have been created in init()
   if (!myEmitterListPort)
   {
      return;
   }

   //Attempt to take control of the emitter list output port
   if (myEmitterListPort->attemptToActivate())
   {
      //Clean out list of emitter systems on port
      myEmitterListPort->emptyList();

      //Iterate through list of current emitter systems
      DtReflectedEmitterSystem* emitterSystem;
      for(emitterSystem = myReflectedEmitterList->first(); emitterSystem;
      emitterSystem = emitterSystem->next())
      {
         DtGlobalObjectDesignator emitterSystemHostId = 
            emitterSystem->emitterSysRep()->hostId();
         
         //Make sure its doesn't belong to us
         if(emitterSystemHostId == myGlobalId)
         {
            continue;
         }

         //Look up the entity with the emitter
         DtVrfObject* emitterEntity = mySimManager->vrfObjectManager()->
            lookupVrfObjectByGlobalId(emitterSystemHostId);

         //If for some reason we can't find it, move on to the next one
         if(!emitterEntity)
         {
            continue;
         }

         //Use the emitter detection range that was configured in the descriptor.
         //If the descriptor was misconfigured and doesn't exist, use a default
         //value instead.
         //Store the square of it here to use inside the loop below for efficiency.
         double emitterDetectionRangeSquared = 25000000.0;
         if (myDescriptor)
         {
            emitterDetectionRangeSquared = myDescriptor->emitterDetectionRange() *
               myDescriptor->emitterDetectionRange();
         }

         //Check for at least one beam with >0 ERP.
         DtListItem *beamItem = emitterSystem->emitterSysRep()->
            beamList()->first();
         DtEmitterBeamRepository *beamSR = NULL;
         bool found = false;
         while (beamItem && !found)
         {
            beamSR = (DtEmitterBeamRepository*) beamItem->data();
            if (beamSR->ERP() > 0)
            {
               //Check the distance to the emitter - if it falls 5 km range, 
               //add it to our list of detected emitters
               double rangeSquaredToEmitter = DtDistSqr(
                  entity()->vrfState()->localPosition(),
                  emitterEntity->vrfState()->localPosition());

               if(rangeSquaredToEmitter < emitterDetectionRangeSquared)
               {
                  //Consider the emitter system to be detected.  Put it on
                  //our port.
                  myEmitterListPort->addEntity(
                     emitterEntity->objectIdentifier());
                  found = true;   //found 1, so don't check for others.
               }
            }
            beamItem = beamItem->next();
         }
      }
   }
}

DtEntityListOutputPort* DtSimpleRadarWarningReceiver::emitterListPort() const
{
   return myEmitterListPort;
}

//Returns a newly created instance of this class.  We need to provide
//this function so that we can register this class with the component
//factory.
DtSimComponent * DtSimpleRadarWarningReceiver::creator(const DtString & name, 
   DtVrfObject* owner, DtSimManager * simManager, DtComponentDescriptor* desc,
   DtReaderWriterRegistry * parentRegistry)
{
   return new DtSimpleRadarWarningReceiver(name, owner, simManager, desc,
      parentRegistry);
}

Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)