Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

robot/TestRobot.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2002 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: TestRobot.cpp 1039 2004-02-11 20:50:52Z jungd $
00019   $Revision: 1.9 $
00020   $Date: 2004-02-11 15:50:52 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <robot/TestRobot>
00026 
00027 #include <base/Application>
00028 
00029 using robot::TestRobot;
00030 
00031 using base::Vector;
00032 using base::IVector;
00033 using base::Matrix4;
00034 using base::VFile;
00035 using base::Application;
00036 
00037 
00038 using consts::Pi;
00039 
00040 
00041 TestRobot::TestRobot()
00042 {
00043   base::IVector jt(6); // joint type
00044   base::Vector alpha(6), a(6), d(6), theta(6);
00045 
00046   for(Int j=0; j<6; j++) jt[j] = Revolute;
00047   // alpha,a,d,theta
00048   alpha[0] = -Pi/2.0; a[0] = 0;             d[0] = 0;             theta[0] = Pi/2.0; 
00049   alpha[1] = 0;       a[1] = 431.8/1000.0;  d[1] = 149.09/1000.0; theta[1] = 0; 
00050   alpha[2] = Pi/2.0;  a[2] = -20.32/1000.0; d[2] = 0;             theta[2] = Pi/2.0;
00051   alpha[3] = -Pi/2.0; a[3] = 0;             d[3] = 433.07/1000.0; theta[3] = 0;
00052   alpha[4] = Pi/2.0;  a[4] = 0;             d[4] = 0;             theta[4] = 0;
00053   alpha[5] = 0;       a[5] = 0;             d[5] = 56.25/1000.0;  theta[5] = 0;
00054 
00055   create("Puma",jt,alpha,a,d,theta);
00056   initManipulators();
00057 }
00058 
00059 
00060 TestRobot::TestRobot(ref<VFile> robotSpecification,
00061                      const base::Point3& initialPosition, const base::Orient& initialOrientation)
00062   : position(initialPosition), orientation(initialOrientation)
00063 {
00064   // Read in specification
00065   ref<RobotDescription> robotDescription(NewObj RobotDescription());
00066 
00067   if (robotSpecification->extension() == "xml")
00068     robotDescription->load(robotSpecification, "xml", 1.0);
00069   else
00070     throw std::invalid_argument(Exception("unsupported file type"));
00071 
00072   setRobotDescription(robotDescription);
00073   initManipulators();
00074 }
00075 
00076 
00077 TestRobot::TestRobot(ref<const robot::RobotDescription> robotDescription,
00078                      const base::Point3& initialPosition, const base::Orient& initialOrientation)
00079   : position(initialPosition), orientation(initialOrientation)
00080 {
00081   setRobotDescription(robotDescription);
00082   initManipulators();
00083 }                    
00084 
00085 
00086 
00087 TestRobot::TestRobot(const IVector& jointType, const Vector& alpha, const Vector& a, const Vector& d, const Vector& theta)
00088 {
00089   create("manipulator", jointType, alpha, a, d, theta);
00090   initManipulators();
00091 }
00092 
00093 
00094 
00095 array<std::pair<String,String> > TestRobot::controlInterfaces() const
00096 {
00097   array<std::pair<String, String> > a;
00098   a.push_back(std::make_pair<String,String>("platformPosition","PlatformPositionControl"));
00099   
00100   for(Int m=0; m<getRobotDescription()->manipulators().size();m++) {
00101     String n(base::intToString(m+1));
00102     a.push_back(std::make_pair<String,String>("manipulatorPosition"+n,"JointPositionControl"));
00103     a.push_back(std::make_pair<String,String>("manipulatorProximity"+n,"LinkProximitySensors"));
00104     a.push_back(std::make_pair<String,String>("manipulatorToolGrip"+n,"ToolGripControl"));
00105     a.push_back(std::make_pair<String,String>("toolPosition"+n,"JointPositionControl"));
00106   }
00107   return a; 
00108 }
00109 
00110 
00111 
00112 void TestRobot::create(String manipName, const IVector& jointType, const Vector& alpha, const Vector& a, const Vector& d, const Vector& theta)
00113 {
00114   const Int dof = jointType.size();
00115   Assert( alpha.size() == dof );
00116   Assert( a.size() == dof );
00117   Assert( d.size() == dof );
00118   Assert( theta.size() == dof );
00119 
00120   // create RobotDescription
00121   
00122   ref<PlatformDescription> pd = ref<PlatformDescription>(NewObj PlatformDescription());
00123   
00124   KinematicChain kc;
00125   for(Int j=0; j<dof; j++) 
00126     kc.push_back(KinematicChain::Link(KinematicChain::Link::LinkType(jointType[j]),
00127                                       alpha[j], a[j], d[j], theta[j],
00128                                       Math::degToRad(-160.0),Math::degToRad(160.0)));
00129 
00130   Matrix4 I; I.setIdentity();
00131 
00132   ref<ManipulatorDescription>  md = ref<ManipulatorDescription>(NewObj ManipulatorDescription(manipName, I, kc));
00133   array<ref<const ManipulatorDescription> > mds(1);
00134   array<Vector3> offsets(1);
00135   mds[0]=md;
00136   offsets[0]=Vector3();
00137   
00138   setRobotDescription( ref<RobotDescription>(NewObj RobotDescription("TestRobot", pd, mds, offsets)) );
00139   
00140 }
00141 
00142 
00143 void TestRobot::initManipulators()
00144 {
00145   // manipulator joint parameters array
00146   ref<const RobotDescription> rd(getRobotDescription());
00147   Int numManpis = rd->manipulators().size();
00148   qa.resize(numManpis);
00149   for (Int i=0; i<numManpis; i++) {
00150     ref<const ManipulatorDescription> md(rd->manipulators()[i]);
00151     Int dof = md->getKinematicChain().dof();
00152     qa[i].reset( zeroVector(dof) );
00153   }
00154 
00155   // tool arrays
00156   tqa.resize(numManpis);
00157   toolGrasped.resize(numManpis);
00158   toolProximity.resize(numManpis);
00159   for(Int i=0; i<numManpis; i++) {
00160     toolGrasped[i] = false;
00161     toolProximity[i] = false;
00162   }
00163   tools.resize(numManpis);
00164 }
00165 
00166 
00167 void TestRobot::placeToolInProximity(ref<const ToolDescription> toolDescription, Int manipIndex)
00168 {
00169   if (toolGrasped[manipIndex]) {
00170     if (!tools[manipIndex]->equals(toolDescription))
00171       throw std::logic_error(Exception(String("tool '") + tools[manipIndex]->getName()
00172                                        + "' must be ungrasped before placing tool '" 
00173                                        + toolDescription->getName() + "' in proximity"));
00174     return; // already in proximity (& grasped)
00175   }
00176 
00177   // nothing grasped
00178   toolProximity[manipIndex] = true;
00179   tools[manipIndex] = toolDescription;
00180 
00181 }
00182 
00183 
00184 void TestRobot::removeToolFromProximity(Int manipIndex)
00185 {
00186   if (toolGrasped[manipIndex])
00187     throw std::logic_error(Exception(String("cannot remove tool '") + tools[manipIndex]->getName()
00188                                      + "' from proximity while is it being grasped"));
00189   toolProximity[manipIndex] = false;
00190 }
00191 
00192 
00193 
00194 ref<robot::ControlInterface> TestRobot::getControlInterface(String interfaceName) throw(std::invalid_argument)
00195 {
00196   if (interfaceName == "platformPosition") {
00197     ref<TestRobot> self(this);
00198     bool mobilePlatform = getRobotDescription()->platform()->isMobile();
00199     return ref<robot::ControlInterface>(NewObj PlatformControlInterface(interfaceName,"PlatformPositionControl",self,mobilePlatform));
00200   }
00201   
00202   if (interfaceName.substr(0,19) == "manipulatorPosition") {
00203     Int n = base::stringToInt(interfaceName.substr(19));
00204     if ((n<1) || (n > getRobotDescription()->manipulators().size())) 
00205       throw std::invalid_argument(Exception(String("manipulator index ")+base::intToString(n)+" out of range"));
00206     ref<TestRobot> self(this);
00207     return ref<robot::ControlInterface>(NewObj ManipulatorControlInterface(interfaceName,"JointPositionControl",self,n-1));
00208   }
00209 
00210   if (interfaceName.substr(0,19) == "manipulatorToolGrip") {
00211     Int n = base::stringToInt(interfaceName.substr(19));
00212     if ((n<1) || (n > getRobotDescription()->manipulators().size())) 
00213       throw std::invalid_argument(Exception(String("manipulator index ")+base::intToString(n)+" out of range"));
00214     ref<TestRobot> self(this);
00215     return ref<robot::ControlInterface>(NewObj ToolControlInterface(interfaceName,"ToolGripControl",self,n-1,true));
00216   }
00217 
00218   if (interfaceName.substr(0,12) == "toolPosition") {
00219     Int n = base::stringToInt(interfaceName.substr(12));
00220     if ((n<1) || (n > getRobotDescription()->manipulators().size())) 
00221       throw std::invalid_argument(Exception(String("manipulator index ")+base::intToString(n)+" out of range"));
00222     ref<TestRobot> self(this);
00223     return ref<robot::ControlInterface>(NewObj ToolControlInterface(interfaceName,"JointPositionControl",self,n-1,false));
00224   }
00225 
00226   if (interfaceName.substr(0,20) == "manipulatorProximity") {
00227     Int n = base::stringToInt(interfaceName.substr(20));
00228     if ((n<1) || (n > getRobotDescription()->manipulators().size())) 
00229       throw std::invalid_argument(Exception(String("manipulator index ")+base::intToString(n)+" out of range"));
00230      
00231   }
00232   
00233   throw std::invalid_argument(Exception(String("unknown interface ")+interfaceName));
00234 }

Generated on Thu Jul 29 15:56:44 2004 for OpenSim by doxygen 1.3.6