Go to Post we would all build robots to play "rock-paper-scissors" if FIRST told us to, cause it is just so freakin' much fun to build a robot until 2:30am every night for six weeks! - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Spotlight this post!  
Unread 12-05-2016, 13:14
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,995
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: NavX MXP Continuous Angle to Calculate Derivative

Quote:
Originally Posted by dougwilliams View Post
You should be able to use some modulo arithmetic operations to solve the jump from 0 / 360.
Quote:
Originally Posted by JamesTerm View Post
Would x=x%180.... (modulo operator) work?
modulo is not what you want here.

what you want is the shortest angle from the previous reading to the present reading, with the correct sign. The IEEERemainder function1 does this with one line of code:
Code:
shortest_angle = IEEERemainder(present-previous,360);
C# and Java both support IEEERemainder.


If your language does not support IEEERemainder, you can use this one-line function instead:
Code:
shortest_angle = (present-previous) - 360*floor(0.5+(present-previous)/360);


EDIT:

Test code:
Code:
#include <math.h>
#include <stdio.h>

double shortest_angle(double previous, double present){
	return (present-previous) - 360.0*floor(0.5+(present-previous)/360.0);}

void test(double previous, double present){
	printf("previous=%f\tpresent=%f\tshortest_angle=%f\n",previous,present,shortest_angle(previous,present));
	}

void main(void){
	test(360,0);
	test(0,360);
	test(360,1);
	test(1,360);
	test(359,0);
	test(0,359);
	test(1,359);
	test(359,1);
	test(359,-1);
	test(-1,359);
	test(3*360+1,-1);
	test(-1,3*360+1);
	}
Output:
Code:
previous=360.000000     present=0.000000        shortest_angle=0.000000
previous=0.000000       present=360.000000      shortest_angle=0.000000
previous=360.000000     present=1.000000        shortest_angle=1.000000
previous=1.000000       present=360.000000      shortest_angle=-1.000000
previous=359.000000     present=0.000000        shortest_angle=1.000000
previous=0.000000       present=359.000000      shortest_angle=-1.000000
previous=1.000000       present=359.000000      shortest_angle=-2.000000
previous=359.000000     present=1.000000        shortest_angle=2.000000
previous=359.000000     present=-1.000000       shortest_angle=0.000000
previous=-1.000000      present=359.000000      shortest_angle=0.000000
previous=1081.000000    present=-1.000000       shortest_angle=-2.000000
previous=-1.000000      present=1081.000000     shortest_angle=2.000000
1 REMAINDER function "x REM y" per IEC 60559 as specified on Page 235 Section 7.12.10.2 of ISO/IEC 9899:TC3


Last edited by Ether : 12-05-2016 at 13:29. Reason: added test code and output
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 10:37.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi