VR-Forces 4.0.4 Class Documentation
Derived Move To Controller

This subclass of the DtGroundMoveToControllerComponent will give up on the movement task after a certain amount of simulation time


Header file:

/*******************************************************************************
** Copyright (c) 1999 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
/*******************************************************************************
** $RCSfile: derivedMoveToController.h,v $ $Revision: 1.5 $ $State: Exp $
*******************************************************************************/

//
//File:        DerivedMoveToController.h
//
//Class:       DerivedMoveToController
//


#ifndef DerivedMoveToController_H_
#define DerivedMoveToController_H_


#include "vrfmodel/gndMvToCntlr.h"

//
//Class Description: DerivedMoveToController
//
//The purpose of this class is to demonstrate how to check for and handle
//a situation in which an entity is unable to carry out a given task behavior.
//In this particular example, it extends the DtGroundMoveToControllerComponent
//with the ability to determine when it should 'give up' trying to reach its 
//waypoint.  The criteria for determining it should give up trying to complete
//its task is that it should stop trying after 10 seconds.  At that point, it 
//sends a task complete report, indicating it has finished (although it may be
//no where near its destination).
//
//There are 2 required member functions that this class overrides to implement
//the 'give up' behavior:
//
//decideToGiveUpTask() : This is the function that looks at the current
//situation and decides if it should give up.  It will be automatically called
//immediately following the entity's tick().
//
//giveUpTask() : This gets called when the decideToGiveUpTask() returns true.
//In this example, it calls taskComplete(), which results in a task complete
//report being sent and the controller put into a non-tasked state.

class DerivedMoveToController : public DtGroundMoveToControllerComponent
{
public:

   //constructor
   DerivedMoveToController(const DtString& name, 
      DtVrfObject* owner,
      DtSimManager* simManager, 
      DtComponentDescriptor* desc = NULL,
      DtReaderWriterRegistry* parentRegistry = NULL);

   //destructor
   virtual ~DerivedMoveToController();

   //Override to initialize myTimeToGiveUp member to current time plus 10 seconds.
   //We'll check this in decideToGiveUp().  Note that this function is only
   //overridden for this particular time-based criteria for giving up.  You would
   //not in general have to override this to add a 'decide to give up' capability
   //to a controller.
   virtual void processMoveToTask(DtSimMessage * msg);

   //Creates and returns a new DerivedMoveToController
   static DtSimComponent* creator(const DtString& name, 
      DtVrfObject* owner,
      DtSimManager* simManager, 
      DtComponentDescriptor* desc = NULL,
      DtReaderWriterRegistry* parentRegistry = NULL);

protected:

   //Override to see if we should give up after 10 seconds (checks current
   //simulation time against myTimeToGiveUp).  
   virtual bool decideToGiveUpTask() const;

   //Calls taskComplete().  This will result in a DtTaskComplete report being
   //sent to whomever issued the task.
   virtual void giveUpTask();

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

   //copy constructor; shallow; does not copy the ports.
   DerivedMoveToController(const DerivedMoveToController & 
      orig);

   //assignment operator; see comments on copy constructor!
   const DerivedMoveToController & operator=(const 
      DerivedMoveToController & orig);

protected:

   //This is the time we decide we want to give up when the entity
   //is executing a task.
   DtTime myTimeToGiveUp;
};

#endif




Implementation:

/*******************************************************************************
** Copyright (c) 1999 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
/*******************************************************************************
** $RCSfile: derivedMoveToController.cxx,v $ $Revision: 1.6 $ $State: Exp $
*******************************************************************************/

//
//File:        DerivedMoveToController.cxx
//
//Class:       DerivedMoveToController
//

#include "derivedMoveToController.h"
#include <vrfcore/simMgr.h>
#include <vrfcore/exClock.h>
#include <vrfobjcore/vrfObject.h>

DerivedMoveToController::DerivedMoveToController(
   const DtString& name, 
   DtVrfObject* owner,
   DtSimManager* simManager, 
   DtComponentDescriptor* desc, 
   DtReaderWriterRegistry* parentRegistry) :
   DtGroundMoveToControllerComponent(name, owner, simManager, desc, 
      parentRegistry)
{
}


DerivedMoveToController::~DerivedMoveToController()
{
}

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

void DerivedMoveToController::processMoveToTask(DtSimMessage * msg)
{
   DtGroundMoveToControllerComponent::processMoveToTask(msg);

   //Initialize time to give up to 10 seconds from now.
   myTimeToGiveUp = mySimManager->exerciseClock()->simTime() + 10.0;

   objectConsoleInfo() << myEntity->objectName().string() << 
      " - Setting time to give up for move-to task to " << myTimeToGiveUp <<
      std::endl;
}

bool DerivedMoveToController::decideToGiveUpTask() const
{
   DtTime currentSimTime = mySimManager->exerciseClock()->simTime();
   if( currentSimTime > myTimeToGiveUp)
   {
      objectConsoleInfo() << myEntity->objectName().string() << 
         " - Giving up move to task at time " << currentSimTime << std::endl;

      return true;
   }
   else
   {
      return false;
   }
}

void DerivedMoveToController::giveUpTask()
{
   objectConsoleInfo() << myEntity->objectName().string() <<
      " - Gave up on move to task.  Calling taskComplete()." << std::endl;

   taskComplete();
}

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)