1 #ifndef OBJECTCONTACTTURNAROUNDDETECTORBASE_H 2 #define OBJECTCONTACTTURNAROUNDDETECTORBASE_H 4 #include "../TorqueFilter/IIRFilter.h" 5 #include <boost/shared_ptr.hpp> 8 #include "hrpsys/util/Hrpsys.h" 32 detector_total_wrench
dtw;
37 std::vector<process_mode>
pmode;
42 dt(_dt), detect_ratio_thre(0.01), start_ratio_thre(0.5), forgetting_ratio_thre(1e3),
43 max_time(0.0), current_time(0.0),
44 detect_count_thre(5), start_count_thre(5), other_detect_count_thre(round(detect_count_thre*1.5)), dtw(
TOTAL_FORCE),
45 is_filter_reset(false), is_hold_values(false), is_other_constraint_detected(false),
46 ref_dwrench(
std::vector<double>(1, 0.0)), raw_wrench(
std::vector<double>(1, 0.0)), filtered_wrench_with_hold(
std::vector<double>(1, 0.0)), filtered_friction_coeff_wrench_with_hold(
std::vector<double>(1, 0.0)),
47 phi1(
std::vector<double>(1, 0.0)), phi2(
std::vector<double>(1, 0.0)), dphi1(
std::vector<double>(1, 0.0)),
51 double default_cutoff_freq = 1;
61 if (ref_dwrench.size() != 1) ref_dwrench.resize(1);
64 ref_dwrench[0] = _ref_diff_wrench/_max_time;
70 for (
size_t i = 0;
i < count.size();
i++) {
75 is_filter_reset =
true;
76 is_other_constraint_detected =
false;
77 std::cerr <<
"[" << print_str <<
"] Start Object Turnaround Detection (";
78 std::cerr <<
"ref_dwrench = [";
79 for (
size_t i = 0;
i < ref_dwrench.size();
i++) std::cerr << ref_dwrench[
i] <<
", ";
80 std::cerr <<
"], detect_thre = [";
81 for (
size_t i = 0;
i < ref_dwrench.size();
i++) std::cerr << ref_dwrench[
i] * detect_ratio_thre <<
", ";
82 std::cerr <<
"], start_thre = [";
83 for (
size_t i = 0;
i < ref_dwrench.size();
i++) std::cerr << ref_dwrench[
i] * start_ratio_thre <<
", ";
84 std::cerr <<
"]), max_time = " << max_time <<
"[s]" << std::endl;
88 phi1.resize(generalized_wrench_dim);
89 phi2.resize(generalized_wrench_dim);
90 dphi1.resize(generalized_wrench_dim);
91 filtered_wrench_with_hold.resize(generalized_wrench_dim);
92 filtered_friction_coeff_wrench_with_hold.resize(generalized_wrench_dim);
93 raw_wrench.resize(generalized_wrench_dim);
94 count.resize(generalized_wrench_dim);
95 pmode.resize(generalized_wrench_dim);
96 for (
size_t i = 0;
i < generalized_wrench_dim;
i++) {
102 const std::vector<hrp::Vector3>&
forces,
const std::vector<hrp::Vector3>& moments,
const std::vector<hrp::Vector3>& hposv)
105 total_force = total_moment1 = total_moment2 = hrp::Vector3::Zero();
106 for (
size_t i = 0;
i < forces.size();
i++) {
107 total_force += forces[
i];
108 total_moment1 += hposv[
i].cross(forces[
i]);
109 total_moment2 += moments[i];
113 const std::vector<hrp::Vector3>& moments,
114 const std::vector<hrp::Vector3>& hposv)
134 ret =
checkDetection(axis.dot(total_moment1 - moment_center.cross(total_force)), 0.0);
140 ret =
checkDetection(axis.dot(total_moment1 + total_moment2 - moment_center.cross(total_force)), 0.0);
146 for (
size_t i = 0;
i < 3;
i++) {
147 resultant_OR_wrench(
i) = total_force(
i);
148 resultant_OR_wrench(
i+3) = total_moment1(
i) + total_moment2(
i);
158 bool checkDetection (
const double raw_wrench_value,
const double raw_friction_coeff_wrench_value)
160 if (is_filter_reset) {
161 std::cerr <<
"[" << print_str <<
"] Object Turnaround Detection Reset Values. (raw_wrench_value = " << raw_wrench_value <<
", raw_friction_coeff_wrench_value = " << raw_friction_coeff_wrench_value <<
")" << std::endl;
162 wrench_filter->reset(raw_wrench_value);
163 dwrench_filter->reset(0);
164 friction_coeff_wrench_filter->reset(raw_friction_coeff_wrench_value);
165 filtered_wrench_with_hold[0] = wrench_filter->getCurrentValue();
166 filtered_friction_coeff_wrench_with_hold[0] = friction_coeff_wrench_filter->getCurrentValue();
167 is_filter_reset =
false;
169 raw_wrench[0] = raw_wrench_value;
170 double prev_filtered_wrench = wrench_filter->getCurrentValue();
171 double filtered_wrench = wrench_filter->passFilter(raw_wrench_value);
172 double filtered_dwrench = dwrench_filter->passFilter((filtered_wrench-prev_filtered_wrench)/dt);
173 friction_coeff_wrench_filter->passFilter(raw_friction_coeff_wrench_value);
176 filtered_wrench_with_hold[0] = wrench_filter->getCurrentValue();
177 filtered_friction_coeff_wrench_with_hold[0] = friction_coeff_wrench_filter->getCurrentValue();
180 phi1[0] = wrench_filter->getCurrentValue();
181 phi2[0] = friction_coeff_wrench_filter->getCurrentValue();
182 dphi1[0] = dwrench_filter->getCurrentValue();
187 if (is_filter_reset) {
188 std::cerr <<
"[" << print_str <<
"] Object Turnaround Detection Reset Values. (raw_resultant_wrench_value = " << raw_resultant_wrench_value.format(Eigen::IOFormat(Eigen::StreamPrecision, 0,
", ",
", ",
"",
"",
"[",
"]")) <<
")" << std::endl;
189 resultant_wrench_filter->reset(raw_resultant_wrench_value);
190 resultant_dwrench_filter->reset(hrp::dvector6::Zero());
192 hrp::dvector6 prev_filtered_resultant_wrench = resultant_wrench_filter->getCurrentValue();
193 hrp::dvector6 filtered_resultant_wrench = resultant_wrench_filter->passFilter(raw_resultant_wrench_value);
194 hrp::dvector6 filtered_resultant_dwrench = resultant_dwrench_filter->passFilter((filtered_resultant_wrench - prev_filtered_resultant_wrench)/dt);
198 if (is_filter_reset) {
199 filtered_wrench_with_hold =
phi1;
200 filtered_friction_coeff_wrench_with_hold =
phi2;
201 filtered_resultant_wrench_with_hold = filtered_resultant_wrench;
202 is_filter_reset =
false;
206 filtered_wrench_with_hold =
phi1;
207 filtered_friction_coeff_wrench_with_hold =
phi2;
208 filtered_resultant_wrench_with_hold = filtered_resultant_wrench;
217 for (
size_t i = 0;
i < ccm.size();
i++) phi[
i] = ccm[
i].
dot(res_wrench);
224 for (
size_t i = 0;
i < ref_dwrench.size();
i++) {
225 if (!is_other_constraint_detected) {
228 if ( (ref_dwrench[i] > 0.0) ? (tmp_dwrench[i] > ref_dwrench[i]*
start_ratio_thre) : (tmp_dwrench[i] < ref_dwrench[i]*start_ratio_thre) ) {
230 if (count[i] > start_count_thre) {
233 std::cerr <<
"[" << print_str <<
"] Object Turnaround Detection Started [idx=" << i <<
"]. (time = " << current_time <<
"[s], " << start_count_thre*dt <<
"[s] after the first start detection)" << std::endl;
240 if ( (ref_dwrench[i] > 0.0) ? (tmp_dwrench[i] < ref_dwrench[i]*
detect_ratio_thre) : (tmp_dwrench[i] > ref_dwrench[i]*detect_ratio_thre) ) {
242 if (count[i] > detect_count_thre) {
245 std::cerr <<
"[" << print_str <<
"] Object Turnaround Detected [idx=" << i <<
"]. (time = " << current_time <<
"[s], " << detect_count_thre*dt <<
"[s] after the first detection)" << std::endl;
248 if ( ((ref_dwrench[i] > 0.0) ? (tmp_dwrench[i] > ref_dwrench[i]*
forgetting_ratio_thre) : (tmp_dwrench[i] < ref_dwrench[i]*forgetting_ratio_thre)) &&
257 if (count[i] > other_detect_count_thre) {
258 is_other_constraint_detected =
true;
259 std::cerr <<
"[" << print_str <<
"] Object Turnaround Other Detected Time Limit [idx=" << i <<
"]. (time = " << current_time <<
"[s], " << other_detect_count_thre*dt <<
"[s] after the first detection)" << std::endl;
268 if (max_time <= current_time && (pmode[i] !=
MODE_DETECTED)) {
269 if (pmode[i] !=
MODE_MAX_TIME) std::cerr <<
"[" << print_str <<
"] Object Turnaround Detection max time reached. [idx=" << i <<
"]" << std::endl;
282 for (
size_t i = 0;
i < pmode.size();
i++) {
292 tmpstr =
"TOTAL_FORCE";
break;
294 tmpstr =
"TOTAL_MOMENT";
break;
296 tmpstr =
"TOTAL_MOMENT2";
break;
298 tmpstr =
"GENERALIZED_WRENCH";
break;
302 std::cerr <<
"[" << print_str <<
"] ObjectContactTurnaroundDetectorBase params (" << tmpstr <<
")" << std::endl;
303 std::cerr <<
"[" << print_str <<
"] wrench_cutoff_freq = " << wrench_filter->getCutOffFreq() <<
"[Hz], dwrench_cutoff_freq = " << dwrench_filter->getCutOffFreq() <<
"[Hz], friction_coeff_wrench_freq = " << friction_coeff_wrench_filter->getCutOffFreq()
304 <<
"[Hz], resultant_wrench_cutoff_freq = " << resultant_wrench_filter->getCutOffFreq() <<
"[Hz], resultant_dwrench_cutoff_freq = " << resultant_dwrench_filter->getCutOffFreq() <<
"[Hz]" << std::endl;
305 std::cerr <<
"[" << print_str <<
"] detect_ratio_thre = " << detect_ratio_thre <<
", start_ratio_thre = " << start_ratio_thre <<
", forgetting_ratio_thre = " << forgetting_ratio_thre
306 <<
", start_time_thre = " << start_count_thre*dt <<
"[s], detect_time_thre = " << detect_count_thre*dt <<
"[s], other_detect_time_thre = " << other_detect_count_thre*dt << std::endl;
307 std::cerr <<
"[" << print_str <<
"] axis = [" <<
axis(0) <<
", " <<
axis(1) <<
", " <<
axis(2)
309 std::cerr <<
"[" << print_str <<
"] constraint_conversion_matrix1 = [";
310 for (
size_t i = 0;
i < constraint_conversion_matrix1.size();
i++) {
311 std::cerr << constraint_conversion_matrix1[
i].format(Eigen::IOFormat(Eigen::StreamPrecision, 0,
", ",
", ",
"",
"",
"[",
"]"));
313 std::cerr <<
"], constraint_conversion_matrix2 = [";
314 for (
size_t i = 0;
i < constraint_conversion_matrix2.size();
i++) {
315 std::cerr << constraint_conversion_matrix2[
i].format(Eigen::IOFormat(Eigen::StreamPrecision, 0,
", ",
", ",
"",
"",
"[",
"]"));
317 std::cerr <<
"]" << std::endl;
318 std::cerr <<
"[" << print_str <<
"] is_hold_values = " << (is_hold_values?
"true":
"false") << std::endl;
319 std::cerr <<
"[" << print_str <<
"] ref_dwrench = [";
320 for (
size_t i = 0;
i < ref_dwrench.size();
i++) std::cerr << ref_dwrench[
i] <<
", ";
321 std::cerr <<
"], max_time = " << max_time <<
"[s]" << std::endl;
328 wrench_filter->setCutOffFreq(a);
329 friction_coeff_wrench_filter->setCutOffFreq(a);
330 resultant_wrench_filter->setCutOffFreq(a);
335 dwrench_filter->setCutOffFreq(a);
336 resultant_dwrench_filter->setCutOffFreq(a);
351 constraint_conversion_matrix1 = ccm1;
352 constraint_conversion_matrix2 = ccm2;
359 is_filter_reset =
true;
384 process_mode
getMode (
const size_t idx)
const {
return pmode[idx]; };
388 size_t data_size = 5;
390 for (
size_t i = 0;
i < pmode.size();
i++) {
391 size_t tmpoff =
i*data_size;
393 ret(1+tmpoff) = raw_wrench[
i];
394 ret(2+tmpoff) = phi1[
i];
395 ret(3+tmpoff) = dphi1[
i];
396 ret(4+tmpoff) = phi2[
i];
407 #endif // OBJECTCONTACTTURNAROUNDDETECTORBASE_H
static std::vector< std::vector< double > > forces
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
double dot(const Vector3 &v1, const Vector3 &v2)
Eigen::Matrix< double, 6, 1 > dvector6