JointController.cpp
Go to the documentation of this file.
00001 #include "JointController.h"
00002 
00003 #include "physics/Model.hh"
00004 #include "physics/World.hh"
00005 #include "physics/Joint.hh"
00006 #include "physics/Link.hh"
00007 
00008 using namespace gazebo;
00009 
00010 JointController::JointController(physics::JointPtr _jointPtr, bool _advancedMode)
00011 : jointPtr(_jointPtr)
00012 , position(0.)
00013 , velocity(0.)
00014 , effort(0.)
00015 , advancedMode(_advancedMode)
00016 , controlMode(POS_COM)
00017 , fault(OK)
00018 {
00019     currStatusPtr.reset(new nasa_r2_common_msgs::JointStatus);
00020     currStatusPtr->publisher = "GazeboSim";
00021     currStatusPtr->joint = _jointPtr->GetName();
00022 
00023     // initialize some parameters for sim only
00024     // this approach may change as robot control develops
00025     currStatusPtr->bridgeEnabled = true; // bridge enable status ignored, but set to true to start
00026     currStatusPtr->motorPowerDetected = true; // always true when sim running
00027     currStatusPtr->embeddedMotCom = true; // default to true on startup
00028 
00029     ROS_DEBUG("GetLimits for %s", _jointPtr->GetName().c_str());
00030     jointLowLimit = _jointPtr->GetLowStop(0).GetAsRadian();
00031     jointHighLimit = _jointPtr->GetHighStop(0).GetAsRadian();
00032 
00033     // set brake based on mode
00034     releaseBrake(!_advancedMode);
00035 }
00036 
00037 JointController::~JointController()
00038 {
00039 }
00040 
00041 void JointController::setPosPid(double _p, double _i, double _d,
00042                                 double _imax, double _imin, double _cmdMax, double _cmdMin)
00043 {
00044     boost::mutex::scoped_lock lock(controllerMutex);
00045     posPid.Init(_p, _i, _d, _imax, _imin, _cmdMax, _cmdMin);
00046 }
00047 
00048 void JointController::setVelPid(double _p, double _i, double _d,
00049                                 double _imax, double _imin, double _cmdMax, double _cmdMin)
00050 {
00051     boost::mutex::scoped_lock lock(controllerMutex);
00052     velPid.Init(_p, _i, _d, _imax, _imin, _cmdMax, _cmdMin);
00053 }
00054 
00055 void JointController::setPosTarget(double target)
00056 {
00057     boost::mutex::scoped_lock lock(controllerMutex);
00058     position = target;
00059 
00060     // if not using advanced mode, target calls determine current mode
00061     if (!advancedMode)
00062     {
00063         controlMode = POS_COM;
00064     }
00065 }
00066 
00067 void JointController::setVelTarget(double target)
00068 {
00069     boost::mutex::scoped_lock lock(controllerMutex);
00070     velocity = target;
00071 
00072     // if not using advanced mode, target calls determine current mode
00073     if (!advancedMode)
00074     {
00075         controlMode = VEL_COM;
00076     }
00077 }
00078 
00079 void JointController::setEffortTarget(double target)
00080 {
00081     boost::mutex::scoped_lock lock(controllerMutex);
00082     effort = target;
00083 
00084     // if not using advanced mode, target calls determine current mode
00085     if (!advancedMode)
00086     {
00087         controlMode = TORQ_COM;
00088     }
00089 }
00090 
00091 void JointController::update(common::Time& stepTime)
00092 {
00093     boost::mutex::scoped_lock lock(controllerMutex);
00094 
00095     if (advancedMode && (!currStatusPtr->embeddedMotCom || !currStatusPtr->motorEnabled))
00096     {
00097         // not ready for command
00098         return;
00099     }
00100 
00101     double cmd = 0;
00102     switch (controlMode)
00103     {
00104     case TORQ_COM:
00105         cmd = effort;
00106         break;
00107     case POS_COM:
00108         cmd = posPid.Update(jointPtr->GetAngle(0).GetAsRadian() - position, stepTime);
00109         break;
00110     case VEL_COM:
00111         cmd = velPid.Update(jointPtr->GetVelocity(0) - velocity, stepTime);
00112         break;
00113     case IMP_COM:
00114     {
00115         static bool warnReported = false;
00116         if (!warnReported)
00117         {
00118             ROS_WARN("GazeboInterface JointController received impedence command which is unsupported. This message will only be shown once.");
00119             warnReported = true;
00120         }
00121         break;
00122     }
00123     default:
00124     {
00125         static bool warnReported = false;
00126         if (!warnReported)
00127         {
00128             ROS_WARN("GazeboInterface JointController received unsupported joint command. This message will only be shown once.");
00129             warnReported = true;
00130         }
00131     }
00132     }
00133 
00134     jointPtr->SetForce(0, cmd);
00135 }
00136 
00137 void JointController::setJointControl(const nasa_r2_common_msgs::JointControl::ConstPtr& msg)
00138 {
00139     if (msg->joint != currStatusPtr->joint)
00140     {
00141         ROS_WARN("GazeboInterface setJointControl %s recieved control command for the wrong joint (%s), ignored",
00142                  currStatusPtr->joint.c_str(), msg->joint.c_str());
00143         return;
00144     }
00145     else
00146     {
00147         boost::mutex::scoped_lock lock(controllerMutex);
00148         // ignore publisher string
00149         // ignore registerValue uint32
00150         currStatusPtr->bridgeEnabled = msg->enableBridge;
00151         currStatusPtr->motorEnabled = msg->enableMotor;
00152         currStatusPtr->embeddedMotCom = msg->embeddedMotCom;
00153         controlMode = (JointControlMode)msg->controlMode;
00154         lock.unlock();
00155         if (msg->clearFaults)
00156         {
00157             clearFaults();
00158         }
00159         releaseBrake(msg->releaseBrake);
00160     }
00161 }
00162 
00163 const nasa_r2_common_msgs::JointStatus& JointController::getJointStatus() const
00164 {
00165     return *currStatusPtr;
00166 }
00167 
00168 void JointController::clearFaults()
00169 {
00170     boost::mutex::scoped_lock lock(controllerMutex);
00171     fault = OK;
00172     currStatusPtr->jointFaulted = false;
00173 }
00174 
00175 void JointController::releaseBrake(bool release)
00176 {
00177     boost::mutex::scoped_lock lock(controllerMutex);
00178     ROS_DEBUG("%s brake for %s", release ? "release" : "set", jointPtr->GetName().c_str());
00179     if (release)
00180     {
00181         jointPtr->SetHighStop(0, jointHighLimit);
00182         jointPtr->SetLowStop(0, jointLowLimit);
00183     }
00184     else
00185     {
00186         math::Angle currAngle = jointPtr->GetAngle(0);
00187         jointPtr->SetHighStop(0, currAngle);
00188         jointPtr->SetLowStop(0, currAngle);
00189     }
00190     currStatusPtr->brakeReleased = release;
00191 }


gazebo_interface
Author(s): rctaylo2
autogenerated on Mon Oct 6 2014 02:44:38