00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "CartesianGeneratorPos.hpp"
00022 #include <assert.h>
00023 #include <ocl/ComponentLoader.hpp>
00024
00025 ORO_LIST_COMPONENT_TYPE( OCL::CartesianGeneratorPos );
00026
00027 namespace OCL
00028 {
00029
00030 using namespace RTT;
00031 using namespace KDL;
00032 using namespace std;
00033
00034 CartesianGeneratorPos::CartesianGeneratorPos(string name)
00035 : TaskContext(name,PreOperational),
00036 _maximum_velocity_local(6,0.0),
00037 _maximum_acceleration_local(6,0.0),
00038 _motion_profile(6,VelocityProfile_Trap(0,0)),
00039 _is_moving(false),
00040 _moveTo( "moveTo", &CartesianGeneratorPos::moveTo,
00041 &CartesianGeneratorPos::moveFinished, this),
00042 _reset_position( "resetPosition", &CartesianGeneratorPos::resetPosition, this),
00043 _position_meas("CartesianSensorPosition"),
00044 _position_desi("CartesianDesiredPosition"),
00045 _velocity_desi("CartesianDesiredVelocity"),
00046 _maximum_velocity("max_vel", "Maximum Velocity in Trajectory",vector<double>(6,0.0)),
00047 _maximum_acceleration("max_acc", "Maximum Acceleration in Trajectory",vector<double>(6,0.0))
00048 {
00049
00050
00051
00052 this->ports()->addPort(&_position_meas);
00053 this->ports()->addPort(&_position_desi);
00054 this->ports()->addPort(&_velocity_desi);
00055
00056
00057 this->properties()->addProperty(&_maximum_velocity);
00058 this->properties()->addProperty(&_maximum_acceleration);
00059
00060
00061 this->commands()->addCommand( &_moveTo,"Set the position setpoint",
00062 "setpoint", "position setpoint for end effector",
00063 "time", "minimum time to execute trajectory" );
00064
00065
00066 this->methods()->addMethod( &_reset_position, "Reset generator's position" );
00067
00068 }
00069
00070 CartesianGeneratorPos::~CartesianGeneratorPos()
00071 {
00072 }
00073
00074 bool CartesianGeneratorPos::configureHook()
00075 {
00076
00077
00078
00079 if(_maximum_acceleration.value().size()!=6||_maximum_velocity.value().size()!=6)
00080 return false;
00081
00082 _maximum_velocity_local=_maximum_velocity.value();
00083 _maximum_acceleration_local=_maximum_acceleration.value();
00084
00085 for(unsigned int i=0;i<6;i++)
00086 _motion_profile[i].SetMax(_maximum_velocity_local[i],_maximum_acceleration_local[i]);
00087
00088 return true;
00089
00090 }
00091
00092 bool CartesianGeneratorPos::startHook()
00093 {
00094 _is_moving = false;
00095 once = true;
00096
00097 return true;
00098 }
00099
00100 void CartesianGeneratorPos::updateHook()
00101 {
00102 if (_is_moving){
00103 _time_passed = TimeService::Instance()->secondsSince(_time_begin);
00104 if ( _time_passed > _max_duration ){
00105
00106 _position_desi_local = _traject_end;
00107 SetToZero(_velocity_desi_local);
00108 _is_moving = false;
00109 }else{
00110
00111 _velocity_delta = Twist(Vector(_motion_profile[0].Pos(_time_passed),_motion_profile[1].Pos(_time_passed),_motion_profile[2].Pos(_time_passed)),
00112 Vector(_motion_profile[3].Pos(_time_passed),_motion_profile[4].Pos(_time_passed),_motion_profile[5].Pos(_time_passed)) );
00113 _position_desi_local = Frame( _traject_begin.M * Rot( _traject_begin.M.Inverse( _velocity_delta.rot ) ), _traject_begin.p + _velocity_delta.vel);
00114
00115
00116 for(unsigned int i=0; i<6; i++)
00117 _velocity_desi_local(i) = _motion_profile[i].Vel( _time_passed );
00118 }
00119 _position_desi.Set(_position_desi_local);
00120 _velocity_desi.Set(_velocity_desi_local);
00121 }else{
00122
00123 if(once){
00124 once=false;
00125 _position_desi_local = _position_meas.Get();
00126 SetToZero(_velocity_desi_local);
00127 _position_desi.Set(_position_desi_local);
00128 _velocity_desi.Set(_velocity_desi_local);
00129 }
00130 }
00131 }
00132
00133 void CartesianGeneratorPos::stopHook()
00134 {
00135 }
00136
00137 void CartesianGeneratorPos::cleanupHook()
00138 {
00139 }
00140
00141 bool CartesianGeneratorPos::moveTo(Frame frame, double time)
00142 {
00143
00144
00145 if (!_is_moving){
00146 _max_duration = 0;
00147 _traject_end = frame;
00148
00149
00150 _traject_begin = _position_meas.Get();
00151 _velocity_begin_end = diff(_traject_begin, _traject_end);
00152
00153
00154 for (unsigned int i=0; i<6; i++){
00155 _motion_profile[i].SetProfileDuration( 0, _velocity_begin_end(i), time );
00156 _max_duration = max( _max_duration, _motion_profile[i].Duration() );
00157 }
00158
00159
00160 for (unsigned int i=0; i<6; i++)
00161 _motion_profile[i].SetProfileDuration( 0, _velocity_begin_end(i), _max_duration );
00162
00163 _time_begin = TimeService::Instance()->getTicks();
00164 _time_passed = 0;
00165
00166 _is_moving = true;
00167 return true;
00168 }
00169 else
00170 return false;
00171 }
00172
00173 bool CartesianGeneratorPos::moveFinished() const
00174 {
00175 return (!_is_moving );
00176 }
00177
00178
00179 void CartesianGeneratorPos::resetPosition()
00180 {
00181 _position_desi_local = _position_meas.Get();
00182 SetToZero(_velocity_desi_local);
00183 _position_desi.Set(_position_desi_local);
00184 _velocity_desi.Set(_velocity_desi_local);
00185 _is_moving = false;
00186 }
00187 }
00188
00189
00190