Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00019 #include "SamplePD_HG.h"
00020
00021 #include <iostream>
00022
00023 #define PD_DOF (17)
00024 #define HG_DOF (12)
00025 #define TIMESTEP 0.002
00026
00027 #define WAIST_P 26
00028 #define WAIST_R 27
00029 #define CHEST 28
00030 #define LARM_SHOULDER_P 19
00031 #define LARM_SHOULDER_R 20
00032 #define LARM_SHOULDER_Y 21
00033 #define LARM_ELBOW 22
00034 #define LARM_WRIST_Y 23
00035 #define LARM_WRIST_P 24
00036 #define LARM_WRIST_R 25
00037 #define RARM_SHOULDER_P 6
00038 #define RARM_SHOULDER_R 7
00039 #define RARM_SHOULDER_Y 8
00040 #define RARM_ELBOW 9
00041 #define RARM_WRIST_Y 10
00042 #define RARM_WRIST_P 11
00043 #define RARM_WRIST_R 12
00044 #define LLEG_HIP_R 13
00045 #define LLEG_HIP_P 14
00046 #define LLEG_HIP_Y 15
00047 #define LLEG_KNEE 16
00048 #define LLEG_ANKLE_P 17
00049 #define LLEG_ANKLE_R 18
00050 #define RLEG_HIP_R 0
00051 #define RLEG_HIP_P 1
00052 #define RLEG_HIP_Y 2
00053 #define RLEG_KNEE 3
00054 #define RLEG_ANKLE_P 4
00055 #define RLEG_ANKLE_R 5
00056
00057 #define ANGLE_FILE "etc/angle.dat"
00058 #define VEL_FILE "etc/vel.dat"
00059 #define ACC_FILE "etc/acc.dat"
00060
00061 #define GAIN_FILE "etc/PDgain.dat"
00062
00063 namespace {
00064 const bool CONTROLLER_BRIDGE_DEBUG = false;
00065 }
00066
00067
00068
00069
00070 static const char* SamplePD_HG_spec[] =
00071 {
00072 "implementation_id", "SamplePD_HG",
00073 "type_name", "SamplePD_HG",
00074 "description", "Sample PD component",
00075 "version", "0.1",
00076 "vendor", "AIST",
00077 "category", "Generic",
00078 "activity_type", "DataFlowComponent",
00079 "max_instance", "10",
00080 "language", "C++",
00081 "lang_type", "compile",
00082
00083
00084 ""
00085 };
00086
00087
00088 SamplePD_HG::SamplePD_HG(RTC::Manager* manager)
00089 : RTC::DataFlowComponentBase(manager),
00090
00091 m_angle_inIn("angle_in", m_angle_in),
00092 m_torqueOut("torque", m_torque),
00093 m_angle_outOut("angle_out", m_angle_out),
00094 m_velOut("vel", m_vel),
00095 m_accOut("acc", m_acc),
00096
00097
00098 dummy(0),
00099 qold(DOF)
00100 {
00101 if( CONTROLLER_BRIDGE_DEBUG )
00102 {
00103 std::cout << "SamplePD_HG::SamplePD_HG" << std::endl;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 }
00117
00118 SamplePD_HG::~SamplePD_HG()
00119 {
00120 closeFiles();
00121 delete [] Pgain;
00122 delete [] Dgain;
00123 }
00124
00125
00126 RTC::ReturnCode_t SamplePD_HG::onInitialize()
00127 {
00128
00129
00130 if( CONTROLLER_BRIDGE_DEBUG )
00131 {
00132 std::cout << "onInitialize" << std::endl;
00133 }
00134
00135
00136 addInPort("angle_in", m_angle_inIn);
00137
00138
00139 addOutPort("torque", m_torqueOut);
00140 addOutPort("angle_out", m_angle_outOut);
00141 addOutPort("vel", m_velOut);
00142 addOutPort("acc", m_accOut);
00143
00144 Pgain = new double[DOF];
00145 Dgain = new double[DOF];
00146
00147 gain.open(GAIN_FILE);
00148 if (gain.is_open()){
00149 for (int i=0; i<DOF; i++){
00150 gain >> Pgain[i];
00151 gain >> Dgain[i];
00152 }
00153 gain.close();
00154 }else{
00155 std::cerr << GAIN_FILE << " not opened" << std::endl;
00156 }
00157
00158
00159 m_angle_in.data.length(DOF);
00160 m_angle_out.data.length(HG_DOF);
00161 m_vel.data.length(HG_DOF);
00162 m_acc.data.length(HG_DOF);
00163 m_torque.data.length(PD_DOF);
00164
00165 return RTC::RTC_OK;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 RTC::ReturnCode_t SamplePD_HG::onActivated(RTC::UniqueId ec_id)
00192 {
00193 std::cout << "on Activated" << std::endl;
00194 openFiles();
00195
00196 if(m_angle_inIn.isNew()){
00197 m_angle_inIn.read();
00198 }
00199
00200 for(int i=0; i < DOF; ++i){
00201 qold[i] = m_angle_in.data[i];
00202 q_ref[i] = dq_ref[i] = ddq_ref[i] = 0.0;
00203 }
00204
00205 return RTC::RTC_OK;
00206 }
00207
00208
00209 RTC::ReturnCode_t SamplePD_HG::onDeactivated(RTC::UniqueId ec_id)
00210 {
00211 std::cout << "on Deactivated" << std::endl;
00212 closeFiles();
00213 return RTC::RTC_OK;
00214 }
00215
00216
00217
00218 RTC::ReturnCode_t SamplePD_HG::onExecute(RTC::UniqueId ec_id)
00219 {
00220 if( CONTROLLER_BRIDGE_DEBUG )
00221 {
00222 std::cout << "onExecute" << std::endl;
00223 std::string localStr;
00224 std::cin >> localStr;
00225 }
00226
00227 if(m_angle_inIn.isNew()){
00228 m_angle_inIn.read();
00229 }
00230
00231 if(!angle.eof()){
00232 angle >> q_ref[0]; vel >> dq_ref[0]; acc >> ddq_ref[0];
00233 for (int i=0; i<DOF; i++){
00234 angle >> q_ref[i];
00235 vel >> dq_ref[i];
00236 acc >> ddq_ref[i];
00237 }
00238 }
00239
00240 double tor_ref[DOF];
00241 for(int i=0; i<DOF; i++){
00242 double q = m_angle_in.data[i];
00243 double dq = (q - qold[i]) / TIMESTEP;
00244 qold[i] = q;
00245
00246 tor_ref[i] = -(q - q_ref[i]) * Pgain[i] - (dq - dq_ref[i]) * Dgain[i];
00247 }
00248
00249 m_torque.data[0] = tor_ref[WAIST_P];
00250 m_torque.data[1] = tor_ref[WAIST_R];
00251 m_torque.data[2] = tor_ref[CHEST];
00252 m_torque.data[3] = tor_ref[LARM_SHOULDER_P];
00253 m_torque.data[4] = tor_ref[LARM_SHOULDER_R];
00254 m_torque.data[5] = tor_ref[LARM_SHOULDER_Y];
00255 m_torque.data[6] = tor_ref[LARM_ELBOW];
00256 m_torque.data[7] = tor_ref[LARM_WRIST_Y];
00257 m_torque.data[8] = tor_ref[LARM_WRIST_P];
00258 m_torque.data[9] = tor_ref[LARM_WRIST_R];
00259 m_torque.data[10] = tor_ref[RARM_SHOULDER_P];
00260 m_torque.data[11] = tor_ref[RARM_SHOULDER_R];
00261 m_torque.data[12] = tor_ref[RARM_SHOULDER_Y];
00262 m_torque.data[13] = tor_ref[RARM_ELBOW];
00263 m_torque.data[14] = tor_ref[RARM_WRIST_Y];
00264 m_torque.data[15] = tor_ref[RARM_WRIST_P];
00265 m_torque.data[16] = tor_ref[RARM_WRIST_R];
00266
00267 m_angle_out.data[0] = q_ref[LLEG_HIP_R];
00268 m_angle_out.data[1] = q_ref[LLEG_HIP_P];
00269 m_angle_out.data[2] = q_ref[LLEG_HIP_Y];
00270 m_angle_out.data[3] = q_ref[LLEG_KNEE];
00271 m_angle_out.data[4] = q_ref[LLEG_ANKLE_P];
00272 m_angle_out.data[5] = q_ref[LLEG_ANKLE_R];
00273 m_angle_out.data[6] = q_ref[RLEG_HIP_R];
00274 m_angle_out.data[7] = q_ref[RLEG_HIP_P];
00275 m_angle_out.data[8] = q_ref[RLEG_HIP_Y];
00276 m_angle_out.data[9] = q_ref[RLEG_KNEE];
00277 m_angle_out.data[10] = q_ref[RLEG_ANKLE_P];
00278 m_angle_out.data[11] = q_ref[RLEG_ANKLE_R];
00279 m_vel.data[0] = dq_ref[LLEG_HIP_R];
00280 m_vel.data[1] = dq_ref[LLEG_HIP_P];
00281 m_vel.data[2] = dq_ref[LLEG_HIP_Y];
00282 m_vel.data[3] = dq_ref[LLEG_KNEE];
00283 m_vel.data[4] = dq_ref[LLEG_ANKLE_P];
00284 m_vel.data[5] = dq_ref[LLEG_ANKLE_R];
00285 m_vel.data[6] = dq_ref[RLEG_HIP_R];
00286 m_vel.data[7] = dq_ref[RLEG_HIP_P];
00287 m_vel.data[8] = dq_ref[RLEG_HIP_Y];
00288 m_vel.data[9] = dq_ref[RLEG_KNEE];
00289 m_vel.data[10] = dq_ref[RLEG_ANKLE_P];
00290 m_vel.data[11] = dq_ref[RLEG_ANKLE_R];
00291 m_acc.data[0] = ddq_ref[LLEG_HIP_R];
00292 m_acc.data[1] = ddq_ref[LLEG_HIP_P];
00293 m_acc.data[2] = ddq_ref[LLEG_HIP_Y];
00294 m_acc.data[3] = ddq_ref[LLEG_KNEE];
00295 m_acc.data[4] = ddq_ref[LLEG_ANKLE_P];
00296 m_acc.data[5] = ddq_ref[LLEG_ANKLE_R];
00297 m_acc.data[6] = ddq_ref[RLEG_HIP_R];
00298 m_acc.data[7] = ddq_ref[RLEG_HIP_P];
00299 m_acc.data[8] = ddq_ref[RLEG_HIP_Y];
00300 m_acc.data[9] = ddq_ref[RLEG_KNEE];
00301 m_acc.data[10] = ddq_ref[RLEG_ANKLE_P];
00302 m_acc.data[11] = ddq_ref[RLEG_ANKLE_R];
00303
00304 m_torqueOut.write();
00305 m_angle_outOut.write();
00306 m_velOut.write();
00307 m_accOut.write();
00308
00309 return RTC::RTC_OK;
00310 }
00311
00312
00313
00314
00315
00316
00317
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 void SamplePD_HG::openFiles()
00349 {
00350 angle.open(ANGLE_FILE);
00351 if (!angle.is_open()){
00352 std::cerr << ANGLE_FILE << " not opened" << std::endl;
00353 }
00354
00355 vel.open(VEL_FILE);
00356 if (!vel.is_open()){
00357 std::cerr << VEL_FILE << " not opened" << std::endl;
00358 }
00359
00360 acc.open(ACC_FILE);
00361 if (!acc.is_open())
00362 {
00363 std::cerr << ACC_FILE << " not opend" << std::endl;
00364 }
00365 }
00366
00367 void SamplePD_HG::closeFiles()
00368 {
00369 if( angle.is_open() ){
00370 angle.close();
00371 angle.clear();
00372 }
00373 if( vel.is_open() ){
00374 vel.close();
00375 vel.clear();
00376 }
00377 if( acc.is_open() ){
00378 acc.close();
00379 acc.clear();
00380 }
00381 }
00382
00383
00384 extern "C"
00385 {
00386
00387 DLL_EXPORT void SamplePD_HGInit(RTC::Manager* manager)
00388 {
00389 coil::Properties profile(SamplePD_HG_spec);
00390 manager->registerFactory(profile,
00391 RTC::Create<SamplePD_HG>,
00392 RTC::Delete<SamplePD_HG>);
00393 }
00394
00395 };
00396