Skip to content

Commit 4428fe0

Browse files
authored
Merge pull request #20 from ichiro-its/enhancement/refactor-distance-calculation
[Enhancement] - Refactor Distance Calculation
2 parents f4ae94e + 5249efd commit 4428fe0

File tree

3 files changed

+30
-114
lines changed

3 files changed

+30
-114
lines changed

include/atama/head/process/head.hpp

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -116,29 +116,9 @@ class Head
116116
keisan::Point2 calculate_object_position_from_pixel(double pixel_x, double pixel_y, bool is_ball = false);
117117
keisan::Point2 calculate_angle_offset_from_pixel(double pixel_x, double pixel_y);
118118

119-
double calculate_distance_from_pan_tilt()
120-
{
121-
return calculate_distance_from_pan_tilt(get_pan_angle(), get_tilt_angle());
122-
}
123-
double calculate_distance_from_pan_tilt(double pan, double tilt);
124-
125-
double calculate_distance_from_pan_tilt_using_regression()
126-
{
127-
return calculate_distance_from_pan_tilt_using_regression(get_pan_angle(), get_tilt_angle());
128-
}
129-
double calculate_distance_from_pan_tilt_using_regression(double pan, double tilt);
130-
131-
double calculate_distance_from_tilt()
132-
{
133-
return calculate_distance_from_tilt(get_tilt_angle());
134-
}
135-
double calculate_distance_from_tilt(double tilt);
136-
137-
double calculate_tilt_from_pan_distance(double distance)
138-
{
139-
return calculate_tilt_from_pan_distance(get_pan_angle(), distance);
140-
}
141-
double calculate_tilt_from_pan_distance(double pan, double distance);
119+
double calculate_distance_from_pan_tilt();
120+
121+
double calculate_tilt_from_pan_distance(double distance);
142122

143123
void look_to_position(double goal_position_x, double goal_position_y);
144124

@@ -183,16 +163,13 @@ class Head
183163
double tilt_p_gain;
184164
double tilt_d_gain;
185165

186-
std::vector<double> pan_tilt_to_dist_coefficients_;
187-
std::vector<std::vector<int>> pan_tilt_to_dist_degress_;
166+
std::vector<double> pan_tilt_to_dist_coefficients;
167+
std::vector<double> pan_distance_to_tilt_coefficients;
168+
std::vector<std::vector<int>> distance_regression_degrees;
188169

189170
double horizontal_fov;
190171
double vertical_fov;
191172

192-
double pan_tilt_to_distance_[7][7];
193-
double tilt_to_distance_[5];
194-
double pan_distance_to_tilt_[5][5];
195-
196173
double pan_error;
197174
double pan_error_difference;
198175

src/atama/head/node/head_node.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ void HeadNode::publish_head_data()
115115

116116
pan_tilt_msg.pan_angle = head->get_pan_angle();
117117
pan_tilt_msg.tilt_angle = head->get_tilt_angle();
118-
pan_tilt_msg.distance = head->calculate_distance_from_pan_tilt(
119-
head->get_pan_angle(), head->get_tilt_angle());
118+
pan_tilt_msg.distance = head->calculate_distance_from_pan_tilt();
120119

121120
set_head_publisher->publish(pan_tilt_msg);
122121
}

src/atama/head/process/head.cpp

Lines changed: 23 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -379,61 +379,37 @@ void Head::scan_custom(control::Command scan_type)
379379
process();
380380
}
381381

