Posted 14 October 2026
WallE, my autonomous wall-following robot had a nominal capability called “run to daylight”. The idea was to use this as a last resort when all the other ‘find a wall and follow it’ tricks failed. Unfortunately, I never could get this to work and never figured out why. Fast forward to the present, and Grok and I were trying to implement the same kind of capability into the new vision-enhanced version, to address the issue of what to do when the robot gets stuck.
Basically, the algorithm is the same as it was for the wall-following version; back up, look around and turn to the heading that provides the most freedom (‘largest depth’ in vision processing language). We (Grok and I) implemented the “back up and look around” operations using the new ‘L/Rxxx’ (where ‘xxx’ is in degrees), and then the ‘turn to the best heading’ utilizes a new ‘Txx’ command which is implemented via the already-existing ‘TurnToHdgDeg()’ function.
Unfortunately, although the ‘back up and look around’ part worked almost immediately, the ‘turn to the best heading’ part failed miserably, with the end result consistently going off in a completely different direction. I was somewhat mystified by this, as I was suffering from the (soon-to-be-debunked) belief that my MPU6050 IMU/DMP was actually delivering accurate heading measurements, and the problem must be in the ‘TurnToHdgDeg()’ function. However, data beats belief, so I decided to go back to basic ‘show me’ experiments.
To validate the MPU6050 IMU function operation, I set up an experiment where I manually rotated the robot on a printed compass rose, and measured the output of the IMU at each point, as shown in the following Excel table and resulting plot:

The above plot looks ugly, but a lot of the ugliness is due to the way IMU heading output jumps from +180 to -179; this is normal. What isn’t normal is the way the IMU reported heading diverges from the actual heading – the +180 to -179 transition occurs between 150 and 160 degrees rather than 180, and the -179 to +180 transition occurs between -140 and -150 degrees on the other side.
At first I thought this must be due to a bad IMU module, but changing out the module for a brand-new one produced the same results – so not that (further validation of my “hardware never fails – its ALWAYS the software” mantra).
So, I made another plot, this time manually changing the ‘opposite sign’ measurements back into the proper range by adding/subtracting 360 degrees. This produced the following plot:

Just looking at the ‘positive’ half from 0 to 180 deg, it is clear that the measured values are consistently higher than the actual physical headings. Looking at the first five values (before the first breakpoint in the plot) we see the differential goes from zero to about 156.8-120 = 36.8 deg over a 150 deg range, or about 36.8/150 = 0.25 deg/deg. Assuming the difference is linear, then the required multiplication factor should be 156.8X = 120 –> X = 120/156.8 = 0.769. For example, a multiplicative factor of 0.787 yielded the following plot:

