While calculating a very simple linear equation is all nice and good, I prefer to use an interpolation curve with 2 points.
Basically, you cal two pairs of numbers (an analog value and corresponding engineering unit value for each pair) and calculate M and B dynamically. I find it's easier to calibrate.
the code can then does the following (for the 2-point curve):
Code:
dx = max_ana - min_ana;
dy = max_eng - min_eng;
m = dx / dy;ana_raw = get_analog_value();
ana_offset = ana_raw - min_ana;
eng_offset = ana_offset * m;
eng = eng_offset + min_eng;
eng is the result.
Under the same basic logic, you can develop blocks to linearly interpolate a 1d input and 1d output table to generate a piecewise function which is calibrated like a pair of tables (input and output), or two 1d input tables and a 2d output table to end up with a 3d surface whichis calibrated with two axis breakpoints and the values for each cell.
Such interpolation curves and tables seem to be a staple of embedded control algorithms, since they are very easy to implement and calibrate (relative to fully modeling the system), and get 'close enough'. FRC software is usually in the 'close enough' realm anyway, so they are perfect.