From: Markus R. <rol...@us...> - 2007-02-25 15:54:00
|
Update of /cvsroot/simspark/simspark/contrib/agentspark In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv11888 Modified Files: Tag: WIN32 agentspark.vcproj Added Files: Tag: WIN32 soccerbotbehavior.cpp soccerbotbehavior.h Log Message: - add default soccerbot behavior from rcssserver3d cvs Index: agentspark.vcproj =================================================================== RCS file: /cvsroot/simspark/simspark/contrib/agentspark/Attic/agentspark.vcproj,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -C2 -d -r1.1.2.1 -r1.1.2.2 *** agentspark.vcproj 23 Feb 2007 19:58:20 -0000 1.1.2.1 --- agentspark.vcproj 25 Feb 2007 15:53:50 -0000 1.1.2.2 *************** *** 191,194 **** --- 191,202 ---- > </File> + <File + RelativePath=".\soccerbotbehavior.cpp" + > + </File> + <File + RelativePath=".\soccerbotbehavior.h" + > + </File> </Files> <Globals> --- NEW FILE: soccerbotbehavior.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Thu Mar 27 2006 Copyright (C) 2006 RoboCup Simulation League Maintenance Committee This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef SOCCERBOTBEHAVIOR_H #define SOCCERBOTBEHAVIOR_H #include "behavior.h" #include <oxygen/gamecontrolserver/baseparser.h> #include <oxygen/gamecontrolserver/predicate.h> #include <zeitgeist/zeitgeist.h> class SoccerbotBehavior : public Behavior { public: struct HingeJointSense { /** joint angle */ float angle; /** joint angle rate */ float rate; HingeJointSense() : angle(0), rate(0) {}; }; struct UniversalJointSense { /** joint angle axis 1*/ float angle1; /** joint angle axis 2*/ float angle2; /** joint angle rate axis 1*/ float rate1; /** joint angle rate axis 2*/ float rate2; UniversalJointSense() : angle1(0), angle2(0), rate1(0), rate2(0) {}; }; enum JointID { JID_HEAD_1 = 0, JID_HEAD_2 = 1, JID_LLEG_1 = 2, JID_RLEG_1 = 3, JID_LLEG_2_3 = 4, JID_RLEG_2_3 = 5, JID_LLEG_4 = 6, JID_RLEG_4 = 7, JID_LLEG_5_6 = 8, JID_RLEG_5_6 = 9, JID_LARM_1_2 = 10, JID_RARM_1_2 = 11, JID_LARM_3 = 12, JID_RARM_3 = 13, JID_LARM_4 = 14, JID_RARM_4 = 15, JID_LARM_5 = 16, JID_RARM_5 = 17 }; enum BehaviorState { ARM_UP = 0, ARM_ROTATE = 1, ARM_WAVE_1 = 2, ARM_WAVE_2 = 3 }; public: SoccerbotBehavior(); virtual std::string Init(); virtual std::string Think(const std::string& message); protected: void SetupJointIDMap(); void ParseHingeJointInfo(const oxygen::Predicate& predicate); void ParseUniversalJointInfo(const oxygen::Predicate& predicate); protected: zeitgeist::Zeitgeist mZG; boost::shared_ptr<oxygen::BaseParser> mParser; // mapping from joint id to joint hinge sense object typedef std::map<JointID, HingeJointSense> THingeJointSenseMap; THingeJointSenseMap mHingeJointSenseMap; // mapping from joint id to joint hinge sense object typedef std::map<JointID, UniversalJointSense> TUniversalJointSenseMap; TUniversalJointSenseMap mUniversalJointSenseMap; // mapping from object name to joint id typedef std::map<std::string, JointID> TJointIDMap; TJointIDMap mJointIDMap; }; #endif // SOCCERBOTBEHAVIOR_H --- NEW FILE: soccerbotbehavior.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Thu Nov 8 2005 Copyright (C) 2005 Koblenz University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "soccerbotbehavior.h" #include <iostream> #include <sstream> #include <cmath> using namespace oxygen; using namespace zeitgeist; using namespace std; using namespace boost; using namespace salt; SoccerbotBehavior::SoccerbotBehavior() : mZG("." PACKAGE_NAME) { } void SoccerbotBehavior::SetupJointIDMap() { mJointIDMap.clear(); mJointIDMap["head_joint_2"] = JID_HEAD_2; mJointIDMap["head_joint_1"] = JID_HEAD_1; mJointIDMap["lleg_joint_1"] = JID_LLEG_1; mJointIDMap["rleg_joint_1"] = JID_RLEG_1; mJointIDMap["lleg_joint_2_3"] = JID_LLEG_2_3; mJointIDMap["rleg_joint_2_3"] = JID_RLEG_2_3; mJointIDMap["lleg_joint_4"] = JID_LLEG_4; mJointIDMap["rleg_joint_4"] = JID_RLEG_4; mJointIDMap["lleg_joint_5_6"] = JID_LLEG_5_6; mJointIDMap["rleg_joint_5_6"] = JID_RLEG_5_6; mJointIDMap["larm_joint_1_2"] = JID_LARM_1_2; mJointIDMap["rarm_joint_1_2"] = JID_RARM_1_2; mJointIDMap["larm_joint_3"] = JID_LARM_3; mJointIDMap["rarm_joint_3"] = JID_RARM_3; mJointIDMap["larm_joint_4"] = JID_LARM_4; mJointIDMap["rarm_joint_4"] = JID_RARM_4; mJointIDMap["larm_joint_5"] = JID_LARM_5; mJointIDMap["rarm_joint_5"] = JID_RARM_5; } string SoccerbotBehavior::Init() { mZG.GetCore()->ImportBundle("sexpparser"); mParser = shared_static_cast<BaseParser> (mZG.GetCore()->New("SexpParser")); if (mParser.get() == 0) { cerr << "unable to create SexpParser instance." << endl; } SetupJointIDMap(); // use the scene effector to build the agent and beam to a // position near the center of the playing field return "(scene rsg/agent/soccerbot.rsg)"; } void SoccerbotBehavior::ParseHingeJointInfo(const oxygen::Predicate& predicate) { //cout << "(SoccerbotBehavior) parsing HJ info" << endl; // read the object name string name; Predicate::Iterator iter(predicate); if (! predicate.GetValue(iter, "name", name)) { return; } // try to lookup the joint id TJointIDMap::iterator idIter = mJointIDMap.find(name); if (idIter == mJointIDMap.end()) { cerr << "(SoccerbotBehavior) unknown joint id!" << endl; return; } JointID jid = (*idIter).second; // read the angle value HingeJointSense sense; if (! predicate.GetValue(iter,"axis", sense.angle)) { return; } // update the map mHingeJointSenseMap[jid] = sense; } void SoccerbotBehavior::ParseUniversalJointInfo(const oxygen::Predicate& predicate) { // read the object name string name; Predicate::Iterator iter(predicate); if (! predicate.GetValue(iter, "name", name)) { return; } // try to lookup the joint id TJointIDMap::iterator idIter = mJointIDMap.find(name); if (idIter == mJointIDMap.end()) { cerr << "(SoccerbotBehavior) unknown joint id!" << endl; return; } JointID jid = (*idIter).second; // record the angle and rate values UniversalJointSense sense; // try to read axis1 angle if (! predicate.GetValue(iter,"ax1", sense.angle1)) { cerr << "(SoccerbotBehavior) could not parse universal joint angle1!" << endl; return; } // try to read axis1 rate if (! predicate.GetValue(iter,"rt1", sense.rate1)) { cerr << "(SoccerbotBehavior) could not parse universal joint rate1!" << endl; return; } // try to read axis2 angle if (! predicate.GetValue(iter,"ax2", sense.angle2)) { cerr << "(SoccerbotBehavior) could not parse universal joint angle2!" << endl; return; } // try to read axis2 rate if (! predicate.GetValue(iter,"rt2", sense.rate2)) { cerr << "(SoccerbotBehavior) could not parse universal joint rate2!" << endl; return; } //cout << "(ParseUniversalJointInfo) got angles " << sense.angle1 // << " and " << sense.angle2 << endl; // update the map mUniversalJointSenseMap[jid] = sense; } string SoccerbotBehavior::Think(const std::string& message) { //sleep(1); static const float gain = 0.1f; static BehaviorState state = ARM_UP; // parse message and extract joint angles //cout << "(SoccerbotBehavior) received message " << message << endl; shared_ptr<PredicateList> predList = mParser->Parse(message); if (predList.get() != 0) { PredicateList& list = *predList; for ( PredicateList::TList::const_iterator iter = list.begin(); iter != list.end(); ++iter ) { const Predicate& predicate = (*iter); // check for a joint percept switch(predicate.name[0]) { case 'H': // hinge joint (HJ) ParseHingeJointInfo(predicate); break; case 'U': // universal joint (UJ) ParseUniversalJointInfo(predicate); break; default: break; } } } float curAngle = 0; float newAngle = 0; // string stream for the server commands stringstream ss(""); // curAngle = mUniversalJointSenseMap[JID_LLEG_5_6].angle1; // if (curAngle < 40.0) // { // newAngle = gain * (40.0 - curAngle); // ss << "(lleg_eff_5_6 0.0 " << newAngle << ")"; // } #if 0 switch(state) { case ARM_UP: curAngle = mUniversalJointSenseMap[JID_RARM_1_2].angle2; if (curAngle < 90.0) { newAngle = gain * (90.0 - curAngle); ss << "(rarm_eff_1_2 0.0 " << newAngle << ")"; } else { state = ARM_ROTATE; } break; case ARM_ROTATE: curAngle = mHingeJointSenseMap[JID_RARM_3].angle; if (curAngle < 90.0) { newAngle = gain * (90.0 - curAngle); ss << "(rarm_eff_3 " << newAngle << ")"; } else { state = ARM_WAVE_1; } break; case ARM_WAVE_1: curAngle = mHingeJointSenseMap[JID_RARM_4].angle; if (curAngle < 90.0) { newAngle = gain * (90.0 - curAngle); ss << "(rarm_eff_4 " << newAngle << ")"; } else { state = ARM_WAVE_2; } break; case ARM_WAVE_2: curAngle = mHingeJointSenseMap[JID_RARM_4].angle; if (curAngle > 60.0 || curAngle <= 59.5) { newAngle = gain * (60.0 - curAngle); ss << "(rarm_eff_4 " << newAngle << ")"; } else { state = ARM_WAVE_1; } break; default: break; } #endif // cout << "+++" << endl; // cout << "current angle: " << curAngle << endl; // cout << "desired angle: " << newAngle << endl; // cout << "(Behavior) sending string " << ss.str() << " to server" << endl; // cout << "State is " << state << endl; // cout << "---" << endl; //return ss.str(); return string(""); } |