Good enough for government work ;).
This factor can’t be applied directly to the IMU yaw output, as that value has already been corrected for the +/- 180 arrangement, so Grok suggested an algorithm to hold a ‘running total’ heading that uses IMU heading changes to produce a separate heading value that is corrected for the slope error and only afterwards applies the +/- 360 adjustment for the +/-180 scheme, as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
float UpdateIMUHdgValDeg() { //Purpose: Get latest yaw (heading) value from IMU //Inputs: None. This function should only be called after mpu.dmpPacketAvailable() returns TRUE //Outputs: // returns true if successful, otherwise false // IMUHdgValDeg updated on success //Plan: //Step1: check for overflow and reset the FIFO if it occurs. In this case, wait for new packet //Step2: read all available packets to get to latest data //Step3: 10/05/26: calculate delta from last raw heading value //Step4: 10/05/26: scale delta by correction factor (1/1.27) //Step5: 10/05/26: add scaled delta to last adjusted value to produce current corrected heading //Step6: update IMUHdgValDeg with latest corrected heading after adjusting for 180/-179 split //Notes: // 10/08/19 changed return type to boolean // 10/08/19 no longer need mpuIntStatus // 10/21/19 completely rewritten to use Homer's algorithm // 05/05/20 changed return type to float vs bool. // 06/13/23 added code to update gl_HdgHistoryArray for 'spinning' condx detection // 10/04/26 added Grok code to properly correct raw IMU headings //10/04/26 static makes them survive across calls for as long as the Teensy is powered static float prevRawDeg = 0; static float continuousDeg = 0; static bool haveYaw = false; int flag = GetCurrentFIFOPacket(fifoBuffer, packetSize, MAX_GETPACKET_LOOPS); //get the latest mpu packet if (flag != 0) //0 = error exit, 1 = normal exit, 2 = recovered from an overflow { // display Euler angles in degrees mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetGravity(&gravity, &q); mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); //compute the yaw value //10/03/26 added fudge factor to synch IMU output with physical reality float rawDeg = ypr[0] * 180.0f / M_PI; // still -180 to +180 if (!haveYaw) { prevRawDeg = rawDeg; continuousDeg = 0; // heading at power-up is 0 haveYaw = true; } else { float step = rawDeg - prevRawDeg; if (step > 180.0f) step -= 360.0f; // crossed the fold backward if (step < -180.0f) step += 360.0f; // crossed the fold forward continuousDeg += step * IMU_HDG_CORRECTION_FACTOR; //1.27 is the long scale from the protractor prevRawDeg = rawDeg; } IMUHdgValDeg = continuousDeg; while (IMUHdgValDeg > 180.0f) IMUHdgValDeg -= 360.0f; while (IMUHdgValDeg < -180.0f) IMUHdgValDeg += 360.0f; } //06/13/23 added to update gl_HdgHistoryArray for 'spinning' condx detection //all array entries bumped down one, with most recent value at i = HEADING_HISTORY_ARRAY_SIZE-1 for (uint16_t i = 1; i < HEADING_HISTORY_ARRAY_SIZE; i++) { gl_HdgHistoryArray[i - 1] = gl_HdgHistoryArray[i]; //gl_pSerPort->printf("gl_HdgHistoryArray[%d] = %2.2f\n", i, gl_HdgHistoryArray[i]); } gl_HdgHistoryArray[HEADING_HISTORY_ARRAY_SIZE - 1] = IMUHdgValDeg; return IMUHdgValDeg;//05/05/20 now returns updated value for use convenience } |
18 October 2026 Update:
Since the last post I have been working hard on the ‘Find Best Direction’ problem, and now I (with lots of help from Grok Code) have it pretty much licked. Rather than continue to mess up my robot Arduino code, I created a small C++ console project to study the process of converting the raw Heading/Front Distance values from a robot 360-degree scan into one or more ‘runs’ of distance/heading pairs where the distance part is greater than some threshold (currently set at 200cm).
Originally I thought that the way to go was to first reverse-sort the timestamp, heading and distance data fields with ‘distance’ as the master field. This would give me all the distances in descending order, with the associated heading and timestamp values listed with them. To do this I ported the ‘QuickSort’ machinery from my May 2024 ‘Wall3_Git’ project into my ‘stuck_recovery’ branch of my current ‘VisionEnhanced4WheelRobot’ Github repo, and then onward to my little ‘RobotBestDistV2.cpp’ exploratory project, shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
// QuickSortV3.cpp : This file contains the 'main' function. Program execution begins and ends there. // /*Quicksort Test program The idea is to determine whether or not I can sort multiple associated arrays by sorting a 'master' array (say front distances) and having other arrays be sorted using the same sort index sequence as the master array. Master (front distance) FrontD: int FrontD[] = { 219, 30, 114, 152, 41, 86 }; Slave (Heading) array: float Hdg[] = { 19, 3, 11, 15, 4, 8 }; */ #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; //int FrontD[] = { 9, 3, 4, 2, 1, 8 }; ////real measurement data //float FrontD[] = { 121.6, 124.1, 146.4, 114.2, 67.6, 59.9, 48.4, 44.2, 35.3, 37.9, 40.1, 42.8, 46.3, 52.4, 59.5, 93.0, 91.3, 85.5, 101.9, 229.0, 260.7, 312.9, 388.1, 414.6, 347.8, 58.1, 51.5, 54.8, 59.5, 75.7, 98.1, 38.3, 121.5, 114.5, 130.8, 138.5 }; //float Hdg[] = { 0.0, 9.4, 18.7, 28.5, 39.1, 49.2, 59.1, 69.2, 78.8, 88.4, 99.0, 109.0, 118.5, 128.3, 138.1, 154.9, 164.7, 174.2, -176.3, -160.3, -149.0, -138.9, -129.9, -118.0, -106.8, -96.8, -87.3, -77.5, -67.9, -58.1, -47.7, -37.4, -28.0, -18.2, -8.7, 1.1 }; std::vector<float> FrontD; std::vector<float> Hdg; //new plan; convert the arrays from the tabular form as they come off the generating program into the comma-delimited arrays as shown above //const char* rawScan = R"( //MSec FrontD Hdg //8717 106.0 0.0 //8979 427.9 6.1 //9179 79.4 10.2 //9443 57.6 16.4 //9707 58.0 22.6 //9914 61.6 26.5 //10177 62.5 33.0 //10441 67.7 39.2 //10650 77.5 43.6 //10913 85.0 49.9 //11177 138.8 56.4 //11321 123.8 60.2 //11585 134.4 66.7 //11847 134.8 72.4 //12056 133.9 76.4 //12319 128.8 82.2 //12528 124.3 86.5 //12791 143.5 92.5 //12991 148.4 96.4 //13255 148.8 102.3 //13399 145.0 106.5 //13661 118.6 112.8 //13870 77.6 116.6 //14133 70.7 122.5 //14277 62.9 126.7 //14541 60.7 133.0 //14685 52.5 136.9 //14949 48.2 143.4 //15213 44.5 149.5 //15595 37.3 160.1 //15859 35.5 165.8 //16123 36.3 171.4 //16332 38.9 175.2 //16596 38.8 -179.4 //16859 38.6 -173.8 //17123 40.2 -167.7 //17330 42.8 -163.7 //17593 43.2 -157.8 //17857 46.0 -152.1 //18121 49.9 -146.2 //18385 55.4 -140.1 //18585 61.1 -136.2 //18849 68.1 -129.9 //19113 78.3 -124.0 //19320 90.2 -119.2 //19583 89.3 -113.4 //19792 86.0 -109.1 //20055 84.2 -102.9 //20255 84.2 -98.8 //20519 89.2 -93.2 //20783 205.2 -87.2 //20927 225.2 -83.2 //21189 224.6 -77.8 //21453 237.6 -71.5 //21542 251.6 -67.2 //21805 276.1 -61.3 //21949 284.7 -57.2 //22213 321.4 -50.9 //22413 362.0 -47.0 //22677 396.5 -40.7 //22877 395.9 -36.7 //23139 395.1 -30.7 //23403 353.2 -25.2 //23612 262.6 -21.1 //23875 395.7 -15.0 //24139 50.5 -8.9 //24403 46.9 -2.8 //24667 47.5 3.2 //24874 50.3 7.2 //25137 52.1 13.3 //25401 57.3 19.2 //25610 62.9 23.2 // //)"; const char* rawScan = R"( MSec FrontD Hdg 8632 408.9 0.0 8894 433.5 - 7.1 9038 367.9 - 11.0 9238 381.3 - 14.7 9438 386.8 - 18.5 9702 366.9 - 24.7 9964 340.4 - 30.6 10173 304.5 - 34.8 10436 286.4 - 40.8 10645 266.9 - 44.9 10908 262.1 - 51.3 11172 233.7 - 57.6 11372 225.6 - 61.7 11636 163.5 - 68.6 11843 87.8 - 72.9 12106 85.9 - 79.1 12315 86.2 - 83.3 12578 89.7 - 89.9 12722 91.3 - 94.4 12922 91.2 - 98.1 13122 83.4 - 102.5 13322 70.8 - 106.3 13586 63.7 - 112.9 13673 60.1 - 117.0 13881 51.5 - 121.0 14024 48.0 - 125.1 14233 47.6 - 129.1 14376 42.9 - 132.8 14576 42.1 - 137.0 14776 38.5 - 141.0 15040 37.1 - 147.9 15240 35.3 - 151.7 15504 34.7 - 158.7 15648 29.8 - 162.9 15855 29.9 - 166.6 15943 29.9 - 170.9 16151 33.8 - 174.7 16414 35.2 180.0 16678 36.9 174.2 16942 38.2 168.1 17142 41.7 163.9 17406 42.9 157.2 17733 50.1 150.7 17876 54.3 146.6 18140 58.9 140.1 18404 73.2 133.9 18604 115.9 129.9 18868 135.9 123.7 19068 140.8 119.5 19332 139.3 113.3 19539 129.8 108.9 19802 111.7 102.3 19891 114.4 98.2 20099 113.3 93.8 20178 126.3 89.8 20387 142.0 85.6 20530 126.3 81.3 20730 31.2 77.3 20874 27.9 72.8 21194 81.5 62.8 21401 76.0 58.6 21544 70.9 54.5 21808 67.4 47.3 21952 61.0 43.1 22161 60.6 39.2 22304 60.0 34.8 22504 57.9 31.0 22704 61.2 26.7 22904 68.7 23.0 23168 417.9 16.9 23375 410.2 12.7 23638 341.2 6.7 )"; //int n = 36; //#define NUM_SAMPLES 36 int n = 72; #define NUM_SAMPLES 72 int THRESHOLD = 200; struct RunItem { float Hdg = 0; float Dist = 0; }; int bestStart = -1, bestEnd = -1; float bestWidth = 0; int numruns = 0; int runStart = -1; RunItem samples[NUM_SAMPLES]; // or 72 void ParseScan() { std::istringstream iss(rawScan); std::string line; while (std::getline(iss, line)) { if (line.empty() || line.find("FrontD") != std::string::npos) continue; // normalize whitespace and the spaced minus that paste sometimes inserts for (char& c : line) if (c == '\t') c = ' '; size_t pos; while ((pos = line.find(" - ")) != std::string::npos) line.replace(pos, 3, "-"); while ((pos = line.find(" -")) != std::string::npos) line.replace(pos, 2, "-"); std::istringstream row(line); float msec, dist, hdg; if (row >> msec >> dist >> hdg) { FrontD.push_back(dist); Hdg.push_back(hdg); } } cout << "FrontD has " << FrontD.size() << " elements\n"; } void TerminateRun(int i) { // run ended at i-1 float width = samples[i - 1].Hdg - samples[runStart].Hdg; //watch out for 'fold' width results > 180 or < -180 if (width > 180) { width -= 360; } else if (width < -180) { width += 360; } width = abs(width); if (width > bestWidth) { bestWidth = width; bestStart = runStart; bestEnd = i - 1; } runStart = -1; numruns++; } // fill samples[] during the scan (or copy from your existing arrays) void GetBestDistance() { //Purpose: Determine 'Best Distance' from a set of NUM_SAMPLES Hdg/Dist pairs //Inputs: // Fixed set of real samples in FrontD[NUMSAMPLES] and Hdg[NUMSAMPLES] synchronized arrays //Outputs: // bestWidth = width (in deg) of the largest contiguous run of FrontD measurements // bestStart, bestEnd = start & end idices for the 'bestWidth' run //Plan: // Step1: fill samples[] during the scan (or copy from your existing arrays) // Step2: work through entire Hdg/Dist sample items looking for contiguous runs (can be just one item) // for each such run, calc bestWidth, bestStart, bestEnd. If 'bestWidth' is greater than all // previous runs detected, replace the old bestWidth, bestStart, bestEnd values ParseScan(); for (int i = 0; i < NUM_SAMPLES; i++) { samples[i].Dist = FrontD[i]; samples[i].Hdg = Hdg[i]; //print out for debug cout << "samples[" << i << "] = (" << samples[i].Dist << ", " << samples[i].Hdg << ")\n"; } for (int i = 0; i < NUM_SAMPLES; i++) { if (samples[i].Dist >= THRESHOLD) { if (runStart < 0) { runStart = i; } // don’t forget to close a run that reaches the end of the array //terminate this run if we run out of samples if (i >= NUM_SAMPLES-1) { TerminateRun(i); } // start a new run // else just keep extending } //this doesn't execute unless samples[i] < THRESHOLD else if (runStart >= 0 ) { TerminateRun(i); } } // after the main loop, before printing the result if (samples[0].Dist >= THRESHOLD && samples[NUM_SAMPLES - 1].Dist >= THRESHOLD) { // find how far the opening extends from each end int endRun = NUM_SAMPLES - 1; while (endRun > 0 && samples[endRun].Dist >= THRESHOLD) endRun--; endRun++; // first index of the trailing far run int startRun = 0; while (startRun < NUM_SAMPLES - 1 && samples[startRun].Dist >= THRESHOLD) startRun++; startRun--; // last index of the leading far run // angular width across the fold float width = samples[startRun].Hdg - samples[endRun].Hdg; if (width > 180) width -= 360; if (width < -180) width += 360; width = fabs(width); if (width > bestWidth) { bestWidth = width; bestStart = endRun; bestEnd = startRun; numruns--; // the two fragments are now one physical run } } cout << "numruns = " << numruns << ", bestwidth = " << bestWidth << ", bestStart = " << bestStart << ", bestEnd = " << bestEnd; return; } template <class T> void mySwap(T& rightval, T& leftval) { T tempright = rightval; rightval = leftval; leftval = tempright; } int partition(bool bFwdSort, float Hdg[], float FrontD[], int start, int end)//10/11/23 added 'bFwdSort' to sig to switch between fwd & rev sort { //cout << "In partition(" << "start = " << start << ", end = " << end << ")\n"; int pivot = FrontD[start]; //cout << "In partition: pivot = FrontD[" << start << "] = " << FrontD[start] << "\n"; int count = 0; for (int i = start + 1; i <= end; i++) { if (bFwdSort) { //cout << "\t" << FrontD[i] << "\n"; if (FrontD[i] <= pivot) { count++; //cout << "In partition: FrontD[" << i << "] = " << FrontD[i] << " <= " << pivot << ", count = " << count << "\n"; } } else //10/11/23 reverse sort { //cout << "\t" << FrontD[i] << "\n"; if (FrontD[i] > pivot)//10/09/23 reverse sort { count++; //cout << "In partition: FrontD[" << i << "] = " << FrontD[i] << " <= " << pivot << ", count = " << count << "\n"; } } } // Giving pivot element its correct position int pivotIndex = start + count; //cout << "In partition just before swap: pivotIndex = " << pivotIndex << "-->> " << FrontD[pivotIndex] << "\n"; //cout << "FrontD contents before swap(" << FrontD[pivotIndex] << ", " << FrontD[start] << ")\n"; mySwap(FrontD[pivotIndex], FrontD[start]); // Sorting left and right parts of the pivot element int i = start, j = end; while (i < pivotIndex && j > pivotIndex) { if (bFwdSort) { while (FrontD[i] <= pivot) { i++; } while (FrontD[j] > pivot) { j--; } } else { while (FrontD[i] > pivot) //reverse sort { i++; } while (FrontD[j] <= pivot)//reverse sort { j--; } } if (i < pivotIndex && j > pivotIndex) { //10/06/23 can't use '++' or '--' here because then second mySwap has wrong indices mySwap(FrontD[i++], FrontD[j--]); } } return pivotIndex; } void quickSort(bool bFwdSort, float Hdg[], float FrontD[], int start, int end) { //cout << "In quickSort(" << "start = " << start << ", end = " << end << ")\n"; // base case if (start >= end) return; // partitioning the FrontD array int p = partition(bFwdSort, Hdg, FrontD, start, end); // Sorting the left part quickSort(bFwdSort, Hdg, FrontD, start, p - 1); // Sorting the right part quickSort(bFwdSort, Hdg, FrontD, p + 1, end); } int main() { GetBestDistance();//added 10/17/26 to isolate new code cout << "FrontD contents before sorts\n"; for (int i = 0; i < n; i++) { cout << FrontD[i] << " "; } cout << "\n\n"; cout << "Hdg contents before sort\n"; for (int i = 0; i < n; i++) { cout << Hdg[i] << " "; } cout << "\n\n"; ////quickSort(true, FrontD, 0, n - 1); //quickSort(true, Hdg, FrontD, 0, n - 1); //cout << "FrontD contents after FORWARD sort\n"; //for (int i = 0; i < n; i++) { // cout << FrontD[i] << " "; //} //cout << "\n\n"; //cout << "Hdg contents after FORWARD sort\n"; //for (int i = 0; i < n; i++) { // cout << Hdg[i] << " "; //} //cout << "\n\n"; //quickSort(false, Hdg, FrontD, 0, n - 1);//REV sort //cout << "FrontD contents after REVERSE sort\n"; //for (int i = 0; i < n; i++) { // cout << FrontD[i] << " "; //} //cout << "\n\n"; //cout << "Hdg contents after sort\n"; //for (int i = 0; i < n; i++) { // cout << Hdg[i] << " "; //} //cout << "\n"; return 0; } |
Almost half of the actual code above is dedicated to QuickSort-related code, and as I have now found out – none of it is actually necessary – elegant, pretty – and useless. Once I took all that out, the RobotBestDistV2.cpp test program shrank down to something much smaller, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
// QuickSortV3.cpp : This file contains the 'main' function. Program execution begins and ends there. // /*Quicksort Test program The idea is to determine whether or not I can sort multiple associated arrays by sorting a 'master' array (say front distances) and having other arrays be sorted using the same sort index sequence as the master array. Master (front distance) FrontD: int FrontD[] = { 219, 30, 114, 152, 41, 86 }; Slave (Heading) array: float Hdg[] = { 19, 3, 11, 15, 4, 8 }; */ #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; //int FrontD[] = { 9, 3, 4, 2, 1, 8 }; ////real measurement data //float FrontD[] = { 121.6, 124.1, 146.4, 114.2, 67.6, 59.9, 48.4, 44.2, 35.3, 37.9, 40.1, 42.8, 46.3, 52.4, 59.5, 93.0, 91.3, 85.5, 101.9, 229.0, 260.7, 312.9, 388.1, 414.6, 347.8, 58.1, 51.5, 54.8, 59.5, 75.7, 98.1, 38.3, 121.5, 114.5, 130.8, 138.5 }; //float Hdg[] = { 0.0, 9.4, 18.7, 28.5, 39.1, 49.2, 59.1, 69.2, 78.8, 88.4, 99.0, 109.0, 118.5, 128.3, 138.1, 154.9, 164.7, 174.2, -176.3, -160.3, -149.0, -138.9, -129.9, -118.0, -106.8, -96.8, -87.3, -77.5, -67.9, -58.1, -47.7, -37.4, -28.0, -18.2, -8.7, 1.1 }; std::vector<float> FrontD; std::vector<float> Hdg; //new plan; convert the arrays from the tabular form as they come off the generating program into the comma-delimited arrays as shown above //const char* rawScan = R"( //MSec FrontD Hdg //8717 106.0 0.0 //8979 427.9 6.1 //9179 79.4 10.2 //9443 57.6 16.4 //9707 58.0 22.6 //9914 61.6 26.5 //10177 62.5 33.0 //10441 67.7 39.2 //10650 77.5 43.6 //10913 85.0 49.9 //11177 138.8 56.4 //11321 123.8 60.2 //11585 134.4 66.7 //11847 134.8 72.4 //12056 133.9 76.4 //12319 128.8 82.2 //12528 124.3 86.5 //12791 143.5 92.5 //12991 148.4 96.4 //13255 148.8 102.3 //13399 145.0 106.5 //13661 118.6 112.8 //13870 77.6 116.6 //14133 70.7 122.5 //14277 62.9 126.7 //14541 60.7 133.0 //14685 52.5 136.9 //14949 48.2 143.4 //15213 44.5 149.5 //15595 37.3 160.1 //15859 35.5 165.8 //16123 36.3 171.4 //16332 38.9 175.2 //16596 38.8 -179.4 //16859 38.6 -173.8 //17123 40.2 -167.7 //17330 42.8 -163.7 //17593 43.2 -157.8 //17857 46.0 -152.1 //18121 49.9 -146.2 //18385 55.4 -140.1 //18585 61.1 -136.2 //18849 68.1 -129.9 //19113 78.3 -124.0 //19320 90.2 -119.2 //19583 89.3 -113.4 //19792 86.0 -109.1 //20055 84.2 -102.9 //20255 84.2 -98.8 //20519 89.2 -93.2 //20783 205.2 -87.2 //20927 225.2 -83.2 //21189 224.6 -77.8 //21453 237.6 -71.5 //21542 251.6 -67.2 //21805 276.1 -61.3 //21949 284.7 -57.2 //22213 321.4 -50.9 //22413 362.0 -47.0 //22677 396.5 -40.7 //22877 395.9 -36.7 //23139 395.1 -30.7 //23403 353.2 -25.2 //23612 262.6 -21.1 //23875 395.7 -15.0 //24139 50.5 -8.9 //24403 46.9 -2.8 //24667 47.5 3.2 //24874 50.3 7.2 //25137 52.1 13.3 //25401 57.3 19.2 //25610 62.9 23.2 // //)"; const char* rawScan = R"( MSec FrontD Hdg 8632 408.9 0.0 8894 433.5 - 7.1 9038 367.9 - 11.0 9238 381.3 - 14.7 9438 386.8 - 18.5 9702 366.9 - 24.7 9964 340.4 - 30.6 10173 304.5 - 34.8 10436 286.4 - 40.8 10645 266.9 - 44.9 10908 262.1 - 51.3 11172 233.7 - 57.6 11372 225.6 - 61.7 11636 163.5 - 68.6 11843 87.8 - 72.9 12106 85.9 - 79.1 12315 86.2 - 83.3 12578 89.7 - 89.9 12722 91.3 - 94.4 12922 91.2 - 98.1 13122 83.4 - 102.5 13322 70.8 - 106.3 13586 63.7 - 112.9 13673 60.1 - 117.0 13881 51.5 - 121.0 14024 48.0 - 125.1 14233 47.6 - 129.1 14376 42.9 - 132.8 14576 42.1 - 137.0 14776 38.5 - 141.0 15040 37.1 - 147.9 15240 35.3 - 151.7 15504 34.7 - 158.7 15648 29.8 - 162.9 15855 29.9 - 166.6 15943 29.9 - 170.9 16151 33.8 - 174.7 16414 35.2 180.0 16678 36.9 174.2 16942 38.2 168.1 17142 41.7 163.9 17406 42.9 157.2 17733 50.1 150.7 17876 54.3 146.6 18140 58.9 140.1 18404 73.2 133.9 18604 115.9 129.9 18868 135.9 123.7 19068 140.8 119.5 19332 139.3 113.3 19539 129.8 108.9 19802 111.7 102.3 19891 114.4 98.2 20099 113.3 93.8 20178 126.3 89.8 20387 142.0 85.6 20530 126.3 81.3 20730 31.2 77.3 20874 27.9 72.8 21194 81.5 62.8 21401 76.0 58.6 21544 70.9 54.5 21808 67.4 47.3 21952 61.0 43.1 22161 60.6 39.2 22304 60.0 34.8 22504 57.9 31.0 22704 61.2 26.7 22904 68.7 23.0 23168 417.9 16.9 23375 410.2 12.7 23638 341.2 6.7 )"; //int n = 36; //#define NUM_SAMPLES 36 int n = 72; #define NUM_SAMPLES 72 int THRESHOLD = 200; struct RunItem { float Hdg = 0; float Dist = 0; }; int bestStart = -1, bestEnd = -1; float bestWidth = 0; int numruns = 0; int runStart = -1; RunItem samples[NUM_SAMPLES]; // or 72 void ParseScan() { std::istringstream iss(rawScan); std::string line; while (std::getline(iss, line)) { if (line.empty() || line.find("FrontD") != std::string::npos) continue; // normalize whitespace and the spaced minus that paste sometimes inserts for (char& c : line) if (c == '\t') c = ' '; size_t pos; while ((pos = line.find(" - ")) != std::string::npos) line.replace(pos, 3, "-"); while ((pos = line.find(" -")) != std::string::npos) line.replace(pos, 2, "-"); std::istringstream row(line); float msec, dist, hdg; if (row >> msec >> dist >> hdg) { FrontD.push_back(dist); Hdg.push_back(hdg); } } cout << "FrontD has " << FrontD.size() << " elements\n"; } void TerminateRun(int i) { // run ended at i-1 float width = samples[i - 1].Hdg - samples[runStart].Hdg; //watch out for 'fold' width results > 180 or < -180 if (width > 180) { width -= 360; } else if (width < -180) { width += 360; } width = abs(width); if (width > bestWidth) { bestWidth = width; bestStart = runStart; bestEnd = i - 1; } runStart = -1; numruns++; } // fill samples[] during the scan (or copy from your existing arrays) void GetBestDistance() { //Purpose: Determine 'Best Distance' from a set of NUM_SAMPLES Hdg/Dist pairs //Inputs: // Fixed set of real samples in FrontD[NUMSAMPLES] and Hdg[NUMSAMPLES] synchronized arrays //Outputs: // bestWidth = width (in deg) of the largest contiguous run of FrontD measurements // bestStart, bestEnd = start & end idices for the 'bestWidth' run //Plan: // Step1: fill samples[] during the scan (or copy from your existing arrays) // Step2: work through entire Hdg/Dist sample items looking for contiguous runs (can be just one item) // for each such run, calc bestWidth, bestStart, bestEnd. If 'bestWidth' is greater than all // previous runs detected, replace the old bestWidth, bestStart, bestEnd values // Step3: If there are multiple 'far' runs, check to if two of them can be stitched // together around the +/- 0 degree fold. //Step1: fill samples[] during the scan (or copy from your existing arrays) ParseScan(); //Step2: work through entire Hdg/Dist sample items looking for contiguous runs (can be just one item) // for each such run, calc bestWidth, bestStart, bestEnd. If 'bestWidth' is greater than all // previous runs detected, replace the old bestWidth, bestStart, bestEnd values for (int i = 0; i < NUM_SAMPLES; i++) { samples[i].Dist = FrontD[i]; samples[i].Hdg = Hdg[i]; //print out for debug cout << "samples[" << i << "] = (" << samples[i].Dist << ", " << samples[i].Hdg << ")\n"; } for (int i = 0; i < NUM_SAMPLES; i++) { if (samples[i].Dist >= THRESHOLD) { if (runStart < 0) { runStart = i; } // don’t forget to close a run that reaches the end of the array //terminate this run if we run out of samples if (i >= NUM_SAMPLES-1) { TerminateRun(i); } // start a new run // else just keep extending } //this doesn't execute unless samples[i] < THRESHOLD else if (runStart >= 0 ) { TerminateRun(i); } } //Step3: If there are multiple 'far' runs, check to if two of them can be stitched // together around the +/- 0 degree fold. if (samples[0].Dist >= THRESHOLD && samples[NUM_SAMPLES - 1].Dist >= THRESHOLD) { // find how far the opening extends from each end int endRun = NUM_SAMPLES - 1; while (endRun > 0 && samples[endRun].Dist >= THRESHOLD) endRun--; endRun++; // first index of the trailing far run int startRun = 0; while (startRun < NUM_SAMPLES - 1 && samples[startRun].Dist >= THRESHOLD) startRun++; startRun--; // last index of the leading far run // angular width across the fold float width = samples[startRun].Hdg - samples[endRun].Hdg; if (width > 180) width -= 360; if (width < -180) width += 360; width = fabs(width); if (width > bestWidth) { bestWidth = width; bestStart = endRun; bestEnd = startRun; numruns--; // the two fragments are now one physical run } } cout << "numruns = " << numruns << ", bestwidth = " << bestWidth << ", bestStart = " << bestStart << ", bestEnd = " << bestEnd; return; } int main() { GetBestDistance();//added 10/17/26 to isolate new code return 0; } |
The above is about 360 lines long, and well over half of it is copy/pasted test data from the robot scans.
After figuring that out, I used the robot code to actually do the 360 scan to collect the heading/distance data and then copy/pasted the data in text form into my RobotBestDistV2.cpp test program. This allowed me to iterate pretty quickly through different techniques while keeping the two programs isolated from each other.
The first set of data (commented out in the above code) was from a 360 scan in 10 deg steps for a total of 36 steps. This was nice, but I wanted to see what would happen with 5 deg steps – and that is the second set of data. I also wanted to verify if this test program would handle the (highly improbable, but not impossible) case where the best ‘far’ lobe spanned the +/- 0 deg split (it does – yay!). Here’s the printout from the last run:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
FrontD has 72 elements samples[0] = (408.9, 0) samples[1] = (433.5, -7.1) samples[2] = (367.9, -11) samples[3] = (381.3, -14.7) samples[4] = (386.8, -18.5) samples[5] = (366.9, -24.7) samples[6] = (340.4, -30.6) samples[7] = (304.5, -34.8) samples[8] = (286.4, -40.8) samples[9] = (266.9, -44.9) samples[10] = (262.1, -51.3) samples[11] = (233.7, -57.6) samples[12] = (225.6, -61.7) samples[13] = (163.5, -68.6) samples[14] = (87.8, -72.9) samples[15] = (85.9, -79.1) samples[16] = (86.2, -83.3) samples[17] = (89.7, -89.9) samples[18] = (91.3, -94.4) samples[19] = (91.2, -98.1) samples[20] = (83.4, -102.5) samples[21] = (70.8, -106.3) samples[22] = (63.7, -112.9) samples[23] = (60.1, -117) samples[24] = (51.5, -121) samples[25] = (48, -125.1) samples[26] = (47.6, -129.1) samples[27] = (42.9, -132.8) samples[28] = (42.1, -137) samples[29] = (38.5, -141) samples[30] = (37.1, -147.9) samples[31] = (35.3, -151.7) samples[32] = (34.7, -158.7) samples[33] = (29.8, -162.9) samples[34] = (29.9, -166.6) samples[35] = (29.9, -170.9) samples[36] = (33.8, -174.7) samples[37] = (35.2, 180) samples[38] = (36.9, 174.2) samples[39] = (38.2, 168.1) samples[40] = (41.7, 163.9) samples[41] = (42.9, 157.2) samples[42] = (50.1, 150.7) samples[43] = (54.3, 146.6) samples[44] = (58.9, 140.1) samples[45] = (73.2, 133.9) samples[46] = (115.9, 129.9) samples[47] = (135.9, 123.7) samples[48] = (140.8, 119.5) samples[49] = (139.3, 113.3) samples[50] = (129.8, 108.9) samples[51] = (111.7, 102.3) samples[52] = (114.4, 98.2) samples[53] = (113.3, 93.8) samples[54] = (126.3, 89.8) samples[55] = (142, 85.6) samples[56] = (126.3, 81.3) samples[57] = (31.2, 77.3) samples[58] = (27.9, 72.8) samples[59] = (81.5, 62.8) samples[60] = (76, 58.6) samples[61] = (70.9, 54.5) samples[62] = (67.4, 47.3) samples[63] = (61, 43.1) samples[64] = (60.6, 39.2) samples[65] = (60, 34.8) samples[66] = (57.9, 31) samples[67] = (61.2, 26.7) samples[68] = (68.7, 23) samples[69] = (417.9, 16.9) samples[70] = (410.2, 12.7) samples[71] = (341.2, 6.7) numruns = 1, bestwidth = 78.6, bestStart = 69, bestEnd = 12 |
Looking at the raw data, you can see that one section of the ‘far’ lobe goes from ‘samples[0]’ through ‘samples[12]’ and the other section goes from ‘samples[69]’ through ‘samples[71]’, for a combined ‘far’ lob of about 78.6 deg. Cool!