00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "nAxesGeneratorVel.hpp"
00021 #include <ocl/ComponentLoader.hpp>
00022 #include <assert.h>
00023
00024 namespace OCL
00025 {
00026 using namespace RTT;
00027 using namespace KDL;
00028 using namespace std;
00029 typedef nAxesGeneratorVel MyType;
00030
00031 nAxesGeneratorVel::nAxesGeneratorVel(string name)
00032 : TaskContext(name,PreOperational),
00033 applyVelocity_cmd( "applyVelocity", &MyType::applyVelocity,
00034 &MyType::velocityFinished, this),
00035 applyVelocities_cmd( "applyVelocities", &MyType::applyVelocities,
00036 &MyType::velocitiesFinished, this),
00037 gotoVelocity_cmd( "gotoVelocity", &MyType::gotoVelocity,
00038 &MyType::velocityFinished, this),
00039 gotoVelocities_cmd( "gotoVelocities", &MyType::gotoVelocities,
00040 &MyType::velocitiesFinished, this),
00041 setInitVelocity_mtd( "setInitVelocity", &MyType::setInitVelocity, this),
00042 setInitVelocities_mtd( "setInitVelocities", &MyType::setInitVelocities, this),
00043 v_d_port("nAxesDesiredVelocity"),
00044 a_max_prop("max_acc", "Maximum Acceleration in Trajectory"),
00045 j_max_prop("max_jerk", "Maximum Jerk in Trajectory"),
00046 num_axes_prop("num_axes","Number of Axes")
00047 {
00048
00049
00050
00051 this->properties()->addProperty(&num_axes_prop);
00052 this->properties()->addProperty(&a_max_prop);
00053 this->properties()->addProperty(&j_max_prop);
00054
00055
00056 this->ports()->addPort(&v_d_port);
00057
00058
00059 this->commands()->addCommand( &applyVelocities_cmd,"Set the velocity",
00060 "velocity", "joint velocity for all axes",
00061 "duration", "duration of movement" );
00062 this->commands()->addCommand( &applyVelocity_cmd,"Set the velocity for one axis",
00063 "axis", "selected axis",
00064 "velocity", "joint velocity for axis",
00065 "duration", "duration of movement" );
00066 this->commands()->addCommand( &gotoVelocities_cmd,"Set the velocities",
00067 "velocities", "joint velocities for all axes",
00068 "duration", "duration of movement" );
00069 this->commands()->addCommand( &gotoVelocity_cmd,"Set the velocity for one axis",
00070 "axis", "selected axis",
00071 "velocity", "joint velocity for axis",
00072 "duration", "duration of movement" );
00073
00074
00075
00076 this->methods()->addMethod( &setInitVelocity_mtd,"set initial velocity",
00077 "axis", "axis where to set velocity",
00078 "velocity", "velocity to set" );
00079 this->methods()->addMethod( &setInitVelocities_mtd,"set initial velocity",
00080 "velocities", "velocities to set" );
00081
00082 }
00083
00084 nAxesGeneratorVel::~nAxesGeneratorVel()
00085 {}
00086
00087 bool nAxesGeneratorVel::configureHook()
00088 {
00089 num_axes=num_axes_prop.rvalue();
00090 if(a_max_prop.value().size()!=num_axes){
00091 Logger::In in(this->getName().data());
00092 log(Error)<<"Size of "<<a_max_prop.getName()
00093 <<" does not match "<<num_axes_prop.getName()
00094 <<endlog();
00095 return false;
00096 }
00097
00098 if(j_max_prop.value().size()!=num_axes){
00099 Logger::In in(this->getName().data());
00100 log(Error)<<"Size of "<<j_max_prop.getName()
00101 <<" does not match "<<num_axes_prop.getName()
00102 <<endlog();
00103 return false;
00104 }
00105
00106 a_max=a_max_prop.rvalue();
00107 j_max=j_max_prop.rvalue();
00108
00109
00110 duration_desired.resize(num_axes);
00111 duration_trajectory.resize(num_axes);
00112 maintain_velocity.resize(num_axes);
00113 time_begin.resize(num_axes);
00114 time_passed.resize(num_axes);
00115 vel_profile.resize(num_axes);
00116
00117
00118 for(unsigned int i=0;i<num_axes;i++)
00119 vel_profile[i].SetMax(a_max[i],j_max[i]);
00120
00121
00122 v_d.assign(num_axes,0);
00123 v_d_port.Set(v_d);
00124 return true;
00125 }
00126
00127
00128 bool nAxesGeneratorVel::startHook()
00129 {
00130 for (unsigned int i=0; i<num_axes; i++){
00131 maintain_velocity[i] = false;
00132 }
00133
00134
00135 for (unsigned int i=0; i<num_axes; i++)
00136 applyVelocity(i, v_d[i], 0.0);
00137
00138 return true;
00139 }
00140
00141
00142 void nAxesGeneratorVel::updateHook()
00143 {
00144 for (unsigned int i = 0 ; i < num_axes ; i++)
00145 time_passed[i] = TimeService::Instance()->secondsSince(time_begin[i]);
00146
00147 for (unsigned int i = 0 ; i < num_axes ; i++){
00148
00149 if (maintain_velocity[i] || time_passed[i] <= duration_desired[i] || duration_desired[i] == 0)
00150 v_d[i] = vel_profile[i].Pos( min(time_passed[i], duration_trajectory[i]) );
00151
00152
00153 else
00154 applyVelocity(i, 0.0, 0.0);
00155 }
00156
00157 v_d_port.Set(v_d);
00158 }
00159
00160 void nAxesGeneratorVel::stopHook()
00161 {
00162 }
00163
00164
00165 bool nAxesGeneratorVel::setInitVelocity(const unsigned int axis, const double velocity)
00166 {
00167 if (axis >= num_axes){
00168 Logger::In in((this->getName()+setInitVelocity_mtd.getName()).data());
00169 log(Error)<<"parameter axis "<<axis<<" to big."<<endlog();
00170 return false;
00171 }else{
00172 v_d[axis] = velocity;
00173 applyVelocity(axis, v_d[axis], 0.0);
00174 return true;
00175 }
00176 }
00177
00178
00179 bool nAxesGeneratorVel::setInitVelocities(const vector<double>& velocity)
00180 {
00181 if(velocity.size()!=num_axes){
00182 Logger::In in((this->getName()+setInitVelocities_mtd.getName()).data());
00183 log(Error)<<"Size of parameter velocity != "<<num_axes<<endlog();
00184 return false;
00185 }
00186 for (unsigned int i=0; i<num_axes; i++)
00187 setInitVelocity(i, velocity[i]);
00188 return true;
00189 }
00190
00191 bool nAxesGeneratorVel::gotoVelocity(const unsigned int axis, const double velocity, double duration)
00192 {
00193 if (axis >= num_axes){
00194 Logger::In in((this->getName()+gotoVelocity_cmd.getName()).data());
00195 log(Error)<<"parameter axis "<<axis<<" to big."<<endlog();
00196 return false;
00197 }
00198 if (duration < 0){
00199 Logger::In in((this->getName()+gotoVelocity_cmd.getName()).data());
00200 log(Error)<<"parameter duration < 0"<<endlog();
00201 return false;
00202 }
00203
00204 time_begin[axis] = TimeService::Instance()->getTicks();
00205 time_passed[axis] = 0;
00206 maintain_velocity[axis] = true;
00207
00208
00209 vel_profile[axis].SetProfileDuration(v_d[axis], velocity, duration);
00210
00211
00212 duration_trajectory[axis] = vel_profile[axis].Duration();
00213 duration_desired[axis] = duration_trajectory[axis];
00214
00215 return true;
00216 }
00217
00218
00219 bool nAxesGeneratorVel::gotoVelocities(const vector<double>& velocity, double duration)
00220 {
00221 if (duration < 0){
00222 Logger::In in((this->getName()+gotoVelocities_cmd.getName()).data());
00223 log(Error)<<"parameter duration < 0"<<endlog();
00224 return false;
00225 }
00226 if(velocity.size()!=num_axes){
00227 Logger::In in((this->getName()+gotoVelocities_cmd.getName()).data());
00228 log(Error)<<"Size of parameter velocity != "<<num_axes<<endlog();
00229 return false;
00230 }
00231
00232 for (unsigned int i=0; i<num_axes; i++)
00233 gotoVelocity(i, velocity[i], duration);
00234
00235 return true;
00236 }
00237
00238 bool nAxesGeneratorVel::applyVelocity(const unsigned int axis, const double velocity, double duration)
00239 {
00240 if (axis >= num_axes){
00241 Logger::In in((this->getName()+applyVelocity_cmd.getName()).data());
00242 log(Error)<<"parameter axis "<<axis<<" to big."<<endlog();
00243 return false;
00244 }
00245 if (duration < 0){
00246 Logger::In in((this->getName()+applyVelocity_cmd.getName()).data());
00247 log(Error)<<"parameter duration < 0"<<endlog();
00248 return false;
00249 }
00250
00251 time_begin[axis] = TimeService::Instance()->getTicks();
00252 time_passed[axis] = 0;
00253 duration_desired[axis] = duration;
00254 maintain_velocity[axis] = false;
00255
00256
00257 vel_profile[axis].SetProfile(v_d[axis], velocity);
00258
00259
00260 duration_trajectory[axis] = vel_profile[axis].Duration();
00261
00262 return true;
00263
00264 }
00265
00266
00267 bool nAxesGeneratorVel::applyVelocities(const vector<double>& velocity, double duration)
00268 {
00269 if (duration < 0){
00270 Logger::In in((this->getName()+applyVelocities_cmd.getName()).data());
00271 log(Error)<<"parameter duration < 0"<<endlog();
00272 return false;
00273 }
00274 if(velocity.size()!=num_axes){
00275 Logger::In in((this->getName()+applyVelocities_cmd.getName()).data());
00276 log(Error)<<"Size of parameter velocity != "<<num_axes<<endlog();
00277 return false;
00278 }
00279
00280 for (unsigned int i=0; i<num_axes; i++)
00281 applyVelocity(i, velocity[i], duration);
00282
00283 return true;
00284 }
00285
00286 bool nAxesGeneratorVel::velocityFinished(const unsigned int axis) const
00287 {
00288 return (time_passed[axis] > duration_desired[axis] || duration_desired[axis] == 0);
00289 }
00290
00291
00292 bool nAxesGeneratorVel::velocitiesFinished() const
00293 {
00294 bool returnValue=true;
00295 for (unsigned int i = 0 ; i < num_axes ; i++)
00296 if (! velocityFinished(i))
00297 returnValue = false;
00298
00299 return returnValue;
00300 }
00301 }
00302
00303 ORO_LIST_COMPONENT_TYPE( OCL::nAxesGeneratorVel )
00304