MILDRobotModelWithApproximatedIK.cpp
Go to the documentation of this file.
1 
20 #include <glpk.h>
21 #include <limits>
22 #include <ros/ros.h>
23 #include <ros/node_handle.h>
24 
29 
30 #include "nav_msgs/GetPlan.h"
31 #include "geometry_msgs/PoseStamped.h"
32 #include "geometry_msgs/Point.h"
33 
34 #include "urdf/model.h"
35 #include "urdf_model/joint.h"
36 #include "tf/tf.h"
37 #include "tf/transform_datatypes.h"
38 //#include <robot_state_publisher/robot_state_publisher.h>
39 #include <kdl_parser/kdl_parser.hpp>
40 #include <kdl/chain.hpp>
41 #include <kdl/chainfksolver.hpp>
42 #include <kdl/chainfksolverpos_recursive.hpp>
43 
44 namespace robot_model_services {
47  mDebugHelperPtr->write(std::stringstream() << "STARTING MILD ROBOT MODEL WITH APPROXIMATED IK", DebugHelper::ROBOT_MODEL);
48 
49  navigationCostClient = n.serviceClient<nav_msgs::GetPlan>("/move_base/make_plan");
50  double speedFactorPTU_,speedFactorBaseMove_,speedFactorBaseRot_;
51  n.getParam("speedFactorPTU", speedFactorPTU_);
52  n.getParam("speedFactorBaseMove", speedFactorBaseMove_);
53  n.getParam("speedFactorBaseRot", speedFactorBaseRot_);
54  mDebugHelperPtr->write(std::stringstream() << "speedFactorPTU: " << speedFactorPTU_, DebugHelper::PARAMETERS);
55  mDebugHelperPtr->write(std::stringstream() << "speedFactorBaseMove: " << speedFactorBaseMove_, DebugHelper::PARAMETERS);
56  mDebugHelperPtr->write(std::stringstream() << "speedFactorBaseRot: " << speedFactorBaseRot_, DebugHelper::PARAMETERS);
57  speedFactorPTU = speedFactorPTU_;
58  speedFactorBaseMove = speedFactorBaseMove_;
59  speedFactorBaseRot = speedFactorBaseRot_;
60  }
61 
63 
64  //Comment?
65  //Solves the inverse kinematical problem for an given robot state and a pose for the camera
66 
68  {
69  mDebugHelperPtr->writeNoticeably("STARTING CALCULATE-ROBOT-STATE METHOD", DebugHelper::ROBOT_MODEL);
70  MILDRobotStatePtr sourceMILDRobotState = boost::static_pointer_cast<MILDRobotState>(sourceRobotState);
71  MILDRobotStatePtr targetMILDRobotState(new MILDRobotState());
72 
73  SimpleVector3 visualAxis = MathHelper::getVisualAxis(orientation);
74  SimpleSphereCoordinates sphereCoords = MathHelper::convertC2S(visualAxis);
75  while (sphereCoords[2] < 0) { sphereCoords[2] += 2 * M_PI; }
76  while (sphereCoords[2] > 2 * M_PI) { sphereCoords[2] -= 2 * M_PI; }
77 
78  double phiMin = mPanLimits.get<0>();
79  double phiMax = mPanLimits.get<1>();
80  double currentPhi = sourceMILDRobotState->pan;
81  double currentRho = sourceMILDRobotState->rotation;
82 
83  mDebugHelperPtr->write(std::stringstream() << "Source robot state: (Pan: " << sourceMILDRobotState->pan
84  << ", Tilt: " << sourceMILDRobotState->tilt
85  << ", Rotation " << sourceMILDRobotState->rotation
86  << ", X:" << sourceMILDRobotState->x
87  << ", Y:" << sourceMILDRobotState->y << ")",
89  mDebugHelperPtr->write(std::stringstream() << "Target View Position: " << position[0] << ", " << position[1] << ", " << position[2],
91  mDebugHelperPtr->write(std::stringstream() << "Target View Orientation: " << orientation.w() << ", " << orientation.x() << ", " << orientation.y()<< ", " << orientation.z(),
93 
94  double alpha = sphereCoords[2] - currentPhi - currentRho;
95  alpha = alpha > M_PI ? alpha - 2 * M_PI : alpha;
96  alpha = alpha < -M_PI ? alpha + 2 * M_PI : alpha;
97 
98  // Truncate pan angle to valid range
99  if (currentPhi < phiMin) {
100  if (currentPhi < phiMin - 10.0) {
101  ROS_WARN_STREAM("Initial Pan-Angle (" << currentPhi * (180/M_PI) << ") was too small.");
102  } else {
103  mDebugHelperPtr->write(std::stringstream() << "Initial Pan-Angle (" << currentPhi * (180/M_PI) << ") was too small.", DebugHelper::ROBOT_MODEL);
104  }
105  currentPhi = phiMin;
106  }
107  if (currentPhi > phiMax) {
108  if (currentPhi > phiMax + 10.0) {
109  ROS_WARN_STREAM("Initial Pan-Angle (" << currentPhi * (180/M_PI) << ") was too large.");
110  } else {
111  mDebugHelperPtr->write(std::stringstream() << "Initial Pan-Angle (" << currentPhi * (180/M_PI) << ") was too large.", DebugHelper::ROBOT_MODEL);
112  }
113  currentPhi = phiMax;
114  }
115 
116  glp_prob *lp;
117 
118  int rows = 1;
119  int cols = 4;
120  int glIdx[1+cols*rows];
121  int glColIdx[1+cols*rows];
122  double glCoef[1+cols*rows];
123 
124  lp = glp_create_prob();
125  glp_set_prob_name(lp, "angle_prob");
126  glp_set_obj_dir(lp, GLP_MIN);
127  // omega_pan * x_pan + omega_rot * x_rot
128  glp_add_cols(lp, cols);
129 
130  int colIdx = 1;
131  glp_set_col_name(lp, colIdx, "x_pan+");
132  double maxPanPlus = fabs(phiMax - currentPhi);
133  glp_set_col_bnds(lp, colIdx, (maxPanPlus == 0.0 ? GLP_FX : GLP_DB), 0.0, maxPanPlus);
134  glp_set_obj_coef(lp, colIdx, mOmegaPan);
135 
136  colIdx++;
137  glp_set_col_name(lp, colIdx, "x_pan-");
138  double maxPanMinus = fabs(currentPhi - phiMin);
139  glp_set_col_bnds(lp, colIdx, (maxPanMinus == 0.0 ? GLP_FX : GLP_DB), 0.0, maxPanMinus);
140  glp_set_obj_coef(lp, colIdx, mOmegaPan);
141 
142  colIdx++;
143  glp_set_col_name(lp, colIdx, "x_rot+");
144  glp_set_col_bnds(lp, colIdx, GLP_DB, 0, M_PI);
145  glp_set_obj_coef(lp, colIdx, mOmegaRot);
146 
147  colIdx++;
148  glp_set_col_name(lp, colIdx, "x_rot-");
149  glp_set_col_bnds(lp, colIdx, GLP_DB, 0.0, M_PI);
150  glp_set_obj_coef(lp, colIdx, mOmegaRot);
151 
152  // constraint alpha
153  int rowIdx = 1;
154  glp_add_rows(lp, rowIdx);
155  glp_set_row_name(lp, rowIdx, "alpha");
156  glp_set_row_bnds(lp, rowIdx, GLP_FX, alpha, alpha);
157 
158  // alpha = x_pan + x_rot
159  glIdx[1] = 1; glColIdx[1] = 1; glCoef[1] = 1.0;
160  glIdx[2] = 1; glColIdx[2] = 2; glCoef[2] = -1.0;
161  glIdx[3] = 1; glColIdx[3] = 3; glCoef[3] = 1.0;
162  glIdx[4] = 1; glColIdx[4] = 4; glCoef[4] = -1.0;
163 
164  // constraint
165  glp_load_matrix(lp, 4, glIdx, glColIdx, glCoef);
166 
167  glp_smcp *param = new glp_smcp();
168  glp_init_smcp(param);
169  param->msg_lev = GLP_MSG_OFF;
170  glp_simplex(lp, param);
171 
172  int statusCode = glp_get_status(lp);
173  if (statusCode != GLP_OPT) {
174  ROS_ERROR("No optimal solution found - inverse kinematics");
175  }
176 
177  double x_pan_plus = glp_get_col_prim(lp, 1);
178  double x_pan_minus = glp_get_col_prim(lp, 2);
179  double x_rot_plus = glp_get_col_prim(lp, 3);
180  double x_rot_minus = glp_get_col_prim(lp, 4);
181 
182  // free memory
183  glp_delete_prob(lp);
184 
185  // set pan
186  targetMILDRobotState->pan = sourceMILDRobotState->pan + x_pan_plus - x_pan_minus;
187 
188  // Truncate pan angle to valid range
189  if (targetMILDRobotState->pan < phiMin) {
190  if (targetMILDRobotState->pan < phiMin - 10.0) {
191  ROS_WARN_STREAM("Calculated Pan-Angle (" << targetMILDRobotState->pan * (180/M_PI) << ") was too small.");
192  } else {
193  mDebugHelperPtr->write(std::stringstream() << "Calculated Pan-Angle (" << targetMILDRobotState->pan * (180/M_PI) << ") was too small.", DebugHelper::ROBOT_MODEL);
194  }
195  targetMILDRobotState->pan = phiMin;
196  }
197  if (targetMILDRobotState->pan > phiMax) {
198  if (targetMILDRobotState->pan > phiMax + 10.0) {
199  ROS_WARN_STREAM("Calculated Pan-Angle (" << targetMILDRobotState->pan * (180/M_PI) << ") was too large.");
200  } else {
201  mDebugHelperPtr->write(std::stringstream() << "Calculated Pan-Angle (" << targetMILDRobotState->pan * (180/M_PI) << ") was too large.", DebugHelper::ROBOT_MODEL);
202  }
203  targetMILDRobotState->pan = phiMax;
204  }
205 
206  // set rotation
207  targetMILDRobotState->rotation = sourceMILDRobotState->rotation + x_rot_plus - x_rot_minus;
208  while (targetMILDRobotState->rotation < 0) { targetMILDRobotState->rotation += 2 * M_PI; };
209  while (targetMILDRobotState->rotation > 2 * M_PI) { targetMILDRobotState->rotation -= 2 * M_PI; };
210 
211  // set tilt
212  targetMILDRobotState->tilt = sphereCoords[1];
213 
214  // set x, y
215  targetMILDRobotState->x = position[0];
216  targetMILDRobotState->y = position[1];
217  mDebugHelperPtr->write(std::stringstream() << "Target state: (Pan: " << targetMILDRobotState->pan
218  << ", Tilt: " << targetMILDRobotState->tilt
219  << ", Rotation " << targetMILDRobotState->rotation
220  << ", X:" << targetMILDRobotState->x
221  << ", Y:" << targetMILDRobotState->y << ")",
223  mDebugHelperPtr->writeNoticeably("ENDING CALCULATE-ROBOT-STATE METHOD", DebugHelper::ROBOT_MODEL);
224  return targetMILDRobotState;
225  }
226 }
227 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
bool param(const std::string &param_name, T &param_val, const T &default_val)
MILDRobotModel implements a model of pan tilt unit robot.
static boost::shared_ptr< DebugHelper > getInstance()
Definition: DebugHelper.cpp:29
Eigen::Matrix< Precision, 3, 1 > SimpleVector3
Definition: typedef.hpp:50
static SimpleVector3 getVisualAxis(const SimpleQuaternion &orientation)
Definition: MathHelper.cpp:53
SimpleVector3 SimpleSphereCoordinates
Definition: typedef.hpp:51
MILDRobotModelWithApproximatedIK()
constructor of the MILDRobotModelWithApproximatedIK
RobotStatePtr calculateRobotState(const RobotStatePtr &sourceRobotState, const SimpleVector3 &position, const SimpleQuaternion &orientation)
calculates the target robot state
boost::tuple< float, float > mPanLimits
this namespace contains all generally usable classes.
Definition: DebugHelper.hpp:27
#define ROS_WARN_STREAM(args)
static SimpleSphereCoordinates convertC2S(const SimpleVector3 &cartesian)
converts cartesian coordinates to sphere coordinates
Definition: MathHelper.cpp:29
virtual ~MILDRobotModelWithApproximatedIK()
destructor of the MILDRobotModelWithApproximatedIK
bool getParam(const std::string &key, std::string &s) const
#define ROS_ERROR(...)
Eigen::Quaternion< Precision > SimpleQuaternion
Definition: typedef.hpp:64


asr_robot_model_services
Author(s): Aumann Florian, Borella Jocelyn, Heller Florian, Meißner Pascal, Schleicher Ralf, Stöckle Patrick, Stroh Daniel, Trautmann Jeremias, Walter Milena, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:49:59