Revision: 7747
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7747&view=rev
Author:   robotos
Date:     2009-05-30 09:37:34 +0000 (Sat, 30 May 2009)

Log Message:
-----------
new GetIface method in Controller. 
Each controller was finding its ifaces in a different way, some refering to [0] 
which is unsafe, others iterating, etc
Now is unified and code size is reduced

Modified Paths:
--------------
    code/gazebo/trunk/server/controllers/Controller.cc
    code/gazebo/trunk/server/controllers/Controller.hh
    code/gazebo/trunk/server/controllers/actarray/bandit/Bandit_Actarray.cc
    code/gazebo/trunk/server/controllers/actarray/generic/Generic_Actarray.cc
    code/gazebo/trunk/server/controllers/audio/Audio.cc
    code/gazebo/trunk/server/controllers/bumper/generic/Generic_Bumper.cc
    code/gazebo/trunk/server/controllers/camera/generic/Generic_Camera.cc
    code/gazebo/trunk/server/controllers/camera/stereo/Stereo_Camera.cc
    code/gazebo/trunk/server/controllers/gripper/pioneer2/Pioneer2_Gripper.cc
    code/gazebo/trunk/server/controllers/imu/Generic_Imu.cc
    code/gazebo/trunk/server/controllers/laser/sicklms200/SickLMS200_Laser.cc
    code/gazebo/trunk/server/controllers/opaque/CMakeLists.txt
    code/gazebo/trunk/server/controllers/opaque/jointforce/JointForce.cc
    
code/gazebo/trunk/server/controllers/position2d/differential/Differential_Position2d.cc
    
code/gazebo/trunk/server/controllers/position2d/holonome3sw/Holonome3Sw_Position2d.cc
    
code/gazebo/trunk/server/controllers/position2d/steering/Steering_Position2d.cc
    code/gazebo/trunk/server/controllers/ptz/generic/Generic_PTZ.cc

Modified: code/gazebo/trunk/server/controllers/Controller.cc
===================================================================
--- code/gazebo/trunk/server/controllers/Controller.cc  2009-05-30 05:43:06 UTC 
(rev 7746)
+++ code/gazebo/trunk/server/controllers/Controller.cc  2009-05-30 09:37:34 UTC 
(rev 7747)
@@ -103,9 +103,6 @@
     // Get the name of the iface
     std::string ifaceName = childNode->GetString("name","",1);
 
