VR-Forces 4.0.4 Class Documentation
Simple Plan Example

The Simple Plan example demonstrates the following:

A CGF application differs from the released back-end in that there is not command line options applied to it and it is intended to be used as part of a larger back-end application that does not have a need to interface with the VR-Forces front-end.

How to Run

This example demonstrates how to programatically create a plan for an entity in an embedded CGF application (outside of the context of the vrfSim application).

Usage

(Release) simplePlan

(Debug) simplePland

To run the application:

  1. Start vrfGui on port 3000
  2. Open the data\terrain\ground-db.mtd terrain database.
  3. Start simplePlan from the /vrforces/bin/ directory.
  4. Right-click on the entity to view its plan.

Note you will need to join the session or you cannot view the entity's plan.

The simplePlan application simulates a tank with a plan. You can observe the execution of the plan in the entity's 'View Plan' window.

Main Application File

/*******************************************************************************
** Copyright (c) 2004 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/

//This example demonstrates how to use the DtCgf class to create entities,
//objects and plans, and then execute the resulting scenario as you would if 
//you were to embed VR-Forces in your own application.

#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#endif

//VR-Forces Headers
#include "vrfcgf/cgf.h"
#include "vrfcgf/cgfDispatcher.h"
#include "vrfcore/simMgr.h"
#include "vrfobjcore/vrfObject.h"
#include "vrftasks/setSpeed.h"
#include "vrftasks/waitDurTask.h"
#include "vrftasks/moveToTask.h"
#include "vrfplan/setStmt.h"
#include "vrfplan/taskStmt.h"
#include "vrfplan/planBlock.h"
#include "vrfplan/plan.h"

#include "vrfobjcore/orgManager.h"
#include "vrfobjcore/vrfObjSR.h"
#include "vrfobjparam/vrfObjParams.h"
#include "vrfobjcore/vrfObjMgr.h"

//for conditional expression
#include "vrfplan/ifStmt.h"
#include "vrfutil/condExpr.h"
#include "vrfplan/ifBlock.h"
#include "vrfutil/ceEntInArea.h"
#include "vrfutil/evaluator.h"
#include "vrfutil/evalFactory.h"

//VR-Link headers
#include <vl/exerciseConn.h>
#include <vlutil/vlPrint.h>
#include <vlutil/vlProcessControl.h>
#include <vl/hostStructs.h>
#include "vrfutil/vrfVlKeyboard.h"

int main(int argc, char *argv[])
{
   //DIS/RPR simulation address

   DtSimulationAddress simAddress(1, 3001);

   //Create the CGF
   DtCgf* cgf = new DtCgf(simAddress);

   //Create the exercise connection
#if DtHLA

   DtString execName           = "VR-Link";     //HLA federation execution name
   DtString federateName       = "simplePlan";  //HLA federate name
   double rprFomVersion        = 1.0;           //RPR FOM version number

   DtExerciseConn* exerciseConn = new DtExerciseConn(execName, federateName,
      new DtRprFomMapper(rprFomVersion));

#else //DIS

   int port = 3000;                             //UDP port number
   int exerciseId = 1;                          //DIS exercise ID

   DtExerciseConn* exerciseConn = new DtExerciseConn(port, exerciseId, 
      simAddress.siteId(), simAddress.applicationId());

#endif

   //Initialize the CGF
   cgf->init(exerciseConn);

   //Create the dispatcher so this application can interface with
   //front-end
   DtCgfDispatcher* cgfDispatcher = new DtCgfDispatcher(cgf,
      cgf->simManager()->messageInterface());

   //Create a new scenario, using the Ground-db.gdb terrain database
   cgf->newScenario("../userData/terrains/Ground-db.mtf");
   //Tick the cgf to get the terrain loaded
   cgf->tick();

   //Create a tank
   DtEntityType tankType(1, 1, 225, 1, 1, 3, 0);
   DtVector initialPosition(900.0, 900.0, 0.0); //Database coordinates, meters
   DtReal initialHeading = 0.0;                 //Heading in radians

   DtVrfObject* tank = cgf->createEntity(
      tankType,                                 
      DtForceFriendly,                          
      initialPosition,              
      initialHeading);                                     

   //Create an area for testing

   DtList verticies;
   DtVector v1(800,800,0);
   DtVector v2(800,1000,0);
   DtVector v3(1000,1000,0);
   DtVector v4(1000,800,0);
   verticies.add((void*)&v1);
   verticies.add((void*)&v2);
   verticies.add((void*)&v3);
   verticies.add((void*)&v4);
   DtVrfObject* area = cgf->createControlArea(verticies, "area");

   //Create a waypoints named "Alpha" and "Beta"

   DtVrfObject* waypoint = cgf->createWaypoint(
      DtVector(1300.0, 1000.0, 0.0),            
      "Alpha");

   DtVrfObject* waypoint2 = cgf->createWaypoint(
      DtVector(300.0, 1000.0, 0.0),            
      "Beta");

   //Create a simple plan for the tank

   DtPlan plan;    
   plan.init();

   //Create some set data requests and tasks to put in the plan

   DtSetSpeedRequest speedReq;
   speedReq.setSpeed(10.0);                     //Ordered speed, in meters/second

   DtWaitDurationTask waitTask;
   waitTask.setSecondsToWait(10.0);

   DtMoveToTask moveToAlphaTask;
   moveToAlphaTask.setControlPtName("Alpha");
   DtMoveToTask moveToBetaTask;
   moveToBetaTask.setControlPtName("Beta");

   //Create statements to hold the set data requests and tasks
   DtSetDataRequestStmt setSpeedStatement;
   DtTaskStmt waitStatement;
   DtTaskStmt moveToAlphaStatement;
   DtTaskStmt moveToBetaStatement;

   setSpeedStatement.setSetDataRequest(&speedReq);
   waitStatement.setTask(&waitTask);
   moveToAlphaStatement.setTask(&moveToAlphaTask);
   moveToBetaStatement.setTask(&moveToBetaTask);

   //Create an "entity in area" conditional expression. The entity has
   //already been created within the test area so this expression will
   //always return true. To test the "else" condition, try changing the
   //initial position of the entity or the area. 
   DtCeEntInArea conditionalExpression;
   conditionalExpression.setEvaluator(DtEvaluator::factory()->
      createEvaluator(conditionalExpression.type()));
   conditionalExpression.setAreaName("area");
   conditionalExpression.setEntityName(tank->objectName());

   DtIfStmt ifStatement;
   ifStatement.setCondExpr(&conditionalExpression);
   ifStatement.thenBlock()->add(moveToAlphaStatement.clone());
   ifStatement.elseBlock()->add(moveToBetaStatement.clone());

   //Add statements to the plan.  The plan assumes responsibility
   //for the memory associated with the statements, so clone the
   //statements and add them to the plan.

   DtPlanBlock* block = plan.mainBlock();

   block->addToStart(setSpeedStatement.clone());
   block->addToStart(waitStatement.clone());
   block->addToEnd(ifStatement.clone());

   //Assign the plan to the tank.
   cgf->assignPlanByName(tank->objectName(),  plan);

   //Put the simulation engine into run mode.

   cgf->run();

   //Main application loop

   while (1)
   {
      //tick the CGF Dispatcher
      cgfDispatcher->tick();

      //tick the CGF
      cgf->tick();

      //Poll & process keyboard input
      char *keyPtr = DtGetInputLine();
      if (keyPtr && *keyPtr == 'q')
      {
         break;
      }

      //Give up the remainder of our time slice.
      DtSleep(0.01);
   }    

   delete cgf;
   delete exerciseConn;

   return 0;
}


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)