1 #ifndef SimpleFullbodyInverseKinematicsSolver_H 2 #define SimpleFullbodyInverseKinematicsSolver_H 5 #include "../ImpedanceController/JointPathEx.h" 6 #include "../ImpedanceController/RatsMatrix.h" 45 : avoid_gain(0.001), reference_gain(0.01),
46 pos_ik_error_count(0), rot_ik_error_count(0),
47 limb_length_margin(0.02)
51 std::map<std::string, IKparam>
ikp;
70 ik_error_debug_print_freq(static_cast<
int>(0.2/_dt)),
72 overwrite_ref_ja_index_vec(), ikp(),
73 move_base_gain(0.8), ratio_for_vel(1.0),
74 pos_ik_thre(0.5*1e-3),
75 rot_ik_thre((1e-2)*
M_PI/180.0),
76 print_str(_print_str), m_dt(_dt),
77 use_limb_stretch_avoidance(false), limb_stretch_avoidance_time_const(1.5)
79 qorg.resize(m_robot->numJoints());
80 qrefv.resize(m_robot->numJoints());
81 limb_stretch_avoidance_vlimit[0] = -1000 * 1e-3 * _dt;
82 limb_stretch_avoidance_vlimit[1] = 50 * 1e-3 * _dt;
88 for ( std::map<std::string, IKparam>::iterator it = ikp.begin(); it != ikp.end(); it++ ) {
89 std::cerr <<
"[" << print_str <<
"] Interlocking Joints for [" << it->first <<
"]" << std::endl;
90 it->second.manip->setInterlockingJointPairIndices(interlocking_joints, print_str);
95 current_root_p = m_robot->rootLink()->p;
96 current_root_R = m_robot->rootLink()->R;
97 for (
unsigned int i = 0;
i < m_robot->numJoints();
i++ ){
98 qorg[
i] = m_robot->joint(
i)->q;
103 for ( std::map<std::string, IKparam>::iterator it = ikp.begin(); it != ikp.end(); it++ ) {
104 if (it->second.is_ik_enable) {
105 for (
unsigned int j = 0;
j < it->second.manip->numJoints();
j++ ){
106 int i = it->second.manip->joint(
j)->jointId;
107 m_robot->joint(i)->q = qorg[i];
113 m_robot->calcForwardKinematics();
117 for (
unsigned int i = 0;
i < m_robot->numJoints();
i++ ){
118 qrefv[
i] = m_robot->joint(
i)->q;
126 m_robot->rootLink()->p = m_robot->rootLink()->p + -1 * move_base_gain * dif_cog;
130 std::vector<hrp::Vector3> tmp_p;
131 std::vector<std::string> tmp_name;
132 for ( std::map<std::string, IKparam>::iterator it = ikp.begin(); it != ikp.end(); it++ ) {
133 if (it->first.find(
"leg") != std::string::npos) {
134 tmp_p.push_back(it->second.target_p0);
135 tmp_name.push_back(it->first);
141 for (
size_t i = 0;
i < overwrite_ref_ja_index_vec.size();
i++) {
142 m_robot->joint(overwrite_ref_ja_index_vec[
i])->q = qrefv[overwrite_ref_ja_index_vec[i]];
144 m_robot->calcForwardKinematics();
145 for ( std::map<std::string, IKparam>::iterator it = ikp.begin(); it != ikp.end(); it++ ) {
146 if (it->second.is_ik_enable)
solveLimbIK (it->second, it->first, ratio_for_vel, is_transition);
150 bool solveLimbIK (
IKparam& param,
const std::string& limb_name,
const double ratio_for_vel,
const bool is_transition)
163 if (vel_p.norm() > pos_ik_thre && is_transition) {
165 std::cerr <<
"[" << print_str <<
"] Too large IK error in " << limb_name <<
" (vel_p) = [" << vel_p(0) <<
" " << vel_p(1) <<
" " << vel_p(2) <<
"][m], count = " << param.
pos_ik_error_count << std::endl;
168 has_ik_failed =
true;
172 if (vel_r.norm() > rot_ik_thre && is_transition) {
174 std::cerr <<
"[" << print_str <<
"] Too large IK error in " << limb_name <<
" (vel_r) = [" << vel_r(0) <<
" " << vel_r(1) <<
" " << vel_r(2) <<
"][rad], count = " << param.
rot_ik_error_count << std::endl;
177 has_ik_failed =
true;
184 has_ik_failed =
false;
185 for ( std::map<std::string, IKparam>::iterator it = ikp.begin(); it != ikp.end(); it++ ) {
186 it->second.pos_ik_error_count = it->second.rot_ik_error_count = 0;
190 void getIKParam (std::vector<std::string>& ee_vec, _CORBA_Unbounded_Sequence<OpenHRP::AutoBalancerService::IKLimbParameters>& ik_limb_parameters)
192 ik_limb_parameters.length(ee_vec.size());
193 for (
size_t i = 0;
i < ee_vec.size();
i++) {
195 OpenHRP::AutoBalancerService::IKLimbParameters& ilp = ik_limb_parameters[
i];
196 ilp.ik_optional_weight_vector.length(param.
manip->numJoints());
197 std::vector<double> ov;
198 ov.resize(param.
manip->numJoints());
199 param.
manip->getOptionalWeightVector(ov);
200 for (
size_t j = 0;
j < param.
manip->numJoints();
j++) {
201 ilp.ik_optional_weight_vector[
j] = ov[
j];
203 ilp.sr_gain = param.
manip->getSRGain();
206 ilp.manipulability_limit = param.
manip->getManipulabilityLimit();
210 void setIKParam (std::vector<std::string>& ee_vec,
const _CORBA_Unbounded_Sequence<OpenHRP::AutoBalancerService::IKLimbParameters>& ik_limb_parameters)
212 std::cerr <<
"[" << print_str <<
"] IK limb parameters" << std::endl;
213 bool is_ik_limb_parameter_valid_length =
true;
214 if (ik_limb_parameters.length() != ee_vec.size()) {
215 is_ik_limb_parameter_valid_length =
false;
216 std::cerr <<
"[" << print_str <<
"] ik_limb_parameters invalid length! Cannot be set. (input = " << ik_limb_parameters.length() <<
", desired = " << ee_vec.size() <<
")" << std::endl;
218 for (
size_t i = 0;
i < ee_vec.size();
i++) {
219 if (ikp[ee_vec[
i]].
manip->numJoints() != ik_limb_parameters[
i].ik_optional_weight_vector.length())
220 is_ik_limb_parameter_valid_length =
false;
222 if (is_ik_limb_parameter_valid_length) {
223 for (
size_t i = 0;
i < ee_vec.size();
i++) {
225 const OpenHRP::AutoBalancerService::IKLimbParameters& ilp = ik_limb_parameters[
i];
226 std::vector<double> ov;
227 ov.resize(param.
manip->numJoints());
228 for (
size_t j = 0;
j < param.
manip->numJoints();
j++) {
229 ov[
j] = ilp.ik_optional_weight_vector[
j];
231 param.
manip->setOptionalWeightVector(ov);
232 param.
manip->setSRGain(ilp.sr_gain);
235 param.
manip->setManipulabilityLimit(ilp.manipulability_limit);
238 std::cerr <<
"[" << print_str <<
"] ik_optional_weight_vector invalid length! Cannot be set. (input = [";
239 for (
size_t i = 0;
i < ee_vec.size();
i++) {
240 std::cerr << ik_limb_parameters[
i].ik_optional_weight_vector.length() <<
", ";
242 std::cerr <<
"], desired = [";
243 for (
size_t i = 0;
i < ee_vec.size();
i++) {
244 std::cerr << ikp[ee_vec[
i]].manip->numJoints() <<
", ";
246 std::cerr <<
"])" << std::endl;
249 if (is_ik_limb_parameter_valid_length) {
256 m_robot->calcForwardKinematics();
257 double tmp_d_root_height = 0.0, prev_d_root_height =
d_root_height;
258 if (use_limb_stretch_avoidance) {
259 for (
size_t i = 0;
i < target_p.size();
i++) {
263 double limb_length_limitation = ikp[target_name[
i]].max_limb_length - ikp[target_name[
i]].limb_length_margin;
264 double tmp = limb_length_limitation * limb_length_limitation - rel_target_p(0) * rel_target_p(0) - rel_target_p(1) * rel_target_p(1);
265 if (rel_target_p.norm() > limb_length_limitation && tmp >= 0) {
266 tmp_d_root_height =
std::min(tmp_d_root_height, rel_target_p(2) + std::sqrt(tmp));
270 d_root_height = tmp_d_root_height == 0.0 ? (- 1/limb_stretch_avoidance_time_const * d_root_height * m_dt +
d_root_height) : tmp_d_root_height;
272 d_root_height = - 1/limb_stretch_avoidance_time_const * d_root_height * m_dt +
d_root_height;
274 d_root_height =
vlimit(d_root_height, prev_d_root_height + limb_stretch_avoidance_vlimit[0], prev_d_root_height + limb_stretch_avoidance_vlimit[1]);
277 double vlimit(
const double value,
const double llimit_value,
const double ulimit_value)
279 if (value > ulimit_value) {
281 }
else if (value < llimit_value) {
289 std::cerr <<
"[" << print_str <<
"] move_base_gain = " << move_base_gain << std::endl;
290 std::cerr <<
"[" << print_str <<
"] pos_ik_thre = " << pos_ik_thre <<
"[m], rot_ik_thre = " << rot_ik_thre <<
"[rad]" << std::endl;
295 std::cerr <<
"[" << print_str <<
"] ik_optional_weight_vectors = ";
296 for (
size_t i = 0;
i < ee_vec.size();
i++) {
298 std::vector<double> ov;
299 ov.resize(param.
manip->numJoints());
300 param.
manip->getOptionalWeightVector(ov);
302 for (
size_t j = 0;
j < param.
manip->numJoints();
j++) {
303 std::cerr << ov[
j] <<
" ";
307 std::cerr << std::endl;
308 std::cerr <<
"[" << print_str <<
"] sr_gains = [";
309 for (
size_t i = 0;
i < ee_vec.size();
i++) {
310 std::cerr << ikp[ee_vec[
i]].manip->getSRGain() <<
", ";
312 std::cerr <<
"]" << std::endl;
313 std::cerr <<
"[" << print_str <<
"] avoid_gains = [";
314 for (
size_t i = 0;
i < ee_vec.size();
i++) {
315 std::cerr << ikp[ee_vec[
i]].avoid_gain <<
", ";
317 std::cerr <<
"]" << std::endl;
318 std::cerr <<
"[" << print_str <<
"] reference_gains = [";
319 for (
size_t i = 0;
i < ee_vec.size();
i++) {
320 std::cerr << ikp[ee_vec[
i]].reference_gain <<
", ";
322 std::cerr <<
"]" << std::endl;
323 std::cerr <<
"[" << print_str <<
"] manipulability_limits = [";
324 for (
size_t i = 0;
i < ee_vec.size();
i++) {
325 std::cerr << ikp[ee_vec[
i]].manip->getManipulabilityLimit() <<
", ";
327 std::cerr <<
"]" << std::endl;
330 return ikp[limb_name].target_link->p + ikp[limb_name].target_link->R * ikp[limb_name].localPos;
333 return ikp[limb_name].target_link->R * ikp[limb_name].localR;
337 #endif // SimpleFullbodyInverseKinematicsSolver_H hrp::Vector3 getEndEffectorPos(const std::string &limb_name)
void revertRobotStateToCurrent()
void getIKParam(std::vector< std::string > &ee_vec, _CORBA_Unbounded_Sequence< OpenHRP::AutoBalancerService::IKLimbParameters > &ik_limb_parameters)
hrp::Vector3 target_root_p
void difference_rotation(hrp::Vector3 &ret_dif_rot, const hrp::Matrix33 &self_rot, const hrp::Matrix33 &target_rot)
double vlimit(const double value, const double llimit_value, const double ulimit_value)
bool solveLimbIK(IKparam ¶m, const std::string &limb_name, const double ratio_for_vel, const bool is_transition)
std::map< std::string, IKparam > ikp
bool use_limb_stretch_avoidance
int ik_error_debug_print_freq
double limb_stretch_avoidance_vlimit[2]
hrp::Matrix33 current_root_R
std::vector< int > overwrite_ref_ja_index_vec
~SimpleFullbodyInverseKinematicsSolver()
void setReferenceJointAngles()
hrp::JointPathExPtr manip
void limbStretchAvoidanceControl(const std::vector< hrp::Vector3 > &target_p, const std::vector< std::string > &target_name)
hrp::Vector3 current_root_p
hrp::Matrix33 getEndEffectorRot(const std::string &limb_name)
def j(str, encoding="cp932")
void solveFullbodyIK(const hrp::Vector3 &_dif_cog, const bool is_transition)
hrp::Matrix33 target_root_R
SimpleFullbodyInverseKinematicsSolver(hrp::BodyPtr &_robot, const std::string &_print_str, const double _dt)
double limb_length_margin
void checkIKTracking(IKparam ¶m, const std::string &limb_name, const bool is_transition)
void storeCurrentParameters()
double limb_stretch_avoidance_time_const
void initializeInterlockingJoints(std::vector< std::pair< hrp::Link *, hrp::Link * > > &interlocking_joints)
void setIKParam(std::vector< std::string > &ee_vec, const _CORBA_Unbounded_Sequence< OpenHRP::AutoBalancerService::IKLimbParameters > &ik_limb_parameters)
void printIKparam(std::vector< std::string > &ee_vec)
size_t pos_ik_error_count
size_t rot_ik_error_count