MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Docs/ProgrammingWithMOOS/code/Ex2/SimpleApp.cpp
Go to the documentation of this file.
00001 
00002 #include "SimpleApp.h"
00003 
00004 #include <MOOSGenLib/MOOSGenLibGlobalHelper.h>
00005 
00006 //default constructor
00007 CSimpleApp::CSimpleApp()
00008 {
00009 }
00010 //default (virtual) destructor
00011 CSimpleApp::~CSimpleApp()
00012 {
00013 }
00014 
00027 bool CSimpleApp::OnNewMail(MOOSMSG_LIST &NewMail)
00028 {
00029     MOOSMSG_LIST::iterator p;
00030     
00031     for(p=NewMail.begin();p!=NewMail.end();p++)
00032     {
00033     //lets get a reference to the Message - no need for pointless copy
00034     CMOOSMsg & rMsg = *p;
00035     
00036     // repetitive "ifs" is one way to build a switch yard for
00037     // the messages
00038     if(MOOSStrCmp(rMsg.GetKey(),"VehicleStatus"))
00039     {
00040         //this message is about something called "VariableX"
00041         OnVehicleStatus(rMsg);
00042     }
00043     else if(MOOSStrCmp(rMsg.GetKey(),"Heading"))
00044     {
00045         //this message is about something called "VariableY"
00046         OnHeading(rMsg);
00047     }
00048     }
00049 
00050    
00051 
00052     return true;
00053 }
00054 
00055 
00061 bool CSimpleApp::OnConnectToServer()
00062 {
00063     //do registrations
00064     DoRegistrations();
00065 
00066     return true;
00067 }
00068 
00071 bool CSimpleApp::Iterate()
00072 { 
00073     
00074     return true;
00075 }
00076 
00080 bool CSimpleApp::OnStartUp()
00081 {       
00082     //do registrations - its good practice to do this BOTH in OnStartUp and
00083     //in OnConnectToServer - that way if comms is lost registrations will be
00084     //reinstigated when the connection is remade 
00085     DoRegistrations();
00086 
00087     return true;
00088 }
00089 
00090 
00091 bool CSimpleApp::OnVehicleStatus(CMOOSMsg & Msg)
00092 {
00093     MOOSTrace("I (%s) received a notification about \"%s\" the details are:\n",
00094           GetAppName().c_str(),
00095           Msg.GetKey().c_str());
00096 
00097     //if you want to see all the details you can print a message...
00098     //Msg.Trace();
00099 
00100     if(!Msg.IsString())
00101     return MOOSFail("Ouch - I was promised \"VehicleStatus\" would be a string!");
00102     
00103     //OK the guy who wrote the program that publishes VehicleStatus wrote me an
00104     //email saying the format of the message is:
00105     //Status = [Good/Bad/Sunk], BatteryVoltage = <double>, Bilge=[on/off]
00106     //so here we parse the bits we want from the string
00107     std::string sStatus="Unknown";
00108     if(!MOOSValFromString(sStatus,Msg.GetString(),"Status"))
00109     MOOSTrace("warning field \"Status\" not found in VehicleStatus string %s",MOOSHERE);
00110 
00111     double dfBatteryVoltage=-1;
00112     if(!MOOSValFromString(dfBatteryVoltage,Msg.GetString(),"BatteryVoltage"))
00113     MOOSTrace("warning field \"BatteryVoltage\" not found in VehicleStatus string %s",MOOSHERE);
00114 
00115     //simple print out our findings..
00116     MOOSTrace("Status is \"%s\" and battery voltage is %.2fV\n",sStatus.c_str(),dfBatteryVoltage);
00117     
00118     return true;
00119 }
00120 
00121 
00122 bool CSimpleApp::OnHeading(CMOOSMsg & Msg)
00123 {
00124     MOOSTrace("I (%s) received a notification about \"%s\" the details are:\n",
00125           GetAppName().c_str(),  //note GetAppName() returns the name of this application as seen by the DB
00126           Msg.GetKey().c_str()); //note GetKey() return the name of the variable
00127 
00128 
00129     //if you want to see all the details you can print a message...
00130     //Msg.Trace();
00131 
00132     //you might want to be sure that the message is in the format you were expecting
00133     //in this case heading comes as a single double...
00134 
00135     if(!Msg.IsDouble())
00136     return MOOSFail("Ouch - was promised \"Heading\" would be a double %s", MOOSHERE);
00137 
00138     double dfHeading = Msg.GetDouble();
00139     double dfTime = Msg.GetTime();
00140 
00141     MOOSTrace("The heading (according to process %s),at time %f (%f since appstart) is %f\n",
00142           Msg.GetSource().c_str(), //who wrote it
00143           dfTime,//when
00144           dfTime-GetAppStartTime(),//time since we started running (easier to read)
00145           dfHeading);//the actual heading
00146     
00147     return true;
00148 }
00149 
00150 
00151 
00152 
00153 void CSimpleApp::DoRegistrations()
00154 {
00155     //register to be told about every change (write) to "VehicleStatus"
00156     m_Comms.Register("VehicleStatus",0);
00157     
00158     //register to be told about changes (writes) to "Heading" at at most
00159     //4 times a second
00160     m_Comms.Register("Heading",0.25);
00161 
00162     return;
00163 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines