I'm using a compass for a heading to steer to. I saw, months ago, how to code it using >=180 or <=180. For the life of me I can't find it again. Should have but didn't book mark it!!!!!!
A sample code and brief description would be appreciated.
I'm using a compass for a heading to steer to. I saw, months ago, how to code it using >=180 or <=180. For the life of me I can't find it again. Should have but didn't book mark it!!!!!!
A sample code and brief description would be appreciated.
Show us the code you are having difficulty with.
Here's the code - at least a start.
// Calabration program is in LSM303 examples
#include <Wire.h>
#include <LSM303.h>
LSM303 compass;
//===========================Array variables=================================
const int numReadings = 40; // number of readings for the smoothing.
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float heading_ave = 0; // the heading_ave
//=============================================================================
float heading_target = 360 // this will be read in by button push
float heading_error = 0 // make sure this is 0 at startup
int error_tolerance = 2 // target to start steering
void setup() {
Serial.begin(9600);
Wire.begin();
compass.init();
compass.enableDefault();
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
// Calibration values X Y Z
compass.m_min = (LSM303::vector<int16_t>) {
-405, -616, -745
};
compass.m_max = (LSM303::vector<int16_t>) {
+562, +458, +97
};
}
void loop() {
compass.read();
float heading = compass.heading();
//================================ Array - Smooth readings ======================
total = total - readings[readIndex]; // subtract the last reading:
readings[readIndex] = heading; // read from the sensor:
total = total + readings[readIndex]; // add the reading to the total:
readIndex = readIndex + 1; // advance to the next position in the array:
if (readIndex >= numReadings) { // if we're at the end of the array...
readIndex = 0; // ...wrap around to the beginning:
}
heading_ave = total / numReadings; // calculate the heading_ave:
//================================== end of Array ===============================
//=============================== steering calculations =========================
heading_target - heading_ave = heading_error
if {
heading_error > error_tolerance //turn right
}
if {
heading_error > error_tolerance //turn left
}
//==================================== end of steering ================================
Serial.print(heading_target );
Serial.print(heading_ave );
Serial.println(heading_error );
delay(100);
}
I think I found what I was looking for
// routine to calculate heading error in degrees, taking into account compass wrap
int heading_error(int bearing, int current_heading)
{
int error = current_heading - bearing;
if (error > 180) error -= 360;
if (error < -180) error += 360;
return error;
}
it can be found at this post
https://forum.arduino.cc/index.php?topic=361165.0
I've got my grand kids coming in a few minutes - they will leave tomorrow so will try to work on this after that. Be back with the results later.
Thanks
Oh. I apologize for removing my post (#2). (Which I have since restored.) It had what you needed. I assumed you were finished searching when you posted #3 and were not looking for how to wrap the value.
Ok the grand kids left. The 8 yr old and I put together a R2D2 robot kit. It took longer to download the app and instructions than to put it together.
After he got to playing with it and his 5 yr old sister got to making her high pitched noises, it was all to much noise for grandpas old ears!!!
Coding Badly, thanks for putting that back up! I used the info on the 2nd link, it's simple and works great.
heading_error = (heading_target - heading_ave);
Next I'll add in the stepper motor stuff. That is what I'll be driving.
Can you folks help with that or should I start a new post in another section?
Just in case you want to see here is the code.
// Calabration program is in LSM303 examples
#include <Wire.h>
#include <LSM303.h>
LSM303 compass;
//===========================Array variables=================================
const int numReadings = 10; // number of readings for the smoothing.
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float heading_ave = 0; // the heading_ave
//=============================================================================
float heading_target = 340; // this will be read in by button push
float heading_error = 0; // make sure this is 0 at startup
int error_tolerance_plus = 2; // target to start steering
int error_tolerance_minus = 2; // target to start steering
void setup() {
Serial.begin(9600);
Wire.begin();
compass.init();
compass.enableDefault();
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
// Calibration values X Y Z
compass.m_min = (LSM303::vector<int16_t>) {
-405, -616, -745
};
compass.m_max = (LSM303::vector<int16_t>) {
+562, +458, +97
};
}
void loop() {
compass.read();
float heading = compass.heading();
//================================ Array - Smooth readings ======================
total = total - readings[readIndex]; // subtract the last reading:
readings[readIndex] = heading; // read from the sensor:
total = total + readings[readIndex]; // add the reading to the total:
readIndex = readIndex + 1; // advance to the next position in the array:
if (readIndex >= numReadings) { // if we're at the end of the array...
readIndex = 0; // ...wrap around to the beginning:
}
heading_ave = total / numReadings; // calculate the heading_ave:
//================================== end of Array ===============================
//=============================== steering calculaitons =========================
heading_error = (heading_target - heading_ave); // returns value for correction
if (heading_error >= error_tolerance_plus)
{
//Turn motor ++++
}
if (heading_error <= error_tolerance_minus)
{
//Turn motor ----
}
//==================================== end of steering ================================
/*Serial.print(heading_target );
Serial.print("\t");
Serial.print(heading_ave );
Serial.print("\t");
Serial.println(heading_error );
*/
delay(1000);
}
RayJorgensen:
Next I'll add in the stepper motor stuff. That is what I'll be driving. Can you folks help with that or should I start a new post in another section?
New thread is a reasonable choice.