00001
00017 #include "MotorControl.h"
00018
00019
00020 #include <stdio.h>
00021 #include <iostream>
00022
00023
00024
00025
00026 static const char* motorcontrol_spec[] =
00027 {
00028 "implementation_id", "MotorControl",
00029 "type_name", "MotorControl",
00030 "description", "MotorControl",
00031 "version", "4.0.0",
00032 "vendor", "AIST INVENT",
00033 "category", "Controller",
00034 "activity_type", "PERIODIC",
00035 "kind", "DataFlowComponent",
00036 "max_instance", "10",
00037 "language", "C++",
00038 "lang_type", "compile",
00039 "exec_cxt.periodic.rate", "1000.0",
00040
00041 "conf.default.PGainL", "5.0",
00042 "conf.default.DGainL", "2.0",
00043 "conf.default.PGainR", "5.0",
00044 "conf.default.DGainR", "2.0",
00045 "conf.default.SimulatedOffsetX", "0.0",
00046 "conf.default.SimulatedOffsetY", "0.0",
00047 "conf.default.SimulatedOffsetAngle", "0.0",
00048 "conf.default.leftWheelID", "0",
00049 "conf.default.rightWheelID", "1",
00050 "conf.default.radiusOfLeftWheel", "0.1",
00051 "conf.default.radiusOfRightWheel", "0.1",
00052 "conf.default.lengthOfAxle", "0.441",
00053 "conf.default.radiusOfBodyArea", "0.45",
00054 ""
00055 };
00056
00057
00062 MotorControl::MotorControl(RTC::Manager* manager)
00063
00064 : RTC::DataFlowComponentBase(manager),
00065 m_TargetVelocityIn("TargetVelocity", m_TargetVelocity),
00066 m_InCurrentWheelAngleIn("InCurrentWheelAngle", m_InCurrentWheelAngle),
00067 m_InSimulatedPositionIn("InSimulatedPosition", m_InSimulatedPosition),
00068 m_OutSimulatedPositionToInventGUIOut("OutSimulatedPositionToInventGUI", m_OutSimulatedPositionToInventGUI),
00069 m_TorqueOut("Torque", m_Torque),
00070 m_OutCurrentWheelAngleOut("OutCurrentWheelAngle", m_OutCurrentWheelAngle),
00071 m_OutSimulatedPositionToLocalizationOut("OutSimulatedPositionToLocalization", m_OutSimulatedPositionToLocalization),
00072 m_InventGUIProvPort("InventGUIProv"),
00073 m_BumpProvPort("BumpProv")
00074
00075
00076 {
00077 }
00078
00082 MotorControl::~MotorControl()
00083 {
00084 RTC_INFO(("on Destructer"));
00085
00086 }
00087
00088
00092 RTC::ReturnCode_t MotorControl::onInitialize()
00093 {
00094
00095
00096
00097 registerInPort("TargetVelocity", m_TargetVelocityIn);
00098 registerInPort("InCurrentWheelAngle", m_InCurrentWheelAngleIn);
00099 registerInPort("InSimulatedPosition", m_InSimulatedPositionIn);
00100
00101
00102 registerOutPort("OutSimulatedPositionToInventGUI", m_OutSimulatedPositionToInventGUIOut);
00103 registerOutPort("Torque", m_TorqueOut);
00104 registerOutPort("OutCurrentWheelAngle", m_OutCurrentWheelAngleOut);
00105 registerOutPort("OutSimulatedPositionToLocalization", m_OutSimulatedPositionToLocalizationOut);
00106
00107
00108 m_InventGUIProvPort.registerProvider("InventGUIMotor", "Motor", m_InventGUIMotor);
00109 m_BumpProvPort.registerProvider("BumpMotor", "Motor", m_BumpMotor);
00110
00111
00112
00113
00114 registerPort(m_InventGUIProvPort);
00115 registerPort(m_BumpProvPort);
00116
00117
00118
00119
00120
00121 bindParameter("PGainL", m_PGainL, "5.0");
00122 bindParameter("DGainL", m_DGainL, "2.0");
00123 bindParameter("PGainR", m_PGainR, "5.0");
00124 bindParameter("DGainR", m_DGainR, "2.0");
00125 bindParameter("SimulatedOffsetX", m_SimulatedOffsetX, "0.0");
00126 bindParameter("SimulatedOffsetY", m_SimulatedOffsetY, "0.0");
00127 bindParameter("SimulatedOffsetAngle", m_SimulatedOffsetAngle, "0.0");
00128 bindParameter("leftWheelID", m_leftWheelID, "0");
00129 bindParameter("rightWheelID", m_rightWheelID, "1");
00130 bindParameter("radiusOfLeftWheel", m_radiusOfLeftWheel, "0.1");
00131 bindParameter("radiusOfRightWheel", m_radiusOfRightWheel, "0.1");
00132 bindParameter("lengthOfAxle", m_lengthOfAxle, "0.441");
00133 bindParameter("radiusOfBodyArea", m_radiusOfBodyArea, "0.45");
00134
00135
00136
00137
00138 RTC_INFO(("on Constructer"));
00139
00140
00141
00142 m_Torque.data.length(Dof);
00143 m_OutCurrentWheelAngle.data.length(Dof);
00144
00145
00146 m_OutSimulatedPositionToInventGUI.data.length(4);
00147 m_OutSimulatedPositionToLocalization.data.length(4);
00148
00149 #if CALLBACK
00150
00151
00152 #endif
00153
00154
00155
00156 return RTC::RTC_OK;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00187 RTC::ReturnCode_t MotorControl::onActivated(RTC::UniqueId ec_id)
00188 {
00189
00190 RTC_INFO(("on Activated"));
00191
00192
00193
00194 m_InCurrentWheelAngleIn.update();
00195 m_TargetVelocityIn.update();
00196 m_InSimulatedPositionIn.update();
00197
00198
00199 Body.leftID = m_leftWheelID;
00200 Body.rightID = m_rightWheelID;
00201 Body.wheelRadiusLeft = m_radiusOfLeftWheel;
00202 Body.wheelRadiusRight = m_radiusOfRightWheel;
00203 Body.bodyRadius = 0.5 * m_lengthOfAxle;
00204 Body.length = m_radiusOfBodyArea;
00205
00206 RTC_DEBUG(("Body.leftID: %d Body.rightID: %d", Body.leftID, Body.rightID));
00207 RTC_DEBUG(("Body.wheelRadius[L]: %f Body.wheelRadius[R]: %f Body.bodyRadius: %f",Body.wheelRadiusLeft, Body.wheelRadiusRight, Body.bodyRadius));
00208 #if DEBUG
00209
00210
00211
00212 ofs.open("SimulatePosition.dat");
00213 #endif
00214
00215
00216
00217 for(int i=0; i < Dof; i++) qOld[i] = 0.0;
00218
00219
00220 compSwitch = -1;
00221 InitializeFlag = false;
00222
00223
00224 sec = nsec = 0;
00225 TimeStampOld = TimeStamp = 0.0;
00226
00227
00228
00229 return RTC::RTC_OK;
00230 }
00231
00236 RTC::ReturnCode_t MotorControl::onDeactivated(RTC::UniqueId ec_id)
00237 {
00238
00239
00240 #if DEBUG
00241 ofs.close();
00242 #endif
00243
00244 return RTC::RTC_OK;
00245 }
00246
00256 RTC::ReturnCode_t MotorControl::onExecute(RTC::UniqueId ec_id)
00257 {
00258
00259
00260
00261
00262
00263
00264
00265
00266 if(m_InventGUIMotor.m_StartFlag){
00267 m_InventGUIMotor.m_StartFlag = false;
00268 compSwitch = 1;
00269 RTC_INFO(("====== [start() was called from InventGUIComp] ======================"));
00270
00271
00272 }else if(m_InventGUIMotor.m_FinishFlag){
00273 m_InventGUIMotor.m_FinishFlag = false;
00274 compSwitch = 2;
00275 RTC_INFO(("====== [finish() was called from InventGUIComp] ======================"));
00276
00277
00278 }else if(m_InventGUIMotor.m_ClearFlag){
00279 m_InventGUIMotor.m_ClearFlag = false;
00280 compSwitch = 3;
00281
00282 RTC_INFO(("====== [clear() was called from InventGUIComp] ======================"));
00283
00284
00285 }else if(m_BumpMotor.m_StopFlag){
00286 m_BumpMotor.m_StopFlag = false;
00287 compSwitch = 4;
00288
00289 RTC_INFO(("====== [Stop() was called from BumpDetectionComp] ======================"));
00290 }
00291
00292
00293
00294
00295
00296
00297 if(m_InventGUIMotor.m_GainFlag){
00298 m_PGainL = m_InventGUIMotor.m_PGainL;
00299 m_PGainR = m_InventGUIMotor.m_PGainR;
00300 m_DGainL = m_InventGUIMotor.m_DGainL;
00301 m_DGainR = m_InventGUIMotor.m_DGainR;
00302 m_InventGUIMotor.m_GainFlag = false;
00303 RTC_INFO(("== [GAIN] change GAIN value"));
00304
00305 }
00306
00307
00308
00309
00310
00311
00312 if (m_InCurrentWheelAngleIn.isNew()) {
00313
00314
00315 m_InCurrentWheelAngleIn.read();
00316
00317 double dummy_sec = m_InCurrentWheelAngle.tm.sec + 1.0e-9*m_InCurrentWheelAngle.tm.nsec;
00318 RTC_DEBUG(("--[TIME]: %f nsec = %ld", dummy_sec, m_InCurrentWheelAngle.tm.nsec));
00319 #if DEBUG
00320
00321
00322 #endif
00323
00324
00325 double q[Dof];
00326 q[Body.leftID] = m_InCurrentWheelAngle.data[Body.leftID];
00327 q[Body.rightID] = m_InCurrentWheelAngle.data[Body.rightID];
00328
00329
00330 sec = m_InCurrentWheelAngle.tm.sec;
00331 nsec = m_InCurrentWheelAngle.tm.nsec;
00332 TimeStamp = sec + 1.0e-9*nsec;
00333
00334
00335 if (!InitializeFlag) {
00336 InitializeFlag = true;
00337 } else {
00338
00339 timeStep = TimeStamp - TimeStampOld;
00340
00341
00342 double dq[Dof];
00343 dq[Body.leftID] = (q[Body.leftID] - qOld[Body.leftID]) / timeStep;
00344 dq[Body.rightID] = (q[Body.rightID] - qOld[Body.rightID]) / timeStep;
00345
00346
00347 double q_ref[Dof], dq_ref[Dof];
00348
00349
00350 if (m_TargetVelocityIn.isNew()) {
00351
00352
00353 m_TargetVelocityIn.read();
00354
00355 double TransVel,RotVel,Rho;
00356 TransVel = m_TargetVelocity.vx;
00357 RotVel = m_TargetVelocity.w;
00358
00359 if (RotVel == 0.0) {
00360
00361 dq_ref[Body.leftID] = TransVel / Body.wheelRadiusLeft;
00362 dq_ref[Body.rightID] = TransVel / Body.wheelRadiusRight;
00363 } else {
00364 Rho = TransVel / RotVel;
00365
00366 dq_ref[Body.leftID] = (Rho - Body.bodyRadius) * RotVel / Body.wheelRadiusLeft;
00367 dq_ref[Body.rightID] = (Rho + Body.bodyRadius) * RotVel / Body.wheelRadiusRight;
00368 }
00369
00370
00371 q_ref[Body.leftID] = q[Body.leftID] + 0.5 * (dq_ref[Body.leftID] + dq[Body.leftID]) * timeStep;
00372 q_ref[Body.rightID] = q[Body.rightID] + 0.5 * (dq_ref[Body.rightID] + dq[Body.rightID]) * timeStep;
00373
00374
00375 m_Torque.data[Body.leftID] = (q_ref[Body.leftID] - q[Body.leftID]) * m_PGainL + (dq_ref[Body.leftID] - dq[Body.leftID]) * m_DGainL;
00376 m_Torque.data[Body.rightID] = (q_ref[Body.rightID] - q[Body.rightID]) * m_PGainR + (dq_ref[Body.rightID] - dq[Body.rightID]) * m_DGainR;
00377
00378 RTC_DEBUG((" Pgain(L): %f Pgain(R): %f Dgain(L): %f Dgain(R): %f", m_PGainL, m_PGainR, m_DGainL, m_DGainR));;
00379 RTC_DEBUG((" TransVel: %f RotVel: %f", TransVel, RotVel));
00380 RTC_DEBUG((" q_ref(L): %f q(L): %f [q_ref-q](L): %f dq_ref(L): %f dq(L): %f [dq_ref-dq](L): %f", q_ref[Body.leftID], q[Body.leftID], q_ref[Body.leftID] - q[Body.leftID], dq_ref[Body.leftID], dq[Body.leftID], dq_ref[Body.leftID] - dq[Body.leftID]));
00381 RTC_DEBUG((" q_ref(R): %f q(R): %f [q_ref-q](R): %f dq_ref(R): %f dq(R): %f [dq_ref-dq](R): %f", q_ref[Body.rightID], q[Body.rightID], q_ref[Body.rightID] - q[Body.rightID], dq_ref[Body.rightID], dq[Body.rightID], dq_ref[Body.rightID] - dq[Body.rightID]));;
00382 RTC_DEBUG((" Ptorque(L): %f Dtorque(L): %f", (q_ref[Body.leftID] - q[Body.leftID]) * m_PGainL, (dq_ref[Body.leftID] - dq[Body.leftID]) * m_DGainL));
00383 RTC_DEBUG((" Ptorque(R): %f Dtorque(R): %f", (q_ref[Body.rightID] - q[Body.rightID]) * m_PGainR, (dq_ref[Body.rightID] - dq[Body.rightID]) * m_DGainR));
00384 #if DEBUG
00385
00386
00387
00388
00389
00390
00391 #endif
00392
00393 } else {
00394 #if DEBUG
00395
00396 #endif
00397 m_Torque.data[Body.leftID] = 0.0;
00398 m_Torque.data[Body.rightID] = 0.0;
00399 }
00400
00401 RTC_DEBUG(("[MotorControl Torque] Time = %f[s] Torque(L,R) = (%f, %f)", TimeStamp, m_Torque.data[Body.leftID], m_Torque.data[Body.rightID]));
00402 #if LOG
00403
00404 #endif
00405
00406
00407 m_Torque.tm.sec = sec;
00408 m_Torque.tm.nsec = nsec;
00409
00410 m_TorqueOut.write();
00411
00412 }
00413
00414
00415 m_OutCurrentWheelAngle.data[Body.leftID] = m_InCurrentWheelAngle.data[Body.leftID];
00416 m_OutCurrentWheelAngle.data[Body.rightID] = m_InCurrentWheelAngle.data[Body.rightID];
00417
00418 m_OutCurrentWheelAngle.tm.sec = sec;
00419 m_OutCurrentWheelAngle.tm.nsec = nsec;
00420
00421 m_OutCurrentWheelAngleOut.write();
00422
00423
00424 RTC_DEBUG((" wheel[L] = %f , wheel[R] = %f TIME1= %ld TIME2= %ld", m_OutCurrentWheelAngle.data[Body.leftID], m_OutCurrentWheelAngle.data[Body.rightID], m_OutCurrentWheelAngle.tm.sec, m_OutCurrentWheelAngle.tm.nsec));
00425 #if DEBUG
00426
00427 #endif
00428
00429
00430 qOld[Body.leftID] = q[Body.leftID];
00431 qOld[Body.rightID] = q[Body.rightID];
00432 TimeStampOld = TimeStamp;
00433
00434
00435 if (m_InSimulatedPositionIn.isNew()) {
00436 m_InSimulatedPositionIn.read();
00437
00438
00439 double simu_x,simu_y,simu_theta,simu_fai;
00440 double length = sqrt(m_SimulatedOffsetX * m_SimulatedOffsetX + m_SimulatedOffsetY * m_SimulatedOffsetY);
00441
00442 simu_theta = -1.0*atan2(m_InSimulatedPosition.data[4], m_InSimulatedPosition.data[3]);
00443 simu_fai = atan2(m_SimulatedOffsetY, m_SimulatedOffsetX);
00444
00445
00446 simu_x = m_InSimulatedPosition.data[0] + length*cos(simu_theta + simu_fai);
00447 simu_y = m_InSimulatedPosition.data[1] + length*sin(simu_theta + simu_fai);
00448
00449
00450 m_OutSimulatedPositionToInventGUI.data[0] = simu_x; m_OutSimulatedPositionToLocalization.data[0] = simu_x;
00451
00452
00453 m_OutSimulatedPositionToInventGUI.data[1] = simu_y; m_OutSimulatedPositionToLocalization.data[1] = simu_y;
00454
00455
00456
00457
00458
00459
00460
00461 m_OutSimulatedPositionToInventGUI.data[2] = m_InSimulatedPosition.data[2]; m_OutSimulatedPositionToLocalization.data[2] = m_InSimulatedPosition.data[2];
00462
00463
00464 simu_theta += m_SimulatedOffsetAngle;
00465
00466
00467 while (simu_theta <= -1.0*M_PI || simu_theta >= 1.0*M_PI) {
00468 if( simu_theta <= -1.0*M_PI) simu_theta += 2.0*M_PI;
00469 else if ( simu_theta >= 1.0*M_PI) simu_theta -= 2.0*M_PI;
00470 }
00471
00472 m_OutSimulatedPositionToInventGUI.data[3] = simu_theta; m_OutSimulatedPositionToLocalization.data[3] = simu_theta;
00473
00474 RTC_DEBUG(("[BEFORE SIMULATED] [X] = %f , [Y] = %f TH[rad]= %f TH[deg]= %f TIME1= %ld TIME2= %ld", m_InSimulatedPosition.data[0], m_InSimulatedPosition.data[1], -1.0*atan2(m_InSimulatedPosition.data[4], m_InSimulatedPosition.data[3]), -180*atan2(m_InSimulatedPosition.data[4], m_InSimulatedPosition.data[3])/M_PI, m_InSimulatedPosition.tm.sec ,m_InSimulatedPosition.tm.nsec));
00475 RTC_DEBUG(("[AFTER SIMULATED] [X] = %f , [Y] = %f TH[rad]= %f TH[deg]= %f"));
00476
00477 #if DEBUG
00478
00479
00480 for(int i=0;i<4;i++) ofs << m_OutSimulatedPositionToInventGUI.data[i] << " " ;
00481 ofs << std::endl;
00482
00483
00484 #endif
00485
00486
00487 m_OutSimulatedPositionToInventGUI.tm.sec = m_InSimulatedPosition.tm.sec;
00488 m_OutSimulatedPositionToInventGUI.tm.nsec = m_InSimulatedPosition.tm.nsec;
00489 m_OutSimulatedPositionToLocalization.tm.sec = m_InSimulatedPosition.tm.sec;
00490 m_OutSimulatedPositionToLocalization.tm.nsec = m_InSimulatedPosition.tm.nsec;
00491
00492 m_OutSimulatedPositionToInventGUIOut.write();
00493 m_OutSimulatedPositionToLocalizationOut.write();
00494 }
00495
00496 } else {
00497
00498 #if DEBUG
00499
00500 #endif
00501
00502 }
00503
00504
00505
00506 return RTC::RTC_OK;
00507 }
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 extern "C"
00547 {
00548
00549 void MotorControlInit(RTC::Manager* manager)
00550 {
00551 coil::Properties profile(motorcontrol_spec);
00552 manager->registerFactory(profile,
00553 RTC::Create<MotorControl>,
00554 RTC::Delete<MotorControl>);
00555 }
00556
00557 };
00558
00559