The mowbot looks pretty interesting -- I like the variety of sensors you incorporated.
Here is some basic code you can use to test the course correcting ability of the LSM303. The code will print to screen "turn right" or "turn left." After a set amount of time, the code will print "reverse" and set you on a new heading.
#include <Wire.h>
#include <LSM303.h>
int target = 350;
int target2 = 180;
unsigned long timer = 60000;
boolean reverse = false;
LSM303 compass;
void setup() {
Serial.begin(9600);
Wire.begin();
compass.init();
compass.enableDefault();
/*
Calibration values; the default values of +/-32767 for each axis
lead to an assumed magnetometer bias of 0. Use the Calibrate example
program to determine appropriate values for your particular unit.
*/
compass.m_min = (LSM303::vector<int16_t>){-485, -508, -291};
compass.m_max = (LSM303::vector<int16_t>){+476, +456, +588};
}
void loop() {
compass.read();
/*
When given no arguments, the heading() function returns the angular
difference in the horizontal plane between a default vector and
north, in degrees.
The default vector is chosen by the library to point along the
surface of the PCB, in the direction of the top of the text on the
silkscreen. This is the +X axis on the Pololu LSM303D carrier and
the -Y axis on the Pololu LSM303DLHC, LSM303DLM, and LSM303DLH
carriers.
To use a different vector as a reference, use the version of heading()
that takes a vector argument; for example, use
compass.heading((LSM303::vector<int>){0, 0, 1});
to use the +Z axis as a reference.
*/
int heading = compass.heading();
float deviation = abs(target - heading);
if (deviation > 1){
if (deviation >= 180){
if (heading < target){
Serial.println("turn left");}
else{
Serial.println("turn right");}
}
else {
if (heading < target){
Serial.println("turn right");}
else{Serial.println("turn left");}
}}
else{Serial.println("On Course");}
Serial.println(heading);
if (reverse == false) {
if (millis() >= timer){
target = target2;
reverse = true;
Serial.println("Reverse");}}
delay(100);
}
You can change the variables target, target2 and timer, the delay interval and also deviation in the line of code:
if (deviation > 1){
You can further modify the code so that instead of (or in addition to) to printing commands, the commands are sent to the motors. For smoother operation, you can average the deviation over several loops before course-correcting.