PotentialFunction.cpp
Go to the documentation of this file.
00001 /*
00002  * PotentialFunction.cpp
00003  *
00004  *  Created on: Sep 8, 2012
00005  *      Author: mriedel
00006  */
00007 
00008 #include <telekyb_calculus/Potentials/PotentialFunction.hpp>
00009 
00010 #include <telekyb_defines/telekyb_defines.hpp>
00011 
00012 #include <ros/console.h>
00013 
00014 namespace TELEKYB_NAMESPACE {
00015 
00016 PotentialFunction::PotentialFunction(const std::string& potentialFunctionName_, PotentialFunctionType type_)
00017         : options(potentialFunctionName_), type(type_)
00018 {
00019 
00020 //      //TODO: Implement exception
00021         if (options.tPotFuncZeroD->isOnInitialValue()) {
00022                 ROS_ERROR("%s on initial value! This is most certainly not what you want!", options.tPotFuncZeroD->getNSName().c_str());
00023         }
00024         if (options.tPotFuncInfD->isOnInitialValue()) {
00025                 ROS_ERROR("%s on initial value! This is most certainly not what you want!", options.tPotFuncInfD->getNSName().c_str());
00026         }
00027         if (options.tPotFuncSatValue->isOnInitialValue()) {
00028                 ROS_ERROR("%s on initial value! This is most certainly not what you want!", options.tPotFuncSatValue->getNSName().c_str());
00029         }
00030 
00031 //      optionStruct.tPotFuncZeroD = options.tPotFuncZeroD->getValue();
00032 //      optionStruct.tPotFuncInfD = options.tPotFuncInfD->getValue();
00033 //      optionStruct.tPotFuncSatValue = options.tPotFuncSatValue->getValue();
00034 //      optionStruct.tPotFuncGain = options.tPotFuncGain->getValue();
00035 
00036         updateBoundsValues();
00037 
00038         // Monitor Changes
00039         registerOptionListeners();
00040 }
00041 PotentialFunction::	PotentialFunction(const std::string& potentialFunctionName_, PotentialFunctionType type_,
00042                 double tPotFuncZeroD_, double tPotFuncInfD_, double tPotFuncSatValue_, double tPotFuncGain_)
00043         : options(potentialFunctionName_), type(type_)
00044 {
00045 
00046         // set check automatic
00047         options.tPotFuncZeroD->setValue(tPotFuncZeroD_);
00048         options.tPotFuncInfD->setValue(tPotFuncInfD_);
00049         options.tPotFuncSatValue->setValue(tPotFuncSatValue_);
00050         options.tPotFuncGain->setValue(tPotFuncGain_);
00051 
00052         // no callbacks implemented yet
00053         updateBoundsValues();
00054 
00055         // Monitor Changes
00056         registerOptionListeners();
00057 }
00058 
00059 PotentialFunction::~PotentialFunction() {
00060         options.tPotFuncZeroD->unRegisterOptionListener(this);
00061         options.tPotFuncInfD->unRegisterOptionListener(this);
00062         options.tPotFuncSatValue->unRegisterOptionListener(this);
00063         options.tPotFuncGain->unRegisterOptionListener(this);
00064 }
00065 
00066 void PotentialFunction::registerOptionListeners() {
00067         options.tPotFuncZeroD->registerOptionListener(this);
00068         options.tPotFuncInfD->registerOptionListener(this);
00069         options.tPotFuncSatValue->registerOptionListener(this);
00070         options.tPotFuncGain->registerOptionListener(this);
00071 }
00072 
00073 void PotentialFunction::updateBoundsValues() {
00074 //      ROS_INFO("Called updateBoundsValues()");
00075         //PotentialFunctionType type = getType();
00076 
00077         double zeroDistance = options.tPotFuncZeroD->getValue();
00078         double infDistance = options.tPotFuncInfD->getValue();
00079 
00080 //      ROS_INFO("Values: tPotFuncZeroD: %f, tPotFuncInfD: %f", zeroDistance, infDistance);
00081 
00082         // Beware: Do not check for equality here. Otherwise: Infinite loop.
00083         if (zeroDistance < infDistance && type == PotentialFunctionType::Repulsive) {
00084                 ROS_ERROR("Potential Function Type %s, with d_0(%f) < d_inf(%f). Inverting...", type.str(), zeroDistance, infDistance);
00085                 options.tPotFuncZeroD->setValue(infDistance);
00086                 options.tPotFuncInfD->setValue(zeroDistance);
00087         } else if (infDistance < zeroDistance && type == PotentialFunctionType::Attractive) {
00088                 ROS_ERROR("Potential Function Type %s, with d_0(%f) > d_inf(%f). Inverting...", type.str(), zeroDistance, infDistance);
00089                 options.tPotFuncZeroD->setValue(infDistance);
00090                 options.tPotFuncInfD->setValue(zeroDistance);
00091         } else {
00092                 // everything ok;
00093         }
00094 }
00095 
00096 PotentialFunctionType PotentialFunction::getType() const {
00097         return type;
00098 }
00099 
00100 PotentialFunctionOptions& PotentialFunction::getOptions() {
00101         return options;
00102 }
00103 
00104 double PotentialFunction::getPotential(double d) const {
00105         if (type == PotentialFunctionType::Repulsive) {
00106                 // repulsive
00107                 if (d < options.tPotFuncInfD->getValue()) {
00108                         return options.tPotFuncSatValue->getValue();
00109                 } else if (d >= options.tPotFuncZeroD->getValue()) {
00110                         return 0.0;
00111                 } else {
00112                         return std::min(getPotentialImpl(d), options.tPotFuncSatValue->getValue());
00113                 }
00114 
00115         } else {
00116                 // attractive
00117                 if (d <= options.tPotFuncZeroD->getValue()) {
00118                         return 0.0;
00119                 } else if (d > options.tPotFuncInfD->getValue()) {
00120                         return options.tPotFuncSatValue->getValue();
00121                 } else {
00122                         return std::min(getPotentialImpl(d), options.tPotFuncSatValue->getValue());
00123                 }
00124         }
00125 }
00126 
00127 void PotentialFunction::optionDidChange(const Option<double>* option_) {
00128         if (option_ == options.tPotFuncZeroD) {
00129                 ROS_WARN("Changed tPotFuncZeroD");
00130                 updateBoundsValues();
00131         } else if (option_ == options.tPotFuncInfD) {
00132                 ROS_WARN("Changed tPotFuncInfD");
00133                 updateBoundsValues();
00134         } else {
00135                 // we don't care
00136         }
00137 }
00138 
00139 } /* namespace telekyb */
00140 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


telekyb_calculus
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:13:18