@@ -4,8 +4,7 @@ PitchLookupModel::PitchLookupModel() {}
44
55PitchLookupModel::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