Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <trajmodules/PMObsAvoid.hpp>
00009
00010 #include <telekyb_base/ROS.hpp>
00011
00012
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
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
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
00076 lastObstaclePointsLock.unlock();
00077
00078
00079
00080 if (obsAvoidActive) {
00081
00082
00083
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
00092
00093 Velocity3D velocityTerm = trajInput.velocity + options.tPMObsDerivgain->getValue()
00094 * ( trajInput.position - lastPosition);
00095 if (velocityTerm.norm() > options.tPMObsVelocitySaturation->getValue()) {
00096
00097 velocityTerm = velocityTerm.normalized() * options.tPMObsVelocitySaturation->getValue();
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 lastVelocity = obsVelocity + velocityTerm;
00109 lastPosition += elapsedSec * lastVelocity;
00110
00111
00112
00113
00114
00115 trajInput.setPosition(lastPosition, lastVelocity);
00116
00117 }
00118 } else {
00119
00120 if (obsVelocity.norm() != 0.0) {
00121
00122 obsAvoidActive = true;
00123 lastPosition = currentState.position;
00124 lastVelocity = currentState.linVelocity + obsVelocity;
00125
00126
00127
00128 trajInput.position = lastPosition;
00129 trajInput.setPosition(lastPosition, lastVelocity);
00130 }
00131
00132 }
00133
00134
00135
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 }