00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "nAxesControllerVel.hpp"
00022 #include <ocl/ComponentLoader.hpp>
00023 #include <assert.h>
00024
00025 namespace OCL{
00026
00027 using namespace RTT;
00028 using namespace std;
00029 typedef nAxesControllerVel MyType;
00030
00031 nAxesControllerVel::nAxesControllerVel(string name)
00032 : TaskContext(name,PreOperational),
00033 reset_all_mtd( "resetAll", &MyType::resetAll, this),
00034 resetAxis_mtd( "resetAxis", &MyType::resetAxis, this),
00035 p_m_port("nAxesSensorPosition"),
00036 v_d_port("nAxesDesiredVelocity"),
00037 v_out_port("nAxesOutputVelocity"),
00038 gain_prop("K", "Proportional Gain"),
00039 num_axes_prop("num_axes","Number of Axes")
00040 {
00041
00042
00043
00044 this->ports()->addPort(&p_m_port);
00045 this->ports()->addPort(&v_d_port);
00046 this->ports()->addPort(&v_out_port);
00047
00048
00049 this->properties()->addProperty(&num_axes_prop);
00050 this->properties()->addProperty(&gain_prop);
00051
00052
00053 this->methods()->addMethod( &reset_all_mtd,"reset all axes of the controller");
00054 this->methods()->addMethod( &resetAxis_mtd,"reset a single axis of the controller",
00055 "axis","axis to reset");
00056
00057 }
00058
00059
00060 nAxesControllerVel::~nAxesControllerVel(){};
00061
00062 bool nAxesControllerVel::configureHook(){
00063 num_axes=num_axes_prop.rvalue();
00064 if(gain_prop.value().size()!=num_axes){
00065 Logger::In in(this->getName().data());
00066 log(Error)<<"Size of "<<gain_prop.getName()
00067 <<" does not match "<<num_axes_prop.getName()
00068 <<endlog();
00069 return false;
00070 }
00071
00072 gain.resize(num_axes);
00073 gain=gain_prop.rvalue();
00074
00075
00076 p_m.resize(num_axes);
00077 p_d.resize(num_axes);
00078 v_d.resize(num_axes);
00079 is_initialized.resize(num_axes);
00080 time_begin.resize(num_axes);
00081
00082
00083 v_out.assign(num_axes,0);
00084 v_out_port.Set(v_out);
00085 return true;
00086
00087 }
00088
00089 bool nAxesControllerVel::startHook()
00090 {
00091
00092 if(!p_m_port.ready()){
00093 Logger::In in(this->getName().data());
00094 log(Error)<<p_m_port.getName()<<" not ready"<<endlog();
00095 return false;
00096 }
00097 if(!v_d_port.ready()){
00098 Logger::In in(this->getName().data());
00099 log(Error)<<v_d_port.getName()<<" not ready"<<endlog();
00100 return false;
00101 }
00102 if(p_m_port.Get().size()!=num_axes){
00103 Logger::In in(this->getName().data());
00104 log(Error)<<"Size of "<<p_m_port.getName()<<": "<<p_m_port.Get().size()<<" != " << num_axes<<endlog();
00105 return false;
00106 }
00107 if(v_d_port.Get().size()!=num_axes){
00108 Logger::In in(this->getName().data());
00109 log(Error)<<"Size of "<<v_d_port.getName()<<": "<<v_d_port.Get().size()<<" != " << num_axes<<endlog();
00110 return false;
00111 }
00112
00113
00114 for(unsigned int i=0; i<num_axes; i++)
00115 is_initialized[i] = false;
00116
00117 return true;
00118 }
00119
00120 void nAxesControllerVel::updateHook()
00121 {
00122
00123 p_m = p_m_port.Get();
00124 v_d = v_d_port.Get();
00125
00126
00127 for (unsigned int i=0; i<num_axes; i++){
00128 if (!is_initialized[i]){
00129 is_initialized[i] = true;
00130 p_d[i] = p_m[i];
00131 time_begin[i] = TimeService::Instance()->getTicks();
00132 }
00133 }
00134
00135
00136 for(unsigned int i=0; i<num_axes; i++){
00137 double time_difference = TimeService::Instance()->secondsSince(time_begin[i]);
00138 p_d[i] += v_out[i] * time_difference;
00139 v_out[i] = (gain[i] * (p_d[i] - p_m[i])) + v_d[i];
00140 time_begin[i] = TimeService::Instance()->getTicks();
00141 }
00142 v_out_port.Set(v_out);
00143 }
00144
00145 void nAxesControllerVel::stopHook()
00146 {
00147 for(unsigned int i=0; i<num_axes; i++){
00148 v_out[i] = 0.0;
00149 }
00150 v_out_port.Set(v_out);
00151 }
00152
00153 void nAxesControllerVel::resetAll()
00154 {
00155 for(unsigned int i=0; i<num_axes; i++)
00156 is_initialized[i] = false;
00157 }
00158
00159 void nAxesControllerVel::resetAxis(int axis)
00160 {
00161 is_initialized[axis] = false;
00162 }
00163 }
00164 ORO_LIST_COMPONENT_TYPE( OCL::nAxesControllerVel )
00165