382-
double Head::calculate_distance_from_pan_tilt(double pan, double tilt)
383-
{
384-
double distance = 0.0;
385-
386-
for (int i = 6; i >= 0; --i) {
387-
for (int j = 0; j <= i; ++j) {
388-
double x1 = pow((pan + pan_center + pan_offset), (i - j));
389-
double x2 = pow((tilt + tilt_center + tilt_offset), j);
390-
distance += (pan_tilt_to_distance_[i - j][j]) * x1 * x2;
391-
} // action_->playUntilStop(motion_manager_->WALKREADY);
392-
}
393-
394-
return (distance > 0.0) ? distance : 0.0;
395-
}
396-
397-
double Head::calculate_distance_from_pan_tilt_using_regression(double pan, double tilt)
382+
double Head::calculate_distance_from_pan_tilt()
398383
{
399384
double distance = 0.0;
385+
double pan = get_pan_angle();
386+
double tilt = get_tilt_angle();
400387
int coef_index = 0;
401-
for (int i = 0; i < pan_tilt_to_dist_coefficients_.size(); i++)
388+
for (int i = 0; i < pan_tilt_to_dist_coefficients.size(); i++)
402389
{
403-
double x1 = pow((pan + pan_offset), pan_tilt_to_dist_degress_[i][0]);
404-
double x2 = pow((tilt + tilt_offset), pan_tilt_to_dist_degress_[i][1]);
405-
406-
distance += pan_tilt_to_dist_coefficients_[coef_index++] * x1 * x2;
407-
}
390+
double x1 = pow((pan + pan_offset), distance_regression_degrees[i][0]);
391+
double x2 = pow((tilt + tilt_offset), distance_regression_degrees[i][1]);
408392

409-
return (distance > 0.0) ? distance : 0.0;
410-
}
411-
412-
double Head::calculate_distance_from_tilt(double tilt)
413-
{
414-
double distance = 0.0;
415-
416-
for (int i = 0; i <= 4; ++i) {
417-
double x2 = pow((tilt + tilt_center + tilt_offset), i);
418-
distance += (tilt_to_distance_[i]) * x2;
393+
distance += pan_tilt_to_dist_coefficients[coef_index++] * x1 * x2;
419394
}
420395

421396
return (distance > 0.0) ? distance : 0.0;
422397
}
423398

