int main(int argc ,char * argv[]) { try { const char * sMissionFile = "Mission.moos"; const char * sMOOSName = "pTextInterpreterApp"; switch(argc) { case 3: sMOOSName = argv[2]; case 2: sMissionFile = argv[1]; } TextInterpreterApp TheApp; TheApp.Run(sMOOSName,sMissionFile); return 0; } catch (std::exception &e) { std::cerr << "**ERROR** " << e.what() << std::endl; return MOOSFail( "Closing due to an exception" ); } catch (...) { return MOOSFail( "Closing due to an untyped exception." ); } }
bool OnNewMail(MOOSMSG_LIST & NewMail){ //process it /** MOOSMSG_LIST::iterator p; for(p=NewMail.begin();p!=NewMail.end();p++) { //lets get a reference to the Message - no need for pointless copy CMOOSMsg & rMsg = *p; // repetitive "ifs" is one way to build a switch yard for // the messages p->Trace(); } **/ //Retrieve informaiton MOOSMSG_LIST::iterator p1; for(p1=NewMail.begin();p1!=NewMail.end();p1++) { //lets get a reference to the Message - no need for pointless copy CMOOSMsg & rMsg = *p1; // repetitive "ifs" is one way to build a switch yard for // the messages if(MOOSStrCmp(rMsg.GetKey(),"DESIRED_PORTTHRUSTER")) { //this message is about something called "DESIRED_PORTTHRUSTER" CMOOSMsg &Msg = rMsg; if(!Msg.IsDouble()) return MOOSFail("Ouch - was promised \"DESIRED_PORTTHRUSTER\" would be a double"); DESIRED_PORTTHRUSTER = Msg.GetDouble(); //MOOSTrace("DESIRED_PORTTHRUSTER is %f\n",DESIRED_PORTTHRUSTER);//the actual heading //if you want to see all details in Msg, you can print a message by //Msg.Trace(); } else if(MOOSStrCmp(rMsg.GetKey(),"DESIRED_STARBOARDTHRUSTER")) { //this message is about something called "Input_FR" CMOOSMsg &Msg1 = rMsg; if(!Msg1.IsDouble()) return MOOSFail("Ouch - was promised \"DESIRED_STARBOARDTHRUSTER\" would be a double"); DESIRED_STARBOARDTHRUSTER = Msg1.GetDouble(); //MOOSTrace("DESIRED_STARBOARDTHRUSTER is %f\n",DESIRED_STARBOARDTHRUSTER);//the actual heading //if you want to see all details in Msg, you can print a message by //Msg.Trace(); } } return true; }
bool Joystick::OnStartUp() { AppCastingMOOSApp::OnStartUp(); setlocale(LC_ALL, "C"); STRING_LIST sParams; m_MissionReader.EnableVerbatimQuoting(false); if(!m_MissionReader.GetConfiguration(GetAppName(), sParams)) reportConfigWarning("No config block found for " + GetAppName()); int inputId = -1; int inputType = -1; float ratio = 1.; float axis_min = -1.; float axis_max = 1.; ControlMode inputMode = Value; STRING_LIST::iterator p; sParams.reverse(); for(p = sParams.begin() ; p != sParams.end() ; p++) { string orig = *p; string line = *p; string param = toupper(biteStringX(line, '=')); string value = line; bool handled = false; if(param == "DEVICE_NAME") { mJoystickControl.mDeviceName = value; handled = true; } else if(param == "MODE") { if (tolower(value) == "value") inputMode = Value; else if (tolower(value) == "increment") inputMode = Increment; else if (tolower(value) == "switch") inputMode = Switch; handled = true; } else if(param == "MIN") { if(!MOOSIsNumeric(value)) reportConfigWarning(orig); else axis_min = atof(value.c_str()); handled = true; } else if(param == "MAX") { if(!MOOSIsNumeric(value)) reportConfigWarning(orig); else axis_max = atof(value.c_str()); handled = true; } else if(param == "BUTTON") { if(!MOOSIsNumeric(value)) reportConfigWarning(orig); else { inputId = atoi(value.c_str()); inputType = JS_EVENT_BUTTON; } handled = true; } else if(param == "AXIS") { if(!MOOSIsNumeric(value)) reportConfigWarning(orig); else { inputId = atoi(value.c_str()); inputType = JS_EVENT_AXIS; } handled = true; } else if(param == "SCALE") { if(!MOOSIsNumeric(value)) reportConfigWarning(orig); else { axis_max = atof(value.c_str()); axis_min = -axis_max; } handled = true; } else if(param == "SCALE_RATIO") { if(!MOOSIsNumeric(value)) reportConfigWarning(orig); else ratio = atof(value.c_str()); handled = true; } else if(param == "MOOS_DEST_VAR") { value = toupper(value); if(inputId >= 0) { mControlsVariables[make_pair(inputType, inputId)] = pair<string, JoystickControlState>(value, JoystickControlState()); double offset = ratio*axis_min; double gain = (ratio*axis_max - offset); mJoystickControl.setState(inputType, inputId, inputMode, gain, offset); handled = true; } } if(!handled) reportUnhandledConfigWarning(orig); } if(!mJoystickControl.OpenPort()) MOOSFail("Cannot open the joystick device '%s'.\n", mJoystickControl.mDeviceName.c_str()); registerVariables(); return(true); }
bool CMOOSPlayBackV2::MessageFromLine(const std::string & sLine, CMOOSMsg &Msg) { int n = 0; std::string sTime,sKey,sSrc; m_ALog.GetNextToken(sLine,n,sTime); m_ALog.GetNextToken(sLine,n,sKey); m_ALog.GetNextToken(sLine,n,sSrc); size_t nData = sLine.find_first_not_of(" \t",n); if(nData==string::npos) return false; std::string sData = sLine.substr(nData,sLine.size()-nData); Msg.m_dfTime = MOOSTime(); if(MOOSIsNumeric(sData)) { Msg.m_dfVal = atof(sData.c_str()); Msg.m_cDataType = MOOS_DOUBLE; Msg.m_sVal = ""; } else { Msg.m_dfVal = 0.0; if(sData.find("<MOOS_BINARY>") !=std::string::npos) { //Msg.MarkAsBinary(); long long nOffset; if(!MOOSValFromString(nOffset,sData,"Offset")) return MOOSFail("badly formed MOOS_BINARY indicator - missing \"Offset=xyz\""); std::string sFile; if(!MOOSValFromString(sFile,sData,"File")) return MOOSFail("badly formed MOOS_BINARY indicator - missing \"File=XYZ\""); int nBytes; if(!MOOSValFromString(nBytes,sData,"Bytes")) return MOOSFail("badly formed MOOS_BINARY indicator - missing \"Bytes=xyz\""); //corner case of binary file changing half way through a log....(Why this might happen //I don't know but catch it anyway.... if(sFile != m_sBinaryFileName && m_BinaryFile.is_open() ) { m_BinaryFile.close(); } m_sBinaryFileName = sFile; if(!m_BinaryFile.is_open()) { //open the file with the correct name //here we need to point the file towards the full path - the blog file should be sitting next //to teh alog being played /** splits a fully qualified path into parts -path, filestem and extension */ std::string sPath,sFileName,sExtension; if(!MOOSFileParts(m_ALog.GetFileName(),sPath,sFileName,sExtension)) return MOOSFail("failed to parse alog file into file parts %s",MOOSHERE); std::string sFullBinaryLogPath = sPath+"/"+m_sBinaryFileName; MOOSTrace("opening binary file %s\n",sFullBinaryLogPath.c_str()); m_BinaryFile.open(sFullBinaryLogPath.c_str(),std::ios::binary); if(!m_BinaryFile) { return MOOSFail("unable to open binary file called %s", m_sBinaryFileName.c_str()); } } //move to the right place in the file m_BinaryFile.seekg(nOffset); //make space char * pBD = new char[nBytes]; //read the right number of bytes m_BinaryFile.read(pBD,nBytes); //copy data Msg.m_sVal.assign(pBD,nBytes); //delete temporary space delete [] pBD; Msg.MarkAsBinary(); } else { Msg.m_dfVal = 0.0; Msg.m_cDataType = MOOS_STRING; Msg.m_sVal = sData; } } //fill in standard aspects Msg.m_sSrc = sSrc; Msg.m_sKey = sKey; Msg.m_cMsgType = MOOS_NOTIFY; return true; }