Skip to content

Commit c950f86

Browse files
author
purduerm
committed
fix bugs in PitchLookupModel
1 parent 5c4d741 commit c950f86

5 files changed

Lines changed: 54 additions & 58 deletions

File tree

src/prm_control/control_communicator/include/PitchLookupModel.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class PitchLookupModel {
1414
PitchLookupModel();
1515
explicit PitchLookupModel(std::string filename);
1616

17-
int load_file();
17+
void load_file();
1818
void write_file();
19-
float get_pitch(int distance, int height);
19+
float get_offset(int distance, int height);
2020

2121
private:
2222
int lower_y = INT_MAX;
@@ -28,6 +28,7 @@ class PitchLookupModel {
2828
std::thread timer;
2929

3030
float map(float value, float old_min, float old_max, float new_min, float new_max);
31+
void print_to_file(std::string text);
3132
};
3233

3334
#endif // PITCH_LOOKUP_MODEL_HPP
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
3 3
2-
500 0 0.00
2+
1000 -1000 0.00
33
1000 0 0.00
4-
1500 0 0.00
5-
2000 200 0.00
6-
2500 0 0.00
4+
1000 1000 0.00
5+
2000 -1000 0.00
6+
2000 0 0.00
7+
2000 1000 10.00
8+
3000 -1000 0.00
79
3000 0 0.00
8-
3500 0 0.00
9-
4000 200 0.00
10-
4500 0 0.00
10+
3000 1000 10.00

src/prm_control/control_communicator/src/ControlCommunicator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void ControlCommunicator::compute_aim(float bullet_speed, float target_x, float
6565

6666
// lookup table for empirical pitch correction
6767
float dst = sqrt(target_x * target_x + target_y * target_y + target_z * target_z);
68-
pitch += lut->get_pitch(dst, -target_y);
68+
pitch += lut->get_offset(dst, target_y);
6969

7070
// check nan and ensure between -180 and 180
7171
if (std::isnan(pitch) || std::isnan(yaw) ||

src/prm_control/control_communicator/src/ControlCommunicatorNode.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void ControlCommunicatorNode::publish_static_tf(float x, float y, float z, float
6767
}
6868

6969
void ControlCommunicatorNode::auto_aim_handler(const std::shared_ptr<vision_msgs::msg::PredictedArmor> msg)
70-
{
70+
{
7171
if (!control_communicator->is_connected || control_communicator->port_fd < 0)
7272
{
7373
RCLCPP_WARN(this->get_logger(), "UART Not connected, ignoring aim message.");
@@ -87,16 +87,18 @@ void ControlCommunicatorNode::auto_aim_handler(const std::shared_ptr<vision_msgs
8787
int bytes_written = write(control_communicator->port_fd, &package, sizeof(PackageOut));
8888
fsync(control_communicator->port_fd);
8989

90+
if (this->auto_aim_frame_id % 100 == 0 && this->auto_aim_frame_id != 0)
91+
{
92+
float dst = sqrt(pow(msg->x, 2) + pow(msg->y, 2) + pow(msg->z, 2));
93+
RCLCPP_INFO(this->get_logger(), "Yaw: %.2f | Pitch: %.2f | dst: %.2f | (x, y, z): (%.2f, %.2f, %.2f)", yaw, pitch, dst, msg->x, msg->y, msg->z);
94+
}
95+
9096
if (bytes_written != sizeof(PackageOut))
9197
{
9298
RCLCPP_ERROR(this->get_logger(), "Failed to write complete package to UART. Bytes written: %d, Expected: %lu", bytes_written, sizeof(PackageOut));
9399
return;
94100
}
95101

96-
if (this->auto_aim_frame_id % 100 == 0)
97-
{
98-
RCLCPP_INFO(this->get_logger(), "Auto Aim ID: %d | yaw: %f | pitch: %f | dst: %f", this->auto_aim_frame_id, package.autoAimPackage.yaw, package.autoAimPackage.pitch, roundf(sqrt(msg->x * msg->x + msg->y * msg->y + msg->z * msg->z)));
99-
}
100102
auto_aim_frame_id++;
101103
}
102104

@@ -183,7 +185,7 @@ void ControlCommunicatorNode::read_uart()
183185
old_target_robot_color = target_robot_color.data;
184186
}
185187

186-
if (this->auto_aim_frame_id % 500 == 0 && this->auto_aim_frame_id != 0)
188+
if (this->auto_aim_frame_id % 5000 == 0 && this->auto_aim_frame_id != 0)
187189
{
188190
RCLCPP_INFO(this->get_logger(), "READ UART: x: %f | y: %f | x_vel: %f | y_vel: %f | yaw_vel: %f | pitch_vel: %f | pitch: %f | is_enemy_red: %d | is_match_running: %d", package.x, package.y, package.x_vel, package.y_vel, this->yaw_vel, this->pitch_vel, this->pitch, this->is_enemy_red, this->is_match_running);
189191
}

src/prm_control/control_communicator/src/PitchLookupModel.cpp

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ PitchLookupModel::PitchLookupModel() {}
44

55
PitchLookupModel::PitchLookupModel(std::string filename) {
66
this->filename = filename;
7-
int succ = this->load_file();
8-
assert(succ == 1);
7+
this->load_file();
98

109
// timer to read file every 2 seconds for updates
1110
this->timer = std::thread([this]() {
@@ -26,10 +25,10 @@ PitchLookupModel::PitchLookupModel(std::string filename) {
2625
* z coordinate, y coordinate, and pitch. Length measurements are in
2726
* millimeters, and angle measurements are in degrees.
2827
*/
29-
int PitchLookupModel::load_file() {
28+
void PitchLookupModel::load_file() {
3029
FILE* fp = fopen(filename.c_str(), "r");
3130
if (fp == nullptr) {
32-
return -1;
31+
return;
3332
}
3433

3534
int y_count = 0;
@@ -48,7 +47,7 @@ int PitchLookupModel::load_file() {
4847
if (read_count != 3) {
4948
fclose(fp);
5049
fp = NULL;
51-
return -2;
50+
return;
5251
}
5352

5453
if (z < this->lower_z) this->lower_z = z;
@@ -62,7 +61,6 @@ int PitchLookupModel::load_file() {
6261

6362
fclose(fp);
6463
fp = NULL;
65-
return 1;
6664
}
6765

6866
/**
@@ -92,20 +90,18 @@ void PitchLookupModel::write_file() {
9290
fp = NULL;
9391
}
9492

93+
9594
/**
96-
* @brief Lookup the corresponding pitch with distance and height
95+
* @brief Lookup the corresponding pitch with distance and height, using bilinear interpolation
9796
*
9897
* @param distance Distance away from the camera in millimeters
9998
* @param height Height above the ground in millimeters
10099
*
101100
* @return Pitch in degrees
102101
*/
103-
float PitchLookupModel::get_pitch(int distance, int height) {
104-
assert(this->pitch_lookup_table.size() > 1);
105-
assert(this->pitch_lookup_table[0].size() > 1);
106-
102+
float PitchLookupModel::get_offset(int distance, int height) {
107103
// Calculate the step size of the lookup table
108-
int step_size = (this->upper_z - this->lower_z) /
104+
int step_size = (this->upper_z - this->lower_z) /
109105
(this->pitch_lookup_table.size() - 1);
110106

111107
// Calculate the indices of the tightest bound containing distance and height
@@ -120,45 +116,31 @@ float PitchLookupModel::get_pitch(int distance, int height) {
120116
int lower_step_y = this->lower_y + step_size * lower_step_y_idx;
121117
int upper_step_y = this->lower_y + step_size * upper_step_y_idx;
122118

123-
// Calculate the pitch of the four points with tightest bounds
124119
if (lower_step_z_idx < 0 || lower_step_z_idx >= this->pitch_lookup_table.size() ||
125120
upper_step_z_idx < 0 || upper_step_z_idx >= this->pitch_lookup_table.size() ||
126121
lower_step_y_idx < 0 || lower_step_y_idx >= this->pitch_lookup_table[0].size() ||
127122
upper_step_y_idx < 0 || upper_step_y_idx >= this->pitch_lookup_table[0].size()) {
128123
return 0; // No computed pitch offset
129124
}
130125

126+
// Calculate the pitch of the four points with tightest bounds
131127
float pitch_low_low = this->pitch_lookup_table[lower_step_z_idx][lower_step_y_idx];
132128
float pitch_low_high = this->pitch_lookup_table[lower_step_z_idx][upper_step_y_idx];
133129
float pitch_high_low = this->pitch_lookup_table[upper_step_z_idx][lower_step_y_idx];
134130
float pitch_high_high = this->pitch_lookup_table[upper_step_z_idx][upper_step_y_idx];
135131

136-
// Calculate pitch of the corresponding z position by fixing y to the lower bound
137-
float pitch_y_low = this->map(
138-
static_cast<float>(distance),
139-
static_cast<float>(lower_step_z),
140-
static_cast<float>(upper_step_z),
141-
std::fmin(pitch_low_low, pitch_high_low),
142-
std::fmax(pitch_low_low, pitch_high_low));
143-
144-
// Calculate pitch of the corresponding z position by fixing y to the upper bound
145-
float pitch_y_high = this->map(
146-
static_cast<float>(distance),
147-
static_cast<float>(lower_step_z),
148-
static_cast<float>(upper_step_z),
149-
std::fmin(pitch_low_high, pitch_high_high),
150-
std::fmax(pitch_low_high, pitch_high_high));
151-
152-
assert(upper_z != lower_z);
153-
assert(step_size != 0);
154-
155-
// Combine the calculated new bounds to calculate the new pitch
156-
return this->map(
157-
static_cast<float>(height),
158-
static_cast<float>(lower_step_y),
159-
static_cast<float>(upper_step_y),
160-
std::fmin(pitch_y_low, pitch_y_high),
161-
std::fmax(pitch_y_low, pitch_y_high));
132+
// Calculate the pitch using bilinear interpolation
133+
float pitch_low = (pitch_low_low * (upper_step_y - height) +
134+
pitch_low_high * (height - lower_step_y)) /
135+
(upper_step_y - lower_step_y);
136+
float pitch_high = (pitch_high_low * (upper_step_y - height) +
137+
pitch_high_high * (height - lower_step_y)) /
138+
(upper_step_y - lower_step_y);
139+
float pitch = (pitch_low * (upper_step_z - distance) +
140+
pitch_high * (distance - lower_step_z)) /
141+
(upper_step_z - lower_step_z);
142+
143+
return pitch;
162144
}
163145

164146
/**
@@ -172,8 +154,8 @@ float PitchLookupModel::get_pitch(int distance, int height) {
172154
*
173155
* @return Mapped value
174156
*/
175-
float PitchLookupModel::map(float value, float old_min, float old_may,
176-
float new_min, float new_may) {
157+
float PitchLookupModel::map(float value, float old_min, float old_may, float new_min, float new_may)
158+
{
177159
if (old_min > old_may || new_min > new_may) {
178160
return -1;
179161
}
@@ -183,4 +165,15 @@ float PitchLookupModel::map(float value, float old_min, float old_may,
183165
}
184166

185167
return new_min + (value - old_min) * (new_may - new_min) / (old_may - old_min);
186-
}
168+
}
169+
170+
void PitchLookupModel::print_to_file(std::string text) {
171+
FILE* fp = fopen("/home/purduerm/Documents/output.txt", "a");
172+
if (fp == nullptr) {
173+
return;
174+
}
175+
176+
fprintf(fp, "%s\n", text.c_str());
177+
fclose(fp);
178+
}
179+

0 commit comments

Comments
 (0)