News:

Let's find out together what makes a PIC Tick!

Main Menu

Compass deviation alarm

Started by evoortman, Apr 23, 2021, 12:30 PM

Previous topic - Next topic

evoortman

Hi,

I am working on a project with a compass sensor. The sensor output is 0° to 359°.
I need to activate an alarm if my heading is more than 20° off the compass.
Let's say my desired heading is 50° ± 20° then I can just use the code below.

If heading > desired_heading + 20 Or heading < desired_heading - 20 Then
  alarm = 1
Else
  alarm = 0
EndIf

However, this code will only work if my desired heading is more than 20° from the 0° point on the compass (> = 20° and <= 340°)
But what if my desired heading is 5°?    5 - 20 = -15  which is 345° on the compass.
Even if my desired heading is 350°?  350 + 20 = 370  which is  10° on the compass.

Can anyone give me a push in the right direction how to make the calculation if the desired heading is less than 20° from the 0° point of the compass?

Thanks!

RGV250

Hi,
I have not got a lot of time to give a better answer right now but if you test for greater than 360 and if so subtract it. If the value is negative then add 360.

Bob

trastikata

#2
Hope this helps.

Dim InitialHeading As SWord
Dim MinHeading As SWord
Dim MaxHeading As SWord
Dim CurrentHeading As SWord
Dim alarm As Bit

GoSub Calculate_My_Limits

Main:   
    While 1 = 1
        If CurrentHeading > MaxHeading And CurrentHeading < MinHeading Then
            alarm = 1
        Else
            alarm = 0
        EndIf
       
        'Recalculate limits in case it is needed
        GoSub Calculate_My_Limits   
    Wend

   
   
Calculate_My_Limits:
    MinHeading = InitialHeading - 20
    If MinHeading < 0 Then
        MinHeading = 360 + MinHeading
    EndIf
   
    MaxHeading = InitialHeading + 20
    If MaxHeading > 360 Then
        MaxHeading = MaxHeading - 360
    EndIf
Return   

evoortman

Thanks. Sorry for the late replay.
I will give it a try next week.

evoortman

Hi trastikata,

It seems your code almost works correctly :-)
The alarm only works correctly if the initialheading is between 340° and 20°. If not then the alarm is never active.

If i change the AND for OR in the comparison in the while loop, then the alarm only works correctly if the initialheading is between 20° and 340°. If not then the alarm is always active.

Do you have any idea why?

Thanks for your help!

trastikata

Quote from: evoortman on May 10, 2021, 10:30 AMDo you have any idea why?

Yes, because you are working in different quadrants.

Quote from: evoortman on May 10, 2021, 10:30 AMThe alarm only works correctly if the initialheading is between 340° and 20°. If i change the AND for OR in the comparison in the while loop, then the alarm only works correctly if the initialheading is between 20° and 340°.

You asked for a push in the right direction, I gave you one. Where is the problem finishing the code - look carefully what have I chosen to quote  :)!


evoortman

It was not my intention to criticize your code. I looked at it and really didn't see why. (I still don't exactly actually)
Maybe I assumed too much that I could immediately use the code completely working in my application.

Pepe

test it


Dim InitialHeading As Word
Dim MinHeading As SWord
Dim MaxHeading As Word
Dim CurrentHeading As Word
Dim alarm As Bit

Symbol Error = 20

GoSub Calculate_My_Limits

Main:

   While 1 = 1
   
     If CurrentHeading >= Error And CurrentHeading <= 360 - Error Then
                   If CurrentHeading > MaxHeading Or CurrentHeading < MinHeading Then
                                                                                      alarm = 1
                                                                                 Else
                                                                                      alarm = 0
                   EndIf
           Else
                   If CurrentHeading > MaxHeading And CurrentHeading < MinHeading Then
                                                                                       alarm = 1
                                                                                  Else
                                                                                       alarm = 0
                   EndIf
                                   
     End If 
        'Recalculate limits in case it is needed
        GoSub Calculate_My_Limits   
    Wend

   
   
Calculate_My_Limits:

    MinHeading = InitialHeading - Error
    If MinHeading < 0 Then
        MinHeading = 360 + MinHeading
    EndIf
   
    MaxHeading = InitialHeading + Error
    If MaxHeading > 360 Then
        MaxHeading = MaxHeading - 360
    EndIf
Return

trastikata

As Pepe said, but he made a typo:

"If CurrentHeading >= Error And CurrentHeading <= 360 - Error Then"

should be

"If InitialHeading >= Error And InitialHeading <= 360 - Error Then"

Pepe