MOOS 0.2375
|
00001 00002 #include "Simulator.h" 00003 #include <math.h> 00004 00005 //default constructor 00006 CSimulator::CSimulator() 00007 { 00008 } 00009 00010 //default (virtual) destructor 00011 CSimulator::~CSimulator() 00012 { 00013 } 00014 00027 bool CSimulator::OnNewMail(MOOSMSG_LIST &NewMail) 00028 { 00029 return true; 00030 } 00031 00037 bool CSimulator::OnConnectToServer() 00038 { 00039 return true; 00040 } 00041 00044 bool CSimulator::Iterate() 00045 { 00046 static int k = 0; // a simple counter to simulate a simulator - not an important detail... 00047 if(k++%10==0) 00048 { 00049 //simulate some brownian motion 00050 m_dfHeading+=MOOSWhiteNoise(0.1); 00051 00052 //publish the data (2nd param is a double so it will be forever double data...) 00053 std::string sVarName = m_sVehicleName+"_Heading"; 00054 m_Comms.Notify(sVarName,m_dfHeading,MOOSTime()); 00055 } 00056 if(k%35==0) 00057 { 00058 00059 m_dfBatteryVoltage-=fabs(MOOSWhiteNoise(0.1)); 00060 std::string sStatus = MOOSFormat("Status=%s,BatteryVoltage=%.2f,Bilge = %s", 00061 m_dfBatteryVoltage>50.0? "Good":"Bad", 00062 m_dfBatteryVoltage, 00063 m_sBilge.c_str()); 00064 00065 //publish the data (2nd param is a std::string so it will be forever string data...) 00066 //note how name of variable is set by what was read from configuration file 00067 std::string sVarName = m_sVehicleName+"_Status"; 00068 m_Comms.Notify(m_sVehicleName,sStatus,MOOSTime()); 00069 } 00070 return true; 00071 } 00072 00076 bool CSimulator::OnStartUp() 00077 { 00078 //here we extract the vehicle name.. 00079 m_sVehicleName = "UnNamed"; 00080 if(!m_MissionReader.GetConfigurationParam("VehicleName",m_sVehicleName)) 00081 MOOSTrace("Warning parameter \"VechicleName\" not specified. Using default of \"%s\"\n",m_sVehicleName.c_str()); 00082 00083 00084 //here we extract a vector of doubles from the configuration file 00085 std::vector<double> vInitialLocation(3,0.0); 00086 int nRows=vInitialLocation.size(); 00087 int nCols = 1; 00088 if(!m_MissionReader.GetConfigurationParam("InitialLocation",vInitialLocation,nRows,nCols)) 00089 MOOSTrace("Warning parameter \"InitialLocation\" not specified. Using default of \"%s\"\n",DoubleVector2String(vInitialLocation).c_str()); 00090 00091 //here we extract a more complicated compound string parameter 00092 std::string sComplex; 00093 if(m_MissionReader.GetConfigurationParam("InitialConditions",sComplex)) 00094 { 00095 //OK now we can suck out individual parameters from sComplex 00096 00097 //what is the initial Bilge condition status? 00098 m_sBilge = "Off"; 00099 MOOSValFromString(m_sBilge,sComplex,"Bilge"); 00100 00101 //what is the initial battery Voltage? 00102 m_dfBatteryVoltage = 100.0; 00103 MOOSValFromString(m_dfBatteryVoltage,sComplex,"BatteryVoltage"); 00104 00105 //what is the initial heading 00106 m_dfHeading = 0; 00107 MOOSValFromString(m_dfHeading,sComplex,"Heading"); 00108 00109 } 00110 else 00111 { 00112 //bad news - this one is compulsory for this application... 00113 return MOOSFail("no \"InitialConditions\" specified in mission file (compulsory)\n"); 00114 } 00115 00116 00117 MOOSTrace("Verbose Summary:\n"); 00118 MOOSTrace("\tVehicle is called : %s\n",m_sVehicleName.c_str()); 00119 MOOSTrace("\tInitial Location is : %s\n",DoubleVector2String(vInitialLocation).c_str()); 00120 MOOSTrace("\tHeading is : %f\n",m_dfHeading); 00121 MOOSTrace("\tBatteryVoltage is : %f\n",m_dfBatteryVoltage); 00122 00123 00124 00125 return true; 00126 }