424-
double Head::calculate_tilt_from_pan_distance(double pan, double distance)
399+
double Head::calculate_tilt_from_pan_distance(double distance)
425400
{
426401
double tilt = 0.0;
402+
double pan = get_pan_angle();
403+
int coef_index = 0;
404+
for (int i = 0; i < pan_distance_to_tilt_coefficients.size(); i++)
405+
{
406+
double x1 = pow((pan + pan_offset), distance_regression_degrees[i][0]);
407+
double x2 = pow((tilt + tilt_offset), distance_regression_degrees[i][1]);
427408

428-
for (int i = 4; i >= 0; --i) {
429-
for (int j = 0; j <= i; ++j) {
430-
double x1 = pow((pan + pan_center + pan_offset), (i - j));
431-
double x2 = pow(distance, j);
432-
tilt += (pan_distance_to_tilt_[i - j][j]) * x1 * x2;
433-
}
409+
tilt += pan_distance_to_tilt_coefficients[coef_index++] * x1 * x2;
434410
}
435411

436-
return ((tilt < 15.0) ? tilt : 15.0) - tilt_center - tilt_offset;
412+
return tilt;
437413
}
438414

439415
void Head::look_to_position(double goal_position_x, double goal_position_y)
@@ -444,7 +420,7 @@ void Head::look_to_position(double goal_position_x, double goal_position_y)
444420
float dy = goal_position_y - robot_position_y;
445421

446422
float pan = yaw - keisan::signed_arctan(dy, dx).normalize().degree();
447-
float tilt = calculate_tilt_from_pan_distance(keisan::signed_arctan(dy, dx).degree());
423+
float tilt = calculate_tilt_from_pan_distance(std::hypot(dx, dy));
448424

449425
move_by_angle(pan - pan_center, tilt);
450426
}
@@ -510,10 +486,11 @@ void Head::load_config(const std::string & file_name)
510486
std::cerr << "parse error at byte " << ex.byte << std::endl;
511487
throw ex;
512488
}
513-
} else if (key == "PanTiltToDistanceRegression") {
489+
} else if (key == "DistanceRegression") {
514490
try {
515-
val.at("distance_polynomial_coefficients").get_to(pan_tilt_to_dist_coefficients_);
516-
val.at("distance_polynomial_degrees").get_to(pan_tilt_to_dist_degress_);
491+
val.at("distance_polynomial_coefficients").get_to(pan_tilt_to_dist_coefficients);
492+
val.at("tilt_polynomial_coefficients").get_to(pan_distance_to_tilt_coefficients);
493+
val.at("distance_polynomial_degrees").get_to(distance_regression_degrees);
517494
} catch (nlohmann::json::parse_error & ex) {
518495
std::cerr << "parse error at byte " << ex.byte << std::endl;
519496
throw ex;
@@ -526,43 +503,6 @@ void Head::load_config(const std::string & file_name)
526503
std::cerr << "parse error at byte " << ex.byte << std::endl;
527504
throw ex;
528505
}
529-
} else if (key == "PanTiltToDistance") {
530-
try {
531-
for (int i = 6; i >= 0; --i) {
532-
for (int j = 0; j <= i; ++j) {
533-
char buffer[32];
534-
snprintf(buffer, sizeof(buffer), "pan_%d_tilt_%d", (i - j), j);
535-
val.at(buffer).get_to(pan_tilt_to_distance_[i - j][j]);
536-
}
537-
}
538-
} catch (nlohmann::json::parse_error & ex) {
539-
std::cerr << "parse error at byte " << ex.byte << std::endl;
540-
throw ex;
541-
}
542-
} else if (key == "TiltToDistance") {
543-
try {
544-
for (int i = 0; i <= 4; ++i) {
545-
char buffer[32];
546-
snprintf(buffer, sizeof(buffer), "pan_%d_tilt_%d", 0, i);
547-
val.at(buffer).get_to(tilt_to_distance_[i]);
548-
}
549-
} catch (nlohmann::json::parse_error & ex) {
550-
std::cerr << "parse error at byte " << ex.byte << std::endl;
551-
throw ex;
552-
}
553-
} else if (key == "PanDistanceToTilt") {
554-
try {
555-
for (int i = 4; i >= 0; --i) {
556-
for (int j = 0; j <= i; ++j) {
557-
char buffer[32];
558-
snprintf(buffer, sizeof(buffer), "pan_%d_distance_%d", (i - j), j);
559-
val.at(buffer).get_to(pan_distance_to_tilt_[i - j][j]);
560-
}
561-
}
562-
} catch (nlohmann::json::parse_error & ex) {
563-
std::cerr << "parse error at byte " << ex.byte << std::endl;
564-
throw ex;
565-
}
566506
}
567507
}
568508
} catch (nlohmann::json::parse_error & ex) {
@@ -673,9 +613,9 @@ keisan::Point2 Head::calculate_object_position_from_pixel(double pixel_x, double
673613
// calculate object distance from pixels and center distance
674614
double horizontal_degree = horizontal_fov * pixel_x;
675615
double vertical_degree = vertical_fov * pixel_y;
676-
double center_real_distance = is_ball ? Head::calculate_distance_from_pan_tilt_using_regression() : Head::calculate_distance_from_pan_tilt_using_regression() + 7;
616+
double center_real_distance = is_ball ? Head::calculate_distance_from_pan_tilt() : Head::calculate_distance_from_pan_tilt() + 7;
677617

678-
if (Head::calculate_distance_from_pan_tilt_using_regression() > -1) {
618+
if (Head::calculate_distance_from_pan_tilt() > -1) {
679619
double camera_height = center_real_distance / keisan::make_radian((90 + Head::get_tilt_angle())).tan();
680620
double x_relative_from_camera = camera_height * keisan::make_radian((90 + Head::get_tilt_angle() - vertical_degree)).tan();
681621
double y_relative_from_camera = x_relative_from_camera * keisan::make_radian(horizontal_degree).tan();

0 commit comments

Comments
 (0)