VR-Forces 4.0.4 Class Documentation
VRF Message Printer

This subclass of DtVrfMessageInterface creates and registers with the various factories to be able to create and print interactions.

How to Run

This example demonstrates how to listen for and display VR-Forces-specific interface messages.

Usage:

(Release) vrfMsgDump

(Debug) vrfMsgDumpd

To run the application:

  1. Start VR-Forces from the start menu.
    1. Start vrfMsgDump

The contents of all VR-Forces-specific interface messages (e.g. tasks, set data requests) will be displayed in the vrfMsgDump console window. As you run VR-Forces, any task or set data request messages that get created over the course of a scenario will be displayed by vrfMsgDump.


Header file:

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

#ifndef DtSimpleRadarWarningResponse_H_
#define DtSimpleRadarWarningResponse_H_

#include "vrfmodel/fixWngPltCtl.h"

//class DtSimpleRadarWarningResponse:
//
//Instances of DtSimpleRadarWarningResponse are an example of a controller
//that implements a reactive behavior when a it receives sensor input that
//a radar has been detected.  It receives a list of emitter systems (hostile
//radar systems).  It takes the first emitter in the list and flys away to
//a location in the opposite direction.

class DtVrfObject;
class DtEntityListInputPort;   

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


   //destructor
   virtual ~DtSimpleRadarWarningResponse();

   //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-response".
   virtual DtString type() const;

   //Calculate new control values.
   virtual void tick();

   //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:

   //Called by DtSimComponent::init to create myEntityListPort used to manage
   //multiple sensors that might provide targets to this controller.
   virtual bool createPorts();

   //Looks up safe control point by name.  If the object exists and it has
   //a valid state repository, returns the object.  Otherwise returns NULL.
   virtual DtVrfObject* getControlPoint(const DtString& name);

   //Returns true if we are in the vicinity of the given position.  To be
   //in the vicinity of the position means that we are within 5 seconds of
   //the given location, given our ordered speed.
   virtual bool inVicinity(const DtVector& localPosition);

protected:

   //default constructor
   DtSimpleRadarWarningResponse();

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

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

protected:

   //Port though which the list of hostile emitter systems will be received.
   DtEntityListInputPort* myEmitterListPort; 
   
   //true while in state of reacting to the detection of an emitter.
   bool myReactingToWarning;

   //Name of the safe location (a control point) we can retreat to.
   DtString mySafeControlPointName;

   DtVector myLocalGoal;
};

#endif


Implementation:

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

#include "simpleRdrWrnResp.h"
#include <vlutil/vlPrint.h>
#include "vrfcore/simMgr.h"
#include "vrfcore/exClock.h"
#include "vrfobjcore/fixWingSR.h"
#include "vrfobjcore/entListIOPort.h"
#include "vrfobjcore/vrfObjMgr.h"
#include "vrfobjcore/vrfObject.h"
#include "vrfobjcore/vrfKinState.h"
#include "vrfobjcore/vrfObjGeom.h"
#include "vrfobjcore/fixWingFlightPSR.h"
#include "vrfobjcore/outputPortGroup.h"

DtSimpleRadarWarningResponse::DtSimpleRadarWarningResponse(
   const DtString & name, DtVrfObject* owner, DtSimManager * simManager, 
   DtComponentDescriptor* desc, DtReaderWriterRegistry * const parentRegistry) :
   DtFixedWingPilotController(name, owner, simManager, desc, parentRegistry),
   myEmitterListPort(NULL),
   myReactingToWarning(false),
   mySafeControlPointName("Safe Haven")
{
}

DtSimpleRadarWarningResponse::~DtSimpleRadarWarningResponse()
{
   if (myEmitterListPort)
   {
      delete myEmitterListPort;
      myEmitterListPort = NULL;
   }
}

DtString DtSimpleRadarWarningResponse::type() const
{
   return DtString("simple-radar-warning-response");
}

bool DtSimpleRadarWarningResponse::createPorts()
{
   if (!(myEmitterListPort = new DtEntityListInputPort("emitter-list")))
   {
      return false;
   }

   addInputPort(myEmitterListPort);

   return DtFixedWingPilotController::createPorts();
}

