PDA

View Full Version : Autopilot compass help



George
- 6th May 2009, 05:44
I've been fluffing around with excel trying to make up a spreadsheet that works suitably for an auto pilot for a boat. (the spreadsheet is only used to test the formula so I can then write some code following)

I'm using a CMPS03 module with I2C and got that part working well, my problem comes is that my maths is very poor and cant figure out a simple set of rules to make it work well. If for example the set course is 180deg and the boat gets kicked off to 160deg I know I have to correct to the right by 20 degress, and correct to the left for 20 degrees if it gets kicked off to 200 degrees.

However if it's set to say a 10 degree course and it gets knocked left 20 degrees will leave the compass reading 350 then with my current maths ability it will have to do a full loop around the wrong way to come back to 10 degrees.

I was thinking about trying to make a conversion number to add to convert the set point and all subsequent readings relative to 180 deg, then I know if it's higher than 180 I turn left and lower I turn the servo right. But after 2 days of playing with ideas I havent managed it - sigh. I've nearly got it, but then I get issues with -ve numbers and slight aberrations around that 0/360 line -

Any ideas would be greatfully received

Thanks

Darrel Taylor
- 6th May 2009, 05:53
Some interesting possiblities ...

Math help - rolling average Wind Direction
http://www.picbasic.co.uk/forum/showthread.php?t=6650
<br>

ScaleRobotics
- 6th May 2009, 06:20
This is part of the code that does that for the RCAP autopilot. I think it will help you. If you still have problems, let me know.



'b2dest is bearing to destination ie direction to assigned waypoint
'cmg course made good, in this case from a gps, current course direction of vehicle
'offcourse is the difference between b2dest and cmg
'dir_steer : which direction to steer the servo

IF cmg > b2dest Then 'IF cmg > b2dest Then
dir_steer = 1
offcourse = cmg - b2dest 'offcourse = cmg - b2dest
Else
dir_steer = 0
offcourse = b2dest - cmg 'offcourse = b2dest - cmg
EndIF

offcourse = offcourse / 10 ' Get rid of the fraction

'If more than 180 degrees offcourse change direction
'to take the shortest turn to our course
IF offcourse > 180 Then
dir_steer = dir_steer ^ %1 'switch direction to steer servo
offcourse = 360 - offcourse
EndIF

George
- 6th May 2009, 21:32
Thanks heaps guys just what I needed - a slightly different approach works a treat.

George
- 6th May 2009, 21:50
I cant believe how simple that is - I must be getting senile dementia!