-    this->ifaceTypes.push_back(ifaceType);
-    this->ifaceNames.push_back(ifaceName);
-
     try
     {
       // Use the factory to get a new iface based on the type
@@ -155,19 +152,16 @@
 /// Save the controller.
 void Controller::Save(std::string &prefix, std::ostream &stream)
 {
-  std::vector<std::string>::iterator iter1;
-  std::vector<std::string>::iterator iter2;
+  std::vector<Iface*>::iterator iter;
 
   stream << prefix << "<controller:" << this->typeName << " name=\"" << 
this->nameP->GetValue() << "\">\n";
 
   stream << prefix << "  " << *(this->updatePeriodP) << "\n";
 
   // Ouptut the interfaces
-  for (iter1 = this->ifaceTypes.begin(), iter2=this->ifaceNames.begin(); 
-       iter1 != this->ifaceTypes.end() && iter2 != this->ifaceNames.end(); 
-       iter1++, iter2++)
+  for (iter = this->ifaces.begin(); iter != this->ifaces.end(); iter++)
   {
-    stream << prefix << "  <interface:" << *(iter1) << " name=\"" << *(iter2) 
<< "\"/>\n";
+    stream << prefix << "  <interface:" << (*iter)->GetType() << " name=\"" << 
(*iter)->GetId() << "\"/>\n";
   }
 
   std::string p = prefix + "  ";
@@ -256,3 +250,31 @@
 {
   return this->nameP->GetValue();
 }
+
+////////////////////////////////////////////////////////////////////////////////
+// Get a interface of the controller
+Iface* Controller::GetIface(std::string type, bool mandatory, int number)
+{
+  std::vector<Iface*>::iterator iter;
+  int order = number;
+  Iface *iface = NULL;
+
+  for (iter = this->ifaces.begin(); iter != this->ifaces.end(); iter++)
+  {
+    if ((*iter)->GetType() == type)
+    {
+      if (order == 0)
+        iface = (*iter);
+      else
+        order --;
+    }
+  }
+  
+  if ((iface == NULL) and mandatory)
+  {
+    std::ostringstream stream;
+    stream << "Controller " << this->GetName() << "trying to get " << type << 
" interface but it is not defined";
+    gzthrow(stream.str());
+  }
+  return iface;
+}
\ No newline at end of file

Modified: code/gazebo/trunk/server/controllers/Controller.hh
===================================================================
--- code/gazebo/trunk/server/controllers/Controller.hh  2009-05-30 05:43:06 UTC 
(rev 7746)
+++ code/gazebo/trunk/server/controllers/Controller.hh  2009-05-30 09:37:34 UTC 
(rev 7747)
@@ -98,14 +98,20 @@
   /// \brief Return the name of this controller
   /// \return The name of the controller
   public: std::string GetName() const;
-
+  
+  /// \brief Return Iface by type
+  /// \param type The type of the iface to retrieve
+  /// \param number If several ifaces of the same type present, which one
+  /// \return Iface, or exception if not found. 
+  protected: Iface* GetIface(std::string type, bool mandatory=true, int 
number=0);
+  
   /// \brief The controller's name
   protected: ParamT<std::string> *nameP;
 
   /// \brief The entity that owns this controller
   protected: Entity *parent;
 
-  /// \breif flag to keep controllers updating continuously
+  /// \brief flag to keep controllers updating continuously
   protected: ParamT<bool> *alwaysOnP;
 
   /// \brief Update period 
@@ -120,9 +126,6 @@
   /// \brief Array of all the iface for this controller
   protected: std::vector<Iface*> ifaces;
 
-  private: std::vector< std::string > ifaceTypes; 
-  private: std::vector< std::string > ifaceNames; 
-
   protected: std::vector<Param*> parameters;
 };
 

Modified: 
code/gazebo/trunk/server/controllers/actarray/bandit/Bandit_Actarray.cc
===================================================================
--- code/gazebo/trunk/server/controllers/actarray/bandit/Bandit_Actarray.cc     
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/actarray/bandit/Bandit_Actarray.cc     
2009-05-30 09:37:34 UTC (rev 7747)
@@ -77,12 +77,8 @@
 {
   XMLConfigNode *jNode;
   int i =0;
-  this->myIface = dynamic_cast<ActarrayIface*>(this->ifaces[0]);
+  this->myIface = dynamic_cast<ActarrayIface*>(this->GetIface("actarray"));
 
-  if (!this->myIface)
-    gzthrow("Bandit_Actarray controller requires a Actarray Iface");
-
-
   Param::Begin(&this->parameters);
   for (i=0, jNode = node->GetChild("joint"); jNode; i++)
   {

Modified: 
code/gazebo/trunk/server/controllers/actarray/generic/Generic_Actarray.cc
===================================================================
--- code/gazebo/trunk/server/controllers/actarray/generic/Generic_Actarray.cc   
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/actarray/generic/Generic_Actarray.cc   
2009-05-30 09:37:34 UTC (rev 7747)
@@ -73,11 +73,8 @@
 void Generic_Actarray::LoadChild(XMLConfigNode *node)
 {
   XMLConfigNode *jNode;
-  this->myIface = dynamic_cast<ActarrayIface*>(this->ifaces[0]);
+  this->myIface = dynamic_cast<ActarrayIface*>(this->GetIface("actarray"));
 
-  if (!this->myIface)
-    gzthrow("Generic_Actarray controller requires a Actarray Iface");
-
   for (n_joints=0, jNode = node->GetChild("joint"); jNode; n_joints++, jNode = 
jNode->GetNext("joint"))
   {
   }

Modified: code/gazebo/trunk/server/controllers/audio/Audio.cc
===================================================================
--- code/gazebo/trunk/server/controllers/audio/Audio.cc 2009-05-30 05:43:06 UTC 
(rev 7746)
+++ code/gazebo/trunk/server/controllers/audio/Audio.cc 2009-05-30 09:37:34 UTC 
(rev 7747)
@@ -66,11 +66,7 @@
 // Load the controller
 void AudioController::LoadChild(XMLConfigNode *node)
 {
-  this->audioIface = dynamic_cast<AudioIface*>(this->ifaces[0]);
-
-  if (!this->audioIface)
-    gzthrow("Audio controller requires an audio interface");
-
+  this->audioIface = dynamic_cast<AudioIface*>(this->GetIface("audio"));
   this->openALSource = OpenAL::Instance()->CreateSource( node );
 
 }

Modified: code/gazebo/trunk/server/controllers/bumper/generic/Generic_Bumper.cc
===================================================================
--- code/gazebo/trunk/server/controllers/bumper/generic/Generic_Bumper.cc       
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/bumper/generic/Generic_Bumper.cc       
2009-05-30 09:37:34 UTC (rev 7747)
@@ -59,12 +59,7 @@
 // Load the controller
 void Generic_Bumper::LoadChild(XMLConfigNode *node)
 {
-  this->myIface = dynamic_cast<BumperIface*>(this->ifaces[0]);
-
-  if (!this->myIface) 
-  {
-    gzthrow("Generic_Bumper controller requires an BumperIface");
-  }
+  this->myIface = dynamic_cast<BumperIface*>(this->GetIface("bumper"));
 }
 
 
////////////////////////////////////////////////////////////////////////////////

Modified: code/gazebo/trunk/server/controllers/camera/generic/Generic_Camera.cc
===================================================================
--- code/gazebo/trunk/server/controllers/camera/generic/Generic_Camera.cc       
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/camera/generic/Generic_Camera.cc       
2009-05-30 09:37:34 UTC (rev 7747)
@@ -63,10 +63,7 @@
 // Load the controller
 void Generic_Camera::LoadChild(XMLConfigNode *node)
 {
-  this->cameraIface = dynamic_cast<CameraIface*>(this->ifaces[0]);
-
-  if (!this->cameraIface)
-    gzthrow("Generic_Camera controller requires a CameraIface");
+  this->cameraIface = dynamic_cast<CameraIface*>(this->GetIface("camera"));
 }
 
 
////////////////////////////////////////////////////////////////////////////////

Modified: code/gazebo/trunk/server/controllers/camera/stereo/Stereo_Camera.cc
===================================================================
--- code/gazebo/trunk/server/controllers/camera/stereo/Stereo_Camera.cc 
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/camera/stereo/Stereo_Camera.cc 
2009-05-30 09:37:34 UTC (rev 7747)
@@ -70,24 +70,17 @@
 // Load the controller
 void Stereo_Camera::LoadChild(XMLConfigNode *node)
 {
-  std::vector<Iface*>::iterator iter;
+  CameraIface *ciface = NULL;
 
-  for (iter = this->ifaces.begin(); iter != this->ifaces.end(); iter++)
-  {
-    if ((*iter)->GetType() == "stereo")
-      this->stereoIface = dynamic_cast<StereoCameraIface*>(*iter);
-    else if ((*iter)->GetType() == "camera")
-    {
-      CameraIface *ciface = dynamic_cast<CameraIface*>(*iter);
-      this->cameraIfaces[ciface->GetId()] = ciface;
-    }
-  }
+  this->stereoIface = 
dynamic_cast<StereoCameraIface*>(this->GetIface("stereo"));
 
+  ciface = dynamic_cast<CameraIface*>(this->GetIface("camera",true,0));
+  this->cameraIfaces[ciface->GetId()] = ciface;
+  ciface = dynamic_cast<CameraIface*>(this->GetIface("camera",true,1));
+  this->cameraIfaces[ciface->GetId()] = ciface;
+
   this->leftCameraNameP->Load(node);
   this->rightCameraNameP->Load(node);
-
-  if (!this->stereoIface)
-    gzthrow("Stereo_Camera controller requires a StereoCameraIface");
 }
 
 
////////////////////////////////////////////////////////////////////////////////

Modified: 
code/gazebo/trunk/server/controllers/gripper/pioneer2/Pioneer2_Gripper.cc
===================================================================
--- code/gazebo/trunk/server/controllers/gripper/pioneer2/Pioneer2_Gripper.cc   
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/gripper/pioneer2/Pioneer2_Gripper.cc   
2009-05-30 09:37:34 UTC (rev 7747)
@@ -100,23 +100,9 @@
 void Pioneer2_Gripper::LoadChild(XMLConfigNode *node)
 {
   XMLConfigNode *jNode;
-  this->gripIface = dynamic_cast<GripperIface*>(this->ifaces[0]);
+  this->gripIface = dynamic_cast<GripperIface*>(this->GetIface("gripper"));
+  this->actIface = dynamic_cast<ActarrayIface*>(this->GetIface("actarray"));
 
-  if (!this->gripIface)
-  {
-    this->gripIface = dynamic_cast<GripperIface*>(this->ifaces[1]);
-    if (!this->gripIface)
-      gzthrow("Pioneer2_Gripper controller requires a GripperIface");
-  }
-
-  this->actIface = dynamic_cast<ActarrayIface*>(this->ifaces[1]);
-  if (!this->actIface)
-  {
-    this->actIface = dynamic_cast<ActarrayIface*>(this->ifaces[0]);
-    if (!this->actIface)
-      gzthrow("Pioneer2_Gripper controller requires an ActarrayIface");
-  }
-
   Param::Begin(&this->parameters);
   jNode = node->GetChild("leftJoint");
   if (jNode)

Modified: code/gazebo/trunk/server/controllers/imu/Generic_Imu.cc
===================================================================
--- code/gazebo/trunk/server/controllers/imu/Generic_Imu.cc     2009-05-30 
05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/imu/Generic_Imu.cc     2009-05-30 
09:37:34 UTC (rev 7747)
@@ -64,16 +64,7 @@
 // Load the controller
 void Generic_Imu::LoadChild(XMLConfigNode *node)
 {
-  std::vector<Iface*>::iterator iter;
-
-  for (iter = this->ifaces.begin(); iter != this->ifaces.end(); iter++)
-  {
-    if ((*iter)->GetType() == "imu")
-      this->imuIface = dynamic_cast<ImuIface*>(*iter);
-  }
-
-  if (!this->imuIface) gzthrow("GenericImu controller requires a ImuIface");
-
+  this->imuIface = dynamic_cast<ImuIface*>(this->GetIface("imu"));
 }
 
 
////////////////////////////////////////////////////////////////////////////////

Modified: 
code/gazebo/trunk/server/controllers/laser/sicklms200/SickLMS200_Laser.cc
===================================================================
--- code/gazebo/trunk/server/controllers/laser/sicklms200/SickLMS200_Laser.cc   
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/laser/sicklms200/SickLMS200_Laser.cc   
2009-05-30 09:37:34 UTC (rev 7747)
@@ -68,18 +68,8 @@
 // Load the controller
 void SickLMS200_Laser::LoadChild(XMLConfigNode *node)
 {
-  std::vector<Iface*>::iterator iter;
-
-  for (iter = this->ifaces.begin(); iter != this->ifaces.end(); iter++)
-  {
-    if ((*iter)->GetType() == "laser")
-      this->laserIface = dynamic_cast<LaserIface*>(*iter);
-    else if ((*iter)->GetType() == "fiducial")
-      this->fiducialIface = dynamic_cast<FiducialIface*>(*iter);
-  }
-
-  if (!this->laserIface) gzthrow("SickLMS200_Laser controller requires a 
LaserIface");
-
+  this->laserIface = dynamic_cast<LaserIface*>(this->GetIface("laser"));
+  this->fiducialIface = 
dynamic_cast<FiducialIface*>(this->GetIface("fiducial", false));
 }
 
 
////////////////////////////////////////////////////////////////////////////////

Modified: code/gazebo/trunk/server/controllers/opaque/CMakeLists.txt
===================================================================
--- code/gazebo/trunk/server/controllers/opaque/CMakeLists.txt  2009-05-30 
05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/opaque/CMakeLists.txt  2009-05-30 
09:37:34 UTC (rev 7747)
@@ -1 +1,2 @@
 ADD_SUBDIRECTORY(jointforce)
+ADD_SUBDIRECTORY(cannon)

Modified: code/gazebo/trunk/server/controllers/opaque/jointforce/JointForce.cc
===================================================================
--- code/gazebo/trunk/server/controllers/opaque/jointforce/JointForce.cc        
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/opaque/jointforce/JointForce.cc        
2009-05-30 09:37:34 UTC (rev 7747)
@@ -64,10 +64,7 @@
     std::string jointName;
     dJointFeedback *jFeedback = new dJointFeedback;
     int i =0;
-    this->myIface = dynamic_cast<OpaqueIface*>(this->ifaces[0]);
-    if (!this->myIface) {
-        gzthrow("JointForce controller requires an  OpaqueIface");
-    }
+    this->myIface = dynamic_cast<OpaqueIface*>(this->GetIface("opaque"));
     jNode = node->GetChild("joint");
     while(jNode && i < GAZEBO_JOINTFORCE_CONTROLLER_MAX_FEEDBACKS)
     {

Modified: 
code/gazebo/trunk/server/controllers/position2d/differential/Differential_Position2d.cc
===================================================================
--- 
code/gazebo/trunk/server/controllers/position2d/differential/Differential_Position2d.cc
     2009-05-30 05:43:06 UTC (rev 7746)
+++ 
code/gazebo/trunk/server/controllers/position2d/differential/Differential_Position2d.cc
     2009-05-30 09:37:34 UTC (rev 7747)
@@ -84,11 +84,8 @@
 // Load the controller
 void Differential_Position2d::LoadChild(XMLConfigNode *node)
 {
-  this->myIface = dynamic_cast<PositionIface*>(this->ifaces[0]);
+  this->myIface = dynamic_cast<PositionIface*>(this->GetIface("position"));
 
-  if (!this->myIface)
-    gzthrow("Differential_Position2d controller requires a PositionIface");
-
   // the defaults are from pioneer2dx
   this->wheelSepP->Load(node);
   this->wheelDiamP->Load(node);

Modified: 
code/gazebo/trunk/server/controllers/position2d/holonome3sw/Holonome3Sw_Position2d.cc
===================================================================
--- 
code/gazebo/trunk/server/controllers/position2d/holonome3sw/Holonome3Sw_Position2d.cc
       2009-05-30 05:43:06 UTC (rev 7746)
+++ 
code/gazebo/trunk/server/controllers/position2d/holonome3sw/Holonome3Sw_Position2d.cc
       2009-05-30 09:37:34 UTC (rev 7747)
@@ -65,11 +65,8 @@
 // Load the controller
 void Holonome3Sw_Position2d::LoadChild(XMLConfigNode *node)
 {
-  this->myIface = dynamic_cast<PositionIface*>(this->ifaces[0]);
+  this->myIface = dynamic_cast<PositionIface*>(this->GetIface("position"));
 
-  if (!this->myIface)
-    gzthrow("Holonome3Sw_Position2d controller requires a PositionIface");
-
   // Get wheels child
   node = node->GetChild("wheels");
   if (!node)

Modified: 
code/gazebo/trunk/server/controllers/position2d/steering/Steering_Position2d.cc
===================================================================
--- 
code/gazebo/trunk/server/controllers/position2d/steering/Steering_Position2d.cc 
    2009-05-30 05:43:06 UTC (rev 7746)
+++ 
code/gazebo/trunk/server/controllers/position2d/steering/Steering_Position2d.cc 
    2009-05-30 09:37:34 UTC (rev 7747)
@@ -83,12 +83,8 @@
   float defaultMaxAngle;
 
 
-  this->myIface = dynamic_cast<PositionIface*>(this->ifaces[0]);
+  this->myIface = dynamic_cast<PositionIface*>(this->GetIface("position"));
 
-  if (!this->myIface)
-    gzthrow("Steering_Position2d controller requires a PositionIface");
-
-
   //general
   defaultTorque = node->GetFloat("torque", 1000, 0);
 

Modified: code/gazebo/trunk/server/controllers/ptz/generic/Generic_PTZ.cc
===================================================================
--- code/gazebo/trunk/server/controllers/ptz/generic/Generic_PTZ.cc     
2009-05-30 05:43:06 UTC (rev 7746)
+++ code/gazebo/trunk/server/controllers/ptz/generic/Generic_PTZ.cc     
2009-05-30 09:37:34 UTC (rev 7747)
@@ -88,11 +88,8 @@
 // Load the controller
 void Generic_PTZ::LoadChild(XMLConfigNode *node)
 {
-  this->ptzIface = dynamic_cast<PTZIface*>(this->ifaces[0]);
+  this->ptzIface = dynamic_cast<PTZIface*>(this->GetIface("ptz"));
 
-  if (!this->ptzIface)
-    gzthrow("Generic_PTZ controller requires a PTZIface");
-
   this->panJointNameP->Load(node);
   this->tiltJointNameP->Load(node);
   this->motionGainP->Load(node);


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT 
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp as they present alongside digital heavyweights like Barbarian 
Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com 
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to