I'm working on an magnetometer & servo sailboat auto-pilot.
The basics are very simple: the arduino Uno subtracts the desired course (in degrees) from the boat's heading (also in degrees), and the resulting number used for a servo that steers the boat. Thus the angle of the rudder will be directly impacted by the angle of the course error. Here's the catch: If the set course is 0 degrees and the boat's heading is 359 (just one degree error), the boat will steer hard to the opposite direction and do a full circle in order to get back to 0.
I'm quite new to coding, and although I generally get along well with Google and reading forums, I just can't think of a solution. My autopilot works flawlessly except if I get too near to 360 or 0 degrees (North). Any tips?
Here is the critical part of the code:
int Course = map(analogRead(potCourse), 0, 1023, 0, 360); //Convert bit to degrees
float heading = atan2(event.magnetic.y, event.magnetic.x); //read magnetometer
float HeadingDeg = heading * 180/M_PI; //convert radians to degrees
int ErrorAngle = (HeadingDeg - Course); //calculate difference between heading and course
ErrorAngle = constrain(trimtabOriginal, -90, 90); //maximum rudder movement
ServoAngle = map(trimtabAngle, -90, 90, 0, 180); //convert to servo values
steeringServo.write(ServoAngle);
int Course = map(analogRead(potCourse), 0, 1023, 0, 360); //Convert bit to degrees
float heading = atan2(event.magnetic.y, event.magnetic.x); //read magnetometer
float HeadingDeg = heading * 180/M_PI; //convert radians to degrees
//
float deltaAngle = abs(HeadingDeg - Course);
if ( deltaAngle > 180)
{
// direction = counterclockwise;
servoAngle = 90 + (360 - deltaAngle) / 2;
}
else
{
// direction = clockwise;
servoAngle = 90 - deltaAngle / 2;
}
maybe I swapped the formulas, but you should get the idea.
(assumption 90 == midposition servo)
Probably you have better formulas for the servoAngle ,
In the above way they adjust more or less in a linear way.
isn't the issue adjusting the math error for large angle to be relative
aren't there just 4 cases. "course" is the desired direction, "heading" is the actual direction of the boat. "delta" is the mathematical difference, "correction" is the relative difference
I think so, it's what I did here, except maybe it should be course-heading, but I was just 'correcting' the original code, so I kept the subtraction the same:
These single input systems will fail when you need it the most.
Boat may point the right direction but the waves, wind and tide have other ideas on where it ends up.
A large (45') yacht ended up on the rocks in a beach near here some 10 years ago from using single ended steering guidance.
Had to be cut into pieces to remove it after a month or so.
Suggest you look at 3 or more satellite input systems.
Thank you for your comment. It's an experimental system, based on the most reliable self-steering: a windvane. I basically copy the "trim tab windvane" design (the simplest windvane in existence, used by Bernard Moitessier to circumnavigate), but replace the windvane action by a compass and servo. Of course ultimately the sailor is always responsible for regularly verifying the course of his craft. Going down for a nap while the boat is sailing close to shore is NEVER a good idea.