There is a really easy way to determine good focus. I have suggested this in several other posts, but I will repeat it here since you have mentioned it.
You are already calculating the least square fit best lines for both the left and right sides of the, hopefully, V curve. Just sum the deviations of each data point from the value on the best fit line. You would want to ignore the lowest values in the center of the V that are not on either line. Suitably normalized, that is an excellent measure of the quality of the focus.
If you allowed the user to specify a minimum acceptable value, and allowed some corrective action to then be taken if the focus fails, you will have accomplished a MAJOR IMPROVEMENT in the focus routine. The current behavior of the program to move my focus from a perfectly good position due to a disastrously bad focus run resulting from clouds, has caused me to lose large portions of many nights imaging.