void DtSimpleRadarWarningResponse::tick()
{
   DtFixedWingPilotController::tick();

   //If dT is > 0 (we're not paused), then do the work
   if (dT() > 0.) 
   {
      //Called each frame by the deriving controllers to set up need variables,
      //such as myNormalBodyVelocity and myLocalAngularVelocity.
      initializeControllerForTick();

      DtVrfObject* safeControlPoint = getControlPoint(mySafeControlPointName);
      
      if (!safeControlPoint)
      {
         DtWarn("DtSimpleRadarWarningResponse::tick() : Invalid safe control "
            "point %s\n", mySafeControlPointName.string());

         return;
      }

      DtVector safeLocation = safeControlPoint->vrfState()->
         vrfKinematicState()->localPosition();

      //If I'm already reacting to a previous radar warning
      if (myReactingToWarning)
      {

         //Take control of the port group - this gives us exclusive control
         //over the fixed-wing actuator until we give that control up or a 
         //higher priority controller overrides us
         if (myFixedWingControlPortGroup->attemptToActivate())
         {
            if (inVicinity(safeLocation))
            {
               //We're done

               objectConsoleInfo() << "radarWarnRx Example - " << 
                  "We have reached the safe location!  " << std::endl;
               objectConsoleInfo() << "radarWarnRx Example - " << 
                  "Resume normal activity." << std::endl;

               myReactingToWarning = false;

               //Give up control of actuator's port group so other 
               //components may control it. 
               myFixedWingControlPortGroup->deactivate();
            }
            else
            {
               //Move toward the safe location
               flyTo(safeLocation, myFixedWingEntityState->orderedSpeed());
            }
         }
      }
      else
      {
         if (! myEmitterListPort || ! myEmitterListPort->activeConnection() ||
            !myEmitterListPort->value())
         {
            objectConsoleVerbose() << "radarWarnRx Example - " << 
            "No emitter list for radar warning reaction controller" <<
            std::endl;

            return;
         }

         //Get 1st emitter system on list of emitters on the port 
         const DtEntityTargetList* emitterList = myEmitterListPort->value();
      
         //If there aren't any emitters out there, there's nothing to react to
         if (!(emitterList->first() && emitterList->first()->data()))
         {
            //Mark the data as "old"
            myEmitterListPort->dataReceived();

            return;
         }

         //We have an emitter out there
         DtEntityTargetInformation * info = 
            (DtEntityTargetInformation *)emitterList->first()->data();

         //Look up the entity with the radar
         DtVrfObject* emitterSystemHost = 
            mySimManager->vrfObjectManager()->lookupVrfObject(info->id());

         if (!emitterSystemHost)
         {
            //Mark the data as "old"
            myEmitterListPort->dataReceived();

            return;
         }

         //We're now reacting to an emitter
         myReactingToWarning = true;

         objectConsoleInfo() << "radarWarnRx Example - " << 
         "Simple radar warning rx detected emitter " << info->id().string() <<
         "." << std::endl;
         objectConsoleInfo() << "radarWarnRx Example - " << 
         "Move away from the source!" << std::endl;

         //Take control of port group - this gives us exclusive control
         //over the fixed-wing actuator for the duration of the frame (no other
         //controllers that come after this one will be able to control it).
         if (myFixedWingControlPortGroup->attemptToActivate())
         {
            //Move toward the safe location
            flyTo(safeLocation, myFixedWingEntityState->orderedSpeed());
         }
      
         //Mark the data as "old"
         myEmitterListPort->dataReceived();
      }
   }
}

DtSimComponent * DtSimpleRadarWarningResponse::creator(const DtString & name, 
   DtVrfObject* owner, DtSimManager * simManager, DtComponentDescriptor* desc,
   DtReaderWriterRegistry * parentRegistry)
{
   return new DtSimpleRadarWarningResponse(name, owner, simManager, desc, 
      parentRegistry);
}

DtVrfObject* DtSimpleRadarWarningResponse::getControlPoint(const DtString& name)
{
   DtVrfObject* controlPoint = mySimManager->vrfObjectManager()->
      lookupVrfObjectByName(mySafeControlPointName);
   
   if (!controlPoint)
   {
      objectConsoleWarn() << "radarWarnRx Example - " << 
      "DtSimpleRadarWarningResponse::getControlPoint() : Can't find control point " <<
      name << std::endl;
      return NULL;
   }
   
   if (!controlPoint->vrfState())
   {
      objectConsoleWarn() << "radarWarnRx Example - " << 
         "Control point " << name << " does not have state repository!" << std::endl;
      return NULL;
   }

   return controlPoint;
}

bool DtSimpleRadarWarningResponse::inVicinity(
   const DtVector& localPosition)
{
   DtLocalVector location = myFixedWingEntityState->localPosition();

   myProcessState->setDistanceSquared(location.distSquaredTo2D(localPosition));
   
   if (myProcessState->lastDistanceSquared() == 0.0)
   {
      myProcessState->setLastDistanceSquared(myProcessState->distanceSquared());
   }

   if (myProcessState->distanceSquared() < (myFixedWingEntityState->orderedSpeed() * 
      myFixedWingEntityState->orderedSpeed() * 25.0)) //5 seconds away...
   {
      return true;
   }

   return false;
}

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)