Determine 180 degree turn using compass

Thomas McGrath III mcgrath3 at mac.com
Sun Jan 15 13:08:18 EST 2012


OK, this is for the math experts here.

Using a working Compass app I would like to determine when the phone is turned around from an initial starting point. So I am working out the best ways to do this.
1. Start by capturing the start heading
2. Store that in a global
3. Store that the start heading has been set
4. Using headingChanged capture the currentHeading at intervals
5. Check the new currentHeading against the start heading
6. If enough of a difference is detected then count that as a lap
7. Set the new currentHeading as the global heading
8. Using headingChanged capture the currentHeading at intervals
9. Repeat until stopped
10. return total lap count

So this seems straight forward (if I haven't missed anything)
And if I start at 10 degrees and turn right 170 now I am at 180. Good. I just subtract 10 from 180 and I know I have turned at least 170 degrees and can count that as a lap (give or take) 

BUT then the new starting point may not be right since I might be mid turn when the interval fires.

And if the starting point is say 10 and I turn left 170 degrees and now I am at 200 and 10 - 200 is -190 but I have not really turned -190 but only 170 and again this may be incorrect based on when the sample interval was fired.

So, It looks to me like I need to come up with a better way to estimate the general direction I am starting from and a better  way to set the intervals. As well as figuring out the right way to handle the compass degrees when they pass 0/360.

I have never had to do this before and would like some advice and feedback on what/how to get there.

So far I started with this code after starting the compass heading capture:

global  myLapCountNumber

on headingChanged
   put iphoneCurrentHeading() into tHeading
   lapCount tHeading["heading"]
end headingChanged

on lapCount pNum
   if pNum is not "" then
      if myLapCountNumber is not "" then
         if pNum > myLapCountNumber then
            if pNum - myLapCountNumber > 150 then
               answer pNum && "-" && myLapCountNumber && "= Lap Right +"
               put pNum into myLapCountNumber
            end if
         else -- pNum is less than myLapCountNumber
            if myLapCountNumber - pNum > 150 then
               answer myLapCountNumber && "-" && pNum && "= Lap Left -"
               put pNum into myLapCountNumber
            end if
         end if
      else 
         put 0 into myLapCountNumber -- Needs changed to a sampling of the general direction
      end if
   end if 
end lapCount

-- Tom McGrath III
http://lazyriver.on-rev.com
3mcgrath at comcast.net





More information about the use-livecode mailing list