The magnetometer in the Pololu 3Pi+

i’am using the Pololu 3Pi+ 32U4 for my project to drive it with a WiFi module. However, i’m using the Magnetometer as a Compass and i’am following the Pololu turnWithCompass exemple.
it works fine but can anyone explaine to me the next code lines in the Pololu exemple:

float x_scaled = 2.0*(float)(v.x - m_min.x) / (m_max.x - m_min.x) - 1.0;
float y_scaled = 2.0*(float)(v.y - m_min.y) / (m_max.y - m_min.y) - 1.0;

where from comes this Formula.
Thank you in advance


This part of the program is just taking the averaged measurements from the magnetometer in the x and y directions (v.x and v.y) and scaling (normalizing) them per the minimum and maximum values from the calibration process (m_min.x, m_min.y, m_max.x, and m_max.y) so that they end up in the range of -1 to 1. You can read more about the formula used for this here:

- Patrick

thank you for the response, it was very helpful.

1 Like

if i may ask, why the 2.0 and the -1.0. For what they are used here.
Best Regards.

Those operations are to have the effect of rescaling the result to a range between -1 and +1. Without them, the result would end up between 0 and +1.

- Patrick

thank you