PMObsAvoid.cpp
Go to the documentation of this file.
00001 /*
00002  * PMObsAvoid.cpp
00003  *
00004  *  Created on: Dec 15, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include <trajmodules/PMObsAvoid.hpp>
00009 
00010 #include <telekyb_base/ROS.hpp>
00011 
00012 // Declare
00013 PLUGINLIB_DECLARE_CLASS(tk_trajprocessor, PMObsAvoid, telekyb_traj::PMObsAvoid, TELEKYB_NAMESPACE::TrajectoryModule);
00014 
00015 #define BREAK_POS_ERROR 0.05
00016 
00017 namespace telekyb_traj {
00018 
00019 PMObsAvoidOptions::PMObsAvoidOptions()
00020         : OptionContainer("PMObsAvoid")
00021 {
00022         tObsPointsTopicName = addOption<std::string>("tObsPointsTopicName", "Topic Name of Obstacle Points",
00023                         "undef", true, true);
00024         tPMObsDerivgain = addOption<double>("tPMObsDerivgain", "Integral Gain for velocity estimation Step",
00025                         2.0, false, true);
00026         tPMObsVelocitySaturation = addOption<double>("tPMObsVelocitySaturation", "Saturates Velocity Input + gain*(poserror)",
00027                         1.0, false, true);
00028         tPMObsAccelerationSaturation = addOption<double>("tPMObsAccelerationSaturation", "Saturates Acceleration Input",
00029                         2.0, false, true);
00030 }
00031 
00032 
00033 PMObsAvoid::PMObsAvoid()
00034         : TrajectoryModule("tk_trajprocessor/PMObsAvoid", TrajModulePosType::Position, 100),
00035           obsAvoidPotentialAlg(getName())
00036 {
00037 
00038 }
00039 
00040 void PMObsAvoid::initialize()
00041 {
00042 
00043 }
00044 
00045 void PMObsAvoid::destroy()
00046 {
00047 
00048 }
00049 
00050 // set back to intial conditions
00051 void PMObsAvoid::willTurnActive()
00052 {
00053         lastObstaclePoints.clear();
00054 
00055         ros::NodeHandle n(ROSModule::Instance().getMainNodeHandle());
00056         obsPointSub = n.subscribe(options.tObsPointsTopicName->getValue(), 1, &PMObsAvoid::obsPointCB, this);
00057 
00058         obsAvoidActive = false;
00059         timeStep.reset();
00060 }
00061 
00062 // called after turning inactive
00063 void PMObsAvoid::didTurnInactive()
00064 {
00065         obsPointSub.shutdown();
00066 }
00067 
00068 bool PMObsAvoid::trajectoryStep(const TKState& currentState, TKTrajectory& trajInput)
00069 {
00070         double elapsedSec = timeStep.getElapsed().toDSec();
00071         timeStep.reset();
00072 
00073         boost::mutex::scoped_lock lastObstaclePointsLock(lastObstaclePointsMutex);
00074         Velocity3D obsVelocity = obsAvoidPotentialAlg.getObstacleVelocity(currentState.position, lastObstaclePoints);
00075 //      Acceleration3D obsAcceleration = obsAvoidPotentialAlg.getObstacleAcceleration(currentState.position, lastObstaclePoints);
00076         lastObstaclePointsLock.unlock();
00077 
00078         //ROS_INFO_STREAM("ObsVelocity: " << std::endl << obsVelocity);
00079 
00080         if (obsAvoidActive) {
00081                 //ROS_INFO("active!");
00082                 // do stuff
00083                 // condition to turn inactive?
00084                 if (obsVelocity.norm() == 0.0 && (currentState.position - lastPosition).norm() < BREAK_POS_ERROR) {
00085                         obsAvoidActive = false;
00086                 } else {
00087                         boost::mutex::scoped_lock lastObstaclePointsLock(lastObstaclePointsMutex);
00088                         obsVelocity = obsAvoidPotentialAlg.getObstacleVelocity(lastPosition, lastObstaclePoints);
00089                         lastObstaclePointsLock.unlock();
00090 
00091                         //ROS_INFO_STREAM("ObsVelocity: " << std::endl << obsVelocity);
00092                         // calculate new lastPosition and Velocity
00093                         Velocity3D velocityTerm = trajInput.velocity + options.tPMObsDerivgain->getValue()
00094                                         * ( trajInput.position - lastPosition);
00095                         if (velocityTerm.norm() > options.tPMObsVelocitySaturation->getValue()) {
00096                                 //ROS_ERROR("Saturating PMObsAvoid");
00097                                 velocityTerm = velocityTerm.normalized() * options.tPMObsVelocitySaturation->getValue();
00098                         }
00099 
00100                         //TODO: How to control this term?
00101 //                      Acceleration3D accelerationTerm = trajInput.acceleration;
00102 //                      if (accelerationTerm.norm() > options.tPMObsAccelerationSaturation->getValue()) {
00103 //                              //ROS_ERROR("Saturating PMObsAvoid");
00104 //                              accelerationTerm = accelerationTerm.normalized() * options.tPMObsAccelerationSaturation->getValue();
00105 //                      }
00106 
00107 //                      lastAcceleration = obsAcceleration + accelerationTerm; // no feedback TODO!
00108                         lastVelocity = obsVelocity + velocityTerm;
00109                         lastPosition += elapsedSec * lastVelocity;
00110 
00111                         //ROS_INFO_STREAM("Velocity: " << std::endl << lastVelocity);
00112                         //ROS_INFO_STREAM("Position: " << std::endl << lastPosition);
00113 
00114                         // set
00115                         trajInput.setPosition(lastPosition, lastVelocity);
00116 
00117                 }
00118         } else {
00119                 // inactive
00120                 if (obsVelocity.norm() != 0.0) {
00121                         // turn active
00122                         obsAvoidActive = true;
00123                         lastPosition = currentState.position;
00124                         lastVelocity = currentState.linVelocity + obsVelocity;
00125 //                      lastAcceleration = Acceleration3D::Zero(); // TODO!
00126 
00127                         // set
00128                         trajInput.position = lastPosition;
00129                         trajInput.setPosition(lastPosition, lastVelocity);
00130                 }
00131 
00132         }
00133 
00134 
00135         //trajInput.setVelocity(trajInput.velocity + obsVelocity);
00136 
00137         return true;
00138 }
00139 
00140 void PMObsAvoid::obsPointCB(const telekyb_msgs::StampedPointArray::ConstPtr& obsPointsMsg)
00141 {
00142         boost::mutex::scoped_lock lastObstaclePointsLock(lastObstaclePointsMutex);
00143         lastObstaclePoints.resize(obsPointsMsg->points.size());
00144         for (unsigned int i = 0; i < lastObstaclePoints.size(); ++i) {
00145                 lastObstaclePoints[i](0) = obsPointsMsg->points[i].x;
00146                 lastObstaclePoints[i](1) = obsPointsMsg->points[i].y;
00147                 lastObstaclePoints[i](2) = obsPointsMsg->points[i].z;
00148         }
00149 }
00150 
00151 } /* namespace telekyb_traj */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


tk_trajprocessor
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:13:29