00001 /* 00002 * Copyright (c) 2008, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 // Author: Wim Meeussen 00031 00032 #include "pr2_bringup_gazebo_demo/joint_calibration_simulator.h" 00033 #include <angles/angles.h> 00034 00035 00036 namespace pr2_bringup_gazebo_demo { 00037 00038 00039 JointCalibrationSimulator::JointCalibrationSimulator() 00040 : calibration_initialized_(false), 00041 calibration_has_rising_(false), 00042 calibration_has_falling_(false), 00043 calibration_continuous_(false), 00044 got_info_(false) 00045 { 00046 } 00047 00048 void JointCalibrationSimulator::GetJointCalibrationInfo(pr2_mechanism_model::JointState* js) 00049 { 00050 // simulate calibration backward propagation 00051 if (js->joint_->calibration){ 00052 if (js->joint_->calibration->rising){ 00053 calibration_has_rising_ = true; 00054 calibration_rising_ = *(js->joint_->calibration->rising); 00055 } 00056 if (js->joint_->calibration->falling){ 00057 calibration_has_falling_ = true; 00058 calibration_falling_ = *(js->joint_->calibration->falling); 00059 } 00060 } 00061 00062 00063 // continuous joints 00064 if (js->joint_->type == urdf::Joint::CONTINUOUS){ 00065 calibration_continuous_ = true; 00066 if (calibration_has_rising_ && !calibration_has_falling_){ 00067 calibration_has_falling_ = true; 00068 calibration_falling_ = calibration_rising_ + M_PI; 00069 } 00070 if (!calibration_has_rising_ && calibration_has_falling_){ 00071 calibration_has_rising_ = true; 00072 calibration_rising_ = calibration_falling_ + M_PI; 00073 } 00074 calibration_rising_ = angles::normalize_angle(calibration_rising_); 00075 calibration_falling_ = angles::normalize_angle(calibration_falling_); 00076 if (calibration_rising_ < calibration_falling_) 00077 calibration_bump_ = true; 00078 else 00079 calibration_bump_ = false; 00080 } 00081 00082 // check 00083 if (js->joint_->type != urdf::Joint::CONTINUOUS && calibration_has_rising_ && calibration_has_falling_) 00084 ROS_ERROR("Non-continuous joint with both rising and falling edge not supported"); 00085 00086 this->got_info_ = true; 00087 } 00088 00089 void JointCalibrationSimulator::simulateJointCalibration(pr2_mechanism_model::JointState* js, pr2_hardware_interface::Actuator* as) 00090 { 00091 // setup calibration information for the joint 00092 if (!this->got_info_) this->GetJointCalibrationInfo(js); 00093 00094 // current joint_angle 00095 double pos = js->position_; 00096 double as_pos = as->state_.position_; 00097 00098 // compute calibration reading 00099 if (calibration_continuous_){ 00100 double pos_c = angles::normalize_angle(pos); 00101 if (calibration_bump_){ 00102 if (pos_c > calibration_rising_ && pos_c < calibration_falling_) 00103 as->state_.calibration_reading_ = true; 00104 else 00105 as->state_.calibration_reading_ = false; 00106 } 00107 else{ 00108 if (pos_c < calibration_rising_ && pos_c > calibration_falling_) 00109 as->state_.calibration_reading_ = false; // in low part 00110 else 00111 as->state_.calibration_reading_ = true; 00112 } 00113 } 00114 else{ 00115 if (calibration_has_rising_) 00116 as->state_.calibration_reading_ = pos > calibration_rising_; 00117 else if (calibration_has_falling_) 00118 as->state_.calibration_reading_ = pos < calibration_falling_; 00119 } 00120 00121 if (calibration_initialized_){ 00122 // tripped calibration flag 00123 //ROS_ERROR("debug: %s %d %d",js->joint_->name.c_str(),old_calibration_reading_ ,as->state_.calibration_reading_); 00124 if (old_calibration_reading_ != as->state_.calibration_reading_){ 00125 if (as->state_.calibration_reading_){ // low to high 00126 if (pos > old_calibration_pos_) // joint pos increasing and we are in the high region 00127 { 00128 as->state_.calibration_rising_edge_valid_ = true; 00129 as->state_.last_calibration_rising_edge_ = old_calibration_as_pos_; 00130 } 00131 else 00132 { 00133 as->state_.calibration_falling_edge_valid_ = true; 00134 as->state_.last_calibration_falling_edge_ = old_calibration_as_pos_; 00135 } 00136 } 00137 else{ // high to low 00138 if (pos > old_calibration_pos_) // joint pos increasing and we are in the low region 00139 { 00140 as->state_.calibration_falling_edge_valid_ = true; 00141 as->state_.last_calibration_falling_edge_ = old_calibration_as_pos_; 00142 } 00143 else 00144 { 00145 as->state_.calibration_rising_edge_valid_ = true; 00146 as->state_.last_calibration_rising_edge_ = old_calibration_as_pos_; 00147 } 00148 } 00149 } 00150 } 00151 00152 // store state 00153 old_calibration_reading_ = as->state_.calibration_reading_; 00154 old_calibration_pos_ = pos; 00155 old_calibration_as_pos_ = as_pos; 00156 calibration_initialized_ = true; 00157 } 00158 00159 00160 00161 } //namespace 00162