00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "nAxesControllerPos.hpp"
00023 #include <ocl/ComponentLoader.hpp>
00024
00025 #include <assert.h>
00026
00027 namespace OCL
00028 {
00029
00030 using namespace RTT;
00031 using namespace std;
00032 typedef nAxesControllerPos MyType;
00033
00034
00035 nAxesControllerPos::nAxesControllerPos(string name)
00036 : TaskContext(name,PreOperational),
00037 measureOffset( "measureVelocityOffset", &MyType::startMeasuringOffsets,
00038 &MyType::finishedMeasuringOffsets, this),
00039 p_meas_port("nAxesSensorPosition"),
00040 p_desi_port("nAxesDesiredPosition"),
00041 v_out_port("nAxesOutputVelocity"),
00042 offset_attr("nAxesVelocityOffset"),
00043 gain_prop("K", "Proportional Gain"),
00044 num_axes_prop("num_axes","Number of Axes")
00045 {
00046
00047
00048
00049 this->ports()->addPort(&p_meas_port);
00050 this->ports()->addPort(&p_desi_port);
00051 this->ports()->addPort(&v_out_port);
00052
00053
00054 this->properties()->addProperty(&num_axes_prop);
00055 this->properties()->addProperty(&gain_prop);
00056 this->attributes()->addAttribute(&offset_attr);
00057
00058
00059 this->commands()->addCommand( &measureOffset,
00060 "calculate the velocity offset on the axes",
00061 "time_sleep", "time to wait before starting measurement",
00062 "num_samples", "number of samples to take");
00063 }
00064
00065 nAxesControllerPos::~nAxesControllerPos(){};
00066
00067 bool nAxesControllerPos::configureHook()
00068 {
00069
00070 num_axes=num_axes_prop.rvalue();
00071 if(gain_prop.value().size()!=num_axes){
00072 Logger::In in(this->getName().data());
00073 log(Error)<<"Size of "<<gain_prop.getName()
00074 <<" does not match "<<num_axes_prop.getName()
00075 <<endlog();
00076 return false;
00077 }
00078
00079 gain=gain_prop.rvalue();
00080
00081
00082 p_meas.resize(num_axes);
00083 p_desi.resize(num_axes);
00084 offset_measurement.resize(num_axes);
00085
00086
00087 v_out.assign(num_axes,0);
00088 v_out_port.Set(v_out);
00089 offset_attr.set(offset_measurement);
00090 return true;
00091 }
00092
00093
00094 bool nAxesControllerPos::startHook()
00095 {
00096
00097 if(!p_meas_port.ready()){
00098 Logger::In in(this->getName().data());
00099 log(Error)<<p_meas_port.getName()<<" not ready"<<endlog();
00100 return false;
00101 }
00102 if(!p_desi_port.ready()){
00103 Logger::In in(this->getName().data());
00104 log(Error)<<p_desi_port.getName()<<" not ready"<<endlog();
00105 return false;
00106 }
00107 if(p_meas_port.Get().size()!=num_axes){
00108 Logger::In in(this->getName().data());
00109 log(Error)<<"Size of "<<p_meas_port.getName()<<": "<<p_meas_port.Get().size()<<" != " << num_axes<<endlog();
00110 return false;
00111 }
00112 if(p_desi_port.Get().size()!=num_axes){
00113 Logger::In in(this->getName().data());
00114 log(Error)<<"Size of "<<p_desi_port.getName()<<": "<<p_desi_port.Get().size()<<" != " << num_axes<<endlog();
00115 return false;
00116 }
00117
00118
00119 is_measuring = false;
00120
00121 return true;
00122
00123 }
00124
00125
00126 void nAxesControllerPos::updateHook()
00127 {
00128
00129 p_meas = p_meas_port.Get();
00130 p_desi = p_desi_port.Get();
00131
00132
00133 for(unsigned int i=0; i<num_axes; i++)
00134 v_out[i] = gain[i] * (p_desi[i] - p_meas[i]);
00135
00136
00137 if (is_measuring && TimeService::Instance()->secondsSince(time_begin) > time_sleep){
00138 for (unsigned int i=0; i<num_axes; i++)
00139 offset_measurement[i] += v_out[i] / num_samples;
00140 num_samples_taken++;
00141 if (num_samples_taken == num_samples){
00142 is_measuring = false;
00143 offset_attr.set(offset_measurement);
00144 }
00145 }
00146
00147 v_out_port.Set(v_out);
00148 }
00149
00150
00151
00152 void nAxesControllerPos::stopHook()
00153 {
00154 for(unsigned int i=0; i<num_axes; i++){
00155 v_out[i] = 0.0;
00156 }
00157 v_out_port.Set(v_out);
00158 }
00159
00160 bool nAxesControllerPos::startMeasuringOffsets(double _time_sleep, int _num_samples)
00161 {
00162 Logger::In in(this->getName().data());
00163 log(Info) <<"Start measuring offsets"<<endlog();
00164
00165
00166 if (is_measuring)
00167 return false;
00168
00169
00170 else{
00171 for (unsigned int i=0; i<num_axes; i++){
00172 offset_measurement[i] = 0;
00173 }
00174 time_sleep = max(1.0, _time_sleep);
00175 time_begin = TimeService::Instance()->getTicks();
00176 num_samples = max(1,_num_samples);
00177 num_samples_taken = 0;
00178 is_measuring = true;
00179 return true;
00180 }
00181 }
00182
00183
00184 bool nAxesControllerPos::finishedMeasuringOffsets() const
00185 {
00186 return !is_measuring;
00187 }
00188
00189
00190 }
00191
00192 ORO_CREATE_COMPONENT_TYPE()
00193 ORO_LIST_COMPONENT_TYPE( OCL::nAxesControllerPos )
00194
00195