Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00019 #include "SampleController.h"
00020 #include <iostream>
00021
00022 #define TIMESTEP 0.002
00023
00024 #define ANGLE_FILE "etc/Sample.pos"
00025 #define VEL_FILE "etc/Sample.vel"
00026 #define GAIN_FILE "etc/SR_PDgain.dat"
00027
00028 #define RARM_SHOULDER_P 6
00029 #define RARM_SHOULDER_R 7
00030 #define RARM_ELBOW 9
00031 #define RARM_WRIST_Y 10
00032 #define RARM_WRIST_R 12
00033
00034
00035
00036 using namespace std;
00037
00038
00039
00040 static const char* samplecontroller_spec[] =
00041 {
00042 "implementation_id", "SampleController",
00043 "type_name", "SampleController",
00044 "description", "Sample component",
00045 "version", "0.1",
00046 "vendor", "AIST",
00047 "category", "Generic",
00048 "activity_type", "DataFlowComponent",
00049 "max_instance", "10",
00050 "language", "C++",
00051 "lang_type", "compile",
00052
00053
00054 ""
00055 };
00056
00057
00058 SampleController::SampleController(RTC::Manager* manager)
00059 : RTC::DataFlowComponentBase(manager),
00060
00061 m_angleIn("angle", m_angle),
00062 m_rhsensorIn("rhsensor", m_rhsensor),
00063 m_torqueOut("torque", m_torque),
00064
00065
00066
00067
00068 dummy(0)
00069 {
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 }
00082
00083 SampleController::~SampleController()
00084 {
00085 }
00086
00087
00088 RTC::ReturnCode_t SampleController::onInitialize()
00089 {
00090
00091
00092
00093
00094 addInPort("angle", m_angleIn);
00095 addInPort("rhsensor", m_rhsensorIn);
00096
00097
00098 addOutPort("torque", m_torqueOut);
00099
00100
00101
00102
00103
00104 Pgain = new double[DOF];
00105 Dgain = new double[DOF];
00106 std::ifstream gain;
00107
00108 gain.open(GAIN_FILE);
00109 if (gain.is_open()){
00110 for (int i=0; i<DOF; i++){
00111 gain >> Pgain[i];
00112 gain >> Dgain[i];
00113 #ifdef SC_DEBUG
00114 cout << Pgain[i] << " " << Dgain[i] << " ";
00115 #endif
00116 }
00117 gain.close();
00118 #ifdef SC_DEBUG
00119 cout << endl;
00120 #endif
00121 }else{
00122 cerr << GAIN_FILE << " not opened" << endl;
00123 }
00124
00125 m_torque.data.length(DOF);
00126 m_rhsensor.data.length(6);
00127 m_angle.data.length(DOF);
00128
00129
00130
00131
00132 qold = new double[DOF];
00133
00134 #ifdef SC_DEBUG
00135 out.open("debug.log");
00136 #endif
00137
00138 return RTC::RTC_OK;
00139 }
00140
00141 RTC::ReturnCode_t SampleController::onFinalize()
00142 {
00143 closeFiles();
00144 delete [] Pgain;
00145 delete [] Dgain;
00146 delete[] qold;
00147 #ifdef SC_DEBUG
00148 out.close();
00149 #endif
00150 return RTC::RTC_OK;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 RTC::ReturnCode_t SampleController::onActivated(RTC::UniqueId ec_id)
00169 {
00170 std::cout << "on Activated" << std::endl;
00171 goal_set = true;
00172 pattern = false;
00173 remain_t = 2.0;
00174 step = 0;
00175 openFiles();
00176
00177 if(m_angleIn.isNew()){
00178 m_angleIn.read();
00179 }
00180
00181 for(int i=0; i < DOF; ++i){
00182 qold[i] = m_angle.data[i];
00183 q_ref[i] = dq_ref[i] = q_goal[i] = dq_goal[i] = 0.0;
00184 }
00185
00186 return RTC::RTC_OK;
00187 }
00188
00189
00190 RTC::ReturnCode_t SampleController::onDeactivated(RTC::UniqueId ec_id)
00191 {
00192 std::cout << "on Deactivated" << std::endl;
00193 closeFiles();
00194 return RTC::RTC_OK;
00195 }
00196
00197
00198 RTC::ReturnCode_t SampleController::onExecute(RTC::UniqueId ec_id)
00199 {
00200 if(m_angleIn.isNew()){
00201 m_angleIn.read();
00202 }
00203 if(m_rhsensorIn.isNew()){
00204 m_rhsensorIn.read();
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 if( goal_set ){
00214 goal_set = false;
00215 switch(step){
00216 case 0 :
00217 remain_t = 2.0;
00218 double dumy;
00219 angle >> dumy; vel >> dumy;
00220 for (int i=0; i<DOF; i++){
00221 angle >> q_goal[i];
00222 vel >> dq_goal[i];
00223 q_ref[i] = m_angle.data[i];
00224 }
00225 break;
00226 case 1 :
00227 pattern = true;
00228 break;
00229 case 2 :
00230 remain_t = 3.0;
00231 for(int i=0; i<DOF; i++){
00232 q_goal[i] = q_ref[i] = m_angle.data[i];
00233 }
00234 q_goal[RARM_SHOULDER_R] = -0.4;
00235 q_goal[RARM_SHOULDER_P] = 0.75;
00236 q_goal[RARM_ELBOW] = -2.0;
00237 break;
00238 case 3 :
00239 remain_t = 2.0;
00240 q_goal[RARM_ELBOW] = -1.57;
00241 q_goal[RARM_SHOULDER_P] = -0.2;
00242 q_goal[RARM_WRIST_R] = 1.5;
00243 break;
00244 case 4 :
00245 remain_t =1.0;
00246 q_goal[RARM_ELBOW] = -1.3;
00247 break;
00248 case 5 :
00249 remain_t =5.0;
00250 q_goal[RARM_SHOULDER_R] = 0.1;
00251 break;
00252 case 6 :
00253 remain_t =2.0;
00254 q_goal[RARM_WRIST_R] = -0.3;
00255 break;
00256 case 7 :
00257 remain_t =0.5;
00258 break;
00259 case 8 :
00260 remain_t =1.0;
00261 q_goal[RARM_SHOULDER_P] = 0.13;
00262 q_goal[RARM_ELBOW] = -1.8;
00263 break;
00264 case 9 :
00265 remain_t = 0.0;
00266 step = -2;
00267 break;
00268 }
00269 step++;
00270 }
00271
00272 if( step==6 && m_rhsensor.data[1] < -2 ) {
00273 remain_t = 0;
00274 q_goal[RARM_SHOULDER_R] = m_angle.data[RARM_SHOULDER_R];
00275 }
00276
00277 if( !pattern ){
00278 if (remain_t > TIMESTEP){
00279 for(int i=0; i<DOF; i++){
00280 dq_ref[i] = (q_goal[i]-q_ref[i])/remain_t;
00281 q_ref[i] = q_ref[i] + dq_ref[i]*TIMESTEP;
00282 }
00283 remain_t -= TIMESTEP;
00284 }else{
00285 for(int i=0; i<DOF; i++){
00286 dq_ref[i]= 0;
00287 q_ref[i] = q_goal[i];
00288 }
00289 if(step >=0 ) goal_set = true;
00290 }
00291 }else{
00292 angle >> dq_ref[0]; vel >> dq_ref[0];
00293 for (int i=0; i<DOF; i++){
00294 angle >> q_ref[i];
00295 vel >> dq_ref[i];
00296 }
00297 if( angle.eof() ) {
00298 pattern = false;
00299 goal_set = true;
00300 }
00301 }
00302
00303 for(int i=0; i<DOF; i++){
00304 double dq = (m_angle.data[i] - qold[i]) / TIMESTEP;
00305 m_torque.data[i] = -(m_angle.data[i] - q_ref[i]) * Pgain[i] - (dq - dq_ref[i]) * Dgain[i];
00306 qold[i] = m_angle.data[i];
00307 #ifdef SC_DEBUG
00308 out << m_angle.data[i] << " " << q_ref[i] << " " << dq << " " << dq_ref[i] << " " << m_torque.data[i] << " ";
00309 #endif
00310 }
00311 m_torqueOut.write();
00312
00313 #ifdef SC_DEBUG
00314 out << endl;
00315 #endif
00316
00317 return RTC::RTC_OK;
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 void SampleController::openFiles()
00357 {
00358 angle.open(ANGLE_FILE);
00359 if (!angle.is_open()){
00360 std::cerr << ANGLE_FILE << " not opened" << std::endl;
00361 }
00362
00363 vel.open(VEL_FILE);
00364 if (!vel.is_open()){
00365 std::cerr << VEL_FILE << " not opened" << std::endl;
00366 }
00367 }
00368 void SampleController::closeFiles()
00369 {
00370 if(angle.is_open())
00371 {
00372 angle.close();
00373 angle.clear();
00374 }
00375 if(vel.is_open()){
00376 vel.close();
00377 vel.clear();
00378 }
00379 }
00380
00381
00382 extern "C"
00383 {
00384
00385 DLL_EXPORT void SampleControllerInit(RTC::Manager* manager)
00386 {
00387 coil::Properties profile(samplecontroller_spec);
00388 manager->registerFactory(profile,
00389 RTC::Create<SampleController>,
00390 RTC::Delete<SampleController>);
00391 }
00392
00393 };
00394
00395