View Single Post
  #2   Spotlight this post!  
Unread 02-02-2006, 09:58
Joel J's Avatar
Joel J Joel J is offline
do you..
no team
 
Join Date: May 2001
Rookie Year: 2000
Location: San Jose, CA
Posts: 1,445
Joel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond repute
Re: Strange but...need fresh eyes

Quote:
Originally Posted by TubaMorg
We've been trying to figure this one out for a couple of days. Here is the code in question:

p1_y = (unsigned char) (((double) p1_y + (double)127)*.75);

if (p1_y > 250)
{
p1_y = (unsigned char)250;
}

PrintToScreen ("P1_y is %d\n", (int)p1_y);


The first line is a conversion we need to use to make our steering wheel paddles work. p1_y is the value sent to the drive motors. What is happening is the wheels spin forward until the upper limit (250) at which point they immediately spin backwards or stop. Output from the print line reflects this condition, increasing to 250 then converting to 1 or 127. We thought maybe the steering wheel had started to send the wrong signal at its limit, but changing the limit set point (i.e. from 250 down to 245) results in the same thing. Any ideas?
Are you overflowing?

Does p1_y go from -128 to 127, or from 0 to 255.

If its from 0 to 255, then adding 127 definitely takes you out of the range of an unsigned char.

If its from -128 to 127, then it seems that saving (127+127) to a signed char would also result in overflow.

If you want to scale by 75% you can multiply by three, then divide by four:

p1_y = 3*(p1_y + 127)/4;

No need for floats.
__________________
Joel Johnson

Division By Zero (229) Alumni, 2003-2007
RAGE (173) Alumni, 1999-2003