Vision-enhanced Robot ‘Find Best Direction’ Issue

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:

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:

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:

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:

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!

Leave a Reply

Your email address will not be published. Required fields are marked *