tammytam:
This is not ideal in the least, but its quick and dirty. Also if your magno has a brain fart half way across the zero crossing, it'll cause issues. By that I mean if it reads bearing 0, followed by 259, followed by 0 again while turning left, it'll behave incorrectly (might, probably is, bugs in this):
int g_desired_bearing = 0;
bool g_left = false;
bool g_zero_crossing = false;
[code]void loop()
{
int current_bearing = get_magno_bearing();
if( g_left )
{
if( g_zero_crossing && current_bearing < g_desired_bearing )
{
turn_left();
}
else
{
g_zero_crossing = false;
if( current_bearing > g_desired_bearing )
{
turn_left();
}
}
}
else
{
// do similar thing for right hand side ...
}
}
void set_bearing( int bearing, bool left )
{
g_desired_bearing = bearing;
g_left = left;
g_zero_crossing = false;
int current_bearing = get_magno_bearing();
if( g_left )
{
if( g_desired_bearing > current_bearing )
g_zero_crossing = true;
}
else
{
if( g_desired_bearing < current_bearing )
g_zero_crossing = true;
}
}
[/code]
Hi Tam, thanks for the reply. However i have little to no idea to the code that you've provided me. I'm sorry that i have minimal knowledge about HMC5883L, thus i'm using someone else's library. All i did was extract the output give : "RoundDegreeInt" to which ever variable i need. I then let my robot turn to a certain degree until (RoundDegreeInt = Degree1) where Degree1 can be the previous saved angle.
A sample of the code from void_lopp() can be seen below:
MagnetometerRaw raw = compass.ReadRawAxis();
// Retrived the scaled values from the compass (scaled to the configured scale).
MagnetometerScaled scaled = compass.ReadScaledAxis();
// Values are accessed like so:
int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)
int MilliGauss_OnThe_YAxis = scaled.YAxis;
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(scaled.YAxis, scaled.XAxis);
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: 2� 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
float declinationAngle = -0.0040724291 ;
heading += declinationAngle;
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Check for wrap due to addition of declination.
if(heading > 2*PI)
heading -= 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;
//correcting the angle issue
if (headingDegrees >= 1 && headingDegrees < 240)
{
headingDegrees = map(headingDegrees,0,239,0,179);
}
else if (headingDegrees >= 240)
{
headingDegrees = map(headingDegrees,240,360,180,360);
}
//rounding the angle
RoundDegreeInt =round(headingDegrees);
//smoothing value
if( RoundDegreeInt < (PreviousDegree + 3) && RoundDegreeInt > (PreviousDegree - 3) ) {
RoundDegreeInt = PreviousDegree;
}
PreviousDegree = RoundDegreeInt;