The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Logic not trapping
Discuss Logic not trapping in the C Programming forum on Dev Shed. Logic not trapping C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 30th, 2012, 10:38 AM
|
|
|
|
Logic not trapping
Hi,
This code is short and sweet. It is complete as posted. The problem is this: when the value of hdg_ins exceeds M_PI*2.0 (360 degrees) or goes negative (less than 0) the bounds check fails and doesn't execute.
1) Why
2) How to fix it?
Code:
{
hdg_error = hdg_error + ((7.0/25920000.0)*M_PI);
if(hdg_error >= (M_PI*2.0)) hdg_error = hdg_error - (M_PI*2.0);
if(hdg_error < 0.0) hdg_error = hdg_error + M_PI*2;
inertial_hdg = inertial_hdg + (rateyaw * cos(-roll)) + (ratepitch * sin(-roll));
hdg_ins = (inertial_hdg * 0.025) + hdg_error;
if(hdg_ins >= (M_PI*2.0)) hdg_ins = hdg_ins - (M_PI*2.0);
if(hdg_ins < 0.0) hdg_ins = hdg_ins + (M_PI*2.0);
}
Many thanks in advance.
Best regards,
AstroTux.
|

August 30th, 2012, 01:41 PM
|
 |
Contributed User
|
|
|
|
I think you would need to post a test case we can copy/paste/run for ourselves.
Compiling a bit of code and stepping through with gdb shows nothing unexpected here.
Code:
Breakpoint 1, main () at foo.c:18
18 if(hdg_ins >= (M_PI*2.0)) hdg_ins = hdg_ins - (M_PI*2.0);
(gdb) list
13 if(hdg_error < 0.0) hdg_error = hdg_error + M_PI*2;
14
15 inertial_hdg = inertial_hdg + (rateyaw * cos(-roll)) + (ratepitch * sin(-roll));
16 hdg_ins = (inertial_hdg * 0.025) + hdg_error;
17
18 if(hdg_ins >= (M_PI*2.0)) hdg_ins = hdg_ins - (M_PI*2.0);
19 if(hdg_ins < 0.0) hdg_ins = hdg_ins + (M_PI*2.0);
20 }
21
22 return 0;
(gdb) set hdg_ins = 7
(gdb) step
19 if(hdg_ins < 0.0) hdg_ins = hdg_ins + (M_PI*2.0);
(gdb) step
22 return 0;
(gdb) print hdg_ins
$5 = 0.71681469282041377
(gdb) run
Breakpoint 1, main () at foo.c:18
18 if(hdg_ins >= (M_PI*2.0)) hdg_ins = hdg_ins - (M_PI*2.0);
(gdb) set hdg_ins = -7
(gdb) step
19 if(hdg_ins < 0.0) hdg_ins = hdg_ins + (M_PI*2.0);
(gdb) step
22 return 0;
(gdb) print hdg_ins
$6 = -0.71681469282041377
(gdb)
I set positive and negative out of range values, and it all seems fine.
The only thing I can think of is if hdg_ins is a float, and you're getting some weird rounding very close to the range limits, which results in the adjustments still being out of range.
|

August 30th, 2012, 02:15 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
It would also help if you gave us the test input values that you're using. Plus the variable declarations as salem alluded to.
|

August 30th, 2012, 03:03 PM
|
 |
Contributing User
|
|
|
|
|
use fmod, not if if if
Code:
#define PI2 (2*M_PI)
{
hdg_error += (7.0/25920000.0)*M_PI;
hdg_error = fmod(hdg_error+PI2,PI2);
inertial_hdg = inertial_hdg + (rateyaw * cos(-roll)) + (ratepitch * sin(-roll));
hdg_ins = (inertial_hdg * 0.025) + hdg_error;
hdg_ins = fmod(hdg_ins+PI2,PI2);
}
__________________
[code] Code tags[/code] are essential for python code!
|

September 7th, 2012, 10:01 AM
|
|
|
Hi,
It was the float vs. integer thing.
e.g.
Code:
double result = 1/6;
1/6 treated like an integer. Destination type is double. How can I change this default behavior?
If I have:
Code:
double result = 1.0/6.0;
I get the correct result.
Best regards,
AstroTux.
|

September 7th, 2012, 12:19 PM
|
|
Contributing User
|
|
Join Date: Apr 2012
Posts: 58
Time spent in forums: 1 Day 17 h 52 m 2 sec
Reputation Power: 2
|
|
Try this code & compare results.
Code:
#include <stdio.h>
int
main (void)
{
double result;
result = 1/6;
printf ("\nresult = %f",result);
result = 1./6.;
printf ("\nresult = %f",result);
result = (float) 1/6;
printf ("\nresult = %f",result);
result = (double) 1/6;
printf ("\nresult = %f",result);
return 0;
}
Last edited by EEmaestro : September 7th, 2012 at 12:23 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|