00001 00029 #include "sr_utilities/calibration.hpp" 00030 #include "sr_utilities/sr_math_utils.hpp" 00031 00032 #include <boost/assert.hpp> 00033 #include <algorithm> 00034 00035 #include <boost/foreach.hpp> 00036 #include <iostream> 00037 00038 namespace shadow_robot 00039 { 00040 JointCalibration::JointCalibration(std::vector<joint_calibration::Point> calibration_table) 00041 { 00042 this->calibration_table_ = calibration_table; 00043 //calibration size - 1 because we use this number to access the last value in the vector 00044 calibration_table_size_ = calibration_table.size() - 1; 00045 00046 //fails if we have only one calibration point as it's not possible 00047 //to interpolate from one point 00048 BOOST_ASSERT(calibration_table_size_ > 0); 00049 00050 /* 00051 * make sure that the given calibration table is ordered by 00052 * growing values of the raw_value 00053 */ 00054 std::sort(this->calibration_table_.begin(), this->calibration_table_.end(), joint_calibration::sort_growing_raw_operator); 00055 } 00056 00064 double JointCalibration::compute(double raw_reading) 00065 { 00070 joint_calibration::Point low_point, high_point; 00071 00072 //That takes care of computing a reading that's before 00073 // the calibration table as well as a reading that's in the 00074 // first calibration table case. 00075 low_point = calibration_table_[0]; 00076 high_point = calibration_table_[1]; 00077 00078 bool found = false; 00079 00080 //if we have more than 2 points in our calibration table 00081 // or if the raw value isn't before the calibration table 00082 if( (raw_reading > calibration_table_[0].raw_value) ) 00083 { 00084 if( (calibration_table_size_ > 1) ) 00085 { 00086 for(unsigned int index_cal=1; index_cal < calibration_table_size_; ++index_cal) 00087 { 00088 if( (raw_reading >= calibration_table_[index_cal - 1].raw_value) && 00089 (raw_reading < calibration_table_[index_cal].raw_value) ) 00090 { 00091 low_point = calibration_table_[index_cal - 1]; 00092 high_point = calibration_table_[index_cal]; 00093 00094 found = true; 00095 break; 00096 } 00097 } //end for 00098 00099 //the point is outside of the table 00100 if( !found ) 00101 { 00102 low_point = calibration_table_[calibration_table_size_ - 1]; 00103 high_point = calibration_table_[calibration_table_size_]; 00104 } 00105 } // end if 2 values only in the table 00106 } //end if raw_reading before table 00107 00108 return sr_math_utils::linear_interpolate_(raw_reading, low_point.raw_value, 00109 low_point.calibrated_value, 00110 high_point.raw_value, 00111 high_point.calibrated_value); 00112 } 00113 } 00114 00115 /* For the emacs weenies in the crowd. 00116 Local Variables: 00117 c-basic-offset: 2 00118 End: 00119 */ 00120 00121