buffer

Hi
As English is not my native language i don't know the right word , so i will describe what i want to do..

i have a code that takes readings from a HMC5883L magnetometer that i am using as a compass.
with a button i can select the Heading that exact time and then the code drives a motor forward or reverse to correct the route to match the stored Heading if it starts moving away from the desire route .

what i want to do is to take some readings (like a buffer) that the arduino will check and then if they are still out of the headings range then will send the command to move the motor

( i could be use delay but that would not be so wise i think )

does that have any special name to search it ? (or find any similar example to study)

thanks in advance

Maybe you are thinking about an array.

Use an array to store readings.

I don't understand your description of the project. One compass reading (the current heading) is enough to determine whether the vehicle is traveling in the desired direction (the bearing).

thanks for the help i will check the array then

the project is to make a small autopilot for my small sailing boat.. so i want to use something like buffer readings in order to avoid motor try to correct the bearing all the time..

At sea traveling is not so smooth so the compass will always give flux readings as the boat will bounce at waves

So, average several readings over a period of time. No need for an array.

With compass headings, you have to be very careful about averaging, because you don't want to average headings that cross the 0/360 boundary (for example, averaging 359 and 1 gives you 180 degrees, instead of 0 degrees). Look up "circular mean" for one solution.

thanks again

the truth is that i had noticed that problem all ready as i was testing my code and the plan was to search for solution or post a question about it later... when i would had finish with other small issues i was having (like the one i post it now )

so thanks for the info about my next major problem :slight_smile:

i will search about ''circular mean''

Hmm that 'circular mean' is true pain to figure out how to make it work...

maybe one option i thought is that as i my project plan is to set the course of my boat manualy first and then store the bearing to the arduino in order to make the autopilot and as i dont rely to the truth magnetic north for my compass, i could tweak the library or the code in order when i start up my arduino setups the magnetometer like it is pointing to south .

so no need to worry about the 'circular mean''

it is just one thought ... dont be sure if it will work or is if it can be done..

that 'circular mean' is true pain to figure out how to make it work

I can't imagine why. Explain.

Because i saw in some examples have to use lot of trigonometry formulas (that something i have to use from high school and forget most of them :stuck_out_tongue: )

as my code takes the ''sett point - desire bearing'' and subtracted it from the ''current bearing'' to result a number ( -/+) in order to drive the motor, was more easy for me to make some new if statements to handle this problem

the code for drive the motor is

if (autopilotOnOff == LOW) {
    Setpoint1 = map(corectionway, -45, 45, -255, 255);
  }
  
  Input1 = map(corectionway, -20, 20, -255, 255);
  
  PID1.Compute();
  Serial.print("\t");
  Serial.print("Output1 = ");
  Serial.println(Output1);

  if (Output1 > 0) {
    analogWrite(5, Output1);
    analogWrite(6, 0);
  }
  else if (Output1 < 0) {
    Output1a = abs(Output1);
    analogWrite(5, 0);
    analogWrite(6, Output1a);
  }

and my solution for 360 - 0 bearing is this one

  long headingDegrees = heading * 180 / M_PI;
  long NewheadingDegrees = heading * 180 / M_PI;

  if (settingtheway == LOW && autopilotOnOff == LOW) {
    myway = headingDegrees;
  }




  if ((myway > 350) && (myway <= 360)) {
    if (headingDegrees >= 0  && headingDegrees <= 20) {

      NewheadingDegrees = (headingDegrees + 360) ;
    }
  }


  if ((myway >= 0) && (myway <= 20)) {
    if (headingDegrees >= 350  && headingDegrees <= 360) {

      NewheadingDegrees = myway + (360 - headingDegrees);
    }
  }

  if (myway > 20 && myway <= 350 ) {

    NewheadingDegrees = headingDegrees;
  }


  if (autopilotOnOff == LOW && buttonsetup == true) {

    if ( myway < headingDegrees - 2 || myway > headingDegrees + 2) {

      if ((myway <= 20) && (headingDegrees >= 350)) {


        corectionway = myway + (360 - headingDegrees);
      }


      else if ((myway <= 20) && (headingDegrees >= 0)) {


        corectionway = myway - NewheadingDegrees;
      }

      else  if (myway > 21) {
        corectionway = (myway - NewheadingDegrees);
      }
    }
    else if (myway == headingDegrees) {
      corectionway = myway - NewheadingDegrees;
    }
  }

Maybe is not an orthodox way but worked for me... until now i have tested in all the ways and gaved me the results i wanted

If high school math is that terrifying, navigation will certainly be a challenge.

No so much terrifying but last time i used them where back to 1996 so long time ago..

maybe in the future that would like to make my program work more efficient had to revise them.

For sure not planning to travel around the globe , so navigation is little simpler in my situation.

Greece (where i live) is very easy sea to navigate even without a compass.. because has so many islands most of the times you travel always by sight (of course you have to have knowledge about the islands near by you )

buck to the topic ... thanks again for the guidance .. helped me a lot to find what i need to do to my program