00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "claraty/me_joint.h"
00027 #include "claraty/mechanism_model.h"
00028 #include "claraty/me_body.h"
00029 #include "claraty/xml_out.h"
00030 #include "claraty/string_util.h"
00031
00032 using namespace std;
00033
00034 namespace claraty {
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 bool Joint_Constraint::evaluate_constraint_expression()
00059 {
00060 double slope_sign = 1.0;
00061 _dependent_body_joint_value_index = 0;
00062
00063
00064 std::string string_expression(s_expr_attribute);
00065
00066
00067 for (unsigned int i=0; i<string_expression.length(); ++i)
00068 {
00069 if ((string_expression[i] == ' ')||(string_expression[i] == '\t')
00070 ||(string_expression[i] == '\n')||(string_expression[i] == '\r'))
00071 {
00072 string_expression.erase(i, 1);
00073 --i;
00074 }
00075 }
00076
00077 if (string_expression[0] == '-')
00078 {
00079 slope_sign = -1.0;
00080 string_expression.erase(0,1);
00081 }
00082
00083 std::string string_expression_copy = string_expression;
00084
00085
00086 if (string_expression.length() == 0)
00087 {
00088 return false;
00089 }
00090
00091 if (string_expression.find("*", 0) == std::string::npos)
00092 {
00093
00094 return false;
00095 }
00096 string delims("*+-)( \t\n\r");
00097 string tokens("");
00098 vector<string> out_list = string_get_tokens(string_expression,
00099 delims,
00100 tokens);
00101
00102
00103 if ((out_list.size() > 4) || (out_list.size() < 2))
00104 {
00105 return false;
00106 }
00107
00108
00109 if (string_expression_copy[string_expression_copy.find(out_list[1])-1] != '*')
00110 {
00111 return false;
00112 }
00113
00114 if (out_list.size() == 3)
00115 {
00116 if (string_expression_copy[string_expression_copy.find(out_list[2])-1]
00117 == '(')
00118 {
00119 string_eat_long(out_list[2], _dependent_body_joint_value_index);
00120 }
00121 else if (string_expression_copy[
00122 string_expression_copy.find(out_list[2])-1] == '+')
00123 {
00124 string_eat_double(out_list[2], _offset);
00125 }
00126 else if (string_expression_copy[
00127 string_expression_copy.find(out_list[2])-1] == '-')
00128 {
00129 string_eat_double(out_list[2], _offset);
00130 _offset = - _offset;
00131 }
00132 else
00133 {
00134 return false;
00135 }
00136 }
00137
00138 if (out_list.size() == 4)
00139 {
00140 if (string_expression_copy[string_expression_copy.find(out_list[2])-1]
00141 == '(')
00142 {
00143 string_eat_long(out_list[2], _dependent_body_joint_value_index);
00144 }
00145 else
00146 {
00147 return false;
00148 }
00149
00150 if (string_expression_copy[string_expression_copy.find(out_list[3])-1]
00151 == '+')
00152 {
00153 string_eat_double(out_list[3], _offset);
00154 }
00155 else if (string_expression_copy[
00156 string_expression_copy.find(out_list[3])-1] == '-')
00157 {
00158 string_eat_double(out_list[3], _offset);
00159 _offset = - _offset;
00160 }
00161 else
00162 {
00163 return false;
00164 }
00165 }
00166
00167 string_eat_double(out_list[0], _slope);
00168 _slope *= slope_sign;
00169
00170 _dependent_body_name = out_list[1];
00171
00172 return true;
00173
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 ME_Joint::ME_Joint()
00183 : _limited(false),
00184 _number_of_dofs(1),
00185 _parent_frame(0),
00186 _constrained(false),
00187 _stiffness(0),
00188 _constraint(0)
00189 {
00190 _values.resize(_number_of_dofs);
00191 _joint_axis.resize(3);
00192 _joint_axis(0) = 0.0;
00193 _joint_axis(1) = 0.0;
00194 _joint_axis(2) = 1.0;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 ME_Joint::ME_Joint(ME_Joint & me_joint)
00206 : _index(me_joint._index),
00207 _limited(me_joint._limited),
00208 _offset(me_joint._offset),
00209 _home(me_joint._home),
00210 _number_of_dofs(me_joint._number_of_dofs),
00211 _parent_frame(me_joint._parent_frame),
00212 _constrained(me_joint._constrained),
00213 _name(me_joint._name),
00214 _limits(me_joint._limits)
00215
00216 {
00217 if (me_joint._parent_frame)
00218 _parent_frame = new Frame(*me_joint._parent_frame);
00219
00220 _limits = me_joint._limits;
00221 if (me_joint._stiffness)
00222 {
00223 _stiffness = new Joint_Stiffness(*me_joint._stiffness);
00224 }
00225 if (me_joint._constraint)
00226 {
00227 _constraint = new Joint_Constraint(*me_joint._constraint);
00228 }
00229
00230 set_type(me_joint._type);
00231 _joint_axis.resize(3);
00232 _joint_axis = me_joint._joint_axis;
00233 _values.resize(_number_of_dofs);
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 int ME_Joint::get_number_dofs()
00246 {
00247 return _number_of_dofs;
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 std::string ME_Joint::get_name()
00259 {
00260 return _name;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270 void ME_Joint::set_name(std::string name)
00271 {
00272 _name = name;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 std::string ME_Joint::get_type()
00284 {
00285 return _unresolve_type(_type);
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 vector<double> & ME_Joint::get_values_vector()
00298 {
00299 if (_constrained)
00300 {
00301 _values[0] = get_value(0);
00302 }
00303 return _values;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 void ME_Joint::set_joint_axes(Vector<double> joint_axis)
00316 {
00317 _joint_axis(0) = joint_axis(0);
00318 _joint_axis(1) = joint_axis(1);
00319 _joint_axis(2) = joint_axis(2);
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 void ME_Joint::get_joint_axes(Vector<double> & joint_axis)
00331 {
00332 joint_axis(0) = _joint_axis(0);
00333 joint_axis(1) = _joint_axis(1);
00334 joint_axis(2) = _joint_axis(2);
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 Vector<double>& ME_Joint::get_joint_axis()
00346 {
00347 return _joint_axis;
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357 void ME_Joint::rotate_joint_axes(const Quaternion<double> & rotation)
00358 {
00359 Quaternion<double> rotation_conjugate(rotation);
00360 rotation_conjugate.conjugate();
00361
00362 set_joint_axes(rotation_conjugate * Point<double>(_joint_axis));
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372 void ME_Joint::rotate_joint_axes(const RMatrix<double> & rotation)
00373 {
00374 Quaternion<double> q_rotation(rotation);
00375 rotate_joint_axes(q_rotation);
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385 void ME_Joint::set_type(short type)
00386 {
00387 _type = type;
00388 switch(_type)
00389 {
00390 case PIN:
00391 _number_of_dofs = 1;
00392 break;
00393 case SLIDER:
00394 _number_of_dofs = 1;
00395 break;
00396 case FULL6DOF:
00397 _number_of_dofs = 6;
00398 break;
00399 case STATIC:
00400 _number_of_dofs = 0;
00401 break;
00402 default:
00403 break;
00404 }
00405 _values.resize(_number_of_dofs);
00406 }
00407
00408
00409
00410
00411
00412
00413
00414
00415 void ME_Joint::set_type(std::string type)
00416 {
00417 _type = _resolve_type(type);
00418 switch(_type)
00419 {
00420 case PIN:
00421 _number_of_dofs = 1;
00422 break;
00423 case SLIDER:
00424 _number_of_dofs = 1;
00425 break;
00426 case FULL6DOF:
00427 _number_of_dofs = 6;
00428 break;
00429 case STATIC:
00430 _number_of_dofs = 0;
00431 break;
00432 default:
00433 break;
00434 }
00435 _values.resize(_number_of_dofs);
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 bool ME_Joint::is_actuated()
00447 {
00448 return _actuated;
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 void ME_Joint::set_actuated(bool actuated)
00460 {
00461 _actuated = actuated;
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472 double & ME_Joint::get_offset()
00473 {
00474 return _offset;
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 double ME_Joint::get_value(int dof_index)
00488 {
00489 if (_number_of_dofs == 0)
00490 return 0.0;
00491 if (!_constrained)
00492 return _values[dof_index];
00493 else
00494 {
00495 return _constraint->get_slope() *
00496 _me_body->get_tree_body(
00497 _constraint->get_dependent_body_name()).get_joint().get_value(
00498 _constraint->get_dependent_joint_index())
00499 + _constraint->get_offset();
00500 }
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 void ME_Joint::set_value(double value, int dof_index)
00512 {
00513 if (_number_of_dofs != 0)
00514 _values[dof_index] = value;
00515 }
00516
00517
00518
00519
00520
00521
00522
00523
00524 Transform ME_Joint::get_transform()
00525 {
00526 Transform result;
00527 switch(_type)
00528 {
00529 case PIN:
00530 result = result.rotate_z(_values[0]);
00531 break;
00532 case SLIDER:
00533 result = result.translate_z(_values[0]);
00534 break;
00535 case FULL6DOF:
00536 result = Transform(Point<double>(_values[0], _values[1],_values[2]),
00537 RMatrix<double>(RPY,_values[3], _values[4], _values[5]));
00538 break;
00539 case STATIC:
00540 result = Transform::identity;
00541 }
00542 return result;
00543 }
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 void ME_Joint::set_offset(double offset)
00554 {
00555 _offset = offset;
00556 }
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 double & ME_Joint::get_home()
00567 {
00568 return _home;
00569 }
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579 void ME_Joint::set_home(double home)
00580 {
00581 _home = home;
00582 }
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594 std::string ME_Joint::_unresolve_type(int joint_type)
00595 {
00596 if (joint_type == PIN)
00597 return std::string("revolute");
00598
00599 if (joint_type == SLIDER)
00600 return std::string("prismatic");
00601
00602 if (joint_type == FULL6DOF)
00603 return std::string("full6dof");
00604
00605 if (joint_type == STATIC)
00606 return std::string("fixed_mount");
00607
00608 return std::string("");
00609 }
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620 int ME_Joint::_resolve_type(std::string joint_type)
00621 {
00622 if (strcmp(joint_type.c_str(), "revolute") == 0)
00623 return PIN;
00624
00625 if (strcmp(joint_type.c_str(), "prismatic") == 0)
00626 return SLIDER;
00627
00628 if (strcmp(joint_type.c_str(), "full6dof") == 0)
00629 return FULL6DOF;
00630
00631 if (strcmp(joint_type.c_str(), "fixed_mount") == 0)
00632 return STATIC;
00633
00634 return UNDEFINED;
00635 }
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649 void ME_Joint::get_limits(double & joint_min, double & joint_max,
00650 double & joint_vmax,
00651 double & joint_tmin, double & joint_tmax)
00652 {
00653 _limits.get_parameters(joint_min, joint_max, joint_vmax,
00654 joint_tmin, joint_tmax);
00655 }
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669 void ME_Joint::set_limits(double joint_min, double joint_max,
00670 double joint_vmax,
00671 double joint_tmin, double joint_tmax)
00672 {
00673 _limits.set_parameters(joint_min, joint_max, joint_vmax,
00674 joint_tmin, joint_tmax);
00675 _limited = true;
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688 void ME_Joint::get_stiffness(double & kx, double & ky, double & kz)
00689 {
00690 if(_stiffness)
00691 _stiffness->get_parameters(kx, ky, kz);
00692 else
00693 kx = 0.0; ky = 0.0; kz = 0.0;
00694 }
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706 void ME_Joint::set_stiffness(double kx, double ky, double kz)
00707 {
00708 if (!_stiffness)
00709 _stiffness = new Joint_Stiffness();
00710 _stiffness->set_parameters(kx, ky, kz);
00711 }
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721 std::string ME_Joint::get_constraint_expression()
00722 {
00723 if (_constraint)
00724 return _constraint->get_parameters();
00725 else
00726 return ("");
00727 }
00728
00729
00730
00731
00732
00733
00734
00735
00736 void ME_Joint::set_constraint_expression(std::string expr)
00737 {
00738 if (!_constraint)
00739 _constraint = new Joint_Constraint;
00740 _constraint->set_parameters(expr);
00741 if ((expr.length() > 0) &&(_constraint->evaluate_constraint_expression()))
00742 _constrained = true;
00743 else
00744 {
00745 delete _constraint;
00746 _constraint = NULL;
00747 _constrained = false;
00748 }
00749
00750 }
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761 std::ostream& operator<< (std::ostream& os, ME_Joint & me_joint)
00762 {
00763 std::string spacing = "";
00764 std::string child_spacing = "";
00765 spacing.append(XML_Out::get_indentation_space(), ' ');
00766 child_spacing.append(XML_Out::get_indentation_space()
00767 + XML_Out::get_tab_space(), ' ');
00768 os << spacing;
00769 os << "<ME_Joint ";
00770 os << "name = \"" << me_joint.get_name() << "\" ";
00771
00772 os << "type = \"" << me_joint.get_type() << "\" ";
00773
00774 os << "actuated = \"" << (me_joint.is_actuated() ? "true" : "false")
00775 << "\" " ;
00776
00777 os << "offset = \"" << me_joint.get_offset() << "\" ";
00778
00779 os << "home = \"" << me_joint.get_home() << "\" ";
00780
00781
00782
00783
00784
00785
00786
00787 os << ">" << endl;
00788
00789
00790 double jmin, jmax, vmax, tmin, tmax;
00791 me_joint.get_limits(jmin, jmax, vmax, tmin, tmax);
00792 os << child_spacing;
00793 os << "<Joint_Limits ";
00794 os << "min = \"" << jmin << "\" ";
00795 os << "max = \"" << jmax << "\" ";
00796 os << "vmax = \"" << vmax << "\" ";
00797 os << "torque_min = \"" << tmin << "\" ";
00798 os << "torque_max = \"" << tmax << "\" ";
00799 os << "/>" << endl;
00800
00801 double kx, ky, kz;
00802 me_joint.get_stiffness(kx, ky, kz);
00803 os << child_spacing;
00804 os << "<Joint_Stiffness ";
00805 os << "kx = \"" << kx << "\" ";
00806 os << "ky = \"" << ky << "\" ";
00807 os << "kz = \"" << kz << "\" ";
00808 os << "/>" << endl;
00809
00810 os << child_spacing;
00811 os << "<Joint_Constraint ";
00812 os << "expr = \"" << me_joint.get_constraint_expression() << "\" ";
00813 os << "/>" << endl;
00814
00815 os << spacing;
00816 os << "</ME_Joint> \n";
00817
00818 return os;
00819 }
00820
00821
00822
00823 }